OSDN Git Service

Allow no-capture lambdas to convert to function pointer.
[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         error_at (location, "%qE in class %qT does not name a type",
2405                   id, parser->scope);
2406       else
2407         gcc_unreachable ();
2408     }
2409   cp_parser_commit_to_tentative_parse (parser);
2410 }
2411
2412 /* Check for a common situation where a type-name should be present,
2413    but is not, and issue a sensible error message.  Returns true if an
2414    invalid type-name was detected.
2415
2416    The situation handled by this function are variable declarations of the
2417    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2418    Usually, `ID' should name a type, but if we got here it means that it
2419    does not. We try to emit the best possible error message depending on
2420    how exactly the id-expression looks like.  */
2421
2422 static bool
2423 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2424 {
2425   tree id;
2426   cp_token *token = cp_lexer_peek_token (parser->lexer);
2427
2428   cp_parser_parse_tentatively (parser);
2429   id = cp_parser_id_expression (parser,
2430                                 /*template_keyword_p=*/false,
2431                                 /*check_dependency_p=*/true,
2432                                 /*template_p=*/NULL,
2433                                 /*declarator_p=*/true,
2434                                 /*optional_p=*/false);
2435   /* After the id-expression, there should be a plain identifier,
2436      otherwise this is not a simple variable declaration. Also, if
2437      the scope is dependent, we cannot do much.  */
2438   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2439       || (parser->scope && TYPE_P (parser->scope)
2440           && dependent_type_p (parser->scope))
2441       || TREE_CODE (id) == TYPE_DECL)
2442     {
2443       cp_parser_abort_tentative_parse (parser);
2444       return false;
2445     }
2446   if (!cp_parser_parse_definitely (parser))
2447     return false;
2448
2449   /* Emit a diagnostic for the invalid type.  */
2450   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2451                                         id, token->location);
2452   /* Skip to the end of the declaration; there's no point in
2453      trying to process it.  */
2454   cp_parser_skip_to_end_of_block_or_statement (parser);
2455   return true;
2456 }
2457
2458 /* Consume tokens up to, and including, the next non-nested closing `)'.
2459    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2460    are doing error recovery. Returns -1 if OR_COMMA is true and we
2461    found an unnested comma.  */
2462
2463 static int
2464 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2465                                        bool recovering,
2466                                        bool or_comma,
2467                                        bool consume_paren)
2468 {
2469   unsigned paren_depth = 0;
2470   unsigned brace_depth = 0;
2471   unsigned square_depth = 0;
2472
2473   if (recovering && !or_comma
2474       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2475     return 0;
2476
2477   while (true)
2478     {
2479       cp_token * token = cp_lexer_peek_token (parser->lexer);
2480
2481       switch (token->type)
2482         {
2483         case CPP_EOF:
2484         case CPP_PRAGMA_EOL:
2485           /* If we've run out of tokens, then there is no closing `)'.  */
2486           return 0;
2487
2488         /* This is good for lambda expression capture-lists.  */
2489         case CPP_OPEN_SQUARE:
2490           ++square_depth;
2491           break;
2492         case CPP_CLOSE_SQUARE:
2493           if (!square_depth--)
2494             return 0;
2495           break;
2496
2497         case CPP_SEMICOLON:
2498           /* This matches the processing in skip_to_end_of_statement.  */
2499           if (!brace_depth)
2500             return 0;
2501           break;
2502
2503         case CPP_OPEN_BRACE:
2504           ++brace_depth;
2505           break;
2506         case CPP_CLOSE_BRACE:
2507           if (!brace_depth--)
2508             return 0;
2509           break;
2510
2511         case CPP_COMMA:
2512           if (recovering && or_comma && !brace_depth && !paren_depth
2513               && !square_depth)
2514             return -1;
2515           break;
2516
2517         case CPP_OPEN_PAREN:
2518           if (!brace_depth)
2519             ++paren_depth;
2520           break;
2521
2522         case CPP_CLOSE_PAREN:
2523           if (!brace_depth && !paren_depth--)
2524             {
2525               if (consume_paren)
2526                 cp_lexer_consume_token (parser->lexer);
2527               return 1;
2528             }
2529           break;
2530
2531         default:
2532           break;
2533         }
2534
2535       /* Consume the token.  */
2536       cp_lexer_consume_token (parser->lexer);
2537     }
2538 }
2539
2540 /* Consume tokens until we reach the end of the current statement.
2541    Normally, that will be just before consuming a `;'.  However, if a
2542    non-nested `}' comes first, then we stop before consuming that.  */
2543
2544 static void
2545 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2546 {
2547   unsigned nesting_depth = 0;
2548
2549   while (true)
2550     {
2551       cp_token *token = cp_lexer_peek_token (parser->lexer);
2552
2553       switch (token->type)
2554         {
2555         case CPP_EOF:
2556         case CPP_PRAGMA_EOL:
2557           /* If we've run out of tokens, stop.  */
2558           return;
2559
2560         case CPP_SEMICOLON:
2561           /* If the next token is a `;', we have reached the end of the
2562              statement.  */
2563           if (!nesting_depth)
2564             return;
2565           break;
2566
2567         case CPP_CLOSE_BRACE:
2568           /* If this is a non-nested '}', stop before consuming it.
2569              That way, when confronted with something like:
2570
2571                { 3 + }
2572
2573              we stop before consuming the closing '}', even though we
2574              have not yet reached a `;'.  */
2575           if (nesting_depth == 0)
2576             return;
2577
2578           /* If it is the closing '}' for a block that we have
2579              scanned, stop -- but only after consuming the token.
2580              That way given:
2581
2582                 void f g () { ... }
2583                 typedef int I;
2584
2585              we will stop after the body of the erroneously declared
2586              function, but before consuming the following `typedef'
2587              declaration.  */
2588           if (--nesting_depth == 0)
2589             {
2590               cp_lexer_consume_token (parser->lexer);
2591               return;
2592             }
2593
2594         case CPP_OPEN_BRACE:
2595           ++nesting_depth;
2596           break;
2597
2598         default:
2599           break;
2600         }
2601
2602       /* Consume the token.  */
2603       cp_lexer_consume_token (parser->lexer);
2604     }
2605 }
2606
2607 /* This function is called at the end of a statement or declaration.
2608    If the next token is a semicolon, it is consumed; otherwise, error
2609    recovery is attempted.  */
2610
2611 static void
2612 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2613 {
2614   /* Look for the trailing `;'.  */
2615   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2616     {
2617       /* If there is additional (erroneous) input, skip to the end of
2618          the statement.  */
2619       cp_parser_skip_to_end_of_statement (parser);
2620       /* If the next token is now a `;', consume it.  */
2621       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2622         cp_lexer_consume_token (parser->lexer);
2623     }
2624 }
2625
2626 /* Skip tokens until we have consumed an entire block, or until we
2627    have consumed a non-nested `;'.  */
2628
2629 static void
2630 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2631 {
2632   int nesting_depth = 0;
2633
2634   while (nesting_depth >= 0)
2635     {
2636       cp_token *token = cp_lexer_peek_token (parser->lexer);
2637
2638       switch (token->type)
2639         {
2640         case CPP_EOF:
2641         case CPP_PRAGMA_EOL:
2642           /* If we've run out of tokens, stop.  */
2643           return;
2644
2645         case CPP_SEMICOLON:
2646           /* Stop if this is an unnested ';'. */
2647           if (!nesting_depth)
2648             nesting_depth = -1;
2649           break;
2650
2651         case CPP_CLOSE_BRACE:
2652           /* Stop if this is an unnested '}', or closes the outermost
2653              nesting level.  */
2654           nesting_depth--;
2655           if (nesting_depth < 0)
2656             return;
2657           if (!nesting_depth)
2658             nesting_depth = -1;
2659           break;
2660
2661         case CPP_OPEN_BRACE:
2662           /* Nest. */
2663           nesting_depth++;
2664           break;
2665
2666         default:
2667           break;
2668         }
2669
2670       /* Consume the token.  */
2671       cp_lexer_consume_token (parser->lexer);
2672     }
2673 }
2674
2675 /* Skip tokens until a non-nested closing curly brace is the next
2676    token, or there are no more tokens. Return true in the first case,
2677    false otherwise.  */
2678
2679 static bool
2680 cp_parser_skip_to_closing_brace (cp_parser *parser)
2681 {
2682   unsigned nesting_depth = 0;
2683
2684   while (true)
2685     {
2686       cp_token *token = cp_lexer_peek_token (parser->lexer);
2687
2688       switch (token->type)
2689         {
2690         case CPP_EOF:
2691         case CPP_PRAGMA_EOL:
2692           /* If we've run out of tokens, stop.  */
2693           return false;
2694
2695         case CPP_CLOSE_BRACE:
2696           /* If the next token is a non-nested `}', then we have reached
2697              the end of the current block.  */
2698           if (nesting_depth-- == 0)
2699             return true;
2700           break;
2701
2702         case CPP_OPEN_BRACE:
2703           /* If it the next token is a `{', then we are entering a new
2704              block.  Consume the entire block.  */
2705           ++nesting_depth;
2706           break;
2707
2708         default:
2709           break;
2710         }
2711
2712       /* Consume the token.  */
2713       cp_lexer_consume_token (parser->lexer);
2714     }
2715 }
2716
2717 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2718    parameter is the PRAGMA token, allowing us to purge the entire pragma
2719    sequence.  */
2720
2721 static void
2722 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2723 {
2724   cp_token *token;
2725
2726   parser->lexer->in_pragma = false;
2727
2728   do
2729     token = cp_lexer_consume_token (parser->lexer);
2730   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2731
2732   /* Ensure that the pragma is not parsed again.  */
2733   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2734 }
2735
2736 /* Require pragma end of line, resyncing with it as necessary.  The
2737    arguments are as for cp_parser_skip_to_pragma_eol.  */
2738
2739 static void
2740 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2741 {
2742   parser->lexer->in_pragma = false;
2743   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2744     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2745 }
2746
2747 /* This is a simple wrapper around make_typename_type. When the id is
2748    an unresolved identifier node, we can provide a superior diagnostic
2749    using cp_parser_diagnose_invalid_type_name.  */
2750
2751 static tree
2752 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2753                               tree id, location_t id_location)
2754 {
2755   tree result;
2756   if (TREE_CODE (id) == IDENTIFIER_NODE)
2757     {
2758       result = make_typename_type (scope, id, typename_type,
2759                                    /*complain=*/tf_none);
2760       if (result == error_mark_node)
2761         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2762       return result;
2763     }
2764   return make_typename_type (scope, id, typename_type, tf_error);
2765 }
2766
2767 /* This is a wrapper around the
2768    make_{pointer,ptrmem,reference}_declarator functions that decides
2769    which one to call based on the CODE and CLASS_TYPE arguments. The
2770    CODE argument should be one of the values returned by
2771    cp_parser_ptr_operator. */
2772 static cp_declarator *
2773 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2774                                     cp_cv_quals cv_qualifiers,
2775                                     cp_declarator *target)
2776 {
2777   if (code == ERROR_MARK)
2778     return cp_error_declarator;
2779
2780   if (code == INDIRECT_REF)
2781     if (class_type == NULL_TREE)
2782       return make_pointer_declarator (cv_qualifiers, target);
2783     else
2784       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2785   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2786     return make_reference_declarator (cv_qualifiers, target, false);
2787   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2788     return make_reference_declarator (cv_qualifiers, target, true);
2789   gcc_unreachable ();
2790 }
2791
2792 /* Create a new C++ parser.  */
2793
2794 static cp_parser *
2795 cp_parser_new (void)
2796 {
2797   cp_parser *parser;
2798   cp_lexer *lexer;
2799   unsigned i;
2800
2801   /* cp_lexer_new_main is called before calling ggc_alloc because
2802      cp_lexer_new_main might load a PCH file.  */
2803   lexer = cp_lexer_new_main ();
2804
2805   /* Initialize the binops_by_token so that we can get the tree
2806      directly from the token.  */
2807   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2808     binops_by_token[binops[i].token_type] = binops[i];
2809
2810   parser = GGC_CNEW (cp_parser);
2811   parser->lexer = lexer;
2812   parser->context = cp_parser_context_new (NULL);
2813
2814   /* For now, we always accept GNU extensions.  */
2815   parser->allow_gnu_extensions_p = 1;
2816
2817   /* The `>' token is a greater-than operator, not the end of a
2818      template-id.  */
2819   parser->greater_than_is_operator_p = true;
2820
2821   parser->default_arg_ok_p = true;
2822
2823   /* We are not parsing a constant-expression.  */
2824   parser->integral_constant_expression_p = false;
2825   parser->allow_non_integral_constant_expression_p = false;
2826   parser->non_integral_constant_expression_p = false;
2827
2828   /* Local variable names are not forbidden.  */
2829   parser->local_variables_forbidden_p = false;
2830
2831   /* We are not processing an `extern "C"' declaration.  */
2832   parser->in_unbraced_linkage_specification_p = false;
2833
2834   /* We are not processing a declarator.  */
2835   parser->in_declarator_p = false;
2836
2837   /* We are not processing a template-argument-list.  */
2838   parser->in_template_argument_list_p = false;
2839
2840   /* We are not in an iteration statement.  */
2841   parser->in_statement = 0;
2842
2843   /* We are not in a switch statement.  */
2844   parser->in_switch_statement_p = false;
2845
2846   /* We are not parsing a type-id inside an expression.  */
2847   parser->in_type_id_in_expr_p = false;
2848
2849   /* Declarations aren't implicitly extern "C".  */
2850   parser->implicit_extern_c = false;
2851
2852   /* String literals should be translated to the execution character set.  */
2853   parser->translate_strings_p = true;
2854
2855   /* We are not parsing a function body.  */
2856   parser->in_function_body = false;
2857
2858   /* The unparsed function queue is empty.  */
2859   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2860
2861   /* There are no classes being defined.  */
2862   parser->num_classes_being_defined = 0;
2863
2864   /* No template parameters apply.  */
2865   parser->num_template_parameter_lists = 0;
2866
2867   return parser;
2868 }
2869
2870 /* Create a cp_lexer structure which will emit the tokens in CACHE
2871    and push it onto the parser's lexer stack.  This is used for delayed
2872    parsing of in-class method bodies and default arguments, and should
2873    not be confused with tentative parsing.  */
2874 static void
2875 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2876 {
2877   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2878   lexer->next = parser->lexer;
2879   parser->lexer = lexer;
2880
2881   /* Move the current source position to that of the first token in the
2882      new lexer.  */
2883   cp_lexer_set_source_position_from_token (lexer->next_token);
2884 }
2885
2886 /* Pop the top lexer off the parser stack.  This is never used for the
2887    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2888 static void
2889 cp_parser_pop_lexer (cp_parser *parser)
2890 {
2891   cp_lexer *lexer = parser->lexer;
2892   parser->lexer = lexer->next;
2893   cp_lexer_destroy (lexer);
2894
2895   /* Put the current source position back where it was before this
2896      lexer was pushed.  */
2897   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2898 }
2899
2900 /* Lexical conventions [gram.lex]  */
2901
2902 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2903    identifier.  */
2904
2905 static tree
2906 cp_parser_identifier (cp_parser* parser)
2907 {
2908   cp_token *token;
2909
2910   /* Look for the identifier.  */
2911   token = cp_parser_require (parser, CPP_NAME, "identifier");
2912   /* Return the value.  */
2913   return token ? token->u.value : error_mark_node;
2914 }
2915
2916 /* Parse a sequence of adjacent string constants.  Returns a
2917    TREE_STRING representing the combined, nul-terminated string
2918    constant.  If TRANSLATE is true, translate the string to the
2919    execution character set.  If WIDE_OK is true, a wide string is
2920    invalid here.
2921
2922    C++98 [lex.string] says that if a narrow string literal token is
2923    adjacent to a wide string literal token, the behavior is undefined.
2924    However, C99 6.4.5p4 says that this results in a wide string literal.
2925    We follow C99 here, for consistency with the C front end.
2926
2927    This code is largely lifted from lex_string() in c-lex.c.
2928
2929    FUTURE: ObjC++ will need to handle @-strings here.  */
2930 static tree
2931 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2932 {
2933   tree value;
2934   size_t count;
2935   struct obstack str_ob;
2936   cpp_string str, istr, *strs;
2937   cp_token *tok;
2938   enum cpp_ttype type;
2939
2940   tok = cp_lexer_peek_token (parser->lexer);
2941   if (!cp_parser_is_string_literal (tok))
2942     {
2943       cp_parser_error (parser, "expected string-literal");
2944       return error_mark_node;
2945     }
2946
2947   type = tok->type;
2948
2949   /* Try to avoid the overhead of creating and destroying an obstack
2950      for the common case of just one string.  */
2951   if (!cp_parser_is_string_literal
2952       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2953     {
2954       cp_lexer_consume_token (parser->lexer);
2955
2956       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2957       str.len = TREE_STRING_LENGTH (tok->u.value);
2958       count = 1;
2959
2960       strs = &str;
2961     }
2962   else
2963     {
2964       gcc_obstack_init (&str_ob);
2965       count = 0;
2966
2967       do
2968         {
2969           cp_lexer_consume_token (parser->lexer);
2970           count++;
2971           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2972           str.len = TREE_STRING_LENGTH (tok->u.value);
2973
2974           if (type != tok->type)
2975             {
2976               if (type == CPP_STRING)
2977                 type = tok->type;
2978               else if (tok->type != CPP_STRING)
2979                 error_at (tok->location,
2980                           "unsupported non-standard concatenation "
2981                           "of string literals");
2982             }
2983
2984           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2985
2986           tok = cp_lexer_peek_token (parser->lexer);
2987         }
2988       while (cp_parser_is_string_literal (tok));
2989
2990       strs = (cpp_string *) obstack_finish (&str_ob);
2991     }
2992
2993   if (type != CPP_STRING && !wide_ok)
2994     {
2995       cp_parser_error (parser, "a wide string is invalid in this context");
2996       type = CPP_STRING;
2997     }
2998
2999   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3000       (parse_in, strs, count, &istr, type))
3001     {
3002       value = build_string (istr.len, (const char *)istr.text);
3003       free (CONST_CAST (unsigned char *, istr.text));
3004
3005       switch (type)
3006         {
3007         default:
3008         case CPP_STRING:
3009         case CPP_UTF8STRING:
3010           TREE_TYPE (value) = char_array_type_node;
3011           break;
3012         case CPP_STRING16:
3013           TREE_TYPE (value) = char16_array_type_node;
3014           break;
3015         case CPP_STRING32:
3016           TREE_TYPE (value) = char32_array_type_node;
3017           break;
3018         case CPP_WSTRING:
3019           TREE_TYPE (value) = wchar_array_type_node;
3020           break;
3021         }
3022
3023       value = fix_string_type (value);
3024     }
3025   else
3026     /* cpp_interpret_string has issued an error.  */
3027     value = error_mark_node;
3028
3029   if (count > 1)
3030     obstack_free (&str_ob, 0);
3031
3032   return value;
3033 }
3034
3035
3036 /* Basic concepts [gram.basic]  */
3037
3038 /* Parse a translation-unit.
3039
3040    translation-unit:
3041      declaration-seq [opt]
3042
3043    Returns TRUE if all went well.  */
3044
3045 static bool
3046 cp_parser_translation_unit (cp_parser* parser)
3047 {
3048   /* The address of the first non-permanent object on the declarator
3049      obstack.  */
3050   static void *declarator_obstack_base;
3051
3052   bool success;
3053
3054   /* Create the declarator obstack, if necessary.  */
3055   if (!cp_error_declarator)
3056     {
3057       gcc_obstack_init (&declarator_obstack);
3058       /* Create the error declarator.  */
3059       cp_error_declarator = make_declarator (cdk_error);
3060       /* Create the empty parameter list.  */
3061       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3062       /* Remember where the base of the declarator obstack lies.  */
3063       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3064     }
3065
3066   cp_parser_declaration_seq_opt (parser);
3067
3068   /* If there are no tokens left then all went well.  */
3069   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3070     {
3071       /* Get rid of the token array; we don't need it any more.  */
3072       cp_lexer_destroy (parser->lexer);
3073       parser->lexer = NULL;
3074
3075       /* This file might have been a context that's implicitly extern
3076          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3077       if (parser->implicit_extern_c)
3078         {
3079           pop_lang_context ();
3080           parser->implicit_extern_c = false;
3081         }
3082
3083       /* Finish up.  */
3084       finish_translation_unit ();
3085
3086       success = true;
3087     }
3088   else
3089     {
3090       cp_parser_error (parser, "expected declaration");
3091       success = false;
3092     }
3093
3094   /* Make sure the declarator obstack was fully cleaned up.  */
3095   gcc_assert (obstack_next_free (&declarator_obstack)
3096               == declarator_obstack_base);
3097
3098   /* All went well.  */
3099   return success;
3100 }
3101
3102 /* Expressions [gram.expr] */
3103
3104 /* Parse a primary-expression.
3105
3106    primary-expression:
3107      literal
3108      this
3109      ( expression )
3110      id-expression
3111
3112    GNU Extensions:
3113
3114    primary-expression:
3115      ( compound-statement )
3116      __builtin_va_arg ( assignment-expression , type-id )
3117      __builtin_offsetof ( type-id , offsetof-expression )
3118
3119    C++ Extensions:
3120      __has_nothrow_assign ( type-id )   
3121      __has_nothrow_constructor ( type-id )
3122      __has_nothrow_copy ( type-id )
3123      __has_trivial_assign ( type-id )   
3124      __has_trivial_constructor ( type-id )
3125      __has_trivial_copy ( type-id )
3126      __has_trivial_destructor ( type-id )
3127      __has_virtual_destructor ( type-id )     
3128      __is_abstract ( type-id )
3129      __is_base_of ( type-id , type-id )
3130      __is_class ( type-id )
3131      __is_convertible_to ( type-id , type-id )     
3132      __is_empty ( type-id )
3133      __is_enum ( type-id )
3134      __is_pod ( type-id )
3135      __is_polymorphic ( type-id )
3136      __is_union ( type-id )
3137
3138    Objective-C++ Extension:
3139
3140    primary-expression:
3141      objc-expression
3142
3143    literal:
3144      __null
3145
3146    ADDRESS_P is true iff this expression was immediately preceded by
3147    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3148    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3149    true iff this expression is a template argument.
3150
3151    Returns a representation of the expression.  Upon return, *IDK
3152    indicates what kind of id-expression (if any) was present.  */
3153
3154 static tree
3155 cp_parser_primary_expression (cp_parser *parser,
3156                               bool address_p,
3157                               bool cast_p,
3158                               bool template_arg_p,
3159                               cp_id_kind *idk)
3160 {
3161   cp_token *token = NULL;
3162
3163   /* Assume the primary expression is not an id-expression.  */
3164   *idk = CP_ID_KIND_NONE;
3165
3166   /* Peek at the next token.  */
3167   token = cp_lexer_peek_token (parser->lexer);
3168   switch (token->type)
3169     {
3170       /* literal:
3171            integer-literal
3172            character-literal
3173            floating-literal
3174            string-literal
3175            boolean-literal  */
3176     case CPP_CHAR:
3177     case CPP_CHAR16:
3178     case CPP_CHAR32:
3179     case CPP_WCHAR:
3180     case CPP_NUMBER:
3181       token = cp_lexer_consume_token (parser->lexer);
3182       if (TREE_CODE (token->u.value) == FIXED_CST)
3183         {
3184           error_at (token->location,
3185                     "fixed-point types not supported in C++");
3186           return error_mark_node;
3187         }
3188       /* Floating-point literals are only allowed in an integral
3189          constant expression if they are cast to an integral or
3190          enumeration type.  */
3191       if (TREE_CODE (token->u.value) == REAL_CST
3192           && parser->integral_constant_expression_p
3193           && pedantic)
3194         {
3195           /* CAST_P will be set even in invalid code like "int(2.7 +
3196              ...)".   Therefore, we have to check that the next token
3197              is sure to end the cast.  */
3198           if (cast_p)
3199             {
3200               cp_token *next_token;
3201
3202               next_token = cp_lexer_peek_token (parser->lexer);
3203               if (/* The comma at the end of an
3204                      enumerator-definition.  */
3205                   next_token->type != CPP_COMMA
3206                   /* The curly brace at the end of an enum-specifier.  */
3207                   && next_token->type != CPP_CLOSE_BRACE
3208                   /* The end of a statement.  */
3209                   && next_token->type != CPP_SEMICOLON
3210                   /* The end of the cast-expression.  */
3211                   && next_token->type != CPP_CLOSE_PAREN
3212                   /* The end of an array bound.  */
3213                   && next_token->type != CPP_CLOSE_SQUARE
3214                   /* The closing ">" in a template-argument-list.  */
3215                   && (next_token->type != CPP_GREATER
3216                       || parser->greater_than_is_operator_p)
3217                   /* C++0x only: A ">>" treated like two ">" tokens,
3218                      in a template-argument-list.  */
3219                   && (next_token->type != CPP_RSHIFT
3220                       || (cxx_dialect == cxx98)
3221                       || parser->greater_than_is_operator_p))
3222                 cast_p = false;
3223             }
3224
3225           /* If we are within a cast, then the constraint that the
3226              cast is to an integral or enumeration type will be
3227              checked at that point.  If we are not within a cast, then
3228              this code is invalid.  */
3229           if (!cast_p)
3230             cp_parser_non_integral_constant_expression
3231               (parser, "floating-point literal");
3232         }
3233       return token->u.value;
3234
3235     case CPP_STRING:
3236     case CPP_STRING16:
3237     case CPP_STRING32:
3238     case CPP_WSTRING:
3239     case CPP_UTF8STRING:
3240       /* ??? Should wide strings be allowed when parser->translate_strings_p
3241          is false (i.e. in attributes)?  If not, we can kill the third
3242          argument to cp_parser_string_literal.  */
3243       return cp_parser_string_literal (parser,
3244                                        parser->translate_strings_p,
3245                                        true);
3246
3247     case CPP_OPEN_PAREN:
3248       {
3249         tree expr;
3250         bool saved_greater_than_is_operator_p;
3251
3252         /* Consume the `('.  */
3253         cp_lexer_consume_token (parser->lexer);
3254         /* Within a parenthesized expression, a `>' token is always
3255            the greater-than operator.  */
3256         saved_greater_than_is_operator_p
3257           = parser->greater_than_is_operator_p;
3258         parser->greater_than_is_operator_p = true;
3259         /* If we see `( { ' then we are looking at the beginning of
3260            a GNU statement-expression.  */
3261         if (cp_parser_allow_gnu_extensions_p (parser)
3262             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3263           {
3264             /* Statement-expressions are not allowed by the standard.  */
3265             pedwarn (token->location, OPT_pedantic, 
3266                      "ISO C++ forbids braced-groups within expressions");
3267
3268             /* And they're not allowed outside of a function-body; you
3269                cannot, for example, write:
3270
3271                  int i = ({ int j = 3; j + 1; });
3272
3273                at class or namespace scope.  */
3274             if (!parser->in_function_body
3275                 || parser->in_template_argument_list_p)
3276               {
3277                 error_at (token->location,
3278                           "statement-expressions are not allowed outside "
3279                           "functions nor in template-argument lists");
3280                 cp_parser_skip_to_end_of_block_or_statement (parser);
3281                 expr = error_mark_node;
3282               }
3283             else
3284               {
3285                 /* Start the statement-expression.  */
3286                 expr = begin_stmt_expr ();
3287                 /* Parse the compound-statement.  */
3288                 cp_parser_compound_statement (parser, expr, false);
3289                 /* Finish up.  */
3290                 expr = finish_stmt_expr (expr, false);
3291               }
3292           }
3293         else
3294           {
3295             /* Parse the parenthesized expression.  */
3296             expr = cp_parser_expression (parser, cast_p, idk);
3297             /* Let the front end know that this expression was
3298                enclosed in parentheses. This matters in case, for
3299                example, the expression is of the form `A::B', since
3300                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3301                not.  */
3302             finish_parenthesized_expr (expr);
3303           }
3304         /* The `>' token might be the end of a template-id or
3305            template-parameter-list now.  */
3306         parser->greater_than_is_operator_p
3307           = saved_greater_than_is_operator_p;
3308         /* Consume the `)'.  */
3309         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3310           cp_parser_skip_to_end_of_statement (parser);
3311
3312         return expr;
3313       }
3314
3315     case CPP_OPEN_SQUARE:
3316       if (c_dialect_objc ())
3317         /* We have an Objective-C++ message. */
3318         return cp_parser_objc_expression (parser);
3319       maybe_warn_cpp0x ("lambda expressions");
3320       return cp_parser_lambda_expression (parser);
3321
3322     case CPP_OBJC_STRING:
3323       if (c_dialect_objc ())
3324         /* We have an Objective-C++ string literal. */
3325         return cp_parser_objc_expression (parser);
3326       cp_parser_error (parser, "expected primary-expression");
3327       return error_mark_node;
3328
3329     case CPP_KEYWORD:
3330       switch (token->keyword)
3331         {
3332           /* These two are the boolean literals.  */
3333         case RID_TRUE:
3334           cp_lexer_consume_token (parser->lexer);
3335           return boolean_true_node;
3336         case RID_FALSE:
3337           cp_lexer_consume_token (parser->lexer);
3338           return boolean_false_node;
3339
3340           /* The `__null' literal.  */
3341         case RID_NULL:
3342           cp_lexer_consume_token (parser->lexer);
3343           return null_node;
3344
3345           /* Recognize the `this' keyword.  */
3346         case RID_THIS:
3347           cp_lexer_consume_token (parser->lexer);
3348           if (parser->local_variables_forbidden_p)
3349             {
3350               error_at (token->location,
3351                         "%<this%> may not be used in this context");
3352               return error_mark_node;
3353             }
3354           /* Pointers cannot appear in constant-expressions.  */
3355           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3356             return error_mark_node;
3357           return finish_this_expr ();
3358
3359           /* The `operator' keyword can be the beginning of an
3360              id-expression.  */
3361         case RID_OPERATOR:
3362           goto id_expression;
3363
3364         case RID_FUNCTION_NAME:
3365         case RID_PRETTY_FUNCTION_NAME:
3366         case RID_C99_FUNCTION_NAME:
3367           {
3368             const char *name;
3369
3370             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3371                __func__ are the names of variables -- but they are
3372                treated specially.  Therefore, they are handled here,
3373                rather than relying on the generic id-expression logic
3374                below.  Grammatically, these names are id-expressions.
3375
3376                Consume the token.  */
3377             token = cp_lexer_consume_token (parser->lexer);
3378
3379             switch (token->keyword)
3380               {
3381               case RID_FUNCTION_NAME:
3382                 name = "%<__FUNCTION__%>";
3383                 break;
3384               case RID_PRETTY_FUNCTION_NAME:
3385                 name = "%<__PRETTY_FUNCTION__%>";
3386                 break;
3387               case RID_C99_FUNCTION_NAME:
3388                 name = "%<__func__%>";
3389                 break;
3390               default:
3391                 gcc_unreachable ();
3392               }
3393
3394             if (cp_parser_non_integral_constant_expression (parser, name))
3395               return error_mark_node;
3396
3397             /* Look up the name.  */
3398             return finish_fname (token->u.value);
3399           }
3400
3401         case RID_VA_ARG:
3402           {
3403             tree expression;
3404             tree type;
3405
3406             /* The `__builtin_va_arg' construct is used to handle
3407                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3408             cp_lexer_consume_token (parser->lexer);
3409             /* Look for the opening `('.  */
3410             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3411             /* Now, parse the assignment-expression.  */
3412             expression = cp_parser_assignment_expression (parser,
3413                                                           /*cast_p=*/false, NULL);
3414             /* Look for the `,'.  */
3415             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3416             /* Parse the type-id.  */
3417             type = cp_parser_type_id (parser);
3418             /* Look for the closing `)'.  */
3419             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3420             /* Using `va_arg' in a constant-expression is not
3421                allowed.  */
3422             if (cp_parser_non_integral_constant_expression (parser,
3423                                                             "%<va_arg%>"))
3424               return error_mark_node;
3425             return build_x_va_arg (expression, type);
3426           }
3427
3428         case RID_OFFSETOF:
3429           return cp_parser_builtin_offsetof (parser);
3430
3431         case RID_HAS_NOTHROW_ASSIGN:
3432         case RID_HAS_NOTHROW_CONSTRUCTOR:
3433         case RID_HAS_NOTHROW_COPY:        
3434         case RID_HAS_TRIVIAL_ASSIGN:
3435         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3436         case RID_HAS_TRIVIAL_COPY:        
3437         case RID_HAS_TRIVIAL_DESTRUCTOR:
3438         case RID_HAS_VIRTUAL_DESTRUCTOR:
3439         case RID_IS_ABSTRACT:
3440         case RID_IS_BASE_OF:
3441         case RID_IS_CLASS:
3442         case RID_IS_CONVERTIBLE_TO:
3443         case RID_IS_EMPTY:
3444         case RID_IS_ENUM:
3445         case RID_IS_POD:
3446         case RID_IS_POLYMORPHIC:
3447         case RID_IS_STD_LAYOUT:
3448         case RID_IS_TRIVIAL:
3449         case RID_IS_UNION:
3450           return cp_parser_trait_expr (parser, token->keyword);
3451
3452         /* Objective-C++ expressions.  */
3453         case RID_AT_ENCODE:
3454         case RID_AT_PROTOCOL:
3455         case RID_AT_SELECTOR:
3456           return cp_parser_objc_expression (parser);
3457
3458         default:
3459           cp_parser_error (parser, "expected primary-expression");
3460           return error_mark_node;
3461         }
3462
3463       /* An id-expression can start with either an identifier, a
3464          `::' as the beginning of a qualified-id, or the "operator"
3465          keyword.  */
3466     case CPP_NAME:
3467     case CPP_SCOPE:
3468     case CPP_TEMPLATE_ID:
3469     case CPP_NESTED_NAME_SPECIFIER:
3470       {
3471         tree id_expression;
3472         tree decl;
3473         const char *error_msg;
3474         bool template_p;
3475         bool done;
3476         cp_token *id_expr_token;
3477
3478       id_expression:
3479         /* Parse the id-expression.  */
3480         id_expression
3481           = cp_parser_id_expression (parser,
3482                                      /*template_keyword_p=*/false,
3483                                      /*check_dependency_p=*/true,
3484                                      &template_p,
3485                                      /*declarator_p=*/false,
3486                                      /*optional_p=*/false);
3487         if (id_expression == error_mark_node)
3488           return error_mark_node;
3489         id_expr_token = token;
3490         token = cp_lexer_peek_token (parser->lexer);
3491         done = (token->type != CPP_OPEN_SQUARE
3492                 && token->type != CPP_OPEN_PAREN
3493                 && token->type != CPP_DOT
3494                 && token->type != CPP_DEREF
3495                 && token->type != CPP_PLUS_PLUS
3496                 && token->type != CPP_MINUS_MINUS);
3497         /* If we have a template-id, then no further lookup is
3498            required.  If the template-id was for a template-class, we
3499            will sometimes have a TYPE_DECL at this point.  */
3500         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3501                  || TREE_CODE (id_expression) == TYPE_DECL)
3502           decl = id_expression;
3503         /* Look up the name.  */
3504         else
3505           {
3506             tree ambiguous_decls;
3507
3508             decl = cp_parser_lookup_name (parser, id_expression,
3509                                           none_type,
3510                                           template_p,
3511                                           /*is_namespace=*/false,
3512                                           /*check_dependency=*/true,
3513                                           &ambiguous_decls,
3514                                           id_expr_token->location);
3515             /* If the lookup was ambiguous, an error will already have
3516                been issued.  */
3517             if (ambiguous_decls)
3518               return error_mark_node;
3519
3520             /* In Objective-C++, an instance variable (ivar) may be preferred
3521                to whatever cp_parser_lookup_name() found.  */
3522             decl = objc_lookup_ivar (decl, id_expression);
3523
3524             /* If name lookup gives us a SCOPE_REF, then the
3525                qualifying scope was dependent.  */
3526             if (TREE_CODE (decl) == SCOPE_REF)
3527               {
3528                 /* At this point, we do not know if DECL is a valid
3529                    integral constant expression.  We assume that it is
3530                    in fact such an expression, so that code like:
3531
3532                       template <int N> struct A {
3533                         int a[B<N>::i];
3534                       };
3535                      
3536                    is accepted.  At template-instantiation time, we
3537                    will check that B<N>::i is actually a constant.  */
3538                 return decl;
3539               }
3540             /* Check to see if DECL is a local variable in a context
3541                where that is forbidden.  */
3542             if (parser->local_variables_forbidden_p
3543                 && local_variable_p (decl))
3544               {
3545                 /* It might be that we only found DECL because we are
3546                    trying to be generous with pre-ISO scoping rules.
3547                    For example, consider:
3548
3549                      int i;
3550                      void g() {
3551                        for (int i = 0; i < 10; ++i) {}
3552                        extern void f(int j = i);
3553                      }
3554
3555                    Here, name look up will originally find the out
3556                    of scope `i'.  We need to issue a warning message,
3557                    but then use the global `i'.  */
3558                 decl = check_for_out_of_scope_variable (decl);
3559                 if (local_variable_p (decl))
3560                   {
3561                     error_at (id_expr_token->location,
3562                               "local variable %qD may not appear in this context",
3563                               decl);
3564                     return error_mark_node;
3565                   }
3566               }
3567           }
3568
3569         decl = (finish_id_expression
3570                 (id_expression, decl, parser->scope,
3571                  idk,
3572                  parser->integral_constant_expression_p,
3573                  parser->allow_non_integral_constant_expression_p,
3574                  &parser->non_integral_constant_expression_p,
3575                  template_p, done, address_p,
3576                  template_arg_p,
3577                  &error_msg,
3578                  id_expr_token->location));
3579         if (error_msg)
3580           cp_parser_error (parser, error_msg);
3581         return decl;
3582       }
3583
3584       /* Anything else is an error.  */
3585     default:
3586       cp_parser_error (parser, "expected primary-expression");
3587       return error_mark_node;
3588     }
3589 }
3590
3591 /* Parse an id-expression.
3592
3593    id-expression:
3594      unqualified-id
3595      qualified-id
3596
3597    qualified-id:
3598      :: [opt] nested-name-specifier template [opt] unqualified-id
3599      :: identifier
3600      :: operator-function-id
3601      :: template-id
3602
3603    Return a representation of the unqualified portion of the
3604    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3605    a `::' or nested-name-specifier.
3606
3607    Often, if the id-expression was a qualified-id, the caller will
3608    want to make a SCOPE_REF to represent the qualified-id.  This
3609    function does not do this in order to avoid wastefully creating
3610    SCOPE_REFs when they are not required.
3611
3612    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3613    `template' keyword.
3614
3615    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3616    uninstantiated templates.
3617
3618    If *TEMPLATE_P is non-NULL, it is set to true iff the
3619    `template' keyword is used to explicitly indicate that the entity
3620    named is a template.
3621
3622    If DECLARATOR_P is true, the id-expression is appearing as part of
3623    a declarator, rather than as part of an expression.  */
3624
3625 static tree
3626 cp_parser_id_expression (cp_parser *parser,
3627                          bool template_keyword_p,
3628                          bool check_dependency_p,
3629                          bool *template_p,
3630                          bool declarator_p,
3631                          bool optional_p)
3632 {
3633   bool global_scope_p;
3634   bool nested_name_specifier_p;
3635
3636   /* Assume the `template' keyword was not used.  */
3637   if (template_p)
3638     *template_p = template_keyword_p;
3639
3640   /* Look for the optional `::' operator.  */
3641   global_scope_p
3642     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3643        != NULL_TREE);
3644   /* Look for the optional nested-name-specifier.  */
3645   nested_name_specifier_p
3646     = (cp_parser_nested_name_specifier_opt (parser,
3647                                             /*typename_keyword_p=*/false,
3648                                             check_dependency_p,
3649                                             /*type_p=*/false,
3650                                             declarator_p)
3651        != NULL_TREE);
3652   /* If there is a nested-name-specifier, then we are looking at
3653      the first qualified-id production.  */
3654   if (nested_name_specifier_p)
3655     {
3656       tree saved_scope;
3657       tree saved_object_scope;
3658       tree saved_qualifying_scope;
3659       tree unqualified_id;
3660       bool is_template;
3661
3662       /* See if the next token is the `template' keyword.  */
3663       if (!template_p)
3664         template_p = &is_template;
3665       *template_p = cp_parser_optional_template_keyword (parser);
3666       /* Name lookup we do during the processing of the
3667          unqualified-id might obliterate SCOPE.  */
3668       saved_scope = parser->scope;
3669       saved_object_scope = parser->object_scope;
3670       saved_qualifying_scope = parser->qualifying_scope;
3671       /* Process the final unqualified-id.  */
3672       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3673                                                  check_dependency_p,
3674                                                  declarator_p,
3675                                                  /*optional_p=*/false);
3676       /* Restore the SAVED_SCOPE for our caller.  */
3677       parser->scope = saved_scope;
3678       parser->object_scope = saved_object_scope;
3679       parser->qualifying_scope = saved_qualifying_scope;
3680
3681       return unqualified_id;
3682     }
3683   /* Otherwise, if we are in global scope, then we are looking at one
3684      of the other qualified-id productions.  */
3685   else if (global_scope_p)
3686     {
3687       cp_token *token;
3688       tree id;
3689
3690       /* Peek at the next token.  */
3691       token = cp_lexer_peek_token (parser->lexer);
3692
3693       /* If it's an identifier, and the next token is not a "<", then
3694          we can avoid the template-id case.  This is an optimization
3695          for this common case.  */
3696       if (token->type == CPP_NAME
3697           && !cp_parser_nth_token_starts_template_argument_list_p
3698                (parser, 2))
3699         return cp_parser_identifier (parser);
3700
3701       cp_parser_parse_tentatively (parser);
3702       /* Try a template-id.  */
3703       id = cp_parser_template_id (parser,
3704                                   /*template_keyword_p=*/false,
3705                                   /*check_dependency_p=*/true,
3706                                   declarator_p);
3707       /* If that worked, we're done.  */
3708       if (cp_parser_parse_definitely (parser))
3709         return id;
3710
3711       /* Peek at the next token.  (Changes in the token buffer may
3712          have invalidated the pointer obtained above.)  */
3713       token = cp_lexer_peek_token (parser->lexer);
3714
3715       switch (token->type)
3716         {
3717         case CPP_NAME:
3718           return cp_parser_identifier (parser);
3719
3720         case CPP_KEYWORD:
3721           if (token->keyword == RID_OPERATOR)
3722             return cp_parser_operator_function_id (parser);
3723           /* Fall through.  */
3724
3725         default:
3726           cp_parser_error (parser, "expected id-expression");
3727           return error_mark_node;
3728         }
3729     }
3730   else
3731     return cp_parser_unqualified_id (parser, template_keyword_p,
3732                                      /*check_dependency_p=*/true,
3733                                      declarator_p,
3734                                      optional_p);
3735 }
3736
3737 /* Parse an unqualified-id.
3738
3739    unqualified-id:
3740      identifier
3741      operator-function-id
3742      conversion-function-id
3743      ~ class-name
3744      template-id
3745
3746    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3747    keyword, in a construct like `A::template ...'.
3748
3749    Returns a representation of unqualified-id.  For the `identifier'
3750    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3751    production a BIT_NOT_EXPR is returned; the operand of the
3752    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3753    other productions, see the documentation accompanying the
3754    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3755    names are looked up in uninstantiated templates.  If DECLARATOR_P
3756    is true, the unqualified-id is appearing as part of a declarator,
3757    rather than as part of an expression.  */
3758
3759 static tree
3760 cp_parser_unqualified_id (cp_parser* parser,
3761                           bool template_keyword_p,
3762                           bool check_dependency_p,
3763                           bool declarator_p,
3764                           bool optional_p)
3765 {
3766   cp_token *token;
3767
3768   /* Peek at the next token.  */
3769   token = cp_lexer_peek_token (parser->lexer);
3770
3771   switch (token->type)
3772     {
3773     case CPP_NAME:
3774       {
3775         tree id;
3776
3777         /* We don't know yet whether or not this will be a
3778            template-id.  */
3779         cp_parser_parse_tentatively (parser);
3780         /* Try a template-id.  */
3781         id = cp_parser_template_id (parser, template_keyword_p,
3782                                     check_dependency_p,
3783                                     declarator_p);
3784         /* If it worked, we're done.  */
3785         if (cp_parser_parse_definitely (parser))
3786           return id;
3787         /* Otherwise, it's an ordinary identifier.  */
3788         return cp_parser_identifier (parser);
3789       }
3790
3791     case CPP_TEMPLATE_ID:
3792       return cp_parser_template_id (parser, template_keyword_p,
3793                                     check_dependency_p,
3794                                     declarator_p);
3795
3796     case CPP_COMPL:
3797       {
3798         tree type_decl;
3799         tree qualifying_scope;
3800         tree object_scope;
3801         tree scope;
3802         bool done;
3803
3804         /* Consume the `~' token.  */
3805         cp_lexer_consume_token (parser->lexer);
3806         /* Parse the class-name.  The standard, as written, seems to
3807            say that:
3808
3809              template <typename T> struct S { ~S (); };
3810              template <typename T> S<T>::~S() {}
3811
3812            is invalid, since `~' must be followed by a class-name, but
3813            `S<T>' is dependent, and so not known to be a class.
3814            That's not right; we need to look in uninstantiated
3815            templates.  A further complication arises from:
3816
3817              template <typename T> void f(T t) {
3818                t.T::~T();
3819              }
3820
3821            Here, it is not possible to look up `T' in the scope of `T'
3822            itself.  We must look in both the current scope, and the
3823            scope of the containing complete expression.
3824
3825            Yet another issue is:
3826
3827              struct S {
3828                int S;
3829                ~S();
3830              };
3831
3832              S::~S() {}
3833
3834            The standard does not seem to say that the `S' in `~S'
3835            should refer to the type `S' and not the data member
3836            `S::S'.  */
3837
3838         /* DR 244 says that we look up the name after the "~" in the
3839            same scope as we looked up the qualifying name.  That idea
3840            isn't fully worked out; it's more complicated than that.  */
3841         scope = parser->scope;
3842         object_scope = parser->object_scope;
3843         qualifying_scope = parser->qualifying_scope;
3844
3845         /* Check for invalid scopes.  */
3846         if (scope == error_mark_node)
3847           {
3848             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3849               cp_lexer_consume_token (parser->lexer);
3850             return error_mark_node;
3851           }
3852         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3853           {
3854             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3855               error_at (token->location,
3856                         "scope %qT before %<~%> is not a class-name",
3857                         scope);
3858             cp_parser_simulate_error (parser);
3859             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3860               cp_lexer_consume_token (parser->lexer);
3861             return error_mark_node;
3862           }
3863         gcc_assert (!scope || TYPE_P (scope));
3864
3865         /* If the name is of the form "X::~X" it's OK.  */
3866         token = cp_lexer_peek_token (parser->lexer);
3867         if (scope
3868             && token->type == CPP_NAME
3869             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3870                 == CPP_OPEN_PAREN)
3871             && constructor_name_p (token->u.value, scope))
3872           {
3873             cp_lexer_consume_token (parser->lexer);
3874             return build_nt (BIT_NOT_EXPR, scope);
3875           }
3876
3877         /* If there was an explicit qualification (S::~T), first look
3878            in the scope given by the qualification (i.e., S).  */
3879         done = false;
3880         type_decl = NULL_TREE;
3881         if (scope)
3882           {
3883             cp_parser_parse_tentatively (parser);
3884             type_decl = cp_parser_class_name (parser,
3885                                               /*typename_keyword_p=*/false,
3886                                               /*template_keyword_p=*/false,
3887                                               none_type,
3888                                               /*check_dependency=*/false,
3889                                               /*class_head_p=*/false,
3890                                               declarator_p);
3891             if (cp_parser_parse_definitely (parser))
3892               done = true;
3893           }
3894         /* In "N::S::~S", look in "N" as well.  */
3895         if (!done && scope && qualifying_scope)
3896           {
3897             cp_parser_parse_tentatively (parser);
3898             parser->scope = qualifying_scope;
3899             parser->object_scope = NULL_TREE;
3900             parser->qualifying_scope = NULL_TREE;
3901             type_decl
3902               = cp_parser_class_name (parser,
3903                                       /*typename_keyword_p=*/false,
3904                                       /*template_keyword_p=*/false,
3905                                       none_type,
3906                                       /*check_dependency=*/false,
3907                                       /*class_head_p=*/false,
3908                                       declarator_p);
3909             if (cp_parser_parse_definitely (parser))
3910               done = true;
3911           }
3912         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3913         else if (!done && object_scope)
3914           {
3915             cp_parser_parse_tentatively (parser);
3916             parser->scope = object_scope;
3917             parser->object_scope = NULL_TREE;
3918             parser->qualifying_scope = NULL_TREE;
3919             type_decl
3920               = cp_parser_class_name (parser,
3921                                       /*typename_keyword_p=*/false,
3922                                       /*template_keyword_p=*/false,
3923                                       none_type,
3924                                       /*check_dependency=*/false,
3925                                       /*class_head_p=*/false,
3926                                       declarator_p);
3927             if (cp_parser_parse_definitely (parser))
3928               done = true;
3929           }
3930         /* Look in the surrounding context.  */
3931         if (!done)
3932           {
3933             parser->scope = NULL_TREE;
3934             parser->object_scope = NULL_TREE;
3935             parser->qualifying_scope = NULL_TREE;
3936             if (processing_template_decl)
3937               cp_parser_parse_tentatively (parser);
3938             type_decl
3939               = cp_parser_class_name (parser,
3940                                       /*typename_keyword_p=*/false,
3941                                       /*template_keyword_p=*/false,
3942                                       none_type,
3943                                       /*check_dependency=*/false,
3944                                       /*class_head_p=*/false,
3945                                       declarator_p);
3946             if (processing_template_decl
3947                 && ! cp_parser_parse_definitely (parser))
3948               {
3949                 /* We couldn't find a type with this name, so just accept
3950                    it and check for a match at instantiation time.  */
3951                 type_decl = cp_parser_identifier (parser);
3952                 if (type_decl != error_mark_node)
3953                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3954                 return type_decl;
3955               }
3956           }
3957         /* If an error occurred, assume that the name of the
3958            destructor is the same as the name of the qualifying
3959            class.  That allows us to keep parsing after running
3960            into ill-formed destructor names.  */
3961         if (type_decl == error_mark_node && scope)
3962           return build_nt (BIT_NOT_EXPR, scope);
3963         else if (type_decl == error_mark_node)
3964           return error_mark_node;
3965
3966         /* Check that destructor name and scope match.  */
3967         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3968           {
3969             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3970               error_at (token->location,
3971                         "declaration of %<~%T%> as member of %qT",
3972                         type_decl, scope);
3973             cp_parser_simulate_error (parser);
3974             return error_mark_node;
3975           }
3976
3977         /* [class.dtor]
3978
3979            A typedef-name that names a class shall not be used as the
3980            identifier in the declarator for a destructor declaration.  */
3981         if (declarator_p
3982             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3983             && !DECL_SELF_REFERENCE_P (type_decl)
3984             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3985           error_at (token->location,
3986                     "typedef-name %qD used as destructor declarator",
3987                     type_decl);
3988
3989         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3990       }
3991
3992     case CPP_KEYWORD:
3993       if (token->keyword == RID_OPERATOR)
3994         {
3995           tree id;
3996
3997           /* This could be a template-id, so we try that first.  */
3998           cp_parser_parse_tentatively (parser);
3999           /* Try a template-id.  */
4000           id = cp_parser_template_id (parser, template_keyword_p,
4001                                       /*check_dependency_p=*/true,
4002                                       declarator_p);
4003           /* If that worked, we're done.  */
4004           if (cp_parser_parse_definitely (parser))
4005             return id;
4006           /* We still don't know whether we're looking at an
4007              operator-function-id or a conversion-function-id.  */
4008           cp_parser_parse_tentatively (parser);
4009           /* Try an operator-function-id.  */
4010           id = cp_parser_operator_function_id (parser);
4011           /* If that didn't work, try a conversion-function-id.  */
4012           if (!cp_parser_parse_definitely (parser))
4013             id = cp_parser_conversion_function_id (parser);
4014
4015           return id;
4016         }
4017       /* Fall through.  */
4018
4019     default:
4020       if (optional_p)
4021         return NULL_TREE;
4022       cp_parser_error (parser, "expected unqualified-id");
4023       return error_mark_node;
4024     }
4025 }
4026
4027 /* Parse an (optional) nested-name-specifier.
4028
4029    nested-name-specifier: [C++98]
4030      class-or-namespace-name :: nested-name-specifier [opt]
4031      class-or-namespace-name :: template nested-name-specifier [opt]
4032
4033    nested-name-specifier: [C++0x]
4034      type-name ::
4035      namespace-name ::
4036      nested-name-specifier identifier ::
4037      nested-name-specifier template [opt] simple-template-id ::
4038
4039    PARSER->SCOPE should be set appropriately before this function is
4040    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4041    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4042    in name lookups.
4043
4044    Sets PARSER->SCOPE to the class (TYPE) or namespace
4045    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4046    it unchanged if there is no nested-name-specifier.  Returns the new
4047    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4048
4049    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4050    part of a declaration and/or decl-specifier.  */
4051
4052 static tree
4053 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4054                                      bool typename_keyword_p,
4055                                      bool check_dependency_p,
4056                                      bool type_p,
4057                                      bool is_declaration)
4058 {
4059   bool success = false;
4060   cp_token_position start = 0;
4061   cp_token *token;
4062
4063   /* Remember where the nested-name-specifier starts.  */
4064   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4065     {
4066       start = cp_lexer_token_position (parser->lexer, false);
4067       push_deferring_access_checks (dk_deferred);
4068     }
4069
4070   while (true)
4071     {
4072       tree new_scope;
4073       tree old_scope;
4074       tree saved_qualifying_scope;
4075       bool template_keyword_p;
4076
4077       /* Spot cases that cannot be the beginning of a
4078          nested-name-specifier.  */
4079       token = cp_lexer_peek_token (parser->lexer);
4080
4081       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4082          the already parsed nested-name-specifier.  */
4083       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4084         {
4085           /* Grab the nested-name-specifier and continue the loop.  */
4086           cp_parser_pre_parsed_nested_name_specifier (parser);
4087           /* If we originally encountered this nested-name-specifier
4088              with IS_DECLARATION set to false, we will not have
4089              resolved TYPENAME_TYPEs, so we must do so here.  */
4090           if (is_declaration
4091               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4092             {
4093               new_scope = resolve_typename_type (parser->scope,
4094                                                  /*only_current_p=*/false);
4095               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4096                 parser->scope = new_scope;
4097             }
4098           success = true;
4099           continue;
4100         }
4101
4102       /* Spot cases that cannot be the beginning of a
4103          nested-name-specifier.  On the second and subsequent times
4104          through the loop, we look for the `template' keyword.  */
4105       if (success && token->keyword == RID_TEMPLATE)
4106         ;
4107       /* A template-id can start a nested-name-specifier.  */
4108       else if (token->type == CPP_TEMPLATE_ID)
4109         ;
4110       else
4111         {
4112           /* If the next token is not an identifier, then it is
4113              definitely not a type-name or namespace-name.  */
4114           if (token->type != CPP_NAME)
4115             break;
4116           /* If the following token is neither a `<' (to begin a
4117              template-id), nor a `::', then we are not looking at a
4118              nested-name-specifier.  */
4119           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4120           if (token->type != CPP_SCOPE
4121               && !cp_parser_nth_token_starts_template_argument_list_p
4122                   (parser, 2))
4123             break;
4124         }
4125
4126       /* The nested-name-specifier is optional, so we parse
4127          tentatively.  */
4128       cp_parser_parse_tentatively (parser);
4129
4130       /* Look for the optional `template' keyword, if this isn't the
4131          first time through the loop.  */
4132       if (success)
4133         template_keyword_p = cp_parser_optional_template_keyword (parser);
4134       else
4135         template_keyword_p = false;
4136
4137       /* Save the old scope since the name lookup we are about to do
4138          might destroy it.  */
4139       old_scope = parser->scope;
4140       saved_qualifying_scope = parser->qualifying_scope;
4141       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4142          look up names in "X<T>::I" in order to determine that "Y" is
4143          a template.  So, if we have a typename at this point, we make
4144          an effort to look through it.  */
4145       if (is_declaration
4146           && !typename_keyword_p
4147           && parser->scope
4148           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4149         parser->scope = resolve_typename_type (parser->scope,
4150                                                /*only_current_p=*/false);
4151       /* Parse the qualifying entity.  */
4152       new_scope
4153         = cp_parser_qualifying_entity (parser,
4154                                        typename_keyword_p,
4155                                        template_keyword_p,
4156                                        check_dependency_p,
4157                                        type_p,
4158                                        is_declaration);
4159       /* Look for the `::' token.  */
4160       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4161
4162       /* If we found what we wanted, we keep going; otherwise, we're
4163          done.  */
4164       if (!cp_parser_parse_definitely (parser))
4165         {
4166           bool error_p = false;
4167
4168           /* Restore the OLD_SCOPE since it was valid before the
4169              failed attempt at finding the last
4170              class-or-namespace-name.  */
4171           parser->scope = old_scope;
4172           parser->qualifying_scope = saved_qualifying_scope;
4173           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4174             break;
4175           /* If the next token is an identifier, and the one after
4176              that is a `::', then any valid interpretation would have
4177              found a class-or-namespace-name.  */
4178           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4179                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4180                      == CPP_SCOPE)
4181                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4182                      != CPP_COMPL))
4183             {
4184               token = cp_lexer_consume_token (parser->lexer);
4185               if (!error_p)
4186                 {
4187                   if (!token->ambiguous_p)
4188                     {
4189                       tree decl;
4190                       tree ambiguous_decls;
4191
4192                       decl = cp_parser_lookup_name (parser, token->u.value,
4193                                                     none_type,
4194                                                     /*is_template=*/false,
4195                                                     /*is_namespace=*/false,
4196                                                     /*check_dependency=*/true,
4197                                                     &ambiguous_decls,
4198                                                     token->location);
4199                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4200                         error_at (token->location,
4201                                   "%qD used without template parameters",
4202                                   decl);
4203                       else if (ambiguous_decls)
4204                         {
4205                           error_at (token->location,
4206                                     "reference to %qD is ambiguous",
4207                                     token->u.value);
4208                           print_candidates (ambiguous_decls);
4209                           decl = error_mark_node;
4210                         }
4211                       else
4212                         {
4213                           const char* msg = "is not a class or namespace";
4214                           if (cxx_dialect != cxx98)
4215                             msg = "is not a class, namespace, or enumeration";
4216                           cp_parser_name_lookup_error
4217                             (parser, token->u.value, decl, msg,
4218                              token->location);
4219                         }
4220                     }
4221                   parser->scope = error_mark_node;
4222                   error_p = true;
4223                   /* Treat this as a successful nested-name-specifier
4224                      due to:
4225
4226                      [basic.lookup.qual]
4227
4228                      If the name found is not a class-name (clause
4229                      _class_) or namespace-name (_namespace.def_), the
4230                      program is ill-formed.  */
4231                   success = true;
4232                 }
4233               cp_lexer_consume_token (parser->lexer);
4234             }
4235           break;
4236         }
4237       /* We've found one valid nested-name-specifier.  */
4238       success = true;
4239       /* Name lookup always gives us a DECL.  */
4240       if (TREE_CODE (new_scope) == TYPE_DECL)
4241         new_scope = TREE_TYPE (new_scope);
4242       /* Uses of "template" must be followed by actual templates.  */
4243       if (template_keyword_p
4244           && !(CLASS_TYPE_P (new_scope)
4245                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4246                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4247                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4248           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4249                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4250                    == TEMPLATE_ID_EXPR)))
4251         permerror (input_location, TYPE_P (new_scope)
4252                    ? "%qT is not a template"
4253                    : "%qD is not a template",
4254                    new_scope);
4255       /* If it is a class scope, try to complete it; we are about to
4256          be looking up names inside the class.  */
4257       if (TYPE_P (new_scope)
4258           /* Since checking types for dependency can be expensive,
4259              avoid doing it if the type is already complete.  */
4260           && !COMPLETE_TYPE_P (new_scope)
4261           /* Do not try to complete dependent types.  */
4262           && !dependent_type_p (new_scope))
4263         {
4264           new_scope = complete_type (new_scope);
4265           /* If it is a typedef to current class, use the current
4266              class instead, as the typedef won't have any names inside
4267              it yet.  */
4268           if (!COMPLETE_TYPE_P (new_scope)
4269               && currently_open_class (new_scope))
4270             new_scope = TYPE_MAIN_VARIANT (new_scope);
4271         }
4272       /* Make sure we look in the right scope the next time through
4273          the loop.  */
4274       parser->scope = new_scope;
4275     }
4276
4277   /* If parsing tentatively, replace the sequence of tokens that makes
4278      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4279      token.  That way, should we re-parse the token stream, we will
4280      not have to repeat the effort required to do the parse, nor will
4281      we issue duplicate error messages.  */
4282   if (success && start)
4283     {
4284       cp_token *token;
4285
4286       token = cp_lexer_token_at (parser->lexer, start);
4287       /* Reset the contents of the START token.  */
4288       token->type = CPP_NESTED_NAME_SPECIFIER;
4289       /* Retrieve any deferred checks.  Do not pop this access checks yet
4290          so the memory will not be reclaimed during token replacing below.  */
4291       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4292       token->u.tree_check_value->value = parser->scope;
4293       token->u.tree_check_value->checks = get_deferred_access_checks ();
4294       token->u.tree_check_value->qualifying_scope =
4295         parser->qualifying_scope;
4296       token->keyword = RID_MAX;
4297
4298       /* Purge all subsequent tokens.  */
4299       cp_lexer_purge_tokens_after (parser->lexer, start);
4300     }
4301
4302   if (start)
4303     pop_to_parent_deferring_access_checks ();
4304
4305   return success ? parser->scope : NULL_TREE;
4306 }
4307
4308 /* Parse a nested-name-specifier.  See
4309    cp_parser_nested_name_specifier_opt for details.  This function
4310    behaves identically, except that it will an issue an error if no
4311    nested-name-specifier is present.  */
4312
4313 static tree
4314 cp_parser_nested_name_specifier (cp_parser *parser,
4315                                  bool typename_keyword_p,
4316                                  bool check_dependency_p,
4317                                  bool type_p,
4318                                  bool is_declaration)
4319 {
4320   tree scope;
4321
4322   /* Look for the nested-name-specifier.  */
4323   scope = cp_parser_nested_name_specifier_opt (parser,
4324                                                typename_keyword_p,
4325                                                check_dependency_p,
4326                                                type_p,
4327                                                is_declaration);
4328   /* If it was not present, issue an error message.  */
4329   if (!scope)
4330     {
4331       cp_parser_error (parser, "expected nested-name-specifier");
4332       parser->scope = NULL_TREE;
4333     }
4334
4335   return scope;
4336 }
4337
4338 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4339    this is either a class-name or a namespace-name (which corresponds
4340    to the class-or-namespace-name production in the grammar). For
4341    C++0x, it can also be a type-name that refers to an enumeration
4342    type.
4343
4344    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4345    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4346    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4347    TYPE_P is TRUE iff the next name should be taken as a class-name,
4348    even the same name is declared to be another entity in the same
4349    scope.
4350
4351    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4352    specified by the class-or-namespace-name.  If neither is found the
4353    ERROR_MARK_NODE is returned.  */
4354
4355 static tree
4356 cp_parser_qualifying_entity (cp_parser *parser,
4357                              bool typename_keyword_p,
4358                              bool template_keyword_p,
4359                              bool check_dependency_p,
4360                              bool type_p,
4361                              bool is_declaration)
4362 {
4363   tree saved_scope;
4364   tree saved_qualifying_scope;
4365   tree saved_object_scope;
4366   tree scope;
4367   bool only_class_p;
4368   bool successful_parse_p;
4369
4370   /* Before we try to parse the class-name, we must save away the
4371      current PARSER->SCOPE since cp_parser_class_name will destroy
4372      it.  */
4373   saved_scope = parser->scope;
4374   saved_qualifying_scope = parser->qualifying_scope;
4375   saved_object_scope = parser->object_scope;
4376   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4377      there is no need to look for a namespace-name.  */
4378   only_class_p = template_keyword_p 
4379     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4380   if (!only_class_p)
4381     cp_parser_parse_tentatively (parser);
4382   scope = cp_parser_class_name (parser,
4383                                 typename_keyword_p,
4384                                 template_keyword_p,
4385                                 type_p ? class_type : none_type,
4386                                 check_dependency_p,
4387                                 /*class_head_p=*/false,
4388                                 is_declaration);
4389   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4390   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4391   if (!only_class_p 
4392       && cxx_dialect != cxx98
4393       && !successful_parse_p)
4394     {
4395       /* Restore the saved scope.  */
4396       parser->scope = saved_scope;
4397       parser->qualifying_scope = saved_qualifying_scope;
4398       parser->object_scope = saved_object_scope;
4399
4400       /* Parse tentatively.  */
4401       cp_parser_parse_tentatively (parser);
4402      
4403       /* Parse a typedef-name or enum-name.  */
4404       scope = cp_parser_nonclass_name (parser);
4405       successful_parse_p = cp_parser_parse_definitely (parser);
4406     }
4407   /* If that didn't work, try for a namespace-name.  */
4408   if (!only_class_p && !successful_parse_p)
4409     {
4410       /* Restore the saved scope.  */
4411       parser->scope = saved_scope;
4412       parser->qualifying_scope = saved_qualifying_scope;
4413       parser->object_scope = saved_object_scope;
4414       /* If we are not looking at an identifier followed by the scope
4415          resolution operator, then this is not part of a
4416          nested-name-specifier.  (Note that this function is only used
4417          to parse the components of a nested-name-specifier.)  */
4418       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4419           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4420         return error_mark_node;
4421       scope = cp_parser_namespace_name (parser);
4422     }
4423
4424   return scope;
4425 }
4426
4427 /* Parse a postfix-expression.
4428
4429    postfix-expression:
4430      primary-expression
4431      postfix-expression [ expression ]
4432      postfix-expression ( expression-list [opt] )
4433      simple-type-specifier ( expression-list [opt] )
4434      typename :: [opt] nested-name-specifier identifier
4435        ( expression-list [opt] )
4436      typename :: [opt] nested-name-specifier template [opt] template-id
4437        ( expression-list [opt] )
4438      postfix-expression . template [opt] id-expression
4439      postfix-expression -> template [opt] id-expression
4440      postfix-expression . pseudo-destructor-name
4441      postfix-expression -> pseudo-destructor-name
4442      postfix-expression ++
4443      postfix-expression --
4444      dynamic_cast < type-id > ( expression )
4445      static_cast < type-id > ( expression )
4446      reinterpret_cast < type-id > ( expression )
4447      const_cast < type-id > ( expression )
4448      typeid ( expression )
4449      typeid ( type-id )
4450
4451    GNU Extension:
4452
4453    postfix-expression:
4454      ( type-id ) { initializer-list , [opt] }
4455
4456    This extension is a GNU version of the C99 compound-literal
4457    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4458    but they are essentially the same concept.)
4459
4460    If ADDRESS_P is true, the postfix expression is the operand of the
4461    `&' operator.  CAST_P is true if this expression is the target of a
4462    cast.
4463
4464    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4465    class member access expressions [expr.ref].
4466
4467    Returns a representation of the expression.  */
4468
4469 static tree
4470 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4471                               bool member_access_only_p,
4472                               cp_id_kind * pidk_return)
4473 {
4474   cp_token *token;
4475   enum rid keyword;
4476   cp_id_kind idk = CP_ID_KIND_NONE;
4477   tree postfix_expression = NULL_TREE;
4478   bool is_member_access = false;
4479
4480   /* Peek at the next token.  */
4481   token = cp_lexer_peek_token (parser->lexer);
4482   /* Some of the productions are determined by keywords.  */
4483   keyword = token->keyword;
4484   switch (keyword)
4485     {
4486     case RID_DYNCAST:
4487     case RID_STATCAST:
4488     case RID_REINTCAST:
4489     case RID_CONSTCAST:
4490       {
4491         tree type;
4492         tree expression;
4493         const char *saved_message;
4494
4495         /* All of these can be handled in the same way from the point
4496            of view of parsing.  Begin by consuming the token
4497            identifying the cast.  */
4498         cp_lexer_consume_token (parser->lexer);
4499
4500         /* New types cannot be defined in the cast.  */
4501         saved_message = parser->type_definition_forbidden_message;
4502         parser->type_definition_forbidden_message
4503           = "types may not be defined in casts";
4504
4505         /* Look for the opening `<'.  */
4506         cp_parser_require (parser, CPP_LESS, "%<<%>");
4507         /* Parse the type to which we are casting.  */
4508         type = cp_parser_type_id (parser);
4509         /* Look for the closing `>'.  */
4510         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4511         /* Restore the old message.  */
4512         parser->type_definition_forbidden_message = saved_message;
4513
4514         /* And the expression which is being cast.  */
4515         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4516         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4517         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4518
4519         /* Only type conversions to integral or enumeration types
4520            can be used in constant-expressions.  */
4521         if (!cast_valid_in_integral_constant_expression_p (type)
4522             && (cp_parser_non_integral_constant_expression
4523                 (parser,
4524                  "a cast to a type other than an integral or "
4525                  "enumeration type")))
4526           return error_mark_node;
4527
4528         switch (keyword)
4529           {
4530           case RID_DYNCAST:
4531             postfix_expression
4532               = build_dynamic_cast (type, expression, tf_warning_or_error);
4533             break;
4534           case RID_STATCAST:
4535             postfix_expression
4536               = build_static_cast (type, expression, tf_warning_or_error);
4537             break;
4538           case RID_REINTCAST:
4539             postfix_expression
4540               = build_reinterpret_cast (type, expression, 
4541                                         tf_warning_or_error);
4542             break;
4543           case RID_CONSTCAST:
4544             postfix_expression
4545               = build_const_cast (type, expression, tf_warning_or_error);
4546             break;
4547           default:
4548             gcc_unreachable ();
4549           }
4550       }
4551       break;
4552
4553     case RID_TYPEID:
4554       {
4555         tree type;
4556         const char *saved_message;
4557         bool saved_in_type_id_in_expr_p;
4558
4559         /* Consume the `typeid' token.  */
4560         cp_lexer_consume_token (parser->lexer);
4561         /* Look for the `(' token.  */
4562         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4563         /* Types cannot be defined in a `typeid' expression.  */
4564         saved_message = parser->type_definition_forbidden_message;
4565         parser->type_definition_forbidden_message
4566           = "types may not be defined in a %<typeid%> expression";
4567         /* We can't be sure yet whether we're looking at a type-id or an
4568            expression.  */
4569         cp_parser_parse_tentatively (parser);
4570         /* Try a type-id first.  */
4571         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4572         parser->in_type_id_in_expr_p = true;
4573         type = cp_parser_type_id (parser);
4574         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4575         /* Look for the `)' token.  Otherwise, we can't be sure that
4576            we're not looking at an expression: consider `typeid (int
4577            (3))', for example.  */
4578         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4579         /* If all went well, simply lookup the type-id.  */
4580         if (cp_parser_parse_definitely (parser))
4581           postfix_expression = get_typeid (type);
4582         /* Otherwise, fall back to the expression variant.  */
4583         else
4584           {
4585             tree expression;
4586
4587             /* Look for an expression.  */
4588             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4589             /* Compute its typeid.  */
4590             postfix_expression = build_typeid (expression);
4591             /* Look for the `)' token.  */
4592             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4593           }
4594         /* Restore the saved message.  */
4595         parser->type_definition_forbidden_message = saved_message;
4596         /* `typeid' may not appear in an integral constant expression.  */
4597         if (cp_parser_non_integral_constant_expression(parser,
4598                                                        "%<typeid%> operator"))
4599           return error_mark_node;
4600       }
4601       break;
4602
4603     case RID_TYPENAME:
4604       {
4605         tree type;
4606         /* The syntax permitted here is the same permitted for an
4607            elaborated-type-specifier.  */
4608         type = cp_parser_elaborated_type_specifier (parser,
4609                                                     /*is_friend=*/false,
4610                                                     /*is_declaration=*/false);
4611         postfix_expression = cp_parser_functional_cast (parser, type);
4612       }
4613       break;
4614
4615     default:
4616       {
4617         tree type;
4618
4619         /* If the next thing is a simple-type-specifier, we may be
4620            looking at a functional cast.  We could also be looking at
4621            an id-expression.  So, we try the functional cast, and if
4622            that doesn't work we fall back to the primary-expression.  */
4623         cp_parser_parse_tentatively (parser);
4624         /* Look for the simple-type-specifier.  */
4625         type = cp_parser_simple_type_specifier (parser,
4626                                                 /*decl_specs=*/NULL,
4627                                                 CP_PARSER_FLAGS_NONE);
4628         /* Parse the cast itself.  */
4629         if (!cp_parser_error_occurred (parser))
4630           postfix_expression
4631             = cp_parser_functional_cast (parser, type);
4632         /* If that worked, we're done.  */
4633         if (cp_parser_parse_definitely (parser))
4634           break;
4635
4636         /* If the functional-cast didn't work out, try a
4637            compound-literal.  */
4638         if (cp_parser_allow_gnu_extensions_p (parser)
4639             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4640           {
4641             VEC(constructor_elt,gc) *initializer_list = NULL;
4642             bool saved_in_type_id_in_expr_p;
4643
4644             cp_parser_parse_tentatively (parser);
4645             /* Consume the `('.  */
4646             cp_lexer_consume_token (parser->lexer);
4647             /* Parse the type.  */
4648             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4649             parser->in_type_id_in_expr_p = true;
4650             type = cp_parser_type_id (parser);
4651             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4652             /* Look for the `)'.  */
4653             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4654             /* Look for the `{'.  */
4655             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4656             /* If things aren't going well, there's no need to
4657                keep going.  */
4658             if (!cp_parser_error_occurred (parser))
4659               {
4660                 bool non_constant_p;
4661                 /* Parse the initializer-list.  */
4662                 initializer_list
4663                   = cp_parser_initializer_list (parser, &non_constant_p);
4664                 /* Allow a trailing `,'.  */
4665                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4666                   cp_lexer_consume_token (parser->lexer);
4667                 /* Look for the final `}'.  */
4668                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4669               }
4670             /* If that worked, we're definitely looking at a
4671                compound-literal expression.  */
4672             if (cp_parser_parse_definitely (parser))
4673               {
4674                 /* Warn the user that a compound literal is not
4675                    allowed in standard C++.  */
4676                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4677                 /* For simplicity, we disallow compound literals in
4678                    constant-expressions.  We could
4679                    allow compound literals of integer type, whose
4680                    initializer was a constant, in constant
4681                    expressions.  Permitting that usage, as a further
4682                    extension, would not change the meaning of any
4683                    currently accepted programs.  (Of course, as
4684                    compound literals are not part of ISO C++, the
4685                    standard has nothing to say.)  */
4686                 if (cp_parser_non_integral_constant_expression 
4687                     (parser, "non-constant compound literals"))
4688                   {
4689                     postfix_expression = error_mark_node;
4690                     break;
4691                   }
4692                 /* Form the representation of the compound-literal.  */
4693                 postfix_expression
4694                   = (finish_compound_literal
4695                      (type, build_constructor (init_list_type_node,
4696                                                initializer_list)));
4697                 break;
4698               }
4699           }
4700
4701         /* It must be a primary-expression.  */
4702         postfix_expression
4703           = cp_parser_primary_expression (parser, address_p, cast_p,
4704                                           /*template_arg_p=*/false,
4705                                           &idk);
4706       }
4707       break;
4708     }
4709
4710   /* Keep looping until the postfix-expression is complete.  */
4711   while (true)
4712     {
4713       if (idk == CP_ID_KIND_UNQUALIFIED
4714           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4715           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4716         /* It is not a Koenig lookup function call.  */
4717         postfix_expression
4718           = unqualified_name_lookup_error (postfix_expression);
4719
4720       /* Peek at the next token.  */
4721       token = cp_lexer_peek_token (parser->lexer);
4722
4723       switch (token->type)
4724         {
4725         case CPP_OPEN_SQUARE:
4726           postfix_expression
4727             = cp_parser_postfix_open_square_expression (parser,
4728                                                         postfix_expression,
4729                                                         false);
4730           idk = CP_ID_KIND_NONE;
4731           is_member_access = false;
4732           break;
4733
4734         case CPP_OPEN_PAREN:
4735           /* postfix-expression ( expression-list [opt] ) */
4736           {
4737             bool koenig_p;
4738             bool is_builtin_constant_p;
4739             bool saved_integral_constant_expression_p = false;
4740             bool saved_non_integral_constant_expression_p = false;
4741             VEC(tree,gc) *args;
4742
4743             is_member_access = false;
4744
4745             is_builtin_constant_p
4746               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4747             if (is_builtin_constant_p)
4748               {
4749                 /* The whole point of __builtin_constant_p is to allow
4750                    non-constant expressions to appear as arguments.  */
4751                 saved_integral_constant_expression_p
4752                   = parser->integral_constant_expression_p;
4753                 saved_non_integral_constant_expression_p
4754                   = parser->non_integral_constant_expression_p;
4755                 parser->integral_constant_expression_p = false;
4756               }
4757             args = (cp_parser_parenthesized_expression_list
4758                     (parser, /*is_attribute_list=*/false,
4759                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4760                      /*non_constant_p=*/NULL));
4761             if (is_builtin_constant_p)
4762               {
4763                 parser->integral_constant_expression_p
4764                   = saved_integral_constant_expression_p;
4765                 parser->non_integral_constant_expression_p
4766                   = saved_non_integral_constant_expression_p;
4767               }
4768
4769             if (args == NULL)
4770               {
4771                 postfix_expression = error_mark_node;
4772                 break;
4773               }
4774
4775             /* Function calls are not permitted in
4776                constant-expressions.  */
4777             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4778                 && cp_parser_non_integral_constant_expression (parser,
4779                                                                "a function call"))
4780               {
4781                 postfix_expression = error_mark_node;
4782                 release_tree_vector (args);
4783                 break;
4784               }
4785
4786             koenig_p = false;
4787             if (idk == CP_ID_KIND_UNQUALIFIED
4788                 || idk == CP_ID_KIND_TEMPLATE_ID)
4789               {
4790                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4791                   {
4792                     if (!VEC_empty (tree, args))
4793                       {
4794                         koenig_p = true;
4795                         if (!any_type_dependent_arguments_p (args))
4796                           postfix_expression
4797                             = perform_koenig_lookup (postfix_expression, args);
4798                       }
4799                     else
4800                       postfix_expression
4801                         = unqualified_fn_lookup_error (postfix_expression);
4802                   }
4803                 /* We do not perform argument-dependent lookup if
4804                    normal lookup finds a non-function, in accordance
4805                    with the expected resolution of DR 218.  */
4806                 else if (!VEC_empty (tree, args)
4807                          && is_overloaded_fn (postfix_expression))
4808                   {
4809                     tree fn = get_first_fn (postfix_expression);
4810
4811                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4812                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4813
4814                     /* Only do argument dependent lookup if regular
4815                        lookup does not find a set of member functions.
4816                        [basic.lookup.koenig]/2a  */
4817                     if (!DECL_FUNCTION_MEMBER_P (fn))
4818                       {
4819                         koenig_p = true;
4820                         if (!any_type_dependent_arguments_p (args))
4821                           postfix_expression
4822                             = perform_koenig_lookup (postfix_expression, args);
4823                       }
4824                   }
4825               }
4826
4827             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4828               {
4829                 tree instance = TREE_OPERAND (postfix_expression, 0);
4830                 tree fn = TREE_OPERAND (postfix_expression, 1);
4831
4832                 if (processing_template_decl
4833                     && (type_dependent_expression_p (instance)
4834                         || (!BASELINK_P (fn)
4835                             && TREE_CODE (fn) != FIELD_DECL)
4836                         || type_dependent_expression_p (fn)
4837                         || any_type_dependent_arguments_p (args)))
4838                   {
4839                     postfix_expression
4840                       = build_nt_call_vec (postfix_expression, args);
4841                     release_tree_vector (args);
4842                     break;
4843                   }
4844
4845                 if (BASELINK_P (fn))
4846                   {
4847                   postfix_expression
4848                     = (build_new_method_call
4849                        (instance, fn, &args, NULL_TREE,
4850                         (idk == CP_ID_KIND_QUALIFIED
4851                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4852                         /*fn_p=*/NULL,
4853                         tf_warning_or_error));
4854                   }
4855                 else
4856                   postfix_expression
4857                     = finish_call_expr (postfix_expression, &args,
4858                                         /*disallow_virtual=*/false,
4859                                         /*koenig_p=*/false,
4860                                         tf_warning_or_error);
4861               }
4862             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4863                      || TREE_CODE (postfix_expression) == MEMBER_REF
4864                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4865               postfix_expression = (build_offset_ref_call_from_tree
4866                                     (postfix_expression, &args));
4867             else if (idk == CP_ID_KIND_QUALIFIED)
4868               /* A call to a static class member, or a namespace-scope
4869                  function.  */
4870               postfix_expression
4871                 = finish_call_expr (postfix_expression, &args,
4872                                     /*disallow_virtual=*/true,
4873                                     koenig_p,
4874                                     tf_warning_or_error);
4875             else
4876               /* All other function calls.  */
4877               postfix_expression
4878                 = finish_call_expr (postfix_expression, &args,
4879                                     /*disallow_virtual=*/false,
4880                                     koenig_p,
4881                                     tf_warning_or_error);
4882
4883             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4884             idk = CP_ID_KIND_NONE;
4885
4886             release_tree_vector (args);
4887           }
4888           break;
4889
4890         case CPP_DOT:
4891         case CPP_DEREF:
4892           /* postfix-expression . template [opt] id-expression
4893              postfix-expression . pseudo-destructor-name
4894              postfix-expression -> template [opt] id-expression
4895              postfix-expression -> pseudo-destructor-name */
4896
4897           /* Consume the `.' or `->' operator.  */
4898           cp_lexer_consume_token (parser->lexer);
4899
4900           postfix_expression
4901             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4902                                                       postfix_expression,
4903                                                       false, &idk,
4904                                                       token->location);
4905
4906           is_member_access = true;
4907           break;
4908
4909         case CPP_PLUS_PLUS:
4910           /* postfix-expression ++  */
4911           /* Consume the `++' token.  */
4912           cp_lexer_consume_token (parser->lexer);
4913           /* Generate a representation for the complete expression.  */
4914           postfix_expression
4915             = finish_increment_expr (postfix_expression,
4916                                      POSTINCREMENT_EXPR);
4917           /* Increments may not appear in constant-expressions.  */
4918           if (cp_parser_non_integral_constant_expression (parser,
4919                                                           "an increment"))
4920             postfix_expression = error_mark_node;
4921           idk = CP_ID_KIND_NONE;
4922           is_member_access = false;
4923           break;
4924
4925         case CPP_MINUS_MINUS:
4926           /* postfix-expression -- */
4927           /* Consume the `--' token.  */
4928           cp_lexer_consume_token (parser->lexer);
4929           /* Generate a representation for the complete expression.  */
4930           postfix_expression
4931             = finish_increment_expr (postfix_expression,
4932                                      POSTDECREMENT_EXPR);
4933           /* Decrements may not appear in constant-expressions.  */
4934           if (cp_parser_non_integral_constant_expression (parser,
4935                                                           "a decrement"))
4936             postfix_expression = error_mark_node;
4937           idk = CP_ID_KIND_NONE;
4938           is_member_access = false;
4939           break;
4940
4941         default:
4942           if (pidk_return != NULL)
4943             * pidk_return = idk;
4944           if (member_access_only_p)
4945             return is_member_access? postfix_expression : error_mark_node;
4946           else
4947             return postfix_expression;
4948         }
4949     }
4950
4951   /* We should never get here.  */
4952   gcc_unreachable ();
4953   return error_mark_node;
4954 }
4955
4956 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4957    by cp_parser_builtin_offsetof.  We're looking for
4958
4959      postfix-expression [ expression ]
4960
4961    FOR_OFFSETOF is set if we're being called in that context, which
4962    changes how we deal with integer constant expressions.  */
4963
4964 static tree
4965 cp_parser_postfix_open_square_expression (cp_parser *parser,
4966                                           tree postfix_expression,
4967                                           bool for_offsetof)
4968 {
4969   tree index;
4970
4971   /* Consume the `[' token.  */
4972   cp_lexer_consume_token (parser->lexer);
4973
4974   /* Parse the index expression.  */
4975   /* ??? For offsetof, there is a question of what to allow here.  If
4976      offsetof is not being used in an integral constant expression context,
4977      then we *could* get the right answer by computing the value at runtime.
4978      If we are in an integral constant expression context, then we might
4979      could accept any constant expression; hard to say without analysis.
4980      Rather than open the barn door too wide right away, allow only integer
4981      constant expressions here.  */
4982   if (for_offsetof)
4983     index = cp_parser_constant_expression (parser, false, NULL);
4984   else
4985     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4986
4987   /* Look for the closing `]'.  */
4988   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4989
4990   /* Build the ARRAY_REF.  */
4991   postfix_expression = grok_array_decl (postfix_expression, index);
4992
4993   /* When not doing offsetof, array references are not permitted in
4994      constant-expressions.  */
4995   if (!for_offsetof
4996       && (cp_parser_non_integral_constant_expression
4997           (parser, "an array reference")))
4998     postfix_expression = error_mark_node;
4999
5000   return postfix_expression;
5001 }
5002
5003 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5004    by cp_parser_builtin_offsetof.  We're looking for
5005
5006      postfix-expression . template [opt] id-expression
5007      postfix-expression . pseudo-destructor-name
5008      postfix-expression -> template [opt] id-expression
5009      postfix-expression -> pseudo-destructor-name
5010
5011    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5012    limits what of the above we'll actually accept, but nevermind.
5013    TOKEN_TYPE is the "." or "->" token, which will already have been
5014    removed from the stream.  */
5015
5016 static tree
5017 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5018                                         enum cpp_ttype token_type,
5019                                         tree postfix_expression,
5020                                         bool for_offsetof, cp_id_kind *idk,
5021                                         location_t location)
5022 {
5023   tree name;
5024   bool dependent_p;
5025   bool pseudo_destructor_p;
5026   tree scope = NULL_TREE;
5027
5028   /* If this is a `->' operator, dereference the pointer.  */
5029   if (token_type == CPP_DEREF)
5030     postfix_expression = build_x_arrow (postfix_expression);
5031   /* Check to see whether or not the expression is type-dependent.  */
5032   dependent_p = type_dependent_expression_p (postfix_expression);
5033   /* The identifier following the `->' or `.' is not qualified.  */
5034   parser->scope = NULL_TREE;
5035   parser->qualifying_scope = NULL_TREE;
5036   parser->object_scope = NULL_TREE;
5037   *idk = CP_ID_KIND_NONE;
5038
5039   /* Enter the scope corresponding to the type of the object
5040      given by the POSTFIX_EXPRESSION.  */
5041   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5042     {
5043       scope = TREE_TYPE (postfix_expression);
5044       /* According to the standard, no expression should ever have
5045          reference type.  Unfortunately, we do not currently match
5046          the standard in this respect in that our internal representation
5047          of an expression may have reference type even when the standard
5048          says it does not.  Therefore, we have to manually obtain the
5049          underlying type here.  */
5050       scope = non_reference (scope);
5051       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5052       if (scope == unknown_type_node)
5053         {
5054           error_at (location, "%qE does not have class type",
5055                     postfix_expression);
5056           scope = NULL_TREE;
5057         }
5058       else
5059         scope = complete_type_or_else (scope, NULL_TREE);
5060       /* Let the name lookup machinery know that we are processing a
5061          class member access expression.  */
5062       parser->context->object_type = scope;
5063       /* If something went wrong, we want to be able to discern that case,
5064          as opposed to the case where there was no SCOPE due to the type
5065          of expression being dependent.  */
5066       if (!scope)
5067         scope = error_mark_node;
5068       /* If the SCOPE was erroneous, make the various semantic analysis
5069          functions exit quickly -- and without issuing additional error
5070          messages.  */
5071       if (scope == error_mark_node)
5072         postfix_expression = error_mark_node;
5073     }
5074
5075   /* Assume this expression is not a pseudo-destructor access.  */
5076   pseudo_destructor_p = false;
5077
5078   /* If the SCOPE is a scalar type, then, if this is a valid program,
5079      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5080      is type dependent, it can be pseudo-destructor-name or something else.
5081      Try to parse it as pseudo-destructor-name first.  */
5082   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5083     {
5084       tree s;
5085       tree type;
5086
5087       cp_parser_parse_tentatively (parser);
5088       /* Parse the pseudo-destructor-name.  */
5089       s = NULL_TREE;
5090       cp_parser_pseudo_destructor_name (parser, &s, &type);
5091       if (dependent_p
5092           && (cp_parser_error_occurred (parser)
5093               || TREE_CODE (type) != TYPE_DECL
5094               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5095         cp_parser_abort_tentative_parse (parser);
5096       else if (cp_parser_parse_definitely (parser))
5097         {
5098           pseudo_destructor_p = true;
5099           postfix_expression
5100             = finish_pseudo_destructor_expr (postfix_expression,
5101                                              s, TREE_TYPE (type));
5102         }
5103     }
5104
5105   if (!pseudo_destructor_p)
5106     {
5107       /* If the SCOPE is not a scalar type, we are looking at an
5108          ordinary class member access expression, rather than a
5109          pseudo-destructor-name.  */
5110       bool template_p;
5111       cp_token *token = cp_lexer_peek_token (parser->lexer);
5112       /* Parse the id-expression.  */
5113       name = (cp_parser_id_expression
5114               (parser,
5115                cp_parser_optional_template_keyword (parser),
5116                /*check_dependency_p=*/true,
5117                &template_p,
5118                /*declarator_p=*/false,
5119                /*optional_p=*/false));
5120       /* In general, build a SCOPE_REF if the member name is qualified.
5121          However, if the name was not dependent and has already been
5122          resolved; there is no need to build the SCOPE_REF.  For example;
5123
5124              struct X { void f(); };
5125              template <typename T> void f(T* t) { t->X::f(); }
5126
5127          Even though "t" is dependent, "X::f" is not and has been resolved
5128          to a BASELINK; there is no need to include scope information.  */
5129
5130       /* But we do need to remember that there was an explicit scope for
5131          virtual function calls.  */
5132       if (parser->scope)
5133         *idk = CP_ID_KIND_QUALIFIED;
5134
5135       /* If the name is a template-id that names a type, we will get a
5136          TYPE_DECL here.  That is invalid code.  */
5137       if (TREE_CODE (name) == TYPE_DECL)
5138         {
5139           error_at (token->location, "invalid use of %qD", name);
5140           postfix_expression = error_mark_node;
5141         }
5142       else
5143         {
5144           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5145             {
5146               name = build_qualified_name (/*type=*/NULL_TREE,
5147                                            parser->scope,
5148                                            name,
5149                                            template_p);
5150               parser->scope = NULL_TREE;
5151               parser->qualifying_scope = NULL_TREE;
5152               parser->object_scope = NULL_TREE;
5153             }
5154           if (scope && name && BASELINK_P (name))
5155             adjust_result_of_qualified_name_lookup
5156               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5157           postfix_expression
5158             = finish_class_member_access_expr (postfix_expression, name,
5159                                                template_p, 
5160                                                tf_warning_or_error);
5161         }
5162     }
5163
5164   /* We no longer need to look up names in the scope of the object on
5165      the left-hand side of the `.' or `->' operator.  */
5166   parser->context->object_type = NULL_TREE;
5167
5168   /* Outside of offsetof, these operators may not appear in
5169      constant-expressions.  */
5170   if (!for_offsetof
5171       && (cp_parser_non_integral_constant_expression
5172           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5173     postfix_expression = error_mark_node;
5174
5175   return postfix_expression;
5176 }
5177
5178 /* Parse a parenthesized expression-list.
5179
5180    expression-list:
5181      assignment-expression
5182      expression-list, assignment-expression
5183
5184    attribute-list:
5185      expression-list
5186      identifier
5187      identifier, expression-list
5188
5189    CAST_P is true if this expression is the target of a cast.
5190
5191    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5192    argument pack.
5193
5194    Returns a vector of trees.  Each element is a representation of an
5195    assignment-expression.  NULL is returned if the ( and or ) are
5196    missing.  An empty, but allocated, vector is returned on no
5197    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is true
5198    if this is really an attribute list being parsed.  If
5199    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5200    not all of the expressions in the list were constant.  */
5201
5202 static VEC(tree,gc) *
5203 cp_parser_parenthesized_expression_list (cp_parser* parser,
5204                                          bool is_attribute_list,
5205                                          bool cast_p,
5206                                          bool allow_expansion_p,
5207                                          bool *non_constant_p)
5208 {
5209   VEC(tree,gc) *expression_list;
5210   bool fold_expr_p = is_attribute_list;
5211   tree identifier = NULL_TREE;
5212   bool saved_greater_than_is_operator_p;
5213
5214   /* Assume all the expressions will be constant.  */
5215   if (non_constant_p)
5216     *non_constant_p = false;
5217
5218   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5219     return NULL;
5220
5221   expression_list = make_tree_vector ();
5222
5223   /* Within a parenthesized expression, a `>' token is always
5224      the greater-than operator.  */
5225   saved_greater_than_is_operator_p
5226     = parser->greater_than_is_operator_p;
5227   parser->greater_than_is_operator_p = true;
5228
5229   /* Consume expressions until there are no more.  */
5230   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5231     while (true)
5232       {
5233         tree expr;
5234
5235         /* At the beginning of attribute lists, check to see if the
5236            next token is an identifier.  */
5237         if (is_attribute_list
5238             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5239           {
5240             cp_token *token;
5241
5242             /* Consume the identifier.  */
5243             token = cp_lexer_consume_token (parser->lexer);
5244             /* Save the identifier.  */
5245             identifier = token->u.value;
5246           }
5247         else
5248           {
5249             bool expr_non_constant_p;
5250
5251             /* Parse the next assignment-expression.  */
5252             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5253               {
5254                 /* A braced-init-list.  */
5255                 maybe_warn_cpp0x ("extended initializer lists");
5256                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5257                 if (non_constant_p && expr_non_constant_p)
5258                   *non_constant_p = true;
5259               }
5260             else if (non_constant_p)
5261               {
5262                 expr = (cp_parser_constant_expression
5263                         (parser, /*allow_non_constant_p=*/true,
5264                          &expr_non_constant_p));
5265                 if (expr_non_constant_p)
5266                   *non_constant_p = true;
5267               }
5268             else
5269               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5270
5271             if (fold_expr_p)
5272               expr = fold_non_dependent_expr (expr);
5273
5274             /* If we have an ellipsis, then this is an expression
5275                expansion.  */
5276             if (allow_expansion_p
5277                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5278               {
5279                 /* Consume the `...'.  */
5280                 cp_lexer_consume_token (parser->lexer);
5281
5282                 /* Build the argument pack.  */
5283                 expr = make_pack_expansion (expr);
5284               }
5285
5286              /* Add it to the list.  We add error_mark_node
5287                 expressions to the list, so that we can still tell if
5288                 the correct form for a parenthesized expression-list
5289                 is found. That gives better errors.  */
5290             VEC_safe_push (tree, gc, expression_list, expr);
5291
5292             if (expr == error_mark_node)
5293               goto skip_comma;
5294           }
5295
5296         /* After the first item, attribute lists look the same as
5297            expression lists.  */
5298         is_attribute_list = false;
5299
5300       get_comma:;
5301         /* If the next token isn't a `,', then we are done.  */
5302         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5303           break;
5304
5305         /* Otherwise, consume the `,' and keep going.  */
5306         cp_lexer_consume_token (parser->lexer);
5307       }
5308
5309   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5310     {
5311       int ending;
5312
5313     skip_comma:;
5314       /* We try and resync to an unnested comma, as that will give the
5315          user better diagnostics.  */
5316       ending = cp_parser_skip_to_closing_parenthesis (parser,
5317                                                       /*recovering=*/true,
5318                                                       /*or_comma=*/true,
5319                                                       /*consume_paren=*/true);
5320       if (ending < 0)
5321         goto get_comma;
5322       if (!ending)
5323         {
5324           parser->greater_than_is_operator_p
5325             = saved_greater_than_is_operator_p;
5326           return NULL;
5327         }
5328     }
5329
5330   parser->greater_than_is_operator_p
5331     = saved_greater_than_is_operator_p;
5332
5333   if (identifier)
5334     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5335
5336   return expression_list;
5337 }
5338
5339 /* Parse a pseudo-destructor-name.
5340
5341    pseudo-destructor-name:
5342      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5343      :: [opt] nested-name-specifier template template-id :: ~ type-name
5344      :: [opt] nested-name-specifier [opt] ~ type-name
5345
5346    If either of the first two productions is used, sets *SCOPE to the
5347    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5348    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5349    or ERROR_MARK_NODE if the parse fails.  */
5350
5351 static void
5352 cp_parser_pseudo_destructor_name (cp_parser* parser,
5353                                   tree* scope,
5354                                   tree* type)
5355 {
5356   bool nested_name_specifier_p;
5357
5358   /* Assume that things will not work out.  */
5359   *type = error_mark_node;
5360
5361   /* Look for the optional `::' operator.  */
5362   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5363   /* Look for the optional nested-name-specifier.  */
5364   nested_name_specifier_p
5365     = (cp_parser_nested_name_specifier_opt (parser,
5366                                             /*typename_keyword_p=*/false,
5367                                             /*check_dependency_p=*/true,
5368                                             /*type_p=*/false,
5369                                             /*is_declaration=*/false)
5370        != NULL_TREE);
5371   /* Now, if we saw a nested-name-specifier, we might be doing the
5372      second production.  */
5373   if (nested_name_specifier_p
5374       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5375     {
5376       /* Consume the `template' keyword.  */
5377       cp_lexer_consume_token (parser->lexer);
5378       /* Parse the template-id.  */
5379       cp_parser_template_id (parser,
5380                              /*template_keyword_p=*/true,
5381                              /*check_dependency_p=*/false,
5382                              /*is_declaration=*/true);
5383       /* Look for the `::' token.  */
5384       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5385     }
5386   /* If the next token is not a `~', then there might be some
5387      additional qualification.  */
5388   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5389     {
5390       /* At this point, we're looking for "type-name :: ~".  The type-name
5391          must not be a class-name, since this is a pseudo-destructor.  So,
5392          it must be either an enum-name, or a typedef-name -- both of which
5393          are just identifiers.  So, we peek ahead to check that the "::"
5394          and "~" tokens are present; if they are not, then we can avoid
5395          calling type_name.  */
5396       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5397           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5398           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5399         {
5400           cp_parser_error (parser, "non-scalar type");
5401           return;
5402         }
5403
5404       /* Look for the type-name.  */
5405       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5406       if (*scope == error_mark_node)
5407         return;
5408
5409       /* Look for the `::' token.  */
5410       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5411     }
5412   else
5413     *scope = NULL_TREE;
5414
5415   /* Look for the `~'.  */
5416   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5417   /* Look for the type-name again.  We are not responsible for
5418      checking that it matches the first type-name.  */
5419   *type = cp_parser_nonclass_name (parser);
5420 }
5421
5422 /* Parse a unary-expression.
5423
5424    unary-expression:
5425      postfix-expression
5426      ++ cast-expression
5427      -- cast-expression
5428      unary-operator cast-expression
5429      sizeof unary-expression
5430      sizeof ( type-id )
5431      new-expression
5432      delete-expression
5433
5434    GNU Extensions:
5435
5436    unary-expression:
5437      __extension__ cast-expression
5438      __alignof__ unary-expression
5439      __alignof__ ( type-id )
5440      __real__ cast-expression
5441      __imag__ cast-expression
5442      && identifier
5443
5444    ADDRESS_P is true iff the unary-expression is appearing as the
5445    operand of the `&' operator.   CAST_P is true if this expression is
5446    the target of a cast.
5447
5448    Returns a representation of the expression.  */
5449
5450 static tree
5451 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5452                             cp_id_kind * pidk)
5453 {
5454   cp_token *token;
5455   enum tree_code unary_operator;
5456
5457   /* Peek at the next token.  */
5458   token = cp_lexer_peek_token (parser->lexer);
5459   /* Some keywords give away the kind of expression.  */
5460   if (token->type == CPP_KEYWORD)
5461     {
5462       enum rid keyword = token->keyword;
5463
5464       switch (keyword)
5465         {
5466         case RID_ALIGNOF:
5467         case RID_SIZEOF:
5468           {
5469             tree operand;
5470             enum tree_code op;
5471
5472             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5473             /* Consume the token.  */
5474             cp_lexer_consume_token (parser->lexer);
5475             /* Parse the operand.  */
5476             operand = cp_parser_sizeof_operand (parser, keyword);
5477
5478             if (TYPE_P (operand))
5479               return cxx_sizeof_or_alignof_type (operand, op, true);
5480             else
5481               return cxx_sizeof_or_alignof_expr (operand, op, true);
5482           }
5483
5484         case RID_NEW:
5485           return cp_parser_new_expression (parser);
5486
5487         case RID_DELETE:
5488           return cp_parser_delete_expression (parser);
5489
5490         case RID_EXTENSION:
5491           {
5492             /* The saved value of the PEDANTIC flag.  */
5493             int saved_pedantic;
5494             tree expr;
5495
5496             /* Save away the PEDANTIC flag.  */
5497             cp_parser_extension_opt (parser, &saved_pedantic);
5498             /* Parse the cast-expression.  */
5499             expr = cp_parser_simple_cast_expression (parser);
5500             /* Restore the PEDANTIC flag.  */
5501             pedantic = saved_pedantic;
5502
5503             return expr;
5504           }
5505
5506         case RID_REALPART:
5507         case RID_IMAGPART:
5508           {
5509             tree expression;
5510
5511             /* Consume the `__real__' or `__imag__' token.  */
5512             cp_lexer_consume_token (parser->lexer);
5513             /* Parse the cast-expression.  */
5514             expression = cp_parser_simple_cast_expression (parser);
5515             /* Create the complete representation.  */
5516             return build_x_unary_op ((keyword == RID_REALPART
5517                                       ? REALPART_EXPR : IMAGPART_EXPR),
5518                                      expression,
5519                                      tf_warning_or_error);
5520           }
5521           break;
5522
5523         default:
5524           break;
5525         }
5526     }
5527
5528   /* Look for the `:: new' and `:: delete', which also signal the
5529      beginning of a new-expression, or delete-expression,
5530      respectively.  If the next token is `::', then it might be one of
5531      these.  */
5532   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5533     {
5534       enum rid keyword;
5535
5536       /* See if the token after the `::' is one of the keywords in
5537          which we're interested.  */
5538       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5539       /* If it's `new', we have a new-expression.  */
5540       if (keyword == RID_NEW)
5541         return cp_parser_new_expression (parser);
5542       /* Similarly, for `delete'.  */
5543       else if (keyword == RID_DELETE)
5544         return cp_parser_delete_expression (parser);
5545     }
5546
5547   /* Look for a unary operator.  */
5548   unary_operator = cp_parser_unary_operator (token);
5549   /* The `++' and `--' operators can be handled similarly, even though
5550      they are not technically unary-operators in the grammar.  */
5551   if (unary_operator == ERROR_MARK)
5552     {
5553       if (token->type == CPP_PLUS_PLUS)
5554         unary_operator = PREINCREMENT_EXPR;
5555       else if (token->type == CPP_MINUS_MINUS)
5556         unary_operator = PREDECREMENT_EXPR;
5557       /* Handle the GNU address-of-label extension.  */
5558       else if (cp_parser_allow_gnu_extensions_p (parser)
5559                && token->type == CPP_AND_AND)
5560         {
5561           tree identifier;
5562           tree expression;
5563           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5564
5565           /* Consume the '&&' token.  */
5566           cp_lexer_consume_token (parser->lexer);
5567           /* Look for the identifier.  */
5568           identifier = cp_parser_identifier (parser);
5569           /* Create an expression representing the address.  */
5570           expression = finish_label_address_expr (identifier, loc);
5571           if (cp_parser_non_integral_constant_expression (parser,
5572                                                 "the address of a label"))
5573             expression = error_mark_node;
5574           return expression;
5575         }
5576     }
5577   if (unary_operator != ERROR_MARK)
5578     {
5579       tree cast_expression;
5580       tree expression = error_mark_node;
5581       const char *non_constant_p = NULL;
5582
5583       /* Consume the operator token.  */
5584       token = cp_lexer_consume_token (parser->lexer);
5585       /* Parse the cast-expression.  */
5586       cast_expression
5587         = cp_parser_cast_expression (parser,
5588                                      unary_operator == ADDR_EXPR,
5589                                      /*cast_p=*/false, pidk);
5590       /* Now, build an appropriate representation.  */
5591       switch (unary_operator)
5592         {
5593         case INDIRECT_REF:
5594           non_constant_p = "%<*%>";
5595           expression = build_x_indirect_ref (cast_expression, "unary *",
5596                                              tf_warning_or_error);
5597           break;
5598
5599         case ADDR_EXPR:
5600           non_constant_p = "%<&%>";
5601           /* Fall through.  */
5602         case BIT_NOT_EXPR:
5603           expression = build_x_unary_op (unary_operator, cast_expression,
5604                                          tf_warning_or_error);
5605           break;
5606
5607         case PREINCREMENT_EXPR:
5608         case PREDECREMENT_EXPR:
5609           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5610                             ? "%<++%>" : "%<--%>");
5611           /* Fall through.  */
5612         case UNARY_PLUS_EXPR:
5613         case NEGATE_EXPR:
5614         case TRUTH_NOT_EXPR:
5615           expression = finish_unary_op_expr (unary_operator, cast_expression);
5616           break;
5617
5618         default:
5619           gcc_unreachable ();
5620         }
5621
5622       if (non_constant_p
5623           && cp_parser_non_integral_constant_expression (parser,
5624                                                          non_constant_p))
5625         expression = error_mark_node;
5626
5627       return expression;
5628     }
5629
5630   return cp_parser_postfix_expression (parser, address_p, cast_p,
5631                                        /*member_access_only_p=*/false,
5632                                        pidk);
5633 }
5634
5635 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5636    unary-operator, the corresponding tree code is returned.  */
5637
5638 static enum tree_code
5639 cp_parser_unary_operator (cp_token* token)
5640 {
5641   switch (token->type)
5642     {
5643     case CPP_MULT:
5644       return INDIRECT_REF;
5645
5646     case CPP_AND:
5647       return ADDR_EXPR;
5648
5649     case CPP_PLUS:
5650       return UNARY_PLUS_EXPR;
5651
5652     case CPP_MINUS:
5653       return NEGATE_EXPR;
5654
5655     case CPP_NOT:
5656       return TRUTH_NOT_EXPR;
5657
5658     case CPP_COMPL:
5659       return BIT_NOT_EXPR;
5660
5661     default:
5662       return ERROR_MARK;
5663     }
5664 }
5665
5666 /* Parse a new-expression.
5667
5668    new-expression:
5669      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5670      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5671
5672    Returns a representation of the expression.  */
5673
5674 static tree
5675 cp_parser_new_expression (cp_parser* parser)
5676 {
5677   bool global_scope_p;
5678   VEC(tree,gc) *placement;
5679   tree type;
5680   VEC(tree,gc) *initializer;
5681   tree nelts;
5682   tree ret;
5683
5684   /* Look for the optional `::' operator.  */
5685   global_scope_p
5686     = (cp_parser_global_scope_opt (parser,
5687                                    /*current_scope_valid_p=*/false)
5688        != NULL_TREE);
5689   /* Look for the `new' operator.  */
5690   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5691   /* There's no easy way to tell a new-placement from the
5692      `( type-id )' construct.  */
5693   cp_parser_parse_tentatively (parser);
5694   /* Look for a new-placement.  */
5695   placement = cp_parser_new_placement (parser);
5696   /* If that didn't work out, there's no new-placement.  */
5697   if (!cp_parser_parse_definitely (parser))
5698     {
5699       if (placement != NULL)
5700         release_tree_vector (placement);
5701       placement = NULL;
5702     }
5703
5704   /* If the next token is a `(', then we have a parenthesized
5705      type-id.  */
5706   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5707     {
5708       cp_token *token;
5709       /* Consume the `('.  */
5710       cp_lexer_consume_token (parser->lexer);
5711       /* Parse the type-id.  */
5712       type = cp_parser_type_id (parser);
5713       /* Look for the closing `)'.  */
5714       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5715       token = cp_lexer_peek_token (parser->lexer);
5716       /* There should not be a direct-new-declarator in this production,
5717          but GCC used to allowed this, so we check and emit a sensible error
5718          message for this case.  */
5719       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5720         {
5721           error_at (token->location,
5722                     "array bound forbidden after parenthesized type-id");
5723           inform (token->location, 
5724                   "try removing the parentheses around the type-id");
5725           cp_parser_direct_new_declarator (parser);
5726         }
5727       nelts = NULL_TREE;
5728     }
5729   /* Otherwise, there must be a new-type-id.  */
5730   else
5731     type = cp_parser_new_type_id (parser, &nelts);
5732
5733   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5734   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5735       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5736     initializer = cp_parser_new_initializer (parser);
5737   else
5738     initializer = NULL;
5739
5740   /* A new-expression may not appear in an integral constant
5741      expression.  */
5742   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5743     ret = error_mark_node;
5744   else
5745     {
5746       /* Create a representation of the new-expression.  */
5747       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5748                        tf_warning_or_error);
5749     }
5750
5751   if (placement != NULL)
5752     release_tree_vector (placement);
5753   if (initializer != NULL)
5754     release_tree_vector (initializer);
5755
5756   return ret;
5757 }
5758
5759 /* Parse a new-placement.
5760
5761    new-placement:
5762      ( expression-list )
5763
5764    Returns the same representation as for an expression-list.  */
5765
5766 static VEC(tree,gc) *
5767 cp_parser_new_placement (cp_parser* parser)
5768 {
5769   VEC(tree,gc) *expression_list;
5770
5771   /* Parse the expression-list.  */
5772   expression_list = (cp_parser_parenthesized_expression_list
5773                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5774                       /*non_constant_p=*/NULL));
5775
5776   return expression_list;
5777 }
5778
5779 /* Parse a new-type-id.
5780
5781    new-type-id:
5782      type-specifier-seq new-declarator [opt]
5783
5784    Returns the TYPE allocated.  If the new-type-id indicates an array
5785    type, *NELTS is set to the number of elements in the last array
5786    bound; the TYPE will not include the last array bound.  */
5787
5788 static tree
5789 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5790 {
5791   cp_decl_specifier_seq type_specifier_seq;
5792   cp_declarator *new_declarator;
5793   cp_declarator *declarator;
5794   cp_declarator *outer_declarator;
5795   const char *saved_message;
5796   tree type;
5797
5798   /* The type-specifier sequence must not contain type definitions.
5799      (It cannot contain declarations of new types either, but if they
5800      are not definitions we will catch that because they are not
5801      complete.)  */
5802   saved_message = parser->type_definition_forbidden_message;
5803   parser->type_definition_forbidden_message
5804     = "types may not be defined in a new-type-id";
5805   /* Parse the type-specifier-seq.  */
5806   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5807                                 /*is_trailing_return=*/false,
5808                                 &type_specifier_seq);
5809   /* Restore the old message.  */
5810   parser->type_definition_forbidden_message = saved_message;
5811   /* Parse the new-declarator.  */
5812   new_declarator = cp_parser_new_declarator_opt (parser);
5813
5814   /* Determine the number of elements in the last array dimension, if
5815      any.  */
5816   *nelts = NULL_TREE;
5817   /* Skip down to the last array dimension.  */
5818   declarator = new_declarator;
5819   outer_declarator = NULL;
5820   while (declarator && (declarator->kind == cdk_pointer
5821                         || declarator->kind == cdk_ptrmem))
5822     {
5823       outer_declarator = declarator;
5824       declarator = declarator->declarator;
5825     }
5826   while (declarator
5827          && declarator->kind == cdk_array
5828          && declarator->declarator
5829          && declarator->declarator->kind == cdk_array)
5830     {
5831       outer_declarator = declarator;
5832       declarator = declarator->declarator;
5833     }
5834
5835   if (declarator && declarator->kind == cdk_array)
5836     {
5837       *nelts = declarator->u.array.bounds;
5838       if (*nelts == error_mark_node)
5839         *nelts = integer_one_node;
5840
5841       if (outer_declarator)
5842         outer_declarator->declarator = declarator->declarator;
5843       else
5844         new_declarator = NULL;
5845     }
5846
5847   type = groktypename (&type_specifier_seq, new_declarator, false);
5848   return type;
5849 }
5850
5851 /* Parse an (optional) new-declarator.
5852
5853    new-declarator:
5854      ptr-operator new-declarator [opt]
5855      direct-new-declarator
5856
5857    Returns the declarator.  */
5858
5859 static cp_declarator *
5860 cp_parser_new_declarator_opt (cp_parser* parser)
5861 {
5862   enum tree_code code;
5863   tree type;
5864   cp_cv_quals cv_quals;
5865
5866   /* We don't know if there's a ptr-operator next, or not.  */
5867   cp_parser_parse_tentatively (parser);
5868   /* Look for a ptr-operator.  */
5869   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5870   /* If that worked, look for more new-declarators.  */
5871   if (cp_parser_parse_definitely (parser))
5872     {
5873       cp_declarator *declarator;
5874
5875       /* Parse another optional declarator.  */
5876       declarator = cp_parser_new_declarator_opt (parser);
5877
5878       return cp_parser_make_indirect_declarator
5879         (code, type, cv_quals, declarator);
5880     }
5881
5882   /* If the next token is a `[', there is a direct-new-declarator.  */
5883   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5884     return cp_parser_direct_new_declarator (parser);
5885
5886   return NULL;
5887 }
5888
5889 /* Parse a direct-new-declarator.
5890
5891    direct-new-declarator:
5892      [ expression ]
5893      direct-new-declarator [constant-expression]
5894
5895    */
5896
5897 static cp_declarator *
5898 cp_parser_direct_new_declarator (cp_parser* parser)
5899 {
5900   cp_declarator *declarator = NULL;
5901
5902   while (true)
5903     {
5904       tree expression;
5905
5906       /* Look for the opening `['.  */
5907       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5908       /* The first expression is not required to be constant.  */
5909       if (!declarator)
5910         {
5911           cp_token *token = cp_lexer_peek_token (parser->lexer);
5912           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5913           /* The standard requires that the expression have integral
5914              type.  DR 74 adds enumeration types.  We believe that the
5915              real intent is that these expressions be handled like the
5916              expression in a `switch' condition, which also allows
5917              classes with a single conversion to integral or
5918              enumeration type.  */
5919           if (!processing_template_decl)
5920             {
5921               expression
5922                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5923                                               expression,
5924                                               /*complain=*/true);
5925               if (!expression)
5926                 {
5927                   error_at (token->location,
5928                             "expression in new-declarator must have integral "
5929                             "or enumeration type");
5930                   expression = error_mark_node;
5931                 }
5932             }
5933         }
5934       /* But all the other expressions must be.  */
5935       else
5936         expression
5937           = cp_parser_constant_expression (parser,
5938                                            /*allow_non_constant=*/false,
5939                                            NULL);
5940       /* Look for the closing `]'.  */
5941       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5942
5943       /* Add this bound to the declarator.  */
5944       declarator = make_array_declarator (declarator, expression);
5945
5946       /* If the next token is not a `[', then there are no more
5947          bounds.  */
5948       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5949         break;
5950     }
5951
5952   return declarator;
5953 }
5954
5955 /* Parse a new-initializer.
5956
5957    new-initializer:
5958      ( expression-list [opt] )
5959      braced-init-list
5960
5961    Returns a representation of the expression-list.  */
5962
5963 static VEC(tree,gc) *
5964 cp_parser_new_initializer (cp_parser* parser)
5965 {
5966   VEC(tree,gc) *expression_list;
5967
5968   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5969     {
5970       tree t;
5971       bool expr_non_constant_p;
5972       maybe_warn_cpp0x ("extended initializer lists");
5973       t = cp_parser_braced_list (parser, &expr_non_constant_p);
5974       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5975       expression_list = make_tree_vector_single (t);
5976     }
5977   else
5978     expression_list = (cp_parser_parenthesized_expression_list
5979                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5980                         /*non_constant_p=*/NULL));
5981
5982   return expression_list;
5983 }
5984
5985 /* Parse a delete-expression.
5986
5987    delete-expression:
5988      :: [opt] delete cast-expression
5989      :: [opt] delete [ ] cast-expression
5990
5991    Returns a representation of the expression.  */
5992
5993 static tree
5994 cp_parser_delete_expression (cp_parser* parser)
5995 {
5996   bool global_scope_p;
5997   bool array_p;
5998   tree expression;
5999
6000   /* Look for the optional `::' operator.  */
6001   global_scope_p
6002     = (cp_parser_global_scope_opt (parser,
6003                                    /*current_scope_valid_p=*/false)
6004        != NULL_TREE);
6005   /* Look for the `delete' keyword.  */
6006   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6007   /* See if the array syntax is in use.  */
6008   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6009     {
6010       /* Consume the `[' token.  */
6011       cp_lexer_consume_token (parser->lexer);
6012       /* Look for the `]' token.  */
6013       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6014       /* Remember that this is the `[]' construct.  */
6015       array_p = true;
6016     }
6017   else
6018     array_p = false;
6019
6020   /* Parse the cast-expression.  */
6021   expression = cp_parser_simple_cast_expression (parser);
6022
6023   /* A delete-expression may not appear in an integral constant
6024      expression.  */
6025   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6026     return error_mark_node;
6027
6028   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6029 }
6030
6031 /* Returns true if TOKEN may start a cast-expression and false
6032    otherwise.  */
6033
6034 static bool
6035 cp_parser_token_starts_cast_expression (cp_token *token)
6036 {
6037   switch (token->type)
6038     {
6039     case CPP_COMMA:
6040     case CPP_SEMICOLON:
6041     case CPP_QUERY:
6042     case CPP_COLON:
6043     case CPP_CLOSE_SQUARE:
6044     case CPP_CLOSE_PAREN:
6045     case CPP_CLOSE_BRACE:
6046     case CPP_DOT:
6047     case CPP_DOT_STAR:
6048     case CPP_DEREF:
6049     case CPP_DEREF_STAR:
6050     case CPP_DIV:
6051     case CPP_MOD:
6052     case CPP_LSHIFT:
6053     case CPP_RSHIFT:
6054     case CPP_LESS:
6055     case CPP_GREATER:
6056     case CPP_LESS_EQ:
6057     case CPP_GREATER_EQ:
6058     case CPP_EQ_EQ:
6059     case CPP_NOT_EQ:
6060     case CPP_EQ:
6061     case CPP_MULT_EQ:
6062     case CPP_DIV_EQ:
6063     case CPP_MOD_EQ:
6064     case CPP_PLUS_EQ:
6065     case CPP_MINUS_EQ:
6066     case CPP_RSHIFT_EQ:
6067     case CPP_LSHIFT_EQ:
6068     case CPP_AND_EQ:
6069     case CPP_XOR_EQ:
6070     case CPP_OR_EQ:
6071     case CPP_XOR:
6072     case CPP_OR:
6073     case CPP_OR_OR:
6074     case CPP_EOF:
6075       return false;
6076
6077       /* '[' may start a primary-expression in obj-c++.  */
6078     case CPP_OPEN_SQUARE:
6079       return c_dialect_objc ();
6080
6081     default:
6082       return true;
6083     }
6084 }
6085
6086 /* Parse a cast-expression.
6087
6088    cast-expression:
6089      unary-expression
6090      ( type-id ) cast-expression
6091
6092    ADDRESS_P is true iff the unary-expression is appearing as the
6093    operand of the `&' operator.   CAST_P is true if this expression is
6094    the target of a cast.
6095
6096    Returns a representation of the expression.  */
6097
6098 static tree
6099 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6100                            cp_id_kind * pidk)
6101 {
6102   /* If it's a `(', then we might be looking at a cast.  */
6103   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6104     {
6105       tree type = NULL_TREE;
6106       tree expr = NULL_TREE;
6107       bool compound_literal_p;
6108       const char *saved_message;
6109
6110       /* There's no way to know yet whether or not this is a cast.
6111          For example, `(int (3))' is a unary-expression, while `(int)
6112          3' is a cast.  So, we resort to parsing tentatively.  */
6113       cp_parser_parse_tentatively (parser);
6114       /* Types may not be defined in a cast.  */
6115       saved_message = parser->type_definition_forbidden_message;
6116       parser->type_definition_forbidden_message
6117         = "types may not be defined in casts";
6118       /* Consume the `('.  */
6119       cp_lexer_consume_token (parser->lexer);
6120       /* A very tricky bit is that `(struct S) { 3 }' is a
6121          compound-literal (which we permit in C++ as an extension).
6122          But, that construct is not a cast-expression -- it is a
6123          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6124          is legal; if the compound-literal were a cast-expression,
6125          you'd need an extra set of parentheses.)  But, if we parse
6126          the type-id, and it happens to be a class-specifier, then we
6127          will commit to the parse at that point, because we cannot
6128          undo the action that is done when creating a new class.  So,
6129          then we cannot back up and do a postfix-expression.
6130
6131          Therefore, we scan ahead to the closing `)', and check to see
6132          if the token after the `)' is a `{'.  If so, we are not
6133          looking at a cast-expression.
6134
6135          Save tokens so that we can put them back.  */
6136       cp_lexer_save_tokens (parser->lexer);
6137       /* Skip tokens until the next token is a closing parenthesis.
6138          If we find the closing `)', and the next token is a `{', then
6139          we are looking at a compound-literal.  */
6140       compound_literal_p
6141         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6142                                                   /*consume_paren=*/true)
6143            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6144       /* Roll back the tokens we skipped.  */
6145       cp_lexer_rollback_tokens (parser->lexer);
6146       /* If we were looking at a compound-literal, simulate an error
6147          so that the call to cp_parser_parse_definitely below will
6148          fail.  */
6149       if (compound_literal_p)
6150         cp_parser_simulate_error (parser);
6151       else
6152         {
6153           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6154           parser->in_type_id_in_expr_p = true;
6155           /* Look for the type-id.  */
6156           type = cp_parser_type_id (parser);
6157           /* Look for the closing `)'.  */
6158           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6159           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6160         }
6161
6162       /* Restore the saved message.  */
6163       parser->type_definition_forbidden_message = saved_message;
6164
6165       /* At this point this can only be either a cast or a
6166          parenthesized ctor such as `(T ())' that looks like a cast to
6167          function returning T.  */
6168       if (!cp_parser_error_occurred (parser)
6169           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6170                                                      (parser->lexer)))
6171         {
6172           cp_parser_parse_definitely (parser);
6173           expr = cp_parser_cast_expression (parser,
6174                                             /*address_p=*/false,
6175                                             /*cast_p=*/true, pidk);
6176
6177           /* Warn about old-style casts, if so requested.  */
6178           if (warn_old_style_cast
6179               && !in_system_header
6180               && !VOID_TYPE_P (type)
6181               && current_lang_name != lang_name_c)
6182             warning (OPT_Wold_style_cast, "use of old-style cast");
6183
6184           /* Only type conversions to integral or enumeration types
6185              can be used in constant-expressions.  */
6186           if (!cast_valid_in_integral_constant_expression_p (type)
6187               && (cp_parser_non_integral_constant_expression
6188                   (parser,
6189                    "a cast to a type other than an integral or "
6190                    "enumeration type")))
6191             return error_mark_node;
6192
6193           /* Perform the cast.  */
6194           expr = build_c_cast (input_location, type, expr);
6195           return expr;
6196         }
6197       else 
6198         cp_parser_abort_tentative_parse (parser);
6199     }
6200
6201   /* If we get here, then it's not a cast, so it must be a
6202      unary-expression.  */
6203   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6204 }
6205
6206 /* Parse a binary expression of the general form:
6207
6208    pm-expression:
6209      cast-expression
6210      pm-expression .* cast-expression
6211      pm-expression ->* cast-expression
6212
6213    multiplicative-expression:
6214      pm-expression
6215      multiplicative-expression * pm-expression
6216      multiplicative-expression / pm-expression
6217      multiplicative-expression % pm-expression
6218
6219    additive-expression:
6220      multiplicative-expression
6221      additive-expression + multiplicative-expression
6222      additive-expression - multiplicative-expression
6223
6224    shift-expression:
6225      additive-expression
6226      shift-expression << additive-expression
6227      shift-expression >> additive-expression
6228
6229    relational-expression:
6230      shift-expression
6231      relational-expression < shift-expression
6232      relational-expression > shift-expression
6233      relational-expression <= shift-expression
6234      relational-expression >= shift-expression
6235
6236   GNU Extension:
6237
6238    relational-expression:
6239      relational-expression <? shift-expression
6240      relational-expression >? shift-expression
6241
6242    equality-expression:
6243      relational-expression
6244      equality-expression == relational-expression
6245      equality-expression != relational-expression
6246
6247    and-expression:
6248      equality-expression
6249      and-expression & equality-expression
6250
6251    exclusive-or-expression:
6252      and-expression
6253      exclusive-or-expression ^ and-expression
6254
6255    inclusive-or-expression:
6256      exclusive-or-expression
6257      inclusive-or-expression | exclusive-or-expression
6258
6259    logical-and-expression:
6260      inclusive-or-expression
6261      logical-and-expression && inclusive-or-expression
6262
6263    logical-or-expression:
6264      logical-and-expression
6265      logical-or-expression || logical-and-expression
6266
6267    All these are implemented with a single function like:
6268
6269    binary-expression:
6270      simple-cast-expression
6271      binary-expression <token> binary-expression
6272
6273    CAST_P is true if this expression is the target of a cast.
6274
6275    The binops_by_token map is used to get the tree codes for each <token> type.
6276    binary-expressions are associated according to a precedence table.  */
6277
6278 #define TOKEN_PRECEDENCE(token)                              \
6279 (((token->type == CPP_GREATER                                \
6280    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6281   && !parser->greater_than_is_operator_p)                    \
6282  ? PREC_NOT_OPERATOR                                         \
6283  : binops_by_token[token->type].prec)
6284
6285 static tree
6286 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6287                              bool no_toplevel_fold_p,
6288                              enum cp_parser_prec prec,
6289                              cp_id_kind * pidk)
6290 {
6291   cp_parser_expression_stack stack;
6292   cp_parser_expression_stack_entry *sp = &stack[0];
6293   tree lhs, rhs;
6294   cp_token *token;
6295   enum tree_code tree_type, lhs_type, rhs_type;
6296   enum cp_parser_prec new_prec, lookahead_prec;
6297   bool overloaded_p;
6298
6299   /* Parse the first expression.  */
6300   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6301   lhs_type = ERROR_MARK;
6302
6303   for (;;)
6304     {
6305       /* Get an operator token.  */
6306       token = cp_lexer_peek_token (parser->lexer);
6307
6308       if (warn_cxx0x_compat
6309           && token->type == CPP_RSHIFT
6310           && !parser->greater_than_is_operator_p)
6311         {
6312           if (warning_at (token->location, OPT_Wc__0x_compat, 
6313                           "%<>>%> operator will be treated as"
6314                           " two right angle brackets in C++0x"))
6315             inform (token->location,
6316                     "suggest parentheses around %<>>%> expression");
6317         }
6318
6319       new_prec = TOKEN_PRECEDENCE (token);
6320
6321       /* Popping an entry off the stack means we completed a subexpression:
6322          - either we found a token which is not an operator (`>' where it is not
6323            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6324            will happen repeatedly;
6325          - or, we found an operator which has lower priority.  This is the case
6326            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6327            parsing `3 * 4'.  */
6328       if (new_prec <= prec)
6329         {
6330           if (sp == stack)
6331             break;
6332           else
6333             goto pop;
6334         }
6335
6336      get_rhs:
6337       tree_type = binops_by_token[token->type].tree_type;
6338
6339       /* We used the operator token.  */
6340       cp_lexer_consume_token (parser->lexer);
6341
6342       /* For "false && x" or "true || x", x will never be executed;
6343          disable warnings while evaluating it.  */
6344       if (tree_type == TRUTH_ANDIF_EXPR)
6345         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6346       else if (tree_type == TRUTH_ORIF_EXPR)
6347         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6348
6349       /* Extract another operand.  It may be the RHS of this expression
6350          or the LHS of a new, higher priority expression.  */
6351       rhs = cp_parser_simple_cast_expression (parser);
6352       rhs_type = ERROR_MARK;
6353
6354       /* Get another operator token.  Look up its precedence to avoid
6355          building a useless (immediately popped) stack entry for common
6356          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6357       token = cp_lexer_peek_token (parser->lexer);
6358       lookahead_prec = TOKEN_PRECEDENCE (token);
6359       if (lookahead_prec > new_prec)
6360         {
6361           /* ... and prepare to parse the RHS of the new, higher priority
6362              expression.  Since precedence levels on the stack are
6363              monotonically increasing, we do not have to care about
6364              stack overflows.  */
6365           sp->prec = prec;
6366           sp->tree_type = tree_type;
6367           sp->lhs = lhs;
6368           sp->lhs_type = lhs_type;
6369           sp++;
6370           lhs = rhs;
6371           lhs_type = rhs_type;
6372           prec = new_prec;
6373           new_prec = lookahead_prec;
6374           goto get_rhs;
6375
6376          pop:
6377           lookahead_prec = new_prec;
6378           /* If the stack is not empty, we have parsed into LHS the right side
6379              (`4' in the example above) of an expression we had suspended.
6380              We can use the information on the stack to recover the LHS (`3')
6381              from the stack together with the tree code (`MULT_EXPR'), and
6382              the precedence of the higher level subexpression
6383              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6384              which will be used to actually build the additive expression.  */
6385           --sp;
6386           prec = sp->prec;
6387           tree_type = sp->tree_type;
6388           rhs = lhs;
6389           rhs_type = lhs_type;
6390           lhs = sp->lhs;
6391           lhs_type = sp->lhs_type;
6392         }
6393
6394       /* Undo the disabling of warnings done above.  */
6395       if (tree_type == TRUTH_ANDIF_EXPR)
6396         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6397       else if (tree_type == TRUTH_ORIF_EXPR)
6398         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6399
6400       overloaded_p = false;
6401       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6402          ERROR_MARK for everything that is not a binary expression.
6403          This makes warn_about_parentheses miss some warnings that
6404          involve unary operators.  For unary expressions we should
6405          pass the correct tree_code unless the unary expression was
6406          surrounded by parentheses.
6407       */
6408       if (no_toplevel_fold_p
6409           && lookahead_prec <= prec
6410           && sp == stack
6411           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6412         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6413       else
6414         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6415                                  &overloaded_p, tf_warning_or_error);
6416       lhs_type = tree_type;
6417
6418       /* If the binary operator required the use of an overloaded operator,
6419          then this expression cannot be an integral constant-expression.
6420          An overloaded operator can be used even if both operands are
6421          otherwise permissible in an integral constant-expression if at
6422          least one of the operands is of enumeration type.  */
6423
6424       if (overloaded_p
6425           && (cp_parser_non_integral_constant_expression
6426               (parser, "calls to overloaded operators")))
6427         return error_mark_node;
6428     }
6429
6430   return lhs;
6431 }
6432
6433
6434 /* Parse the `? expression : assignment-expression' part of a
6435    conditional-expression.  The LOGICAL_OR_EXPR is the
6436    logical-or-expression that started the conditional-expression.
6437    Returns a representation of the entire conditional-expression.
6438
6439    This routine is used by cp_parser_assignment_expression.
6440
6441      ? expression : assignment-expression
6442
6443    GNU Extensions:
6444
6445      ? : assignment-expression */
6446
6447 static tree
6448 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6449 {
6450   tree expr;
6451   tree assignment_expr;
6452
6453   /* Consume the `?' token.  */
6454   cp_lexer_consume_token (parser->lexer);
6455   if (cp_parser_allow_gnu_extensions_p (parser)
6456       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6457     {
6458       /* Implicit true clause.  */
6459       expr = NULL_TREE;
6460       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6461     }
6462   else
6463     {
6464       /* Parse the expression.  */
6465       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6466       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6467       c_inhibit_evaluation_warnings +=
6468         ((logical_or_expr == truthvalue_true_node)
6469          - (logical_or_expr == truthvalue_false_node));
6470     }
6471
6472   /* The next token should be a `:'.  */
6473   cp_parser_require (parser, CPP_COLON, "%<:%>");
6474   /* Parse the assignment-expression.  */
6475   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6476   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6477
6478   /* Build the conditional-expression.  */
6479   return build_x_conditional_expr (logical_or_expr,
6480                                    expr,
6481                                    assignment_expr,
6482                                    tf_warning_or_error);
6483 }
6484
6485 /* Parse an assignment-expression.
6486
6487    assignment-expression:
6488      conditional-expression
6489      logical-or-expression assignment-operator assignment_expression
6490      throw-expression
6491
6492    CAST_P is true if this expression is the target of a cast.
6493
6494    Returns a representation for the expression.  */
6495
6496 static tree
6497 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6498                                  cp_id_kind * pidk)
6499 {
6500   tree expr;
6501
6502   /* If the next token is the `throw' keyword, then we're looking at
6503      a throw-expression.  */
6504   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6505     expr = cp_parser_throw_expression (parser);
6506   /* Otherwise, it must be that we are looking at a
6507      logical-or-expression.  */
6508   else
6509     {
6510       /* Parse the binary expressions (logical-or-expression).  */
6511       expr = cp_parser_binary_expression (parser, cast_p, false,
6512                                           PREC_NOT_OPERATOR, pidk);
6513       /* If the next token is a `?' then we're actually looking at a
6514          conditional-expression.  */
6515       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6516         return cp_parser_question_colon_clause (parser, expr);
6517       else
6518         {
6519           enum tree_code assignment_operator;
6520
6521           /* If it's an assignment-operator, we're using the second
6522              production.  */
6523           assignment_operator
6524             = cp_parser_assignment_operator_opt (parser);
6525           if (assignment_operator != ERROR_MARK)
6526             {
6527               bool non_constant_p;
6528
6529               /* Parse the right-hand side of the assignment.  */
6530               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6531
6532               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6533                 maybe_warn_cpp0x ("extended initializer lists");
6534
6535               /* An assignment may not appear in a
6536                  constant-expression.  */
6537               if (cp_parser_non_integral_constant_expression (parser,
6538                                                               "an assignment"))
6539                 return error_mark_node;
6540               /* Build the assignment expression.  */
6541               expr = build_x_modify_expr (expr,
6542                                           assignment_operator,
6543                                           rhs,
6544                                           tf_warning_or_error);
6545             }
6546         }
6547     }
6548
6549   return expr;
6550 }
6551
6552 /* Parse an (optional) assignment-operator.
6553
6554    assignment-operator: one of
6555      = *= /= %= += -= >>= <<= &= ^= |=
6556
6557    GNU Extension:
6558
6559    assignment-operator: one of
6560      <?= >?=
6561
6562    If the next token is an assignment operator, the corresponding tree
6563    code is returned, and the token is consumed.  For example, for
6564    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6565    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6566    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6567    operator, ERROR_MARK is returned.  */
6568
6569 static enum tree_code
6570 cp_parser_assignment_operator_opt (cp_parser* parser)
6571 {
6572   enum tree_code op;
6573   cp_token *token;
6574
6575   /* Peek at the next token.  */
6576   token = cp_lexer_peek_token (parser->lexer);
6577
6578   switch (token->type)
6579     {
6580     case CPP_EQ:
6581       op = NOP_EXPR;
6582       break;
6583
6584     case CPP_MULT_EQ:
6585       op = MULT_EXPR;
6586       break;
6587
6588     case CPP_DIV_EQ:
6589       op = TRUNC_DIV_EXPR;
6590       break;
6591
6592     case CPP_MOD_EQ:
6593       op = TRUNC_MOD_EXPR;
6594       break;
6595
6596     case CPP_PLUS_EQ:
6597       op = PLUS_EXPR;
6598       break;
6599
6600     case CPP_MINUS_EQ:
6601       op = MINUS_EXPR;
6602       break;
6603
6604     case CPP_RSHIFT_EQ:
6605       op = RSHIFT_EXPR;
6606       break;
6607
6608     case CPP_LSHIFT_EQ:
6609       op = LSHIFT_EXPR;
6610       break;
6611
6612     case CPP_AND_EQ:
6613       op = BIT_AND_EXPR;
6614       break;
6615
6616     case CPP_XOR_EQ:
6617       op = BIT_XOR_EXPR;
6618       break;
6619
6620     case CPP_OR_EQ:
6621       op = BIT_IOR_EXPR;
6622       break;
6623
6624     default:
6625       /* Nothing else is an assignment operator.  */
6626       op = ERROR_MARK;
6627     }
6628
6629   /* If it was an assignment operator, consume it.  */
6630   if (op != ERROR_MARK)
6631     cp_lexer_consume_token (parser->lexer);
6632
6633   return op;
6634 }
6635
6636 /* Parse an expression.
6637
6638    expression:
6639      assignment-expression
6640      expression , assignment-expression
6641
6642    CAST_P is true if this expression is the target of a cast.
6643
6644    Returns a representation of the expression.  */
6645
6646 static tree
6647 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6648 {
6649   tree expression = NULL_TREE;
6650
6651   while (true)
6652     {
6653       tree assignment_expression;
6654
6655       /* Parse the next assignment-expression.  */
6656       assignment_expression
6657         = cp_parser_assignment_expression (parser, cast_p, pidk);
6658       /* If this is the first assignment-expression, we can just
6659          save it away.  */
6660       if (!expression)
6661         expression = assignment_expression;
6662       else
6663         expression = build_x_compound_expr (expression,
6664                                             assignment_expression,
6665                                             tf_warning_or_error);
6666       /* If the next token is not a comma, then we are done with the
6667          expression.  */
6668       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6669         break;
6670       /* Consume the `,'.  */
6671       cp_lexer_consume_token (parser->lexer);
6672       /* A comma operator cannot appear in a constant-expression.  */
6673       if (cp_parser_non_integral_constant_expression (parser,
6674                                                       "a comma operator"))
6675         expression = error_mark_node;
6676     }
6677
6678   return expression;
6679 }
6680
6681 /* Parse a constant-expression.
6682
6683    constant-expression:
6684      conditional-expression
6685
6686   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6687   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6688   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6689   is false, NON_CONSTANT_P should be NULL.  */
6690
6691 static tree
6692 cp_parser_constant_expression (cp_parser* parser,
6693                                bool allow_non_constant_p,
6694                                bool *non_constant_p)
6695 {
6696   bool saved_integral_constant_expression_p;
6697   bool saved_allow_non_integral_constant_expression_p;
6698   bool saved_non_integral_constant_expression_p;
6699   tree expression;
6700
6701   /* It might seem that we could simply parse the
6702      conditional-expression, and then check to see if it were
6703      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6704      one that the compiler can figure out is constant, possibly after
6705      doing some simplifications or optimizations.  The standard has a
6706      precise definition of constant-expression, and we must honor
6707      that, even though it is somewhat more restrictive.
6708
6709      For example:
6710
6711        int i[(2, 3)];
6712
6713      is not a legal declaration, because `(2, 3)' is not a
6714      constant-expression.  The `,' operator is forbidden in a
6715      constant-expression.  However, GCC's constant-folding machinery
6716      will fold this operation to an INTEGER_CST for `3'.  */
6717
6718   /* Save the old settings.  */
6719   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6720   saved_allow_non_integral_constant_expression_p
6721     = parser->allow_non_integral_constant_expression_p;
6722   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6723   /* We are now parsing a constant-expression.  */
6724   parser->integral_constant_expression_p = true;
6725   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6726   parser->non_integral_constant_expression_p = false;
6727   /* Although the grammar says "conditional-expression", we parse an
6728      "assignment-expression", which also permits "throw-expression"
6729      and the use of assignment operators.  In the case that
6730      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6731      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6732      actually essential that we look for an assignment-expression.
6733      For example, cp_parser_initializer_clauses uses this function to
6734      determine whether a particular assignment-expression is in fact
6735      constant.  */
6736   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6737   /* Restore the old settings.  */
6738   parser->integral_constant_expression_p
6739     = saved_integral_constant_expression_p;
6740   parser->allow_non_integral_constant_expression_p
6741     = saved_allow_non_integral_constant_expression_p;
6742   if (allow_non_constant_p)
6743     *non_constant_p = parser->non_integral_constant_expression_p;
6744   else if (parser->non_integral_constant_expression_p)
6745     expression = error_mark_node;
6746   parser->non_integral_constant_expression_p
6747     = saved_non_integral_constant_expression_p;
6748
6749   return expression;
6750 }
6751
6752 /* Parse __builtin_offsetof.
6753
6754    offsetof-expression:
6755      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6756
6757    offsetof-member-designator:
6758      id-expression
6759      | offsetof-member-designator "." id-expression
6760      | offsetof-member-designator "[" expression "]"
6761      | offsetof-member-designator "->" id-expression  */
6762
6763 static tree
6764 cp_parser_builtin_offsetof (cp_parser *parser)
6765 {
6766   int save_ice_p, save_non_ice_p;
6767   tree type, expr;
6768   cp_id_kind dummy;
6769   cp_token *token;
6770
6771   /* We're about to accept non-integral-constant things, but will
6772      definitely yield an integral constant expression.  Save and
6773      restore these values around our local parsing.  */
6774   save_ice_p = parser->integral_constant_expression_p;
6775   save_non_ice_p = parser->non_integral_constant_expression_p;
6776
6777   /* Consume the "__builtin_offsetof" token.  */
6778   cp_lexer_consume_token (parser->lexer);
6779   /* Consume the opening `('.  */
6780   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6781   /* Parse the type-id.  */
6782   type = cp_parser_type_id (parser);
6783   /* Look for the `,'.  */
6784   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6785   token = cp_lexer_peek_token (parser->lexer);
6786
6787   /* Build the (type *)null that begins the traditional offsetof macro.  */
6788   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6789                             tf_warning_or_error);
6790
6791   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6792   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6793                                                  true, &dummy, token->location);
6794   while (true)
6795     {
6796       token = cp_lexer_peek_token (parser->lexer);
6797       switch (token->type)
6798         {
6799         case CPP_OPEN_SQUARE:
6800           /* offsetof-member-designator "[" expression "]" */
6801           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6802           break;
6803
6804         case CPP_DEREF:
6805           /* offsetof-member-designator "->" identifier */
6806           expr = grok_array_decl (expr, integer_zero_node);
6807           /* FALLTHRU */
6808
6809         case CPP_DOT:
6810           /* offsetof-member-designator "." identifier */
6811           cp_lexer_consume_token (parser->lexer);
6812           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6813                                                          expr, true, &dummy,
6814                                                          token->location);
6815           break;
6816
6817         case CPP_CLOSE_PAREN:
6818           /* Consume the ")" token.  */
6819           cp_lexer_consume_token (parser->lexer);
6820           goto success;
6821
6822         default:
6823           /* Error.  We know the following require will fail, but
6824              that gives the proper error message.  */
6825           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6826           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6827           expr = error_mark_node;
6828           goto failure;
6829         }
6830     }
6831
6832  success:
6833   /* If we're processing a template, we can't finish the semantics yet.
6834      Otherwise we can fold the entire expression now.  */
6835   if (processing_template_decl)
6836     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6837   else
6838     expr = finish_offsetof (expr);
6839
6840  failure:
6841   parser->integral_constant_expression_p = save_ice_p;
6842   parser->non_integral_constant_expression_p = save_non_ice_p;
6843
6844   return expr;
6845 }
6846
6847 /* Parse a trait expression.  */
6848
6849 static tree
6850 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6851 {
6852   cp_trait_kind kind;
6853   tree type1, type2 = NULL_TREE;
6854   bool binary = false;
6855   cp_decl_specifier_seq decl_specs;
6856
6857   switch (keyword)
6858     {
6859     case RID_HAS_NOTHROW_ASSIGN:
6860       kind = CPTK_HAS_NOTHROW_ASSIGN;
6861       break;
6862     case RID_HAS_NOTHROW_CONSTRUCTOR:
6863       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6864       break;
6865     case RID_HAS_NOTHROW_COPY:
6866       kind = CPTK_HAS_NOTHROW_COPY;
6867       break;
6868     case RID_HAS_TRIVIAL_ASSIGN:
6869       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6870       break;
6871     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6872       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6873       break;
6874     case RID_HAS_TRIVIAL_COPY:
6875       kind = CPTK_HAS_TRIVIAL_COPY;
6876       break;
6877     case RID_HAS_TRIVIAL_DESTRUCTOR:
6878       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6879       break;
6880     case RID_HAS_VIRTUAL_DESTRUCTOR:
6881       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6882       break;
6883     case RID_IS_ABSTRACT:
6884       kind = CPTK_IS_ABSTRACT;
6885       break;
6886     case RID_IS_BASE_OF:
6887       kind = CPTK_IS_BASE_OF;
6888       binary = true;
6889       break;
6890     case RID_IS_CLASS:
6891       kind = CPTK_IS_CLASS;
6892       break;
6893     case RID_IS_CONVERTIBLE_TO:
6894       kind = CPTK_IS_CONVERTIBLE_TO;
6895       binary = true;
6896       break;
6897     case RID_IS_EMPTY:
6898       kind = CPTK_IS_EMPTY;
6899       break;
6900     case RID_IS_ENUM:
6901       kind = CPTK_IS_ENUM;
6902       break;
6903     case RID_IS_POD:
6904       kind = CPTK_IS_POD;
6905       break;
6906     case RID_IS_POLYMORPHIC:
6907       kind = CPTK_IS_POLYMORPHIC;
6908       break;
6909     case RID_IS_STD_LAYOUT:
6910       kind = CPTK_IS_STD_LAYOUT;
6911       break;
6912     case RID_IS_TRIVIAL:
6913       kind = CPTK_IS_TRIVIAL;
6914       break;
6915     case RID_IS_UNION:
6916       kind = CPTK_IS_UNION;
6917       break;
6918     default:
6919       gcc_unreachable ();
6920     }
6921
6922   /* Consume the token.  */
6923   cp_lexer_consume_token (parser->lexer);
6924
6925   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6926
6927   type1 = cp_parser_type_id (parser);
6928
6929   if (type1 == error_mark_node)
6930     return error_mark_node;
6931
6932   /* Build a trivial decl-specifier-seq.  */
6933   clear_decl_specs (&decl_specs);
6934   decl_specs.type = type1;
6935
6936   /* Call grokdeclarator to figure out what type this is.  */
6937   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6938                           /*initialized=*/0, /*attrlist=*/NULL);
6939
6940   if (binary)
6941     {
6942       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6943  
6944       type2 = cp_parser_type_id (parser);
6945
6946       if (type2 == error_mark_node)
6947         return error_mark_node;
6948
6949       /* Build a trivial decl-specifier-seq.  */
6950       clear_decl_specs (&decl_specs);
6951       decl_specs.type = type2;
6952
6953       /* Call grokdeclarator to figure out what type this is.  */
6954       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6955                               /*initialized=*/0, /*attrlist=*/NULL);
6956     }
6957
6958   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6959
6960   /* Complete the trait expression, which may mean either processing
6961      the trait expr now or saving it for template instantiation.  */
6962   return finish_trait_expr (kind, type1, type2);
6963 }
6964
6965 /* Lambdas that appear in variable initializer or default argument scope
6966    get that in their mangling, so we need to record it.  We might as well
6967    use the count for function and namespace scopes as well.  */
6968 static GTY(()) tree lambda_scope;
6969 static GTY(()) int lambda_count;
6970 typedef struct GTY(()) tree_int
6971 {
6972   tree t;
6973   int i;
6974 } tree_int;
6975 DEF_VEC_O(tree_int);
6976 DEF_VEC_ALLOC_O(tree_int,gc);
6977 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
6978
6979 static void
6980 start_lambda_scope (tree decl)
6981 {
6982   tree_int ti;
6983   gcc_assert (decl);
6984   /* Once we're inside a function, we ignore other scopes and just push
6985      the function again so that popping works properly.  */
6986   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
6987     decl = current_function_decl;
6988   ti.t = lambda_scope;
6989   ti.i = lambda_count;
6990   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
6991   if (lambda_scope != decl)
6992     {
6993       /* Don't reset the count if we're still in the same function.  */
6994       lambda_scope = decl;
6995       lambda_count = 0;
6996     }
6997 }
6998
6999 static void
7000 record_lambda_scope (tree lambda)
7001 {
7002   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7003   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7004 }
7005
7006 static void
7007 finish_lambda_scope (void)
7008 {
7009   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7010   if (lambda_scope != p->t)
7011     {
7012       lambda_scope = p->t;
7013       lambda_count = p->i;
7014     }
7015   VEC_pop (tree_int, lambda_scope_stack);
7016 }
7017
7018 /* Parse a lambda expression.
7019
7020    lambda-expression:
7021      lambda-introducer lambda-declarator [opt] compound-statement
7022
7023    Returns a representation of the expression.  */
7024
7025 static tree
7026 cp_parser_lambda_expression (cp_parser* parser)
7027 {
7028   tree lambda_expr = build_lambda_expr ();
7029   tree type;
7030
7031   LAMBDA_EXPR_LOCATION (lambda_expr)
7032     = cp_lexer_peek_token (parser->lexer)->location;
7033
7034   /* We may be in the middle of deferred access check.  Disable
7035      it now.  */
7036   push_deferring_access_checks (dk_no_deferred);
7037
7038   type = begin_lambda_type (lambda_expr);
7039
7040   record_lambda_scope (lambda_expr);
7041
7042   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7043   determine_visibility (TYPE_NAME (type));
7044
7045   {
7046     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7047     unsigned int saved_num_template_parameter_lists
7048         = parser->num_template_parameter_lists;
7049
7050     parser->num_template_parameter_lists = 0;
7051
7052     cp_parser_lambda_introducer (parser, lambda_expr);
7053
7054     /* By virtue of defining a local class, a lambda expression has access to
7055        the private variables of enclosing classes.  */
7056
7057     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7058
7059     cp_parser_lambda_body (parser, lambda_expr);
7060
7061     /* The capture list was built up in reverse order; fix that now.  */
7062     {
7063       tree newlist = NULL_TREE;
7064       tree elt, next;
7065
7066       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7067            elt; elt = next)
7068         {
7069           tree field = TREE_PURPOSE (elt);
7070           char *buf;
7071
7072           next = TREE_CHAIN (elt);
7073           TREE_CHAIN (elt) = newlist;
7074           newlist = elt;
7075
7076           /* Also add __ to the beginning of the field name so that code
7077              outside the lambda body can't see the captured name.  We could
7078              just remove the name entirely, but this is more useful for
7079              debugging.  */
7080           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7081             /* The 'this' capture already starts with __.  */
7082             continue;
7083
7084           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7085           buf[1] = buf[0] = '_';
7086           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7087                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7088           DECL_NAME (field) = get_identifier (buf);
7089         }
7090       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7091     }
7092
7093     maybe_add_lambda_conv_op (type);
7094
7095     type = finish_struct (type, /*attributes=*/NULL_TREE);
7096
7097     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7098   }
7099
7100   pop_deferring_access_checks ();
7101
7102   return build_lambda_object (lambda_expr);
7103 }
7104
7105 /* Parse the beginning of a lambda expression.
7106
7107    lambda-introducer:
7108      [ lambda-capture [opt] ]
7109
7110    LAMBDA_EXPR is the current representation of the lambda expression.  */
7111
7112 static void
7113 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7114 {
7115   /* Need commas after the first capture.  */
7116   bool first = true;
7117
7118   /* Eat the leading `['.  */
7119   cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7120
7121   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7122   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7123       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7124     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7125   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7126     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7127
7128   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7129     {
7130       cp_lexer_consume_token (parser->lexer);
7131       first = false;
7132     }
7133
7134   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7135     {
7136       cp_token* capture_token;
7137       tree capture_id;
7138       tree capture_init_expr;
7139       cp_id_kind idk = CP_ID_KIND_NONE;
7140       bool explicit_init_p = false;
7141
7142       enum capture_kind_type
7143       {
7144         BY_COPY,
7145         BY_REFERENCE
7146       };
7147       enum capture_kind_type capture_kind = BY_COPY;
7148
7149       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7150         {
7151           error ("expected end of capture-list");
7152           return;
7153         }
7154
7155       if (first)
7156         first = false;
7157       else
7158         cp_parser_require (parser, CPP_COMMA, "%<,%>");
7159
7160       /* Possibly capture `this'.  */
7161       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7162         {
7163           cp_lexer_consume_token (parser->lexer);
7164           add_capture (lambda_expr,
7165                        /*id=*/get_identifier ("__this"),
7166                        /*initializer=*/finish_this_expr(),
7167                        /*by_reference_p=*/false,
7168                        explicit_init_p);
7169           continue;
7170         }
7171
7172       /* Remember whether we want to capture as a reference or not.  */
7173       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7174         {
7175           capture_kind = BY_REFERENCE;
7176           cp_lexer_consume_token (parser->lexer);
7177         }
7178
7179       /* Get the identifier.  */
7180       capture_token = cp_lexer_peek_token (parser->lexer);
7181       capture_id = cp_parser_identifier (parser);
7182
7183       if (capture_id == error_mark_node)
7184         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7185            delimiters, but I modified this to stop on unnested ']' as well.  It
7186            was already changed to stop on unnested '}', so the
7187            "closing_parenthesis" name is no more misleading with my change.  */
7188         {
7189           cp_parser_skip_to_closing_parenthesis (parser,
7190                                                  /*recovering=*/true,
7191                                                  /*or_comma=*/true,
7192                                                  /*consume_paren=*/true);
7193           break;
7194         }
7195
7196       /* Find the initializer for this capture.  */
7197       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7198         {
7199           /* An explicit expression exists.  */
7200           cp_lexer_consume_token (parser->lexer);
7201           pedwarn (input_location, OPT_pedantic,
7202                    "ISO C++ does not allow initializers "
7203                    "in lambda expression capture lists");
7204           capture_init_expr = cp_parser_assignment_expression (parser,
7205                                                                /*cast_p=*/true,
7206                                                                &idk);
7207           explicit_init_p = true;
7208         }
7209       else
7210         {
7211           const char* error_msg;
7212
7213           /* Turn the identifier into an id-expression.  */
7214           capture_init_expr
7215             = cp_parser_lookup_name
7216                 (parser,
7217                  capture_id,
7218                  none_type,
7219                  /*is_template=*/false,
7220                  /*is_namespace=*/false,
7221                  /*check_dependency=*/true,
7222                  /*ambiguous_decls=*/NULL,
7223                  capture_token->location);
7224
7225           capture_init_expr
7226             = finish_id_expression
7227                 (capture_id,
7228                  capture_init_expr,
7229                  parser->scope,
7230                  &idk,
7231                  /*integral_constant_expression_p=*/false,
7232                  /*allow_non_integral_constant_expression_p=*/false,
7233                  /*non_integral_constant_expression_p=*/NULL,
7234                  /*template_p=*/false,
7235                  /*done=*/true,
7236                  /*address_p=*/false,
7237                  /*template_arg_p=*/false,
7238                  &error_msg,
7239                  capture_token->location);
7240         }
7241
7242       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7243         capture_init_expr
7244           = unqualified_name_lookup_error (capture_init_expr);
7245
7246       add_capture (lambda_expr,
7247                    capture_id,
7248                    capture_init_expr,
7249                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7250                    explicit_init_p);
7251     }
7252
7253   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7254 }
7255
7256 /* Parse the (optional) middle of a lambda expression.
7257
7258    lambda-declarator:
7259      ( parameter-declaration-clause [opt] )
7260        attribute-specifier [opt]
7261        mutable [opt]
7262        exception-specification [opt]
7263        lambda-return-type-clause [opt]
7264
7265    LAMBDA_EXPR is the current representation of the lambda expression.  */
7266
7267 static void
7268 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7269 {
7270   /* 5.1.1.4 of the standard says:
7271        If a lambda-expression does not include a lambda-declarator, it is as if
7272        the lambda-declarator were ().
7273      This means an empty parameter list, no attributes, and no exception
7274      specification.  */
7275   tree param_list = void_list_node;
7276   tree attributes = NULL_TREE;
7277   tree exception_spec = NULL_TREE;
7278   tree t;
7279
7280   /* The lambda-declarator is optional, but must begin with an opening
7281      parenthesis if present.  */
7282   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7283     {
7284       cp_lexer_consume_token (parser->lexer);
7285
7286       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7287
7288       /* Parse parameters.  */
7289       param_list = cp_parser_parameter_declaration_clause (parser);
7290
7291       /* Default arguments shall not be specified in the
7292          parameter-declaration-clause of a lambda-declarator.  */
7293       for (t = param_list; t; t = TREE_CHAIN (t))
7294         if (TREE_PURPOSE (t))
7295           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7296                    "default argument specified for lambda parameter");
7297
7298       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7299
7300       attributes = cp_parser_attributes_opt (parser);
7301
7302       /* Parse optional `mutable' keyword.  */
7303       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7304         {
7305           cp_lexer_consume_token (parser->lexer);
7306           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7307         }
7308
7309       /* Parse optional exception specification.  */
7310       exception_spec = cp_parser_exception_specification_opt (parser);
7311
7312       /* Parse optional trailing return type.  */
7313       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7314         {
7315           cp_lexer_consume_token (parser->lexer);
7316           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7317         }
7318
7319       /* The function parameters must be in scope all the way until after the
7320          trailing-return-type in case of decltype.  */
7321       for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7322         pop_binding (DECL_NAME (t), t);
7323
7324       leave_scope ();
7325     }
7326
7327   /* Create the function call operator.
7328
7329      Messing with declarators like this is no uglier than building up the
7330      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7331      other code.  */
7332   {
7333     cp_decl_specifier_seq return_type_specs;
7334     cp_declarator* declarator;
7335     tree fco;
7336     int quals;
7337     void *p;
7338
7339     clear_decl_specs (&return_type_specs);
7340     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7341       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7342     else
7343       /* Maybe we will deduce the return type later, but we can use void
7344          as a placeholder return type anyways.  */
7345       return_type_specs.type = void_type_node;
7346
7347     p = obstack_alloc (&declarator_obstack, 0);
7348
7349     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7350                                      sfk_none);
7351
7352     quals = TYPE_UNQUALIFIED;
7353     if (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) == NULL_TREE
7354         && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
7355       {
7356         /* A lambda with no captures has a static op() and a conversion op
7357            to function type.  */
7358         if (LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7359           error ("lambda expression with no captures declared mutable");
7360         return_type_specs.storage_class = sc_static;
7361       }
7362     else if (!LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7363       quals = TYPE_QUAL_CONST;
7364     declarator = make_call_declarator (declarator, param_list, quals,
7365                                        exception_spec,
7366                                        /*late_return_type=*/NULL_TREE);
7367
7368     fco = grokmethod (&return_type_specs,
7369                       declarator,
7370                       attributes);
7371     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7372     DECL_ARTIFICIAL (fco) = 1;
7373
7374     finish_member_declaration (fco);
7375
7376     obstack_free (&declarator_obstack, p);
7377   }
7378 }
7379
7380 /* Parse the body of a lambda expression, which is simply
7381
7382    compound-statement
7383
7384    but which requires special handling.
7385    LAMBDA_EXPR is the current representation of the lambda expression.  */
7386
7387 static void
7388 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7389 {
7390   bool nested = (current_function_decl != NULL_TREE);
7391   if (nested)
7392     push_function_context ();
7393
7394   /* Finish the function call operator
7395      - class_specifier
7396      + late_parsing_for_member
7397      + function_definition_after_declarator
7398      + ctor_initializer_opt_and_function_body  */
7399   {
7400     tree fco = lambda_function (lambda_expr);
7401     tree body;
7402     bool done = false;
7403
7404     /* Let the front end know that we are going to be defining this
7405        function.  */
7406     start_preparsed_function (fco,
7407                               NULL_TREE,
7408                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7409
7410     start_lambda_scope (fco);
7411     body = begin_function_body ();
7412
7413     /* 5.1.1.4 of the standard says:
7414          If a lambda-expression does not include a trailing-return-type, it
7415          is as if the trailing-return-type denotes the following type:
7416           * if the compound-statement is of the form
7417                { return attribute-specifier [opt] expression ; }
7418              the type of the returned expression after lvalue-to-rvalue
7419              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7420              (_conv.array_ 4.2), and function-to-pointer conversion
7421              (_conv.func_ 4.3);
7422           * otherwise, void.  */
7423
7424     /* In a lambda that has neither a lambda-return-type-clause
7425        nor a deducible form, errors should be reported for return statements
7426        in the body.  Since we used void as the placeholder return type, parsing
7427        the body as usual will give such desired behavior.  */
7428     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7429         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7430         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7431         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7432       {
7433         tree compound_stmt;
7434         tree expr = NULL_TREE;
7435         cp_id_kind idk = CP_ID_KIND_NONE;
7436
7437         /* Parse tentatively in case there's more after the initial return
7438            statement.  */
7439         cp_parser_parse_tentatively (parser);
7440
7441         cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7442         cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7443
7444         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7445
7446         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7447         cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7448
7449         if (cp_parser_parse_definitely (parser))
7450           {
7451             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7452
7453             compound_stmt = begin_compound_stmt (0);
7454             /* Will get error here if type not deduced yet.  */
7455             finish_return_stmt (expr);
7456             finish_compound_stmt (compound_stmt);
7457
7458             done = true;
7459           }
7460       }
7461
7462     if (!done)
7463       {
7464         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7465           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7466         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7467            cp_parser_compound_stmt does not pass it.  */
7468         cp_parser_function_body (parser);
7469         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7470       }
7471
7472     finish_function_body (body);
7473     finish_lambda_scope ();
7474
7475     /* Finish the function and generate code for it if necessary.  */
7476     expand_or_defer_fn (finish_function (/*inline*/2));
7477   }
7478
7479   if (nested)
7480     pop_function_context();
7481 }
7482
7483 /* Statements [gram.stmt.stmt]  */
7484
7485 /* Parse a statement.
7486
7487    statement:
7488      labeled-statement
7489      expression-statement
7490      compound-statement
7491      selection-statement
7492      iteration-statement
7493      jump-statement
7494      declaration-statement
7495      try-block
7496
7497   IN_COMPOUND is true when the statement is nested inside a
7498   cp_parser_compound_statement; this matters for certain pragmas.
7499
7500   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7501   is a (possibly labeled) if statement which is not enclosed in braces
7502   and has an else clause.  This is used to implement -Wparentheses.  */
7503
7504 static void
7505 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7506                      bool in_compound, bool *if_p)
7507 {
7508   tree statement;
7509   cp_token *token;
7510   location_t statement_location;
7511
7512  restart:
7513   if (if_p != NULL)
7514     *if_p = false;
7515   /* There is no statement yet.  */
7516   statement = NULL_TREE;
7517   /* Peek at the next token.  */
7518   token = cp_lexer_peek_token (parser->lexer);
7519   /* Remember the location of the first token in the statement.  */
7520   statement_location = token->location;
7521   /* If this is a keyword, then that will often determine what kind of
7522      statement we have.  */
7523   if (token->type == CPP_KEYWORD)
7524     {
7525       enum rid keyword = token->keyword;
7526
7527       switch (keyword)
7528         {
7529         case RID_CASE:
7530         case RID_DEFAULT:
7531           /* Looks like a labeled-statement with a case label.
7532              Parse the label, and then use tail recursion to parse
7533              the statement.  */
7534           cp_parser_label_for_labeled_statement (parser);
7535           goto restart;
7536
7537         case RID_IF:
7538         case RID_SWITCH:
7539           statement = cp_parser_selection_statement (parser, if_p);
7540           break;
7541
7542         case RID_WHILE:
7543         case RID_DO:
7544         case RID_FOR:
7545           statement = cp_parser_iteration_statement (parser);
7546           break;
7547
7548         case RID_BREAK:
7549         case RID_CONTINUE:
7550         case RID_RETURN:
7551         case RID_GOTO:
7552           statement = cp_parser_jump_statement (parser);
7553           break;
7554
7555           /* Objective-C++ exception-handling constructs.  */
7556         case RID_AT_TRY:
7557         case RID_AT_CATCH:
7558         case RID_AT_FINALLY:
7559         case RID_AT_SYNCHRONIZED:
7560         case RID_AT_THROW:
7561           statement = cp_parser_objc_statement (parser);
7562           break;
7563
7564         case RID_TRY:
7565           statement = cp_parser_try_block (parser);
7566           break;
7567
7568         case RID_NAMESPACE:
7569           /* This must be a namespace alias definition.  */
7570           cp_parser_declaration_statement (parser);
7571           return;
7572           
7573         default:
7574           /* It might be a keyword like `int' that can start a
7575              declaration-statement.  */
7576           break;
7577         }
7578     }
7579   else if (token->type == CPP_NAME)
7580     {
7581       /* If the next token is a `:', then we are looking at a
7582          labeled-statement.  */
7583       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7584       if (token->type == CPP_COLON)
7585         {
7586           /* Looks like a labeled-statement with an ordinary label.
7587              Parse the label, and then use tail recursion to parse
7588              the statement.  */
7589           cp_parser_label_for_labeled_statement (parser);
7590           goto restart;
7591         }
7592     }
7593   /* Anything that starts with a `{' must be a compound-statement.  */
7594   else if (token->type == CPP_OPEN_BRACE)
7595     statement = cp_parser_compound_statement (parser, NULL, false);
7596   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7597      a statement all its own.  */
7598   else if (token->type == CPP_PRAGMA)
7599     {
7600       /* Only certain OpenMP pragmas are attached to statements, and thus
7601          are considered statements themselves.  All others are not.  In
7602          the context of a compound, accept the pragma as a "statement" and
7603          return so that we can check for a close brace.  Otherwise we
7604          require a real statement and must go back and read one.  */
7605       if (in_compound)
7606         cp_parser_pragma (parser, pragma_compound);
7607       else if (!cp_parser_pragma (parser, pragma_stmt))
7608         goto restart;
7609       return;
7610     }
7611   else if (token->type == CPP_EOF)
7612     {
7613       cp_parser_error (parser, "expected statement");
7614       return;
7615     }
7616
7617   /* Everything else must be a declaration-statement or an
7618      expression-statement.  Try for the declaration-statement
7619      first, unless we are looking at a `;', in which case we know that
7620      we have an expression-statement.  */
7621   if (!statement)
7622     {
7623       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7624         {
7625           cp_parser_parse_tentatively (parser);
7626           /* Try to parse the declaration-statement.  */
7627           cp_parser_declaration_statement (parser);
7628           /* If that worked, we're done.  */
7629           if (cp_parser_parse_definitely (parser))
7630             return;
7631         }
7632       /* Look for an expression-statement instead.  */
7633       statement = cp_parser_expression_statement (parser, in_statement_expr);
7634     }
7635
7636   /* Set the line number for the statement.  */
7637   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7638     SET_EXPR_LOCATION (statement, statement_location);
7639 }
7640
7641 /* Parse the label for a labeled-statement, i.e.
7642
7643    identifier :
7644    case constant-expression :
7645    default :
7646
7647    GNU Extension:
7648    case constant-expression ... constant-expression : statement
7649
7650    When a label is parsed without errors, the label is added to the
7651    parse tree by the finish_* functions, so this function doesn't
7652    have to return the label.  */
7653
7654 static void
7655 cp_parser_label_for_labeled_statement (cp_parser* parser)
7656 {
7657   cp_token *token;
7658   tree label = NULL_TREE;
7659
7660   /* The next token should be an identifier.  */
7661   token = cp_lexer_peek_token (parser->lexer);
7662   if (token->type != CPP_NAME
7663       && token->type != CPP_KEYWORD)
7664     {
7665       cp_parser_error (parser, "expected labeled-statement");
7666       return;
7667     }
7668
7669   switch (token->keyword)
7670     {
7671     case RID_CASE:
7672       {
7673         tree expr, expr_hi;
7674         cp_token *ellipsis;
7675
7676         /* Consume the `case' token.  */
7677         cp_lexer_consume_token (parser->lexer);
7678         /* Parse the constant-expression.  */
7679         expr = cp_parser_constant_expression (parser,
7680                                               /*allow_non_constant_p=*/false,
7681                                               NULL);
7682
7683         ellipsis = cp_lexer_peek_token (parser->lexer);
7684         if (ellipsis->type == CPP_ELLIPSIS)
7685           {
7686             /* Consume the `...' token.  */
7687             cp_lexer_consume_token (parser->lexer);
7688             expr_hi =
7689               cp_parser_constant_expression (parser,
7690                                              /*allow_non_constant_p=*/false,
7691                                              NULL);
7692             /* We don't need to emit warnings here, as the common code
7693                will do this for us.  */
7694           }
7695         else
7696           expr_hi = NULL_TREE;
7697
7698         if (parser->in_switch_statement_p)
7699           finish_case_label (token->location, expr, expr_hi);
7700         else
7701           error_at (token->location,
7702                     "case label %qE not within a switch statement",
7703                     expr);
7704       }
7705       break;
7706
7707     case RID_DEFAULT:
7708       /* Consume the `default' token.  */
7709       cp_lexer_consume_token (parser->lexer);
7710
7711       if (parser->in_switch_statement_p)
7712         finish_case_label (token->location, NULL_TREE, NULL_TREE);
7713       else
7714         error_at (token->location, "case label not within a switch statement");
7715       break;
7716
7717     default:
7718       /* Anything else must be an ordinary label.  */
7719       label = finish_label_stmt (cp_parser_identifier (parser));
7720       break;
7721     }
7722
7723   /* Require the `:' token.  */
7724   cp_parser_require (parser, CPP_COLON, "%<:%>");
7725
7726   /* An ordinary label may optionally be followed by attributes.
7727      However, this is only permitted if the attributes are then
7728      followed by a semicolon.  This is because, for backward
7729      compatibility, when parsing
7730        lab: __attribute__ ((unused)) int i;
7731      we want the attribute to attach to "i", not "lab".  */
7732   if (label != NULL_TREE
7733       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7734     {
7735       tree attrs;
7736
7737       cp_parser_parse_tentatively (parser);
7738       attrs = cp_parser_attributes_opt (parser);
7739       if (attrs == NULL_TREE
7740           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7741         cp_parser_abort_tentative_parse (parser);
7742       else if (!cp_parser_parse_definitely (parser))
7743         ;
7744       else
7745         cplus_decl_attributes (&label, attrs, 0);
7746     }
7747 }
7748
7749 /* Parse an expression-statement.
7750
7751    expression-statement:
7752      expression [opt] ;
7753
7754    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7755    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7756    indicates whether this expression-statement is part of an
7757    expression statement.  */
7758
7759 static tree
7760 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7761 {
7762   tree statement = NULL_TREE;
7763
7764   /* If the next token is a ';', then there is no expression
7765      statement.  */
7766   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7767     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7768
7769   /* Consume the final `;'.  */
7770   cp_parser_consume_semicolon_at_end_of_statement (parser);
7771
7772   if (in_statement_expr
7773       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7774     /* This is the final expression statement of a statement
7775        expression.  */
7776     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7777   else if (statement)
7778     statement = finish_expr_stmt (statement);
7779   else
7780     finish_stmt ();
7781
7782   return statement;
7783 }
7784
7785 /* Parse a compound-statement.
7786
7787    compound-statement:
7788      { statement-seq [opt] }
7789
7790    GNU extension:
7791
7792    compound-statement:
7793      { label-declaration-seq [opt] statement-seq [opt] }
7794
7795    label-declaration-seq:
7796      label-declaration
7797      label-declaration-seq label-declaration
7798
7799    Returns a tree representing the statement.  */
7800
7801 static tree
7802 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7803                               bool in_try)
7804 {
7805   tree compound_stmt;
7806
7807   /* Consume the `{'.  */
7808   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7809     return error_mark_node;
7810   /* Begin the compound-statement.  */
7811   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7812   /* If the next keyword is `__label__' we have a label declaration.  */
7813   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7814     cp_parser_label_declaration (parser);
7815   /* Parse an (optional) statement-seq.  */
7816   cp_parser_statement_seq_opt (parser, in_statement_expr);
7817   /* Finish the compound-statement.  */
7818   finish_compound_stmt (compound_stmt);
7819   /* Consume the `}'.  */
7820   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7821
7822   return compound_stmt;
7823 }
7824
7825 /* Parse an (optional) statement-seq.
7826
7827    statement-seq:
7828      statement
7829      statement-seq [opt] statement  */
7830
7831 static void
7832 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7833 {
7834   /* Scan statements until there aren't any more.  */
7835   while (true)
7836     {
7837       cp_token *token = cp_lexer_peek_token (parser->lexer);
7838
7839       /* If we're looking at a `}', then we've run out of statements.  */
7840       if (token->type == CPP_CLOSE_BRACE
7841           || token->type == CPP_EOF
7842           || token->type == CPP_PRAGMA_EOL)
7843         break;
7844       
7845       /* If we are in a compound statement and find 'else' then
7846          something went wrong.  */
7847       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7848         {
7849           if (parser->in_statement & IN_IF_STMT) 
7850             break;
7851           else
7852             {
7853               token = cp_lexer_consume_token (parser->lexer);
7854               error_at (token->location, "%<else%> without a previous %<if%>");
7855             }
7856         }
7857
7858       /* Parse the statement.  */
7859       cp_parser_statement (parser, in_statement_expr, true, NULL);
7860     }
7861 }
7862
7863 /* Parse a selection-statement.
7864
7865    selection-statement:
7866      if ( condition ) statement
7867      if ( condition ) statement else statement
7868      switch ( condition ) statement
7869
7870    Returns the new IF_STMT or SWITCH_STMT.
7871
7872    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7873    is a (possibly labeled) if statement which is not enclosed in
7874    braces and has an else clause.  This is used to implement
7875    -Wparentheses.  */
7876
7877 static tree
7878 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7879 {
7880   cp_token *token;
7881   enum rid keyword;
7882
7883   if (if_p != NULL)
7884     *if_p = false;
7885
7886   /* Peek at the next token.  */
7887   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7888
7889   /* See what kind of keyword it is.  */
7890   keyword = token->keyword;
7891   switch (keyword)
7892     {
7893     case RID_IF:
7894     case RID_SWITCH:
7895       {
7896         tree statement;
7897         tree condition;
7898
7899         /* Look for the `('.  */
7900         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7901           {
7902             cp_parser_skip_to_end_of_statement (parser);
7903             return error_mark_node;
7904           }
7905
7906         /* Begin the selection-statement.  */
7907         if (keyword == RID_IF)
7908           statement = begin_if_stmt ();
7909         else
7910           statement = begin_switch_stmt ();
7911
7912         /* Parse the condition.  */
7913         condition = cp_parser_condition (parser);
7914         /* Look for the `)'.  */
7915         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7916           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7917                                                  /*consume_paren=*/true);
7918
7919         if (keyword == RID_IF)
7920           {
7921             bool nested_if;
7922             unsigned char in_statement;
7923
7924             /* Add the condition.  */
7925             finish_if_stmt_cond (condition, statement);
7926
7927             /* Parse the then-clause.  */
7928             in_statement = parser->in_statement;
7929             parser->in_statement |= IN_IF_STMT;
7930             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7931               {
7932                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7933                 add_stmt (build_empty_stmt (loc));
7934                 cp_lexer_consume_token (parser->lexer);
7935                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7936                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7937                               "empty body in an %<if%> statement");
7938                 nested_if = false;
7939               }
7940             else
7941               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7942             parser->in_statement = in_statement;
7943
7944             finish_then_clause (statement);
7945
7946             /* If the next token is `else', parse the else-clause.  */
7947             if (cp_lexer_next_token_is_keyword (parser->lexer,
7948                                                 RID_ELSE))
7949               {
7950                 /* Consume the `else' keyword.  */
7951                 cp_lexer_consume_token (parser->lexer);
7952                 begin_else_clause (statement);
7953                 /* Parse the else-clause.  */
7954                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7955                   {
7956                     location_t loc;
7957                     loc = cp_lexer_peek_token (parser->lexer)->location;
7958                     warning_at (loc,
7959                                 OPT_Wempty_body, "suggest braces around "
7960                                 "empty body in an %<else%> statement");
7961                     add_stmt (build_empty_stmt (loc));
7962                     cp_lexer_consume_token (parser->lexer);
7963                   }
7964                 else
7965                   cp_parser_implicitly_scoped_statement (parser, NULL);
7966
7967                 finish_else_clause (statement);
7968
7969                 /* If we are currently parsing a then-clause, then
7970                    IF_P will not be NULL.  We set it to true to
7971                    indicate that this if statement has an else clause.
7972                    This may trigger the Wparentheses warning below
7973                    when we get back up to the parent if statement.  */
7974                 if (if_p != NULL)
7975                   *if_p = true;
7976               }
7977             else
7978               {
7979                 /* This if statement does not have an else clause.  If
7980                    NESTED_IF is true, then the then-clause is an if
7981                    statement which does have an else clause.  We warn
7982                    about the potential ambiguity.  */
7983                 if (nested_if)
7984                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
7985                               "suggest explicit braces to avoid ambiguous"
7986                               " %<else%>");
7987               }
7988
7989             /* Now we're all done with the if-statement.  */
7990             finish_if_stmt (statement);
7991           }
7992         else
7993           {
7994             bool in_switch_statement_p;
7995             unsigned char in_statement;
7996
7997             /* Add the condition.  */
7998             finish_switch_cond (condition, statement);
7999
8000             /* Parse the body of the switch-statement.  */
8001             in_switch_statement_p = parser->in_switch_statement_p;
8002             in_statement = parser->in_statement;
8003             parser->in_switch_statement_p = true;
8004             parser->in_statement |= IN_SWITCH_STMT;
8005             cp_parser_implicitly_scoped_statement (parser, NULL);
8006             parser->in_switch_statement_p = in_switch_statement_p;
8007             parser->in_statement = in_statement;
8008
8009             /* Now we're all done with the switch-statement.  */
8010             finish_switch_stmt (statement);
8011           }
8012
8013         return statement;
8014       }
8015       break;
8016
8017     default:
8018       cp_parser_error (parser, "expected selection-statement");
8019       return error_mark_node;
8020     }
8021 }
8022
8023 /* Parse a condition.
8024
8025    condition:
8026      expression
8027      type-specifier-seq declarator = initializer-clause
8028      type-specifier-seq declarator braced-init-list
8029
8030    GNU Extension:
8031
8032    condition:
8033      type-specifier-seq declarator asm-specification [opt]
8034        attributes [opt] = assignment-expression
8035
8036    Returns the expression that should be tested.  */
8037
8038 static tree
8039 cp_parser_condition (cp_parser* parser)
8040 {
8041   cp_decl_specifier_seq type_specifiers;
8042   const char *saved_message;
8043
8044   /* Try the declaration first.  */
8045   cp_parser_parse_tentatively (parser);
8046   /* New types are not allowed in the type-specifier-seq for a
8047      condition.  */
8048   saved_message = parser->type_definition_forbidden_message;
8049   parser->type_definition_forbidden_message
8050     = "types may not be defined in conditions";
8051   /* Parse the type-specifier-seq.  */
8052   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
8053                                 /*is_trailing_return=*/false,
8054                                 &type_specifiers);
8055   /* Restore the saved message.  */
8056   parser->type_definition_forbidden_message = saved_message;
8057   /* If all is well, we might be looking at a declaration.  */
8058   if (!cp_parser_error_occurred (parser))
8059     {
8060       tree decl;
8061       tree asm_specification;
8062       tree attributes;
8063       cp_declarator *declarator;
8064       tree initializer = NULL_TREE;
8065
8066       /* Parse the declarator.  */
8067       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8068                                          /*ctor_dtor_or_conv_p=*/NULL,
8069                                          /*parenthesized_p=*/NULL,
8070                                          /*member_p=*/false);
8071       /* Parse the attributes.  */
8072       attributes = cp_parser_attributes_opt (parser);
8073       /* Parse the asm-specification.  */
8074       asm_specification = cp_parser_asm_specification_opt (parser);
8075       /* If the next token is not an `=' or '{', then we might still be
8076          looking at an expression.  For example:
8077
8078            if (A(a).x)
8079
8080          looks like a decl-specifier-seq and a declarator -- but then
8081          there is no `=', so this is an expression.  */
8082       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8083           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8084         cp_parser_simulate_error (parser);
8085         
8086       /* If we did see an `=' or '{', then we are looking at a declaration
8087          for sure.  */
8088       if (cp_parser_parse_definitely (parser))
8089         {
8090           tree pushed_scope;
8091           bool non_constant_p;
8092           bool flags = LOOKUP_ONLYCONVERTING;
8093
8094           /* Create the declaration.  */
8095           decl = start_decl (declarator, &type_specifiers,
8096                              /*initialized_p=*/true,
8097                              attributes, /*prefix_attributes=*/NULL_TREE,
8098                              &pushed_scope);
8099
8100           /* Parse the initializer.  */
8101           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8102             {
8103               initializer = cp_parser_braced_list (parser, &non_constant_p);
8104               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8105               flags = 0;
8106             }
8107           else
8108             {
8109               /* Consume the `='.  */
8110               cp_parser_require (parser, CPP_EQ, "%<=%>");
8111               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8112             }
8113           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8114             maybe_warn_cpp0x ("extended initializer lists");
8115
8116           if (!non_constant_p)
8117             initializer = fold_non_dependent_expr (initializer);
8118
8119           /* Process the initializer.  */
8120           cp_finish_decl (decl,
8121                           initializer, !non_constant_p,
8122                           asm_specification,
8123                           flags);
8124
8125           if (pushed_scope)
8126             pop_scope (pushed_scope);
8127
8128           return convert_from_reference (decl);
8129         }
8130     }
8131   /* If we didn't even get past the declarator successfully, we are
8132      definitely not looking at a declaration.  */
8133   else
8134     cp_parser_abort_tentative_parse (parser);
8135
8136   /* Otherwise, we are looking at an expression.  */
8137   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8138 }
8139
8140 /* Parse an iteration-statement.
8141
8142    iteration-statement:
8143      while ( condition ) statement
8144      do statement while ( expression ) ;
8145      for ( for-init-statement condition [opt] ; expression [opt] )
8146        statement
8147
8148    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
8149
8150 static tree
8151 cp_parser_iteration_statement (cp_parser* parser)
8152 {
8153   cp_token *token;
8154   enum rid keyword;
8155   tree statement;
8156   unsigned char in_statement;
8157
8158   /* Peek at the next token.  */
8159   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8160   if (!token)
8161     return error_mark_node;
8162
8163   /* Remember whether or not we are already within an iteration
8164      statement.  */
8165   in_statement = parser->in_statement;
8166
8167   /* See what kind of keyword it is.  */
8168   keyword = token->keyword;
8169   switch (keyword)
8170     {
8171     case RID_WHILE:
8172       {
8173         tree condition;
8174
8175         /* Begin the while-statement.  */
8176         statement = begin_while_stmt ();
8177         /* Look for the `('.  */
8178         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8179         /* Parse the condition.  */
8180         condition = cp_parser_condition (parser);
8181         finish_while_stmt_cond (condition, statement);
8182         /* Look for the `)'.  */
8183         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8184         /* Parse the dependent statement.  */
8185         parser->in_statement = IN_ITERATION_STMT;
8186         cp_parser_already_scoped_statement (parser);
8187         parser->in_statement = in_statement;
8188         /* We're done with the while-statement.  */
8189         finish_while_stmt (statement);
8190       }
8191       break;
8192
8193     case RID_DO:
8194       {
8195         tree expression;
8196
8197         /* Begin the do-statement.  */
8198         statement = begin_do_stmt ();
8199         /* Parse the body of the do-statement.  */
8200         parser->in_statement = IN_ITERATION_STMT;
8201         cp_parser_implicitly_scoped_statement (parser, NULL);
8202         parser->in_statement = in_statement;
8203         finish_do_body (statement);
8204         /* Look for the `while' keyword.  */
8205         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8206         /* Look for the `('.  */
8207         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8208         /* Parse the expression.  */
8209         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8210         /* We're done with the do-statement.  */
8211         finish_do_stmt (expression, statement);
8212         /* Look for the `)'.  */
8213         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8214         /* Look for the `;'.  */
8215         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8216       }
8217       break;
8218
8219     case RID_FOR:
8220       {
8221         tree condition = NULL_TREE;
8222         tree expression = NULL_TREE;
8223
8224         /* Begin the for-statement.  */
8225         statement = begin_for_stmt ();
8226         /* Look for the `('.  */
8227         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8228         /* Parse the initialization.  */
8229         cp_parser_for_init_statement (parser);
8230         finish_for_init_stmt (statement);
8231
8232         /* If there's a condition, process it.  */
8233         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8234           condition = cp_parser_condition (parser);
8235         finish_for_cond (condition, statement);
8236         /* Look for the `;'.  */
8237         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8238
8239         /* If there's an expression, process it.  */
8240         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8241           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8242         finish_for_expr (expression, statement);
8243         /* Look for the `)'.  */
8244         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8245
8246         /* Parse the body of the for-statement.  */
8247         parser->in_statement = IN_ITERATION_STMT;
8248         cp_parser_already_scoped_statement (parser);
8249         parser->in_statement = in_statement;
8250
8251         /* We're done with the for-statement.  */
8252         finish_for_stmt (statement);
8253       }
8254       break;
8255
8256     default:
8257       cp_parser_error (parser, "expected iteration-statement");
8258       statement = error_mark_node;
8259       break;
8260     }
8261
8262   return statement;
8263 }
8264
8265 /* Parse a for-init-statement.
8266
8267    for-init-statement:
8268      expression-statement
8269      simple-declaration  */
8270
8271 static void
8272 cp_parser_for_init_statement (cp_parser* parser)
8273 {
8274   /* If the next token is a `;', then we have an empty
8275      expression-statement.  Grammatically, this is also a
8276      simple-declaration, but an invalid one, because it does not
8277      declare anything.  Therefore, if we did not handle this case
8278      specially, we would issue an error message about an invalid
8279      declaration.  */
8280   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8281     {
8282       /* We're going to speculatively look for a declaration, falling back
8283          to an expression, if necessary.  */
8284       cp_parser_parse_tentatively (parser);
8285       /* Parse the declaration.  */
8286       cp_parser_simple_declaration (parser,
8287                                     /*function_definition_allowed_p=*/false);
8288       /* If the tentative parse failed, then we shall need to look for an
8289          expression-statement.  */
8290       if (cp_parser_parse_definitely (parser))
8291         return;
8292     }
8293
8294   cp_parser_expression_statement (parser, false);
8295 }
8296
8297 /* Parse a jump-statement.
8298
8299    jump-statement:
8300      break ;
8301      continue ;
8302      return expression [opt] ;
8303      return braced-init-list ;
8304      goto identifier ;
8305
8306    GNU extension:
8307
8308    jump-statement:
8309      goto * expression ;
8310
8311    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
8312
8313 static tree
8314 cp_parser_jump_statement (cp_parser* parser)
8315 {
8316   tree statement = error_mark_node;
8317   cp_token *token;
8318   enum rid keyword;
8319   unsigned char in_statement;
8320
8321   /* Peek at the next token.  */
8322   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8323   if (!token)
8324     return error_mark_node;
8325
8326   /* See what kind of keyword it is.  */
8327   keyword = token->keyword;
8328   switch (keyword)
8329     {
8330     case RID_BREAK:
8331       in_statement = parser->in_statement & ~IN_IF_STMT;      
8332       switch (in_statement)
8333         {
8334         case 0:
8335           error_at (token->location, "break statement not within loop or switch");
8336           break;
8337         default:
8338           gcc_assert ((in_statement & IN_SWITCH_STMT)
8339                       || in_statement == IN_ITERATION_STMT);
8340           statement = finish_break_stmt ();
8341           break;
8342         case IN_OMP_BLOCK:
8343           error_at (token->location, "invalid exit from OpenMP structured block");
8344           break;
8345         case IN_OMP_FOR:
8346           error_at (token->location, "break statement used with OpenMP for loop");
8347           break;
8348         }
8349       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8350       break;
8351
8352     case RID_CONTINUE:
8353       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8354         {
8355         case 0:
8356           error_at (token->location, "continue statement not within a loop");
8357           break;
8358         case IN_ITERATION_STMT:
8359         case IN_OMP_FOR:
8360           statement = finish_continue_stmt ();
8361           break;
8362         case IN_OMP_BLOCK:
8363           error_at (token->location, "invalid exit from OpenMP structured block");
8364           break;
8365         default:
8366           gcc_unreachable ();
8367         }
8368       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8369       break;
8370
8371     case RID_RETURN:
8372       {
8373         tree expr;
8374         bool expr_non_constant_p;
8375
8376         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8377           {
8378             maybe_warn_cpp0x ("extended initializer lists");
8379             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8380           }
8381         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8382           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8383         else
8384           /* If the next token is a `;', then there is no
8385              expression.  */
8386           expr = NULL_TREE;
8387         /* Build the return-statement.  */
8388         statement = finish_return_stmt (expr);
8389         /* Look for the final `;'.  */
8390         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8391       }
8392       break;
8393
8394     case RID_GOTO:
8395       /* Create the goto-statement.  */
8396       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8397         {
8398           /* Issue a warning about this use of a GNU extension.  */
8399           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8400           /* Consume the '*' token.  */
8401           cp_lexer_consume_token (parser->lexer);
8402           /* Parse the dependent expression.  */
8403           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8404         }
8405       else
8406         finish_goto_stmt (cp_parser_identifier (parser));
8407       /* Look for the final `;'.  */
8408       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8409       break;
8410
8411     default:
8412       cp_parser_error (parser, "expected jump-statement");
8413       break;
8414     }
8415
8416   return statement;
8417 }
8418
8419 /* Parse a declaration-statement.
8420
8421    declaration-statement:
8422      block-declaration  */
8423
8424 static void
8425 cp_parser_declaration_statement (cp_parser* parser)
8426 {
8427   void *p;
8428
8429   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8430   p = obstack_alloc (&declarator_obstack, 0);
8431
8432  /* Parse the block-declaration.  */
8433   cp_parser_block_declaration (parser, /*statement_p=*/true);
8434
8435   /* Free any declarators allocated.  */
8436   obstack_free (&declarator_obstack, p);
8437
8438   /* Finish off the statement.  */
8439   finish_stmt ();
8440 }
8441
8442 /* Some dependent statements (like `if (cond) statement'), are
8443    implicitly in their own scope.  In other words, if the statement is
8444    a single statement (as opposed to a compound-statement), it is
8445    none-the-less treated as if it were enclosed in braces.  Any
8446    declarations appearing in the dependent statement are out of scope
8447    after control passes that point.  This function parses a statement,
8448    but ensures that is in its own scope, even if it is not a
8449    compound-statement.
8450
8451    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8452    is a (possibly labeled) if statement which is not enclosed in
8453    braces and has an else clause.  This is used to implement
8454    -Wparentheses.
8455
8456    Returns the new statement.  */
8457
8458 static tree
8459 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8460 {
8461   tree statement;
8462
8463   if (if_p != NULL)
8464     *if_p = false;
8465
8466   /* Mark if () ; with a special NOP_EXPR.  */
8467   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8468     {
8469       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8470       cp_lexer_consume_token (parser->lexer);
8471       statement = add_stmt (build_empty_stmt (loc));
8472     }
8473   /* if a compound is opened, we simply parse the statement directly.  */
8474   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8475     statement = cp_parser_compound_statement (parser, NULL, false);
8476   /* If the token is not a `{', then we must take special action.  */
8477   else
8478     {
8479       /* Create a compound-statement.  */
8480       statement = begin_compound_stmt (0);
8481       /* Parse the dependent-statement.  */
8482       cp_parser_statement (parser, NULL_TREE, false, if_p);
8483       /* Finish the dummy compound-statement.  */
8484       finish_compound_stmt (statement);
8485     }
8486
8487   /* Return the statement.  */
8488   return statement;
8489 }
8490
8491 /* For some dependent statements (like `while (cond) statement'), we
8492    have already created a scope.  Therefore, even if the dependent
8493    statement is a compound-statement, we do not want to create another
8494    scope.  */
8495
8496 static void
8497 cp_parser_already_scoped_statement (cp_parser* parser)
8498 {
8499   /* If the token is a `{', then we must take special action.  */
8500   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8501     cp_parser_statement (parser, NULL_TREE, false, NULL);
8502   else
8503     {
8504       /* Avoid calling cp_parser_compound_statement, so that we
8505          don't create a new scope.  Do everything else by hand.  */
8506       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8507       /* If the next keyword is `__label__' we have a label declaration.  */
8508       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8509         cp_parser_label_declaration (parser);
8510       /* Parse an (optional) statement-seq.  */
8511       cp_parser_statement_seq_opt (parser, NULL_TREE);
8512       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8513     }
8514 }
8515
8516 /* Declarations [gram.dcl.dcl] */
8517
8518 /* Parse an optional declaration-sequence.
8519
8520    declaration-seq:
8521      declaration
8522      declaration-seq declaration  */
8523
8524 static void
8525 cp_parser_declaration_seq_opt (cp_parser* parser)
8526 {
8527   while (true)
8528     {
8529       cp_token *token;
8530
8531       token = cp_lexer_peek_token (parser->lexer);
8532
8533       if (token->type == CPP_CLOSE_BRACE
8534           || token->type == CPP_EOF
8535           || token->type == CPP_PRAGMA_EOL)
8536         break;
8537
8538       if (token->type == CPP_SEMICOLON)
8539         {
8540           /* A declaration consisting of a single semicolon is
8541              invalid.  Allow it unless we're being pedantic.  */
8542           cp_lexer_consume_token (parser->lexer);
8543           if (!in_system_header)
8544             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8545           continue;
8546         }
8547
8548       /* If we're entering or exiting a region that's implicitly
8549          extern "C", modify the lang context appropriately.  */
8550       if (!parser->implicit_extern_c && token->implicit_extern_c)
8551         {
8552           push_lang_context (lang_name_c);
8553           parser->implicit_extern_c = true;
8554         }
8555       else if (parser->implicit_extern_c && !token->implicit_extern_c)
8556         {
8557           pop_lang_context ();
8558           parser->implicit_extern_c = false;
8559         }
8560
8561       if (token->type == CPP_PRAGMA)
8562         {
8563           /* A top-level declaration can consist solely of a #pragma.
8564              A nested declaration cannot, so this is done here and not
8565              in cp_parser_declaration.  (A #pragma at block scope is
8566              handled in cp_parser_statement.)  */
8567           cp_parser_pragma (parser, pragma_external);
8568           continue;
8569         }
8570
8571       /* Parse the declaration itself.  */
8572       cp_parser_declaration (parser);
8573     }
8574 }
8575
8576 /* Parse a declaration.
8577
8578    declaration:
8579      block-declaration
8580      function-definition
8581      template-declaration
8582      explicit-instantiation
8583      explicit-specialization
8584      linkage-specification
8585      namespace-definition
8586
8587    GNU extension:
8588
8589    declaration:
8590       __extension__ declaration */
8591
8592 static void
8593 cp_parser_declaration (cp_parser* parser)
8594 {
8595   cp_token token1;
8596   cp_token token2;
8597   int saved_pedantic;
8598   void *p;
8599
8600   /* Check for the `__extension__' keyword.  */
8601   if (cp_parser_extension_opt (parser, &saved_pedantic))
8602     {
8603       /* Parse the qualified declaration.  */
8604       cp_parser_declaration (parser);
8605       /* Restore the PEDANTIC flag.  */
8606       pedantic = saved_pedantic;
8607
8608       return;
8609     }
8610
8611   /* Try to figure out what kind of declaration is present.  */
8612   token1 = *cp_lexer_peek_token (parser->lexer);
8613
8614   if (token1.type != CPP_EOF)
8615     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8616   else
8617     {
8618       token2.type = CPP_EOF;
8619       token2.keyword = RID_MAX;
8620     }
8621
8622   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8623   p = obstack_alloc (&declarator_obstack, 0);
8624
8625   /* If the next token is `extern' and the following token is a string
8626      literal, then we have a linkage specification.  */
8627   if (token1.keyword == RID_EXTERN
8628       && cp_parser_is_string_literal (&token2))
8629     cp_parser_linkage_specification (parser);
8630   /* If the next token is `template', then we have either a template
8631      declaration, an explicit instantiation, or an explicit
8632      specialization.  */
8633   else if (token1.keyword == RID_TEMPLATE)
8634     {
8635       /* `template <>' indicates a template specialization.  */
8636       if (token2.type == CPP_LESS
8637           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8638         cp_parser_explicit_specialization (parser);
8639       /* `template <' indicates a template declaration.  */
8640       else if (token2.type == CPP_LESS)
8641         cp_parser_template_declaration (parser, /*member_p=*/false);
8642       /* Anything else must be an explicit instantiation.  */
8643       else
8644         cp_parser_explicit_instantiation (parser);
8645     }
8646   /* If the next token is `export', then we have a template
8647      declaration.  */
8648   else if (token1.keyword == RID_EXPORT)
8649     cp_parser_template_declaration (parser, /*member_p=*/false);
8650   /* If the next token is `extern', 'static' or 'inline' and the one
8651      after that is `template', we have a GNU extended explicit
8652      instantiation directive.  */
8653   else if (cp_parser_allow_gnu_extensions_p (parser)
8654            && (token1.keyword == RID_EXTERN
8655                || token1.keyword == RID_STATIC
8656                || token1.keyword == RID_INLINE)
8657            && token2.keyword == RID_TEMPLATE)
8658     cp_parser_explicit_instantiation (parser);
8659   /* If the next token is `namespace', check for a named or unnamed
8660      namespace definition.  */
8661   else if (token1.keyword == RID_NAMESPACE
8662            && (/* A named namespace definition.  */
8663                (token2.type == CPP_NAME
8664                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8665                     != CPP_EQ))
8666                /* An unnamed namespace definition.  */
8667                || token2.type == CPP_OPEN_BRACE
8668                || token2.keyword == RID_ATTRIBUTE))
8669     cp_parser_namespace_definition (parser);
8670   /* An inline (associated) namespace definition.  */
8671   else if (token1.keyword == RID_INLINE
8672            && token2.keyword == RID_NAMESPACE)
8673     cp_parser_namespace_definition (parser);
8674   /* Objective-C++ declaration/definition.  */
8675   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8676     cp_parser_objc_declaration (parser);
8677   /* We must have either a block declaration or a function
8678      definition.  */
8679   else
8680     /* Try to parse a block-declaration, or a function-definition.  */
8681     cp_parser_block_declaration (parser, /*statement_p=*/false);
8682
8683   /* Free any declarators allocated.  */
8684   obstack_free (&declarator_obstack, p);
8685 }
8686
8687 /* Parse a block-declaration.
8688
8689    block-declaration:
8690      simple-declaration
8691      asm-definition
8692      namespace-alias-definition
8693      using-declaration
8694      using-directive
8695
8696    GNU Extension:
8697
8698    block-declaration:
8699      __extension__ block-declaration
8700
8701    C++0x Extension:
8702
8703    block-declaration:
8704      static_assert-declaration
8705
8706    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8707    part of a declaration-statement.  */
8708
8709 static void
8710 cp_parser_block_declaration (cp_parser *parser,
8711                              bool      statement_p)
8712 {
8713   cp_token *token1;
8714   int saved_pedantic;
8715
8716   /* Check for the `__extension__' keyword.  */
8717   if (cp_parser_extension_opt (parser, &saved_pedantic))
8718     {
8719       /* Parse the qualified declaration.  */
8720       cp_parser_block_declaration (parser, statement_p);
8721       /* Restore the PEDANTIC flag.  */
8722       pedantic = saved_pedantic;
8723
8724       return;
8725     }
8726
8727   /* Peek at the next token to figure out which kind of declaration is
8728      present.  */
8729   token1 = cp_lexer_peek_token (parser->lexer);
8730
8731   /* If the next keyword is `asm', we have an asm-definition.  */
8732   if (token1->keyword == RID_ASM)
8733     {
8734       if (statement_p)
8735         cp_parser_commit_to_tentative_parse (parser);
8736       cp_parser_asm_definition (parser);
8737     }
8738   /* If the next keyword is `namespace', we have a
8739      namespace-alias-definition.  */
8740   else if (token1->keyword == RID_NAMESPACE)
8741     cp_parser_namespace_alias_definition (parser);
8742   /* If the next keyword is `using', we have either a
8743      using-declaration or a using-directive.  */
8744   else if (token1->keyword == RID_USING)
8745     {
8746       cp_token *token2;
8747
8748       if (statement_p)
8749         cp_parser_commit_to_tentative_parse (parser);
8750       /* If the token after `using' is `namespace', then we have a
8751          using-directive.  */
8752       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8753       if (token2->keyword == RID_NAMESPACE)
8754         cp_parser_using_directive (parser);
8755       /* Otherwise, it's a using-declaration.  */
8756       else
8757         cp_parser_using_declaration (parser,
8758                                      /*access_declaration_p=*/false);
8759     }
8760   /* If the next keyword is `__label__' we have a misplaced label
8761      declaration.  */
8762   else if (token1->keyword == RID_LABEL)
8763     {
8764       cp_lexer_consume_token (parser->lexer);
8765       error_at (token1->location, "%<__label__%> not at the beginning of a block");
8766       cp_parser_skip_to_end_of_statement (parser);
8767       /* If the next token is now a `;', consume it.  */
8768       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8769         cp_lexer_consume_token (parser->lexer);
8770     }
8771   /* If the next token is `static_assert' we have a static assertion.  */
8772   else if (token1->keyword == RID_STATIC_ASSERT)
8773     cp_parser_static_assert (parser, /*member_p=*/false);
8774   /* Anything else must be a simple-declaration.  */
8775   else
8776     cp_parser_simple_declaration (parser, !statement_p);
8777 }
8778
8779 /* Parse a simple-declaration.
8780
8781    simple-declaration:
8782      decl-specifier-seq [opt] init-declarator-list [opt] ;
8783
8784    init-declarator-list:
8785      init-declarator
8786      init-declarator-list , init-declarator
8787
8788    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8789    function-definition as a simple-declaration.  */
8790
8791 static void
8792 cp_parser_simple_declaration (cp_parser* parser,
8793                               bool function_definition_allowed_p)
8794 {
8795   cp_decl_specifier_seq decl_specifiers;
8796   int declares_class_or_enum;
8797   bool saw_declarator;
8798
8799   /* Defer access checks until we know what is being declared; the
8800      checks for names appearing in the decl-specifier-seq should be
8801      done as if we were in the scope of the thing being declared.  */
8802   push_deferring_access_checks (dk_deferred);
8803
8804   /* Parse the decl-specifier-seq.  We have to keep track of whether
8805      or not the decl-specifier-seq declares a named class or
8806      enumeration type, since that is the only case in which the
8807      init-declarator-list is allowed to be empty.
8808
8809      [dcl.dcl]
8810
8811      In a simple-declaration, the optional init-declarator-list can be
8812      omitted only when declaring a class or enumeration, that is when
8813      the decl-specifier-seq contains either a class-specifier, an
8814      elaborated-type-specifier, or an enum-specifier.  */
8815   cp_parser_decl_specifier_seq (parser,
8816                                 CP_PARSER_FLAGS_OPTIONAL,
8817                                 &decl_specifiers,
8818                                 &declares_class_or_enum);
8819   /* We no longer need to defer access checks.  */
8820   stop_deferring_access_checks ();
8821
8822   /* In a block scope, a valid declaration must always have a
8823      decl-specifier-seq.  By not trying to parse declarators, we can
8824      resolve the declaration/expression ambiguity more quickly.  */
8825   if (!function_definition_allowed_p
8826       && !decl_specifiers.any_specifiers_p)
8827     {
8828       cp_parser_error (parser, "expected declaration");
8829       goto done;
8830     }
8831
8832   /* If the next two tokens are both identifiers, the code is
8833      erroneous. The usual cause of this situation is code like:
8834
8835        T t;
8836
8837      where "T" should name a type -- but does not.  */
8838   if (!decl_specifiers.type
8839       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8840     {
8841       /* If parsing tentatively, we should commit; we really are
8842          looking at a declaration.  */
8843       cp_parser_commit_to_tentative_parse (parser);
8844       /* Give up.  */
8845       goto done;
8846     }
8847
8848   /* If we have seen at least one decl-specifier, and the next token
8849      is not a parenthesis, then we must be looking at a declaration.
8850      (After "int (" we might be looking at a functional cast.)  */
8851   if (decl_specifiers.any_specifiers_p
8852       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8853       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8854       && !cp_parser_error_occurred (parser))
8855     cp_parser_commit_to_tentative_parse (parser);
8856
8857   /* Keep going until we hit the `;' at the end of the simple
8858      declaration.  */
8859   saw_declarator = false;
8860   while (cp_lexer_next_token_is_not (parser->lexer,
8861                                      CPP_SEMICOLON))
8862     {
8863       cp_token *token;
8864       bool function_definition_p;
8865       tree decl;
8866
8867       if (saw_declarator)
8868         {
8869           /* If we are processing next declarator, coma is expected */
8870           token = cp_lexer_peek_token (parser->lexer);
8871           gcc_assert (token->type == CPP_COMMA);
8872           cp_lexer_consume_token (parser->lexer);
8873         }
8874       else
8875         saw_declarator = true;
8876
8877       /* Parse the init-declarator.  */
8878       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8879                                         /*checks=*/NULL,
8880                                         function_definition_allowed_p,
8881                                         /*member_p=*/false,
8882                                         declares_class_or_enum,
8883                                         &function_definition_p);
8884       /* If an error occurred while parsing tentatively, exit quickly.
8885          (That usually happens when in the body of a function; each
8886          statement is treated as a declaration-statement until proven
8887          otherwise.)  */
8888       if (cp_parser_error_occurred (parser))
8889         goto done;
8890       /* Handle function definitions specially.  */
8891       if (function_definition_p)
8892         {
8893           /* If the next token is a `,', then we are probably
8894              processing something like:
8895
8896                void f() {}, *p;
8897
8898              which is erroneous.  */
8899           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8900             {
8901               cp_token *token = cp_lexer_peek_token (parser->lexer);
8902               error_at (token->location,
8903                         "mixing"
8904                         " declarations and function-definitions is forbidden");
8905             }
8906           /* Otherwise, we're done with the list of declarators.  */
8907           else
8908             {
8909               pop_deferring_access_checks ();
8910               return;
8911             }
8912         }
8913       /* The next token should be either a `,' or a `;'.  */
8914       token = cp_lexer_peek_token (parser->lexer);
8915       /* If it's a `,', there are more declarators to come.  */
8916       if (token->type == CPP_COMMA)
8917         /* will be consumed next time around */;
8918       /* If it's a `;', we are done.  */
8919       else if (token->type == CPP_SEMICOLON)
8920         break;
8921       /* Anything else is an error.  */
8922       else
8923         {
8924           /* If we have already issued an error message we don't need
8925              to issue another one.  */
8926           if (decl != error_mark_node
8927               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8928             cp_parser_error (parser, "expected %<,%> or %<;%>");
8929           /* Skip tokens until we reach the end of the statement.  */
8930           cp_parser_skip_to_end_of_statement (parser);
8931           /* If the next token is now a `;', consume it.  */
8932           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8933             cp_lexer_consume_token (parser->lexer);
8934           goto done;
8935         }
8936       /* After the first time around, a function-definition is not
8937          allowed -- even if it was OK at first.  For example:
8938
8939            int i, f() {}
8940
8941          is not valid.  */
8942       function_definition_allowed_p = false;
8943     }
8944
8945   /* Issue an error message if no declarators are present, and the
8946      decl-specifier-seq does not itself declare a class or
8947      enumeration.  */
8948   if (!saw_declarator)
8949     {
8950       if (cp_parser_declares_only_class_p (parser))
8951         shadow_tag (&decl_specifiers);
8952       /* Perform any deferred access checks.  */
8953       perform_deferred_access_checks ();
8954     }
8955
8956   /* Consume the `;'.  */
8957   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8958
8959  done:
8960   pop_deferring_access_checks ();
8961 }
8962
8963 /* Parse a decl-specifier-seq.
8964
8965    decl-specifier-seq:
8966      decl-specifier-seq [opt] decl-specifier
8967
8968    decl-specifier:
8969      storage-class-specifier
8970      type-specifier
8971      function-specifier
8972      friend
8973      typedef
8974
8975    GNU Extension:
8976
8977    decl-specifier:
8978      attributes
8979
8980    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8981
8982    The parser flags FLAGS is used to control type-specifier parsing.
8983
8984    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8985    flags:
8986
8987      1: one of the decl-specifiers is an elaborated-type-specifier
8988         (i.e., a type declaration)
8989      2: one of the decl-specifiers is an enum-specifier or a
8990         class-specifier (i.e., a type definition)
8991
8992    */
8993
8994 static void
8995 cp_parser_decl_specifier_seq (cp_parser* parser,
8996                               cp_parser_flags flags,
8997                               cp_decl_specifier_seq *decl_specs,
8998                               int* declares_class_or_enum)
8999 {
9000   bool constructor_possible_p = !parser->in_declarator_p;
9001   cp_token *start_token = NULL;
9002
9003   /* Clear DECL_SPECS.  */
9004   clear_decl_specs (decl_specs);
9005
9006   /* Assume no class or enumeration type is declared.  */
9007   *declares_class_or_enum = 0;
9008
9009   /* Keep reading specifiers until there are no more to read.  */
9010   while (true)
9011     {
9012       bool constructor_p;
9013       bool found_decl_spec;
9014       cp_token *token;
9015
9016       /* Peek at the next token.  */
9017       token = cp_lexer_peek_token (parser->lexer);
9018
9019       /* Save the first token of the decl spec list for error
9020          reporting.  */
9021       if (!start_token)
9022         start_token = token;
9023       /* Handle attributes.  */
9024       if (token->keyword == RID_ATTRIBUTE)
9025         {
9026           /* Parse the attributes.  */
9027           decl_specs->attributes
9028             = chainon (decl_specs->attributes,
9029                        cp_parser_attributes_opt (parser));
9030           continue;
9031         }
9032       /* Assume we will find a decl-specifier keyword.  */
9033       found_decl_spec = true;
9034       /* If the next token is an appropriate keyword, we can simply
9035          add it to the list.  */
9036       switch (token->keyword)
9037         {
9038           /* decl-specifier:
9039                friend
9040                constexpr */
9041         case RID_FRIEND:
9042           if (!at_class_scope_p ())
9043             {
9044               error_at (token->location, "%<friend%> used outside of class");
9045               cp_lexer_purge_token (parser->lexer);
9046             }
9047           else
9048             {
9049               ++decl_specs->specs[(int) ds_friend];
9050               /* Consume the token.  */
9051               cp_lexer_consume_token (parser->lexer);
9052             }
9053           break;
9054
9055         case RID_CONSTEXPR:
9056           ++decl_specs->specs[(int) ds_constexpr];
9057           cp_lexer_consume_token (parser->lexer);
9058           break;
9059
9060           /* function-specifier:
9061                inline
9062                virtual
9063                explicit  */
9064         case RID_INLINE:
9065         case RID_VIRTUAL:
9066         case RID_EXPLICIT:
9067           cp_parser_function_specifier_opt (parser, decl_specs);
9068           break;
9069
9070           /* decl-specifier:
9071                typedef  */
9072         case RID_TYPEDEF:
9073           ++decl_specs->specs[(int) ds_typedef];
9074           /* Consume the token.  */
9075           cp_lexer_consume_token (parser->lexer);
9076           /* A constructor declarator cannot appear in a typedef.  */
9077           constructor_possible_p = false;
9078           /* The "typedef" keyword can only occur in a declaration; we
9079              may as well commit at this point.  */
9080           cp_parser_commit_to_tentative_parse (parser);
9081
9082           if (decl_specs->storage_class != sc_none)
9083             decl_specs->conflicting_specifiers_p = true;
9084           break;
9085
9086           /* storage-class-specifier:
9087                auto
9088                register
9089                static
9090                extern
9091                mutable
9092
9093              GNU Extension:
9094                thread  */
9095         case RID_AUTO:
9096           if (cxx_dialect == cxx98) 
9097             {
9098               /* Consume the token.  */
9099               cp_lexer_consume_token (parser->lexer);
9100
9101               /* Complain about `auto' as a storage specifier, if
9102                  we're complaining about C++0x compatibility.  */
9103               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9104                           " will change meaning in C++0x; please remove it");
9105
9106               /* Set the storage class anyway.  */
9107               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9108                                            token->location);
9109             }
9110           else
9111             /* C++0x auto type-specifier.  */
9112             found_decl_spec = false;
9113           break;
9114
9115         case RID_REGISTER:
9116         case RID_STATIC:
9117         case RID_EXTERN:
9118         case RID_MUTABLE:
9119           /* Consume the token.  */
9120           cp_lexer_consume_token (parser->lexer);
9121           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9122                                        token->location);
9123           break;
9124         case RID_THREAD:
9125           /* Consume the token.  */
9126           cp_lexer_consume_token (parser->lexer);
9127           ++decl_specs->specs[(int) ds_thread];
9128           break;
9129
9130         default:
9131           /* We did not yet find a decl-specifier yet.  */
9132           found_decl_spec = false;
9133           break;
9134         }
9135
9136       /* Constructors are a special case.  The `S' in `S()' is not a
9137          decl-specifier; it is the beginning of the declarator.  */
9138       constructor_p
9139         = (!found_decl_spec
9140            && constructor_possible_p
9141            && (cp_parser_constructor_declarator_p
9142                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9143
9144       /* If we don't have a DECL_SPEC yet, then we must be looking at
9145          a type-specifier.  */
9146       if (!found_decl_spec && !constructor_p)
9147         {
9148           int decl_spec_declares_class_or_enum;
9149           bool is_cv_qualifier;
9150           tree type_spec;
9151
9152           type_spec
9153             = cp_parser_type_specifier (parser, flags,
9154                                         decl_specs,
9155                                         /*is_declaration=*/true,
9156                                         &decl_spec_declares_class_or_enum,
9157                                         &is_cv_qualifier);
9158           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9159
9160           /* If this type-specifier referenced a user-defined type
9161              (a typedef, class-name, etc.), then we can't allow any
9162              more such type-specifiers henceforth.
9163
9164              [dcl.spec]
9165
9166              The longest sequence of decl-specifiers that could
9167              possibly be a type name is taken as the
9168              decl-specifier-seq of a declaration.  The sequence shall
9169              be self-consistent as described below.
9170
9171              [dcl.type]
9172
9173              As a general rule, at most one type-specifier is allowed
9174              in the complete decl-specifier-seq of a declaration.  The
9175              only exceptions are the following:
9176
9177              -- const or volatile can be combined with any other
9178                 type-specifier.
9179
9180              -- signed or unsigned can be combined with char, long,
9181                 short, or int.
9182
9183              -- ..
9184
9185              Example:
9186
9187                typedef char* Pc;
9188                void g (const int Pc);
9189
9190              Here, Pc is *not* part of the decl-specifier seq; it's
9191              the declarator.  Therefore, once we see a type-specifier
9192              (other than a cv-qualifier), we forbid any additional
9193              user-defined types.  We *do* still allow things like `int
9194              int' to be considered a decl-specifier-seq, and issue the
9195              error message later.  */
9196           if (type_spec && !is_cv_qualifier)
9197             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9198           /* A constructor declarator cannot follow a type-specifier.  */
9199           if (type_spec)
9200             {
9201               constructor_possible_p = false;
9202               found_decl_spec = true;
9203             }
9204         }
9205
9206       /* If we still do not have a DECL_SPEC, then there are no more
9207          decl-specifiers.  */
9208       if (!found_decl_spec)
9209         break;
9210
9211       decl_specs->any_specifiers_p = true;
9212       /* After we see one decl-specifier, further decl-specifiers are
9213          always optional.  */
9214       flags |= CP_PARSER_FLAGS_OPTIONAL;
9215     }
9216
9217   cp_parser_check_decl_spec (decl_specs, start_token->location);
9218
9219   /* Don't allow a friend specifier with a class definition.  */
9220   if (decl_specs->specs[(int) ds_friend] != 0
9221       && (*declares_class_or_enum & 2))
9222     error_at (start_token->location,
9223               "class definition may not be declared a friend");
9224 }
9225
9226 /* Parse an (optional) storage-class-specifier.
9227
9228    storage-class-specifier:
9229      auto
9230      register
9231      static
9232      extern
9233      mutable
9234
9235    GNU Extension:
9236
9237    storage-class-specifier:
9238      thread
9239
9240    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9241
9242 static tree
9243 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9244 {
9245   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9246     {
9247     case RID_AUTO:
9248       if (cxx_dialect != cxx98)
9249         return NULL_TREE;
9250       /* Fall through for C++98.  */
9251
9252     case RID_REGISTER:
9253     case RID_STATIC:
9254     case RID_EXTERN:
9255     case RID_MUTABLE:
9256     case RID_THREAD:
9257       /* Consume the token.  */
9258       return cp_lexer_consume_token (parser->lexer)->u.value;
9259
9260     default:
9261       return NULL_TREE;
9262     }
9263 }
9264
9265 /* Parse an (optional) function-specifier.
9266
9267    function-specifier:
9268      inline
9269      virtual
9270      explicit
9271
9272    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9273    Updates DECL_SPECS, if it is non-NULL.  */
9274
9275 static tree
9276 cp_parser_function_specifier_opt (cp_parser* parser,
9277                                   cp_decl_specifier_seq *decl_specs)
9278 {
9279   cp_token *token = cp_lexer_peek_token (parser->lexer);
9280   switch (token->keyword)
9281     {
9282     case RID_INLINE:
9283       if (decl_specs)
9284         ++decl_specs->specs[(int) ds_inline];
9285       break;
9286
9287     case RID_VIRTUAL:
9288       /* 14.5.2.3 [temp.mem]
9289
9290          A member function template shall not be virtual.  */
9291       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9292         error_at (token->location, "templates may not be %<virtual%>");
9293       else if (decl_specs)
9294         ++decl_specs->specs[(int) ds_virtual];
9295       break;
9296
9297     case RID_EXPLICIT:
9298       if (decl_specs)
9299         ++decl_specs->specs[(int) ds_explicit];
9300       break;
9301
9302     default:
9303       return NULL_TREE;
9304     }
9305
9306   /* Consume the token.  */
9307   return cp_lexer_consume_token (parser->lexer)->u.value;
9308 }
9309
9310 /* Parse a linkage-specification.
9311
9312    linkage-specification:
9313      extern string-literal { declaration-seq [opt] }
9314      extern string-literal declaration  */
9315
9316 static void
9317 cp_parser_linkage_specification (cp_parser* parser)
9318 {
9319   tree linkage;
9320
9321   /* Look for the `extern' keyword.  */
9322   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9323
9324   /* Look for the string-literal.  */
9325   linkage = cp_parser_string_literal (parser, false, false);
9326
9327   /* Transform the literal into an identifier.  If the literal is a
9328      wide-character string, or contains embedded NULs, then we can't
9329      handle it as the user wants.  */
9330   if (strlen (TREE_STRING_POINTER (linkage))
9331       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9332     {
9333       cp_parser_error (parser, "invalid linkage-specification");
9334       /* Assume C++ linkage.  */
9335       linkage = lang_name_cplusplus;
9336     }
9337   else
9338     linkage = get_identifier (TREE_STRING_POINTER (linkage));
9339
9340   /* We're now using the new linkage.  */
9341   push_lang_context (linkage);
9342
9343   /* If the next token is a `{', then we're using the first
9344      production.  */
9345   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9346     {
9347       /* Consume the `{' token.  */
9348       cp_lexer_consume_token (parser->lexer);
9349       /* Parse the declarations.  */
9350       cp_parser_declaration_seq_opt (parser);
9351       /* Look for the closing `}'.  */
9352       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9353     }
9354   /* Otherwise, there's just one declaration.  */
9355   else
9356     {
9357       bool saved_in_unbraced_linkage_specification_p;
9358
9359       saved_in_unbraced_linkage_specification_p
9360         = parser->in_unbraced_linkage_specification_p;
9361       parser->in_unbraced_linkage_specification_p = true;
9362       cp_parser_declaration (parser);
9363       parser->in_unbraced_linkage_specification_p
9364         = saved_in_unbraced_linkage_specification_p;
9365     }
9366
9367   /* We're done with the linkage-specification.  */
9368   pop_lang_context ();
9369 }
9370
9371 /* Parse a static_assert-declaration.
9372
9373    static_assert-declaration:
9374      static_assert ( constant-expression , string-literal ) ; 
9375
9376    If MEMBER_P, this static_assert is a class member.  */
9377
9378 static void 
9379 cp_parser_static_assert(cp_parser *parser, bool member_p)
9380 {
9381   tree condition;
9382   tree message;
9383   cp_token *token;
9384   location_t saved_loc;
9385
9386   /* Peek at the `static_assert' token so we can keep track of exactly
9387      where the static assertion started.  */
9388   token = cp_lexer_peek_token (parser->lexer);
9389   saved_loc = token->location;
9390
9391   /* Look for the `static_assert' keyword.  */
9392   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
9393                                   "%<static_assert%>"))
9394     return;
9395
9396   /*  We know we are in a static assertion; commit to any tentative
9397       parse.  */
9398   if (cp_parser_parsing_tentatively (parser))
9399     cp_parser_commit_to_tentative_parse (parser);
9400
9401   /* Parse the `(' starting the static assertion condition.  */
9402   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9403
9404   /* Parse the constant-expression.  */
9405   condition = 
9406     cp_parser_constant_expression (parser,
9407                                    /*allow_non_constant_p=*/false,
9408                                    /*non_constant_p=*/NULL);
9409
9410   /* Parse the separating `,'.  */
9411   cp_parser_require (parser, CPP_COMMA, "%<,%>");
9412
9413   /* Parse the string-literal message.  */
9414   message = cp_parser_string_literal (parser, 
9415                                       /*translate=*/false,
9416                                       /*wide_ok=*/true);
9417
9418   /* A `)' completes the static assertion.  */
9419   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9420     cp_parser_skip_to_closing_parenthesis (parser, 
9421                                            /*recovering=*/true, 
9422                                            /*or_comma=*/false,
9423                                            /*consume_paren=*/true);
9424
9425   /* A semicolon terminates the declaration.  */
9426   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9427
9428   /* Complete the static assertion, which may mean either processing 
9429      the static assert now or saving it for template instantiation.  */
9430   finish_static_assert (condition, message, saved_loc, member_p);
9431 }
9432
9433 /* Parse a `decltype' type. Returns the type. 
9434
9435    simple-type-specifier:
9436      decltype ( expression )  */
9437
9438 static tree
9439 cp_parser_decltype (cp_parser *parser)
9440 {
9441   tree expr;
9442   bool id_expression_or_member_access_p = false;
9443   const char *saved_message;
9444   bool saved_integral_constant_expression_p;
9445   bool saved_non_integral_constant_expression_p;
9446   cp_token *id_expr_start_token;
9447
9448   /* Look for the `decltype' token.  */
9449   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9450     return error_mark_node;
9451
9452   /* Types cannot be defined in a `decltype' expression.  Save away the
9453      old message.  */
9454   saved_message = parser->type_definition_forbidden_message;
9455
9456   /* And create the new one.  */
9457   parser->type_definition_forbidden_message
9458     = "types may not be defined in %<decltype%> expressions";
9459
9460   /* The restrictions on constant-expressions do not apply inside
9461      decltype expressions.  */
9462   saved_integral_constant_expression_p
9463     = parser->integral_constant_expression_p;
9464   saved_non_integral_constant_expression_p
9465     = parser->non_integral_constant_expression_p;
9466   parser->integral_constant_expression_p = false;
9467
9468   /* Do not actually evaluate the expression.  */
9469   ++cp_unevaluated_operand;
9470
9471   /* Do not warn about problems with the expression.  */
9472   ++c_inhibit_evaluation_warnings;
9473
9474   /* Parse the opening `('.  */
9475   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9476     return error_mark_node;
9477   
9478   /* First, try parsing an id-expression.  */
9479   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9480   cp_parser_parse_tentatively (parser);
9481   expr = cp_parser_id_expression (parser,
9482                                   /*template_keyword_p=*/false,
9483                                   /*check_dependency_p=*/true,
9484                                   /*template_p=*/NULL,
9485                                   /*declarator_p=*/false,
9486                                   /*optional_p=*/false);
9487
9488   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9489     {
9490       bool non_integral_constant_expression_p = false;
9491       tree id_expression = expr;
9492       cp_id_kind idk;
9493       const char *error_msg;
9494
9495       if (TREE_CODE (expr) == IDENTIFIER_NODE)
9496         /* Lookup the name we got back from the id-expression.  */
9497         expr = cp_parser_lookup_name (parser, expr,
9498                                       none_type,
9499                                       /*is_template=*/false,
9500                                       /*is_namespace=*/false,
9501                                       /*check_dependency=*/true,
9502                                       /*ambiguous_decls=*/NULL,
9503                                       id_expr_start_token->location);
9504
9505       if (expr
9506           && expr != error_mark_node
9507           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9508           && TREE_CODE (expr) != TYPE_DECL
9509           && (TREE_CODE (expr) != BIT_NOT_EXPR
9510               || !TYPE_P (TREE_OPERAND (expr, 0)))
9511           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9512         {
9513           /* Complete lookup of the id-expression.  */
9514           expr = (finish_id_expression
9515                   (id_expression, expr, parser->scope, &idk,
9516                    /*integral_constant_expression_p=*/false,
9517                    /*allow_non_integral_constant_expression_p=*/true,
9518                    &non_integral_constant_expression_p,
9519                    /*template_p=*/false,
9520                    /*done=*/true,
9521                    /*address_p=*/false,
9522                    /*template_arg_p=*/false,
9523                    &error_msg,
9524                    id_expr_start_token->location));
9525
9526           if (expr == error_mark_node)
9527             /* We found an id-expression, but it was something that we
9528                should not have found. This is an error, not something
9529                we can recover from, so note that we found an
9530                id-expression and we'll recover as gracefully as
9531                possible.  */
9532             id_expression_or_member_access_p = true;
9533         }
9534
9535       if (expr 
9536           && expr != error_mark_node
9537           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9538         /* We have an id-expression.  */
9539         id_expression_or_member_access_p = true;
9540     }
9541
9542   if (!id_expression_or_member_access_p)
9543     {
9544       /* Abort the id-expression parse.  */
9545       cp_parser_abort_tentative_parse (parser);
9546
9547       /* Parsing tentatively, again.  */
9548       cp_parser_parse_tentatively (parser);
9549
9550       /* Parse a class member access.  */
9551       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9552                                            /*cast_p=*/false,
9553                                            /*member_access_only_p=*/true, NULL);
9554
9555       if (expr 
9556           && expr != error_mark_node
9557           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9558         /* We have an id-expression.  */
9559         id_expression_or_member_access_p = true;
9560     }
9561
9562   if (id_expression_or_member_access_p)
9563     /* We have parsed the complete id-expression or member access.  */
9564     cp_parser_parse_definitely (parser);
9565   else
9566     {
9567       bool saved_greater_than_is_operator_p;
9568
9569       /* Abort our attempt to parse an id-expression or member access
9570          expression.  */
9571       cp_parser_abort_tentative_parse (parser);
9572
9573       /* Within a parenthesized expression, a `>' token is always
9574          the greater-than operator.  */
9575       saved_greater_than_is_operator_p
9576         = parser->greater_than_is_operator_p;
9577       parser->greater_than_is_operator_p = true;
9578
9579       /* Parse a full expression.  */
9580       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9581
9582       /* The `>' token might be the end of a template-id or
9583          template-parameter-list now.  */
9584       parser->greater_than_is_operator_p
9585         = saved_greater_than_is_operator_p;
9586     }
9587
9588   /* Go back to evaluating expressions.  */
9589   --cp_unevaluated_operand;
9590   --c_inhibit_evaluation_warnings;
9591
9592   /* Restore the old message and the integral constant expression
9593      flags.  */
9594   parser->type_definition_forbidden_message = saved_message;
9595   parser->integral_constant_expression_p
9596     = saved_integral_constant_expression_p;
9597   parser->non_integral_constant_expression_p
9598     = saved_non_integral_constant_expression_p;
9599
9600   if (expr == error_mark_node)
9601     {
9602       /* Skip everything up to the closing `)'.  */
9603       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9604                                              /*consume_paren=*/true);
9605       return error_mark_node;
9606     }
9607   
9608   /* Parse to the closing `)'.  */
9609   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9610     {
9611       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9612                                              /*consume_paren=*/true);
9613       return error_mark_node;
9614     }
9615
9616   return finish_decltype_type (expr, id_expression_or_member_access_p);
9617 }
9618
9619 /* Special member functions [gram.special] */
9620
9621 /* Parse a conversion-function-id.
9622
9623    conversion-function-id:
9624      operator conversion-type-id
9625
9626    Returns an IDENTIFIER_NODE representing the operator.  */
9627
9628 static tree
9629 cp_parser_conversion_function_id (cp_parser* parser)
9630 {
9631   tree type;
9632   tree saved_scope;
9633   tree saved_qualifying_scope;
9634   tree saved_object_scope;
9635   tree pushed_scope = NULL_TREE;
9636
9637   /* Look for the `operator' token.  */
9638   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9639     return error_mark_node;
9640   /* When we parse the conversion-type-id, the current scope will be
9641      reset.  However, we need that information in able to look up the
9642      conversion function later, so we save it here.  */
9643   saved_scope = parser->scope;
9644   saved_qualifying_scope = parser->qualifying_scope;
9645   saved_object_scope = parser->object_scope;
9646   /* We must enter the scope of the class so that the names of
9647      entities declared within the class are available in the
9648      conversion-type-id.  For example, consider:
9649
9650        struct S {
9651          typedef int I;
9652          operator I();
9653        };
9654
9655        S::operator I() { ... }
9656
9657      In order to see that `I' is a type-name in the definition, we
9658      must be in the scope of `S'.  */
9659   if (saved_scope)
9660     pushed_scope = push_scope (saved_scope);
9661   /* Parse the conversion-type-id.  */
9662   type = cp_parser_conversion_type_id (parser);
9663   /* Leave the scope of the class, if any.  */
9664   if (pushed_scope)
9665     pop_scope (pushed_scope);
9666   /* Restore the saved scope.  */
9667   parser->scope = saved_scope;
9668   parser->qualifying_scope = saved_qualifying_scope;
9669   parser->object_scope = saved_object_scope;
9670   /* If the TYPE is invalid, indicate failure.  */
9671   if (type == error_mark_node)
9672     return error_mark_node;
9673   return mangle_conv_op_name_for_type (type);
9674 }
9675
9676 /* Parse a conversion-type-id:
9677
9678    conversion-type-id:
9679      type-specifier-seq conversion-declarator [opt]
9680
9681    Returns the TYPE specified.  */
9682
9683 static tree
9684 cp_parser_conversion_type_id (cp_parser* parser)
9685 {
9686   tree attributes;
9687   cp_decl_specifier_seq type_specifiers;
9688   cp_declarator *declarator;
9689   tree type_specified;
9690
9691   /* Parse the attributes.  */
9692   attributes = cp_parser_attributes_opt (parser);
9693   /* Parse the type-specifiers.  */
9694   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9695                                 /*is_trailing_return=*/false,
9696                                 &type_specifiers);
9697   /* If that didn't work, stop.  */
9698   if (type_specifiers.type == error_mark_node)
9699     return error_mark_node;
9700   /* Parse the conversion-declarator.  */
9701   declarator = cp_parser_conversion_declarator_opt (parser);
9702
9703   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9704                                     /*initialized=*/0, &attributes);
9705   if (attributes)
9706     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9707
9708   /* Don't give this error when parsing tentatively.  This happens to
9709      work because we always parse this definitively once.  */
9710   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9711       && type_uses_auto (type_specified))
9712     {
9713       error ("invalid use of %<auto%> in conversion operator");
9714       return error_mark_node;
9715     }
9716
9717   return type_specified;
9718 }
9719
9720 /* Parse an (optional) conversion-declarator.
9721
9722    conversion-declarator:
9723      ptr-operator conversion-declarator [opt]
9724
9725    */
9726
9727 static cp_declarator *
9728 cp_parser_conversion_declarator_opt (cp_parser* parser)
9729 {
9730   enum tree_code code;
9731   tree class_type;
9732   cp_cv_quals cv_quals;
9733
9734   /* We don't know if there's a ptr-operator next, or not.  */
9735   cp_parser_parse_tentatively (parser);
9736   /* Try the ptr-operator.  */
9737   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9738   /* If it worked, look for more conversion-declarators.  */
9739   if (cp_parser_parse_definitely (parser))
9740     {
9741       cp_declarator *declarator;
9742
9743       /* Parse another optional declarator.  */
9744       declarator = cp_parser_conversion_declarator_opt (parser);
9745
9746       return cp_parser_make_indirect_declarator
9747         (code, class_type, cv_quals, declarator);
9748    }
9749
9750   return NULL;
9751 }
9752
9753 /* Parse an (optional) ctor-initializer.
9754
9755    ctor-initializer:
9756      : mem-initializer-list
9757
9758    Returns TRUE iff the ctor-initializer was actually present.  */
9759
9760 static bool
9761 cp_parser_ctor_initializer_opt (cp_parser* parser)
9762 {
9763   /* If the next token is not a `:', then there is no
9764      ctor-initializer.  */
9765   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9766     {
9767       /* Do default initialization of any bases and members.  */
9768       if (DECL_CONSTRUCTOR_P (current_function_decl))
9769         finish_mem_initializers (NULL_TREE);
9770
9771       return false;
9772     }
9773
9774   /* Consume the `:' token.  */
9775   cp_lexer_consume_token (parser->lexer);
9776   /* And the mem-initializer-list.  */
9777   cp_parser_mem_initializer_list (parser);
9778
9779   return true;
9780 }
9781
9782 /* Parse a mem-initializer-list.
9783
9784    mem-initializer-list:
9785      mem-initializer ... [opt]
9786      mem-initializer ... [opt] , mem-initializer-list  */
9787
9788 static void
9789 cp_parser_mem_initializer_list (cp_parser* parser)
9790 {
9791   tree mem_initializer_list = NULL_TREE;
9792   cp_token *token = cp_lexer_peek_token (parser->lexer);
9793
9794   /* Let the semantic analysis code know that we are starting the
9795      mem-initializer-list.  */
9796   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9797     error_at (token->location,
9798               "only constructors take base initializers");
9799
9800   /* Loop through the list.  */
9801   while (true)
9802     {
9803       tree mem_initializer;
9804
9805       token = cp_lexer_peek_token (parser->lexer);
9806       /* Parse the mem-initializer.  */
9807       mem_initializer = cp_parser_mem_initializer (parser);
9808       /* If the next token is a `...', we're expanding member initializers. */
9809       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9810         {
9811           /* Consume the `...'. */
9812           cp_lexer_consume_token (parser->lexer);
9813
9814           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9815              can be expanded but members cannot. */
9816           if (mem_initializer != error_mark_node
9817               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9818             {
9819               error_at (token->location,
9820                         "cannot expand initializer for member %<%D%>",
9821                         TREE_PURPOSE (mem_initializer));
9822               mem_initializer = error_mark_node;
9823             }
9824
9825           /* Construct the pack expansion type. */
9826           if (mem_initializer != error_mark_node)
9827             mem_initializer = make_pack_expansion (mem_initializer);
9828         }
9829       /* Add it to the list, unless it was erroneous.  */
9830       if (mem_initializer != error_mark_node)
9831         {
9832           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9833           mem_initializer_list = mem_initializer;
9834         }
9835       /* If the next token is not a `,', we're done.  */
9836       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9837         break;
9838       /* Consume the `,' token.  */
9839       cp_lexer_consume_token (parser->lexer);
9840     }
9841
9842   /* Perform semantic analysis.  */
9843   if (DECL_CONSTRUCTOR_P (current_function_decl))
9844     finish_mem_initializers (mem_initializer_list);
9845 }
9846
9847 /* Parse a mem-initializer.
9848
9849    mem-initializer:
9850      mem-initializer-id ( expression-list [opt] )
9851      mem-initializer-id braced-init-list
9852
9853    GNU extension:
9854
9855    mem-initializer:
9856      ( expression-list [opt] )
9857
9858    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9859    class) or FIELD_DECL (for a non-static data member) to initialize;
9860    the TREE_VALUE is the expression-list.  An empty initialization
9861    list is represented by void_list_node.  */
9862
9863 static tree
9864 cp_parser_mem_initializer (cp_parser* parser)
9865 {
9866   tree mem_initializer_id;
9867   tree expression_list;
9868   tree member;
9869   cp_token *token = cp_lexer_peek_token (parser->lexer);
9870
9871   /* Find out what is being initialized.  */
9872   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9873     {
9874       permerror (token->location,
9875                  "anachronistic old-style base class initializer");
9876       mem_initializer_id = NULL_TREE;
9877     }
9878   else
9879     {
9880       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9881       if (mem_initializer_id == error_mark_node)
9882         return mem_initializer_id;
9883     }
9884   member = expand_member_init (mem_initializer_id);
9885   if (member && !DECL_P (member))
9886     in_base_initializer = 1;
9887
9888   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9889     {
9890       bool expr_non_constant_p;
9891       maybe_warn_cpp0x ("extended initializer lists");
9892       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9893       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9894       expression_list = build_tree_list (NULL_TREE, expression_list);
9895     }
9896   else
9897     {
9898       VEC(tree,gc)* vec;
9899       vec = cp_parser_parenthesized_expression_list (parser, false,
9900                                                      /*cast_p=*/false,
9901                                                      /*allow_expansion_p=*/true,
9902                                                      /*non_constant_p=*/NULL);
9903       if (vec == NULL)
9904         return error_mark_node;
9905       expression_list = build_tree_list_vec (vec);
9906       release_tree_vector (vec);
9907     }
9908
9909   if (expression_list == error_mark_node)
9910     return error_mark_node;
9911   if (!expression_list)
9912     expression_list = void_type_node;
9913
9914   in_base_initializer = 0;
9915
9916   return member ? build_tree_list (member, expression_list) : error_mark_node;
9917 }
9918
9919 /* Parse a mem-initializer-id.
9920
9921    mem-initializer-id:
9922      :: [opt] nested-name-specifier [opt] class-name
9923      identifier
9924
9925    Returns a TYPE indicating the class to be initializer for the first
9926    production.  Returns an IDENTIFIER_NODE indicating the data member
9927    to be initialized for the second production.  */
9928
9929 static tree
9930 cp_parser_mem_initializer_id (cp_parser* parser)
9931 {
9932   bool global_scope_p;
9933   bool nested_name_specifier_p;
9934   bool template_p = false;
9935   tree id;
9936
9937   cp_token *token = cp_lexer_peek_token (parser->lexer);
9938
9939   /* `typename' is not allowed in this context ([temp.res]).  */
9940   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9941     {
9942       error_at (token->location, 
9943                 "keyword %<typename%> not allowed in this context (a qualified "
9944                 "member initializer is implicitly a type)");
9945       cp_lexer_consume_token (parser->lexer);
9946     }
9947   /* Look for the optional `::' operator.  */
9948   global_scope_p
9949     = (cp_parser_global_scope_opt (parser,
9950                                    /*current_scope_valid_p=*/false)
9951        != NULL_TREE);
9952   /* Look for the optional nested-name-specifier.  The simplest way to
9953      implement:
9954
9955        [temp.res]
9956
9957        The keyword `typename' is not permitted in a base-specifier or
9958        mem-initializer; in these contexts a qualified name that
9959        depends on a template-parameter is implicitly assumed to be a
9960        type name.
9961
9962      is to assume that we have seen the `typename' keyword at this
9963      point.  */
9964   nested_name_specifier_p
9965     = (cp_parser_nested_name_specifier_opt (parser,
9966                                             /*typename_keyword_p=*/true,
9967                                             /*check_dependency_p=*/true,
9968                                             /*type_p=*/true,
9969                                             /*is_declaration=*/true)
9970        != NULL_TREE);
9971   if (nested_name_specifier_p)
9972     template_p = cp_parser_optional_template_keyword (parser);
9973   /* If there is a `::' operator or a nested-name-specifier, then we
9974      are definitely looking for a class-name.  */
9975   if (global_scope_p || nested_name_specifier_p)
9976     return cp_parser_class_name (parser,
9977                                  /*typename_keyword_p=*/true,
9978                                  /*template_keyword_p=*/template_p,
9979                                  none_type,
9980                                  /*check_dependency_p=*/true,
9981                                  /*class_head_p=*/false,
9982                                  /*is_declaration=*/true);
9983   /* Otherwise, we could also be looking for an ordinary identifier.  */
9984   cp_parser_parse_tentatively (parser);
9985   /* Try a class-name.  */
9986   id = cp_parser_class_name (parser,
9987                              /*typename_keyword_p=*/true,
9988                              /*template_keyword_p=*/false,
9989                              none_type,
9990                              /*check_dependency_p=*/true,
9991                              /*class_head_p=*/false,
9992                              /*is_declaration=*/true);
9993   /* If we found one, we're done.  */
9994   if (cp_parser_parse_definitely (parser))
9995     return id;
9996   /* Otherwise, look for an ordinary identifier.  */
9997   return cp_parser_identifier (parser);
9998 }
9999
10000 /* Overloading [gram.over] */
10001
10002 /* Parse an operator-function-id.
10003
10004    operator-function-id:
10005      operator operator
10006
10007    Returns an IDENTIFIER_NODE for the operator which is a
10008    human-readable spelling of the identifier, e.g., `operator +'.  */
10009
10010 static tree
10011 cp_parser_operator_function_id (cp_parser* parser)
10012 {
10013   /* Look for the `operator' keyword.  */
10014   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10015     return error_mark_node;
10016   /* And then the name of the operator itself.  */
10017   return cp_parser_operator (parser);
10018 }
10019
10020 /* Parse an operator.
10021
10022    operator:
10023      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10024      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10025      || ++ -- , ->* -> () []
10026
10027    GNU Extensions:
10028
10029    operator:
10030      <? >? <?= >?=
10031
10032    Returns an IDENTIFIER_NODE for the operator which is a
10033    human-readable spelling of the identifier, e.g., `operator +'.  */
10034
10035 static tree
10036 cp_parser_operator (cp_parser* parser)
10037 {
10038   tree id = NULL_TREE;
10039   cp_token *token;
10040
10041   /* Peek at the next token.  */
10042   token = cp_lexer_peek_token (parser->lexer);
10043   /* Figure out which operator we have.  */
10044   switch (token->type)
10045     {
10046     case CPP_KEYWORD:
10047       {
10048         enum tree_code op;
10049
10050         /* The keyword should be either `new' or `delete'.  */
10051         if (token->keyword == RID_NEW)
10052           op = NEW_EXPR;
10053         else if (token->keyword == RID_DELETE)
10054           op = DELETE_EXPR;
10055         else
10056           break;
10057
10058         /* Consume the `new' or `delete' token.  */
10059         cp_lexer_consume_token (parser->lexer);
10060
10061         /* Peek at the next token.  */
10062         token = cp_lexer_peek_token (parser->lexer);
10063         /* If it's a `[' token then this is the array variant of the
10064            operator.  */
10065         if (token->type == CPP_OPEN_SQUARE)
10066           {
10067             /* Consume the `[' token.  */
10068             cp_lexer_consume_token (parser->lexer);
10069             /* Look for the `]' token.  */
10070             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10071             id = ansi_opname (op == NEW_EXPR
10072                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10073           }
10074         /* Otherwise, we have the non-array variant.  */
10075         else
10076           id = ansi_opname (op);
10077
10078         return id;
10079       }
10080
10081     case CPP_PLUS:
10082       id = ansi_opname (PLUS_EXPR);
10083       break;
10084
10085     case CPP_MINUS:
10086       id = ansi_opname (MINUS_EXPR);
10087       break;
10088
10089     case CPP_MULT:
10090       id = ansi_opname (MULT_EXPR);
10091       break;
10092
10093     case CPP_DIV:
10094       id = ansi_opname (TRUNC_DIV_EXPR);
10095       break;
10096
10097     case CPP_MOD:
10098       id = ansi_opname (TRUNC_MOD_EXPR);
10099       break;
10100
10101     case CPP_XOR:
10102       id = ansi_opname (BIT_XOR_EXPR);
10103       break;
10104
10105     case CPP_AND:
10106       id = ansi_opname (BIT_AND_EXPR);
10107       break;
10108
10109     case CPP_OR:
10110       id = ansi_opname (BIT_IOR_EXPR);
10111       break;
10112
10113     case CPP_COMPL:
10114       id = ansi_opname (BIT_NOT_EXPR);
10115       break;
10116
10117     case CPP_NOT:
10118       id = ansi_opname (TRUTH_NOT_EXPR);
10119       break;
10120
10121     case CPP_EQ:
10122       id = ansi_assopname (NOP_EXPR);
10123       break;
10124
10125     case CPP_LESS:
10126       id = ansi_opname (LT_EXPR);
10127       break;
10128
10129     case CPP_GREATER:
10130       id = ansi_opname (GT_EXPR);
10131       break;
10132
10133     case CPP_PLUS_EQ:
10134       id = ansi_assopname (PLUS_EXPR);
10135       break;
10136
10137     case CPP_MINUS_EQ:
10138       id = ansi_assopname (MINUS_EXPR);
10139       break;
10140
10141     case CPP_MULT_EQ:
10142       id = ansi_assopname (MULT_EXPR);
10143       break;
10144
10145     case CPP_DIV_EQ:
10146       id = ansi_assopname (TRUNC_DIV_EXPR);
10147       break;
10148
10149     case CPP_MOD_EQ:
10150       id = ansi_assopname (TRUNC_MOD_EXPR);
10151       break;
10152
10153     case CPP_XOR_EQ:
10154       id = ansi_assopname (BIT_XOR_EXPR);
10155       break;
10156
10157     case CPP_AND_EQ:
10158       id = ansi_assopname (BIT_AND_EXPR);
10159       break;
10160
10161     case CPP_OR_EQ:
10162       id = ansi_assopname (BIT_IOR_EXPR);
10163       break;
10164
10165     case CPP_LSHIFT:
10166       id = ansi_opname (LSHIFT_EXPR);
10167       break;
10168
10169     case CPP_RSHIFT:
10170       id = ansi_opname (RSHIFT_EXPR);
10171       break;
10172
10173     case CPP_LSHIFT_EQ:
10174       id = ansi_assopname (LSHIFT_EXPR);
10175       break;
10176
10177     case CPP_RSHIFT_EQ:
10178       id = ansi_assopname (RSHIFT_EXPR);
10179       break;
10180
10181     case CPP_EQ_EQ:
10182       id = ansi_opname (EQ_EXPR);
10183       break;
10184
10185     case CPP_NOT_EQ:
10186       id = ansi_opname (NE_EXPR);
10187       break;
10188
10189     case CPP_LESS_EQ:
10190       id = ansi_opname (LE_EXPR);
10191       break;
10192
10193     case CPP_GREATER_EQ:
10194       id = ansi_opname (GE_EXPR);
10195       break;
10196
10197     case CPP_AND_AND:
10198       id = ansi_opname (TRUTH_ANDIF_EXPR);
10199       break;
10200
10201     case CPP_OR_OR:
10202       id = ansi_opname (TRUTH_ORIF_EXPR);
10203       break;
10204
10205     case CPP_PLUS_PLUS:
10206       id = ansi_opname (POSTINCREMENT_EXPR);
10207       break;
10208
10209     case CPP_MINUS_MINUS:
10210       id = ansi_opname (PREDECREMENT_EXPR);
10211       break;
10212
10213     case CPP_COMMA:
10214       id = ansi_opname (COMPOUND_EXPR);
10215       break;
10216
10217     case CPP_DEREF_STAR:
10218       id = ansi_opname (MEMBER_REF);
10219       break;
10220
10221     case CPP_DEREF:
10222       id = ansi_opname (COMPONENT_REF);
10223       break;
10224
10225     case CPP_OPEN_PAREN:
10226       /* Consume the `('.  */
10227       cp_lexer_consume_token (parser->lexer);
10228       /* Look for the matching `)'.  */
10229       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10230       return ansi_opname (CALL_EXPR);
10231
10232     case CPP_OPEN_SQUARE:
10233       /* Consume the `['.  */
10234       cp_lexer_consume_token (parser->lexer);
10235       /* Look for the matching `]'.  */
10236       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10237       return ansi_opname (ARRAY_REF);
10238
10239     default:
10240       /* Anything else is an error.  */
10241       break;
10242     }
10243
10244   /* If we have selected an identifier, we need to consume the
10245      operator token.  */
10246   if (id)
10247     cp_lexer_consume_token (parser->lexer);
10248   /* Otherwise, no valid operator name was present.  */
10249   else
10250     {
10251       cp_parser_error (parser, "expected operator");
10252       id = error_mark_node;
10253     }
10254
10255   return id;
10256 }
10257
10258 /* Parse a template-declaration.
10259
10260    template-declaration:
10261      export [opt] template < template-parameter-list > declaration
10262
10263    If MEMBER_P is TRUE, this template-declaration occurs within a
10264    class-specifier.
10265
10266    The grammar rule given by the standard isn't correct.  What
10267    is really meant is:
10268
10269    template-declaration:
10270      export [opt] template-parameter-list-seq
10271        decl-specifier-seq [opt] init-declarator [opt] ;
10272      export [opt] template-parameter-list-seq
10273        function-definition
10274
10275    template-parameter-list-seq:
10276      template-parameter-list-seq [opt]
10277      template < template-parameter-list >  */
10278
10279 static void
10280 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10281 {
10282   /* Check for `export'.  */
10283   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10284     {
10285       /* Consume the `export' token.  */
10286       cp_lexer_consume_token (parser->lexer);
10287       /* Warn that we do not support `export'.  */
10288       warning (0, "keyword %<export%> not implemented, and will be ignored");
10289     }
10290
10291   cp_parser_template_declaration_after_export (parser, member_p);
10292 }
10293
10294 /* Parse a template-parameter-list.
10295
10296    template-parameter-list:
10297      template-parameter
10298      template-parameter-list , template-parameter
10299
10300    Returns a TREE_LIST.  Each node represents a template parameter.
10301    The nodes are connected via their TREE_CHAINs.  */
10302
10303 static tree
10304 cp_parser_template_parameter_list (cp_parser* parser)
10305 {
10306   tree parameter_list = NULL_TREE;
10307
10308   begin_template_parm_list ();
10309   while (true)
10310     {
10311       tree parameter;
10312       bool is_non_type;
10313       bool is_parameter_pack;
10314       location_t parm_loc;
10315
10316       /* Parse the template-parameter.  */
10317       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10318       parameter = cp_parser_template_parameter (parser, 
10319                                                 &is_non_type,
10320                                                 &is_parameter_pack);
10321       /* Add it to the list.  */
10322       if (parameter != error_mark_node)
10323         parameter_list = process_template_parm (parameter_list,
10324                                                 parm_loc,
10325                                                 parameter,
10326                                                 is_non_type,
10327                                                 is_parameter_pack);
10328       else
10329        {
10330          tree err_parm = build_tree_list (parameter, parameter);
10331          TREE_VALUE (err_parm) = error_mark_node;
10332          parameter_list = chainon (parameter_list, err_parm);
10333        }
10334
10335       /* If the next token is not a `,', we're done.  */
10336       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10337         break;
10338       /* Otherwise, consume the `,' token.  */
10339       cp_lexer_consume_token (parser->lexer);
10340     }
10341
10342   return end_template_parm_list (parameter_list);
10343 }
10344
10345 /* Parse a template-parameter.
10346
10347    template-parameter:
10348      type-parameter
10349      parameter-declaration
10350
10351    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
10352    the parameter.  The TREE_PURPOSE is the default value, if any.
10353    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
10354    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
10355    set to true iff this parameter is a parameter pack. */
10356
10357 static tree
10358 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10359                               bool *is_parameter_pack)
10360 {
10361   cp_token *token;
10362   cp_parameter_declarator *parameter_declarator;
10363   cp_declarator *id_declarator;
10364   tree parm;
10365
10366   /* Assume it is a type parameter or a template parameter.  */
10367   *is_non_type = false;
10368   /* Assume it not a parameter pack. */
10369   *is_parameter_pack = false;
10370   /* Peek at the next token.  */
10371   token = cp_lexer_peek_token (parser->lexer);
10372   /* If it is `class' or `template', we have a type-parameter.  */
10373   if (token->keyword == RID_TEMPLATE)
10374     return cp_parser_type_parameter (parser, is_parameter_pack);
10375   /* If it is `class' or `typename' we do not know yet whether it is a
10376      type parameter or a non-type parameter.  Consider:
10377
10378        template <typename T, typename T::X X> ...
10379
10380      or:
10381
10382        template <class C, class D*> ...
10383
10384      Here, the first parameter is a type parameter, and the second is
10385      a non-type parameter.  We can tell by looking at the token after
10386      the identifier -- if it is a `,', `=', or `>' then we have a type
10387      parameter.  */
10388   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10389     {
10390       /* Peek at the token after `class' or `typename'.  */
10391       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10392       /* If it's an ellipsis, we have a template type parameter
10393          pack. */
10394       if (token->type == CPP_ELLIPSIS)
10395         return cp_parser_type_parameter (parser, is_parameter_pack);
10396       /* If it's an identifier, skip it.  */
10397       if (token->type == CPP_NAME)
10398         token = cp_lexer_peek_nth_token (parser->lexer, 3);
10399       /* Now, see if the token looks like the end of a template
10400          parameter.  */
10401       if (token->type == CPP_COMMA
10402           || token->type == CPP_EQ
10403           || token->type == CPP_GREATER)
10404         return cp_parser_type_parameter (parser, is_parameter_pack);
10405     }
10406
10407   /* Otherwise, it is a non-type parameter.
10408
10409      [temp.param]
10410
10411      When parsing a default template-argument for a non-type
10412      template-parameter, the first non-nested `>' is taken as the end
10413      of the template parameter-list rather than a greater-than
10414      operator.  */
10415   *is_non_type = true;
10416   parameter_declarator
10417      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10418                                         /*parenthesized_p=*/NULL);
10419
10420   /* If the parameter declaration is marked as a parameter pack, set
10421      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10422      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10423      grokdeclarator. */
10424   if (parameter_declarator
10425       && parameter_declarator->declarator
10426       && parameter_declarator->declarator->parameter_pack_p)
10427     {
10428       *is_parameter_pack = true;
10429       parameter_declarator->declarator->parameter_pack_p = false;
10430     }
10431
10432   /* If the next token is an ellipsis, and we don't already have it
10433      marked as a parameter pack, then we have a parameter pack (that
10434      has no declarator).  */
10435   if (!*is_parameter_pack
10436       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10437       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10438     {
10439       /* Consume the `...'.  */
10440       cp_lexer_consume_token (parser->lexer);
10441       maybe_warn_variadic_templates ();
10442       
10443       *is_parameter_pack = true;
10444     }
10445   /* We might end up with a pack expansion as the type of the non-type
10446      template parameter, in which case this is a non-type template
10447      parameter pack.  */
10448   else if (parameter_declarator
10449            && parameter_declarator->decl_specifiers.type
10450            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10451     {
10452       *is_parameter_pack = true;
10453       parameter_declarator->decl_specifiers.type = 
10454         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10455     }
10456
10457   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10458     {
10459       /* Parameter packs cannot have default arguments.  However, a
10460          user may try to do so, so we'll parse them and give an
10461          appropriate diagnostic here.  */
10462
10463       /* Consume the `='.  */
10464       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10465       cp_lexer_consume_token (parser->lexer);
10466       
10467       /* Find the name of the parameter pack.  */     
10468       id_declarator = parameter_declarator->declarator;
10469       while (id_declarator && id_declarator->kind != cdk_id)
10470         id_declarator = id_declarator->declarator;
10471       
10472       if (id_declarator && id_declarator->kind == cdk_id)
10473         error_at (start_token->location,
10474                   "template parameter pack %qD cannot have a default argument",
10475                   id_declarator->u.id.unqualified_name);
10476       else
10477         error_at (start_token->location,
10478                   "template parameter pack cannot have a default argument");
10479       
10480       /* Parse the default argument, but throw away the result.  */
10481       cp_parser_default_argument (parser, /*template_parm_p=*/true);
10482     }
10483
10484   parm = grokdeclarator (parameter_declarator->declarator,
10485                          &parameter_declarator->decl_specifiers,
10486                          PARM, /*initialized=*/0,
10487                          /*attrlist=*/NULL);
10488   if (parm == error_mark_node)
10489     return error_mark_node;
10490
10491   return build_tree_list (parameter_declarator->default_argument, parm);
10492 }
10493
10494 /* Parse a type-parameter.
10495
10496    type-parameter:
10497      class identifier [opt]
10498      class identifier [opt] = type-id
10499      typename identifier [opt]
10500      typename identifier [opt] = type-id
10501      template < template-parameter-list > class identifier [opt]
10502      template < template-parameter-list > class identifier [opt]
10503        = id-expression
10504
10505    GNU Extension (variadic templates):
10506
10507    type-parameter:
10508      class ... identifier [opt]
10509      typename ... identifier [opt]
10510
10511    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
10512    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
10513    the declaration of the parameter.
10514
10515    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10516
10517 static tree
10518 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10519 {
10520   cp_token *token;
10521   tree parameter;
10522
10523   /* Look for a keyword to tell us what kind of parameter this is.  */
10524   token = cp_parser_require (parser, CPP_KEYWORD,
10525                              "%<class%>, %<typename%>, or %<template%>");
10526   if (!token)
10527     return error_mark_node;
10528
10529   switch (token->keyword)
10530     {
10531     case RID_CLASS:
10532     case RID_TYPENAME:
10533       {
10534         tree identifier;
10535         tree default_argument;
10536
10537         /* If the next token is an ellipsis, we have a template
10538            argument pack. */
10539         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10540           {
10541             /* Consume the `...' token. */
10542             cp_lexer_consume_token (parser->lexer);
10543             maybe_warn_variadic_templates ();
10544
10545             *is_parameter_pack = true;
10546           }
10547
10548         /* If the next token is an identifier, then it names the
10549            parameter.  */
10550         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10551           identifier = cp_parser_identifier (parser);
10552         else
10553           identifier = NULL_TREE;
10554
10555         /* Create the parameter.  */
10556         parameter = finish_template_type_parm (class_type_node, identifier);
10557
10558         /* If the next token is an `=', we have a default argument.  */
10559         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10560           {
10561             /* Consume the `=' token.  */
10562             cp_lexer_consume_token (parser->lexer);
10563             /* Parse the default-argument.  */
10564             push_deferring_access_checks (dk_no_deferred);
10565             default_argument = cp_parser_type_id (parser);
10566
10567             /* Template parameter packs cannot have default
10568                arguments. */
10569             if (*is_parameter_pack)
10570               {
10571                 if (identifier)
10572                   error_at (token->location,
10573                             "template parameter pack %qD cannot have a "
10574                             "default argument", identifier);
10575                 else
10576                   error_at (token->location,
10577                             "template parameter packs cannot have "
10578                             "default arguments");
10579                 default_argument = NULL_TREE;
10580               }
10581             pop_deferring_access_checks ();
10582           }
10583         else
10584           default_argument = NULL_TREE;
10585
10586         /* Create the combined representation of the parameter and the
10587            default argument.  */
10588         parameter = build_tree_list (default_argument, parameter);
10589       }
10590       break;
10591
10592     case RID_TEMPLATE:
10593       {
10594         tree parameter_list;
10595         tree identifier;
10596         tree default_argument;
10597
10598         /* Look for the `<'.  */
10599         cp_parser_require (parser, CPP_LESS, "%<<%>");
10600         /* Parse the template-parameter-list.  */
10601         parameter_list = cp_parser_template_parameter_list (parser);
10602         /* Look for the `>'.  */
10603         cp_parser_require (parser, CPP_GREATER, "%<>%>");
10604         /* Look for the `class' keyword.  */
10605         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10606         /* If the next token is an ellipsis, we have a template
10607            argument pack. */
10608         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10609           {
10610             /* Consume the `...' token. */
10611             cp_lexer_consume_token (parser->lexer);
10612             maybe_warn_variadic_templates ();
10613
10614             *is_parameter_pack = true;
10615           }
10616         /* If the next token is an `=', then there is a
10617            default-argument.  If the next token is a `>', we are at
10618            the end of the parameter-list.  If the next token is a `,',
10619            then we are at the end of this parameter.  */
10620         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10621             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10622             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10623           {
10624             identifier = cp_parser_identifier (parser);
10625             /* Treat invalid names as if the parameter were nameless.  */
10626             if (identifier == error_mark_node)
10627               identifier = NULL_TREE;
10628           }
10629         else
10630           identifier = NULL_TREE;
10631
10632         /* Create the template parameter.  */
10633         parameter = finish_template_template_parm (class_type_node,
10634                                                    identifier);
10635
10636         /* If the next token is an `=', then there is a
10637            default-argument.  */
10638         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10639           {
10640             bool is_template;
10641
10642             /* Consume the `='.  */
10643             cp_lexer_consume_token (parser->lexer);
10644             /* Parse the id-expression.  */
10645             push_deferring_access_checks (dk_no_deferred);
10646             /* save token before parsing the id-expression, for error
10647                reporting */
10648             token = cp_lexer_peek_token (parser->lexer);
10649             default_argument
10650               = cp_parser_id_expression (parser,
10651                                          /*template_keyword_p=*/false,
10652                                          /*check_dependency_p=*/true,
10653                                          /*template_p=*/&is_template,
10654                                          /*declarator_p=*/false,
10655                                          /*optional_p=*/false);
10656             if (TREE_CODE (default_argument) == TYPE_DECL)
10657               /* If the id-expression was a template-id that refers to
10658                  a template-class, we already have the declaration here,
10659                  so no further lookup is needed.  */
10660                  ;
10661             else
10662               /* Look up the name.  */
10663               default_argument
10664                 = cp_parser_lookup_name (parser, default_argument,
10665                                          none_type,
10666                                          /*is_template=*/is_template,
10667                                          /*is_namespace=*/false,
10668                                          /*check_dependency=*/true,
10669                                          /*ambiguous_decls=*/NULL,
10670                                          token->location);
10671             /* See if the default argument is valid.  */
10672             default_argument
10673               = check_template_template_default_arg (default_argument);
10674
10675             /* Template parameter packs cannot have default
10676                arguments. */
10677             if (*is_parameter_pack)
10678               {
10679                 if (identifier)
10680                   error_at (token->location,
10681                             "template parameter pack %qD cannot "
10682                             "have a default argument",
10683                             identifier);
10684                 else
10685                   error_at (token->location, "template parameter packs cannot "
10686                             "have default arguments");
10687                 default_argument = NULL_TREE;
10688               }
10689             pop_deferring_access_checks ();
10690           }
10691         else
10692           default_argument = NULL_TREE;
10693
10694         /* Create the combined representation of the parameter and the
10695            default argument.  */
10696         parameter = build_tree_list (default_argument, parameter);
10697       }
10698       break;
10699
10700     default:
10701       gcc_unreachable ();
10702       break;
10703     }
10704
10705   return parameter;
10706 }
10707
10708 /* Parse a template-id.
10709
10710    template-id:
10711      template-name < template-argument-list [opt] >
10712
10713    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10714    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10715    returned.  Otherwise, if the template-name names a function, or set
10716    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10717    names a class, returns a TYPE_DECL for the specialization.
10718
10719    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10720    uninstantiated templates.  */
10721
10722 static tree
10723 cp_parser_template_id (cp_parser *parser,
10724                        bool template_keyword_p,
10725                        bool check_dependency_p,
10726                        bool is_declaration)
10727 {
10728   int i;
10729   tree templ;
10730   tree arguments;
10731   tree template_id;
10732   cp_token_position start_of_id = 0;
10733   deferred_access_check *chk;
10734   VEC (deferred_access_check,gc) *access_check;
10735   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10736   bool is_identifier;
10737
10738   /* If the next token corresponds to a template-id, there is no need
10739      to reparse it.  */
10740   next_token = cp_lexer_peek_token (parser->lexer);
10741   if (next_token->type == CPP_TEMPLATE_ID)
10742     {
10743       struct tree_check *check_value;
10744
10745       /* Get the stored value.  */
10746       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10747       /* Perform any access checks that were deferred.  */
10748       access_check = check_value->checks;
10749       if (access_check)
10750         {
10751           for (i = 0 ;
10752                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10753                ++i)
10754             {
10755               perform_or_defer_access_check (chk->binfo,
10756                                              chk->decl,
10757                                              chk->diag_decl);
10758             }
10759         }
10760       /* Return the stored value.  */
10761       return check_value->value;
10762     }
10763
10764   /* Avoid performing name lookup if there is no possibility of
10765      finding a template-id.  */
10766   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10767       || (next_token->type == CPP_NAME
10768           && !cp_parser_nth_token_starts_template_argument_list_p
10769                (parser, 2)))
10770     {
10771       cp_parser_error (parser, "expected template-id");
10772       return error_mark_node;
10773     }
10774
10775   /* Remember where the template-id starts.  */
10776   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10777     start_of_id = cp_lexer_token_position (parser->lexer, false);
10778
10779   push_deferring_access_checks (dk_deferred);
10780
10781   /* Parse the template-name.  */
10782   is_identifier = false;
10783   token = cp_lexer_peek_token (parser->lexer);
10784   templ = cp_parser_template_name (parser, template_keyword_p,
10785                                    check_dependency_p,
10786                                    is_declaration,
10787                                    &is_identifier);
10788   if (templ == error_mark_node || is_identifier)
10789     {
10790       pop_deferring_access_checks ();
10791       return templ;
10792     }
10793
10794   /* If we find the sequence `[:' after a template-name, it's probably
10795      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10796      parse correctly the argument list.  */
10797   next_token = cp_lexer_peek_token (parser->lexer);
10798   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10799   if (next_token->type == CPP_OPEN_SQUARE
10800       && next_token->flags & DIGRAPH
10801       && next_token_2->type == CPP_COLON
10802       && !(next_token_2->flags & PREV_WHITE))
10803     {
10804       cp_parser_parse_tentatively (parser);
10805       /* Change `:' into `::'.  */
10806       next_token_2->type = CPP_SCOPE;
10807       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10808          CPP_LESS.  */
10809       cp_lexer_consume_token (parser->lexer);
10810
10811       /* Parse the arguments.  */
10812       arguments = cp_parser_enclosed_template_argument_list (parser);
10813       if (!cp_parser_parse_definitely (parser))
10814         {
10815           /* If we couldn't parse an argument list, then we revert our changes
10816              and return simply an error. Maybe this is not a template-id
10817              after all.  */
10818           next_token_2->type = CPP_COLON;
10819           cp_parser_error (parser, "expected %<<%>");
10820           pop_deferring_access_checks ();
10821           return error_mark_node;
10822         }
10823       /* Otherwise, emit an error about the invalid digraph, but continue
10824          parsing because we got our argument list.  */
10825       if (permerror (next_token->location,
10826                      "%<<::%> cannot begin a template-argument list"))
10827         {
10828           static bool hint = false;
10829           inform (next_token->location,
10830                   "%<<:%> is an alternate spelling for %<[%>."
10831                   " Insert whitespace between %<<%> and %<::%>");
10832           if (!hint && !flag_permissive)
10833             {
10834               inform (next_token->location, "(if you use %<-fpermissive%>"
10835                       " G++ will accept your code)");
10836               hint = true;
10837             }
10838         }
10839     }
10840   else
10841     {
10842       /* Look for the `<' that starts the template-argument-list.  */
10843       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10844         {
10845           pop_deferring_access_checks ();
10846           return error_mark_node;
10847         }
10848       /* Parse the arguments.  */
10849       arguments = cp_parser_enclosed_template_argument_list (parser);
10850     }
10851
10852   /* Build a representation of the specialization.  */
10853   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10854     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10855   else if (DECL_CLASS_TEMPLATE_P (templ)
10856            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10857     {
10858       bool entering_scope;
10859       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10860          template (rather than some instantiation thereof) only if
10861          is not nested within some other construct.  For example, in
10862          "template <typename T> void f(T) { A<T>::", A<T> is just an
10863          instantiation of A.  */
10864       entering_scope = (template_parm_scope_p ()
10865                         && cp_lexer_next_token_is (parser->lexer,
10866                                                    CPP_SCOPE));
10867       template_id
10868         = finish_template_type (templ, arguments, entering_scope);
10869     }
10870   else
10871     {
10872       /* If it's not a class-template or a template-template, it should be
10873          a function-template.  */
10874       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10875                    || TREE_CODE (templ) == OVERLOAD
10876                    || BASELINK_P (templ)));
10877
10878       template_id = lookup_template_function (templ, arguments);
10879     }
10880
10881   /* If parsing tentatively, replace the sequence of tokens that makes
10882      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10883      should we re-parse the token stream, we will not have to repeat
10884      the effort required to do the parse, nor will we issue duplicate
10885      error messages about problems during instantiation of the
10886      template.  */
10887   if (start_of_id)
10888     {
10889       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10890
10891       /* Reset the contents of the START_OF_ID token.  */
10892       token->type = CPP_TEMPLATE_ID;
10893       /* Retrieve any deferred checks.  Do not pop this access checks yet
10894          so the memory will not be reclaimed during token replacing below.  */
10895       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10896       token->u.tree_check_value->value = template_id;
10897       token->u.tree_check_value->checks = get_deferred_access_checks ();
10898       token->keyword = RID_MAX;
10899
10900       /* Purge all subsequent tokens.  */
10901       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10902
10903       /* ??? Can we actually assume that, if template_id ==
10904          error_mark_node, we will have issued a diagnostic to the
10905          user, as opposed to simply marking the tentative parse as
10906          failed?  */
10907       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10908         error_at (token->location, "parse error in template argument list");
10909     }
10910
10911   pop_deferring_access_checks ();
10912   return template_id;
10913 }
10914
10915 /* Parse a template-name.
10916
10917    template-name:
10918      identifier
10919
10920    The standard should actually say:
10921
10922    template-name:
10923      identifier
10924      operator-function-id
10925
10926    A defect report has been filed about this issue.
10927
10928    A conversion-function-id cannot be a template name because they cannot
10929    be part of a template-id. In fact, looking at this code:
10930
10931    a.operator K<int>()
10932
10933    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10934    It is impossible to call a templated conversion-function-id with an
10935    explicit argument list, since the only allowed template parameter is
10936    the type to which it is converting.
10937
10938    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10939    `template' keyword, in a construction like:
10940
10941      T::template f<3>()
10942
10943    In that case `f' is taken to be a template-name, even though there
10944    is no way of knowing for sure.
10945
10946    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10947    name refers to a set of overloaded functions, at least one of which
10948    is a template, or an IDENTIFIER_NODE with the name of the template,
10949    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10950    names are looked up inside uninstantiated templates.  */
10951
10952 static tree
10953 cp_parser_template_name (cp_parser* parser,
10954                          bool template_keyword_p,
10955                          bool check_dependency_p,
10956                          bool is_declaration,
10957                          bool *is_identifier)
10958 {
10959   tree identifier;
10960   tree decl;
10961   tree fns;
10962   cp_token *token = cp_lexer_peek_token (parser->lexer);
10963
10964   /* If the next token is `operator', then we have either an
10965      operator-function-id or a conversion-function-id.  */
10966   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10967     {
10968       /* We don't know whether we're looking at an
10969          operator-function-id or a conversion-function-id.  */
10970       cp_parser_parse_tentatively (parser);
10971       /* Try an operator-function-id.  */
10972       identifier = cp_parser_operator_function_id (parser);
10973       /* If that didn't work, try a conversion-function-id.  */
10974       if (!cp_parser_parse_definitely (parser))
10975         {
10976           cp_parser_error (parser, "expected template-name");
10977           return error_mark_node;
10978         }
10979     }
10980   /* Look for the identifier.  */
10981   else
10982     identifier = cp_parser_identifier (parser);
10983
10984   /* If we didn't find an identifier, we don't have a template-id.  */
10985   if (identifier == error_mark_node)
10986     return error_mark_node;
10987
10988   /* If the name immediately followed the `template' keyword, then it
10989      is a template-name.  However, if the next token is not `<', then
10990      we do not treat it as a template-name, since it is not being used
10991      as part of a template-id.  This enables us to handle constructs
10992      like:
10993
10994        template <typename T> struct S { S(); };
10995        template <typename T> S<T>::S();
10996
10997      correctly.  We would treat `S' as a template -- if it were `S<T>'
10998      -- but we do not if there is no `<'.  */
10999
11000   if (processing_template_decl
11001       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11002     {
11003       /* In a declaration, in a dependent context, we pretend that the
11004          "template" keyword was present in order to improve error
11005          recovery.  For example, given:
11006
11007            template <typename T> void f(T::X<int>);
11008
11009          we want to treat "X<int>" as a template-id.  */
11010       if (is_declaration
11011           && !template_keyword_p
11012           && parser->scope && TYPE_P (parser->scope)
11013           && check_dependency_p
11014           && dependent_scope_p (parser->scope)
11015           /* Do not do this for dtors (or ctors), since they never
11016              need the template keyword before their name.  */
11017           && !constructor_name_p (identifier, parser->scope))
11018         {
11019           cp_token_position start = 0;
11020
11021           /* Explain what went wrong.  */
11022           error_at (token->location, "non-template %qD used as template",
11023                     identifier);
11024           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11025                   parser->scope, identifier);
11026           /* If parsing tentatively, find the location of the "<" token.  */
11027           if (cp_parser_simulate_error (parser))
11028             start = cp_lexer_token_position (parser->lexer, true);
11029           /* Parse the template arguments so that we can issue error
11030              messages about them.  */
11031           cp_lexer_consume_token (parser->lexer);
11032           cp_parser_enclosed_template_argument_list (parser);
11033           /* Skip tokens until we find a good place from which to
11034              continue parsing.  */
11035           cp_parser_skip_to_closing_parenthesis (parser,
11036                                                  /*recovering=*/true,
11037                                                  /*or_comma=*/true,
11038                                                  /*consume_paren=*/false);
11039           /* If parsing tentatively, permanently remove the
11040              template argument list.  That will prevent duplicate
11041              error messages from being issued about the missing
11042              "template" keyword.  */
11043           if (start)
11044             cp_lexer_purge_tokens_after (parser->lexer, start);
11045           if (is_identifier)
11046             *is_identifier = true;
11047           return identifier;
11048         }
11049
11050       /* If the "template" keyword is present, then there is generally
11051          no point in doing name-lookup, so we just return IDENTIFIER.
11052          But, if the qualifying scope is non-dependent then we can
11053          (and must) do name-lookup normally.  */
11054       if (template_keyword_p
11055           && (!parser->scope
11056               || (TYPE_P (parser->scope)
11057                   && dependent_type_p (parser->scope))))
11058         return identifier;
11059     }
11060
11061   /* Look up the name.  */
11062   decl = cp_parser_lookup_name (parser, identifier,
11063                                 none_type,
11064                                 /*is_template=*/false,
11065                                 /*is_namespace=*/false,
11066                                 check_dependency_p,
11067                                 /*ambiguous_decls=*/NULL,
11068                                 token->location);
11069   decl = maybe_get_template_decl_from_type_decl (decl);
11070
11071   /* If DECL is a template, then the name was a template-name.  */
11072   if (TREE_CODE (decl) == TEMPLATE_DECL)
11073     ;
11074   else
11075     {
11076       tree fn = NULL_TREE;
11077
11078       /* The standard does not explicitly indicate whether a name that
11079          names a set of overloaded declarations, some of which are
11080          templates, is a template-name.  However, such a name should
11081          be a template-name; otherwise, there is no way to form a
11082          template-id for the overloaded templates.  */
11083       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11084       if (TREE_CODE (fns) == OVERLOAD)
11085         for (fn = fns; fn; fn = OVL_NEXT (fn))
11086           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11087             break;
11088
11089       if (!fn)
11090         {
11091           /* The name does not name a template.  */
11092           cp_parser_error (parser, "expected template-name");
11093           return error_mark_node;
11094         }
11095     }
11096
11097   /* If DECL is dependent, and refers to a function, then just return
11098      its name; we will look it up again during template instantiation.  */
11099   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11100     {
11101       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11102       if (TYPE_P (scope) && dependent_type_p (scope))
11103         return identifier;
11104     }
11105
11106   return decl;
11107 }
11108
11109 /* Parse a template-argument-list.
11110
11111    template-argument-list:
11112      template-argument ... [opt]
11113      template-argument-list , template-argument ... [opt]
11114
11115    Returns a TREE_VEC containing the arguments.  */
11116
11117 static tree
11118 cp_parser_template_argument_list (cp_parser* parser)
11119 {
11120   tree fixed_args[10];
11121   unsigned n_args = 0;
11122   unsigned alloced = 10;
11123   tree *arg_ary = fixed_args;
11124   tree vec;
11125   bool saved_in_template_argument_list_p;
11126   bool saved_ice_p;
11127   bool saved_non_ice_p;
11128
11129   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11130   parser->in_template_argument_list_p = true;
11131   /* Even if the template-id appears in an integral
11132      constant-expression, the contents of the argument list do
11133      not.  */
11134   saved_ice_p = parser->integral_constant_expression_p;
11135   parser->integral_constant_expression_p = false;
11136   saved_non_ice_p = parser->non_integral_constant_expression_p;
11137   parser->non_integral_constant_expression_p = false;
11138   /* Parse the arguments.  */
11139   do
11140     {
11141       tree argument;
11142
11143       if (n_args)
11144         /* Consume the comma.  */
11145         cp_lexer_consume_token (parser->lexer);
11146
11147       /* Parse the template-argument.  */
11148       argument = cp_parser_template_argument (parser);
11149
11150       /* If the next token is an ellipsis, we're expanding a template
11151          argument pack. */
11152       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11153         {
11154           if (argument == error_mark_node)
11155             {
11156               cp_token *token = cp_lexer_peek_token (parser->lexer);
11157               error_at (token->location,
11158                         "expected parameter pack before %<...%>");
11159             }
11160           /* Consume the `...' token. */
11161           cp_lexer_consume_token (parser->lexer);
11162
11163           /* Make the argument into a TYPE_PACK_EXPANSION or
11164              EXPR_PACK_EXPANSION. */
11165           argument = make_pack_expansion (argument);
11166         }
11167
11168       if (n_args == alloced)
11169         {
11170           alloced *= 2;
11171
11172           if (arg_ary == fixed_args)
11173             {
11174               arg_ary = XNEWVEC (tree, alloced);
11175               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11176             }
11177           else
11178             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11179         }
11180       arg_ary[n_args++] = argument;
11181     }
11182   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11183
11184   vec = make_tree_vec (n_args);
11185
11186   while (n_args--)
11187     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11188
11189   if (arg_ary != fixed_args)
11190     free (arg_ary);
11191   parser->non_integral_constant_expression_p = saved_non_ice_p;
11192   parser->integral_constant_expression_p = saved_ice_p;
11193   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11194   return vec;
11195 }
11196
11197 /* Parse a template-argument.
11198
11199    template-argument:
11200      assignment-expression
11201      type-id
11202      id-expression
11203
11204    The representation is that of an assignment-expression, type-id, or
11205    id-expression -- except that the qualified id-expression is
11206    evaluated, so that the value returned is either a DECL or an
11207    OVERLOAD.
11208
11209    Although the standard says "assignment-expression", it forbids
11210    throw-expressions or assignments in the template argument.
11211    Therefore, we use "conditional-expression" instead.  */
11212
11213 static tree
11214 cp_parser_template_argument (cp_parser* parser)
11215 {
11216   tree argument;
11217   bool template_p;
11218   bool address_p;
11219   bool maybe_type_id = false;
11220   cp_token *token = NULL, *argument_start_token = NULL;
11221   cp_id_kind idk;
11222
11223   /* There's really no way to know what we're looking at, so we just
11224      try each alternative in order.
11225
11226        [temp.arg]
11227
11228        In a template-argument, an ambiguity between a type-id and an
11229        expression is resolved to a type-id, regardless of the form of
11230        the corresponding template-parameter.
11231
11232      Therefore, we try a type-id first.  */
11233   cp_parser_parse_tentatively (parser);
11234   argument = cp_parser_template_type_arg (parser);
11235   /* If there was no error parsing the type-id but the next token is a
11236      '>>', our behavior depends on which dialect of C++ we're
11237      parsing. In C++98, we probably found a typo for '> >'. But there
11238      are type-id which are also valid expressions. For instance:
11239
11240      struct X { int operator >> (int); };
11241      template <int V> struct Foo {};
11242      Foo<X () >> 5> r;
11243
11244      Here 'X()' is a valid type-id of a function type, but the user just
11245      wanted to write the expression "X() >> 5". Thus, we remember that we
11246      found a valid type-id, but we still try to parse the argument as an
11247      expression to see what happens. 
11248
11249      In C++0x, the '>>' will be considered two separate '>'
11250      tokens.  */
11251   if (!cp_parser_error_occurred (parser)
11252       && cxx_dialect == cxx98
11253       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11254     {
11255       maybe_type_id = true;
11256       cp_parser_abort_tentative_parse (parser);
11257     }
11258   else
11259     {
11260       /* If the next token isn't a `,' or a `>', then this argument wasn't
11261       really finished. This means that the argument is not a valid
11262       type-id.  */
11263       if (!cp_parser_next_token_ends_template_argument_p (parser))
11264         cp_parser_error (parser, "expected template-argument");
11265       /* If that worked, we're done.  */
11266       if (cp_parser_parse_definitely (parser))
11267         return argument;
11268     }
11269   /* We're still not sure what the argument will be.  */
11270   cp_parser_parse_tentatively (parser);
11271   /* Try a template.  */
11272   argument_start_token = cp_lexer_peek_token (parser->lexer);
11273   argument = cp_parser_id_expression (parser,
11274                                       /*template_keyword_p=*/false,
11275                                       /*check_dependency_p=*/true,
11276                                       &template_p,
11277                                       /*declarator_p=*/false,
11278                                       /*optional_p=*/false);
11279   /* If the next token isn't a `,' or a `>', then this argument wasn't
11280      really finished.  */
11281   if (!cp_parser_next_token_ends_template_argument_p (parser))
11282     cp_parser_error (parser, "expected template-argument");
11283   if (!cp_parser_error_occurred (parser))
11284     {
11285       /* Figure out what is being referred to.  If the id-expression
11286          was for a class template specialization, then we will have a
11287          TYPE_DECL at this point.  There is no need to do name lookup
11288          at this point in that case.  */
11289       if (TREE_CODE (argument) != TYPE_DECL)
11290         argument = cp_parser_lookup_name (parser, argument,
11291                                           none_type,
11292                                           /*is_template=*/template_p,
11293                                           /*is_namespace=*/false,
11294                                           /*check_dependency=*/true,
11295                                           /*ambiguous_decls=*/NULL,
11296                                           argument_start_token->location);
11297       if (TREE_CODE (argument) != TEMPLATE_DECL
11298           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11299         cp_parser_error (parser, "expected template-name");
11300     }
11301   if (cp_parser_parse_definitely (parser))
11302     return argument;
11303   /* It must be a non-type argument.  There permitted cases are given
11304      in [temp.arg.nontype]:
11305
11306      -- an integral constant-expression of integral or enumeration
11307         type; or
11308
11309      -- the name of a non-type template-parameter; or
11310
11311      -- the name of an object or function with external linkage...
11312
11313      -- the address of an object or function with external linkage...
11314
11315      -- a pointer to member...  */
11316   /* Look for a non-type template parameter.  */
11317   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11318     {
11319       cp_parser_parse_tentatively (parser);
11320       argument = cp_parser_primary_expression (parser,
11321                                                /*address_p=*/false,
11322                                                /*cast_p=*/false,
11323                                                /*template_arg_p=*/true,
11324                                                &idk);
11325       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11326           || !cp_parser_next_token_ends_template_argument_p (parser))
11327         cp_parser_simulate_error (parser);
11328       if (cp_parser_parse_definitely (parser))
11329         return argument;
11330     }
11331
11332   /* If the next token is "&", the argument must be the address of an
11333      object or function with external linkage.  */
11334   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11335   if (address_p)
11336     cp_lexer_consume_token (parser->lexer);
11337   /* See if we might have an id-expression.  */
11338   token = cp_lexer_peek_token (parser->lexer);
11339   if (token->type == CPP_NAME
11340       || token->keyword == RID_OPERATOR
11341       || token->type == CPP_SCOPE
11342       || token->type == CPP_TEMPLATE_ID
11343       || token->type == CPP_NESTED_NAME_SPECIFIER)
11344     {
11345       cp_parser_parse_tentatively (parser);
11346       argument = cp_parser_primary_expression (parser,
11347                                                address_p,
11348                                                /*cast_p=*/false,
11349                                                /*template_arg_p=*/true,
11350                                                &idk);
11351       if (cp_parser_error_occurred (parser)
11352           || !cp_parser_next_token_ends_template_argument_p (parser))
11353         cp_parser_abort_tentative_parse (parser);
11354       else
11355         {
11356           if (TREE_CODE (argument) == INDIRECT_REF)
11357             {
11358               gcc_assert (REFERENCE_REF_P (argument));
11359               argument = TREE_OPERAND (argument, 0);
11360             }
11361
11362           if (TREE_CODE (argument) == VAR_DECL)
11363             {
11364               /* A variable without external linkage might still be a
11365                  valid constant-expression, so no error is issued here
11366                  if the external-linkage check fails.  */
11367               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
11368                 cp_parser_simulate_error (parser);
11369             }
11370           else if (is_overloaded_fn (argument))
11371             /* All overloaded functions are allowed; if the external
11372                linkage test does not pass, an error will be issued
11373                later.  */
11374             ;
11375           else if (address_p
11376                    && (TREE_CODE (argument) == OFFSET_REF
11377                        || TREE_CODE (argument) == SCOPE_REF))
11378             /* A pointer-to-member.  */
11379             ;
11380           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11381             ;
11382           else
11383             cp_parser_simulate_error (parser);
11384
11385           if (cp_parser_parse_definitely (parser))
11386             {
11387               if (address_p)
11388                 argument = build_x_unary_op (ADDR_EXPR, argument,
11389                                              tf_warning_or_error);
11390               return argument;
11391             }
11392         }
11393     }
11394   /* If the argument started with "&", there are no other valid
11395      alternatives at this point.  */
11396   if (address_p)
11397     {
11398       cp_parser_error (parser, "invalid non-type template argument");
11399       return error_mark_node;
11400     }
11401
11402   /* If the argument wasn't successfully parsed as a type-id followed
11403      by '>>', the argument can only be a constant expression now.
11404      Otherwise, we try parsing the constant-expression tentatively,
11405      because the argument could really be a type-id.  */
11406   if (maybe_type_id)
11407     cp_parser_parse_tentatively (parser);
11408   argument = cp_parser_constant_expression (parser,
11409                                             /*allow_non_constant_p=*/false,
11410                                             /*non_constant_p=*/NULL);
11411   argument = fold_non_dependent_expr (argument);
11412   if (!maybe_type_id)
11413     return argument;
11414   if (!cp_parser_next_token_ends_template_argument_p (parser))
11415     cp_parser_error (parser, "expected template-argument");
11416   if (cp_parser_parse_definitely (parser))
11417     return argument;
11418   /* We did our best to parse the argument as a non type-id, but that
11419      was the only alternative that matched (albeit with a '>' after
11420      it). We can assume it's just a typo from the user, and a
11421      diagnostic will then be issued.  */
11422   return cp_parser_template_type_arg (parser);
11423 }
11424
11425 /* Parse an explicit-instantiation.
11426
11427    explicit-instantiation:
11428      template declaration
11429
11430    Although the standard says `declaration', what it really means is:
11431
11432    explicit-instantiation:
11433      template decl-specifier-seq [opt] declarator [opt] ;
11434
11435    Things like `template int S<int>::i = 5, int S<double>::j;' are not
11436    supposed to be allowed.  A defect report has been filed about this
11437    issue.
11438
11439    GNU Extension:
11440
11441    explicit-instantiation:
11442      storage-class-specifier template
11443        decl-specifier-seq [opt] declarator [opt] ;
11444      function-specifier template
11445        decl-specifier-seq [opt] declarator [opt] ;  */
11446
11447 static void
11448 cp_parser_explicit_instantiation (cp_parser* parser)
11449 {
11450   int declares_class_or_enum;
11451   cp_decl_specifier_seq decl_specifiers;
11452   tree extension_specifier = NULL_TREE;
11453   cp_token *token;
11454
11455   /* Look for an (optional) storage-class-specifier or
11456      function-specifier.  */
11457   if (cp_parser_allow_gnu_extensions_p (parser))
11458     {
11459       extension_specifier
11460         = cp_parser_storage_class_specifier_opt (parser);
11461       if (!extension_specifier)
11462         extension_specifier
11463           = cp_parser_function_specifier_opt (parser,
11464                                               /*decl_specs=*/NULL);
11465     }
11466
11467   /* Look for the `template' keyword.  */
11468   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11469   /* Let the front end know that we are processing an explicit
11470      instantiation.  */
11471   begin_explicit_instantiation ();
11472   /* [temp.explicit] says that we are supposed to ignore access
11473      control while processing explicit instantiation directives.  */
11474   push_deferring_access_checks (dk_no_check);
11475   /* Parse a decl-specifier-seq.  */
11476   token = cp_lexer_peek_token (parser->lexer);
11477   cp_parser_decl_specifier_seq (parser,
11478                                 CP_PARSER_FLAGS_OPTIONAL,
11479                                 &decl_specifiers,
11480                                 &declares_class_or_enum);
11481   /* If there was exactly one decl-specifier, and it declared a class,
11482      and there's no declarator, then we have an explicit type
11483      instantiation.  */
11484   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11485     {
11486       tree type;
11487
11488       type = check_tag_decl (&decl_specifiers);
11489       /* Turn access control back on for names used during
11490          template instantiation.  */
11491       pop_deferring_access_checks ();
11492       if (type)
11493         do_type_instantiation (type, extension_specifier,
11494                                /*complain=*/tf_error);
11495     }
11496   else
11497     {
11498       cp_declarator *declarator;
11499       tree decl;
11500
11501       /* Parse the declarator.  */
11502       declarator
11503         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11504                                 /*ctor_dtor_or_conv_p=*/NULL,
11505                                 /*parenthesized_p=*/NULL,
11506                                 /*member_p=*/false);
11507       if (declares_class_or_enum & 2)
11508         cp_parser_check_for_definition_in_return_type (declarator,
11509                                                        decl_specifiers.type,
11510                                                        decl_specifiers.type_location);
11511       if (declarator != cp_error_declarator)
11512         {
11513           decl = grokdeclarator (declarator, &decl_specifiers,
11514                                  NORMAL, 0, &decl_specifiers.attributes);
11515           /* Turn access control back on for names used during
11516              template instantiation.  */
11517           pop_deferring_access_checks ();
11518           /* Do the explicit instantiation.  */
11519           do_decl_instantiation (decl, extension_specifier);
11520         }
11521       else
11522         {
11523           pop_deferring_access_checks ();
11524           /* Skip the body of the explicit instantiation.  */
11525           cp_parser_skip_to_end_of_statement (parser);
11526         }
11527     }
11528   /* We're done with the instantiation.  */
11529   end_explicit_instantiation ();
11530
11531   cp_parser_consume_semicolon_at_end_of_statement (parser);
11532 }
11533
11534 /* Parse an explicit-specialization.
11535
11536    explicit-specialization:
11537      template < > declaration
11538
11539    Although the standard says `declaration', what it really means is:
11540
11541    explicit-specialization:
11542      template <> decl-specifier [opt] init-declarator [opt] ;
11543      template <> function-definition
11544      template <> explicit-specialization
11545      template <> template-declaration  */
11546
11547 static void
11548 cp_parser_explicit_specialization (cp_parser* parser)
11549 {
11550   bool need_lang_pop;
11551   cp_token *token = cp_lexer_peek_token (parser->lexer);
11552
11553   /* Look for the `template' keyword.  */
11554   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11555   /* Look for the `<'.  */
11556   cp_parser_require (parser, CPP_LESS, "%<<%>");
11557   /* Look for the `>'.  */
11558   cp_parser_require (parser, CPP_GREATER, "%<>%>");
11559   /* We have processed another parameter list.  */
11560   ++parser->num_template_parameter_lists;
11561   /* [temp]
11562
11563      A template ... explicit specialization ... shall not have C
11564      linkage.  */
11565   if (current_lang_name == lang_name_c)
11566     {
11567       error_at (token->location, "template specialization with C linkage");
11568       /* Give it C++ linkage to avoid confusing other parts of the
11569          front end.  */
11570       push_lang_context (lang_name_cplusplus);
11571       need_lang_pop = true;
11572     }
11573   else
11574     need_lang_pop = false;
11575   /* Let the front end know that we are beginning a specialization.  */
11576   if (!begin_specialization ())
11577     {
11578       end_specialization ();
11579       return;
11580     }
11581
11582   /* If the next keyword is `template', we need to figure out whether
11583      or not we're looking a template-declaration.  */
11584   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11585     {
11586       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11587           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11588         cp_parser_template_declaration_after_export (parser,
11589                                                      /*member_p=*/false);
11590       else
11591         cp_parser_explicit_specialization (parser);
11592     }
11593   else
11594     /* Parse the dependent declaration.  */
11595     cp_parser_single_declaration (parser,
11596                                   /*checks=*/NULL,
11597                                   /*member_p=*/false,
11598                                   /*explicit_specialization_p=*/true,
11599                                   /*friend_p=*/NULL);
11600   /* We're done with the specialization.  */
11601   end_specialization ();
11602   /* For the erroneous case of a template with C linkage, we pushed an
11603      implicit C++ linkage scope; exit that scope now.  */
11604   if (need_lang_pop)
11605     pop_lang_context ();
11606   /* We're done with this parameter list.  */
11607   --parser->num_template_parameter_lists;
11608 }
11609
11610 /* Parse a type-specifier.
11611
11612    type-specifier:
11613      simple-type-specifier
11614      class-specifier
11615      enum-specifier
11616      elaborated-type-specifier
11617      cv-qualifier
11618
11619    GNU Extension:
11620
11621    type-specifier:
11622      __complex__
11623
11624    Returns a representation of the type-specifier.  For a
11625    class-specifier, enum-specifier, or elaborated-type-specifier, a
11626    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11627
11628    The parser flags FLAGS is used to control type-specifier parsing.
11629
11630    If IS_DECLARATION is TRUE, then this type-specifier is appearing
11631    in a decl-specifier-seq.
11632
11633    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11634    class-specifier, enum-specifier, or elaborated-type-specifier, then
11635    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
11636    if a type is declared; 2 if it is defined.  Otherwise, it is set to
11637    zero.
11638
11639    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11640    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11641    is set to FALSE.  */
11642
11643 static tree
11644 cp_parser_type_specifier (cp_parser* parser,
11645                           cp_parser_flags flags,
11646                           cp_decl_specifier_seq *decl_specs,
11647                           bool is_declaration,
11648                           int* declares_class_or_enum,
11649                           bool* is_cv_qualifier)
11650 {
11651   tree type_spec = NULL_TREE;
11652   cp_token *token;
11653   enum rid keyword;
11654   cp_decl_spec ds = ds_last;
11655
11656   /* Assume this type-specifier does not declare a new type.  */
11657   if (declares_class_or_enum)
11658     *declares_class_or_enum = 0;
11659   /* And that it does not specify a cv-qualifier.  */
11660   if (is_cv_qualifier)
11661     *is_cv_qualifier = false;
11662   /* Peek at the next token.  */
11663   token = cp_lexer_peek_token (parser->lexer);
11664
11665   /* If we're looking at a keyword, we can use that to guide the
11666      production we choose.  */
11667   keyword = token->keyword;
11668   switch (keyword)
11669     {
11670     case RID_ENUM:
11671       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11672         goto elaborated_type_specifier;
11673
11674       /* Look for the enum-specifier.  */
11675       type_spec = cp_parser_enum_specifier (parser);
11676       /* If that worked, we're done.  */
11677       if (type_spec)
11678         {
11679           if (declares_class_or_enum)
11680             *declares_class_or_enum = 2;
11681           if (decl_specs)
11682             cp_parser_set_decl_spec_type (decl_specs,
11683                                           type_spec,
11684                                           token->location,
11685                                           /*user_defined_p=*/true);
11686           return type_spec;
11687         }
11688       else
11689         goto elaborated_type_specifier;
11690
11691       /* Any of these indicate either a class-specifier, or an
11692          elaborated-type-specifier.  */
11693     case RID_CLASS:
11694     case RID_STRUCT:
11695     case RID_UNION:
11696       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11697         goto elaborated_type_specifier;
11698
11699       /* Parse tentatively so that we can back up if we don't find a
11700          class-specifier.  */
11701       cp_parser_parse_tentatively (parser);
11702       /* Look for the class-specifier.  */
11703       type_spec = cp_parser_class_specifier (parser);
11704       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11705       /* If that worked, we're done.  */
11706       if (cp_parser_parse_definitely (parser))
11707         {
11708           if (declares_class_or_enum)
11709             *declares_class_or_enum = 2;
11710           if (decl_specs)
11711             cp_parser_set_decl_spec_type (decl_specs,
11712                                           type_spec,
11713                                           token->location,
11714                                           /*user_defined_p=*/true);
11715           return type_spec;
11716         }
11717
11718       /* Fall through.  */
11719     elaborated_type_specifier:
11720       /* We're declaring (not defining) a class or enum.  */
11721       if (declares_class_or_enum)
11722         *declares_class_or_enum = 1;
11723
11724       /* Fall through.  */
11725     case RID_TYPENAME:
11726       /* Look for an elaborated-type-specifier.  */
11727       type_spec
11728         = (cp_parser_elaborated_type_specifier
11729            (parser,
11730             decl_specs && decl_specs->specs[(int) ds_friend],
11731             is_declaration));
11732       if (decl_specs)
11733         cp_parser_set_decl_spec_type (decl_specs,
11734                                       type_spec,
11735                                       token->location,
11736                                       /*user_defined_p=*/true);
11737       return type_spec;
11738
11739     case RID_CONST:
11740       ds = ds_const;
11741       if (is_cv_qualifier)
11742         *is_cv_qualifier = true;
11743       break;
11744
11745     case RID_VOLATILE:
11746       ds = ds_volatile;
11747       if (is_cv_qualifier)
11748         *is_cv_qualifier = true;
11749       break;
11750
11751     case RID_RESTRICT:
11752       ds = ds_restrict;
11753       if (is_cv_qualifier)
11754         *is_cv_qualifier = true;
11755       break;
11756
11757     case RID_COMPLEX:
11758       /* The `__complex__' keyword is a GNU extension.  */
11759       ds = ds_complex;
11760       break;
11761
11762     default:
11763       break;
11764     }
11765
11766   /* Handle simple keywords.  */
11767   if (ds != ds_last)
11768     {
11769       if (decl_specs)
11770         {
11771           ++decl_specs->specs[(int)ds];
11772           decl_specs->any_specifiers_p = true;
11773         }
11774       return cp_lexer_consume_token (parser->lexer)->u.value;
11775     }
11776
11777   /* If we do not already have a type-specifier, assume we are looking
11778      at a simple-type-specifier.  */
11779   type_spec = cp_parser_simple_type_specifier (parser,
11780                                                decl_specs,
11781                                                flags);
11782
11783   /* If we didn't find a type-specifier, and a type-specifier was not
11784      optional in this context, issue an error message.  */
11785   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11786     {
11787       cp_parser_error (parser, "expected type specifier");
11788       return error_mark_node;
11789     }
11790
11791   return type_spec;
11792 }
11793
11794 /* Parse a simple-type-specifier.
11795
11796    simple-type-specifier:
11797      :: [opt] nested-name-specifier [opt] type-name
11798      :: [opt] nested-name-specifier template template-id
11799      char
11800      wchar_t
11801      bool
11802      short
11803      int
11804      long
11805      signed
11806      unsigned
11807      float
11808      double
11809      void
11810
11811    C++0x Extension:
11812
11813    simple-type-specifier:
11814      auto
11815      decltype ( expression )   
11816      char16_t
11817      char32_t
11818
11819    GNU Extension:
11820
11821    simple-type-specifier:
11822      __typeof__ unary-expression
11823      __typeof__ ( type-id )
11824
11825    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11826    appropriately updated.  */
11827
11828 static tree
11829 cp_parser_simple_type_specifier (cp_parser* parser,
11830                                  cp_decl_specifier_seq *decl_specs,
11831                                  cp_parser_flags flags)
11832 {
11833   tree type = NULL_TREE;
11834   cp_token *token;
11835
11836   /* Peek at the next token.  */
11837   token = cp_lexer_peek_token (parser->lexer);
11838
11839   /* If we're looking at a keyword, things are easy.  */
11840   switch (token->keyword)
11841     {
11842     case RID_CHAR:
11843       if (decl_specs)
11844         decl_specs->explicit_char_p = true;
11845       type = char_type_node;
11846       break;
11847     case RID_CHAR16:
11848       type = char16_type_node;
11849       break;
11850     case RID_CHAR32:
11851       type = char32_type_node;
11852       break;
11853     case RID_WCHAR:
11854       type = wchar_type_node;
11855       break;
11856     case RID_BOOL:
11857       type = boolean_type_node;
11858       break;
11859     case RID_SHORT:
11860       if (decl_specs)
11861         ++decl_specs->specs[(int) ds_short];
11862       type = short_integer_type_node;
11863       break;
11864     case RID_INT:
11865       if (decl_specs)
11866         decl_specs->explicit_int_p = true;
11867       type = integer_type_node;
11868       break;
11869     case RID_LONG:
11870       if (decl_specs)
11871         ++decl_specs->specs[(int) ds_long];
11872       type = long_integer_type_node;
11873       break;
11874     case RID_SIGNED:
11875       if (decl_specs)
11876         ++decl_specs->specs[(int) ds_signed];
11877       type = integer_type_node;
11878       break;
11879     case RID_UNSIGNED:
11880       if (decl_specs)
11881         ++decl_specs->specs[(int) ds_unsigned];
11882       type = unsigned_type_node;
11883       break;
11884     case RID_FLOAT:
11885       type = float_type_node;
11886       break;
11887     case RID_DOUBLE:
11888       type = double_type_node;
11889       break;
11890     case RID_VOID:
11891       type = void_type_node;
11892       break;
11893       
11894     case RID_AUTO:
11895       maybe_warn_cpp0x ("C++0x auto");
11896       type = make_auto ();
11897       break;
11898
11899     case RID_DECLTYPE:
11900       /* Parse the `decltype' type.  */
11901       type = cp_parser_decltype (parser);
11902
11903       if (decl_specs)
11904         cp_parser_set_decl_spec_type (decl_specs, type,
11905                                       token->location,
11906                                       /*user_defined_p=*/true);
11907
11908       return type;
11909
11910     case RID_TYPEOF:
11911       /* Consume the `typeof' token.  */
11912       cp_lexer_consume_token (parser->lexer);
11913       /* Parse the operand to `typeof'.  */
11914       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11915       /* If it is not already a TYPE, take its type.  */
11916       if (!TYPE_P (type))
11917         type = finish_typeof (type);
11918
11919       if (decl_specs)
11920         cp_parser_set_decl_spec_type (decl_specs, type,
11921                                       token->location,
11922                                       /*user_defined_p=*/true);
11923
11924       return type;
11925
11926     default:
11927       break;
11928     }
11929
11930   /* If the type-specifier was for a built-in type, we're done.  */
11931   if (type)
11932     {
11933       tree id;
11934
11935       /* Record the type.  */
11936       if (decl_specs
11937           && (token->keyword != RID_SIGNED
11938               && token->keyword != RID_UNSIGNED
11939               && token->keyword != RID_SHORT
11940               && token->keyword != RID_LONG))
11941         cp_parser_set_decl_spec_type (decl_specs,
11942                                       type,
11943                                       token->location,
11944                                       /*user_defined=*/false);
11945       if (decl_specs)
11946         decl_specs->any_specifiers_p = true;
11947
11948       /* Consume the token.  */
11949       id = cp_lexer_consume_token (parser->lexer)->u.value;
11950
11951       /* There is no valid C++ program where a non-template type is
11952          followed by a "<".  That usually indicates that the user thought
11953          that the type was a template.  */
11954       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11955
11956       return TYPE_NAME (type);
11957     }
11958
11959   /* The type-specifier must be a user-defined type.  */
11960   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11961     {
11962       bool qualified_p;
11963       bool global_p;
11964
11965       /* Don't gobble tokens or issue error messages if this is an
11966          optional type-specifier.  */
11967       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11968         cp_parser_parse_tentatively (parser);
11969
11970       /* Look for the optional `::' operator.  */
11971       global_p
11972         = (cp_parser_global_scope_opt (parser,
11973                                        /*current_scope_valid_p=*/false)
11974            != NULL_TREE);
11975       /* Look for the nested-name specifier.  */
11976       qualified_p
11977         = (cp_parser_nested_name_specifier_opt (parser,
11978                                                 /*typename_keyword_p=*/false,
11979                                                 /*check_dependency_p=*/true,
11980                                                 /*type_p=*/false,
11981                                                 /*is_declaration=*/false)
11982            != NULL_TREE);
11983       token = cp_lexer_peek_token (parser->lexer);
11984       /* If we have seen a nested-name-specifier, and the next token
11985          is `template', then we are using the template-id production.  */
11986       if (parser->scope
11987           && cp_parser_optional_template_keyword (parser))
11988         {
11989           /* Look for the template-id.  */
11990           type = cp_parser_template_id (parser,
11991                                         /*template_keyword_p=*/true,
11992                                         /*check_dependency_p=*/true,
11993                                         /*is_declaration=*/false);
11994           /* If the template-id did not name a type, we are out of
11995              luck.  */
11996           if (TREE_CODE (type) != TYPE_DECL)
11997             {
11998               cp_parser_error (parser, "expected template-id for type");
11999               type = NULL_TREE;
12000             }
12001         }
12002       /* Otherwise, look for a type-name.  */
12003       else
12004         type = cp_parser_type_name (parser);
12005       /* Keep track of all name-lookups performed in class scopes.  */
12006       if (type
12007           && !global_p
12008           && !qualified_p
12009           && TREE_CODE (type) == TYPE_DECL
12010           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12011         maybe_note_name_used_in_class (DECL_NAME (type), type);
12012       /* If it didn't work out, we don't have a TYPE.  */
12013       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12014           && !cp_parser_parse_definitely (parser))
12015         type = NULL_TREE;
12016       if (type && decl_specs)
12017         cp_parser_set_decl_spec_type (decl_specs, type,
12018                                       token->location,
12019                                       /*user_defined=*/true);
12020     }
12021
12022   /* If we didn't get a type-name, issue an error message.  */
12023   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12024     {
12025       cp_parser_error (parser, "expected type-name");
12026       return error_mark_node;
12027     }
12028
12029   /* There is no valid C++ program where a non-template type is
12030      followed by a "<".  That usually indicates that the user thought
12031      that the type was a template.  */
12032   if (type && type != error_mark_node)
12033     {
12034       /* As a last-ditch effort, see if TYPE is an Objective-C type.
12035          If it is, then the '<'...'>' enclose protocol names rather than
12036          template arguments, and so everything is fine.  */
12037       if (c_dialect_objc ()
12038           && (objc_is_id (type) || objc_is_class_name (type)))
12039         {
12040           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12041           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12042
12043           /* Clobber the "unqualified" type previously entered into
12044              DECL_SPECS with the new, improved protocol-qualified version.  */
12045           if (decl_specs)
12046             decl_specs->type = qual_type;
12047
12048           return qual_type;
12049         }
12050
12051       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12052                                                token->location);
12053     }
12054
12055   return type;
12056 }
12057
12058 /* Parse a type-name.
12059
12060    type-name:
12061      class-name
12062      enum-name
12063      typedef-name
12064
12065    enum-name:
12066      identifier
12067
12068    typedef-name:
12069      identifier
12070
12071    Returns a TYPE_DECL for the type.  */
12072
12073 static tree
12074 cp_parser_type_name (cp_parser* parser)
12075 {
12076   tree type_decl;
12077
12078   /* We can't know yet whether it is a class-name or not.  */
12079   cp_parser_parse_tentatively (parser);
12080   /* Try a class-name.  */
12081   type_decl = cp_parser_class_name (parser,
12082                                     /*typename_keyword_p=*/false,
12083                                     /*template_keyword_p=*/false,
12084                                     none_type,
12085                                     /*check_dependency_p=*/true,
12086                                     /*class_head_p=*/false,
12087                                     /*is_declaration=*/false);
12088   /* If it's not a class-name, keep looking.  */
12089   if (!cp_parser_parse_definitely (parser))
12090     {
12091       /* It must be a typedef-name or an enum-name.  */
12092       return cp_parser_nonclass_name (parser);
12093     }
12094
12095   return type_decl;
12096 }
12097
12098 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12099
12100    enum-name:
12101      identifier
12102
12103    typedef-name:
12104      identifier
12105
12106    Returns a TYPE_DECL for the type.  */
12107
12108 static tree
12109 cp_parser_nonclass_name (cp_parser* parser)
12110 {
12111   tree type_decl;
12112   tree identifier;
12113
12114   cp_token *token = cp_lexer_peek_token (parser->lexer);
12115   identifier = cp_parser_identifier (parser);
12116   if (identifier == error_mark_node)
12117     return error_mark_node;
12118
12119   /* Look up the type-name.  */
12120   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12121
12122   if (TREE_CODE (type_decl) != TYPE_DECL
12123       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12124     {
12125       /* See if this is an Objective-C type.  */
12126       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12127       tree type = objc_get_protocol_qualified_type (identifier, protos);
12128       if (type)
12129         type_decl = TYPE_NAME (type);
12130     }
12131   
12132   /* Issue an error if we did not find a type-name.  */
12133   if (TREE_CODE (type_decl) != TYPE_DECL)
12134     {
12135       if (!cp_parser_simulate_error (parser))
12136         cp_parser_name_lookup_error (parser, identifier, type_decl,
12137                                      "is not a type", token->location);
12138       return error_mark_node;
12139     }
12140   /* Remember that the name was used in the definition of the
12141      current class so that we can check later to see if the
12142      meaning would have been different after the class was
12143      entirely defined.  */
12144   else if (type_decl != error_mark_node
12145            && !parser->scope)
12146     maybe_note_name_used_in_class (identifier, type_decl);
12147   
12148   return type_decl;
12149 }
12150
12151 /* Parse an elaborated-type-specifier.  Note that the grammar given
12152    here incorporates the resolution to DR68.
12153
12154    elaborated-type-specifier:
12155      class-key :: [opt] nested-name-specifier [opt] identifier
12156      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12157      enum-key :: [opt] nested-name-specifier [opt] identifier
12158      typename :: [opt] nested-name-specifier identifier
12159      typename :: [opt] nested-name-specifier template [opt]
12160        template-id
12161
12162    GNU extension:
12163
12164    elaborated-type-specifier:
12165      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12166      class-key attributes :: [opt] nested-name-specifier [opt]
12167                template [opt] template-id
12168      enum attributes :: [opt] nested-name-specifier [opt] identifier
12169
12170    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12171    declared `friend'.  If IS_DECLARATION is TRUE, then this
12172    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12173    something is being declared.
12174
12175    Returns the TYPE specified.  */
12176
12177 static tree
12178 cp_parser_elaborated_type_specifier (cp_parser* parser,
12179                                      bool is_friend,
12180                                      bool is_declaration)
12181 {
12182   enum tag_types tag_type;
12183   tree identifier;
12184   tree type = NULL_TREE;
12185   tree attributes = NULL_TREE;
12186   tree globalscope;
12187   cp_token *token = NULL;
12188
12189   /* See if we're looking at the `enum' keyword.  */
12190   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12191     {
12192       /* Consume the `enum' token.  */
12193       cp_lexer_consume_token (parser->lexer);
12194       /* Remember that it's an enumeration type.  */
12195       tag_type = enum_type;
12196       /* Parse the optional `struct' or `class' key (for C++0x scoped
12197          enums).  */
12198       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12199           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12200         {
12201           if (cxx_dialect == cxx98)
12202             maybe_warn_cpp0x ("scoped enums");
12203
12204           /* Consume the `struct' or `class'.  */
12205           cp_lexer_consume_token (parser->lexer);
12206         }
12207       /* Parse the attributes.  */
12208       attributes = cp_parser_attributes_opt (parser);
12209     }
12210   /* Or, it might be `typename'.  */
12211   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12212                                            RID_TYPENAME))
12213     {
12214       /* Consume the `typename' token.  */
12215       cp_lexer_consume_token (parser->lexer);
12216       /* Remember that it's a `typename' type.  */
12217       tag_type = typename_type;
12218     }
12219   /* Otherwise it must be a class-key.  */
12220   else
12221     {
12222       tag_type = cp_parser_class_key (parser);
12223       if (tag_type == none_type)
12224         return error_mark_node;
12225       /* Parse the attributes.  */
12226       attributes = cp_parser_attributes_opt (parser);
12227     }
12228
12229   /* Look for the `::' operator.  */
12230   globalscope =  cp_parser_global_scope_opt (parser,
12231                                              /*current_scope_valid_p=*/false);
12232   /* Look for the nested-name-specifier.  */
12233   if (tag_type == typename_type && !globalscope)
12234     {
12235       if (!cp_parser_nested_name_specifier (parser,
12236                                            /*typename_keyword_p=*/true,
12237                                            /*check_dependency_p=*/true,
12238                                            /*type_p=*/true,
12239                                             is_declaration))
12240         return error_mark_node;
12241     }
12242   else
12243     /* Even though `typename' is not present, the proposed resolution
12244        to Core Issue 180 says that in `class A<T>::B', `B' should be
12245        considered a type-name, even if `A<T>' is dependent.  */
12246     cp_parser_nested_name_specifier_opt (parser,
12247                                          /*typename_keyword_p=*/true,
12248                                          /*check_dependency_p=*/true,
12249                                          /*type_p=*/true,
12250                                          is_declaration);
12251  /* For everything but enumeration types, consider a template-id.
12252     For an enumeration type, consider only a plain identifier.  */
12253   if (tag_type != enum_type)
12254     {
12255       bool template_p = false;
12256       tree decl;
12257
12258       /* Allow the `template' keyword.  */
12259       template_p = cp_parser_optional_template_keyword (parser);
12260       /* If we didn't see `template', we don't know if there's a
12261          template-id or not.  */
12262       if (!template_p)
12263         cp_parser_parse_tentatively (parser);
12264       /* Parse the template-id.  */
12265       token = cp_lexer_peek_token (parser->lexer);
12266       decl = cp_parser_template_id (parser, template_p,
12267                                     /*check_dependency_p=*/true,
12268                                     is_declaration);
12269       /* If we didn't find a template-id, look for an ordinary
12270          identifier.  */
12271       if (!template_p && !cp_parser_parse_definitely (parser))
12272         ;
12273       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12274          in effect, then we must assume that, upon instantiation, the
12275          template will correspond to a class.  */
12276       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12277                && tag_type == typename_type)
12278         type = make_typename_type (parser->scope, decl,
12279                                    typename_type,
12280                                    /*complain=*/tf_error);
12281       /* If the `typename' keyword is in effect and DECL is not a type
12282          decl. Then type is non existant.   */
12283       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12284         type = NULL_TREE; 
12285       else 
12286         type = TREE_TYPE (decl);
12287     }
12288
12289   if (!type)
12290     {
12291       token = cp_lexer_peek_token (parser->lexer);
12292       identifier = cp_parser_identifier (parser);
12293
12294       if (identifier == error_mark_node)
12295         {
12296           parser->scope = NULL_TREE;
12297           return error_mark_node;
12298         }
12299
12300       /* For a `typename', we needn't call xref_tag.  */
12301       if (tag_type == typename_type
12302           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12303         return cp_parser_make_typename_type (parser, parser->scope,
12304                                              identifier,
12305                                              token->location);
12306       /* Look up a qualified name in the usual way.  */
12307       if (parser->scope)
12308         {
12309           tree decl;
12310           tree ambiguous_decls;
12311
12312           decl = cp_parser_lookup_name (parser, identifier,
12313                                         tag_type,
12314                                         /*is_template=*/false,
12315                                         /*is_namespace=*/false,
12316                                         /*check_dependency=*/true,
12317                                         &ambiguous_decls,
12318                                         token->location);
12319
12320           /* If the lookup was ambiguous, an error will already have been
12321              issued.  */
12322           if (ambiguous_decls)
12323             return error_mark_node;
12324
12325           /* If we are parsing friend declaration, DECL may be a
12326              TEMPLATE_DECL tree node here.  However, we need to check
12327              whether this TEMPLATE_DECL results in valid code.  Consider
12328              the following example:
12329
12330                namespace N {
12331                  template <class T> class C {};
12332                }
12333                class X {
12334                  template <class T> friend class N::C; // #1, valid code
12335                };
12336                template <class T> class Y {
12337                  friend class N::C;                    // #2, invalid code
12338                };
12339
12340              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12341              name lookup of `N::C'.  We see that friend declaration must
12342              be template for the code to be valid.  Note that
12343              processing_template_decl does not work here since it is
12344              always 1 for the above two cases.  */
12345
12346           decl = (cp_parser_maybe_treat_template_as_class
12347                   (decl, /*tag_name_p=*/is_friend
12348                          && parser->num_template_parameter_lists));
12349
12350           if (TREE_CODE (decl) != TYPE_DECL)
12351             {
12352               cp_parser_diagnose_invalid_type_name (parser,
12353                                                     parser->scope,
12354                                                     identifier,
12355                                                     token->location);
12356               return error_mark_node;
12357             }
12358
12359           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12360             {
12361               bool allow_template = (parser->num_template_parameter_lists
12362                                       || DECL_SELF_REFERENCE_P (decl));
12363               type = check_elaborated_type_specifier (tag_type, decl, 
12364                                                       allow_template);
12365
12366               if (type == error_mark_node)
12367                 return error_mark_node;
12368             }
12369
12370           /* Forward declarations of nested types, such as
12371
12372                class C1::C2;
12373                class C1::C2::C3;
12374
12375              are invalid unless all components preceding the final '::'
12376              are complete.  If all enclosing types are complete, these
12377              declarations become merely pointless.
12378
12379              Invalid forward declarations of nested types are errors
12380              caught elsewhere in parsing.  Those that are pointless arrive
12381              here.  */
12382
12383           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12384               && !is_friend && !processing_explicit_instantiation)
12385             warning (0, "declaration %qD does not declare anything", decl);
12386
12387           type = TREE_TYPE (decl);
12388         }
12389       else
12390         {
12391           /* An elaborated-type-specifier sometimes introduces a new type and
12392              sometimes names an existing type.  Normally, the rule is that it
12393              introduces a new type only if there is not an existing type of
12394              the same name already in scope.  For example, given:
12395
12396                struct S {};
12397                void f() { struct S s; }
12398
12399              the `struct S' in the body of `f' is the same `struct S' as in
12400              the global scope; the existing definition is used.  However, if
12401              there were no global declaration, this would introduce a new
12402              local class named `S'.
12403
12404              An exception to this rule applies to the following code:
12405
12406                namespace N { struct S; }
12407
12408              Here, the elaborated-type-specifier names a new type
12409              unconditionally; even if there is already an `S' in the
12410              containing scope this declaration names a new type.
12411              This exception only applies if the elaborated-type-specifier
12412              forms the complete declaration:
12413
12414                [class.name]
12415
12416                A declaration consisting solely of `class-key identifier ;' is
12417                either a redeclaration of the name in the current scope or a
12418                forward declaration of the identifier as a class name.  It
12419                introduces the name into the current scope.
12420
12421              We are in this situation precisely when the next token is a `;'.
12422
12423              An exception to the exception is that a `friend' declaration does
12424              *not* name a new type; i.e., given:
12425
12426                struct S { friend struct T; };
12427
12428              `T' is not a new type in the scope of `S'.
12429
12430              Also, `new struct S' or `sizeof (struct S)' never results in the
12431              definition of a new type; a new type can only be declared in a
12432              declaration context.  */
12433
12434           tag_scope ts;
12435           bool template_p;
12436
12437           if (is_friend)
12438             /* Friends have special name lookup rules.  */
12439             ts = ts_within_enclosing_non_class;
12440           else if (is_declaration
12441                    && cp_lexer_next_token_is (parser->lexer,
12442                                               CPP_SEMICOLON))
12443             /* This is a `class-key identifier ;' */
12444             ts = ts_current;
12445           else
12446             ts = ts_global;
12447
12448           template_p =
12449             (parser->num_template_parameter_lists
12450              && (cp_parser_next_token_starts_class_definition_p (parser)
12451                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12452           /* An unqualified name was used to reference this type, so
12453              there were no qualifying templates.  */
12454           if (!cp_parser_check_template_parameters (parser,
12455                                                     /*num_templates=*/0,
12456                                                     token->location,
12457                                                     /*declarator=*/NULL))
12458             return error_mark_node;
12459           type = xref_tag (tag_type, identifier, ts, template_p);
12460         }
12461     }
12462
12463   if (type == error_mark_node)
12464     return error_mark_node;
12465
12466   /* Allow attributes on forward declarations of classes.  */
12467   if (attributes)
12468     {
12469       if (TREE_CODE (type) == TYPENAME_TYPE)
12470         warning (OPT_Wattributes,
12471                  "attributes ignored on uninstantiated type");
12472       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12473                && ! processing_explicit_instantiation)
12474         warning (OPT_Wattributes,
12475                  "attributes ignored on template instantiation");
12476       else if (is_declaration && cp_parser_declares_only_class_p (parser))
12477         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12478       else
12479         warning (OPT_Wattributes,
12480                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12481     }
12482
12483   if (tag_type != enum_type)
12484     cp_parser_check_class_key (tag_type, type);
12485
12486   /* A "<" cannot follow an elaborated type specifier.  If that
12487      happens, the user was probably trying to form a template-id.  */
12488   cp_parser_check_for_invalid_template_id (parser, type, token->location);
12489
12490   return type;
12491 }
12492
12493 /* Parse an enum-specifier.
12494
12495    enum-specifier:
12496      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12497
12498    enum-key:
12499      enum
12500      enum class   [C++0x]
12501      enum struct  [C++0x]
12502
12503    enum-base:   [C++0x]
12504      : type-specifier-seq
12505
12506    GNU Extensions:
12507      enum-key attributes[opt] identifier [opt] enum-base [opt] 
12508        { enumerator-list [opt] }attributes[opt]
12509
12510    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12511    if the token stream isn't an enum-specifier after all.  */
12512
12513 static tree
12514 cp_parser_enum_specifier (cp_parser* parser)
12515 {
12516   tree identifier;
12517   tree type;
12518   tree attributes;
12519   bool scoped_enum_p = false;
12520   bool has_underlying_type = false;
12521   tree underlying_type = NULL_TREE;
12522
12523   /* Parse tentatively so that we can back up if we don't find a
12524      enum-specifier.  */
12525   cp_parser_parse_tentatively (parser);
12526
12527   /* Caller guarantees that the current token is 'enum', an identifier
12528      possibly follows, and the token after that is an opening brace.
12529      If we don't have an identifier, fabricate an anonymous name for
12530      the enumeration being defined.  */
12531   cp_lexer_consume_token (parser->lexer);
12532
12533   /* Parse the "class" or "struct", which indicates a scoped
12534      enumeration type in C++0x.  */
12535   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12536       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12537     {
12538       if (cxx_dialect == cxx98)
12539         maybe_warn_cpp0x ("scoped enums");
12540
12541       /* Consume the `struct' or `class' token.  */
12542       cp_lexer_consume_token (parser->lexer);
12543
12544       scoped_enum_p = true;
12545     }
12546
12547   attributes = cp_parser_attributes_opt (parser);
12548
12549   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12550     identifier = cp_parser_identifier (parser);
12551   else
12552     identifier = make_anon_name ();
12553
12554   /* Check for the `:' that denotes a specified underlying type in C++0x.
12555      Note that a ':' could also indicate a bitfield width, however.  */
12556   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12557     {
12558       cp_decl_specifier_seq type_specifiers;
12559
12560       /* Consume the `:'.  */
12561       cp_lexer_consume_token (parser->lexer);
12562
12563       /* Parse the type-specifier-seq.  */
12564       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12565                                     /*is_trailing_return=*/false,
12566                                     &type_specifiers);
12567
12568       /* At this point this is surely not elaborated type specifier.  */
12569       if (!cp_parser_parse_definitely (parser))
12570         return NULL_TREE;
12571
12572       if (cxx_dialect == cxx98)
12573         maybe_warn_cpp0x ("scoped enums");
12574
12575       has_underlying_type = true;
12576
12577       /* If that didn't work, stop.  */
12578       if (type_specifiers.type != error_mark_node)
12579         {
12580           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12581                                             /*initialized=*/0, NULL);
12582           if (underlying_type == error_mark_node)
12583             underlying_type = NULL_TREE;
12584         }
12585     }
12586
12587   /* Look for the `{' but don't consume it yet.  */
12588   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12589     {
12590       cp_parser_error (parser, "expected %<{%>");
12591       if (has_underlying_type)
12592         return NULL_TREE;
12593     }
12594
12595   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12596     return NULL_TREE;
12597
12598   /* Issue an error message if type-definitions are forbidden here.  */
12599   if (!cp_parser_check_type_definition (parser))
12600     type = error_mark_node;
12601   else
12602     /* Create the new type.  We do this before consuming the opening
12603        brace so the enum will be recorded as being on the line of its
12604        tag (or the 'enum' keyword, if there is no tag).  */
12605     type = start_enum (identifier, underlying_type, scoped_enum_p);
12606   
12607   /* Consume the opening brace.  */
12608   cp_lexer_consume_token (parser->lexer);
12609
12610   if (type == error_mark_node)
12611     {
12612       cp_parser_skip_to_end_of_block_or_statement (parser);
12613       return error_mark_node;
12614     }
12615
12616   /* If the next token is not '}', then there are some enumerators.  */
12617   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12618     cp_parser_enumerator_list (parser, type);
12619
12620   /* Consume the final '}'.  */
12621   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12622
12623   /* Look for trailing attributes to apply to this enumeration, and
12624      apply them if appropriate.  */
12625   if (cp_parser_allow_gnu_extensions_p (parser))
12626     {
12627       tree trailing_attr = cp_parser_attributes_opt (parser);
12628       trailing_attr = chainon (trailing_attr, attributes);
12629       cplus_decl_attributes (&type,
12630                              trailing_attr,
12631                              (int) ATTR_FLAG_TYPE_IN_PLACE);
12632     }
12633
12634   /* Finish up the enumeration.  */
12635   finish_enum (type);
12636
12637   return type;
12638 }
12639
12640 /* Parse an enumerator-list.  The enumerators all have the indicated
12641    TYPE.
12642
12643    enumerator-list:
12644      enumerator-definition
12645      enumerator-list , enumerator-definition  */
12646
12647 static void
12648 cp_parser_enumerator_list (cp_parser* parser, tree type)
12649 {
12650   while (true)
12651     {
12652       /* Parse an enumerator-definition.  */
12653       cp_parser_enumerator_definition (parser, type);
12654
12655       /* If the next token is not a ',', we've reached the end of
12656          the list.  */
12657       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12658         break;
12659       /* Otherwise, consume the `,' and keep going.  */
12660       cp_lexer_consume_token (parser->lexer);
12661       /* If the next token is a `}', there is a trailing comma.  */
12662       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12663         {
12664           if (!in_system_header)
12665             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12666           break;
12667         }
12668     }
12669 }
12670
12671 /* Parse an enumerator-definition.  The enumerator has the indicated
12672    TYPE.
12673
12674    enumerator-definition:
12675      enumerator
12676      enumerator = constant-expression
12677
12678    enumerator:
12679      identifier  */
12680
12681 static void
12682 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12683 {
12684   tree identifier;
12685   tree value;
12686
12687   /* Look for the identifier.  */
12688   identifier = cp_parser_identifier (parser);
12689   if (identifier == error_mark_node)
12690     return;
12691
12692   /* If the next token is an '=', then there is an explicit value.  */
12693   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12694     {
12695       /* Consume the `=' token.  */
12696       cp_lexer_consume_token (parser->lexer);
12697       /* Parse the value.  */
12698       value = cp_parser_constant_expression (parser,
12699                                              /*allow_non_constant_p=*/false,
12700                                              NULL);
12701     }
12702   else
12703     value = NULL_TREE;
12704
12705   /* If we are processing a template, make sure the initializer of the
12706      enumerator doesn't contain any bare template parameter pack.  */
12707   if (check_for_bare_parameter_packs (value))
12708     value = error_mark_node;
12709
12710   /* Create the enumerator.  */
12711   build_enumerator (identifier, value, type);
12712 }
12713
12714 /* Parse a namespace-name.
12715
12716    namespace-name:
12717      original-namespace-name
12718      namespace-alias
12719
12720    Returns the NAMESPACE_DECL for the namespace.  */
12721
12722 static tree
12723 cp_parser_namespace_name (cp_parser* parser)
12724 {
12725   tree identifier;
12726   tree namespace_decl;
12727
12728   cp_token *token = cp_lexer_peek_token (parser->lexer);
12729
12730   /* Get the name of the namespace.  */
12731   identifier = cp_parser_identifier (parser);
12732   if (identifier == error_mark_node)
12733     return error_mark_node;
12734
12735   /* Look up the identifier in the currently active scope.  Look only
12736      for namespaces, due to:
12737
12738        [basic.lookup.udir]
12739
12740        When looking up a namespace-name in a using-directive or alias
12741        definition, only namespace names are considered.
12742
12743      And:
12744
12745        [basic.lookup.qual]
12746
12747        During the lookup of a name preceding the :: scope resolution
12748        operator, object, function, and enumerator names are ignored.
12749
12750      (Note that cp_parser_qualifying_entity only calls this
12751      function if the token after the name is the scope resolution
12752      operator.)  */
12753   namespace_decl = cp_parser_lookup_name (parser, identifier,
12754                                           none_type,
12755                                           /*is_template=*/false,
12756                                           /*is_namespace=*/true,
12757                                           /*check_dependency=*/true,
12758                                           /*ambiguous_decls=*/NULL,
12759                                           token->location);
12760   /* If it's not a namespace, issue an error.  */
12761   if (namespace_decl == error_mark_node
12762       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12763     {
12764       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12765         error_at (token->location, "%qD is not a namespace-name", identifier);
12766       cp_parser_error (parser, "expected namespace-name");
12767       namespace_decl = error_mark_node;
12768     }
12769
12770   return namespace_decl;
12771 }
12772
12773 /* Parse a namespace-definition.
12774
12775    namespace-definition:
12776      named-namespace-definition
12777      unnamed-namespace-definition
12778
12779    named-namespace-definition:
12780      original-namespace-definition
12781      extension-namespace-definition
12782
12783    original-namespace-definition:
12784      namespace identifier { namespace-body }
12785
12786    extension-namespace-definition:
12787      namespace original-namespace-name { namespace-body }
12788
12789    unnamed-namespace-definition:
12790      namespace { namespace-body } */
12791
12792 static void
12793 cp_parser_namespace_definition (cp_parser* parser)
12794 {
12795   tree identifier, attribs;
12796   bool has_visibility;
12797   bool is_inline;
12798
12799   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12800     {
12801       is_inline = true;
12802       cp_lexer_consume_token (parser->lexer);
12803     }
12804   else
12805     is_inline = false;
12806
12807   /* Look for the `namespace' keyword.  */
12808   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12809
12810   /* Get the name of the namespace.  We do not attempt to distinguish
12811      between an original-namespace-definition and an
12812      extension-namespace-definition at this point.  The semantic
12813      analysis routines are responsible for that.  */
12814   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12815     identifier = cp_parser_identifier (parser);
12816   else
12817     identifier = NULL_TREE;
12818
12819   /* Parse any specified attributes.  */
12820   attribs = cp_parser_attributes_opt (parser);
12821
12822   /* Look for the `{' to start the namespace.  */
12823   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12824   /* Start the namespace.  */
12825   push_namespace (identifier);
12826
12827   /* "inline namespace" is equivalent to a stub namespace definition
12828      followed by a strong using directive.  */
12829   if (is_inline)
12830     {
12831       tree name_space = current_namespace;
12832       /* Set up namespace association.  */
12833       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12834         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12835                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12836       /* Import the contents of the inline namespace.  */
12837       pop_namespace ();
12838       do_using_directive (name_space);
12839       push_namespace (identifier);
12840     }
12841
12842   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12843
12844   /* Parse the body of the namespace.  */
12845   cp_parser_namespace_body (parser);
12846
12847 #ifdef HANDLE_PRAGMA_VISIBILITY
12848   if (has_visibility)
12849     pop_visibility ();
12850 #endif
12851
12852   /* Finish the namespace.  */
12853   pop_namespace ();
12854   /* Look for the final `}'.  */
12855   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12856 }
12857
12858 /* Parse a namespace-body.
12859
12860    namespace-body:
12861      declaration-seq [opt]  */
12862
12863 static void
12864 cp_parser_namespace_body (cp_parser* parser)
12865 {
12866   cp_parser_declaration_seq_opt (parser);
12867 }
12868
12869 /* Parse a namespace-alias-definition.
12870
12871    namespace-alias-definition:
12872      namespace identifier = qualified-namespace-specifier ;  */
12873
12874 static void
12875 cp_parser_namespace_alias_definition (cp_parser* parser)
12876 {
12877   tree identifier;
12878   tree namespace_specifier;
12879
12880   cp_token *token = cp_lexer_peek_token (parser->lexer);
12881
12882   /* Look for the `namespace' keyword.  */
12883   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12884   /* Look for the identifier.  */
12885   identifier = cp_parser_identifier (parser);
12886   if (identifier == error_mark_node)
12887     return;
12888   /* Look for the `=' token.  */
12889   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12890       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12891     {
12892       error_at (token->location, "%<namespace%> definition is not allowed here");
12893       /* Skip the definition.  */
12894       cp_lexer_consume_token (parser->lexer);
12895       if (cp_parser_skip_to_closing_brace (parser))
12896         cp_lexer_consume_token (parser->lexer);
12897       return;
12898     }
12899   cp_parser_require (parser, CPP_EQ, "%<=%>");
12900   /* Look for the qualified-namespace-specifier.  */
12901   namespace_specifier
12902     = cp_parser_qualified_namespace_specifier (parser);
12903   /* Look for the `;' token.  */
12904   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12905
12906   /* Register the alias in the symbol table.  */
12907   do_namespace_alias (identifier, namespace_specifier);
12908 }
12909
12910 /* Parse a qualified-namespace-specifier.
12911
12912    qualified-namespace-specifier:
12913      :: [opt] nested-name-specifier [opt] namespace-name
12914
12915    Returns a NAMESPACE_DECL corresponding to the specified
12916    namespace.  */
12917
12918 static tree
12919 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12920 {
12921   /* Look for the optional `::'.  */
12922   cp_parser_global_scope_opt (parser,
12923                               /*current_scope_valid_p=*/false);
12924
12925   /* Look for the optional nested-name-specifier.  */
12926   cp_parser_nested_name_specifier_opt (parser,
12927                                        /*typename_keyword_p=*/false,
12928                                        /*check_dependency_p=*/true,
12929                                        /*type_p=*/false,
12930                                        /*is_declaration=*/true);
12931
12932   return cp_parser_namespace_name (parser);
12933 }
12934
12935 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12936    access declaration.
12937
12938    using-declaration:
12939      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12940      using :: unqualified-id ;  
12941
12942    access-declaration:
12943      qualified-id ;  
12944
12945    */
12946
12947 static bool
12948 cp_parser_using_declaration (cp_parser* parser, 
12949                              bool access_declaration_p)
12950 {
12951   cp_token *token;
12952   bool typename_p = false;
12953   bool global_scope_p;
12954   tree decl;
12955   tree identifier;
12956   tree qscope;
12957
12958   if (access_declaration_p)
12959     cp_parser_parse_tentatively (parser);
12960   else
12961     {
12962       /* Look for the `using' keyword.  */
12963       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12964       
12965       /* Peek at the next token.  */
12966       token = cp_lexer_peek_token (parser->lexer);
12967       /* See if it's `typename'.  */
12968       if (token->keyword == RID_TYPENAME)
12969         {
12970           /* Remember that we've seen it.  */
12971           typename_p = true;
12972           /* Consume the `typename' token.  */
12973           cp_lexer_consume_token (parser->lexer);
12974         }
12975     }
12976
12977   /* Look for the optional global scope qualification.  */
12978   global_scope_p
12979     = (cp_parser_global_scope_opt (parser,
12980                                    /*current_scope_valid_p=*/false)
12981        != NULL_TREE);
12982
12983   /* If we saw `typename', or didn't see `::', then there must be a
12984      nested-name-specifier present.  */
12985   if (typename_p || !global_scope_p)
12986     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12987                                               /*check_dependency_p=*/true,
12988                                               /*type_p=*/false,
12989                                               /*is_declaration=*/true);
12990   /* Otherwise, we could be in either of the two productions.  In that
12991      case, treat the nested-name-specifier as optional.  */
12992   else
12993     qscope = cp_parser_nested_name_specifier_opt (parser,
12994                                                   /*typename_keyword_p=*/false,
12995                                                   /*check_dependency_p=*/true,
12996                                                   /*type_p=*/false,
12997                                                   /*is_declaration=*/true);
12998   if (!qscope)
12999     qscope = global_namespace;
13000
13001   if (access_declaration_p && cp_parser_error_occurred (parser))
13002     /* Something has already gone wrong; there's no need to parse
13003        further.  Since an error has occurred, the return value of
13004        cp_parser_parse_definitely will be false, as required.  */
13005     return cp_parser_parse_definitely (parser);
13006
13007   token = cp_lexer_peek_token (parser->lexer);
13008   /* Parse the unqualified-id.  */
13009   identifier = cp_parser_unqualified_id (parser,
13010                                          /*template_keyword_p=*/false,
13011                                          /*check_dependency_p=*/true,
13012                                          /*declarator_p=*/true,
13013                                          /*optional_p=*/false);
13014
13015   if (access_declaration_p)
13016     {
13017       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13018         cp_parser_simulate_error (parser);
13019       if (!cp_parser_parse_definitely (parser))
13020         return false;
13021     }
13022
13023   /* The function we call to handle a using-declaration is different
13024      depending on what scope we are in.  */
13025   if (qscope == error_mark_node || identifier == error_mark_node)
13026     ;
13027   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13028            && TREE_CODE (identifier) != BIT_NOT_EXPR)
13029     /* [namespace.udecl]
13030
13031        A using declaration shall not name a template-id.  */
13032     error_at (token->location,
13033               "a template-id may not appear in a using-declaration");
13034   else
13035     {
13036       if (at_class_scope_p ())
13037         {
13038           /* Create the USING_DECL.  */
13039           decl = do_class_using_decl (parser->scope, identifier);
13040
13041           if (check_for_bare_parameter_packs (decl))
13042             return false;
13043           else
13044             /* Add it to the list of members in this class.  */
13045             finish_member_declaration (decl);
13046         }
13047       else
13048         {
13049           decl = cp_parser_lookup_name_simple (parser,
13050                                                identifier,
13051                                                token->location);
13052           if (decl == error_mark_node)
13053             cp_parser_name_lookup_error (parser, identifier,
13054                                          decl, NULL,
13055                                          token->location);
13056           else if (check_for_bare_parameter_packs (decl))
13057             return false;
13058           else if (!at_namespace_scope_p ())
13059             do_local_using_decl (decl, qscope, identifier);
13060           else
13061             do_toplevel_using_decl (decl, qscope, identifier);
13062         }
13063     }
13064
13065   /* Look for the final `;'.  */
13066   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13067   
13068   return true;
13069 }
13070
13071 /* Parse a using-directive.
13072
13073    using-directive:
13074      using namespace :: [opt] nested-name-specifier [opt]
13075        namespace-name ;  */
13076
13077 static void
13078 cp_parser_using_directive (cp_parser* parser)
13079 {
13080   tree namespace_decl;
13081   tree attribs;
13082
13083   /* Look for the `using' keyword.  */
13084   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13085   /* And the `namespace' keyword.  */
13086   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13087   /* Look for the optional `::' operator.  */
13088   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13089   /* And the optional nested-name-specifier.  */
13090   cp_parser_nested_name_specifier_opt (parser,
13091                                        /*typename_keyword_p=*/false,
13092                                        /*check_dependency_p=*/true,
13093                                        /*type_p=*/false,
13094                                        /*is_declaration=*/true);
13095   /* Get the namespace being used.  */
13096   namespace_decl = cp_parser_namespace_name (parser);
13097   /* And any specified attributes.  */
13098   attribs = cp_parser_attributes_opt (parser);
13099   /* Update the symbol table.  */
13100   parse_using_directive (namespace_decl, attribs);
13101   /* Look for the final `;'.  */
13102   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13103 }
13104
13105 /* Parse an asm-definition.
13106
13107    asm-definition:
13108      asm ( string-literal ) ;
13109
13110    GNU Extension:
13111
13112    asm-definition:
13113      asm volatile [opt] ( string-literal ) ;
13114      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13115      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13116                           : asm-operand-list [opt] ) ;
13117      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13118                           : asm-operand-list [opt]
13119                           : asm-clobber-list [opt] ) ;
13120      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13121                                : asm-clobber-list [opt]
13122                                : asm-goto-list ) ;  */
13123
13124 static void
13125 cp_parser_asm_definition (cp_parser* parser)
13126 {
13127   tree string;
13128   tree outputs = NULL_TREE;
13129   tree inputs = NULL_TREE;
13130   tree clobbers = NULL_TREE;
13131   tree labels = NULL_TREE;
13132   tree asm_stmt;
13133   bool volatile_p = false;
13134   bool extended_p = false;
13135   bool invalid_inputs_p = false;
13136   bool invalid_outputs_p = false;
13137   bool goto_p = false;
13138   const char *missing = NULL;
13139
13140   /* Look for the `asm' keyword.  */
13141   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13142   /* See if the next token is `volatile'.  */
13143   if (cp_parser_allow_gnu_extensions_p (parser)
13144       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13145     {
13146       /* Remember that we saw the `volatile' keyword.  */
13147       volatile_p = true;
13148       /* Consume the token.  */
13149       cp_lexer_consume_token (parser->lexer);
13150     }
13151   if (cp_parser_allow_gnu_extensions_p (parser)
13152       && parser->in_function_body
13153       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13154     {
13155       /* Remember that we saw the `goto' keyword.  */
13156       goto_p = true;
13157       /* Consume the token.  */
13158       cp_lexer_consume_token (parser->lexer);
13159     }
13160   /* Look for the opening `('.  */
13161   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13162     return;
13163   /* Look for the string.  */
13164   string = cp_parser_string_literal (parser, false, false);
13165   if (string == error_mark_node)
13166     {
13167       cp_parser_skip_to_closing_parenthesis (parser, true, false,
13168                                              /*consume_paren=*/true);
13169       return;
13170     }
13171
13172   /* If we're allowing GNU extensions, check for the extended assembly
13173      syntax.  Unfortunately, the `:' tokens need not be separated by
13174      a space in C, and so, for compatibility, we tolerate that here
13175      too.  Doing that means that we have to treat the `::' operator as
13176      two `:' tokens.  */
13177   if (cp_parser_allow_gnu_extensions_p (parser)
13178       && parser->in_function_body
13179       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13180           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13181     {
13182       bool inputs_p = false;
13183       bool clobbers_p = false;
13184       bool labels_p = false;
13185
13186       /* The extended syntax was used.  */
13187       extended_p = true;
13188
13189       /* Look for outputs.  */
13190       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13191         {
13192           /* Consume the `:'.  */
13193           cp_lexer_consume_token (parser->lexer);
13194           /* Parse the output-operands.  */
13195           if (cp_lexer_next_token_is_not (parser->lexer,
13196                                           CPP_COLON)
13197               && cp_lexer_next_token_is_not (parser->lexer,
13198                                              CPP_SCOPE)
13199               && cp_lexer_next_token_is_not (parser->lexer,
13200                                              CPP_CLOSE_PAREN)
13201               && !goto_p)
13202             outputs = cp_parser_asm_operand_list (parser);
13203
13204             if (outputs == error_mark_node)
13205               invalid_outputs_p = true;
13206         }
13207       /* If the next token is `::', there are no outputs, and the
13208          next token is the beginning of the inputs.  */
13209       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13210         /* The inputs are coming next.  */
13211         inputs_p = true;
13212
13213       /* Look for inputs.  */
13214       if (inputs_p
13215           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13216         {
13217           /* Consume the `:' or `::'.  */
13218           cp_lexer_consume_token (parser->lexer);
13219           /* Parse the output-operands.  */
13220           if (cp_lexer_next_token_is_not (parser->lexer,
13221                                           CPP_COLON)
13222               && cp_lexer_next_token_is_not (parser->lexer,
13223                                              CPP_SCOPE)
13224               && cp_lexer_next_token_is_not (parser->lexer,
13225                                              CPP_CLOSE_PAREN))
13226             inputs = cp_parser_asm_operand_list (parser);
13227
13228             if (inputs == error_mark_node)
13229               invalid_inputs_p = true;
13230         }
13231       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13232         /* The clobbers are coming next.  */
13233         clobbers_p = true;
13234
13235       /* Look for clobbers.  */
13236       if (clobbers_p
13237           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13238         {
13239           clobbers_p = true;
13240           /* Consume the `:' or `::'.  */
13241           cp_lexer_consume_token (parser->lexer);
13242           /* Parse the clobbers.  */
13243           if (cp_lexer_next_token_is_not (parser->lexer,
13244                                           CPP_COLON)
13245               && cp_lexer_next_token_is_not (parser->lexer,
13246                                              CPP_CLOSE_PAREN))
13247             clobbers = cp_parser_asm_clobber_list (parser);
13248         }
13249       else if (goto_p
13250                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13251         /* The labels are coming next.  */
13252         labels_p = true;
13253
13254       /* Look for labels.  */
13255       if (labels_p
13256           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13257         {
13258           labels_p = true;
13259           /* Consume the `:' or `::'.  */
13260           cp_lexer_consume_token (parser->lexer);
13261           /* Parse the labels.  */
13262           labels = cp_parser_asm_label_list (parser);
13263         }
13264
13265       if (goto_p && !labels_p)
13266         missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13267     }
13268   else if (goto_p)
13269     missing = "%<:%> or %<::%>";
13270
13271   /* Look for the closing `)'.  */
13272   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13273                           missing ? missing : "%<)%>"))
13274     cp_parser_skip_to_closing_parenthesis (parser, true, false,
13275                                            /*consume_paren=*/true);
13276   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13277
13278   if (!invalid_inputs_p && !invalid_outputs_p)
13279     {
13280       /* Create the ASM_EXPR.  */
13281       if (parser->in_function_body)
13282         {
13283           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13284                                       inputs, clobbers, labels);
13285           /* If the extended syntax was not used, mark the ASM_EXPR.  */
13286           if (!extended_p)
13287             {
13288               tree temp = asm_stmt;
13289               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13290                 temp = TREE_OPERAND (temp, 0);
13291
13292               ASM_INPUT_P (temp) = 1;
13293             }
13294         }
13295       else
13296         cgraph_add_asm_node (string);
13297     }
13298 }
13299
13300 /* Declarators [gram.dcl.decl] */
13301
13302 /* Parse an init-declarator.
13303
13304    init-declarator:
13305      declarator initializer [opt]
13306
13307    GNU Extension:
13308
13309    init-declarator:
13310      declarator asm-specification [opt] attributes [opt] initializer [opt]
13311
13312    function-definition:
13313      decl-specifier-seq [opt] declarator ctor-initializer [opt]
13314        function-body
13315      decl-specifier-seq [opt] declarator function-try-block
13316
13317    GNU Extension:
13318
13319    function-definition:
13320      __extension__ function-definition
13321
13322    The DECL_SPECIFIERS apply to this declarator.  Returns a
13323    representation of the entity declared.  If MEMBER_P is TRUE, then
13324    this declarator appears in a class scope.  The new DECL created by
13325    this declarator is returned.
13326
13327    The CHECKS are access checks that should be performed once we know
13328    what entity is being declared (and, therefore, what classes have
13329    befriended it).
13330
13331    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13332    for a function-definition here as well.  If the declarator is a
13333    declarator for a function-definition, *FUNCTION_DEFINITION_P will
13334    be TRUE upon return.  By that point, the function-definition will
13335    have been completely parsed.
13336
13337    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13338    is FALSE.  */
13339
13340 static tree
13341 cp_parser_init_declarator (cp_parser* parser,
13342                            cp_decl_specifier_seq *decl_specifiers,
13343                            VEC (deferred_access_check,gc)* checks,
13344                            bool function_definition_allowed_p,
13345                            bool member_p,
13346                            int declares_class_or_enum,
13347                            bool* function_definition_p)
13348 {
13349   cp_token *token = NULL, *asm_spec_start_token = NULL,
13350            *attributes_start_token = NULL;
13351   cp_declarator *declarator;
13352   tree prefix_attributes;
13353   tree attributes;
13354   tree asm_specification;
13355   tree initializer;
13356   tree decl = NULL_TREE;
13357   tree scope;
13358   int is_initialized;
13359   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
13360      initialized with "= ..", CPP_OPEN_PAREN if initialized with
13361      "(...)".  */
13362   enum cpp_ttype initialization_kind;
13363   bool is_direct_init = false;
13364   bool is_non_constant_init;
13365   int ctor_dtor_or_conv_p;
13366   bool friend_p;
13367   tree pushed_scope = NULL;
13368
13369   /* Gather the attributes that were provided with the
13370      decl-specifiers.  */
13371   prefix_attributes = decl_specifiers->attributes;
13372
13373   /* Assume that this is not the declarator for a function
13374      definition.  */
13375   if (function_definition_p)
13376     *function_definition_p = false;
13377
13378   /* Defer access checks while parsing the declarator; we cannot know
13379      what names are accessible until we know what is being
13380      declared.  */
13381   resume_deferring_access_checks ();
13382
13383   /* Parse the declarator.  */
13384   token = cp_lexer_peek_token (parser->lexer);
13385   declarator
13386     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13387                             &ctor_dtor_or_conv_p,
13388                             /*parenthesized_p=*/NULL,
13389                             /*member_p=*/false);
13390   /* Gather up the deferred checks.  */
13391   stop_deferring_access_checks ();
13392
13393   /* If the DECLARATOR was erroneous, there's no need to go
13394      further.  */
13395   if (declarator == cp_error_declarator)
13396     return error_mark_node;
13397
13398   /* Check that the number of template-parameter-lists is OK.  */
13399   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13400                                                        token->location))
13401     return error_mark_node;
13402
13403   if (declares_class_or_enum & 2)
13404     cp_parser_check_for_definition_in_return_type (declarator,
13405                                                    decl_specifiers->type,
13406                                                    decl_specifiers->type_location);
13407
13408   /* Figure out what scope the entity declared by the DECLARATOR is
13409      located in.  `grokdeclarator' sometimes changes the scope, so
13410      we compute it now.  */
13411   scope = get_scope_of_declarator (declarator);
13412
13413   /* If we're allowing GNU extensions, look for an asm-specification
13414      and attributes.  */
13415   if (cp_parser_allow_gnu_extensions_p (parser))
13416     {
13417       /* Look for an asm-specification.  */
13418       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13419       asm_specification = cp_parser_asm_specification_opt (parser);
13420       /* And attributes.  */
13421       attributes_start_token = cp_lexer_peek_token (parser->lexer);
13422       attributes = cp_parser_attributes_opt (parser);
13423     }
13424   else
13425     {
13426       asm_specification = NULL_TREE;
13427       attributes = NULL_TREE;
13428     }
13429
13430   /* Peek at the next token.  */
13431   token = cp_lexer_peek_token (parser->lexer);
13432   /* Check to see if the token indicates the start of a
13433      function-definition.  */
13434   if (function_declarator_p (declarator)
13435       && cp_parser_token_starts_function_definition_p (token))
13436     {
13437       if (!function_definition_allowed_p)
13438         {
13439           /* If a function-definition should not appear here, issue an
13440              error message.  */
13441           cp_parser_error (parser,
13442                            "a function-definition is not allowed here");
13443           return error_mark_node;
13444         }
13445       else
13446         {
13447           location_t func_brace_location
13448             = cp_lexer_peek_token (parser->lexer)->location;
13449
13450           /* Neither attributes nor an asm-specification are allowed
13451              on a function-definition.  */
13452           if (asm_specification)
13453             error_at (asm_spec_start_token->location,
13454                       "an asm-specification is not allowed "
13455                       "on a function-definition");
13456           if (attributes)
13457             error_at (attributes_start_token->location,
13458                       "attributes are not allowed on a function-definition");
13459           /* This is a function-definition.  */
13460           *function_definition_p = true;
13461
13462           /* Parse the function definition.  */
13463           if (member_p)
13464             decl = cp_parser_save_member_function_body (parser,
13465                                                         decl_specifiers,
13466                                                         declarator,
13467                                                         prefix_attributes);
13468           else
13469             decl
13470               = (cp_parser_function_definition_from_specifiers_and_declarator
13471                  (parser, decl_specifiers, prefix_attributes, declarator));
13472
13473           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13474             {
13475               /* This is where the prologue starts...  */
13476               DECL_STRUCT_FUNCTION (decl)->function_start_locus
13477                 = func_brace_location;
13478             }
13479
13480           return decl;
13481         }
13482     }
13483
13484   /* [dcl.dcl]
13485
13486      Only in function declarations for constructors, destructors, and
13487      type conversions can the decl-specifier-seq be omitted.
13488
13489      We explicitly postpone this check past the point where we handle
13490      function-definitions because we tolerate function-definitions
13491      that are missing their return types in some modes.  */
13492   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13493     {
13494       cp_parser_error (parser,
13495                        "expected constructor, destructor, or type conversion");
13496       return error_mark_node;
13497     }
13498
13499   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
13500   if (token->type == CPP_EQ
13501       || token->type == CPP_OPEN_PAREN
13502       || token->type == CPP_OPEN_BRACE)
13503     {
13504       is_initialized = SD_INITIALIZED;
13505       initialization_kind = token->type;
13506
13507       if (token->type == CPP_EQ
13508           && function_declarator_p (declarator))
13509         {
13510           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13511           if (t2->keyword == RID_DEFAULT)
13512             is_initialized = SD_DEFAULTED;
13513           else if (t2->keyword == RID_DELETE)
13514             is_initialized = SD_DELETED;
13515         }
13516     }
13517   else
13518     {
13519       /* If the init-declarator isn't initialized and isn't followed by a
13520          `,' or `;', it's not a valid init-declarator.  */
13521       if (token->type != CPP_COMMA
13522           && token->type != CPP_SEMICOLON)
13523         {
13524           cp_parser_error (parser, "expected initializer");
13525           return error_mark_node;
13526         }
13527       is_initialized = SD_UNINITIALIZED;
13528       initialization_kind = CPP_EOF;
13529     }
13530
13531   /* Because start_decl has side-effects, we should only call it if we
13532      know we're going ahead.  By this point, we know that we cannot
13533      possibly be looking at any other construct.  */
13534   cp_parser_commit_to_tentative_parse (parser);
13535
13536   /* If the decl specifiers were bad, issue an error now that we're
13537      sure this was intended to be a declarator.  Then continue
13538      declaring the variable(s), as int, to try to cut down on further
13539      errors.  */
13540   if (decl_specifiers->any_specifiers_p
13541       && decl_specifiers->type == error_mark_node)
13542     {
13543       cp_parser_error (parser, "invalid type in declaration");
13544       decl_specifiers->type = integer_type_node;
13545     }
13546
13547   /* Check to see whether or not this declaration is a friend.  */
13548   friend_p = cp_parser_friend_p (decl_specifiers);
13549
13550   /* Enter the newly declared entry in the symbol table.  If we're
13551      processing a declaration in a class-specifier, we wait until
13552      after processing the initializer.  */
13553   if (!member_p)
13554     {
13555       if (parser->in_unbraced_linkage_specification_p)
13556         decl_specifiers->storage_class = sc_extern;
13557       decl = start_decl (declarator, decl_specifiers,
13558                          is_initialized, attributes, prefix_attributes,
13559                          &pushed_scope);
13560     }
13561   else if (scope)
13562     /* Enter the SCOPE.  That way unqualified names appearing in the
13563        initializer will be looked up in SCOPE.  */
13564     pushed_scope = push_scope (scope);
13565
13566   /* Perform deferred access control checks, now that we know in which
13567      SCOPE the declared entity resides.  */
13568   if (!member_p && decl)
13569     {
13570       tree saved_current_function_decl = NULL_TREE;
13571
13572       /* If the entity being declared is a function, pretend that we
13573          are in its scope.  If it is a `friend', it may have access to
13574          things that would not otherwise be accessible.  */
13575       if (TREE_CODE (decl) == FUNCTION_DECL)
13576         {
13577           saved_current_function_decl = current_function_decl;
13578           current_function_decl = decl;
13579         }
13580
13581       /* Perform access checks for template parameters.  */
13582       cp_parser_perform_template_parameter_access_checks (checks);
13583
13584       /* Perform the access control checks for the declarator and the
13585          decl-specifiers.  */
13586       perform_deferred_access_checks ();
13587
13588       /* Restore the saved value.  */
13589       if (TREE_CODE (decl) == FUNCTION_DECL)
13590         current_function_decl = saved_current_function_decl;
13591     }
13592
13593   /* Parse the initializer.  */
13594   initializer = NULL_TREE;
13595   is_direct_init = false;
13596   is_non_constant_init = true;
13597   if (is_initialized)
13598     {
13599       if (function_declarator_p (declarator))
13600         {
13601           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13602            if (initialization_kind == CPP_EQ)
13603              initializer = cp_parser_pure_specifier (parser);
13604            else
13605              {
13606                /* If the declaration was erroneous, we don't really
13607                   know what the user intended, so just silently
13608                   consume the initializer.  */
13609                if (decl != error_mark_node)
13610                  error_at (initializer_start_token->location,
13611                            "initializer provided for function");
13612                cp_parser_skip_to_closing_parenthesis (parser,
13613                                                       /*recovering=*/true,
13614                                                       /*or_comma=*/false,
13615                                                       /*consume_paren=*/true);
13616              }
13617         }
13618       else
13619         {
13620           /* We want to record the extra mangling scope for in-class
13621              initializers of class members and initializers of static data
13622              member templates.  The former is a C++0x feature which isn't
13623              implemented yet, and I expect it will involve deferring
13624              parsing of the initializer until end of class as with default
13625              arguments.  So right here we only handle the latter.  */
13626           if (!member_p && processing_template_decl)
13627             start_lambda_scope (decl);
13628           initializer = cp_parser_initializer (parser,
13629                                                &is_direct_init,
13630                                                &is_non_constant_init);
13631           if (!member_p && processing_template_decl)
13632             finish_lambda_scope ();
13633         }
13634     }
13635
13636   /* The old parser allows attributes to appear after a parenthesized
13637      initializer.  Mark Mitchell proposed removing this functionality
13638      on the GCC mailing lists on 2002-08-13.  This parser accepts the
13639      attributes -- but ignores them.  */
13640   if (cp_parser_allow_gnu_extensions_p (parser)
13641       && initialization_kind == CPP_OPEN_PAREN)
13642     if (cp_parser_attributes_opt (parser))
13643       warning (OPT_Wattributes,
13644                "attributes after parenthesized initializer ignored");
13645
13646   /* For an in-class declaration, use `grokfield' to create the
13647      declaration.  */
13648   if (member_p)
13649     {
13650       if (pushed_scope)
13651         {
13652           pop_scope (pushed_scope);
13653           pushed_scope = false;
13654         }
13655       decl = grokfield (declarator, decl_specifiers,
13656                         initializer, !is_non_constant_init,
13657                         /*asmspec=*/NULL_TREE,
13658                         prefix_attributes);
13659       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13660         cp_parser_save_default_args (parser, decl);
13661     }
13662
13663   /* Finish processing the declaration.  But, skip friend
13664      declarations.  */
13665   if (!friend_p && decl && decl != error_mark_node)
13666     {
13667       cp_finish_decl (decl,
13668                       initializer, !is_non_constant_init,
13669                       asm_specification,
13670                       /* If the initializer is in parentheses, then this is
13671                          a direct-initialization, which means that an
13672                          `explicit' constructor is OK.  Otherwise, an
13673                          `explicit' constructor cannot be used.  */
13674                       ((is_direct_init || !is_initialized)
13675                        ? 0 : LOOKUP_ONLYCONVERTING));
13676     }
13677   else if ((cxx_dialect != cxx98) && friend_p
13678            && decl && TREE_CODE (decl) == FUNCTION_DECL)
13679     /* Core issue #226 (C++0x only): A default template-argument
13680        shall not be specified in a friend class template
13681        declaration. */
13682     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
13683                              /*is_partial=*/0, /*is_friend_decl=*/1);
13684
13685   if (!friend_p && pushed_scope)
13686     pop_scope (pushed_scope);
13687
13688   return decl;
13689 }
13690
13691 /* Parse a declarator.
13692
13693    declarator:
13694      direct-declarator
13695      ptr-operator declarator
13696
13697    abstract-declarator:
13698      ptr-operator abstract-declarator [opt]
13699      direct-abstract-declarator
13700
13701    GNU Extensions:
13702
13703    declarator:
13704      attributes [opt] direct-declarator
13705      attributes [opt] ptr-operator declarator
13706
13707    abstract-declarator:
13708      attributes [opt] ptr-operator abstract-declarator [opt]
13709      attributes [opt] direct-abstract-declarator
13710
13711    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13712    detect constructor, destructor or conversion operators. It is set
13713    to -1 if the declarator is a name, and +1 if it is a
13714    function. Otherwise it is set to zero. Usually you just want to
13715    test for >0, but internally the negative value is used.
13716
13717    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13718    a decl-specifier-seq unless it declares a constructor, destructor,
13719    or conversion.  It might seem that we could check this condition in
13720    semantic analysis, rather than parsing, but that makes it difficult
13721    to handle something like `f()'.  We want to notice that there are
13722    no decl-specifiers, and therefore realize that this is an
13723    expression, not a declaration.)
13724
13725    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13726    the declarator is a direct-declarator of the form "(...)".
13727
13728    MEMBER_P is true iff this declarator is a member-declarator.  */
13729
13730 static cp_declarator *
13731 cp_parser_declarator (cp_parser* parser,
13732                       cp_parser_declarator_kind dcl_kind,
13733                       int* ctor_dtor_or_conv_p,
13734                       bool* parenthesized_p,
13735                       bool member_p)
13736 {
13737   cp_token *token;
13738   cp_declarator *declarator;
13739   enum tree_code code;
13740   cp_cv_quals cv_quals;
13741   tree class_type;
13742   tree attributes = NULL_TREE;
13743
13744   /* Assume this is not a constructor, destructor, or type-conversion
13745      operator.  */
13746   if (ctor_dtor_or_conv_p)
13747     *ctor_dtor_or_conv_p = 0;
13748
13749   if (cp_parser_allow_gnu_extensions_p (parser))
13750     attributes = cp_parser_attributes_opt (parser);
13751
13752   /* Peek at the next token.  */
13753   token = cp_lexer_peek_token (parser->lexer);
13754
13755   /* Check for the ptr-operator production.  */
13756   cp_parser_parse_tentatively (parser);
13757   /* Parse the ptr-operator.  */
13758   code = cp_parser_ptr_operator (parser,
13759                                  &class_type,
13760                                  &cv_quals);
13761   /* If that worked, then we have a ptr-operator.  */
13762   if (cp_parser_parse_definitely (parser))
13763     {
13764       /* If a ptr-operator was found, then this declarator was not
13765          parenthesized.  */
13766       if (parenthesized_p)
13767         *parenthesized_p = true;
13768       /* The dependent declarator is optional if we are parsing an
13769          abstract-declarator.  */
13770       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13771         cp_parser_parse_tentatively (parser);
13772
13773       /* Parse the dependent declarator.  */
13774       declarator = cp_parser_declarator (parser, dcl_kind,
13775                                          /*ctor_dtor_or_conv_p=*/NULL,
13776                                          /*parenthesized_p=*/NULL,
13777                                          /*member_p=*/false);
13778
13779       /* If we are parsing an abstract-declarator, we must handle the
13780          case where the dependent declarator is absent.  */
13781       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13782           && !cp_parser_parse_definitely (parser))
13783         declarator = NULL;
13784
13785       declarator = cp_parser_make_indirect_declarator
13786         (code, class_type, cv_quals, declarator);
13787     }
13788   /* Everything else is a direct-declarator.  */
13789   else
13790     {
13791       if (parenthesized_p)
13792         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13793                                                    CPP_OPEN_PAREN);
13794       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13795                                                 ctor_dtor_or_conv_p,
13796                                                 member_p);
13797     }
13798
13799   if (attributes && declarator && declarator != cp_error_declarator)
13800     declarator->attributes = attributes;
13801
13802   return declarator;
13803 }
13804
13805 /* Parse a direct-declarator or direct-abstract-declarator.
13806
13807    direct-declarator:
13808      declarator-id
13809      direct-declarator ( parameter-declaration-clause )
13810        cv-qualifier-seq [opt]
13811        exception-specification [opt]
13812      direct-declarator [ constant-expression [opt] ]
13813      ( declarator )
13814
13815    direct-abstract-declarator:
13816      direct-abstract-declarator [opt]
13817        ( parameter-declaration-clause )
13818        cv-qualifier-seq [opt]
13819        exception-specification [opt]
13820      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13821      ( abstract-declarator )
13822
13823    Returns a representation of the declarator.  DCL_KIND is
13824    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13825    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13826    we are parsing a direct-declarator.  It is
13827    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13828    of ambiguity we prefer an abstract declarator, as per
13829    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13830    cp_parser_declarator.  */
13831
13832 static cp_declarator *
13833 cp_parser_direct_declarator (cp_parser* parser,
13834                              cp_parser_declarator_kind dcl_kind,
13835                              int* ctor_dtor_or_conv_p,
13836                              bool member_p)
13837 {
13838   cp_token *token;
13839   cp_declarator *declarator = NULL;
13840   tree scope = NULL_TREE;
13841   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13842   bool saved_in_declarator_p = parser->in_declarator_p;
13843   bool first = true;
13844   tree pushed_scope = NULL_TREE;
13845
13846   while (true)
13847     {
13848       /* Peek at the next token.  */
13849       token = cp_lexer_peek_token (parser->lexer);
13850       if (token->type == CPP_OPEN_PAREN)
13851         {
13852           /* This is either a parameter-declaration-clause, or a
13853              parenthesized declarator. When we know we are parsing a
13854              named declarator, it must be a parenthesized declarator
13855              if FIRST is true. For instance, `(int)' is a
13856              parameter-declaration-clause, with an omitted
13857              direct-abstract-declarator. But `((*))', is a
13858              parenthesized abstract declarator. Finally, when T is a
13859              template parameter `(T)' is a
13860              parameter-declaration-clause, and not a parenthesized
13861              named declarator.
13862
13863              We first try and parse a parameter-declaration-clause,
13864              and then try a nested declarator (if FIRST is true).
13865
13866              It is not an error for it not to be a
13867              parameter-declaration-clause, even when FIRST is
13868              false. Consider,
13869
13870                int i (int);
13871                int i (3);
13872
13873              The first is the declaration of a function while the
13874              second is the definition of a variable, including its
13875              initializer.
13876
13877              Having seen only the parenthesis, we cannot know which of
13878              these two alternatives should be selected.  Even more
13879              complex are examples like:
13880
13881                int i (int (a));
13882                int i (int (3));
13883
13884              The former is a function-declaration; the latter is a
13885              variable initialization.
13886
13887              Thus again, we try a parameter-declaration-clause, and if
13888              that fails, we back out and return.  */
13889
13890           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13891             {
13892               tree params;
13893               unsigned saved_num_template_parameter_lists;
13894               bool is_declarator = false;
13895               tree t;
13896
13897               /* In a member-declarator, the only valid interpretation
13898                  of a parenthesis is the start of a
13899                  parameter-declaration-clause.  (It is invalid to
13900                  initialize a static data member with a parenthesized
13901                  initializer; only the "=" form of initialization is
13902                  permitted.)  */
13903               if (!member_p)
13904                 cp_parser_parse_tentatively (parser);
13905
13906               /* Consume the `('.  */
13907               cp_lexer_consume_token (parser->lexer);
13908               if (first)
13909                 {
13910                   /* If this is going to be an abstract declarator, we're
13911                      in a declarator and we can't have default args.  */
13912                   parser->default_arg_ok_p = false;
13913                   parser->in_declarator_p = true;
13914                 }
13915
13916               /* Inside the function parameter list, surrounding
13917                  template-parameter-lists do not apply.  */
13918               saved_num_template_parameter_lists
13919                 = parser->num_template_parameter_lists;
13920               parser->num_template_parameter_lists = 0;
13921
13922               begin_scope (sk_function_parms, NULL_TREE);
13923
13924               /* Parse the parameter-declaration-clause.  */
13925               params = cp_parser_parameter_declaration_clause (parser);
13926
13927               parser->num_template_parameter_lists
13928                 = saved_num_template_parameter_lists;
13929
13930               /* If all went well, parse the cv-qualifier-seq and the
13931                  exception-specification.  */
13932               if (member_p || cp_parser_parse_definitely (parser))
13933                 {
13934                   cp_cv_quals cv_quals;
13935                   tree exception_specification;
13936                   tree late_return;
13937
13938                   is_declarator = true;
13939
13940                   if (ctor_dtor_or_conv_p)
13941                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13942                   first = false;
13943                   /* Consume the `)'.  */
13944                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13945
13946                   /* Parse the cv-qualifier-seq.  */
13947                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13948                   /* And the exception-specification.  */
13949                   exception_specification
13950                     = cp_parser_exception_specification_opt (parser);
13951
13952                   late_return
13953                     = cp_parser_late_return_type_opt (parser);
13954
13955                   /* Create the function-declarator.  */
13956                   declarator = make_call_declarator (declarator,
13957                                                      params,
13958                                                      cv_quals,
13959                                                      exception_specification,
13960                                                      late_return);
13961                   /* Any subsequent parameter lists are to do with
13962                      return type, so are not those of the declared
13963                      function.  */
13964                   parser->default_arg_ok_p = false;
13965                 }
13966
13967               /* Remove the function parms from scope.  */
13968               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13969                 pop_binding (DECL_NAME (t), t);
13970               leave_scope();
13971
13972               if (is_declarator)
13973                 /* Repeat the main loop.  */
13974                 continue;
13975             }
13976
13977           /* If this is the first, we can try a parenthesized
13978              declarator.  */
13979           if (first)
13980             {
13981               bool saved_in_type_id_in_expr_p;
13982
13983               parser->default_arg_ok_p = saved_default_arg_ok_p;
13984               parser->in_declarator_p = saved_in_declarator_p;
13985
13986               /* Consume the `('.  */
13987               cp_lexer_consume_token (parser->lexer);
13988               /* Parse the nested declarator.  */
13989               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13990               parser->in_type_id_in_expr_p = true;
13991               declarator
13992                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13993                                         /*parenthesized_p=*/NULL,
13994                                         member_p);
13995               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13996               first = false;
13997               /* Expect a `)'.  */
13998               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13999                 declarator = cp_error_declarator;
14000               if (declarator == cp_error_declarator)
14001                 break;
14002
14003               goto handle_declarator;
14004             }
14005           /* Otherwise, we must be done.  */
14006           else
14007             break;
14008         }
14009       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14010                && token->type == CPP_OPEN_SQUARE)
14011         {
14012           /* Parse an array-declarator.  */
14013           tree bounds;
14014
14015           if (ctor_dtor_or_conv_p)
14016             *ctor_dtor_or_conv_p = 0;
14017
14018           first = false;
14019           parser->default_arg_ok_p = false;
14020           parser->in_declarator_p = true;
14021           /* Consume the `['.  */
14022           cp_lexer_consume_token (parser->lexer);
14023           /* Peek at the next token.  */
14024           token = cp_lexer_peek_token (parser->lexer);
14025           /* If the next token is `]', then there is no
14026              constant-expression.  */
14027           if (token->type != CPP_CLOSE_SQUARE)
14028             {
14029               bool non_constant_p;
14030
14031               bounds
14032                 = cp_parser_constant_expression (parser,
14033                                                  /*allow_non_constant=*/true,
14034                                                  &non_constant_p);
14035               if (!non_constant_p)
14036                 bounds = fold_non_dependent_expr (bounds);
14037               /* Normally, the array bound must be an integral constant
14038                  expression.  However, as an extension, we allow VLAs
14039                  in function scopes.  */
14040               else if (!parser->in_function_body)
14041                 {
14042                   error_at (token->location,
14043                             "array bound is not an integer constant");
14044                   bounds = error_mark_node;
14045                 }
14046               else if (processing_template_decl && !error_operand_p (bounds))
14047                 {
14048                   /* Remember this wasn't a constant-expression.  */
14049                   bounds = build_nop (TREE_TYPE (bounds), bounds);
14050                   TREE_SIDE_EFFECTS (bounds) = 1;
14051                 }
14052             }
14053           else
14054             bounds = NULL_TREE;
14055           /* Look for the closing `]'.  */
14056           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14057             {
14058               declarator = cp_error_declarator;
14059               break;
14060             }
14061
14062           declarator = make_array_declarator (declarator, bounds);
14063         }
14064       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14065         {
14066           {
14067             tree qualifying_scope;
14068             tree unqualified_name;
14069             special_function_kind sfk;
14070             bool abstract_ok;
14071             bool pack_expansion_p = false;
14072             cp_token *declarator_id_start_token;
14073
14074             /* Parse a declarator-id */
14075             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14076             if (abstract_ok)
14077               {
14078                 cp_parser_parse_tentatively (parser);
14079
14080                 /* If we see an ellipsis, we should be looking at a
14081                    parameter pack. */
14082                 if (token->type == CPP_ELLIPSIS)
14083                   {
14084                     /* Consume the `...' */
14085                     cp_lexer_consume_token (parser->lexer);
14086
14087                     pack_expansion_p = true;
14088                   }
14089               }
14090
14091             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14092             unqualified_name
14093               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14094             qualifying_scope = parser->scope;
14095             if (abstract_ok)
14096               {
14097                 bool okay = false;
14098
14099                 if (!unqualified_name && pack_expansion_p)
14100                   {
14101                     /* Check whether an error occurred. */
14102                     okay = !cp_parser_error_occurred (parser);
14103
14104                     /* We already consumed the ellipsis to mark a
14105                        parameter pack, but we have no way to report it,
14106                        so abort the tentative parse. We will be exiting
14107                        immediately anyway. */
14108                     cp_parser_abort_tentative_parse (parser);
14109                   }
14110                 else
14111                   okay = cp_parser_parse_definitely (parser);
14112
14113                 if (!okay)
14114                   unqualified_name = error_mark_node;
14115                 else if (unqualified_name
14116                          && (qualifying_scope
14117                              || (TREE_CODE (unqualified_name)
14118                                  != IDENTIFIER_NODE)))
14119                   {
14120                     cp_parser_error (parser, "expected unqualified-id");
14121                     unqualified_name = error_mark_node;
14122                   }
14123               }
14124
14125             if (!unqualified_name)
14126               return NULL;
14127             if (unqualified_name == error_mark_node)
14128               {
14129                 declarator = cp_error_declarator;
14130                 pack_expansion_p = false;
14131                 declarator->parameter_pack_p = false;
14132                 break;
14133               }
14134
14135             if (qualifying_scope && at_namespace_scope_p ()
14136                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14137               {
14138                 /* In the declaration of a member of a template class
14139                    outside of the class itself, the SCOPE will sometimes
14140                    be a TYPENAME_TYPE.  For example, given:
14141
14142                    template <typename T>
14143                    int S<T>::R::i = 3;
14144
14145                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
14146                    this context, we must resolve S<T>::R to an ordinary
14147                    type, rather than a typename type.
14148
14149                    The reason we normally avoid resolving TYPENAME_TYPEs
14150                    is that a specialization of `S' might render
14151                    `S<T>::R' not a type.  However, if `S' is
14152                    specialized, then this `i' will not be used, so there
14153                    is no harm in resolving the types here.  */
14154                 tree type;
14155
14156                 /* Resolve the TYPENAME_TYPE.  */
14157                 type = resolve_typename_type (qualifying_scope,
14158                                               /*only_current_p=*/false);
14159                 /* If that failed, the declarator is invalid.  */
14160                 if (TREE_CODE (type) == TYPENAME_TYPE)
14161                   error_at (declarator_id_start_token->location,
14162                             "%<%T::%E%> is not a type",
14163                             TYPE_CONTEXT (qualifying_scope),
14164                             TYPE_IDENTIFIER (qualifying_scope));
14165                 qualifying_scope = type;
14166               }
14167
14168             sfk = sfk_none;
14169
14170             if (unqualified_name)
14171               {
14172                 tree class_type;
14173
14174                 if (qualifying_scope
14175                     && CLASS_TYPE_P (qualifying_scope))
14176                   class_type = qualifying_scope;
14177                 else
14178                   class_type = current_class_type;
14179
14180                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14181                   {
14182                     tree name_type = TREE_TYPE (unqualified_name);
14183                     if (class_type && same_type_p (name_type, class_type))
14184                       {
14185                         if (qualifying_scope
14186                             && CLASSTYPE_USE_TEMPLATE (name_type))
14187                           {
14188                             error_at (declarator_id_start_token->location,
14189                                       "invalid use of constructor as a template");
14190                             inform (declarator_id_start_token->location,
14191                                     "use %<%T::%D%> instead of %<%T::%D%> to "
14192                                     "name the constructor in a qualified name",
14193                                     class_type,
14194                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14195                                     class_type, name_type);
14196                             declarator = cp_error_declarator;
14197                             break;
14198                           }
14199                         else
14200                           unqualified_name = constructor_name (class_type);
14201                       }
14202                     else
14203                       {
14204                         /* We do not attempt to print the declarator
14205                            here because we do not have enough
14206                            information about its original syntactic
14207                            form.  */
14208                         cp_parser_error (parser, "invalid declarator");
14209                         declarator = cp_error_declarator;
14210                         break;
14211                       }
14212                   }
14213
14214                 if (class_type)
14215                   {
14216                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14217                       sfk = sfk_destructor;
14218                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14219                       sfk = sfk_conversion;
14220                     else if (/* There's no way to declare a constructor
14221                                 for an anonymous type, even if the type
14222                                 got a name for linkage purposes.  */
14223                              !TYPE_WAS_ANONYMOUS (class_type)
14224                              && constructor_name_p (unqualified_name,
14225                                                     class_type))
14226                       {
14227                         unqualified_name = constructor_name (class_type);
14228                         sfk = sfk_constructor;
14229                       }
14230
14231                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
14232                       *ctor_dtor_or_conv_p = -1;
14233                   }
14234               }
14235             declarator = make_id_declarator (qualifying_scope,
14236                                              unqualified_name,
14237                                              sfk);
14238             declarator->id_loc = token->location;
14239             declarator->parameter_pack_p = pack_expansion_p;
14240
14241             if (pack_expansion_p)
14242               maybe_warn_variadic_templates ();
14243           }
14244
14245         handle_declarator:;
14246           scope = get_scope_of_declarator (declarator);
14247           if (scope)
14248             /* Any names that appear after the declarator-id for a
14249                member are looked up in the containing scope.  */
14250             pushed_scope = push_scope (scope);
14251           parser->in_declarator_p = true;
14252           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14253               || (declarator && declarator->kind == cdk_id))
14254             /* Default args are only allowed on function
14255                declarations.  */
14256             parser->default_arg_ok_p = saved_default_arg_ok_p;
14257           else
14258             parser->default_arg_ok_p = false;
14259
14260           first = false;
14261         }
14262       /* We're done.  */
14263       else
14264         break;
14265     }
14266
14267   /* For an abstract declarator, we might wind up with nothing at this
14268      point.  That's an error; the declarator is not optional.  */
14269   if (!declarator)
14270     cp_parser_error (parser, "expected declarator");
14271
14272   /* If we entered a scope, we must exit it now.  */
14273   if (pushed_scope)
14274     pop_scope (pushed_scope);
14275
14276   parser->default_arg_ok_p = saved_default_arg_ok_p;
14277   parser->in_declarator_p = saved_in_declarator_p;
14278
14279   return declarator;
14280 }
14281
14282 /* Parse a ptr-operator.
14283
14284    ptr-operator:
14285      * cv-qualifier-seq [opt]
14286      &
14287      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14288
14289    GNU Extension:
14290
14291    ptr-operator:
14292      & cv-qualifier-seq [opt]
14293
14294    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14295    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14296    an rvalue reference. In the case of a pointer-to-member, *TYPE is
14297    filled in with the TYPE containing the member.  *CV_QUALS is
14298    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14299    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
14300    Note that the tree codes returned by this function have nothing
14301    to do with the types of trees that will be eventually be created
14302    to represent the pointer or reference type being parsed. They are
14303    just constants with suggestive names. */
14304 static enum tree_code
14305 cp_parser_ptr_operator (cp_parser* parser,
14306                         tree* type,
14307                         cp_cv_quals *cv_quals)
14308 {
14309   enum tree_code code = ERROR_MARK;
14310   cp_token *token;
14311
14312   /* Assume that it's not a pointer-to-member.  */
14313   *type = NULL_TREE;
14314   /* And that there are no cv-qualifiers.  */
14315   *cv_quals = TYPE_UNQUALIFIED;
14316
14317   /* Peek at the next token.  */
14318   token = cp_lexer_peek_token (parser->lexer);
14319
14320   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
14321   if (token->type == CPP_MULT)
14322     code = INDIRECT_REF;
14323   else if (token->type == CPP_AND)
14324     code = ADDR_EXPR;
14325   else if ((cxx_dialect != cxx98) &&
14326            token->type == CPP_AND_AND) /* C++0x only */
14327     code = NON_LVALUE_EXPR;
14328
14329   if (code != ERROR_MARK)
14330     {
14331       /* Consume the `*', `&' or `&&'.  */
14332       cp_lexer_consume_token (parser->lexer);
14333
14334       /* A `*' can be followed by a cv-qualifier-seq, and so can a
14335          `&', if we are allowing GNU extensions.  (The only qualifier
14336          that can legally appear after `&' is `restrict', but that is
14337          enforced during semantic analysis.  */
14338       if (code == INDIRECT_REF
14339           || cp_parser_allow_gnu_extensions_p (parser))
14340         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14341     }
14342   else
14343     {
14344       /* Try the pointer-to-member case.  */
14345       cp_parser_parse_tentatively (parser);
14346       /* Look for the optional `::' operator.  */
14347       cp_parser_global_scope_opt (parser,
14348                                   /*current_scope_valid_p=*/false);
14349       /* Look for the nested-name specifier.  */
14350       token = cp_lexer_peek_token (parser->lexer);
14351       cp_parser_nested_name_specifier (parser,
14352                                        /*typename_keyword_p=*/false,
14353                                        /*check_dependency_p=*/true,
14354                                        /*type_p=*/false,
14355                                        /*is_declaration=*/false);
14356       /* If we found it, and the next token is a `*', then we are
14357          indeed looking at a pointer-to-member operator.  */
14358       if (!cp_parser_error_occurred (parser)
14359           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14360         {
14361           /* Indicate that the `*' operator was used.  */
14362           code = INDIRECT_REF;
14363
14364           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14365             error_at (token->location, "%qD is a namespace", parser->scope);
14366           else
14367             {
14368               /* The type of which the member is a member is given by the
14369                  current SCOPE.  */
14370               *type = parser->scope;
14371               /* The next name will not be qualified.  */
14372               parser->scope = NULL_TREE;
14373               parser->qualifying_scope = NULL_TREE;
14374               parser->object_scope = NULL_TREE;
14375               /* Look for the optional cv-qualifier-seq.  */
14376               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14377             }
14378         }
14379       /* If that didn't work we don't have a ptr-operator.  */
14380       if (!cp_parser_parse_definitely (parser))
14381         cp_parser_error (parser, "expected ptr-operator");
14382     }
14383
14384   return code;
14385 }
14386
14387 /* Parse an (optional) cv-qualifier-seq.
14388
14389    cv-qualifier-seq:
14390      cv-qualifier cv-qualifier-seq [opt]
14391
14392    cv-qualifier:
14393      const
14394      volatile
14395
14396    GNU Extension:
14397
14398    cv-qualifier:
14399      __restrict__
14400
14401    Returns a bitmask representing the cv-qualifiers.  */
14402
14403 static cp_cv_quals
14404 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14405 {
14406   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14407
14408   while (true)
14409     {
14410       cp_token *token;
14411       cp_cv_quals cv_qualifier;
14412
14413       /* Peek at the next token.  */
14414       token = cp_lexer_peek_token (parser->lexer);
14415       /* See if it's a cv-qualifier.  */
14416       switch (token->keyword)
14417         {
14418         case RID_CONST:
14419           cv_qualifier = TYPE_QUAL_CONST;
14420           break;
14421
14422         case RID_VOLATILE:
14423           cv_qualifier = TYPE_QUAL_VOLATILE;
14424           break;
14425
14426         case RID_RESTRICT:
14427           cv_qualifier = TYPE_QUAL_RESTRICT;
14428           break;
14429
14430         default:
14431           cv_qualifier = TYPE_UNQUALIFIED;
14432           break;
14433         }
14434
14435       if (!cv_qualifier)
14436         break;
14437
14438       if (cv_quals & cv_qualifier)
14439         {
14440           error_at (token->location, "duplicate cv-qualifier");
14441           cp_lexer_purge_token (parser->lexer);
14442         }
14443       else
14444         {
14445           cp_lexer_consume_token (parser->lexer);
14446           cv_quals |= cv_qualifier;
14447         }
14448     }
14449
14450   return cv_quals;
14451 }
14452
14453 /* Parse a late-specified return type, if any.  This is not a separate
14454    non-terminal, but part of a function declarator, which looks like
14455
14456    -> trailing-type-specifier-seq abstract-declarator(opt)
14457
14458    Returns the type indicated by the type-id.  */
14459
14460 static tree
14461 cp_parser_late_return_type_opt (cp_parser* parser)
14462 {
14463   cp_token *token;
14464
14465   /* Peek at the next token.  */
14466   token = cp_lexer_peek_token (parser->lexer);
14467   /* A late-specified return type is indicated by an initial '->'. */
14468   if (token->type != CPP_DEREF)
14469     return NULL_TREE;
14470
14471   /* Consume the ->.  */
14472   cp_lexer_consume_token (parser->lexer);
14473
14474   return cp_parser_trailing_type_id (parser);
14475 }
14476
14477 /* Parse a declarator-id.
14478
14479    declarator-id:
14480      id-expression
14481      :: [opt] nested-name-specifier [opt] type-name
14482
14483    In the `id-expression' case, the value returned is as for
14484    cp_parser_id_expression if the id-expression was an unqualified-id.
14485    If the id-expression was a qualified-id, then a SCOPE_REF is
14486    returned.  The first operand is the scope (either a NAMESPACE_DECL
14487    or TREE_TYPE), but the second is still just a representation of an
14488    unqualified-id.  */
14489
14490 static tree
14491 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14492 {
14493   tree id;
14494   /* The expression must be an id-expression.  Assume that qualified
14495      names are the names of types so that:
14496
14497        template <class T>
14498        int S<T>::R::i = 3;
14499
14500      will work; we must treat `S<T>::R' as the name of a type.
14501      Similarly, assume that qualified names are templates, where
14502      required, so that:
14503
14504        template <class T>
14505        int S<T>::R<T>::i = 3;
14506
14507      will work, too.  */
14508   id = cp_parser_id_expression (parser,
14509                                 /*template_keyword_p=*/false,
14510                                 /*check_dependency_p=*/false,
14511                                 /*template_p=*/NULL,
14512                                 /*declarator_p=*/true,
14513                                 optional_p);
14514   if (id && BASELINK_P (id))
14515     id = BASELINK_FUNCTIONS (id);
14516   return id;
14517 }
14518
14519 /* Parse a type-id.
14520
14521    type-id:
14522      type-specifier-seq abstract-declarator [opt]
14523
14524    Returns the TYPE specified.  */
14525
14526 static tree
14527 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14528                      bool is_trailing_return)
14529 {
14530   cp_decl_specifier_seq type_specifier_seq;
14531   cp_declarator *abstract_declarator;
14532
14533   /* Parse the type-specifier-seq.  */
14534   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14535                                 is_trailing_return,
14536                                 &type_specifier_seq);
14537   if (type_specifier_seq.type == error_mark_node)
14538     return error_mark_node;
14539
14540   /* There might or might not be an abstract declarator.  */
14541   cp_parser_parse_tentatively (parser);
14542   /* Look for the declarator.  */
14543   abstract_declarator
14544     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14545                             /*parenthesized_p=*/NULL,
14546                             /*member_p=*/false);
14547   /* Check to see if there really was a declarator.  */
14548   if (!cp_parser_parse_definitely (parser))
14549     abstract_declarator = NULL;
14550
14551   if (type_specifier_seq.type
14552       && type_uses_auto (type_specifier_seq.type))
14553     {
14554       /* A type-id with type 'auto' is only ok if the abstract declarator
14555          is a function declarator with a late-specified return type.  */
14556       if (abstract_declarator
14557           && abstract_declarator->kind == cdk_function
14558           && abstract_declarator->u.function.late_return_type)
14559         /* OK */;
14560       else
14561         {
14562           error ("invalid use of %<auto%>");
14563           return error_mark_node;
14564         }
14565     }
14566   
14567   return groktypename (&type_specifier_seq, abstract_declarator,
14568                        is_template_arg);
14569 }
14570
14571 static tree cp_parser_type_id (cp_parser *parser)
14572 {
14573   return cp_parser_type_id_1 (parser, false, false);
14574 }
14575
14576 static tree cp_parser_template_type_arg (cp_parser *parser)
14577 {
14578   return cp_parser_type_id_1 (parser, true, false);
14579 }
14580
14581 static tree cp_parser_trailing_type_id (cp_parser *parser)
14582 {
14583   return cp_parser_type_id_1 (parser, false, true);
14584 }
14585
14586 /* Parse a type-specifier-seq.
14587
14588    type-specifier-seq:
14589      type-specifier type-specifier-seq [opt]
14590
14591    GNU extension:
14592
14593    type-specifier-seq:
14594      attributes type-specifier-seq [opt]
14595
14596    If IS_CONDITION is true, we are at the start of a "condition",
14597    e.g., we've just seen "if (".
14598
14599    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14600    i.e. we've just seen "->".
14601
14602    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
14603
14604 static void
14605 cp_parser_type_specifier_seq (cp_parser* parser,
14606                               bool is_condition,
14607                               bool is_trailing_return,
14608                               cp_decl_specifier_seq *type_specifier_seq)
14609 {
14610   bool seen_type_specifier = false;
14611   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14612   cp_token *start_token = NULL;
14613
14614   /* Clear the TYPE_SPECIFIER_SEQ.  */
14615   clear_decl_specs (type_specifier_seq);
14616
14617   /* In the context of a trailing return type, enum E { } is an
14618      elaborated-type-specifier followed by a function-body, not an
14619      enum-specifier.  */
14620   if (is_trailing_return)
14621     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14622
14623   /* Parse the type-specifiers and attributes.  */
14624   while (true)
14625     {
14626       tree type_specifier;
14627       bool is_cv_qualifier;
14628
14629       /* Check for attributes first.  */
14630       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14631         {
14632           type_specifier_seq->attributes =
14633             chainon (type_specifier_seq->attributes,
14634                      cp_parser_attributes_opt (parser));
14635           continue;
14636         }
14637
14638       /* record the token of the beginning of the type specifier seq,
14639          for error reporting purposes*/
14640      if (!start_token)
14641        start_token = cp_lexer_peek_token (parser->lexer);
14642
14643       /* Look for the type-specifier.  */
14644       type_specifier = cp_parser_type_specifier (parser,
14645                                                  flags,
14646                                                  type_specifier_seq,
14647                                                  /*is_declaration=*/false,
14648                                                  NULL,
14649                                                  &is_cv_qualifier);
14650       if (!type_specifier)
14651         {
14652           /* If the first type-specifier could not be found, this is not a
14653              type-specifier-seq at all.  */
14654           if (!seen_type_specifier)
14655             {
14656               cp_parser_error (parser, "expected type-specifier");
14657               type_specifier_seq->type = error_mark_node;
14658               return;
14659             }
14660           /* If subsequent type-specifiers could not be found, the
14661              type-specifier-seq is complete.  */
14662           break;
14663         }
14664
14665       seen_type_specifier = true;
14666       /* The standard says that a condition can be:
14667
14668             type-specifier-seq declarator = assignment-expression
14669
14670          However, given:
14671
14672            struct S {};
14673            if (int S = ...)
14674
14675          we should treat the "S" as a declarator, not as a
14676          type-specifier.  The standard doesn't say that explicitly for
14677          type-specifier-seq, but it does say that for
14678          decl-specifier-seq in an ordinary declaration.  Perhaps it
14679          would be clearer just to allow a decl-specifier-seq here, and
14680          then add a semantic restriction that if any decl-specifiers
14681          that are not type-specifiers appear, the program is invalid.  */
14682       if (is_condition && !is_cv_qualifier)
14683         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14684     }
14685
14686   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14687 }
14688
14689 /* Parse a parameter-declaration-clause.
14690
14691    parameter-declaration-clause:
14692      parameter-declaration-list [opt] ... [opt]
14693      parameter-declaration-list , ...
14694
14695    Returns a representation for the parameter declarations.  A return
14696    value of NULL indicates a parameter-declaration-clause consisting
14697    only of an ellipsis.  */
14698
14699 static tree
14700 cp_parser_parameter_declaration_clause (cp_parser* parser)
14701 {
14702   tree parameters;
14703   cp_token *token;
14704   bool ellipsis_p;
14705   bool is_error;
14706
14707   /* Peek at the next token.  */
14708   token = cp_lexer_peek_token (parser->lexer);
14709   /* Check for trivial parameter-declaration-clauses.  */
14710   if (token->type == CPP_ELLIPSIS)
14711     {
14712       /* Consume the `...' token.  */
14713       cp_lexer_consume_token (parser->lexer);
14714       return NULL_TREE;
14715     }
14716   else if (token->type == CPP_CLOSE_PAREN)
14717     /* There are no parameters.  */
14718     {
14719 #ifndef NO_IMPLICIT_EXTERN_C
14720       if (in_system_header && current_class_type == NULL
14721           && current_lang_name == lang_name_c)
14722         return NULL_TREE;
14723       else
14724 #endif
14725         return void_list_node;
14726     }
14727   /* Check for `(void)', too, which is a special case.  */
14728   else if (token->keyword == RID_VOID
14729            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14730                == CPP_CLOSE_PAREN))
14731     {
14732       /* Consume the `void' token.  */
14733       cp_lexer_consume_token (parser->lexer);
14734       /* There are no parameters.  */
14735       return void_list_node;
14736     }
14737
14738   /* Parse the parameter-declaration-list.  */
14739   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14740   /* If a parse error occurred while parsing the
14741      parameter-declaration-list, then the entire
14742      parameter-declaration-clause is erroneous.  */
14743   if (is_error)
14744     return NULL;
14745
14746   /* Peek at the next token.  */
14747   token = cp_lexer_peek_token (parser->lexer);
14748   /* If it's a `,', the clause should terminate with an ellipsis.  */
14749   if (token->type == CPP_COMMA)
14750     {
14751       /* Consume the `,'.  */
14752       cp_lexer_consume_token (parser->lexer);
14753       /* Expect an ellipsis.  */
14754       ellipsis_p
14755         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14756     }
14757   /* It might also be `...' if the optional trailing `,' was
14758      omitted.  */
14759   else if (token->type == CPP_ELLIPSIS)
14760     {
14761       /* Consume the `...' token.  */
14762       cp_lexer_consume_token (parser->lexer);
14763       /* And remember that we saw it.  */
14764       ellipsis_p = true;
14765     }
14766   else
14767     ellipsis_p = false;
14768
14769   /* Finish the parameter list.  */
14770   if (!ellipsis_p)
14771     parameters = chainon (parameters, void_list_node);
14772
14773   return parameters;
14774 }
14775
14776 /* Parse a parameter-declaration-list.
14777
14778    parameter-declaration-list:
14779      parameter-declaration
14780      parameter-declaration-list , parameter-declaration
14781
14782    Returns a representation of the parameter-declaration-list, as for
14783    cp_parser_parameter_declaration_clause.  However, the
14784    `void_list_node' is never appended to the list.  Upon return,
14785    *IS_ERROR will be true iff an error occurred.  */
14786
14787 static tree
14788 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14789 {
14790   tree parameters = NULL_TREE;
14791   tree *tail = &parameters; 
14792   bool saved_in_unbraced_linkage_specification_p;
14793   int index = 0;
14794
14795   /* Assume all will go well.  */
14796   *is_error = false;
14797   /* The special considerations that apply to a function within an
14798      unbraced linkage specifications do not apply to the parameters
14799      to the function.  */
14800   saved_in_unbraced_linkage_specification_p 
14801     = parser->in_unbraced_linkage_specification_p;
14802   parser->in_unbraced_linkage_specification_p = false;
14803
14804   /* Look for more parameters.  */
14805   while (true)
14806     {
14807       cp_parameter_declarator *parameter;
14808       tree decl = error_mark_node;
14809       bool parenthesized_p;
14810       /* Parse the parameter.  */
14811       parameter
14812         = cp_parser_parameter_declaration (parser,
14813                                            /*template_parm_p=*/false,
14814                                            &parenthesized_p);
14815
14816       /* We don't know yet if the enclosing context is deprecated, so wait
14817          and warn in grokparms if appropriate.  */
14818       deprecated_state = DEPRECATED_SUPPRESS;
14819
14820       if (parameter)
14821         decl = grokdeclarator (parameter->declarator,
14822                                &parameter->decl_specifiers,
14823                                PARM,
14824                                parameter->default_argument != NULL_TREE,
14825                                &parameter->decl_specifiers.attributes);
14826
14827       deprecated_state = DEPRECATED_NORMAL;
14828
14829       /* If a parse error occurred parsing the parameter declaration,
14830          then the entire parameter-declaration-list is erroneous.  */
14831       if (decl == error_mark_node)
14832         {
14833           *is_error = true;
14834           parameters = error_mark_node;
14835           break;
14836         }
14837
14838       if (parameter->decl_specifiers.attributes)
14839         cplus_decl_attributes (&decl,
14840                                parameter->decl_specifiers.attributes,
14841                                0);
14842       if (DECL_NAME (decl))
14843         decl = pushdecl (decl);
14844
14845       if (decl != error_mark_node)
14846         {
14847           retrofit_lang_decl (decl);
14848           DECL_PARM_INDEX (decl) = ++index;
14849         }
14850
14851       /* Add the new parameter to the list.  */
14852       *tail = build_tree_list (parameter->default_argument, decl);
14853       tail = &TREE_CHAIN (*tail);
14854
14855       /* Peek at the next token.  */
14856       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14857           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14858           /* These are for Objective-C++ */
14859           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14860           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14861         /* The parameter-declaration-list is complete.  */
14862         break;
14863       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14864         {
14865           cp_token *token;
14866
14867           /* Peek at the next token.  */
14868           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14869           /* If it's an ellipsis, then the list is complete.  */
14870           if (token->type == CPP_ELLIPSIS)
14871             break;
14872           /* Otherwise, there must be more parameters.  Consume the
14873              `,'.  */
14874           cp_lexer_consume_token (parser->lexer);
14875           /* When parsing something like:
14876
14877                 int i(float f, double d)
14878
14879              we can tell after seeing the declaration for "f" that we
14880              are not looking at an initialization of a variable "i",
14881              but rather at the declaration of a function "i".
14882
14883              Due to the fact that the parsing of template arguments
14884              (as specified to a template-id) requires backtracking we
14885              cannot use this technique when inside a template argument
14886              list.  */
14887           if (!parser->in_template_argument_list_p
14888               && !parser->in_type_id_in_expr_p
14889               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14890               /* However, a parameter-declaration of the form
14891                  "foat(f)" (which is a valid declaration of a
14892                  parameter "f") can also be interpreted as an
14893                  expression (the conversion of "f" to "float").  */
14894               && !parenthesized_p)
14895             cp_parser_commit_to_tentative_parse (parser);
14896         }
14897       else
14898         {
14899           cp_parser_error (parser, "expected %<,%> or %<...%>");
14900           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14901             cp_parser_skip_to_closing_parenthesis (parser,
14902                                                    /*recovering=*/true,
14903                                                    /*or_comma=*/false,
14904                                                    /*consume_paren=*/false);
14905           break;
14906         }
14907     }
14908
14909   parser->in_unbraced_linkage_specification_p
14910     = saved_in_unbraced_linkage_specification_p;
14911
14912   return parameters;
14913 }
14914
14915 /* Parse a parameter declaration.
14916
14917    parameter-declaration:
14918      decl-specifier-seq ... [opt] declarator
14919      decl-specifier-seq declarator = assignment-expression
14920      decl-specifier-seq ... [opt] abstract-declarator [opt]
14921      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14922
14923    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14924    declares a template parameter.  (In that case, a non-nested `>'
14925    token encountered during the parsing of the assignment-expression
14926    is not interpreted as a greater-than operator.)
14927
14928    Returns a representation of the parameter, or NULL if an error
14929    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14930    true iff the declarator is of the form "(p)".  */
14931
14932 static cp_parameter_declarator *
14933 cp_parser_parameter_declaration (cp_parser *parser,
14934                                  bool template_parm_p,
14935                                  bool *parenthesized_p)
14936 {
14937   int declares_class_or_enum;
14938   bool greater_than_is_operator_p;
14939   cp_decl_specifier_seq decl_specifiers;
14940   cp_declarator *declarator;
14941   tree default_argument;
14942   cp_token *token = NULL, *declarator_token_start = NULL;
14943   const char *saved_message;
14944
14945   /* In a template parameter, `>' is not an operator.
14946
14947      [temp.param]
14948
14949      When parsing a default template-argument for a non-type
14950      template-parameter, the first non-nested `>' is taken as the end
14951      of the template parameter-list rather than a greater-than
14952      operator.  */
14953   greater_than_is_operator_p = !template_parm_p;
14954
14955   /* Type definitions may not appear in parameter types.  */
14956   saved_message = parser->type_definition_forbidden_message;
14957   parser->type_definition_forbidden_message
14958     = "types may not be defined in parameter types";
14959
14960   /* Parse the declaration-specifiers.  */
14961   cp_parser_decl_specifier_seq (parser,
14962                                 CP_PARSER_FLAGS_NONE,
14963                                 &decl_specifiers,
14964                                 &declares_class_or_enum);
14965   /* If an error occurred, there's no reason to attempt to parse the
14966      rest of the declaration.  */
14967   if (cp_parser_error_occurred (parser))
14968     {
14969       parser->type_definition_forbidden_message = saved_message;
14970       return NULL;
14971     }
14972
14973   /* Peek at the next token.  */
14974   token = cp_lexer_peek_token (parser->lexer);
14975
14976   /* If the next token is a `)', `,', `=', `>', or `...', then there
14977      is no declarator. However, when variadic templates are enabled,
14978      there may be a declarator following `...'.  */
14979   if (token->type == CPP_CLOSE_PAREN
14980       || token->type == CPP_COMMA
14981       || token->type == CPP_EQ
14982       || token->type == CPP_GREATER)
14983     {
14984       declarator = NULL;
14985       if (parenthesized_p)
14986         *parenthesized_p = false;
14987     }
14988   /* Otherwise, there should be a declarator.  */
14989   else
14990     {
14991       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14992       parser->default_arg_ok_p = false;
14993
14994       /* After seeing a decl-specifier-seq, if the next token is not a
14995          "(", there is no possibility that the code is a valid
14996          expression.  Therefore, if parsing tentatively, we commit at
14997          this point.  */
14998       if (!parser->in_template_argument_list_p
14999           /* In an expression context, having seen:
15000
15001                (int((char ...
15002
15003              we cannot be sure whether we are looking at a
15004              function-type (taking a "char" as a parameter) or a cast
15005              of some object of type "char" to "int".  */
15006           && !parser->in_type_id_in_expr_p
15007           && cp_parser_uncommitted_to_tentative_parse_p (parser)
15008           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15009         cp_parser_commit_to_tentative_parse (parser);
15010       /* Parse the declarator.  */
15011       declarator_token_start = token;
15012       declarator = cp_parser_declarator (parser,
15013                                          CP_PARSER_DECLARATOR_EITHER,
15014                                          /*ctor_dtor_or_conv_p=*/NULL,
15015                                          parenthesized_p,
15016                                          /*member_p=*/false);
15017       parser->default_arg_ok_p = saved_default_arg_ok_p;
15018       /* After the declarator, allow more attributes.  */
15019       decl_specifiers.attributes
15020         = chainon (decl_specifiers.attributes,
15021                    cp_parser_attributes_opt (parser));
15022     }
15023
15024   /* If the next token is an ellipsis, and we have not seen a
15025      declarator name, and the type of the declarator contains parameter
15026      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15027      a parameter pack expansion expression. Otherwise, leave the
15028      ellipsis for a C-style variadic function. */
15029   token = cp_lexer_peek_token (parser->lexer);
15030   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15031     {
15032       tree type = decl_specifiers.type;
15033
15034       if (type && DECL_P (type))
15035         type = TREE_TYPE (type);
15036
15037       if (type
15038           && TREE_CODE (type) != TYPE_PACK_EXPANSION
15039           && declarator_can_be_parameter_pack (declarator)
15040           && (!declarator || !declarator->parameter_pack_p)
15041           && uses_parameter_packs (type))
15042         {
15043           /* Consume the `...'. */
15044           cp_lexer_consume_token (parser->lexer);
15045           maybe_warn_variadic_templates ();
15046           
15047           /* Build a pack expansion type */
15048           if (declarator)
15049             declarator->parameter_pack_p = true;
15050           else
15051             decl_specifiers.type = make_pack_expansion (type);
15052         }
15053     }
15054
15055   /* The restriction on defining new types applies only to the type
15056      of the parameter, not to the default argument.  */
15057   parser->type_definition_forbidden_message = saved_message;
15058
15059   /* If the next token is `=', then process a default argument.  */
15060   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15061     {
15062       /* Consume the `='.  */
15063       cp_lexer_consume_token (parser->lexer);
15064
15065       /* If we are defining a class, then the tokens that make up the
15066          default argument must be saved and processed later.  */
15067       if (!template_parm_p && at_class_scope_p ()
15068           && TYPE_BEING_DEFINED (current_class_type)
15069           && !LAMBDA_TYPE_P (current_class_type))
15070         {
15071           unsigned depth = 0;
15072           int maybe_template_id = 0;
15073           cp_token *first_token;
15074           cp_token *token;
15075
15076           /* Add tokens until we have processed the entire default
15077              argument.  We add the range [first_token, token).  */
15078           first_token = cp_lexer_peek_token (parser->lexer);
15079           while (true)
15080             {
15081               bool done = false;
15082
15083               /* Peek at the next token.  */
15084               token = cp_lexer_peek_token (parser->lexer);
15085               /* What we do depends on what token we have.  */
15086               switch (token->type)
15087                 {
15088                   /* In valid code, a default argument must be
15089                      immediately followed by a `,' `)', or `...'.  */
15090                 case CPP_COMMA:
15091                   if (depth == 0 && maybe_template_id)
15092                     {
15093                       /* If we've seen a '<', we might be in a
15094                          template-argument-list.  Until Core issue 325 is
15095                          resolved, we don't know how this situation ought
15096                          to be handled, so try to DTRT.  We check whether
15097                          what comes after the comma is a valid parameter
15098                          declaration list.  If it is, then the comma ends
15099                          the default argument; otherwise the default
15100                          argument continues.  */
15101                       bool error = false;
15102
15103                       /* Set ITALP so cp_parser_parameter_declaration_list
15104                          doesn't decide to commit to this parse.  */
15105                       bool saved_italp = parser->in_template_argument_list_p;
15106                       parser->in_template_argument_list_p = true;
15107
15108                       cp_parser_parse_tentatively (parser);
15109                       cp_lexer_consume_token (parser->lexer);
15110                       cp_parser_parameter_declaration_list (parser, &error);
15111                       if (!cp_parser_error_occurred (parser) && !error)
15112                         done = true;
15113                       cp_parser_abort_tentative_parse (parser);
15114
15115                       parser->in_template_argument_list_p = saved_italp;
15116                       break;
15117                     }
15118                 case CPP_CLOSE_PAREN:
15119                 case CPP_ELLIPSIS:
15120                   /* If we run into a non-nested `;', `}', or `]',
15121                      then the code is invalid -- but the default
15122                      argument is certainly over.  */
15123                 case CPP_SEMICOLON:
15124                 case CPP_CLOSE_BRACE:
15125                 case CPP_CLOSE_SQUARE:
15126                   if (depth == 0)
15127                     done = true;
15128                   /* Update DEPTH, if necessary.  */
15129                   else if (token->type == CPP_CLOSE_PAREN
15130                            || token->type == CPP_CLOSE_BRACE
15131                            || token->type == CPP_CLOSE_SQUARE)
15132                     --depth;
15133                   break;
15134
15135                 case CPP_OPEN_PAREN:
15136                 case CPP_OPEN_SQUARE:
15137                 case CPP_OPEN_BRACE:
15138                   ++depth;
15139                   break;
15140
15141                 case CPP_LESS:
15142                   if (depth == 0)
15143                     /* This might be the comparison operator, or it might
15144                        start a template argument list.  */
15145                     ++maybe_template_id;
15146                   break;
15147
15148                 case CPP_RSHIFT:
15149                   if (cxx_dialect == cxx98)
15150                     break;
15151                   /* Fall through for C++0x, which treats the `>>'
15152                      operator like two `>' tokens in certain
15153                      cases.  */
15154
15155                 case CPP_GREATER:
15156                   if (depth == 0)
15157                     {
15158                       /* This might be an operator, or it might close a
15159                          template argument list.  But if a previous '<'
15160                          started a template argument list, this will have
15161                          closed it, so we can't be in one anymore.  */
15162                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15163                       if (maybe_template_id < 0)
15164                         maybe_template_id = 0;
15165                     }
15166                   break;
15167
15168                   /* If we run out of tokens, issue an error message.  */
15169                 case CPP_EOF:
15170                 case CPP_PRAGMA_EOL:
15171                   error_at (token->location, "file ends in default argument");
15172                   done = true;
15173                   break;
15174
15175                 case CPP_NAME:
15176                 case CPP_SCOPE:
15177                   /* In these cases, we should look for template-ids.
15178                      For example, if the default argument is
15179                      `X<int, double>()', we need to do name lookup to
15180                      figure out whether or not `X' is a template; if
15181                      so, the `,' does not end the default argument.
15182
15183                      That is not yet done.  */
15184                   break;
15185
15186                 default:
15187                   break;
15188                 }
15189
15190               /* If we've reached the end, stop.  */
15191               if (done)
15192                 break;
15193
15194               /* Add the token to the token block.  */
15195               token = cp_lexer_consume_token (parser->lexer);
15196             }
15197
15198           /* Create a DEFAULT_ARG to represent the unparsed default
15199              argument.  */
15200           default_argument = make_node (DEFAULT_ARG);
15201           DEFARG_TOKENS (default_argument)
15202             = cp_token_cache_new (first_token, token);
15203           DEFARG_INSTANTIATIONS (default_argument) = NULL;
15204         }
15205       /* Outside of a class definition, we can just parse the
15206          assignment-expression.  */
15207       else
15208         {
15209           token = cp_lexer_peek_token (parser->lexer);
15210           default_argument 
15211             = cp_parser_default_argument (parser, template_parm_p);
15212         }
15213
15214       if (!parser->default_arg_ok_p)
15215         {
15216           if (flag_permissive)
15217             warning (0, "deprecated use of default argument for parameter of non-function");
15218           else
15219             {
15220               error_at (token->location,
15221                         "default arguments are only "
15222                         "permitted for function parameters");
15223               default_argument = NULL_TREE;
15224             }
15225         }
15226       else if ((declarator && declarator->parameter_pack_p)
15227                || (decl_specifiers.type
15228                    && PACK_EXPANSION_P (decl_specifiers.type)))
15229         {
15230           /* Find the name of the parameter pack.  */     
15231           cp_declarator *id_declarator = declarator;
15232           while (id_declarator && id_declarator->kind != cdk_id)
15233             id_declarator = id_declarator->declarator;
15234           
15235           if (id_declarator && id_declarator->kind == cdk_id)
15236             error_at (declarator_token_start->location,
15237                       template_parm_p 
15238                       ? "template parameter pack %qD"
15239                       " cannot have a default argument"
15240                       : "parameter pack %qD cannot have a default argument",
15241                       id_declarator->u.id.unqualified_name);
15242           else
15243             error_at (declarator_token_start->location,
15244                       template_parm_p 
15245                       ? "template parameter pack cannot have a default argument"
15246                       : "parameter pack cannot have a default argument");
15247           
15248           default_argument = NULL_TREE;
15249         }
15250     }
15251   else
15252     default_argument = NULL_TREE;
15253
15254   return make_parameter_declarator (&decl_specifiers,
15255                                     declarator,
15256                                     default_argument);
15257 }
15258
15259 /* Parse a default argument and return it.
15260
15261    TEMPLATE_PARM_P is true if this is a default argument for a
15262    non-type template parameter.  */
15263 static tree
15264 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15265 {
15266   tree default_argument = NULL_TREE;
15267   bool saved_greater_than_is_operator_p;
15268   bool saved_local_variables_forbidden_p;
15269
15270   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15271      set correctly.  */
15272   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15273   parser->greater_than_is_operator_p = !template_parm_p;
15274   /* Local variable names (and the `this' keyword) may not
15275      appear in a default argument.  */
15276   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15277   parser->local_variables_forbidden_p = true;
15278   /* Parse the assignment-expression.  */
15279   if (template_parm_p)
15280     push_deferring_access_checks (dk_no_deferred);
15281   default_argument
15282     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15283   if (template_parm_p)
15284     pop_deferring_access_checks ();
15285   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15286   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15287
15288   return default_argument;
15289 }
15290
15291 /* Parse a function-body.
15292
15293    function-body:
15294      compound_statement  */
15295
15296 static void
15297 cp_parser_function_body (cp_parser *parser)
15298 {
15299   cp_parser_compound_statement (parser, NULL, false);
15300 }
15301
15302 /* Parse a ctor-initializer-opt followed by a function-body.  Return
15303    true if a ctor-initializer was present.  */
15304
15305 static bool
15306 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15307 {
15308   tree body;
15309   bool ctor_initializer_p;
15310
15311   /* Begin the function body.  */
15312   body = begin_function_body ();
15313   /* Parse the optional ctor-initializer.  */
15314   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15315   /* Parse the function-body.  */
15316   cp_parser_function_body (parser);
15317   /* Finish the function body.  */
15318   finish_function_body (body);
15319
15320   return ctor_initializer_p;
15321 }
15322
15323 /* Parse an initializer.
15324
15325    initializer:
15326      = initializer-clause
15327      ( expression-list )
15328
15329    Returns an expression representing the initializer.  If no
15330    initializer is present, NULL_TREE is returned.
15331
15332    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15333    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
15334    set to TRUE if there is no initializer present.  If there is an
15335    initializer, and it is not a constant-expression, *NON_CONSTANT_P
15336    is set to true; otherwise it is set to false.  */
15337
15338 static tree
15339 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15340                        bool* non_constant_p)
15341 {
15342   cp_token *token;
15343   tree init;
15344
15345   /* Peek at the next token.  */
15346   token = cp_lexer_peek_token (parser->lexer);
15347
15348   /* Let our caller know whether or not this initializer was
15349      parenthesized.  */
15350   *is_direct_init = (token->type != CPP_EQ);
15351   /* Assume that the initializer is constant.  */
15352   *non_constant_p = false;
15353
15354   if (token->type == CPP_EQ)
15355     {
15356       /* Consume the `='.  */
15357       cp_lexer_consume_token (parser->lexer);
15358       /* Parse the initializer-clause.  */
15359       init = cp_parser_initializer_clause (parser, non_constant_p);
15360     }
15361   else if (token->type == CPP_OPEN_PAREN)
15362     {
15363       VEC(tree,gc) *vec;
15364       vec = cp_parser_parenthesized_expression_list (parser, false,
15365                                                      /*cast_p=*/false,
15366                                                      /*allow_expansion_p=*/true,
15367                                                      non_constant_p);
15368       if (vec == NULL)
15369         return error_mark_node;
15370       init = build_tree_list_vec (vec);
15371       release_tree_vector (vec);
15372     }
15373   else if (token->type == CPP_OPEN_BRACE)
15374     {
15375       maybe_warn_cpp0x ("extended initializer lists");
15376       init = cp_parser_braced_list (parser, non_constant_p);
15377       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15378     }
15379   else
15380     {
15381       /* Anything else is an error.  */
15382       cp_parser_error (parser, "expected initializer");
15383       init = error_mark_node;
15384     }
15385
15386   return init;
15387 }
15388
15389 /* Parse an initializer-clause.
15390
15391    initializer-clause:
15392      assignment-expression
15393      braced-init-list
15394
15395    Returns an expression representing the initializer.
15396
15397    If the `assignment-expression' production is used the value
15398    returned is simply a representation for the expression.
15399
15400    Otherwise, calls cp_parser_braced_list.  */
15401
15402 static tree
15403 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15404 {
15405   tree initializer;
15406
15407   /* Assume the expression is constant.  */
15408   *non_constant_p = false;
15409
15410   /* If it is not a `{', then we are looking at an
15411      assignment-expression.  */
15412   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15413     {
15414       initializer
15415         = cp_parser_constant_expression (parser,
15416                                         /*allow_non_constant_p=*/true,
15417                                         non_constant_p);
15418       if (!*non_constant_p)
15419         initializer = fold_non_dependent_expr (initializer);
15420     }
15421   else
15422     initializer = cp_parser_braced_list (parser, non_constant_p);
15423
15424   return initializer;
15425 }
15426
15427 /* Parse a brace-enclosed initializer list.
15428
15429    braced-init-list:
15430      { initializer-list , [opt] }
15431      { }
15432
15433    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
15434    the elements of the initializer-list (or NULL, if the last
15435    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
15436    NULL_TREE.  There is no way to detect whether or not the optional
15437    trailing `,' was provided.  NON_CONSTANT_P is as for
15438    cp_parser_initializer.  */     
15439
15440 static tree
15441 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15442 {
15443   tree initializer;
15444
15445   /* Consume the `{' token.  */
15446   cp_lexer_consume_token (parser->lexer);
15447   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
15448   initializer = make_node (CONSTRUCTOR);
15449   /* If it's not a `}', then there is a non-trivial initializer.  */
15450   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15451     {
15452       /* Parse the initializer list.  */
15453       CONSTRUCTOR_ELTS (initializer)
15454         = cp_parser_initializer_list (parser, non_constant_p);
15455       /* A trailing `,' token is allowed.  */
15456       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15457         cp_lexer_consume_token (parser->lexer);
15458     }
15459   /* Now, there should be a trailing `}'.  */
15460   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15461   TREE_TYPE (initializer) = init_list_type_node;
15462   return initializer;
15463 }
15464
15465 /* Parse an initializer-list.
15466
15467    initializer-list:
15468      initializer-clause ... [opt]
15469      initializer-list , initializer-clause ... [opt]
15470
15471    GNU Extension:
15472
15473    initializer-list:
15474      identifier : initializer-clause
15475      initializer-list, identifier : initializer-clause
15476
15477    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
15478    for the initializer.  If the INDEX of the elt is non-NULL, it is the
15479    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
15480    as for cp_parser_initializer.  */
15481
15482 static VEC(constructor_elt,gc) *
15483 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15484 {
15485   VEC(constructor_elt,gc) *v = NULL;
15486
15487   /* Assume all of the expressions are constant.  */
15488   *non_constant_p = false;
15489
15490   /* Parse the rest of the list.  */
15491   while (true)
15492     {
15493       cp_token *token;
15494       tree identifier;
15495       tree initializer;
15496       bool clause_non_constant_p;
15497
15498       /* If the next token is an identifier and the following one is a
15499          colon, we are looking at the GNU designated-initializer
15500          syntax.  */
15501       if (cp_parser_allow_gnu_extensions_p (parser)
15502           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15503           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15504         {
15505           /* Warn the user that they are using an extension.  */
15506           pedwarn (input_location, OPT_pedantic, 
15507                    "ISO C++ does not allow designated initializers");
15508           /* Consume the identifier.  */
15509           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15510           /* Consume the `:'.  */
15511           cp_lexer_consume_token (parser->lexer);
15512         }
15513       else
15514         identifier = NULL_TREE;
15515
15516       /* Parse the initializer.  */
15517       initializer = cp_parser_initializer_clause (parser,
15518                                                   &clause_non_constant_p);
15519       /* If any clause is non-constant, so is the entire initializer.  */
15520       if (clause_non_constant_p)
15521         *non_constant_p = true;
15522
15523       /* If we have an ellipsis, this is an initializer pack
15524          expansion.  */
15525       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15526         {
15527           /* Consume the `...'.  */
15528           cp_lexer_consume_token (parser->lexer);
15529
15530           /* Turn the initializer into an initializer expansion.  */
15531           initializer = make_pack_expansion (initializer);
15532         }
15533
15534       /* Add it to the vector.  */
15535       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15536
15537       /* If the next token is not a comma, we have reached the end of
15538          the list.  */
15539       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15540         break;
15541
15542       /* Peek at the next token.  */
15543       token = cp_lexer_peek_nth_token (parser->lexer, 2);
15544       /* If the next token is a `}', then we're still done.  An
15545          initializer-clause can have a trailing `,' after the
15546          initializer-list and before the closing `}'.  */
15547       if (token->type == CPP_CLOSE_BRACE)
15548         break;
15549
15550       /* Consume the `,' token.  */
15551       cp_lexer_consume_token (parser->lexer);
15552     }
15553
15554   return v;
15555 }
15556
15557 /* Classes [gram.class] */
15558
15559 /* Parse a class-name.
15560
15561    class-name:
15562      identifier
15563      template-id
15564
15565    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15566    to indicate that names looked up in dependent types should be
15567    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
15568    keyword has been used to indicate that the name that appears next
15569    is a template.  TAG_TYPE indicates the explicit tag given before
15570    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
15571    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
15572    is the class being defined in a class-head.
15573
15574    Returns the TYPE_DECL representing the class.  */
15575
15576 static tree
15577 cp_parser_class_name (cp_parser *parser,
15578                       bool typename_keyword_p,
15579                       bool template_keyword_p,
15580                       enum tag_types tag_type,
15581                       bool check_dependency_p,
15582                       bool class_head_p,
15583                       bool is_declaration)
15584 {
15585   tree decl;
15586   tree scope;
15587   bool typename_p;
15588   cp_token *token;
15589   tree identifier = NULL_TREE;
15590
15591   /* All class-names start with an identifier.  */
15592   token = cp_lexer_peek_token (parser->lexer);
15593   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15594     {
15595       cp_parser_error (parser, "expected class-name");
15596       return error_mark_node;
15597     }
15598
15599   /* PARSER->SCOPE can be cleared when parsing the template-arguments
15600      to a template-id, so we save it here.  */
15601   scope = parser->scope;
15602   if (scope == error_mark_node)
15603     return error_mark_node;
15604
15605   /* Any name names a type if we're following the `typename' keyword
15606      in a qualified name where the enclosing scope is type-dependent.  */
15607   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15608                 && dependent_type_p (scope));
15609   /* Handle the common case (an identifier, but not a template-id)
15610      efficiently.  */
15611   if (token->type == CPP_NAME
15612       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15613     {
15614       cp_token *identifier_token;
15615       bool ambiguous_p;
15616
15617       /* Look for the identifier.  */
15618       identifier_token = cp_lexer_peek_token (parser->lexer);
15619       ambiguous_p = identifier_token->ambiguous_p;
15620       identifier = cp_parser_identifier (parser);
15621       /* If the next token isn't an identifier, we are certainly not
15622          looking at a class-name.  */
15623       if (identifier == error_mark_node)
15624         decl = error_mark_node;
15625       /* If we know this is a type-name, there's no need to look it
15626          up.  */
15627       else if (typename_p)
15628         decl = identifier;
15629       else
15630         {
15631           tree ambiguous_decls;
15632           /* If we already know that this lookup is ambiguous, then
15633              we've already issued an error message; there's no reason
15634              to check again.  */
15635           if (ambiguous_p)
15636             {
15637               cp_parser_simulate_error (parser);
15638               return error_mark_node;
15639             }
15640           /* If the next token is a `::', then the name must be a type
15641              name.
15642
15643              [basic.lookup.qual]
15644
15645              During the lookup for a name preceding the :: scope
15646              resolution operator, object, function, and enumerator
15647              names are ignored.  */
15648           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15649             tag_type = typename_type;
15650           /* Look up the name.  */
15651           decl = cp_parser_lookup_name (parser, identifier,
15652                                         tag_type,
15653                                         /*is_template=*/false,
15654                                         /*is_namespace=*/false,
15655                                         check_dependency_p,
15656                                         &ambiguous_decls,
15657                                         identifier_token->location);
15658           if (ambiguous_decls)
15659             {
15660               error_at (identifier_token->location,
15661                         "reference to %qD is ambiguous", identifier);
15662               print_candidates (ambiguous_decls);
15663               if (cp_parser_parsing_tentatively (parser))
15664                 {
15665                   identifier_token->ambiguous_p = true;
15666                   cp_parser_simulate_error (parser);
15667                 }
15668               return error_mark_node;
15669             }
15670         }
15671     }
15672   else
15673     {
15674       /* Try a template-id.  */
15675       decl = cp_parser_template_id (parser, template_keyword_p,
15676                                     check_dependency_p,
15677                                     is_declaration);
15678       if (decl == error_mark_node)
15679         return error_mark_node;
15680     }
15681
15682   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15683
15684   /* If this is a typename, create a TYPENAME_TYPE.  */
15685   if (typename_p && decl != error_mark_node)
15686     {
15687       decl = make_typename_type (scope, decl, typename_type,
15688                                  /*complain=*/tf_error);
15689       if (decl != error_mark_node)
15690         decl = TYPE_NAME (decl);
15691     }
15692
15693   /* Check to see that it is really the name of a class.  */
15694   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15695       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15696       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15697     /* Situations like this:
15698
15699          template <typename T> struct A {
15700            typename T::template X<int>::I i;
15701          };
15702
15703        are problematic.  Is `T::template X<int>' a class-name?  The
15704        standard does not seem to be definitive, but there is no other
15705        valid interpretation of the following `::'.  Therefore, those
15706        names are considered class-names.  */
15707     {
15708       decl = make_typename_type (scope, decl, tag_type, tf_error);
15709       if (decl != error_mark_node)
15710         decl = TYPE_NAME (decl);
15711     }
15712   else if (TREE_CODE (decl) != TYPE_DECL
15713            || TREE_TYPE (decl) == error_mark_node
15714            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15715     decl = error_mark_node;
15716
15717   if (decl == error_mark_node)
15718     cp_parser_error (parser, "expected class-name");
15719   else if (identifier && !parser->scope)
15720     maybe_note_name_used_in_class (identifier, decl);
15721
15722   return decl;
15723 }
15724
15725 /* Parse a class-specifier.
15726
15727    class-specifier:
15728      class-head { member-specification [opt] }
15729
15730    Returns the TREE_TYPE representing the class.  */
15731
15732 static tree
15733 cp_parser_class_specifier (cp_parser* parser)
15734 {
15735   tree type;
15736   tree attributes = NULL_TREE;
15737   bool nested_name_specifier_p;
15738   unsigned saved_num_template_parameter_lists;
15739   bool saved_in_function_body;
15740   bool saved_in_unbraced_linkage_specification_p;
15741   tree old_scope = NULL_TREE;
15742   tree scope = NULL_TREE;
15743   tree bases;
15744
15745   push_deferring_access_checks (dk_no_deferred);
15746
15747   /* Parse the class-head.  */
15748   type = cp_parser_class_head (parser,
15749                                &nested_name_specifier_p,
15750                                &attributes,
15751                                &bases);
15752   /* If the class-head was a semantic disaster, skip the entire body
15753      of the class.  */
15754   if (!type)
15755     {
15756       cp_parser_skip_to_end_of_block_or_statement (parser);
15757       pop_deferring_access_checks ();
15758       return error_mark_node;
15759     }
15760
15761   /* Look for the `{'.  */
15762   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15763     {
15764       pop_deferring_access_checks ();
15765       return error_mark_node;
15766     }
15767
15768   /* Process the base classes. If they're invalid, skip the 
15769      entire class body.  */
15770   if (!xref_basetypes (type, bases))
15771     {
15772       /* Consuming the closing brace yields better error messages
15773          later on.  */
15774       if (cp_parser_skip_to_closing_brace (parser))
15775         cp_lexer_consume_token (parser->lexer);
15776       pop_deferring_access_checks ();
15777       return error_mark_node;
15778     }
15779
15780   /* Issue an error message if type-definitions are forbidden here.  */
15781   cp_parser_check_type_definition (parser);
15782   /* Remember that we are defining one more class.  */
15783   ++parser->num_classes_being_defined;
15784   /* Inside the class, surrounding template-parameter-lists do not
15785      apply.  */
15786   saved_num_template_parameter_lists
15787     = parser->num_template_parameter_lists;
15788   parser->num_template_parameter_lists = 0;
15789   /* We are not in a function body.  */
15790   saved_in_function_body = parser->in_function_body;
15791   parser->in_function_body = false;
15792   /* We are not immediately inside an extern "lang" block.  */
15793   saved_in_unbraced_linkage_specification_p
15794     = parser->in_unbraced_linkage_specification_p;
15795   parser->in_unbraced_linkage_specification_p = false;
15796
15797   /* Start the class.  */
15798   if (nested_name_specifier_p)
15799     {
15800       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15801       old_scope = push_inner_scope (scope);
15802     }
15803   type = begin_class_definition (type, attributes);
15804
15805   if (type == error_mark_node)
15806     /* If the type is erroneous, skip the entire body of the class.  */
15807     cp_parser_skip_to_closing_brace (parser);
15808   else
15809     /* Parse the member-specification.  */
15810     cp_parser_member_specification_opt (parser);
15811
15812   /* Look for the trailing `}'.  */
15813   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15814   /* Look for trailing attributes to apply to this class.  */
15815   if (cp_parser_allow_gnu_extensions_p (parser))
15816     attributes = cp_parser_attributes_opt (parser);
15817   if (type != error_mark_node)
15818     type = finish_struct (type, attributes);
15819   if (nested_name_specifier_p)
15820     pop_inner_scope (old_scope, scope);
15821   /* If this class is not itself within the scope of another class,
15822      then we need to parse the bodies of all of the queued function
15823      definitions.  Note that the queued functions defined in a class
15824      are not always processed immediately following the
15825      class-specifier for that class.  Consider:
15826
15827        struct A {
15828          struct B { void f() { sizeof (A); } };
15829        };
15830
15831      If `f' were processed before the processing of `A' were
15832      completed, there would be no way to compute the size of `A'.
15833      Note that the nesting we are interested in here is lexical --
15834      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15835      for:
15836
15837        struct A { struct B; };
15838        struct A::B { void f() { } };
15839
15840      there is no need to delay the parsing of `A::B::f'.  */
15841   if (--parser->num_classes_being_defined == 0)
15842     {
15843       tree queue_entry;
15844       tree fn;
15845       tree class_type = NULL_TREE;
15846       tree pushed_scope = NULL_TREE;
15847
15848       /* In a first pass, parse default arguments to the functions.
15849          Then, in a second pass, parse the bodies of the functions.
15850          This two-phased approach handles cases like:
15851
15852             struct S {
15853               void f() { g(); }
15854               void g(int i = 3);
15855             };
15856
15857          */
15858       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15859              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15860            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15861            TREE_PURPOSE (parser->unparsed_functions_queues)
15862              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15863         {
15864           fn = TREE_VALUE (queue_entry);
15865           /* If there are default arguments that have not yet been processed,
15866              take care of them now.  */
15867           if (class_type != TREE_PURPOSE (queue_entry))
15868             {
15869               if (pushed_scope)
15870                 pop_scope (pushed_scope);
15871               class_type = TREE_PURPOSE (queue_entry);
15872               pushed_scope = push_scope (class_type);
15873             }
15874           /* Make sure that any template parameters are in scope.  */
15875           maybe_begin_member_template_processing (fn);
15876           /* Parse the default argument expressions.  */
15877           cp_parser_late_parsing_default_args (parser, fn);
15878           /* Remove any template parameters from the symbol table.  */
15879           maybe_end_member_template_processing ();
15880         }
15881       if (pushed_scope)
15882         pop_scope (pushed_scope);
15883       /* Now parse the body of the functions.  */
15884       for (TREE_VALUE (parser->unparsed_functions_queues)
15885              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15886            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15887            TREE_VALUE (parser->unparsed_functions_queues)
15888              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15889         {
15890           /* Figure out which function we need to process.  */
15891           fn = TREE_VALUE (queue_entry);
15892           /* Parse the function.  */
15893           cp_parser_late_parsing_for_member (parser, fn);
15894         }
15895     }
15896
15897   /* Put back any saved access checks.  */
15898   pop_deferring_access_checks ();
15899
15900   /* Restore saved state.  */
15901   parser->in_function_body = saved_in_function_body;
15902   parser->num_template_parameter_lists
15903     = saved_num_template_parameter_lists;
15904   parser->in_unbraced_linkage_specification_p
15905     = saved_in_unbraced_linkage_specification_p;
15906
15907   return type;
15908 }
15909
15910 /* Parse a class-head.
15911
15912    class-head:
15913      class-key identifier [opt] base-clause [opt]
15914      class-key nested-name-specifier identifier base-clause [opt]
15915      class-key nested-name-specifier [opt] template-id
15916        base-clause [opt]
15917
15918    GNU Extensions:
15919      class-key attributes identifier [opt] base-clause [opt]
15920      class-key attributes nested-name-specifier identifier base-clause [opt]
15921      class-key attributes nested-name-specifier [opt] template-id
15922        base-clause [opt]
15923
15924    Upon return BASES is initialized to the list of base classes (or
15925    NULL, if there are none) in the same form returned by
15926    cp_parser_base_clause.
15927
15928    Returns the TYPE of the indicated class.  Sets
15929    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15930    involving a nested-name-specifier was used, and FALSE otherwise.
15931
15932    Returns error_mark_node if this is not a class-head.
15933
15934    Returns NULL_TREE if the class-head is syntactically valid, but
15935    semantically invalid in a way that means we should skip the entire
15936    body of the class.  */
15937
15938 static tree
15939 cp_parser_class_head (cp_parser* parser,
15940                       bool* nested_name_specifier_p,
15941                       tree *attributes_p,
15942                       tree *bases)
15943 {
15944   tree nested_name_specifier;
15945   enum tag_types class_key;
15946   tree id = NULL_TREE;
15947   tree type = NULL_TREE;
15948   tree attributes;
15949   bool template_id_p = false;
15950   bool qualified_p = false;
15951   bool invalid_nested_name_p = false;
15952   bool invalid_explicit_specialization_p = false;
15953   tree pushed_scope = NULL_TREE;
15954   unsigned num_templates;
15955   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15956   /* Assume no nested-name-specifier will be present.  */
15957   *nested_name_specifier_p = false;
15958   /* Assume no template parameter lists will be used in defining the
15959      type.  */
15960   num_templates = 0;
15961
15962   *bases = NULL_TREE;
15963
15964   /* Look for the class-key.  */
15965   class_key = cp_parser_class_key (parser);
15966   if (class_key == none_type)
15967     return error_mark_node;
15968
15969   /* Parse the attributes.  */
15970   attributes = cp_parser_attributes_opt (parser);
15971
15972   /* If the next token is `::', that is invalid -- but sometimes
15973      people do try to write:
15974
15975        struct ::S {};
15976
15977      Handle this gracefully by accepting the extra qualifier, and then
15978      issuing an error about it later if this really is a
15979      class-head.  If it turns out just to be an elaborated type
15980      specifier, remain silent.  */
15981   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15982     qualified_p = true;
15983
15984   push_deferring_access_checks (dk_no_check);
15985
15986   /* Determine the name of the class.  Begin by looking for an
15987      optional nested-name-specifier.  */
15988   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15989   nested_name_specifier
15990     = cp_parser_nested_name_specifier_opt (parser,
15991                                            /*typename_keyword_p=*/false,
15992                                            /*check_dependency_p=*/false,
15993                                            /*type_p=*/false,
15994                                            /*is_declaration=*/false);
15995   /* If there was a nested-name-specifier, then there *must* be an
15996      identifier.  */
15997   if (nested_name_specifier)
15998     {
15999       type_start_token = cp_lexer_peek_token (parser->lexer);
16000       /* Although the grammar says `identifier', it really means
16001          `class-name' or `template-name'.  You are only allowed to
16002          define a class that has already been declared with this
16003          syntax.
16004
16005          The proposed resolution for Core Issue 180 says that wherever
16006          you see `class T::X' you should treat `X' as a type-name.
16007
16008          It is OK to define an inaccessible class; for example:
16009
16010            class A { class B; };
16011            class A::B {};
16012
16013          We do not know if we will see a class-name, or a
16014          template-name.  We look for a class-name first, in case the
16015          class-name is a template-id; if we looked for the
16016          template-name first we would stop after the template-name.  */
16017       cp_parser_parse_tentatively (parser);
16018       type = cp_parser_class_name (parser,
16019                                    /*typename_keyword_p=*/false,
16020                                    /*template_keyword_p=*/false,
16021                                    class_type,
16022                                    /*check_dependency_p=*/false,
16023                                    /*class_head_p=*/true,
16024                                    /*is_declaration=*/false);
16025       /* If that didn't work, ignore the nested-name-specifier.  */
16026       if (!cp_parser_parse_definitely (parser))
16027         {
16028           invalid_nested_name_p = true;
16029           type_start_token = cp_lexer_peek_token (parser->lexer);
16030           id = cp_parser_identifier (parser);
16031           if (id == error_mark_node)
16032             id = NULL_TREE;
16033         }
16034       /* If we could not find a corresponding TYPE, treat this
16035          declaration like an unqualified declaration.  */
16036       if (type == error_mark_node)
16037         nested_name_specifier = NULL_TREE;
16038       /* Otherwise, count the number of templates used in TYPE and its
16039          containing scopes.  */
16040       else
16041         {
16042           tree scope;
16043
16044           for (scope = TREE_TYPE (type);
16045                scope && TREE_CODE (scope) != NAMESPACE_DECL;
16046                scope = (TYPE_P (scope)
16047                         ? TYPE_CONTEXT (scope)
16048                         : DECL_CONTEXT (scope)))
16049             if (TYPE_P (scope)
16050                 && CLASS_TYPE_P (scope)
16051                 && CLASSTYPE_TEMPLATE_INFO (scope)
16052                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16053                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16054               ++num_templates;
16055         }
16056     }
16057   /* Otherwise, the identifier is optional.  */
16058   else
16059     {
16060       /* We don't know whether what comes next is a template-id,
16061          an identifier, or nothing at all.  */
16062       cp_parser_parse_tentatively (parser);
16063       /* Check for a template-id.  */
16064       type_start_token = cp_lexer_peek_token (parser->lexer);
16065       id = cp_parser_template_id (parser,
16066                                   /*template_keyword_p=*/false,
16067                                   /*check_dependency_p=*/true,
16068                                   /*is_declaration=*/true);
16069       /* If that didn't work, it could still be an identifier.  */
16070       if (!cp_parser_parse_definitely (parser))
16071         {
16072           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16073             {
16074               type_start_token = cp_lexer_peek_token (parser->lexer);
16075               id = cp_parser_identifier (parser);
16076             }
16077           else
16078             id = NULL_TREE;
16079         }
16080       else
16081         {
16082           template_id_p = true;
16083           ++num_templates;
16084         }
16085     }
16086
16087   pop_deferring_access_checks ();
16088
16089   if (id)
16090     cp_parser_check_for_invalid_template_id (parser, id,
16091                                              type_start_token->location);
16092
16093   /* If it's not a `:' or a `{' then we can't really be looking at a
16094      class-head, since a class-head only appears as part of a
16095      class-specifier.  We have to detect this situation before calling
16096      xref_tag, since that has irreversible side-effects.  */
16097   if (!cp_parser_next_token_starts_class_definition_p (parser))
16098     {
16099       cp_parser_error (parser, "expected %<{%> or %<:%>");
16100       return error_mark_node;
16101     }
16102
16103   /* At this point, we're going ahead with the class-specifier, even
16104      if some other problem occurs.  */
16105   cp_parser_commit_to_tentative_parse (parser);
16106   /* Issue the error about the overly-qualified name now.  */
16107   if (qualified_p)
16108     {
16109       cp_parser_error (parser,
16110                        "global qualification of class name is invalid");
16111       return error_mark_node;
16112     }
16113   else if (invalid_nested_name_p)
16114     {
16115       cp_parser_error (parser,
16116                        "qualified name does not name a class");
16117       return error_mark_node;
16118     }
16119   else if (nested_name_specifier)
16120     {
16121       tree scope;
16122
16123       /* Reject typedef-names in class heads.  */
16124       if (!DECL_IMPLICIT_TYPEDEF_P (type))
16125         {
16126           error_at (type_start_token->location,
16127                     "invalid class name in declaration of %qD",
16128                     type);
16129           type = NULL_TREE;
16130           goto done;
16131         }
16132
16133       /* Figure out in what scope the declaration is being placed.  */
16134       scope = current_scope ();
16135       /* If that scope does not contain the scope in which the
16136          class was originally declared, the program is invalid.  */
16137       if (scope && !is_ancestor (scope, nested_name_specifier))
16138         {
16139           if (at_namespace_scope_p ())
16140             error_at (type_start_token->location,
16141                       "declaration of %qD in namespace %qD which does not "
16142                       "enclose %qD",
16143                       type, scope, nested_name_specifier);
16144           else
16145             error_at (type_start_token->location,
16146                       "declaration of %qD in %qD which does not enclose %qD",
16147                       type, scope, nested_name_specifier);
16148           type = NULL_TREE;
16149           goto done;
16150         }
16151       /* [dcl.meaning]
16152
16153          A declarator-id shall not be qualified except for the
16154          definition of a ... nested class outside of its class
16155          ... [or] the definition or explicit instantiation of a
16156          class member of a namespace outside of its namespace.  */
16157       if (scope == nested_name_specifier)
16158         {
16159           permerror (nested_name_specifier_token_start->location,
16160                      "extra qualification not allowed");
16161           nested_name_specifier = NULL_TREE;
16162           num_templates = 0;
16163         }
16164     }
16165   /* An explicit-specialization must be preceded by "template <>".  If
16166      it is not, try to recover gracefully.  */
16167   if (at_namespace_scope_p ()
16168       && parser->num_template_parameter_lists == 0
16169       && template_id_p)
16170     {
16171       error_at (type_start_token->location,
16172                 "an explicit specialization must be preceded by %<template <>%>");
16173       invalid_explicit_specialization_p = true;
16174       /* Take the same action that would have been taken by
16175          cp_parser_explicit_specialization.  */
16176       ++parser->num_template_parameter_lists;
16177       begin_specialization ();
16178     }
16179   /* There must be no "return" statements between this point and the
16180      end of this function; set "type "to the correct return value and
16181      use "goto done;" to return.  */
16182   /* Make sure that the right number of template parameters were
16183      present.  */
16184   if (!cp_parser_check_template_parameters (parser, num_templates,
16185                                             type_start_token->location,
16186                                             /*declarator=*/NULL))
16187     {
16188       /* If something went wrong, there is no point in even trying to
16189          process the class-definition.  */
16190       type = NULL_TREE;
16191       goto done;
16192     }
16193
16194   /* Look up the type.  */
16195   if (template_id_p)
16196     {
16197       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16198           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16199               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16200         {
16201           error_at (type_start_token->location,
16202                     "function template %qD redeclared as a class template", id);
16203           type = error_mark_node;
16204         }
16205       else
16206         {
16207           type = TREE_TYPE (id);
16208           type = maybe_process_partial_specialization (type);
16209         }
16210       if (nested_name_specifier)
16211         pushed_scope = push_scope (nested_name_specifier);
16212     }
16213   else if (nested_name_specifier)
16214     {
16215       tree class_type;
16216
16217       /* Given:
16218
16219             template <typename T> struct S { struct T };
16220             template <typename T> struct S<T>::T { };
16221
16222          we will get a TYPENAME_TYPE when processing the definition of
16223          `S::T'.  We need to resolve it to the actual type before we
16224          try to define it.  */
16225       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16226         {
16227           class_type = resolve_typename_type (TREE_TYPE (type),
16228                                               /*only_current_p=*/false);
16229           if (TREE_CODE (class_type) != TYPENAME_TYPE)
16230             type = TYPE_NAME (class_type);
16231           else
16232             {
16233               cp_parser_error (parser, "could not resolve typename type");
16234               type = error_mark_node;
16235             }
16236         }
16237
16238       if (maybe_process_partial_specialization (TREE_TYPE (type))
16239           == error_mark_node)
16240         {
16241           type = NULL_TREE;
16242           goto done;
16243         }
16244
16245       class_type = current_class_type;
16246       /* Enter the scope indicated by the nested-name-specifier.  */
16247       pushed_scope = push_scope (nested_name_specifier);
16248       /* Get the canonical version of this type.  */
16249       type = TYPE_MAIN_DECL (TREE_TYPE (type));
16250       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16251           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16252         {
16253           type = push_template_decl (type);
16254           if (type == error_mark_node)
16255             {
16256               type = NULL_TREE;
16257               goto done;
16258             }
16259         }
16260
16261       type = TREE_TYPE (type);
16262       *nested_name_specifier_p = true;
16263     }
16264   else      /* The name is not a nested name.  */
16265     {
16266       /* If the class was unnamed, create a dummy name.  */
16267       if (!id)
16268         id = make_anon_name ();
16269       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16270                        parser->num_template_parameter_lists);
16271     }
16272
16273   /* Indicate whether this class was declared as a `class' or as a
16274      `struct'.  */
16275   if (TREE_CODE (type) == RECORD_TYPE)
16276     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16277   cp_parser_check_class_key (class_key, type);
16278
16279   /* If this type was already complete, and we see another definition,
16280      that's an error.  */
16281   if (type != error_mark_node && COMPLETE_TYPE_P (type))
16282     {
16283       error_at (type_start_token->location, "redefinition of %q#T",
16284                 type);
16285       error_at (type_start_token->location, "previous definition of %q+#T",
16286                 type);
16287       type = NULL_TREE;
16288       goto done;
16289     }
16290   else if (type == error_mark_node)
16291     type = NULL_TREE;
16292
16293   /* We will have entered the scope containing the class; the names of
16294      base classes should be looked up in that context.  For example:
16295
16296        struct A { struct B {}; struct C; };
16297        struct A::C : B {};
16298
16299      is valid.  */
16300
16301   /* Get the list of base-classes, if there is one.  */
16302   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16303     *bases = cp_parser_base_clause (parser);
16304
16305  done:
16306   /* Leave the scope given by the nested-name-specifier.  We will
16307      enter the class scope itself while processing the members.  */
16308   if (pushed_scope)
16309     pop_scope (pushed_scope);
16310
16311   if (invalid_explicit_specialization_p)
16312     {
16313       end_specialization ();
16314       --parser->num_template_parameter_lists;
16315     }
16316   *attributes_p = attributes;
16317   return type;
16318 }
16319
16320 /* Parse a class-key.
16321
16322    class-key:
16323      class
16324      struct
16325      union
16326
16327    Returns the kind of class-key specified, or none_type to indicate
16328    error.  */
16329
16330 static enum tag_types
16331 cp_parser_class_key (cp_parser* parser)
16332 {
16333   cp_token *token;
16334   enum tag_types tag_type;
16335
16336   /* Look for the class-key.  */
16337   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16338   if (!token)
16339     return none_type;
16340
16341   /* Check to see if the TOKEN is a class-key.  */
16342   tag_type = cp_parser_token_is_class_key (token);
16343   if (!tag_type)
16344     cp_parser_error (parser, "expected class-key");
16345   return tag_type;
16346 }
16347
16348 /* Parse an (optional) member-specification.
16349
16350    member-specification:
16351      member-declaration member-specification [opt]
16352      access-specifier : member-specification [opt]  */
16353
16354 static void
16355 cp_parser_member_specification_opt (cp_parser* parser)
16356 {
16357   while (true)
16358     {
16359       cp_token *token;
16360       enum rid keyword;
16361
16362       /* Peek at the next token.  */
16363       token = cp_lexer_peek_token (parser->lexer);
16364       /* If it's a `}', or EOF then we've seen all the members.  */
16365       if (token->type == CPP_CLOSE_BRACE
16366           || token->type == CPP_EOF
16367           || token->type == CPP_PRAGMA_EOL)
16368         break;
16369
16370       /* See if this token is a keyword.  */
16371       keyword = token->keyword;
16372       switch (keyword)
16373         {
16374         case RID_PUBLIC:
16375         case RID_PROTECTED:
16376         case RID_PRIVATE:
16377           /* Consume the access-specifier.  */
16378           cp_lexer_consume_token (parser->lexer);
16379           /* Remember which access-specifier is active.  */
16380           current_access_specifier = token->u.value;
16381           /* Look for the `:'.  */
16382           cp_parser_require (parser, CPP_COLON, "%<:%>");
16383           break;
16384
16385         default:
16386           /* Accept #pragmas at class scope.  */
16387           if (token->type == CPP_PRAGMA)
16388             {
16389               cp_parser_pragma (parser, pragma_external);
16390               break;
16391             }
16392
16393           /* Otherwise, the next construction must be a
16394              member-declaration.  */
16395           cp_parser_member_declaration (parser);
16396         }
16397     }
16398 }
16399
16400 /* Parse a member-declaration.
16401
16402    member-declaration:
16403      decl-specifier-seq [opt] member-declarator-list [opt] ;
16404      function-definition ; [opt]
16405      :: [opt] nested-name-specifier template [opt] unqualified-id ;
16406      using-declaration
16407      template-declaration
16408
16409    member-declarator-list:
16410      member-declarator
16411      member-declarator-list , member-declarator
16412
16413    member-declarator:
16414      declarator pure-specifier [opt]
16415      declarator constant-initializer [opt]
16416      identifier [opt] : constant-expression
16417
16418    GNU Extensions:
16419
16420    member-declaration:
16421      __extension__ member-declaration
16422
16423    member-declarator:
16424      declarator attributes [opt] pure-specifier [opt]
16425      declarator attributes [opt] constant-initializer [opt]
16426      identifier [opt] attributes [opt] : constant-expression  
16427
16428    C++0x Extensions:
16429
16430    member-declaration:
16431      static_assert-declaration  */
16432
16433 static void
16434 cp_parser_member_declaration (cp_parser* parser)
16435 {
16436   cp_decl_specifier_seq decl_specifiers;
16437   tree prefix_attributes;
16438   tree decl;
16439   int declares_class_or_enum;
16440   bool friend_p;
16441   cp_token *token = NULL;
16442   cp_token *decl_spec_token_start = NULL;
16443   cp_token *initializer_token_start = NULL;
16444   int saved_pedantic;
16445
16446   /* Check for the `__extension__' keyword.  */
16447   if (cp_parser_extension_opt (parser, &saved_pedantic))
16448     {
16449       /* Recurse.  */
16450       cp_parser_member_declaration (parser);
16451       /* Restore the old value of the PEDANTIC flag.  */
16452       pedantic = saved_pedantic;
16453
16454       return;
16455     }
16456
16457   /* Check for a template-declaration.  */
16458   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16459     {
16460       /* An explicit specialization here is an error condition, and we
16461          expect the specialization handler to detect and report this.  */
16462       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16463           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16464         cp_parser_explicit_specialization (parser);
16465       else
16466         cp_parser_template_declaration (parser, /*member_p=*/true);
16467
16468       return;
16469     }
16470
16471   /* Check for a using-declaration.  */
16472   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16473     {
16474       /* Parse the using-declaration.  */
16475       cp_parser_using_declaration (parser,
16476                                    /*access_declaration_p=*/false);
16477       return;
16478     }
16479
16480   /* Check for @defs.  */
16481   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16482     {
16483       tree ivar, member;
16484       tree ivar_chains = cp_parser_objc_defs_expression (parser);
16485       ivar = ivar_chains;
16486       while (ivar)
16487         {
16488           member = ivar;
16489           ivar = TREE_CHAIN (member);
16490           TREE_CHAIN (member) = NULL_TREE;
16491           finish_member_declaration (member);
16492         }
16493       return;
16494     }
16495
16496   /* If the next token is `static_assert' we have a static assertion.  */
16497   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16498     {
16499       cp_parser_static_assert (parser, /*member_p=*/true);
16500       return;
16501     }
16502
16503   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16504     return;
16505
16506   /* Parse the decl-specifier-seq.  */
16507   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16508   cp_parser_decl_specifier_seq (parser,
16509                                 CP_PARSER_FLAGS_OPTIONAL,
16510                                 &decl_specifiers,
16511                                 &declares_class_or_enum);
16512   prefix_attributes = decl_specifiers.attributes;
16513   decl_specifiers.attributes = NULL_TREE;
16514   /* Check for an invalid type-name.  */
16515   if (!decl_specifiers.type
16516       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16517     return;
16518   /* If there is no declarator, then the decl-specifier-seq should
16519      specify a type.  */
16520   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16521     {
16522       /* If there was no decl-specifier-seq, and the next token is a
16523          `;', then we have something like:
16524
16525            struct S { ; };
16526
16527          [class.mem]
16528
16529          Each member-declaration shall declare at least one member
16530          name of the class.  */
16531       if (!decl_specifiers.any_specifiers_p)
16532         {
16533           cp_token *token = cp_lexer_peek_token (parser->lexer);
16534           if (!in_system_header_at (token->location))
16535             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16536         }
16537       else
16538         {
16539           tree type;
16540
16541           /* See if this declaration is a friend.  */
16542           friend_p = cp_parser_friend_p (&decl_specifiers);
16543           /* If there were decl-specifiers, check to see if there was
16544              a class-declaration.  */
16545           type = check_tag_decl (&decl_specifiers);
16546           /* Nested classes have already been added to the class, but
16547              a `friend' needs to be explicitly registered.  */
16548           if (friend_p)
16549             {
16550               /* If the `friend' keyword was present, the friend must
16551                  be introduced with a class-key.  */
16552                if (!declares_class_or_enum)
16553                  error_at (decl_spec_token_start->location,
16554                            "a class-key must be used when declaring a friend");
16555                /* In this case:
16556
16557                     template <typename T> struct A {
16558                       friend struct A<T>::B;
16559                     };
16560
16561                   A<T>::B will be represented by a TYPENAME_TYPE, and
16562                   therefore not recognized by check_tag_decl.  */
16563                if (!type
16564                    && decl_specifiers.type
16565                    && TYPE_P (decl_specifiers.type))
16566                  type = decl_specifiers.type;
16567                if (!type || !TYPE_P (type))
16568                  error_at (decl_spec_token_start->location,
16569                            "friend declaration does not name a class or "
16570                            "function");
16571                else
16572                  make_friend_class (current_class_type, type,
16573                                     /*complain=*/true);
16574             }
16575           /* If there is no TYPE, an error message will already have
16576              been issued.  */
16577           else if (!type || type == error_mark_node)
16578             ;
16579           /* An anonymous aggregate has to be handled specially; such
16580              a declaration really declares a data member (with a
16581              particular type), as opposed to a nested class.  */
16582           else if (ANON_AGGR_TYPE_P (type))
16583             {
16584               /* Remove constructors and such from TYPE, now that we
16585                  know it is an anonymous aggregate.  */
16586               fixup_anonymous_aggr (type);
16587               /* And make the corresponding data member.  */
16588               decl = build_decl (decl_spec_token_start->location,
16589                                  FIELD_DECL, NULL_TREE, type);
16590               /* Add it to the class.  */
16591               finish_member_declaration (decl);
16592             }
16593           else
16594             cp_parser_check_access_in_redeclaration
16595                                               (TYPE_NAME (type),
16596                                                decl_spec_token_start->location);
16597         }
16598     }
16599   else
16600     {
16601       /* See if these declarations will be friends.  */
16602       friend_p = cp_parser_friend_p (&decl_specifiers);
16603
16604       /* Keep going until we hit the `;' at the end of the
16605          declaration.  */
16606       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16607         {
16608           tree attributes = NULL_TREE;
16609           tree first_attribute;
16610
16611           /* Peek at the next token.  */
16612           token = cp_lexer_peek_token (parser->lexer);
16613
16614           /* Check for a bitfield declaration.  */
16615           if (token->type == CPP_COLON
16616               || (token->type == CPP_NAME
16617                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16618                   == CPP_COLON))
16619             {
16620               tree identifier;
16621               tree width;
16622
16623               /* Get the name of the bitfield.  Note that we cannot just
16624                  check TOKEN here because it may have been invalidated by
16625                  the call to cp_lexer_peek_nth_token above.  */
16626               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16627                 identifier = cp_parser_identifier (parser);
16628               else
16629                 identifier = NULL_TREE;
16630
16631               /* Consume the `:' token.  */
16632               cp_lexer_consume_token (parser->lexer);
16633               /* Get the width of the bitfield.  */
16634               width
16635                 = cp_parser_constant_expression (parser,
16636                                                  /*allow_non_constant=*/false,
16637                                                  NULL);
16638
16639               /* Look for attributes that apply to the bitfield.  */
16640               attributes = cp_parser_attributes_opt (parser);
16641               /* Remember which attributes are prefix attributes and
16642                  which are not.  */
16643               first_attribute = attributes;
16644               /* Combine the attributes.  */
16645               attributes = chainon (prefix_attributes, attributes);
16646
16647               /* Create the bitfield declaration.  */
16648               decl = grokbitfield (identifier
16649                                    ? make_id_declarator (NULL_TREE,
16650                                                          identifier,
16651                                                          sfk_none)
16652                                    : NULL,
16653                                    &decl_specifiers,
16654                                    width,
16655                                    attributes);
16656             }
16657           else
16658             {
16659               cp_declarator *declarator;
16660               tree initializer;
16661               tree asm_specification;
16662               int ctor_dtor_or_conv_p;
16663
16664               /* Parse the declarator.  */
16665               declarator
16666                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16667                                         &ctor_dtor_or_conv_p,
16668                                         /*parenthesized_p=*/NULL,
16669                                         /*member_p=*/true);
16670
16671               /* If something went wrong parsing the declarator, make sure
16672                  that we at least consume some tokens.  */
16673               if (declarator == cp_error_declarator)
16674                 {
16675                   /* Skip to the end of the statement.  */
16676                   cp_parser_skip_to_end_of_statement (parser);
16677                   /* If the next token is not a semicolon, that is
16678                      probably because we just skipped over the body of
16679                      a function.  So, we consume a semicolon if
16680                      present, but do not issue an error message if it
16681                      is not present.  */
16682                   if (cp_lexer_next_token_is (parser->lexer,
16683                                               CPP_SEMICOLON))
16684                     cp_lexer_consume_token (parser->lexer);
16685                   return;
16686                 }
16687
16688               if (declares_class_or_enum & 2)
16689                 cp_parser_check_for_definition_in_return_type
16690                                             (declarator, decl_specifiers.type,
16691                                              decl_specifiers.type_location);
16692
16693               /* Look for an asm-specification.  */
16694               asm_specification = cp_parser_asm_specification_opt (parser);
16695               /* Look for attributes that apply to the declaration.  */
16696               attributes = cp_parser_attributes_opt (parser);
16697               /* Remember which attributes are prefix attributes and
16698                  which are not.  */
16699               first_attribute = attributes;
16700               /* Combine the attributes.  */
16701               attributes = chainon (prefix_attributes, attributes);
16702
16703               /* If it's an `=', then we have a constant-initializer or a
16704                  pure-specifier.  It is not correct to parse the
16705                  initializer before registering the member declaration
16706                  since the member declaration should be in scope while
16707                  its initializer is processed.  However, the rest of the
16708                  front end does not yet provide an interface that allows
16709                  us to handle this correctly.  */
16710               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16711                 {
16712                   /* In [class.mem]:
16713
16714                      A pure-specifier shall be used only in the declaration of
16715                      a virtual function.
16716
16717                      A member-declarator can contain a constant-initializer
16718                      only if it declares a static member of integral or
16719                      enumeration type.
16720
16721                      Therefore, if the DECLARATOR is for a function, we look
16722                      for a pure-specifier; otherwise, we look for a
16723                      constant-initializer.  When we call `grokfield', it will
16724                      perform more stringent semantics checks.  */
16725                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
16726                   if (function_declarator_p (declarator))
16727                     initializer = cp_parser_pure_specifier (parser);
16728                   else
16729                     /* Parse the initializer.  */
16730                     initializer = cp_parser_constant_initializer (parser);
16731                 }
16732               /* Otherwise, there is no initializer.  */
16733               else
16734                 initializer = NULL_TREE;
16735
16736               /* See if we are probably looking at a function
16737                  definition.  We are certainly not looking at a
16738                  member-declarator.  Calling `grokfield' has
16739                  side-effects, so we must not do it unless we are sure
16740                  that we are looking at a member-declarator.  */
16741               if (cp_parser_token_starts_function_definition_p
16742                   (cp_lexer_peek_token (parser->lexer)))
16743                 {
16744                   /* The grammar does not allow a pure-specifier to be
16745                      used when a member function is defined.  (It is
16746                      possible that this fact is an oversight in the
16747                      standard, since a pure function may be defined
16748                      outside of the class-specifier.  */
16749                   if (initializer)
16750                     error_at (initializer_token_start->location,
16751                               "pure-specifier on function-definition");
16752                   decl = cp_parser_save_member_function_body (parser,
16753                                                               &decl_specifiers,
16754                                                               declarator,
16755                                                               attributes);
16756                   /* If the member was not a friend, declare it here.  */
16757                   if (!friend_p)
16758                     finish_member_declaration (decl);
16759                   /* Peek at the next token.  */
16760                   token = cp_lexer_peek_token (parser->lexer);
16761                   /* If the next token is a semicolon, consume it.  */
16762                   if (token->type == CPP_SEMICOLON)
16763                     cp_lexer_consume_token (parser->lexer);
16764                   return;
16765                 }
16766               else
16767                 if (declarator->kind == cdk_function)
16768                   declarator->id_loc = token->location;
16769                 /* Create the declaration.  */
16770                 decl = grokfield (declarator, &decl_specifiers,
16771                                   initializer, /*init_const_expr_p=*/true,
16772                                   asm_specification,
16773                                   attributes);
16774             }
16775
16776           /* Reset PREFIX_ATTRIBUTES.  */
16777           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16778             attributes = TREE_CHAIN (attributes);
16779           if (attributes)
16780             TREE_CHAIN (attributes) = NULL_TREE;
16781
16782           /* If there is any qualification still in effect, clear it
16783              now; we will be starting fresh with the next declarator.  */
16784           parser->scope = NULL_TREE;
16785           parser->qualifying_scope = NULL_TREE;
16786           parser->object_scope = NULL_TREE;
16787           /* If it's a `,', then there are more declarators.  */
16788           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16789             cp_lexer_consume_token (parser->lexer);
16790           /* If the next token isn't a `;', then we have a parse error.  */
16791           else if (cp_lexer_next_token_is_not (parser->lexer,
16792                                                CPP_SEMICOLON))
16793             {
16794               cp_parser_error (parser, "expected %<;%>");
16795               /* Skip tokens until we find a `;'.  */
16796               cp_parser_skip_to_end_of_statement (parser);
16797
16798               break;
16799             }
16800
16801           if (decl)
16802             {
16803               /* Add DECL to the list of members.  */
16804               if (!friend_p)
16805                 finish_member_declaration (decl);
16806
16807               if (TREE_CODE (decl) == FUNCTION_DECL)
16808                 cp_parser_save_default_args (parser, decl);
16809             }
16810         }
16811     }
16812
16813   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16814 }
16815
16816 /* Parse a pure-specifier.
16817
16818    pure-specifier:
16819      = 0
16820
16821    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16822    Otherwise, ERROR_MARK_NODE is returned.  */
16823
16824 static tree
16825 cp_parser_pure_specifier (cp_parser* parser)
16826 {
16827   cp_token *token;
16828
16829   /* Look for the `=' token.  */
16830   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16831     return error_mark_node;
16832   /* Look for the `0' token.  */
16833   token = cp_lexer_peek_token (parser->lexer);
16834
16835   if (token->type == CPP_EOF
16836       || token->type == CPP_PRAGMA_EOL)
16837     return error_mark_node;
16838
16839   cp_lexer_consume_token (parser->lexer);
16840
16841   /* Accept = default or = delete in c++0x mode.  */
16842   if (token->keyword == RID_DEFAULT
16843       || token->keyword == RID_DELETE)
16844     {
16845       maybe_warn_cpp0x ("defaulted and deleted functions");
16846       return token->u.value;
16847     }
16848
16849   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16850   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16851     {
16852       cp_parser_error (parser,
16853                        "invalid pure specifier (only %<= 0%> is allowed)");
16854       cp_parser_skip_to_end_of_statement (parser);
16855       return error_mark_node;
16856     }
16857   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16858     {
16859       error_at (token->location, "templates may not be %<virtual%>");
16860       return error_mark_node;
16861     }
16862
16863   return integer_zero_node;
16864 }
16865
16866 /* Parse a constant-initializer.
16867
16868    constant-initializer:
16869      = constant-expression
16870
16871    Returns a representation of the constant-expression.  */
16872
16873 static tree
16874 cp_parser_constant_initializer (cp_parser* parser)
16875 {
16876   /* Look for the `=' token.  */
16877   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16878     return error_mark_node;
16879
16880   /* It is invalid to write:
16881
16882        struct S { static const int i = { 7 }; };
16883
16884      */
16885   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16886     {
16887       cp_parser_error (parser,
16888                        "a brace-enclosed initializer is not allowed here");
16889       /* Consume the opening brace.  */
16890       cp_lexer_consume_token (parser->lexer);
16891       /* Skip the initializer.  */
16892       cp_parser_skip_to_closing_brace (parser);
16893       /* Look for the trailing `}'.  */
16894       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16895
16896       return error_mark_node;
16897     }
16898
16899   return cp_parser_constant_expression (parser,
16900                                         /*allow_non_constant=*/false,
16901                                         NULL);
16902 }
16903
16904 /* Derived classes [gram.class.derived] */
16905
16906 /* Parse a base-clause.
16907
16908    base-clause:
16909      : base-specifier-list
16910
16911    base-specifier-list:
16912      base-specifier ... [opt]
16913      base-specifier-list , base-specifier ... [opt]
16914
16915    Returns a TREE_LIST representing the base-classes, in the order in
16916    which they were declared.  The representation of each node is as
16917    described by cp_parser_base_specifier.
16918
16919    In the case that no bases are specified, this function will return
16920    NULL_TREE, not ERROR_MARK_NODE.  */
16921
16922 static tree
16923 cp_parser_base_clause (cp_parser* parser)
16924 {
16925   tree bases = NULL_TREE;
16926
16927   /* Look for the `:' that begins the list.  */
16928   cp_parser_require (parser, CPP_COLON, "%<:%>");
16929
16930   /* Scan the base-specifier-list.  */
16931   while (true)
16932     {
16933       cp_token *token;
16934       tree base;
16935       bool pack_expansion_p = false;
16936
16937       /* Look for the base-specifier.  */
16938       base = cp_parser_base_specifier (parser);
16939       /* Look for the (optional) ellipsis. */
16940       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16941         {
16942           /* Consume the `...'. */
16943           cp_lexer_consume_token (parser->lexer);
16944
16945           pack_expansion_p = true;
16946         }
16947
16948       /* Add BASE to the front of the list.  */
16949       if (base != error_mark_node)
16950         {
16951           if (pack_expansion_p)
16952             /* Make this a pack expansion type. */
16953             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16954           
16955
16956           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16957             {
16958               TREE_CHAIN (base) = bases;
16959               bases = base;
16960             }
16961         }
16962       /* Peek at the next token.  */
16963       token = cp_lexer_peek_token (parser->lexer);
16964       /* If it's not a comma, then the list is complete.  */
16965       if (token->type != CPP_COMMA)
16966         break;
16967       /* Consume the `,'.  */
16968       cp_lexer_consume_token (parser->lexer);
16969     }
16970
16971   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16972      base class had a qualified name.  However, the next name that
16973      appears is certainly not qualified.  */
16974   parser->scope = NULL_TREE;
16975   parser->qualifying_scope = NULL_TREE;
16976   parser->object_scope = NULL_TREE;
16977
16978   return nreverse (bases);
16979 }
16980
16981 /* Parse a base-specifier.
16982
16983    base-specifier:
16984      :: [opt] nested-name-specifier [opt] class-name
16985      virtual access-specifier [opt] :: [opt] nested-name-specifier
16986        [opt] class-name
16987      access-specifier virtual [opt] :: [opt] nested-name-specifier
16988        [opt] class-name
16989
16990    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16991    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16992    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16993    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16994
16995 static tree
16996 cp_parser_base_specifier (cp_parser* parser)
16997 {
16998   cp_token *token;
16999   bool done = false;
17000   bool virtual_p = false;
17001   bool duplicate_virtual_error_issued_p = false;
17002   bool duplicate_access_error_issued_p = false;
17003   bool class_scope_p, template_p;
17004   tree access = access_default_node;
17005   tree type;
17006
17007   /* Process the optional `virtual' and `access-specifier'.  */
17008   while (!done)
17009     {
17010       /* Peek at the next token.  */
17011       token = cp_lexer_peek_token (parser->lexer);
17012       /* Process `virtual'.  */
17013       switch (token->keyword)
17014         {
17015         case RID_VIRTUAL:
17016           /* If `virtual' appears more than once, issue an error.  */
17017           if (virtual_p && !duplicate_virtual_error_issued_p)
17018             {
17019               cp_parser_error (parser,
17020                                "%<virtual%> specified more than once in base-specified");
17021               duplicate_virtual_error_issued_p = true;
17022             }
17023
17024           virtual_p = true;
17025
17026           /* Consume the `virtual' token.  */
17027           cp_lexer_consume_token (parser->lexer);
17028
17029           break;
17030
17031         case RID_PUBLIC:
17032         case RID_PROTECTED:
17033         case RID_PRIVATE:
17034           /* If more than one access specifier appears, issue an
17035              error.  */
17036           if (access != access_default_node
17037               && !duplicate_access_error_issued_p)
17038             {
17039               cp_parser_error (parser,
17040                                "more than one access specifier in base-specified");
17041               duplicate_access_error_issued_p = true;
17042             }
17043
17044           access = ridpointers[(int) token->keyword];
17045
17046           /* Consume the access-specifier.  */
17047           cp_lexer_consume_token (parser->lexer);
17048
17049           break;
17050
17051         default:
17052           done = true;
17053           break;
17054         }
17055     }
17056   /* It is not uncommon to see programs mechanically, erroneously, use
17057      the 'typename' keyword to denote (dependent) qualified types
17058      as base classes.  */
17059   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17060     {
17061       token = cp_lexer_peek_token (parser->lexer);
17062       if (!processing_template_decl)
17063         error_at (token->location,
17064                   "keyword %<typename%> not allowed outside of templates");
17065       else
17066         error_at (token->location,
17067                   "keyword %<typename%> not allowed in this context "
17068                   "(the base class is implicitly a type)");
17069       cp_lexer_consume_token (parser->lexer);
17070     }
17071
17072   /* Look for the optional `::' operator.  */
17073   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17074   /* Look for the nested-name-specifier.  The simplest way to
17075      implement:
17076
17077        [temp.res]
17078
17079        The keyword `typename' is not permitted in a base-specifier or
17080        mem-initializer; in these contexts a qualified name that
17081        depends on a template-parameter is implicitly assumed to be a
17082        type name.
17083
17084      is to pretend that we have seen the `typename' keyword at this
17085      point.  */
17086   cp_parser_nested_name_specifier_opt (parser,
17087                                        /*typename_keyword_p=*/true,
17088                                        /*check_dependency_p=*/true,
17089                                        typename_type,
17090                                        /*is_declaration=*/true);
17091   /* If the base class is given by a qualified name, assume that names
17092      we see are type names or templates, as appropriate.  */
17093   class_scope_p = (parser->scope && TYPE_P (parser->scope));
17094   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17095
17096   /* Finally, look for the class-name.  */
17097   type = cp_parser_class_name (parser,
17098                                class_scope_p,
17099                                template_p,
17100                                typename_type,
17101                                /*check_dependency_p=*/true,
17102                                /*class_head_p=*/false,
17103                                /*is_declaration=*/true);
17104
17105   if (type == error_mark_node)
17106     return error_mark_node;
17107
17108   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17109 }
17110
17111 /* Exception handling [gram.exception] */
17112
17113 /* Parse an (optional) exception-specification.
17114
17115    exception-specification:
17116      throw ( type-id-list [opt] )
17117
17118    Returns a TREE_LIST representing the exception-specification.  The
17119    TREE_VALUE of each node is a type.  */
17120
17121 static tree
17122 cp_parser_exception_specification_opt (cp_parser* parser)
17123 {
17124   cp_token *token;
17125   tree type_id_list;
17126
17127   /* Peek at the next token.  */
17128   token = cp_lexer_peek_token (parser->lexer);
17129   /* If it's not `throw', then there's no exception-specification.  */
17130   if (!cp_parser_is_keyword (token, RID_THROW))
17131     return NULL_TREE;
17132
17133   /* Consume the `throw'.  */
17134   cp_lexer_consume_token (parser->lexer);
17135
17136   /* Look for the `('.  */
17137   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17138
17139   /* Peek at the next token.  */
17140   token = cp_lexer_peek_token (parser->lexer);
17141   /* If it's not a `)', then there is a type-id-list.  */
17142   if (token->type != CPP_CLOSE_PAREN)
17143     {
17144       const char *saved_message;
17145
17146       /* Types may not be defined in an exception-specification.  */
17147       saved_message = parser->type_definition_forbidden_message;
17148       parser->type_definition_forbidden_message
17149         = "types may not be defined in an exception-specification";
17150       /* Parse the type-id-list.  */
17151       type_id_list = cp_parser_type_id_list (parser);
17152       /* Restore the saved message.  */
17153       parser->type_definition_forbidden_message = saved_message;
17154     }
17155   else
17156     type_id_list = empty_except_spec;
17157
17158   /* Look for the `)'.  */
17159   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17160
17161   return type_id_list;
17162 }
17163
17164 /* Parse an (optional) type-id-list.
17165
17166    type-id-list:
17167      type-id ... [opt]
17168      type-id-list , type-id ... [opt]
17169
17170    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
17171    in the order that the types were presented.  */
17172
17173 static tree
17174 cp_parser_type_id_list (cp_parser* parser)
17175 {
17176   tree types = NULL_TREE;
17177
17178   while (true)
17179     {
17180       cp_token *token;
17181       tree type;
17182
17183       /* Get the next type-id.  */
17184       type = cp_parser_type_id (parser);
17185       /* Parse the optional ellipsis. */
17186       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17187         {
17188           /* Consume the `...'. */
17189           cp_lexer_consume_token (parser->lexer);
17190
17191           /* Turn the type into a pack expansion expression. */
17192           type = make_pack_expansion (type);
17193         }
17194       /* Add it to the list.  */
17195       types = add_exception_specifier (types, type, /*complain=*/1);
17196       /* Peek at the next token.  */
17197       token = cp_lexer_peek_token (parser->lexer);
17198       /* If it is not a `,', we are done.  */
17199       if (token->type != CPP_COMMA)
17200         break;
17201       /* Consume the `,'.  */
17202       cp_lexer_consume_token (parser->lexer);
17203     }
17204
17205   return nreverse (types);
17206 }
17207
17208 /* Parse a try-block.
17209
17210    try-block:
17211      try compound-statement handler-seq  */
17212
17213 static tree
17214 cp_parser_try_block (cp_parser* parser)
17215 {
17216   tree try_block;
17217
17218   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17219   try_block = begin_try_block ();
17220   cp_parser_compound_statement (parser, NULL, true);
17221   finish_try_block (try_block);
17222   cp_parser_handler_seq (parser);
17223   finish_handler_sequence (try_block);
17224
17225   return try_block;
17226 }
17227
17228 /* Parse a function-try-block.
17229
17230    function-try-block:
17231      try ctor-initializer [opt] function-body handler-seq  */
17232
17233 static bool
17234 cp_parser_function_try_block (cp_parser* parser)
17235 {
17236   tree compound_stmt;
17237   tree try_block;
17238   bool ctor_initializer_p;
17239
17240   /* Look for the `try' keyword.  */
17241   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17242     return false;
17243   /* Let the rest of the front end know where we are.  */
17244   try_block = begin_function_try_block (&compound_stmt);
17245   /* Parse the function-body.  */
17246   ctor_initializer_p
17247     = cp_parser_ctor_initializer_opt_and_function_body (parser);
17248   /* We're done with the `try' part.  */
17249   finish_function_try_block (try_block);
17250   /* Parse the handlers.  */
17251   cp_parser_handler_seq (parser);
17252   /* We're done with the handlers.  */
17253   finish_function_handler_sequence (try_block, compound_stmt);
17254
17255   return ctor_initializer_p;
17256 }
17257
17258 /* Parse a handler-seq.
17259
17260    handler-seq:
17261      handler handler-seq [opt]  */
17262
17263 static void
17264 cp_parser_handler_seq (cp_parser* parser)
17265 {
17266   while (true)
17267     {
17268       cp_token *token;
17269
17270       /* Parse the handler.  */
17271       cp_parser_handler (parser);
17272       /* Peek at the next token.  */
17273       token = cp_lexer_peek_token (parser->lexer);
17274       /* If it's not `catch' then there are no more handlers.  */
17275       if (!cp_parser_is_keyword (token, RID_CATCH))
17276         break;
17277     }
17278 }
17279
17280 /* Parse a handler.
17281
17282    handler:
17283      catch ( exception-declaration ) compound-statement  */
17284
17285 static void
17286 cp_parser_handler (cp_parser* parser)
17287 {
17288   tree handler;
17289   tree declaration;
17290
17291   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17292   handler = begin_handler ();
17293   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17294   declaration = cp_parser_exception_declaration (parser);
17295   finish_handler_parms (declaration, handler);
17296   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17297   cp_parser_compound_statement (parser, NULL, false);
17298   finish_handler (handler);
17299 }
17300
17301 /* Parse an exception-declaration.
17302
17303    exception-declaration:
17304      type-specifier-seq declarator
17305      type-specifier-seq abstract-declarator
17306      type-specifier-seq
17307      ...
17308
17309    Returns a VAR_DECL for the declaration, or NULL_TREE if the
17310    ellipsis variant is used.  */
17311
17312 static tree
17313 cp_parser_exception_declaration (cp_parser* parser)
17314 {
17315   cp_decl_specifier_seq type_specifiers;
17316   cp_declarator *declarator;
17317   const char *saved_message;
17318
17319   /* If it's an ellipsis, it's easy to handle.  */
17320   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17321     {
17322       /* Consume the `...' token.  */
17323       cp_lexer_consume_token (parser->lexer);
17324       return NULL_TREE;
17325     }
17326
17327   /* Types may not be defined in exception-declarations.  */
17328   saved_message = parser->type_definition_forbidden_message;
17329   parser->type_definition_forbidden_message
17330     = "types may not be defined in exception-declarations";
17331
17332   /* Parse the type-specifier-seq.  */
17333   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
17334                                 /*is_trailing_return=*/false,
17335                                 &type_specifiers);
17336   /* If it's a `)', then there is no declarator.  */
17337   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17338     declarator = NULL;
17339   else
17340     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17341                                        /*ctor_dtor_or_conv_p=*/NULL,
17342                                        /*parenthesized_p=*/NULL,
17343                                        /*member_p=*/false);
17344
17345   /* Restore the saved message.  */
17346   parser->type_definition_forbidden_message = saved_message;
17347
17348   if (!type_specifiers.any_specifiers_p)
17349     return error_mark_node;
17350
17351   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17352 }
17353
17354 /* Parse a throw-expression.
17355
17356    throw-expression:
17357      throw assignment-expression [opt]
17358
17359    Returns a THROW_EXPR representing the throw-expression.  */
17360
17361 static tree
17362 cp_parser_throw_expression (cp_parser* parser)
17363 {
17364   tree expression;
17365   cp_token* token;
17366
17367   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17368   token = cp_lexer_peek_token (parser->lexer);
17369   /* Figure out whether or not there is an assignment-expression
17370      following the "throw" keyword.  */
17371   if (token->type == CPP_COMMA
17372       || token->type == CPP_SEMICOLON
17373       || token->type == CPP_CLOSE_PAREN
17374       || token->type == CPP_CLOSE_SQUARE
17375       || token->type == CPP_CLOSE_BRACE
17376       || token->type == CPP_COLON)
17377     expression = NULL_TREE;
17378   else
17379     expression = cp_parser_assignment_expression (parser,
17380                                                   /*cast_p=*/false, NULL);
17381
17382   return build_throw (expression);
17383 }
17384
17385 /* GNU Extensions */
17386
17387 /* Parse an (optional) asm-specification.
17388
17389    asm-specification:
17390      asm ( string-literal )
17391
17392    If the asm-specification is present, returns a STRING_CST
17393    corresponding to the string-literal.  Otherwise, returns
17394    NULL_TREE.  */
17395
17396 static tree
17397 cp_parser_asm_specification_opt (cp_parser* parser)
17398 {
17399   cp_token *token;
17400   tree asm_specification;
17401
17402   /* Peek at the next token.  */
17403   token = cp_lexer_peek_token (parser->lexer);
17404   /* If the next token isn't the `asm' keyword, then there's no
17405      asm-specification.  */
17406   if (!cp_parser_is_keyword (token, RID_ASM))
17407     return NULL_TREE;
17408
17409   /* Consume the `asm' token.  */
17410   cp_lexer_consume_token (parser->lexer);
17411   /* Look for the `('.  */
17412   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17413
17414   /* Look for the string-literal.  */
17415   asm_specification = cp_parser_string_literal (parser, false, false);
17416
17417   /* Look for the `)'.  */
17418   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17419
17420   return asm_specification;
17421 }
17422
17423 /* Parse an asm-operand-list.
17424
17425    asm-operand-list:
17426      asm-operand
17427      asm-operand-list , asm-operand
17428
17429    asm-operand:
17430      string-literal ( expression )
17431      [ string-literal ] string-literal ( expression )
17432
17433    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
17434    each node is the expression.  The TREE_PURPOSE is itself a
17435    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17436    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17437    is a STRING_CST for the string literal before the parenthesis. Returns
17438    ERROR_MARK_NODE if any of the operands are invalid.  */
17439
17440 static tree
17441 cp_parser_asm_operand_list (cp_parser* parser)
17442 {
17443   tree asm_operands = NULL_TREE;
17444   bool invalid_operands = false;
17445
17446   while (true)
17447     {
17448       tree string_literal;
17449       tree expression;
17450       tree name;
17451
17452       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17453         {
17454           /* Consume the `[' token.  */
17455           cp_lexer_consume_token (parser->lexer);
17456           /* Read the operand name.  */
17457           name = cp_parser_identifier (parser);
17458           if (name != error_mark_node)
17459             name = build_string (IDENTIFIER_LENGTH (name),
17460                                  IDENTIFIER_POINTER (name));
17461           /* Look for the closing `]'.  */
17462           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17463         }
17464       else
17465         name = NULL_TREE;
17466       /* Look for the string-literal.  */
17467       string_literal = cp_parser_string_literal (parser, false, false);
17468
17469       /* Look for the `('.  */
17470       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17471       /* Parse the expression.  */
17472       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17473       /* Look for the `)'.  */
17474       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17475
17476       if (name == error_mark_node 
17477           || string_literal == error_mark_node 
17478           || expression == error_mark_node)
17479         invalid_operands = true;
17480
17481       /* Add this operand to the list.  */
17482       asm_operands = tree_cons (build_tree_list (name, string_literal),
17483                                 expression,
17484                                 asm_operands);
17485       /* If the next token is not a `,', there are no more
17486          operands.  */
17487       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17488         break;
17489       /* Consume the `,'.  */
17490       cp_lexer_consume_token (parser->lexer);
17491     }
17492
17493   return invalid_operands ? error_mark_node : nreverse (asm_operands);
17494 }
17495
17496 /* Parse an asm-clobber-list.
17497
17498    asm-clobber-list:
17499      string-literal
17500      asm-clobber-list , string-literal
17501
17502    Returns a TREE_LIST, indicating the clobbers in the order that they
17503    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
17504
17505 static tree
17506 cp_parser_asm_clobber_list (cp_parser* parser)
17507 {
17508   tree clobbers = NULL_TREE;
17509
17510   while (true)
17511     {
17512       tree string_literal;
17513
17514       /* Look for the string literal.  */
17515       string_literal = cp_parser_string_literal (parser, false, false);
17516       /* Add it to the list.  */
17517       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17518       /* If the next token is not a `,', then the list is
17519          complete.  */
17520       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17521         break;
17522       /* Consume the `,' token.  */
17523       cp_lexer_consume_token (parser->lexer);
17524     }
17525
17526   return clobbers;
17527 }
17528
17529 /* Parse an asm-label-list.
17530
17531    asm-label-list:
17532      identifier
17533      asm-label-list , identifier
17534
17535    Returns a TREE_LIST, indicating the labels in the order that they
17536    appeared.  The TREE_VALUE of each node is a label.  */
17537
17538 static tree
17539 cp_parser_asm_label_list (cp_parser* parser)
17540 {
17541   tree labels = NULL_TREE;
17542
17543   while (true)
17544     {
17545       tree identifier, label, name;
17546
17547       /* Look for the identifier.  */
17548       identifier = cp_parser_identifier (parser);
17549       if (!error_operand_p (identifier))
17550         {
17551           label = lookup_label (identifier);
17552           if (TREE_CODE (label) == LABEL_DECL)
17553             {
17554               TREE_USED (label) = 1;
17555               check_goto (label);
17556               name = build_string (IDENTIFIER_LENGTH (identifier),
17557                                    IDENTIFIER_POINTER (identifier));
17558               labels = tree_cons (name, label, labels);
17559             }
17560         }
17561       /* If the next token is not a `,', then the list is
17562          complete.  */
17563       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17564         break;
17565       /* Consume the `,' token.  */
17566       cp_lexer_consume_token (parser->lexer);
17567     }
17568
17569   return nreverse (labels);
17570 }
17571
17572 /* Parse an (optional) series of attributes.
17573
17574    attributes:
17575      attributes attribute
17576
17577    attribute:
17578      __attribute__ (( attribute-list [opt] ))
17579
17580    The return value is as for cp_parser_attribute_list.  */
17581
17582 static tree
17583 cp_parser_attributes_opt (cp_parser* parser)
17584 {
17585   tree attributes = NULL_TREE;
17586
17587   while (true)
17588     {
17589       cp_token *token;
17590       tree attribute_list;
17591
17592       /* Peek at the next token.  */
17593       token = cp_lexer_peek_token (parser->lexer);
17594       /* If it's not `__attribute__', then we're done.  */
17595       if (token->keyword != RID_ATTRIBUTE)
17596         break;
17597
17598       /* Consume the `__attribute__' keyword.  */
17599       cp_lexer_consume_token (parser->lexer);
17600       /* Look for the two `(' tokens.  */
17601       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17602       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17603
17604       /* Peek at the next token.  */
17605       token = cp_lexer_peek_token (parser->lexer);
17606       if (token->type != CPP_CLOSE_PAREN)
17607         /* Parse the attribute-list.  */
17608         attribute_list = cp_parser_attribute_list (parser);
17609       else
17610         /* If the next token is a `)', then there is no attribute
17611            list.  */
17612         attribute_list = NULL;
17613
17614       /* Look for the two `)' tokens.  */
17615       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17616       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17617
17618       /* Add these new attributes to the list.  */
17619       attributes = chainon (attributes, attribute_list);
17620     }
17621
17622   return attributes;
17623 }
17624
17625 /* Parse an attribute-list.
17626
17627    attribute-list:
17628      attribute
17629      attribute-list , attribute
17630
17631    attribute:
17632      identifier
17633      identifier ( identifier )
17634      identifier ( identifier , expression-list )
17635      identifier ( expression-list )
17636
17637    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
17638    to an attribute.  The TREE_PURPOSE of each node is the identifier
17639    indicating which attribute is in use.  The TREE_VALUE represents
17640    the arguments, if any.  */
17641
17642 static tree
17643 cp_parser_attribute_list (cp_parser* parser)
17644 {
17645   tree attribute_list = NULL_TREE;
17646   bool save_translate_strings_p = parser->translate_strings_p;
17647
17648   parser->translate_strings_p = false;
17649   while (true)
17650     {
17651       cp_token *token;
17652       tree identifier;
17653       tree attribute;
17654
17655       /* Look for the identifier.  We also allow keywords here; for
17656          example `__attribute__ ((const))' is legal.  */
17657       token = cp_lexer_peek_token (parser->lexer);
17658       if (token->type == CPP_NAME
17659           || token->type == CPP_KEYWORD)
17660         {
17661           tree arguments = NULL_TREE;
17662
17663           /* Consume the token.  */
17664           token = cp_lexer_consume_token (parser->lexer);
17665
17666           /* Save away the identifier that indicates which attribute
17667              this is.  */
17668           identifier = (token->type == CPP_KEYWORD) 
17669             /* For keywords, use the canonical spelling, not the
17670                parsed identifier.  */
17671             ? ridpointers[(int) token->keyword]
17672             : token->u.value;
17673           
17674           attribute = build_tree_list (identifier, NULL_TREE);
17675
17676           /* Peek at the next token.  */
17677           token = cp_lexer_peek_token (parser->lexer);
17678           /* If it's an `(', then parse the attribute arguments.  */
17679           if (token->type == CPP_OPEN_PAREN)
17680             {
17681               VEC(tree,gc) *vec;
17682               vec = cp_parser_parenthesized_expression_list
17683                     (parser, true, /*cast_p=*/false,
17684                      /*allow_expansion_p=*/false,
17685                      /*non_constant_p=*/NULL);
17686               if (vec == NULL)
17687                 arguments = error_mark_node;
17688               else
17689                 {
17690                   arguments = build_tree_list_vec (vec);
17691                   release_tree_vector (vec);
17692                 }
17693               /* Save the arguments away.  */
17694               TREE_VALUE (attribute) = arguments;
17695             }
17696
17697           if (arguments != error_mark_node)
17698             {
17699               /* Add this attribute to the list.  */
17700               TREE_CHAIN (attribute) = attribute_list;
17701               attribute_list = attribute;
17702             }
17703
17704           token = cp_lexer_peek_token (parser->lexer);
17705         }
17706       /* Now, look for more attributes.  If the next token isn't a
17707          `,', we're done.  */
17708       if (token->type != CPP_COMMA)
17709         break;
17710
17711       /* Consume the comma and keep going.  */
17712       cp_lexer_consume_token (parser->lexer);
17713     }
17714   parser->translate_strings_p = save_translate_strings_p;
17715
17716   /* We built up the list in reverse order.  */
17717   return nreverse (attribute_list);
17718 }
17719
17720 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
17721    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
17722    current value of the PEDANTIC flag, regardless of whether or not
17723    the `__extension__' keyword is present.  The caller is responsible
17724    for restoring the value of the PEDANTIC flag.  */
17725
17726 static bool
17727 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17728 {
17729   /* Save the old value of the PEDANTIC flag.  */
17730   *saved_pedantic = pedantic;
17731
17732   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17733     {
17734       /* Consume the `__extension__' token.  */
17735       cp_lexer_consume_token (parser->lexer);
17736       /* We're not being pedantic while the `__extension__' keyword is
17737          in effect.  */
17738       pedantic = 0;
17739
17740       return true;
17741     }
17742
17743   return false;
17744 }
17745
17746 /* Parse a label declaration.
17747
17748    label-declaration:
17749      __label__ label-declarator-seq ;
17750
17751    label-declarator-seq:
17752      identifier , label-declarator-seq
17753      identifier  */
17754
17755 static void
17756 cp_parser_label_declaration (cp_parser* parser)
17757 {
17758   /* Look for the `__label__' keyword.  */
17759   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17760
17761   while (true)
17762     {
17763       tree identifier;
17764
17765       /* Look for an identifier.  */
17766       identifier = cp_parser_identifier (parser);
17767       /* If we failed, stop.  */
17768       if (identifier == error_mark_node)
17769         break;
17770       /* Declare it as a label.  */
17771       finish_label_decl (identifier);
17772       /* If the next token is a `;', stop.  */
17773       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17774         break;
17775       /* Look for the `,' separating the label declarations.  */
17776       cp_parser_require (parser, CPP_COMMA, "%<,%>");
17777     }
17778
17779   /* Look for the final `;'.  */
17780   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17781 }
17782
17783 /* Support Functions */
17784
17785 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17786    NAME should have one of the representations used for an
17787    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17788    is returned.  If PARSER->SCOPE is a dependent type, then a
17789    SCOPE_REF is returned.
17790
17791    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17792    returned; the name was already resolved when the TEMPLATE_ID_EXPR
17793    was formed.  Abstractly, such entities should not be passed to this
17794    function, because they do not need to be looked up, but it is
17795    simpler to check for this special case here, rather than at the
17796    call-sites.
17797
17798    In cases not explicitly covered above, this function returns a
17799    DECL, OVERLOAD, or baselink representing the result of the lookup.
17800    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17801    is returned.
17802
17803    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17804    (e.g., "struct") that was used.  In that case bindings that do not
17805    refer to types are ignored.
17806
17807    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17808    ignored.
17809
17810    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17811    are ignored.
17812
17813    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17814    types.
17815
17816    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17817    TREE_LIST of candidates if name-lookup results in an ambiguity, and
17818    NULL_TREE otherwise.  */
17819
17820 static tree
17821 cp_parser_lookup_name (cp_parser *parser, tree name,
17822                        enum tag_types tag_type,
17823                        bool is_template,
17824                        bool is_namespace,
17825                        bool check_dependency,
17826                        tree *ambiguous_decls,
17827                        location_t name_location)
17828 {
17829   int flags = 0;
17830   tree decl;
17831   tree object_type = parser->context->object_type;
17832
17833   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17834     flags |= LOOKUP_COMPLAIN;
17835
17836   /* Assume that the lookup will be unambiguous.  */
17837   if (ambiguous_decls)
17838     *ambiguous_decls = NULL_TREE;
17839
17840   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17841      no longer valid.  Note that if we are parsing tentatively, and
17842      the parse fails, OBJECT_TYPE will be automatically restored.  */
17843   parser->context->object_type = NULL_TREE;
17844
17845   if (name == error_mark_node)
17846     return error_mark_node;
17847
17848   /* A template-id has already been resolved; there is no lookup to
17849      do.  */
17850   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17851     return name;
17852   if (BASELINK_P (name))
17853     {
17854       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17855                   == TEMPLATE_ID_EXPR);
17856       return name;
17857     }
17858
17859   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17860      it should already have been checked to make sure that the name
17861      used matches the type being destroyed.  */
17862   if (TREE_CODE (name) == BIT_NOT_EXPR)
17863     {
17864       tree type;
17865
17866       /* Figure out to which type this destructor applies.  */
17867       if (parser->scope)
17868         type = parser->scope;
17869       else if (object_type)
17870         type = object_type;
17871       else
17872         type = current_class_type;
17873       /* If that's not a class type, there is no destructor.  */
17874       if (!type || !CLASS_TYPE_P (type))
17875         return error_mark_node;
17876       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17877         lazily_declare_fn (sfk_destructor, type);
17878       if (!CLASSTYPE_DESTRUCTORS (type))
17879           return error_mark_node;
17880       /* If it was a class type, return the destructor.  */
17881       return CLASSTYPE_DESTRUCTORS (type);
17882     }
17883
17884   /* By this point, the NAME should be an ordinary identifier.  If
17885      the id-expression was a qualified name, the qualifying scope is
17886      stored in PARSER->SCOPE at this point.  */
17887   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17888
17889   /* Perform the lookup.  */
17890   if (parser->scope)
17891     {
17892       bool dependent_p;
17893
17894       if (parser->scope == error_mark_node)
17895         return error_mark_node;
17896
17897       /* If the SCOPE is dependent, the lookup must be deferred until
17898          the template is instantiated -- unless we are explicitly
17899          looking up names in uninstantiated templates.  Even then, we
17900          cannot look up the name if the scope is not a class type; it
17901          might, for example, be a template type parameter.  */
17902       dependent_p = (TYPE_P (parser->scope)
17903                      && dependent_scope_p (parser->scope));
17904       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17905           && dependent_p)
17906         /* Defer lookup.  */
17907         decl = error_mark_node;
17908       else
17909         {
17910           tree pushed_scope = NULL_TREE;
17911
17912           /* If PARSER->SCOPE is a dependent type, then it must be a
17913              class type, and we must not be checking dependencies;
17914              otherwise, we would have processed this lookup above.  So
17915              that PARSER->SCOPE is not considered a dependent base by
17916              lookup_member, we must enter the scope here.  */
17917           if (dependent_p)
17918             pushed_scope = push_scope (parser->scope);
17919           /* If the PARSER->SCOPE is a template specialization, it
17920              may be instantiated during name lookup.  In that case,
17921              errors may be issued.  Even if we rollback the current
17922              tentative parse, those errors are valid.  */
17923           decl = lookup_qualified_name (parser->scope, name,
17924                                         tag_type != none_type,
17925                                         /*complain=*/true);
17926
17927           /* If we have a single function from a using decl, pull it out.  */
17928           if (TREE_CODE (decl) == OVERLOAD
17929               && !really_overloaded_fn (decl))
17930             decl = OVL_FUNCTION (decl);
17931
17932           if (pushed_scope)
17933             pop_scope (pushed_scope);
17934         }
17935
17936       /* If the scope is a dependent type and either we deferred lookup or
17937          we did lookup but didn't find the name, rememeber the name.  */
17938       if (decl == error_mark_node && TYPE_P (parser->scope)
17939           && dependent_type_p (parser->scope))
17940         {
17941           if (tag_type)
17942             {
17943               tree type;
17944
17945               /* The resolution to Core Issue 180 says that `struct
17946                  A::B' should be considered a type-name, even if `A'
17947                  is dependent.  */
17948               type = make_typename_type (parser->scope, name, tag_type,
17949                                          /*complain=*/tf_error);
17950               decl = TYPE_NAME (type);
17951             }
17952           else if (is_template
17953                    && (cp_parser_next_token_ends_template_argument_p (parser)
17954                        || cp_lexer_next_token_is (parser->lexer,
17955                                                   CPP_CLOSE_PAREN)))
17956             decl = make_unbound_class_template (parser->scope,
17957                                                 name, NULL_TREE,
17958                                                 /*complain=*/tf_error);
17959           else
17960             decl = build_qualified_name (/*type=*/NULL_TREE,
17961                                          parser->scope, name,
17962                                          is_template);
17963         }
17964       parser->qualifying_scope = parser->scope;
17965       parser->object_scope = NULL_TREE;
17966     }
17967   else if (object_type)
17968     {
17969       tree object_decl = NULL_TREE;
17970       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17971          OBJECT_TYPE is not a class.  */
17972       if (CLASS_TYPE_P (object_type))
17973         /* If the OBJECT_TYPE is a template specialization, it may
17974            be instantiated during name lookup.  In that case, errors
17975            may be issued.  Even if we rollback the current tentative
17976            parse, those errors are valid.  */
17977         object_decl = lookup_member (object_type,
17978                                      name,
17979                                      /*protect=*/0,
17980                                      tag_type != none_type);
17981       /* Look it up in the enclosing context, too.  */
17982       decl = lookup_name_real (name, tag_type != none_type,
17983                                /*nonclass=*/0,
17984                                /*block_p=*/true, is_namespace, flags);
17985       parser->object_scope = object_type;
17986       parser->qualifying_scope = NULL_TREE;
17987       if (object_decl)
17988         decl = object_decl;
17989     }
17990   else
17991     {
17992       decl = lookup_name_real (name, tag_type != none_type,
17993                                /*nonclass=*/0,
17994                                /*block_p=*/true, is_namespace, flags);
17995       parser->qualifying_scope = NULL_TREE;
17996       parser->object_scope = NULL_TREE;
17997     }
17998
17999   /* If the lookup failed, let our caller know.  */
18000   if (!decl || decl == error_mark_node)
18001     return error_mark_node;
18002
18003   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
18004   if (TREE_CODE (decl) == TREE_LIST)
18005     {
18006       if (ambiguous_decls)
18007         *ambiguous_decls = decl;
18008       /* The error message we have to print is too complicated for
18009          cp_parser_error, so we incorporate its actions directly.  */
18010       if (!cp_parser_simulate_error (parser))
18011         {
18012           error_at (name_location, "reference to %qD is ambiguous",
18013                     name);
18014           print_candidates (decl);
18015         }
18016       return error_mark_node;
18017     }
18018
18019   gcc_assert (DECL_P (decl)
18020               || TREE_CODE (decl) == OVERLOAD
18021               || TREE_CODE (decl) == SCOPE_REF
18022               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18023               || BASELINK_P (decl));
18024
18025   /* If we have resolved the name of a member declaration, check to
18026      see if the declaration is accessible.  When the name resolves to
18027      set of overloaded functions, accessibility is checked when
18028      overload resolution is done.
18029
18030      During an explicit instantiation, access is not checked at all,
18031      as per [temp.explicit].  */
18032   if (DECL_P (decl))
18033     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18034
18035   return decl;
18036 }
18037
18038 /* Like cp_parser_lookup_name, but for use in the typical case where
18039    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18040    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
18041
18042 static tree
18043 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18044 {
18045   return cp_parser_lookup_name (parser, name,
18046                                 none_type,
18047                                 /*is_template=*/false,
18048                                 /*is_namespace=*/false,
18049                                 /*check_dependency=*/true,
18050                                 /*ambiguous_decls=*/NULL,
18051                                 location);
18052 }
18053
18054 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18055    the current context, return the TYPE_DECL.  If TAG_NAME_P is
18056    true, the DECL indicates the class being defined in a class-head,
18057    or declared in an elaborated-type-specifier.
18058
18059    Otherwise, return DECL.  */
18060
18061 static tree
18062 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18063 {
18064   /* If the TEMPLATE_DECL is being declared as part of a class-head,
18065      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18066
18067        struct A {
18068          template <typename T> struct B;
18069        };
18070
18071        template <typename T> struct A::B {};
18072
18073      Similarly, in an elaborated-type-specifier:
18074
18075        namespace N { struct X{}; }
18076
18077        struct A {
18078          template <typename T> friend struct N::X;
18079        };
18080
18081      However, if the DECL refers to a class type, and we are in
18082      the scope of the class, then the name lookup automatically
18083      finds the TYPE_DECL created by build_self_reference rather
18084      than a TEMPLATE_DECL.  For example, in:
18085
18086        template <class T> struct S {
18087          S s;
18088        };
18089
18090      there is no need to handle such case.  */
18091
18092   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18093     return DECL_TEMPLATE_RESULT (decl);
18094
18095   return decl;
18096 }
18097
18098 /* If too many, or too few, template-parameter lists apply to the
18099    declarator, issue an error message.  Returns TRUE if all went well,
18100    and FALSE otherwise.  */
18101
18102 static bool
18103 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18104                                                 cp_declarator *declarator,
18105                                                 location_t declarator_location)
18106 {
18107   unsigned num_templates;
18108
18109   /* We haven't seen any classes that involve template parameters yet.  */
18110   num_templates = 0;
18111
18112   switch (declarator->kind)
18113     {
18114     case cdk_id:
18115       if (declarator->u.id.qualifying_scope)
18116         {
18117           tree scope;
18118           tree member;
18119
18120           scope = declarator->u.id.qualifying_scope;
18121           member = declarator->u.id.unqualified_name;
18122
18123           while (scope && CLASS_TYPE_P (scope))
18124             {
18125               /* You're supposed to have one `template <...>'
18126                  for every template class, but you don't need one
18127                  for a full specialization.  For example:
18128
18129                  template <class T> struct S{};
18130                  template <> struct S<int> { void f(); };
18131                  void S<int>::f () {}
18132
18133                  is correct; there shouldn't be a `template <>' for
18134                  the definition of `S<int>::f'.  */
18135               if (!CLASSTYPE_TEMPLATE_INFO (scope))
18136                 /* If SCOPE does not have template information of any
18137                    kind, then it is not a template, nor is it nested
18138                    within a template.  */
18139                 break;
18140               if (explicit_class_specialization_p (scope))
18141                 break;
18142               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18143                 ++num_templates;
18144
18145               scope = TYPE_CONTEXT (scope);
18146             }
18147         }
18148       else if (TREE_CODE (declarator->u.id.unqualified_name)
18149                == TEMPLATE_ID_EXPR)
18150         /* If the DECLARATOR has the form `X<y>' then it uses one
18151            additional level of template parameters.  */
18152         ++num_templates;
18153
18154       return cp_parser_check_template_parameters 
18155         (parser, num_templates, declarator_location, declarator);
18156
18157
18158     case cdk_function:
18159     case cdk_array:
18160     case cdk_pointer:
18161     case cdk_reference:
18162     case cdk_ptrmem:
18163       return (cp_parser_check_declarator_template_parameters
18164               (parser, declarator->declarator, declarator_location));
18165
18166     case cdk_error:
18167       return true;
18168
18169     default:
18170       gcc_unreachable ();
18171     }
18172   return false;
18173 }
18174
18175 /* NUM_TEMPLATES were used in the current declaration.  If that is
18176    invalid, return FALSE and issue an error messages.  Otherwise,
18177    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
18178    declarator and we can print more accurate diagnostics.  */
18179
18180 static bool
18181 cp_parser_check_template_parameters (cp_parser* parser,
18182                                      unsigned num_templates,
18183                                      location_t location,
18184                                      cp_declarator *declarator)
18185 {
18186   /* If there are the same number of template classes and parameter
18187      lists, that's OK.  */
18188   if (parser->num_template_parameter_lists == num_templates)
18189     return true;
18190   /* If there are more, but only one more, then we are referring to a
18191      member template.  That's OK too.  */
18192   if (parser->num_template_parameter_lists == num_templates + 1)
18193     return true;
18194   /* If there are more template classes than parameter lists, we have
18195      something like:
18196
18197        template <class T> void S<T>::R<T>::f ();  */
18198   if (parser->num_template_parameter_lists < num_templates)
18199     {
18200       if (declarator)
18201         error_at (location, "specializing member %<%T::%E%> "
18202                   "requires %<template<>%> syntax", 
18203                   declarator->u.id.qualifying_scope,
18204                   declarator->u.id.unqualified_name);
18205       else 
18206         error_at (location, "too few template-parameter-lists");
18207       return false;
18208     }
18209   /* Otherwise, there are too many template parameter lists.  We have
18210      something like:
18211
18212      template <class T> template <class U> void S::f();  */
18213   error_at (location, "too many template-parameter-lists");
18214   return false;
18215 }
18216
18217 /* Parse an optional `::' token indicating that the following name is
18218    from the global namespace.  If so, PARSER->SCOPE is set to the
18219    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18220    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18221    Returns the new value of PARSER->SCOPE, if the `::' token is
18222    present, and NULL_TREE otherwise.  */
18223
18224 static tree
18225 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18226 {
18227   cp_token *token;
18228
18229   /* Peek at the next token.  */
18230   token = cp_lexer_peek_token (parser->lexer);
18231   /* If we're looking at a `::' token then we're starting from the
18232      global namespace, not our current location.  */
18233   if (token->type == CPP_SCOPE)
18234     {
18235       /* Consume the `::' token.  */
18236       cp_lexer_consume_token (parser->lexer);
18237       /* Set the SCOPE so that we know where to start the lookup.  */
18238       parser->scope = global_namespace;
18239       parser->qualifying_scope = global_namespace;
18240       parser->object_scope = NULL_TREE;
18241
18242       return parser->scope;
18243     }
18244   else if (!current_scope_valid_p)
18245     {
18246       parser->scope = NULL_TREE;
18247       parser->qualifying_scope = NULL_TREE;
18248       parser->object_scope = NULL_TREE;
18249     }
18250
18251   return NULL_TREE;
18252 }
18253
18254 /* Returns TRUE if the upcoming token sequence is the start of a
18255    constructor declarator.  If FRIEND_P is true, the declarator is
18256    preceded by the `friend' specifier.  */
18257
18258 static bool
18259 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18260 {
18261   bool constructor_p;
18262   tree type_decl = NULL_TREE;
18263   bool nested_name_p;
18264   cp_token *next_token;
18265
18266   /* The common case is that this is not a constructor declarator, so
18267      try to avoid doing lots of work if at all possible.  It's not
18268      valid declare a constructor at function scope.  */
18269   if (parser->in_function_body)
18270     return false;
18271   /* And only certain tokens can begin a constructor declarator.  */
18272   next_token = cp_lexer_peek_token (parser->lexer);
18273   if (next_token->type != CPP_NAME
18274       && next_token->type != CPP_SCOPE
18275       && next_token->type != CPP_NESTED_NAME_SPECIFIER
18276       && next_token->type != CPP_TEMPLATE_ID)
18277     return false;
18278
18279   /* Parse tentatively; we are going to roll back all of the tokens
18280      consumed here.  */
18281   cp_parser_parse_tentatively (parser);
18282   /* Assume that we are looking at a constructor declarator.  */
18283   constructor_p = true;
18284
18285   /* Look for the optional `::' operator.  */
18286   cp_parser_global_scope_opt (parser,
18287                               /*current_scope_valid_p=*/false);
18288   /* Look for the nested-name-specifier.  */
18289   nested_name_p
18290     = (cp_parser_nested_name_specifier_opt (parser,
18291                                             /*typename_keyword_p=*/false,
18292                                             /*check_dependency_p=*/false,
18293                                             /*type_p=*/false,
18294                                             /*is_declaration=*/false)
18295        != NULL_TREE);
18296   /* Outside of a class-specifier, there must be a
18297      nested-name-specifier.  */
18298   if (!nested_name_p &&
18299       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18300        || friend_p))
18301     constructor_p = false;
18302   /* If we still think that this might be a constructor-declarator,
18303      look for a class-name.  */
18304   if (constructor_p)
18305     {
18306       /* If we have:
18307
18308            template <typename T> struct S { S(); };
18309            template <typename T> S<T>::S ();
18310
18311          we must recognize that the nested `S' names a class.
18312          Similarly, for:
18313
18314            template <typename T> S<T>::S<T> ();
18315
18316          we must recognize that the nested `S' names a template.  */
18317       type_decl = cp_parser_class_name (parser,
18318                                         /*typename_keyword_p=*/false,
18319                                         /*template_keyword_p=*/false,
18320                                         none_type,
18321                                         /*check_dependency_p=*/false,
18322                                         /*class_head_p=*/false,
18323                                         /*is_declaration=*/false);
18324       /* If there was no class-name, then this is not a constructor.  */
18325       constructor_p = !cp_parser_error_occurred (parser);
18326     }
18327
18328   /* If we're still considering a constructor, we have to see a `(',
18329      to begin the parameter-declaration-clause, followed by either a
18330      `)', an `...', or a decl-specifier.  We need to check for a
18331      type-specifier to avoid being fooled into thinking that:
18332
18333        S::S (f) (int);
18334
18335      is a constructor.  (It is actually a function named `f' that
18336      takes one parameter (of type `int') and returns a value of type
18337      `S::S'.  */
18338   if (constructor_p
18339       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18340     {
18341       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18342           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18343           /* A parameter declaration begins with a decl-specifier,
18344              which is either the "attribute" keyword, a storage class
18345              specifier, or (usually) a type-specifier.  */
18346           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18347         {
18348           tree type;
18349           tree pushed_scope = NULL_TREE;
18350           unsigned saved_num_template_parameter_lists;
18351
18352           /* Names appearing in the type-specifier should be looked up
18353              in the scope of the class.  */
18354           if (current_class_type)
18355             type = NULL_TREE;
18356           else
18357             {
18358               type = TREE_TYPE (type_decl);
18359               if (TREE_CODE (type) == TYPENAME_TYPE)
18360                 {
18361                   type = resolve_typename_type (type,
18362                                                 /*only_current_p=*/false);
18363                   if (TREE_CODE (type) == TYPENAME_TYPE)
18364                     {
18365                       cp_parser_abort_tentative_parse (parser);
18366                       return false;
18367                     }
18368                 }
18369               pushed_scope = push_scope (type);
18370             }
18371
18372           /* Inside the constructor parameter list, surrounding
18373              template-parameter-lists do not apply.  */
18374           saved_num_template_parameter_lists
18375             = parser->num_template_parameter_lists;
18376           parser->num_template_parameter_lists = 0;
18377
18378           /* Look for the type-specifier.  */
18379           cp_parser_type_specifier (parser,
18380                                     CP_PARSER_FLAGS_NONE,
18381                                     /*decl_specs=*/NULL,
18382                                     /*is_declarator=*/true,
18383                                     /*declares_class_or_enum=*/NULL,
18384                                     /*is_cv_qualifier=*/NULL);
18385
18386           parser->num_template_parameter_lists
18387             = saved_num_template_parameter_lists;
18388
18389           /* Leave the scope of the class.  */
18390           if (pushed_scope)
18391             pop_scope (pushed_scope);
18392
18393           constructor_p = !cp_parser_error_occurred (parser);
18394         }
18395     }
18396   else
18397     constructor_p = false;
18398   /* We did not really want to consume any tokens.  */
18399   cp_parser_abort_tentative_parse (parser);
18400
18401   return constructor_p;
18402 }
18403
18404 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18405    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
18406    they must be performed once we are in the scope of the function.
18407
18408    Returns the function defined.  */
18409
18410 static tree
18411 cp_parser_function_definition_from_specifiers_and_declarator
18412   (cp_parser* parser,
18413    cp_decl_specifier_seq *decl_specifiers,
18414    tree attributes,
18415    const cp_declarator *declarator)
18416 {
18417   tree fn;
18418   bool success_p;
18419
18420   /* Begin the function-definition.  */
18421   success_p = start_function (decl_specifiers, declarator, attributes);
18422
18423   /* The things we're about to see are not directly qualified by any
18424      template headers we've seen thus far.  */
18425   reset_specialization ();
18426
18427   /* If there were names looked up in the decl-specifier-seq that we
18428      did not check, check them now.  We must wait until we are in the
18429      scope of the function to perform the checks, since the function
18430      might be a friend.  */
18431   perform_deferred_access_checks ();
18432
18433   if (!success_p)
18434     {
18435       /* Skip the entire function.  */
18436       cp_parser_skip_to_end_of_block_or_statement (parser);
18437       fn = error_mark_node;
18438     }
18439   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18440     {
18441       /* Seen already, skip it.  An error message has already been output.  */
18442       cp_parser_skip_to_end_of_block_or_statement (parser);
18443       fn = current_function_decl;
18444       current_function_decl = NULL_TREE;
18445       /* If this is a function from a class, pop the nested class.  */
18446       if (current_class_name)
18447         pop_nested_class ();
18448     }
18449   else
18450     fn = cp_parser_function_definition_after_declarator (parser,
18451                                                          /*inline_p=*/false);
18452
18453   return fn;
18454 }
18455
18456 /* Parse the part of a function-definition that follows the
18457    declarator.  INLINE_P is TRUE iff this function is an inline
18458    function defined within a class-specifier.
18459
18460    Returns the function defined.  */
18461
18462 static tree
18463 cp_parser_function_definition_after_declarator (cp_parser* parser,
18464                                                 bool inline_p)
18465 {
18466   tree fn;
18467   bool ctor_initializer_p = false;
18468   bool saved_in_unbraced_linkage_specification_p;
18469   bool saved_in_function_body;
18470   unsigned saved_num_template_parameter_lists;
18471   cp_token *token;
18472
18473   saved_in_function_body = parser->in_function_body;
18474   parser->in_function_body = true;
18475   /* If the next token is `return', then the code may be trying to
18476      make use of the "named return value" extension that G++ used to
18477      support.  */
18478   token = cp_lexer_peek_token (parser->lexer);
18479   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18480     {
18481       /* Consume the `return' keyword.  */
18482       cp_lexer_consume_token (parser->lexer);
18483       /* Look for the identifier that indicates what value is to be
18484          returned.  */
18485       cp_parser_identifier (parser);
18486       /* Issue an error message.  */
18487       error_at (token->location,
18488                 "named return values are no longer supported");
18489       /* Skip tokens until we reach the start of the function body.  */
18490       while (true)
18491         {
18492           cp_token *token = cp_lexer_peek_token (parser->lexer);
18493           if (token->type == CPP_OPEN_BRACE
18494               || token->type == CPP_EOF
18495               || token->type == CPP_PRAGMA_EOL)
18496             break;
18497           cp_lexer_consume_token (parser->lexer);
18498         }
18499     }
18500   /* The `extern' in `extern "C" void f () { ... }' does not apply to
18501      anything declared inside `f'.  */
18502   saved_in_unbraced_linkage_specification_p
18503     = parser->in_unbraced_linkage_specification_p;
18504   parser->in_unbraced_linkage_specification_p = false;
18505   /* Inside the function, surrounding template-parameter-lists do not
18506      apply.  */
18507   saved_num_template_parameter_lists
18508     = parser->num_template_parameter_lists;
18509   parser->num_template_parameter_lists = 0;
18510
18511   start_lambda_scope (current_function_decl);
18512
18513   /* If the next token is `try', then we are looking at a
18514      function-try-block.  */
18515   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18516     ctor_initializer_p = cp_parser_function_try_block (parser);
18517   /* A function-try-block includes the function-body, so we only do
18518      this next part if we're not processing a function-try-block.  */
18519   else
18520     ctor_initializer_p
18521       = cp_parser_ctor_initializer_opt_and_function_body (parser);
18522
18523   finish_lambda_scope ();
18524
18525   /* Finish the function.  */
18526   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18527                         (inline_p ? 2 : 0));
18528   /* Generate code for it, if necessary.  */
18529   expand_or_defer_fn (fn);
18530   /* Restore the saved values.  */
18531   parser->in_unbraced_linkage_specification_p
18532     = saved_in_unbraced_linkage_specification_p;
18533   parser->num_template_parameter_lists
18534     = saved_num_template_parameter_lists;
18535   parser->in_function_body = saved_in_function_body;
18536
18537   return fn;
18538 }
18539
18540 /* Parse a template-declaration, assuming that the `export' (and
18541    `extern') keywords, if present, has already been scanned.  MEMBER_P
18542    is as for cp_parser_template_declaration.  */
18543
18544 static void
18545 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18546 {
18547   tree decl = NULL_TREE;
18548   VEC (deferred_access_check,gc) *checks;
18549   tree parameter_list;
18550   bool friend_p = false;
18551   bool need_lang_pop;
18552   cp_token *token;
18553
18554   /* Look for the `template' keyword.  */
18555   token = cp_lexer_peek_token (parser->lexer);
18556   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18557     return;
18558
18559   /* And the `<'.  */
18560   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18561     return;
18562   if (at_class_scope_p () && current_function_decl)
18563     {
18564       /* 14.5.2.2 [temp.mem]
18565
18566          A local class shall not have member templates.  */
18567       error_at (token->location,
18568                 "invalid declaration of member template in local class");
18569       cp_parser_skip_to_end_of_block_or_statement (parser);
18570       return;
18571     }
18572   /* [temp]
18573
18574      A template ... shall not have C linkage.  */
18575   if (current_lang_name == lang_name_c)
18576     {
18577       error_at (token->location, "template with C linkage");
18578       /* Give it C++ linkage to avoid confusing other parts of the
18579          front end.  */
18580       push_lang_context (lang_name_cplusplus);
18581       need_lang_pop = true;
18582     }
18583   else
18584     need_lang_pop = false;
18585
18586   /* We cannot perform access checks on the template parameter
18587      declarations until we know what is being declared, just as we
18588      cannot check the decl-specifier list.  */
18589   push_deferring_access_checks (dk_deferred);
18590
18591   /* If the next token is `>', then we have an invalid
18592      specialization.  Rather than complain about an invalid template
18593      parameter, issue an error message here.  */
18594   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18595     {
18596       cp_parser_error (parser, "invalid explicit specialization");
18597       begin_specialization ();
18598       parameter_list = NULL_TREE;
18599     }
18600   else
18601     /* Parse the template parameters.  */
18602     parameter_list = cp_parser_template_parameter_list (parser);
18603
18604   /* Get the deferred access checks from the parameter list.  These
18605      will be checked once we know what is being declared, as for a
18606      member template the checks must be performed in the scope of the
18607      class containing the member.  */
18608   checks = get_deferred_access_checks ();
18609
18610   /* Look for the `>'.  */
18611   cp_parser_skip_to_end_of_template_parameter_list (parser);
18612   /* We just processed one more parameter list.  */
18613   ++parser->num_template_parameter_lists;
18614   /* If the next token is `template', there are more template
18615      parameters.  */
18616   if (cp_lexer_next_token_is_keyword (parser->lexer,
18617                                       RID_TEMPLATE))
18618     cp_parser_template_declaration_after_export (parser, member_p);
18619   else
18620     {
18621       /* There are no access checks when parsing a template, as we do not
18622          know if a specialization will be a friend.  */
18623       push_deferring_access_checks (dk_no_check);
18624       token = cp_lexer_peek_token (parser->lexer);
18625       decl = cp_parser_single_declaration (parser,
18626                                            checks,
18627                                            member_p,
18628                                            /*explicit_specialization_p=*/false,
18629                                            &friend_p);
18630       pop_deferring_access_checks ();
18631
18632       /* If this is a member template declaration, let the front
18633          end know.  */
18634       if (member_p && !friend_p && decl)
18635         {
18636           if (TREE_CODE (decl) == TYPE_DECL)
18637             cp_parser_check_access_in_redeclaration (decl, token->location);
18638
18639           decl = finish_member_template_decl (decl);
18640         }
18641       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18642         make_friend_class (current_class_type, TREE_TYPE (decl),
18643                            /*complain=*/true);
18644     }
18645   /* We are done with the current parameter list.  */
18646   --parser->num_template_parameter_lists;
18647
18648   pop_deferring_access_checks ();
18649
18650   /* Finish up.  */
18651   finish_template_decl (parameter_list);
18652
18653   /* Register member declarations.  */
18654   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18655     finish_member_declaration (decl);
18656   /* For the erroneous case of a template with C linkage, we pushed an
18657      implicit C++ linkage scope; exit that scope now.  */
18658   if (need_lang_pop)
18659     pop_lang_context ();
18660   /* If DECL is a function template, we must return to parse it later.
18661      (Even though there is no definition, there might be default
18662      arguments that need handling.)  */
18663   if (member_p && decl
18664       && (TREE_CODE (decl) == FUNCTION_DECL
18665           || DECL_FUNCTION_TEMPLATE_P (decl)))
18666     TREE_VALUE (parser->unparsed_functions_queues)
18667       = tree_cons (NULL_TREE, decl,
18668                    TREE_VALUE (parser->unparsed_functions_queues));
18669 }
18670
18671 /* Perform the deferred access checks from a template-parameter-list.
18672    CHECKS is a TREE_LIST of access checks, as returned by
18673    get_deferred_access_checks.  */
18674
18675 static void
18676 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18677 {
18678   ++processing_template_parmlist;
18679   perform_access_checks (checks);
18680   --processing_template_parmlist;
18681 }
18682
18683 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18684    `function-definition' sequence.  MEMBER_P is true, this declaration
18685    appears in a class scope.
18686
18687    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
18688    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
18689
18690 static tree
18691 cp_parser_single_declaration (cp_parser* parser,
18692                               VEC (deferred_access_check,gc)* checks,
18693                               bool member_p,
18694                               bool explicit_specialization_p,
18695                               bool* friend_p)
18696 {
18697   int declares_class_or_enum;
18698   tree decl = NULL_TREE;
18699   cp_decl_specifier_seq decl_specifiers;
18700   bool function_definition_p = false;
18701   cp_token *decl_spec_token_start;
18702
18703   /* This function is only used when processing a template
18704      declaration.  */
18705   gcc_assert (innermost_scope_kind () == sk_template_parms
18706               || innermost_scope_kind () == sk_template_spec);
18707
18708   /* Defer access checks until we know what is being declared.  */
18709   push_deferring_access_checks (dk_deferred);
18710
18711   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18712      alternative.  */
18713   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18714   cp_parser_decl_specifier_seq (parser,
18715                                 CP_PARSER_FLAGS_OPTIONAL,
18716                                 &decl_specifiers,
18717                                 &declares_class_or_enum);
18718   if (friend_p)
18719     *friend_p = cp_parser_friend_p (&decl_specifiers);
18720
18721   /* There are no template typedefs.  */
18722   if (decl_specifiers.specs[(int) ds_typedef])
18723     {
18724       error_at (decl_spec_token_start->location,
18725                 "template declaration of %<typedef%>");
18726       decl = error_mark_node;
18727     }
18728
18729   /* Gather up the access checks that occurred the
18730      decl-specifier-seq.  */
18731   stop_deferring_access_checks ();
18732
18733   /* Check for the declaration of a template class.  */
18734   if (declares_class_or_enum)
18735     {
18736       if (cp_parser_declares_only_class_p (parser))
18737         {
18738           decl = shadow_tag (&decl_specifiers);
18739
18740           /* In this case:
18741
18742                struct C {
18743                  friend template <typename T> struct A<T>::B;
18744                };
18745
18746              A<T>::B will be represented by a TYPENAME_TYPE, and
18747              therefore not recognized by shadow_tag.  */
18748           if (friend_p && *friend_p
18749               && !decl
18750               && decl_specifiers.type
18751               && TYPE_P (decl_specifiers.type))
18752             decl = decl_specifiers.type;
18753
18754           if (decl && decl != error_mark_node)
18755             decl = TYPE_NAME (decl);
18756           else
18757             decl = error_mark_node;
18758
18759           /* Perform access checks for template parameters.  */
18760           cp_parser_perform_template_parameter_access_checks (checks);
18761         }
18762     }
18763   /* If it's not a template class, try for a template function.  If
18764      the next token is a `;', then this declaration does not declare
18765      anything.  But, if there were errors in the decl-specifiers, then
18766      the error might well have come from an attempted class-specifier.
18767      In that case, there's no need to warn about a missing declarator.  */
18768   if (!decl
18769       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18770           || decl_specifiers.type != error_mark_node))
18771     {
18772       decl = cp_parser_init_declarator (parser,
18773                                         &decl_specifiers,
18774                                         checks,
18775                                         /*function_definition_allowed_p=*/true,
18776                                         member_p,
18777                                         declares_class_or_enum,
18778                                         &function_definition_p);
18779
18780     /* 7.1.1-1 [dcl.stc]
18781
18782        A storage-class-specifier shall not be specified in an explicit
18783        specialization...  */
18784     if (decl
18785         && explicit_specialization_p
18786         && decl_specifiers.storage_class != sc_none)
18787       {
18788         error_at (decl_spec_token_start->location,
18789                   "explicit template specialization cannot have a storage class");
18790         decl = error_mark_node;
18791       }
18792     }
18793
18794   pop_deferring_access_checks ();
18795
18796   /* Clear any current qualification; whatever comes next is the start
18797      of something new.  */
18798   parser->scope = NULL_TREE;
18799   parser->qualifying_scope = NULL_TREE;
18800   parser->object_scope = NULL_TREE;
18801   /* Look for a trailing `;' after the declaration.  */
18802   if (!function_definition_p
18803       && (decl == error_mark_node
18804           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18805     cp_parser_skip_to_end_of_block_or_statement (parser);
18806
18807   return decl;
18808 }
18809
18810 /* Parse a cast-expression that is not the operand of a unary "&".  */
18811
18812 static tree
18813 cp_parser_simple_cast_expression (cp_parser *parser)
18814 {
18815   return cp_parser_cast_expression (parser, /*address_p=*/false,
18816                                     /*cast_p=*/false, NULL);
18817 }
18818
18819 /* Parse a functional cast to TYPE.  Returns an expression
18820    representing the cast.  */
18821
18822 static tree
18823 cp_parser_functional_cast (cp_parser* parser, tree type)
18824 {
18825   VEC(tree,gc) *vec;
18826   tree expression_list;
18827   tree cast;
18828   bool nonconst_p;
18829
18830   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18831     {
18832       maybe_warn_cpp0x ("extended initializer lists");
18833       expression_list = cp_parser_braced_list (parser, &nonconst_p);
18834       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18835       if (TREE_CODE (type) == TYPE_DECL)
18836         type = TREE_TYPE (type);
18837       return finish_compound_literal (type, expression_list);
18838     }
18839
18840
18841   vec = cp_parser_parenthesized_expression_list (parser, false,
18842                                                  /*cast_p=*/true,
18843                                                  /*allow_expansion_p=*/true,
18844                                                  /*non_constant_p=*/NULL);
18845   if (vec == NULL)
18846     expression_list = error_mark_node;
18847   else
18848     {
18849       expression_list = build_tree_list_vec (vec);
18850       release_tree_vector (vec);
18851     }
18852
18853   cast = build_functional_cast (type, expression_list,
18854                                 tf_warning_or_error);
18855   /* [expr.const]/1: In an integral constant expression "only type
18856      conversions to integral or enumeration type can be used".  */
18857   if (TREE_CODE (type) == TYPE_DECL)
18858     type = TREE_TYPE (type);
18859   if (cast != error_mark_node
18860       && !cast_valid_in_integral_constant_expression_p (type)
18861       && (cp_parser_non_integral_constant_expression
18862           (parser, "a call to a constructor")))
18863     return error_mark_node;
18864   return cast;
18865 }
18866
18867 /* Save the tokens that make up the body of a member function defined
18868    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18869    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18870    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18871    for the member function.  */
18872
18873 static tree
18874 cp_parser_save_member_function_body (cp_parser* parser,
18875                                      cp_decl_specifier_seq *decl_specifiers,
18876                                      cp_declarator *declarator,
18877                                      tree attributes)
18878 {
18879   cp_token *first;
18880   cp_token *last;
18881   tree fn;
18882
18883   /* Create the FUNCTION_DECL.  */
18884   fn = grokmethod (decl_specifiers, declarator, attributes);
18885   /* If something went badly wrong, bail out now.  */
18886   if (fn == error_mark_node)
18887     {
18888       /* If there's a function-body, skip it.  */
18889       if (cp_parser_token_starts_function_definition_p
18890           (cp_lexer_peek_token (parser->lexer)))
18891         cp_parser_skip_to_end_of_block_or_statement (parser);
18892       return error_mark_node;
18893     }
18894
18895   /* Remember it, if there default args to post process.  */
18896   cp_parser_save_default_args (parser, fn);
18897
18898   /* Save away the tokens that make up the body of the
18899      function.  */
18900   first = parser->lexer->next_token;
18901   /* We can have braced-init-list mem-initializers before the fn body.  */
18902   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18903     {
18904       cp_lexer_consume_token (parser->lexer);
18905       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18906              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18907         {
18908           /* cache_group will stop after an un-nested { } pair, too.  */
18909           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18910             break;
18911
18912           /* variadic mem-inits have ... after the ')'.  */
18913           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18914             cp_lexer_consume_token (parser->lexer);
18915         }
18916     }
18917   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18918   /* Handle function try blocks.  */
18919   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18920     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18921   last = parser->lexer->next_token;
18922
18923   /* Save away the inline definition; we will process it when the
18924      class is complete.  */
18925   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18926   DECL_PENDING_INLINE_P (fn) = 1;
18927
18928   /* We need to know that this was defined in the class, so that
18929      friend templates are handled correctly.  */
18930   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18931
18932   /* Add FN to the queue of functions to be parsed later.  */
18933   TREE_VALUE (parser->unparsed_functions_queues)
18934     = tree_cons (NULL_TREE, fn,
18935                  TREE_VALUE (parser->unparsed_functions_queues));
18936
18937   return fn;
18938 }
18939
18940 /* Parse a template-argument-list, as well as the trailing ">" (but
18941    not the opening ">").  See cp_parser_template_argument_list for the
18942    return value.  */
18943
18944 static tree
18945 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18946 {
18947   tree arguments;
18948   tree saved_scope;
18949   tree saved_qualifying_scope;
18950   tree saved_object_scope;
18951   bool saved_greater_than_is_operator_p;
18952   int saved_unevaluated_operand;
18953   int saved_inhibit_evaluation_warnings;
18954
18955   /* [temp.names]
18956
18957      When parsing a template-id, the first non-nested `>' is taken as
18958      the end of the template-argument-list rather than a greater-than
18959      operator.  */
18960   saved_greater_than_is_operator_p
18961     = parser->greater_than_is_operator_p;
18962   parser->greater_than_is_operator_p = false;
18963   /* Parsing the argument list may modify SCOPE, so we save it
18964      here.  */
18965   saved_scope = parser->scope;
18966   saved_qualifying_scope = parser->qualifying_scope;
18967   saved_object_scope = parser->object_scope;
18968   /* We need to evaluate the template arguments, even though this
18969      template-id may be nested within a "sizeof".  */
18970   saved_unevaluated_operand = cp_unevaluated_operand;
18971   cp_unevaluated_operand = 0;
18972   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
18973   c_inhibit_evaluation_warnings = 0;
18974   /* Parse the template-argument-list itself.  */
18975   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18976       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18977     arguments = NULL_TREE;
18978   else
18979     arguments = cp_parser_template_argument_list (parser);
18980   /* Look for the `>' that ends the template-argument-list. If we find
18981      a '>>' instead, it's probably just a typo.  */
18982   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18983     {
18984       if (cxx_dialect != cxx98)
18985         {
18986           /* In C++0x, a `>>' in a template argument list or cast
18987              expression is considered to be two separate `>'
18988              tokens. So, change the current token to a `>', but don't
18989              consume it: it will be consumed later when the outer
18990              template argument list (or cast expression) is parsed.
18991              Note that this replacement of `>' for `>>' is necessary
18992              even if we are parsing tentatively: in the tentative
18993              case, after calling
18994              cp_parser_enclosed_template_argument_list we will always
18995              throw away all of the template arguments and the first
18996              closing `>', either because the template argument list
18997              was erroneous or because we are replacing those tokens
18998              with a CPP_TEMPLATE_ID token.  The second `>' (which will
18999              not have been thrown away) is needed either to close an
19000              outer template argument list or to complete a new-style
19001              cast.  */
19002           cp_token *token = cp_lexer_peek_token (parser->lexer);
19003           token->type = CPP_GREATER;
19004         }
19005       else if (!saved_greater_than_is_operator_p)
19006         {
19007           /* If we're in a nested template argument list, the '>>' has
19008             to be a typo for '> >'. We emit the error message, but we
19009             continue parsing and we push a '>' as next token, so that
19010             the argument list will be parsed correctly.  Note that the
19011             global source location is still on the token before the
19012             '>>', so we need to say explicitly where we want it.  */
19013           cp_token *token = cp_lexer_peek_token (parser->lexer);
19014           error_at (token->location, "%<>>%> should be %<> >%> "
19015                     "within a nested template argument list");
19016
19017           token->type = CPP_GREATER;
19018         }
19019       else
19020         {
19021           /* If this is not a nested template argument list, the '>>'
19022             is a typo for '>'. Emit an error message and continue.
19023             Same deal about the token location, but here we can get it
19024             right by consuming the '>>' before issuing the diagnostic.  */
19025           cp_token *token = cp_lexer_consume_token (parser->lexer);
19026           error_at (token->location,
19027                     "spurious %<>>%>, use %<>%> to terminate "
19028                     "a template argument list");
19029         }
19030     }
19031   else
19032     cp_parser_skip_to_end_of_template_parameter_list (parser);
19033   /* The `>' token might be a greater-than operator again now.  */
19034   parser->greater_than_is_operator_p
19035     = saved_greater_than_is_operator_p;
19036   /* Restore the SAVED_SCOPE.  */
19037   parser->scope = saved_scope;
19038   parser->qualifying_scope = saved_qualifying_scope;
19039   parser->object_scope = saved_object_scope;
19040   cp_unevaluated_operand = saved_unevaluated_operand;
19041   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19042
19043   return arguments;
19044 }
19045
19046 /* MEMBER_FUNCTION is a member function, or a friend.  If default
19047    arguments, or the body of the function have not yet been parsed,
19048    parse them now.  */
19049
19050 static void
19051 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19052 {
19053   /* If this member is a template, get the underlying
19054      FUNCTION_DECL.  */
19055   if (DECL_FUNCTION_TEMPLATE_P (member_function))
19056     member_function = DECL_TEMPLATE_RESULT (member_function);
19057
19058   /* There should not be any class definitions in progress at this
19059      point; the bodies of members are only parsed outside of all class
19060      definitions.  */
19061   gcc_assert (parser->num_classes_being_defined == 0);
19062   /* While we're parsing the member functions we might encounter more
19063      classes.  We want to handle them right away, but we don't want
19064      them getting mixed up with functions that are currently in the
19065      queue.  */
19066   parser->unparsed_functions_queues
19067     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19068
19069   /* Make sure that any template parameters are in scope.  */
19070   maybe_begin_member_template_processing (member_function);
19071
19072   /* If the body of the function has not yet been parsed, parse it
19073      now.  */
19074   if (DECL_PENDING_INLINE_P (member_function))
19075     {
19076       tree function_scope;
19077       cp_token_cache *tokens;
19078
19079       /* The function is no longer pending; we are processing it.  */
19080       tokens = DECL_PENDING_INLINE_INFO (member_function);
19081       DECL_PENDING_INLINE_INFO (member_function) = NULL;
19082       DECL_PENDING_INLINE_P (member_function) = 0;
19083
19084       /* If this is a local class, enter the scope of the containing
19085          function.  */
19086       function_scope = current_function_decl;
19087       if (function_scope)
19088         push_function_context ();
19089
19090       /* Push the body of the function onto the lexer stack.  */
19091       cp_parser_push_lexer_for_tokens (parser, tokens);
19092
19093       /* Let the front end know that we going to be defining this
19094          function.  */
19095       start_preparsed_function (member_function, NULL_TREE,
19096                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
19097
19098       /* Don't do access checking if it is a templated function.  */
19099       if (processing_template_decl)
19100         push_deferring_access_checks (dk_no_check);
19101
19102       /* Now, parse the body of the function.  */
19103       cp_parser_function_definition_after_declarator (parser,
19104                                                       /*inline_p=*/true);
19105
19106       if (processing_template_decl)
19107         pop_deferring_access_checks ();
19108
19109       /* Leave the scope of the containing function.  */
19110       if (function_scope)
19111         pop_function_context ();
19112       cp_parser_pop_lexer (parser);
19113     }
19114
19115   /* Remove any template parameters from the symbol table.  */
19116   maybe_end_member_template_processing ();
19117
19118   /* Restore the queue.  */
19119   parser->unparsed_functions_queues
19120     = TREE_CHAIN (parser->unparsed_functions_queues);
19121 }
19122
19123 /* If DECL contains any default args, remember it on the unparsed
19124    functions queue.  */
19125
19126 static void
19127 cp_parser_save_default_args (cp_parser* parser, tree decl)
19128 {
19129   tree probe;
19130
19131   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19132        probe;
19133        probe = TREE_CHAIN (probe))
19134     if (TREE_PURPOSE (probe))
19135       {
19136         TREE_PURPOSE (parser->unparsed_functions_queues)
19137           = tree_cons (current_class_type, decl,
19138                        TREE_PURPOSE (parser->unparsed_functions_queues));
19139         break;
19140       }
19141 }
19142
19143 /* FN is a FUNCTION_DECL which may contains a parameter with an
19144    unparsed DEFAULT_ARG.  Parse the default args now.  This function
19145    assumes that the current scope is the scope in which the default
19146    argument should be processed.  */
19147
19148 static void
19149 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19150 {
19151   bool saved_local_variables_forbidden_p;
19152   tree parm, parmdecl;
19153
19154   /* While we're parsing the default args, we might (due to the
19155      statement expression extension) encounter more classes.  We want
19156      to handle them right away, but we don't want them getting mixed
19157      up with default args that are currently in the queue.  */
19158   parser->unparsed_functions_queues
19159     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19160
19161   /* Local variable names (and the `this' keyword) may not appear
19162      in a default argument.  */
19163   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19164   parser->local_variables_forbidden_p = true;
19165
19166   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19167          parmdecl = DECL_ARGUMENTS (fn);
19168        parm && parm != void_list_node;
19169        parm = TREE_CHAIN (parm),
19170          parmdecl = TREE_CHAIN (parmdecl))
19171     {
19172       cp_token_cache *tokens;
19173       tree default_arg = TREE_PURPOSE (parm);
19174       tree parsed_arg;
19175       VEC(tree,gc) *insts;
19176       tree copy;
19177       unsigned ix;
19178
19179       if (!default_arg)
19180         continue;
19181
19182       if (TREE_CODE (default_arg) != DEFAULT_ARG)
19183         /* This can happen for a friend declaration for a function
19184            already declared with default arguments.  */
19185         continue;
19186
19187        /* Push the saved tokens for the default argument onto the parser's
19188           lexer stack.  */
19189       tokens = DEFARG_TOKENS (default_arg);
19190       cp_parser_push_lexer_for_tokens (parser, tokens);
19191
19192       start_lambda_scope (parmdecl);
19193
19194       /* Parse the assignment-expression.  */
19195       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19196       if (parsed_arg == error_mark_node)
19197         {
19198           cp_parser_pop_lexer (parser);
19199           continue;
19200         }
19201
19202       if (!processing_template_decl)
19203         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19204
19205       TREE_PURPOSE (parm) = parsed_arg;
19206
19207       /* Update any instantiations we've already created.  */
19208       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19209            VEC_iterate (tree, insts, ix, copy); ix++)
19210         TREE_PURPOSE (copy) = parsed_arg;
19211
19212       finish_lambda_scope ();
19213
19214       /* If the token stream has not been completely used up, then
19215          there was extra junk after the end of the default
19216          argument.  */
19217       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19218         cp_parser_error (parser, "expected %<,%>");
19219
19220       /* Revert to the main lexer.  */
19221       cp_parser_pop_lexer (parser);
19222     }
19223
19224   /* Make sure no default arg is missing.  */
19225   check_default_args (fn);
19226
19227   /* Restore the state of local_variables_forbidden_p.  */
19228   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19229
19230   /* Restore the queue.  */
19231   parser->unparsed_functions_queues
19232     = TREE_CHAIN (parser->unparsed_functions_queues);
19233 }
19234
19235 /* Parse the operand of `sizeof' (or a similar operator).  Returns
19236    either a TYPE or an expression, depending on the form of the
19237    input.  The KEYWORD indicates which kind of expression we have
19238    encountered.  */
19239
19240 static tree
19241 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19242 {
19243   tree expr = NULL_TREE;
19244   const char *saved_message;
19245   char *tmp;
19246   bool saved_integral_constant_expression_p;
19247   bool saved_non_integral_constant_expression_p;
19248   bool pack_expansion_p = false;
19249
19250   /* Types cannot be defined in a `sizeof' expression.  Save away the
19251      old message.  */
19252   saved_message = parser->type_definition_forbidden_message;
19253   /* And create the new one.  */
19254   tmp = concat ("types may not be defined in %<",
19255                 IDENTIFIER_POINTER (ridpointers[keyword]),
19256                 "%> expressions", NULL);
19257   parser->type_definition_forbidden_message = tmp;
19258
19259   /* The restrictions on constant-expressions do not apply inside
19260      sizeof expressions.  */
19261   saved_integral_constant_expression_p
19262     = parser->integral_constant_expression_p;
19263   saved_non_integral_constant_expression_p
19264     = parser->non_integral_constant_expression_p;
19265   parser->integral_constant_expression_p = false;
19266
19267   /* If it's a `...', then we are computing the length of a parameter
19268      pack.  */
19269   if (keyword == RID_SIZEOF
19270       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19271     {
19272       /* Consume the `...'.  */
19273       cp_lexer_consume_token (parser->lexer);
19274       maybe_warn_variadic_templates ();
19275
19276       /* Note that this is an expansion.  */
19277       pack_expansion_p = true;
19278     }
19279
19280   /* Do not actually evaluate the expression.  */
19281   ++cp_unevaluated_operand;
19282   ++c_inhibit_evaluation_warnings;
19283   /* If it's a `(', then we might be looking at the type-id
19284      construction.  */
19285   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19286     {
19287       tree type;
19288       bool saved_in_type_id_in_expr_p;
19289
19290       /* We can't be sure yet whether we're looking at a type-id or an
19291          expression.  */
19292       cp_parser_parse_tentatively (parser);
19293       /* Consume the `('.  */
19294       cp_lexer_consume_token (parser->lexer);
19295       /* Parse the type-id.  */
19296       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19297       parser->in_type_id_in_expr_p = true;
19298       type = cp_parser_type_id (parser);
19299       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19300       /* Now, look for the trailing `)'.  */
19301       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19302       /* If all went well, then we're done.  */
19303       if (cp_parser_parse_definitely (parser))
19304         {
19305           cp_decl_specifier_seq decl_specs;
19306
19307           /* Build a trivial decl-specifier-seq.  */
19308           clear_decl_specs (&decl_specs);
19309           decl_specs.type = type;
19310
19311           /* Call grokdeclarator to figure out what type this is.  */
19312           expr = grokdeclarator (NULL,
19313                                  &decl_specs,
19314                                  TYPENAME,
19315                                  /*initialized=*/0,
19316                                  /*attrlist=*/NULL);
19317         }
19318     }
19319
19320   /* If the type-id production did not work out, then we must be
19321      looking at the unary-expression production.  */
19322   if (!expr)
19323     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19324                                        /*cast_p=*/false, NULL);
19325
19326   if (pack_expansion_p)
19327     /* Build a pack expansion. */
19328     expr = make_pack_expansion (expr);
19329
19330   /* Go back to evaluating expressions.  */
19331   --cp_unevaluated_operand;
19332   --c_inhibit_evaluation_warnings;
19333
19334   /* Free the message we created.  */
19335   free (tmp);
19336   /* And restore the old one.  */
19337   parser->type_definition_forbidden_message = saved_message;
19338   parser->integral_constant_expression_p
19339     = saved_integral_constant_expression_p;
19340   parser->non_integral_constant_expression_p
19341     = saved_non_integral_constant_expression_p;
19342
19343   return expr;
19344 }
19345
19346 /* If the current declaration has no declarator, return true.  */
19347
19348 static bool
19349 cp_parser_declares_only_class_p (cp_parser *parser)
19350 {
19351   /* If the next token is a `;' or a `,' then there is no
19352      declarator.  */
19353   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19354           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19355 }
19356
19357 /* Update the DECL_SPECS to reflect the storage class indicated by
19358    KEYWORD.  */
19359
19360 static void
19361 cp_parser_set_storage_class (cp_parser *parser,
19362                              cp_decl_specifier_seq *decl_specs,
19363                              enum rid keyword,
19364                              location_t location)
19365 {
19366   cp_storage_class storage_class;
19367
19368   if (parser->in_unbraced_linkage_specification_p)
19369     {
19370       error_at (location, "invalid use of %qD in linkage specification",
19371                 ridpointers[keyword]);
19372       return;
19373     }
19374   else if (decl_specs->storage_class != sc_none)
19375     {
19376       decl_specs->conflicting_specifiers_p = true;
19377       return;
19378     }
19379
19380   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19381       && decl_specs->specs[(int) ds_thread])
19382     {
19383       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19384       decl_specs->specs[(int) ds_thread] = 0;
19385     }
19386
19387   switch (keyword)
19388     {
19389     case RID_AUTO:
19390       storage_class = sc_auto;
19391       break;
19392     case RID_REGISTER:
19393       storage_class = sc_register;
19394       break;
19395     case RID_STATIC:
19396       storage_class = sc_static;
19397       break;
19398     case RID_EXTERN:
19399       storage_class = sc_extern;
19400       break;
19401     case RID_MUTABLE:
19402       storage_class = sc_mutable;
19403       break;
19404     default:
19405       gcc_unreachable ();
19406     }
19407   decl_specs->storage_class = storage_class;
19408
19409   /* A storage class specifier cannot be applied alongside a typedef 
19410      specifier. If there is a typedef specifier present then set 
19411      conflicting_specifiers_p which will trigger an error later
19412      on in grokdeclarator. */
19413   if (decl_specs->specs[(int)ds_typedef])
19414     decl_specs->conflicting_specifiers_p = true;
19415 }
19416
19417 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
19418    is true, the type is a user-defined type; otherwise it is a
19419    built-in type specified by a keyword.  */
19420
19421 static void
19422 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19423                               tree type_spec,
19424                               location_t location,
19425                               bool user_defined_p)
19426 {
19427   decl_specs->any_specifiers_p = true;
19428
19429   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19430      (with, for example, in "typedef int wchar_t;") we remember that
19431      this is what happened.  In system headers, we ignore these
19432      declarations so that G++ can work with system headers that are not
19433      C++-safe.  */
19434   if (decl_specs->specs[(int) ds_typedef]
19435       && !user_defined_p
19436       && (type_spec == boolean_type_node
19437           || type_spec == char16_type_node
19438           || type_spec == char32_type_node
19439           || type_spec == wchar_type_node)
19440       && (decl_specs->type
19441           || decl_specs->specs[(int) ds_long]
19442           || decl_specs->specs[(int) ds_short]
19443           || decl_specs->specs[(int) ds_unsigned]
19444           || decl_specs->specs[(int) ds_signed]))
19445     {
19446       decl_specs->redefined_builtin_type = type_spec;
19447       if (!decl_specs->type)
19448         {
19449           decl_specs->type = type_spec;
19450           decl_specs->user_defined_type_p = false;
19451           decl_specs->type_location = location;
19452         }
19453     }
19454   else if (decl_specs->type)
19455     decl_specs->multiple_types_p = true;
19456   else
19457     {
19458       decl_specs->type = type_spec;
19459       decl_specs->user_defined_type_p = user_defined_p;
19460       decl_specs->redefined_builtin_type = NULL_TREE;
19461       decl_specs->type_location = location;
19462     }
19463 }
19464
19465 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19466    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
19467
19468 static bool
19469 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19470 {
19471   return decl_specifiers->specs[(int) ds_friend] != 0;
19472 }
19473
19474 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
19475    issue an error message indicating that TOKEN_DESC was expected.
19476
19477    Returns the token consumed, if the token had the appropriate type.
19478    Otherwise, returns NULL.  */
19479
19480 static cp_token *
19481 cp_parser_require (cp_parser* parser,
19482                    enum cpp_ttype type,
19483                    const char* token_desc)
19484 {
19485   if (cp_lexer_next_token_is (parser->lexer, type))
19486     return cp_lexer_consume_token (parser->lexer);
19487   else
19488     {
19489       /* Output the MESSAGE -- unless we're parsing tentatively.  */
19490       if (!cp_parser_simulate_error (parser))
19491         {
19492           char *message = concat ("expected ", token_desc, NULL);
19493           cp_parser_error (parser, message);
19494           free (message);
19495         }
19496       return NULL;
19497     }
19498 }
19499
19500 /* An error message is produced if the next token is not '>'.
19501    All further tokens are skipped until the desired token is
19502    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
19503
19504 static void
19505 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19506 {
19507   /* Current level of '< ... >'.  */
19508   unsigned level = 0;
19509   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
19510   unsigned nesting_depth = 0;
19511
19512   /* Are we ready, yet?  If not, issue error message.  */
19513   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19514     return;
19515
19516   /* Skip tokens until the desired token is found.  */
19517   while (true)
19518     {
19519       /* Peek at the next token.  */
19520       switch (cp_lexer_peek_token (parser->lexer)->type)
19521         {
19522         case CPP_LESS:
19523           if (!nesting_depth)
19524             ++level;
19525           break;
19526
19527         case CPP_RSHIFT:
19528           if (cxx_dialect == cxx98)
19529             /* C++0x views the `>>' operator as two `>' tokens, but
19530                C++98 does not. */
19531             break;
19532           else if (!nesting_depth && level-- == 0)
19533             {
19534               /* We've hit a `>>' where the first `>' closes the
19535                  template argument list, and the second `>' is
19536                  spurious.  Just consume the `>>' and stop; we've
19537                  already produced at least one error.  */
19538               cp_lexer_consume_token (parser->lexer);
19539               return;
19540             }
19541           /* Fall through for C++0x, so we handle the second `>' in
19542              the `>>'.  */
19543
19544         case CPP_GREATER:
19545           if (!nesting_depth && level-- == 0)
19546             {
19547               /* We've reached the token we want, consume it and stop.  */
19548               cp_lexer_consume_token (parser->lexer);
19549               return;
19550             }
19551           break;
19552
19553         case CPP_OPEN_PAREN:
19554         case CPP_OPEN_SQUARE:
19555           ++nesting_depth;
19556           break;
19557
19558         case CPP_CLOSE_PAREN:
19559         case CPP_CLOSE_SQUARE:
19560           if (nesting_depth-- == 0)
19561             return;
19562           break;
19563
19564         case CPP_EOF:
19565         case CPP_PRAGMA_EOL:
19566         case CPP_SEMICOLON:
19567         case CPP_OPEN_BRACE:
19568         case CPP_CLOSE_BRACE:
19569           /* The '>' was probably forgotten, don't look further.  */
19570           return;
19571
19572         default:
19573           break;
19574         }
19575
19576       /* Consume this token.  */
19577       cp_lexer_consume_token (parser->lexer);
19578     }
19579 }
19580
19581 /* If the next token is the indicated keyword, consume it.  Otherwise,
19582    issue an error message indicating that TOKEN_DESC was expected.
19583
19584    Returns the token consumed, if the token had the appropriate type.
19585    Otherwise, returns NULL.  */
19586
19587 static cp_token *
19588 cp_parser_require_keyword (cp_parser* parser,
19589                            enum rid keyword,
19590                            const char* token_desc)
19591 {
19592   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19593
19594   if (token && token->keyword != keyword)
19595     {
19596       dyn_string_t error_msg;
19597
19598       /* Format the error message.  */
19599       error_msg = dyn_string_new (0);
19600       dyn_string_append_cstr (error_msg, "expected ");
19601       dyn_string_append_cstr (error_msg, token_desc);
19602       cp_parser_error (parser, error_msg->s);
19603       dyn_string_delete (error_msg);
19604       return NULL;
19605     }
19606
19607   return token;
19608 }
19609
19610 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19611    function-definition.  */
19612
19613 static bool
19614 cp_parser_token_starts_function_definition_p (cp_token* token)
19615 {
19616   return (/* An ordinary function-body begins with an `{'.  */
19617           token->type == CPP_OPEN_BRACE
19618           /* A ctor-initializer begins with a `:'.  */
19619           || token->type == CPP_COLON
19620           /* A function-try-block begins with `try'.  */
19621           || token->keyword == RID_TRY
19622           /* The named return value extension begins with `return'.  */
19623           || token->keyword == RID_RETURN);
19624 }
19625
19626 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19627    definition.  */
19628
19629 static bool
19630 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19631 {
19632   cp_token *token;
19633
19634   token = cp_lexer_peek_token (parser->lexer);
19635   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19636 }
19637
19638 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19639    C++0x) ending a template-argument.  */
19640
19641 static bool
19642 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19643 {
19644   cp_token *token;
19645
19646   token = cp_lexer_peek_token (parser->lexer);
19647   return (token->type == CPP_COMMA 
19648           || token->type == CPP_GREATER
19649           || token->type == CPP_ELLIPSIS
19650           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19651 }
19652
19653 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19654    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
19655
19656 static bool
19657 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19658                                                      size_t n)
19659 {
19660   cp_token *token;
19661
19662   token = cp_lexer_peek_nth_token (parser->lexer, n);
19663   if (token->type == CPP_LESS)
19664     return true;
19665   /* Check for the sequence `<::' in the original code. It would be lexed as
19666      `[:', where `[' is a digraph, and there is no whitespace before
19667      `:'.  */
19668   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19669     {
19670       cp_token *token2;
19671       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19672       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19673         return true;
19674     }
19675   return false;
19676 }
19677
19678 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19679    or none_type otherwise.  */
19680
19681 static enum tag_types
19682 cp_parser_token_is_class_key (cp_token* token)
19683 {
19684   switch (token->keyword)
19685     {
19686     case RID_CLASS:
19687       return class_type;
19688     case RID_STRUCT:
19689       return record_type;
19690     case RID_UNION:
19691       return union_type;
19692
19693     default:
19694       return none_type;
19695     }
19696 }
19697
19698 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
19699
19700 static void
19701 cp_parser_check_class_key (enum tag_types class_key, tree type)
19702 {
19703   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19704     permerror (input_location, "%qs tag used in naming %q#T",
19705             class_key == union_type ? "union"
19706              : class_key == record_type ? "struct" : "class",
19707              type);
19708 }
19709
19710 /* Issue an error message if DECL is redeclared with different
19711    access than its original declaration [class.access.spec/3].
19712    This applies to nested classes and nested class templates.
19713    [class.mem/1].  */
19714
19715 static void
19716 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19717 {
19718   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19719     return;
19720
19721   if ((TREE_PRIVATE (decl)
19722        != (current_access_specifier == access_private_node))
19723       || (TREE_PROTECTED (decl)
19724           != (current_access_specifier == access_protected_node)))
19725     error_at (location, "%qD redeclared with different access", decl);
19726 }
19727
19728 /* Look for the `template' keyword, as a syntactic disambiguator.
19729    Return TRUE iff it is present, in which case it will be
19730    consumed.  */
19731
19732 static bool
19733 cp_parser_optional_template_keyword (cp_parser *parser)
19734 {
19735   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19736     {
19737       /* The `template' keyword can only be used within templates;
19738          outside templates the parser can always figure out what is a
19739          template and what is not.  */
19740       if (!processing_template_decl)
19741         {
19742           cp_token *token = cp_lexer_peek_token (parser->lexer);
19743           error_at (token->location,
19744                     "%<template%> (as a disambiguator) is only allowed "
19745                     "within templates");
19746           /* If this part of the token stream is rescanned, the same
19747              error message would be generated.  So, we purge the token
19748              from the stream.  */
19749           cp_lexer_purge_token (parser->lexer);
19750           return false;
19751         }
19752       else
19753         {
19754           /* Consume the `template' keyword.  */
19755           cp_lexer_consume_token (parser->lexer);
19756           return true;
19757         }
19758     }
19759
19760   return false;
19761 }
19762
19763 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
19764    set PARSER->SCOPE, and perform other related actions.  */
19765
19766 static void
19767 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19768 {
19769   int i;
19770   struct tree_check *check_value;
19771   deferred_access_check *chk;
19772   VEC (deferred_access_check,gc) *checks;
19773
19774   /* Get the stored value.  */
19775   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19776   /* Perform any access checks that were deferred.  */
19777   checks = check_value->checks;
19778   if (checks)
19779     {
19780       for (i = 0 ;
19781            VEC_iterate (deferred_access_check, checks, i, chk) ;
19782            ++i)
19783         {
19784           perform_or_defer_access_check (chk->binfo,
19785                                          chk->decl,
19786                                          chk->diag_decl);
19787         }
19788     }
19789   /* Set the scope from the stored value.  */
19790   parser->scope = check_value->value;
19791   parser->qualifying_scope = check_value->qualifying_scope;
19792   parser->object_scope = NULL_TREE;
19793 }
19794
19795 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
19796    encounter the end of a block before what we were looking for.  */
19797
19798 static bool
19799 cp_parser_cache_group (cp_parser *parser,
19800                        enum cpp_ttype end,
19801                        unsigned depth)
19802 {
19803   while (true)
19804     {
19805       cp_token *token = cp_lexer_peek_token (parser->lexer);
19806
19807       /* Abort a parenthesized expression if we encounter a semicolon.  */
19808       if ((end == CPP_CLOSE_PAREN || depth == 0)
19809           && token->type == CPP_SEMICOLON)
19810         return true;
19811       /* If we've reached the end of the file, stop.  */
19812       if (token->type == CPP_EOF
19813           || (end != CPP_PRAGMA_EOL
19814               && token->type == CPP_PRAGMA_EOL))
19815         return true;
19816       if (token->type == CPP_CLOSE_BRACE && depth == 0)
19817         /* We've hit the end of an enclosing block, so there's been some
19818            kind of syntax error.  */
19819         return true;
19820
19821       /* Consume the token.  */
19822       cp_lexer_consume_token (parser->lexer);
19823       /* See if it starts a new group.  */
19824       if (token->type == CPP_OPEN_BRACE)
19825         {
19826           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19827           /* In theory this should probably check end == '}', but
19828              cp_parser_save_member_function_body needs it to exit
19829              after either '}' or ')' when called with ')'.  */
19830           if (depth == 0)
19831             return false;
19832         }
19833       else if (token->type == CPP_OPEN_PAREN)
19834         {
19835           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19836           if (depth == 0 && end == CPP_CLOSE_PAREN)
19837             return false;
19838         }
19839       else if (token->type == CPP_PRAGMA)
19840         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19841       else if (token->type == end)
19842         return false;
19843     }
19844 }
19845
19846 /* Begin parsing tentatively.  We always save tokens while parsing
19847    tentatively so that if the tentative parsing fails we can restore the
19848    tokens.  */
19849
19850 static void
19851 cp_parser_parse_tentatively (cp_parser* parser)
19852 {
19853   /* Enter a new parsing context.  */
19854   parser->context = cp_parser_context_new (parser->context);
19855   /* Begin saving tokens.  */
19856   cp_lexer_save_tokens (parser->lexer);
19857   /* In order to avoid repetitive access control error messages,
19858      access checks are queued up until we are no longer parsing
19859      tentatively.  */
19860   push_deferring_access_checks (dk_deferred);
19861 }
19862
19863 /* Commit to the currently active tentative parse.  */
19864
19865 static void
19866 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19867 {
19868   cp_parser_context *context;
19869   cp_lexer *lexer;
19870
19871   /* Mark all of the levels as committed.  */
19872   lexer = parser->lexer;
19873   for (context = parser->context; context->next; context = context->next)
19874     {
19875       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19876         break;
19877       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19878       while (!cp_lexer_saving_tokens (lexer))
19879         lexer = lexer->next;
19880       cp_lexer_commit_tokens (lexer);
19881     }
19882 }
19883
19884 /* Abort the currently active tentative parse.  All consumed tokens
19885    will be rolled back, and no diagnostics will be issued.  */
19886
19887 static void
19888 cp_parser_abort_tentative_parse (cp_parser* parser)
19889 {
19890   cp_parser_simulate_error (parser);
19891   /* Now, pretend that we want to see if the construct was
19892      successfully parsed.  */
19893   cp_parser_parse_definitely (parser);
19894 }
19895
19896 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19897    token stream.  Otherwise, commit to the tokens we have consumed.
19898    Returns true if no error occurred; false otherwise.  */
19899
19900 static bool
19901 cp_parser_parse_definitely (cp_parser* parser)
19902 {
19903   bool error_occurred;
19904   cp_parser_context *context;
19905
19906   /* Remember whether or not an error occurred, since we are about to
19907      destroy that information.  */
19908   error_occurred = cp_parser_error_occurred (parser);
19909   /* Remove the topmost context from the stack.  */
19910   context = parser->context;
19911   parser->context = context->next;
19912   /* If no parse errors occurred, commit to the tentative parse.  */
19913   if (!error_occurred)
19914     {
19915       /* Commit to the tokens read tentatively, unless that was
19916          already done.  */
19917       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19918         cp_lexer_commit_tokens (parser->lexer);
19919
19920       pop_to_parent_deferring_access_checks ();
19921     }
19922   /* Otherwise, if errors occurred, roll back our state so that things
19923      are just as they were before we began the tentative parse.  */
19924   else
19925     {
19926       cp_lexer_rollback_tokens (parser->lexer);
19927       pop_deferring_access_checks ();
19928     }
19929   /* Add the context to the front of the free list.  */
19930   context->next = cp_parser_context_free_list;
19931   cp_parser_context_free_list = context;
19932
19933   return !error_occurred;
19934 }
19935
19936 /* Returns true if we are parsing tentatively and are not committed to
19937    this tentative parse.  */
19938
19939 static bool
19940 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19941 {
19942   return (cp_parser_parsing_tentatively (parser)
19943           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19944 }
19945
19946 /* Returns nonzero iff an error has occurred during the most recent
19947    tentative parse.  */
19948
19949 static bool
19950 cp_parser_error_occurred (cp_parser* parser)
19951 {
19952   return (cp_parser_parsing_tentatively (parser)
19953           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19954 }
19955
19956 /* Returns nonzero if GNU extensions are allowed.  */
19957
19958 static bool
19959 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19960 {
19961   return parser->allow_gnu_extensions_p;
19962 }
19963 \f
19964 /* Objective-C++ Productions */
19965
19966
19967 /* Parse an Objective-C expression, which feeds into a primary-expression
19968    above.
19969
19970    objc-expression:
19971      objc-message-expression
19972      objc-string-literal
19973      objc-encode-expression
19974      objc-protocol-expression
19975      objc-selector-expression
19976
19977   Returns a tree representation of the expression.  */
19978
19979 static tree
19980 cp_parser_objc_expression (cp_parser* parser)
19981 {
19982   /* Try to figure out what kind of declaration is present.  */
19983   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19984
19985   switch (kwd->type)
19986     {
19987     case CPP_OPEN_SQUARE:
19988       return cp_parser_objc_message_expression (parser);
19989
19990     case CPP_OBJC_STRING:
19991       kwd = cp_lexer_consume_token (parser->lexer);
19992       return objc_build_string_object (kwd->u.value);
19993
19994     case CPP_KEYWORD:
19995       switch (kwd->keyword)
19996         {
19997         case RID_AT_ENCODE:
19998           return cp_parser_objc_encode_expression (parser);
19999
20000         case RID_AT_PROTOCOL:
20001           return cp_parser_objc_protocol_expression (parser);
20002
20003         case RID_AT_SELECTOR:
20004           return cp_parser_objc_selector_expression (parser);
20005
20006         default:
20007           break;
20008         }
20009     default:
20010       error_at (kwd->location,
20011                 "misplaced %<@%D%> Objective-C++ construct",
20012                 kwd->u.value);
20013       cp_parser_skip_to_end_of_block_or_statement (parser);
20014     }
20015
20016   return error_mark_node;
20017 }
20018
20019 /* Parse an Objective-C message expression.
20020
20021    objc-message-expression:
20022      [ objc-message-receiver objc-message-args ]
20023
20024    Returns a representation of an Objective-C message.  */
20025
20026 static tree
20027 cp_parser_objc_message_expression (cp_parser* parser)
20028 {
20029   tree receiver, messageargs;
20030
20031   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
20032   receiver = cp_parser_objc_message_receiver (parser);
20033   messageargs = cp_parser_objc_message_args (parser);
20034   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20035
20036   return objc_build_message_expr (build_tree_list (receiver, messageargs));
20037 }
20038
20039 /* Parse an objc-message-receiver.
20040
20041    objc-message-receiver:
20042      expression
20043      simple-type-specifier
20044
20045   Returns a representation of the type or expression.  */
20046
20047 static tree
20048 cp_parser_objc_message_receiver (cp_parser* parser)
20049 {
20050   tree rcv;
20051
20052   /* An Objective-C message receiver may be either (1) a type
20053      or (2) an expression.  */
20054   cp_parser_parse_tentatively (parser);
20055   rcv = cp_parser_expression (parser, false, NULL);
20056
20057   if (cp_parser_parse_definitely (parser))
20058     return rcv;
20059
20060   rcv = cp_parser_simple_type_specifier (parser,
20061                                          /*decl_specs=*/NULL,
20062                                          CP_PARSER_FLAGS_NONE);
20063
20064   return objc_get_class_reference (rcv);
20065 }
20066
20067 /* Parse the arguments and selectors comprising an Objective-C message.
20068
20069    objc-message-args:
20070      objc-selector
20071      objc-selector-args
20072      objc-selector-args , objc-comma-args
20073
20074    objc-selector-args:
20075      objc-selector [opt] : assignment-expression
20076      objc-selector-args objc-selector [opt] : assignment-expression
20077
20078    objc-comma-args:
20079      assignment-expression
20080      objc-comma-args , assignment-expression
20081
20082    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20083    selector arguments and TREE_VALUE containing a list of comma
20084    arguments.  */
20085
20086 static tree
20087 cp_parser_objc_message_args (cp_parser* parser)
20088 {
20089   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20090   bool maybe_unary_selector_p = true;
20091   cp_token *token = cp_lexer_peek_token (parser->lexer);
20092
20093   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20094     {
20095       tree selector = NULL_TREE, arg;
20096
20097       if (token->type != CPP_COLON)
20098         selector = cp_parser_objc_selector (parser);
20099
20100       /* Detect if we have a unary selector.  */
20101       if (maybe_unary_selector_p
20102           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20103         return build_tree_list (selector, NULL_TREE);
20104
20105       maybe_unary_selector_p = false;
20106       cp_parser_require (parser, CPP_COLON, "%<:%>");
20107       arg = cp_parser_assignment_expression (parser, false, NULL);
20108
20109       sel_args
20110         = chainon (sel_args,
20111                    build_tree_list (selector, arg));
20112
20113       token = cp_lexer_peek_token (parser->lexer);
20114     }
20115
20116   /* Handle non-selector arguments, if any. */
20117   while (token->type == CPP_COMMA)
20118     {
20119       tree arg;
20120
20121       cp_lexer_consume_token (parser->lexer);
20122       arg = cp_parser_assignment_expression (parser, false, NULL);
20123
20124       addl_args
20125         = chainon (addl_args,
20126                    build_tree_list (NULL_TREE, arg));
20127
20128       token = cp_lexer_peek_token (parser->lexer);
20129     }
20130
20131   return build_tree_list (sel_args, addl_args);
20132 }
20133
20134 /* Parse an Objective-C encode expression.
20135
20136    objc-encode-expression:
20137      @encode objc-typename
20138
20139    Returns an encoded representation of the type argument.  */
20140
20141 static tree
20142 cp_parser_objc_encode_expression (cp_parser* parser)
20143 {
20144   tree type;
20145   cp_token *token;
20146
20147   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
20148   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20149   token = cp_lexer_peek_token (parser->lexer);
20150   type = complete_type (cp_parser_type_id (parser));
20151   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20152
20153   if (!type)
20154     {
20155       error_at (token->location, 
20156                 "%<@encode%> must specify a type as an argument");
20157       return error_mark_node;
20158     }
20159
20160   return objc_build_encode_expr (type);
20161 }
20162
20163 /* Parse an Objective-C @defs expression.  */
20164
20165 static tree
20166 cp_parser_objc_defs_expression (cp_parser *parser)
20167 {
20168   tree name;
20169
20170   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
20171   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20172   name = cp_parser_identifier (parser);
20173   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20174
20175   return objc_get_class_ivars (name);
20176 }
20177
20178 /* Parse an Objective-C protocol expression.
20179
20180   objc-protocol-expression:
20181     @protocol ( identifier )
20182
20183   Returns a representation of the protocol expression.  */
20184
20185 static tree
20186 cp_parser_objc_protocol_expression (cp_parser* parser)
20187 {
20188   tree proto;
20189
20190   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20191   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20192   proto = cp_parser_identifier (parser);
20193   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20194
20195   return objc_build_protocol_expr (proto);
20196 }
20197
20198 /* Parse an Objective-C selector expression.
20199
20200    objc-selector-expression:
20201      @selector ( objc-method-signature )
20202
20203    objc-method-signature:
20204      objc-selector
20205      objc-selector-seq
20206
20207    objc-selector-seq:
20208      objc-selector :
20209      objc-selector-seq objc-selector :
20210
20211   Returns a representation of the method selector.  */
20212
20213 static tree
20214 cp_parser_objc_selector_expression (cp_parser* parser)
20215 {
20216   tree sel_seq = NULL_TREE;
20217   bool maybe_unary_selector_p = true;
20218   cp_token *token;
20219   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20220
20221   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
20222   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20223   token = cp_lexer_peek_token (parser->lexer);
20224
20225   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20226          || token->type == CPP_SCOPE)
20227     {
20228       tree selector = NULL_TREE;
20229
20230       if (token->type != CPP_COLON
20231           || token->type == CPP_SCOPE)
20232         selector = cp_parser_objc_selector (parser);
20233
20234       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20235           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20236         {
20237           /* Detect if we have a unary selector.  */
20238           if (maybe_unary_selector_p)
20239             {
20240               sel_seq = selector;
20241               goto finish_selector;
20242             }
20243           else
20244             {
20245               cp_parser_error (parser, "expected %<:%>");
20246             }
20247         }
20248       maybe_unary_selector_p = false;
20249       token = cp_lexer_consume_token (parser->lexer);
20250
20251       if (token->type == CPP_SCOPE)
20252         {
20253           sel_seq
20254             = chainon (sel_seq,
20255                        build_tree_list (selector, NULL_TREE));
20256           sel_seq
20257             = chainon (sel_seq,
20258                        build_tree_list (NULL_TREE, NULL_TREE));
20259         }
20260       else
20261         sel_seq
20262           = chainon (sel_seq,
20263                      build_tree_list (selector, NULL_TREE));
20264
20265       token = cp_lexer_peek_token (parser->lexer);
20266     }
20267
20268  finish_selector:
20269   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20270
20271   return objc_build_selector_expr (loc, sel_seq);
20272 }
20273
20274 /* Parse a list of identifiers.
20275
20276    objc-identifier-list:
20277      identifier
20278      objc-identifier-list , identifier
20279
20280    Returns a TREE_LIST of identifier nodes.  */
20281
20282 static tree
20283 cp_parser_objc_identifier_list (cp_parser* parser)
20284 {
20285   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20286   cp_token *sep = cp_lexer_peek_token (parser->lexer);
20287
20288   while (sep->type == CPP_COMMA)
20289     {
20290       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20291       list = chainon (list,
20292                       build_tree_list (NULL_TREE,
20293                                        cp_parser_identifier (parser)));
20294       sep = cp_lexer_peek_token (parser->lexer);
20295     }
20296
20297   return list;
20298 }
20299
20300 /* Parse an Objective-C alias declaration.
20301
20302    objc-alias-declaration:
20303      @compatibility_alias identifier identifier ;
20304
20305    This function registers the alias mapping with the Objective-C front end.
20306    It returns nothing.  */
20307
20308 static void
20309 cp_parser_objc_alias_declaration (cp_parser* parser)
20310 {
20311   tree alias, orig;
20312
20313   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
20314   alias = cp_parser_identifier (parser);
20315   orig = cp_parser_identifier (parser);
20316   objc_declare_alias (alias, orig);
20317   cp_parser_consume_semicolon_at_end_of_statement (parser);
20318 }
20319
20320 /* Parse an Objective-C class forward-declaration.
20321
20322    objc-class-declaration:
20323      @class objc-identifier-list ;
20324
20325    The function registers the forward declarations with the Objective-C
20326    front end.  It returns nothing.  */
20327
20328 static void
20329 cp_parser_objc_class_declaration (cp_parser* parser)
20330 {
20331   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
20332   objc_declare_class (cp_parser_objc_identifier_list (parser));
20333   cp_parser_consume_semicolon_at_end_of_statement (parser);
20334 }
20335
20336 /* Parse a list of Objective-C protocol references.
20337
20338    objc-protocol-refs-opt:
20339      objc-protocol-refs [opt]
20340
20341    objc-protocol-refs:
20342      < objc-identifier-list >
20343
20344    Returns a TREE_LIST of identifiers, if any.  */
20345
20346 static tree
20347 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20348 {
20349   tree protorefs = NULL_TREE;
20350
20351   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20352     {
20353       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
20354       protorefs = cp_parser_objc_identifier_list (parser);
20355       cp_parser_require (parser, CPP_GREATER, "%<>%>");
20356     }
20357
20358   return protorefs;
20359 }
20360
20361 /* Parse a Objective-C visibility specification.  */
20362
20363 static void
20364 cp_parser_objc_visibility_spec (cp_parser* parser)
20365 {
20366   cp_token *vis = cp_lexer_peek_token (parser->lexer);
20367
20368   switch (vis->keyword)
20369     {
20370     case RID_AT_PRIVATE:
20371       objc_set_visibility (2);
20372       break;
20373     case RID_AT_PROTECTED:
20374       objc_set_visibility (0);
20375       break;
20376     case RID_AT_PUBLIC:
20377       objc_set_visibility (1);
20378       break;
20379     default:
20380       return;
20381     }
20382
20383   /* Eat '@private'/'@protected'/'@public'.  */
20384   cp_lexer_consume_token (parser->lexer);
20385 }
20386
20387 /* Parse an Objective-C method type.  */
20388
20389 static void
20390 cp_parser_objc_method_type (cp_parser* parser)
20391 {
20392   objc_set_method_type
20393    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20394     ? PLUS_EXPR
20395     : MINUS_EXPR);
20396 }
20397
20398 /* Parse an Objective-C protocol qualifier.  */
20399
20400 static tree
20401 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20402 {
20403   tree quals = NULL_TREE, node;
20404   cp_token *token = cp_lexer_peek_token (parser->lexer);
20405
20406   node = token->u.value;
20407
20408   while (node && TREE_CODE (node) == IDENTIFIER_NODE
20409          && (node == ridpointers [(int) RID_IN]
20410              || node == ridpointers [(int) RID_OUT]
20411              || node == ridpointers [(int) RID_INOUT]
20412              || node == ridpointers [(int) RID_BYCOPY]
20413              || node == ridpointers [(int) RID_BYREF]
20414              || node == ridpointers [(int) RID_ONEWAY]))
20415     {
20416       quals = tree_cons (NULL_TREE, node, quals);
20417       cp_lexer_consume_token (parser->lexer);
20418       token = cp_lexer_peek_token (parser->lexer);
20419       node = token->u.value;
20420     }
20421
20422   return quals;
20423 }
20424
20425 /* Parse an Objective-C typename.  */
20426
20427 static tree
20428 cp_parser_objc_typename (cp_parser* parser)
20429 {
20430   tree type_name = NULL_TREE;
20431
20432   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20433     {
20434       tree proto_quals, cp_type = NULL_TREE;
20435
20436       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20437       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20438
20439       /* An ObjC type name may consist of just protocol qualifiers, in which
20440          case the type shall default to 'id'.  */
20441       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20442         cp_type = cp_parser_type_id (parser);
20443
20444       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20445       type_name = build_tree_list (proto_quals, cp_type);
20446     }
20447
20448   return type_name;
20449 }
20450
20451 /* Check to see if TYPE refers to an Objective-C selector name.  */
20452
20453 static bool
20454 cp_parser_objc_selector_p (enum cpp_ttype type)
20455 {
20456   return (type == CPP_NAME || type == CPP_KEYWORD
20457           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20458           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20459           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20460           || type == CPP_XOR || type == CPP_XOR_EQ);
20461 }
20462
20463 /* Parse an Objective-C selector.  */
20464
20465 static tree
20466 cp_parser_objc_selector (cp_parser* parser)
20467 {
20468   cp_token *token = cp_lexer_consume_token (parser->lexer);
20469
20470   if (!cp_parser_objc_selector_p (token->type))
20471     {
20472       error_at (token->location, "invalid Objective-C++ selector name");
20473       return error_mark_node;
20474     }
20475
20476   /* C++ operator names are allowed to appear in ObjC selectors.  */
20477   switch (token->type)
20478     {
20479     case CPP_AND_AND: return get_identifier ("and");
20480     case CPP_AND_EQ: return get_identifier ("and_eq");
20481     case CPP_AND: return get_identifier ("bitand");
20482     case CPP_OR: return get_identifier ("bitor");
20483     case CPP_COMPL: return get_identifier ("compl");
20484     case CPP_NOT: return get_identifier ("not");
20485     case CPP_NOT_EQ: return get_identifier ("not_eq");
20486     case CPP_OR_OR: return get_identifier ("or");
20487     case CPP_OR_EQ: return get_identifier ("or_eq");
20488     case CPP_XOR: return get_identifier ("xor");
20489     case CPP_XOR_EQ: return get_identifier ("xor_eq");
20490     default: return token->u.value;
20491     }
20492 }
20493
20494 /* Parse an Objective-C params list.  */
20495
20496 static tree
20497 cp_parser_objc_method_keyword_params (cp_parser* parser)
20498 {
20499   tree params = NULL_TREE;
20500   bool maybe_unary_selector_p = true;
20501   cp_token *token = cp_lexer_peek_token (parser->lexer);
20502
20503   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20504     {
20505       tree selector = NULL_TREE, type_name, identifier;
20506
20507       if (token->type != CPP_COLON)
20508         selector = cp_parser_objc_selector (parser);
20509
20510       /* Detect if we have a unary selector.  */
20511       if (maybe_unary_selector_p
20512           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20513         return selector;
20514
20515       maybe_unary_selector_p = false;
20516       cp_parser_require (parser, CPP_COLON, "%<:%>");
20517       type_name = cp_parser_objc_typename (parser);
20518       identifier = cp_parser_identifier (parser);
20519
20520       params
20521         = chainon (params,
20522                    objc_build_keyword_decl (selector,
20523                                             type_name,
20524                                             identifier));
20525
20526       token = cp_lexer_peek_token (parser->lexer);
20527     }
20528
20529   return params;
20530 }
20531
20532 /* Parse the non-keyword Objective-C params.  */
20533
20534 static tree
20535 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20536 {
20537   tree params = make_node (TREE_LIST);
20538   cp_token *token = cp_lexer_peek_token (parser->lexer);
20539   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
20540
20541   while (token->type == CPP_COMMA)
20542     {
20543       cp_parameter_declarator *parmdecl;
20544       tree parm;
20545
20546       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20547       token = cp_lexer_peek_token (parser->lexer);
20548
20549       if (token->type == CPP_ELLIPSIS)
20550         {
20551           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
20552           *ellipsisp = true;
20553           break;
20554         }
20555
20556       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20557       parm = grokdeclarator (parmdecl->declarator,
20558                              &parmdecl->decl_specifiers,
20559                              PARM, /*initialized=*/0,
20560                              /*attrlist=*/NULL);
20561
20562       chainon (params, build_tree_list (NULL_TREE, parm));
20563       token = cp_lexer_peek_token (parser->lexer);
20564     }
20565
20566   return params;
20567 }
20568
20569 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
20570
20571 static void
20572 cp_parser_objc_interstitial_code (cp_parser* parser)
20573 {
20574   cp_token *token = cp_lexer_peek_token (parser->lexer);
20575
20576   /* If the next token is `extern' and the following token is a string
20577      literal, then we have a linkage specification.  */
20578   if (token->keyword == RID_EXTERN
20579       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20580     cp_parser_linkage_specification (parser);
20581   /* Handle #pragma, if any.  */
20582   else if (token->type == CPP_PRAGMA)
20583     cp_parser_pragma (parser, pragma_external);
20584   /* Allow stray semicolons.  */
20585   else if (token->type == CPP_SEMICOLON)
20586     cp_lexer_consume_token (parser->lexer);
20587   /* Finally, try to parse a block-declaration, or a function-definition.  */
20588   else
20589     cp_parser_block_declaration (parser, /*statement_p=*/false);
20590 }
20591
20592 /* Parse a method signature.  */
20593
20594 static tree
20595 cp_parser_objc_method_signature (cp_parser* parser)
20596 {
20597   tree rettype, kwdparms, optparms;
20598   bool ellipsis = false;
20599
20600   cp_parser_objc_method_type (parser);
20601   rettype = cp_parser_objc_typename (parser);
20602   kwdparms = cp_parser_objc_method_keyword_params (parser);
20603   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20604
20605   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20606 }
20607
20608 /* Pars an Objective-C method prototype list.  */
20609
20610 static void
20611 cp_parser_objc_method_prototype_list (cp_parser* parser)
20612 {
20613   cp_token *token = cp_lexer_peek_token (parser->lexer);
20614
20615   while (token->keyword != RID_AT_END)
20616     {
20617       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20618         {
20619           objc_add_method_declaration
20620            (cp_parser_objc_method_signature (parser));
20621           cp_parser_consume_semicolon_at_end_of_statement (parser);
20622         }
20623       else
20624         /* Allow for interspersed non-ObjC++ code.  */
20625         cp_parser_objc_interstitial_code (parser);
20626
20627       token = cp_lexer_peek_token (parser->lexer);
20628     }
20629
20630   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20631   objc_finish_interface ();
20632 }
20633
20634 /* Parse an Objective-C method definition list.  */
20635
20636 static void
20637 cp_parser_objc_method_definition_list (cp_parser* parser)
20638 {
20639   cp_token *token = cp_lexer_peek_token (parser->lexer);
20640
20641   while (token->keyword != RID_AT_END)
20642     {
20643       tree meth;
20644
20645       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20646         {
20647           push_deferring_access_checks (dk_deferred);
20648           objc_start_method_definition
20649            (cp_parser_objc_method_signature (parser));
20650
20651           /* For historical reasons, we accept an optional semicolon.  */
20652           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20653             cp_lexer_consume_token (parser->lexer);
20654
20655           perform_deferred_access_checks ();
20656           stop_deferring_access_checks ();
20657           meth = cp_parser_function_definition_after_declarator (parser,
20658                                                                  false);
20659           pop_deferring_access_checks ();
20660           objc_finish_method_definition (meth);
20661         }
20662       else
20663         /* Allow for interspersed non-ObjC++ code.  */
20664         cp_parser_objc_interstitial_code (parser);
20665
20666       token = cp_lexer_peek_token (parser->lexer);
20667     }
20668
20669   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20670   objc_finish_implementation ();
20671 }
20672
20673 /* Parse Objective-C ivars.  */
20674
20675 static void
20676 cp_parser_objc_class_ivars (cp_parser* parser)
20677 {
20678   cp_token *token = cp_lexer_peek_token (parser->lexer);
20679
20680   if (token->type != CPP_OPEN_BRACE)
20681     return;     /* No ivars specified.  */
20682
20683   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
20684   token = cp_lexer_peek_token (parser->lexer);
20685
20686   while (token->type != CPP_CLOSE_BRACE)
20687     {
20688       cp_decl_specifier_seq declspecs;
20689       int decl_class_or_enum_p;
20690       tree prefix_attributes;
20691
20692       cp_parser_objc_visibility_spec (parser);
20693
20694       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20695         break;
20696
20697       cp_parser_decl_specifier_seq (parser,
20698                                     CP_PARSER_FLAGS_OPTIONAL,
20699                                     &declspecs,
20700                                     &decl_class_or_enum_p);
20701       prefix_attributes = declspecs.attributes;
20702       declspecs.attributes = NULL_TREE;
20703
20704       /* Keep going until we hit the `;' at the end of the
20705          declaration.  */
20706       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20707         {
20708           tree width = NULL_TREE, attributes, first_attribute, decl;
20709           cp_declarator *declarator = NULL;
20710           int ctor_dtor_or_conv_p;
20711
20712           /* Check for a (possibly unnamed) bitfield declaration.  */
20713           token = cp_lexer_peek_token (parser->lexer);
20714           if (token->type == CPP_COLON)
20715             goto eat_colon;
20716
20717           if (token->type == CPP_NAME
20718               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20719                   == CPP_COLON))
20720             {
20721               /* Get the name of the bitfield.  */
20722               declarator = make_id_declarator (NULL_TREE,
20723                                                cp_parser_identifier (parser),
20724                                                sfk_none);
20725
20726              eat_colon:
20727               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20728               /* Get the width of the bitfield.  */
20729               width
20730                 = cp_parser_constant_expression (parser,
20731                                                  /*allow_non_constant=*/false,
20732                                                  NULL);
20733             }
20734           else
20735             {
20736               /* Parse the declarator.  */
20737               declarator
20738                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20739                                         &ctor_dtor_or_conv_p,
20740                                         /*parenthesized_p=*/NULL,
20741                                         /*member_p=*/false);
20742             }
20743
20744           /* Look for attributes that apply to the ivar.  */
20745           attributes = cp_parser_attributes_opt (parser);
20746           /* Remember which attributes are prefix attributes and
20747              which are not.  */
20748           first_attribute = attributes;
20749           /* Combine the attributes.  */
20750           attributes = chainon (prefix_attributes, attributes);
20751
20752           if (width)
20753               /* Create the bitfield declaration.  */
20754               decl = grokbitfield (declarator, &declspecs,
20755                                    width,
20756                                    attributes);
20757           else
20758             decl = grokfield (declarator, &declspecs,
20759                               NULL_TREE, /*init_const_expr_p=*/false,
20760                               NULL_TREE, attributes);
20761
20762           /* Add the instance variable.  */
20763           objc_add_instance_variable (decl);
20764
20765           /* Reset PREFIX_ATTRIBUTES.  */
20766           while (attributes && TREE_CHAIN (attributes) != first_attribute)
20767             attributes = TREE_CHAIN (attributes);
20768           if (attributes)
20769             TREE_CHAIN (attributes) = NULL_TREE;
20770
20771           token = cp_lexer_peek_token (parser->lexer);
20772
20773           if (token->type == CPP_COMMA)
20774             {
20775               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20776               continue;
20777             }
20778           break;
20779         }
20780
20781       cp_parser_consume_semicolon_at_end_of_statement (parser);
20782       token = cp_lexer_peek_token (parser->lexer);
20783     }
20784
20785   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
20786   /* For historical reasons, we accept an optional semicolon.  */
20787   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20788     cp_lexer_consume_token (parser->lexer);
20789 }
20790
20791 /* Parse an Objective-C protocol declaration.  */
20792
20793 static void
20794 cp_parser_objc_protocol_declaration (cp_parser* parser)
20795 {
20796   tree proto, protorefs;
20797   cp_token *tok;
20798
20799   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20800   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20801     {
20802       tok = cp_lexer_peek_token (parser->lexer);
20803       error_at (tok->location, "identifier expected after %<@protocol%>");
20804       goto finish;
20805     }
20806
20807   /* See if we have a forward declaration or a definition.  */
20808   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20809
20810   /* Try a forward declaration first.  */
20811   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20812     {
20813       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20814      finish:
20815       cp_parser_consume_semicolon_at_end_of_statement (parser);
20816     }
20817
20818   /* Ok, we got a full-fledged definition (or at least should).  */
20819   else
20820     {
20821       proto = cp_parser_identifier (parser);
20822       protorefs = cp_parser_objc_protocol_refs_opt (parser);
20823       objc_start_protocol (proto, protorefs);
20824       cp_parser_objc_method_prototype_list (parser);
20825     }
20826 }
20827
20828 /* Parse an Objective-C superclass or category.  */
20829
20830 static void
20831 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20832                                                           tree *categ)
20833 {
20834   cp_token *next = cp_lexer_peek_token (parser->lexer);
20835
20836   *super = *categ = NULL_TREE;
20837   if (next->type == CPP_COLON)
20838     {
20839       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20840       *super = cp_parser_identifier (parser);
20841     }
20842   else if (next->type == CPP_OPEN_PAREN)
20843     {
20844       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20845       *categ = cp_parser_identifier (parser);
20846       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20847     }
20848 }
20849
20850 /* Parse an Objective-C class interface.  */
20851
20852 static void
20853 cp_parser_objc_class_interface (cp_parser* parser)
20854 {
20855   tree name, super, categ, protos;
20856
20857   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20858   name = cp_parser_identifier (parser);
20859   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20860   protos = cp_parser_objc_protocol_refs_opt (parser);
20861
20862   /* We have either a class or a category on our hands.  */
20863   if (categ)
20864     objc_start_category_interface (name, categ, protos);
20865   else
20866     {
20867       objc_start_class_interface (name, super, protos);
20868       /* Handle instance variable declarations, if any.  */
20869       cp_parser_objc_class_ivars (parser);
20870       objc_continue_interface ();
20871     }
20872
20873   cp_parser_objc_method_prototype_list (parser);
20874 }
20875
20876 /* Parse an Objective-C class implementation.  */
20877
20878 static void
20879 cp_parser_objc_class_implementation (cp_parser* parser)
20880 {
20881   tree name, super, categ;
20882
20883   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20884   name = cp_parser_identifier (parser);
20885   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20886
20887   /* We have either a class or a category on our hands.  */
20888   if (categ)
20889     objc_start_category_implementation (name, categ);
20890   else
20891     {
20892       objc_start_class_implementation (name, super);
20893       /* Handle instance variable declarations, if any.  */
20894       cp_parser_objc_class_ivars (parser);
20895       objc_continue_implementation ();
20896     }
20897
20898   cp_parser_objc_method_definition_list (parser);
20899 }
20900
20901 /* Consume the @end token and finish off the implementation.  */
20902
20903 static void
20904 cp_parser_objc_end_implementation (cp_parser* parser)
20905 {
20906   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20907   objc_finish_implementation ();
20908 }
20909
20910 /* Parse an Objective-C declaration.  */
20911
20912 static void
20913 cp_parser_objc_declaration (cp_parser* parser)
20914 {
20915   /* Try to figure out what kind of declaration is present.  */
20916   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20917
20918   switch (kwd->keyword)
20919     {
20920     case RID_AT_ALIAS:
20921       cp_parser_objc_alias_declaration (parser);
20922       break;
20923     case RID_AT_CLASS:
20924       cp_parser_objc_class_declaration (parser);
20925       break;
20926     case RID_AT_PROTOCOL:
20927       cp_parser_objc_protocol_declaration (parser);
20928       break;
20929     case RID_AT_INTERFACE:
20930       cp_parser_objc_class_interface (parser);
20931       break;
20932     case RID_AT_IMPLEMENTATION:
20933       cp_parser_objc_class_implementation (parser);
20934       break;
20935     case RID_AT_END:
20936       cp_parser_objc_end_implementation (parser);
20937       break;
20938     default:
20939       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20940                 kwd->u.value);
20941       cp_parser_skip_to_end_of_block_or_statement (parser);
20942     }
20943 }
20944
20945 /* Parse an Objective-C try-catch-finally statement.
20946
20947    objc-try-catch-finally-stmt:
20948      @try compound-statement objc-catch-clause-seq [opt]
20949        objc-finally-clause [opt]
20950
20951    objc-catch-clause-seq:
20952      objc-catch-clause objc-catch-clause-seq [opt]
20953
20954    objc-catch-clause:
20955      @catch ( exception-declaration ) compound-statement
20956
20957    objc-finally-clause
20958      @finally compound-statement
20959
20960    Returns NULL_TREE.  */
20961
20962 static tree
20963 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20964   location_t location;
20965   tree stmt;
20966
20967   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20968   location = cp_lexer_peek_token (parser->lexer)->location;
20969   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20970      node, lest it get absorbed into the surrounding block.  */
20971   stmt = push_stmt_list ();
20972   cp_parser_compound_statement (parser, NULL, false);
20973   objc_begin_try_stmt (location, pop_stmt_list (stmt));
20974
20975   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20976     {
20977       cp_parameter_declarator *parmdecl;
20978       tree parm;
20979
20980       cp_lexer_consume_token (parser->lexer);
20981       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20982       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20983       parm = grokdeclarator (parmdecl->declarator,
20984                              &parmdecl->decl_specifiers,
20985                              PARM, /*initialized=*/0,
20986                              /*attrlist=*/NULL);
20987       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20988       objc_begin_catch_clause (parm);
20989       cp_parser_compound_statement (parser, NULL, false);
20990       objc_finish_catch_clause ();
20991     }
20992
20993   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20994     {
20995       cp_lexer_consume_token (parser->lexer);
20996       location = cp_lexer_peek_token (parser->lexer)->location;
20997       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20998          node, lest it get absorbed into the surrounding block.  */
20999       stmt = push_stmt_list ();
21000       cp_parser_compound_statement (parser, NULL, false);
21001       objc_build_finally_clause (location, pop_stmt_list (stmt));
21002     }
21003
21004   return objc_finish_try_stmt ();
21005 }
21006
21007 /* Parse an Objective-C synchronized statement.
21008
21009    objc-synchronized-stmt:
21010      @synchronized ( expression ) compound-statement
21011
21012    Returns NULL_TREE.  */
21013
21014 static tree
21015 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21016   location_t location;
21017   tree lock, stmt;
21018
21019   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21020
21021   location = cp_lexer_peek_token (parser->lexer)->location;
21022   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21023   lock = cp_parser_expression (parser, false, NULL);
21024   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21025
21026   /* NB: The @synchronized 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
21031   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21032 }
21033
21034 /* Parse an Objective-C throw statement.
21035
21036    objc-throw-stmt:
21037      @throw assignment-expression [opt] ;
21038
21039    Returns a constructed '@throw' statement.  */
21040
21041 static tree
21042 cp_parser_objc_throw_statement (cp_parser *parser) {
21043   tree expr = NULL_TREE;
21044   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21045
21046   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21047
21048   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21049     expr = cp_parser_assignment_expression (parser, false, NULL);
21050
21051   cp_parser_consume_semicolon_at_end_of_statement (parser);
21052
21053   return objc_build_throw_stmt (loc, expr);
21054 }
21055
21056 /* Parse an Objective-C statement.  */
21057
21058 static tree
21059 cp_parser_objc_statement (cp_parser * parser) {
21060   /* Try to figure out what kind of declaration is present.  */
21061   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21062
21063   switch (kwd->keyword)
21064     {
21065     case RID_AT_TRY:
21066       return cp_parser_objc_try_catch_finally_statement (parser);
21067     case RID_AT_SYNCHRONIZED:
21068       return cp_parser_objc_synchronized_statement (parser);
21069     case RID_AT_THROW:
21070       return cp_parser_objc_throw_statement (parser);
21071     default:
21072       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21073                kwd->u.value);
21074       cp_parser_skip_to_end_of_block_or_statement (parser);
21075     }
21076
21077   return error_mark_node;
21078 }
21079 \f
21080 /* OpenMP 2.5 parsing routines.  */
21081
21082 /* Returns name of the next clause.
21083    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21084    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
21085    returned and the token is consumed.  */
21086
21087 static pragma_omp_clause
21088 cp_parser_omp_clause_name (cp_parser *parser)
21089 {
21090   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21091
21092   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21093     result = PRAGMA_OMP_CLAUSE_IF;
21094   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21095     result = PRAGMA_OMP_CLAUSE_DEFAULT;
21096   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21097     result = PRAGMA_OMP_CLAUSE_PRIVATE;
21098   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21099     {
21100       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21101       const char *p = IDENTIFIER_POINTER (id);
21102
21103       switch (p[0])
21104         {
21105         case 'c':
21106           if (!strcmp ("collapse", p))
21107             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21108           else if (!strcmp ("copyin", p))
21109             result = PRAGMA_OMP_CLAUSE_COPYIN;
21110           else if (!strcmp ("copyprivate", p))
21111             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21112           break;
21113         case 'f':
21114           if (!strcmp ("firstprivate", p))
21115             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21116           break;
21117         case 'l':
21118           if (!strcmp ("lastprivate", p))
21119             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21120           break;
21121         case 'n':
21122           if (!strcmp ("nowait", p))
21123             result = PRAGMA_OMP_CLAUSE_NOWAIT;
21124           else if (!strcmp ("num_threads", p))
21125             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21126           break;
21127         case 'o':
21128           if (!strcmp ("ordered", p))
21129             result = PRAGMA_OMP_CLAUSE_ORDERED;
21130           break;
21131         case 'r':
21132           if (!strcmp ("reduction", p))
21133             result = PRAGMA_OMP_CLAUSE_REDUCTION;
21134           break;
21135         case 's':
21136           if (!strcmp ("schedule", p))
21137             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21138           else if (!strcmp ("shared", p))
21139             result = PRAGMA_OMP_CLAUSE_SHARED;
21140           break;
21141         case 'u':
21142           if (!strcmp ("untied", p))
21143             result = PRAGMA_OMP_CLAUSE_UNTIED;
21144           break;
21145         }
21146     }
21147
21148   if (result != PRAGMA_OMP_CLAUSE_NONE)
21149     cp_lexer_consume_token (parser->lexer);
21150
21151   return result;
21152 }
21153
21154 /* Validate that a clause of the given type does not already exist.  */
21155
21156 static void
21157 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21158                            const char *name, location_t location)
21159 {
21160   tree c;
21161
21162   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21163     if (OMP_CLAUSE_CODE (c) == code)
21164       {
21165         error_at (location, "too many %qs clauses", name);
21166         break;
21167       }
21168 }
21169
21170 /* OpenMP 2.5:
21171    variable-list:
21172      identifier
21173      variable-list , identifier
21174
21175    In addition, we match a closing parenthesis.  An opening parenthesis
21176    will have been consumed by the caller.
21177
21178    If KIND is nonzero, create the appropriate node and install the decl
21179    in OMP_CLAUSE_DECL and add the node to the head of the list.
21180
21181    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21182    return the list created.  */
21183
21184 static tree
21185 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21186                                 tree list)
21187 {
21188   cp_token *token;
21189   while (1)
21190     {
21191       tree name, decl;
21192
21193       token = cp_lexer_peek_token (parser->lexer);
21194       name = cp_parser_id_expression (parser, /*template_p=*/false,
21195                                       /*check_dependency_p=*/true,
21196                                       /*template_p=*/NULL,
21197                                       /*declarator_p=*/false,
21198                                       /*optional_p=*/false);
21199       if (name == error_mark_node)
21200         goto skip_comma;
21201
21202       decl = cp_parser_lookup_name_simple (parser, name, token->location);
21203       if (decl == error_mark_node)
21204         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21205       else if (kind != 0)
21206         {
21207           tree u = build_omp_clause (token->location, kind);
21208           OMP_CLAUSE_DECL (u) = decl;
21209           OMP_CLAUSE_CHAIN (u) = list;
21210           list = u;
21211         }
21212       else
21213         list = tree_cons (decl, NULL_TREE, list);
21214
21215     get_comma:
21216       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21217         break;
21218       cp_lexer_consume_token (parser->lexer);
21219     }
21220
21221   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21222     {
21223       int ending;
21224
21225       /* Try to resync to an unnested comma.  Copied from
21226          cp_parser_parenthesized_expression_list.  */
21227     skip_comma:
21228       ending = cp_parser_skip_to_closing_parenthesis (parser,
21229                                                       /*recovering=*/true,
21230                                                       /*or_comma=*/true,
21231                                                       /*consume_paren=*/true);
21232       if (ending < 0)
21233         goto get_comma;
21234     }
21235
21236   return list;
21237 }
21238
21239 /* Similarly, but expect leading and trailing parenthesis.  This is a very
21240    common case for omp clauses.  */
21241
21242 static tree
21243 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21244 {
21245   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21246     return cp_parser_omp_var_list_no_open (parser, kind, list);
21247   return list;
21248 }
21249
21250 /* OpenMP 3.0:
21251    collapse ( constant-expression ) */
21252
21253 static tree
21254 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21255 {
21256   tree c, num;
21257   location_t loc;
21258   HOST_WIDE_INT n;
21259
21260   loc = cp_lexer_peek_token (parser->lexer)->location;
21261   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21262     return list;
21263
21264   num = cp_parser_constant_expression (parser, false, NULL);
21265
21266   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21267     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21268                                            /*or_comma=*/false,
21269                                            /*consume_paren=*/true);
21270
21271   if (num == error_mark_node)
21272     return list;
21273   num = fold_non_dependent_expr (num);
21274   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21275       || !host_integerp (num, 0)
21276       || (n = tree_low_cst (num, 0)) <= 0
21277       || (int) n != n)
21278     {
21279       error_at (loc, "collapse argument needs positive constant integer expression");
21280       return list;
21281     }
21282
21283   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21284   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21285   OMP_CLAUSE_CHAIN (c) = list;
21286   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21287
21288   return c;
21289 }
21290
21291 /* OpenMP 2.5:
21292    default ( shared | none ) */
21293
21294 static tree
21295 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21296 {
21297   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21298   tree c;
21299
21300   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21301     return list;
21302   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21303     {
21304       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21305       const char *p = IDENTIFIER_POINTER (id);
21306
21307       switch (p[0])
21308         {
21309         case 'n':
21310           if (strcmp ("none", p) != 0)
21311             goto invalid_kind;
21312           kind = OMP_CLAUSE_DEFAULT_NONE;
21313           break;
21314
21315         case 's':
21316           if (strcmp ("shared", p) != 0)
21317             goto invalid_kind;
21318           kind = OMP_CLAUSE_DEFAULT_SHARED;
21319           break;
21320
21321         default:
21322           goto invalid_kind;
21323         }
21324
21325       cp_lexer_consume_token (parser->lexer);
21326     }
21327   else
21328     {
21329     invalid_kind:
21330       cp_parser_error (parser, "expected %<none%> or %<shared%>");
21331     }
21332
21333   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21334     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21335                                            /*or_comma=*/false,
21336                                            /*consume_paren=*/true);
21337
21338   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21339     return list;
21340
21341   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21342   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21343   OMP_CLAUSE_CHAIN (c) = list;
21344   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21345
21346   return c;
21347 }
21348
21349 /* OpenMP 2.5:
21350    if ( expression ) */
21351
21352 static tree
21353 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21354 {
21355   tree t, c;
21356
21357   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21358     return list;
21359
21360   t = cp_parser_condition (parser);
21361
21362   if (t == error_mark_node
21363       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21364     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21365                                            /*or_comma=*/false,
21366                                            /*consume_paren=*/true);
21367
21368   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21369
21370   c = build_omp_clause (location, OMP_CLAUSE_IF);
21371   OMP_CLAUSE_IF_EXPR (c) = t;
21372   OMP_CLAUSE_CHAIN (c) = list;
21373
21374   return c;
21375 }
21376
21377 /* OpenMP 2.5:
21378    nowait */
21379
21380 static tree
21381 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21382                              tree list, location_t location)
21383 {
21384   tree c;
21385
21386   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21387
21388   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21389   OMP_CLAUSE_CHAIN (c) = list;
21390   return c;
21391 }
21392
21393 /* OpenMP 2.5:
21394    num_threads ( expression ) */
21395
21396 static tree
21397 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21398                                   location_t location)
21399 {
21400   tree t, c;
21401
21402   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21403     return list;
21404
21405   t = cp_parser_expression (parser, false, NULL);
21406
21407   if (t == error_mark_node
21408       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21409     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21410                                            /*or_comma=*/false,
21411                                            /*consume_paren=*/true);
21412
21413   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21414                              "num_threads", location);
21415
21416   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21417   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21418   OMP_CLAUSE_CHAIN (c) = list;
21419
21420   return c;
21421 }
21422
21423 /* OpenMP 2.5:
21424    ordered */
21425
21426 static tree
21427 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21428                               tree list, location_t location)
21429 {
21430   tree c;
21431
21432   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21433                              "ordered", location);
21434
21435   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21436   OMP_CLAUSE_CHAIN (c) = list;
21437   return c;
21438 }
21439
21440 /* OpenMP 2.5:
21441    reduction ( reduction-operator : variable-list )
21442
21443    reduction-operator:
21444      One of: + * - & ^ | && || */
21445
21446 static tree
21447 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21448 {
21449   enum tree_code code;
21450   tree nlist, c;
21451
21452   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21453     return list;
21454
21455   switch (cp_lexer_peek_token (parser->lexer)->type)
21456     {
21457     case CPP_PLUS:
21458       code = PLUS_EXPR;
21459       break;
21460     case CPP_MULT:
21461       code = MULT_EXPR;
21462       break;
21463     case CPP_MINUS:
21464       code = MINUS_EXPR;
21465       break;
21466     case CPP_AND:
21467       code = BIT_AND_EXPR;
21468       break;
21469     case CPP_XOR:
21470       code = BIT_XOR_EXPR;
21471       break;
21472     case CPP_OR:
21473       code = BIT_IOR_EXPR;
21474       break;
21475     case CPP_AND_AND:
21476       code = TRUTH_ANDIF_EXPR;
21477       break;
21478     case CPP_OR_OR:
21479       code = TRUTH_ORIF_EXPR;
21480       break;
21481     default:
21482       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21483                                "%<|%>, %<&&%>, or %<||%>");
21484     resync_fail:
21485       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21486                                              /*or_comma=*/false,
21487                                              /*consume_paren=*/true);
21488       return list;
21489     }
21490   cp_lexer_consume_token (parser->lexer);
21491
21492   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21493     goto resync_fail;
21494
21495   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21496   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21497     OMP_CLAUSE_REDUCTION_CODE (c) = code;
21498
21499   return nlist;
21500 }
21501
21502 /* OpenMP 2.5:
21503    schedule ( schedule-kind )
21504    schedule ( schedule-kind , expression )
21505
21506    schedule-kind:
21507      static | dynamic | guided | runtime | auto  */
21508
21509 static tree
21510 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21511 {
21512   tree c, t;
21513
21514   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21515     return list;
21516
21517   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21518
21519   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21520     {
21521       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21522       const char *p = IDENTIFIER_POINTER (id);
21523
21524       switch (p[0])
21525         {
21526         case 'd':
21527           if (strcmp ("dynamic", p) != 0)
21528             goto invalid_kind;
21529           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21530           break;
21531
21532         case 'g':
21533           if (strcmp ("guided", p) != 0)
21534             goto invalid_kind;
21535           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21536           break;
21537
21538         case 'r':
21539           if (strcmp ("runtime", p) != 0)
21540             goto invalid_kind;
21541           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21542           break;
21543
21544         default:
21545           goto invalid_kind;
21546         }
21547     }
21548   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21549     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21550   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21551     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21552   else
21553     goto invalid_kind;
21554   cp_lexer_consume_token (parser->lexer);
21555
21556   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21557     {
21558       cp_token *token;
21559       cp_lexer_consume_token (parser->lexer);
21560
21561       token = cp_lexer_peek_token (parser->lexer);
21562       t = cp_parser_assignment_expression (parser, false, NULL);
21563
21564       if (t == error_mark_node)
21565         goto resync_fail;
21566       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21567         error_at (token->location, "schedule %<runtime%> does not take "
21568                   "a %<chunk_size%> parameter");
21569       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21570         error_at (token->location, "schedule %<auto%> does not take "
21571                   "a %<chunk_size%> parameter");
21572       else
21573         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21574
21575       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21576         goto resync_fail;
21577     }
21578   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21579     goto resync_fail;
21580
21581   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21582   OMP_CLAUSE_CHAIN (c) = list;
21583   return c;
21584
21585  invalid_kind:
21586   cp_parser_error (parser, "invalid schedule kind");
21587  resync_fail:
21588   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21589                                          /*or_comma=*/false,
21590                                          /*consume_paren=*/true);
21591   return list;
21592 }
21593
21594 /* OpenMP 3.0:
21595    untied */
21596
21597 static tree
21598 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21599                              tree list, location_t location)
21600 {
21601   tree c;
21602
21603   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21604
21605   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21606   OMP_CLAUSE_CHAIN (c) = list;
21607   return c;
21608 }
21609
21610 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
21611    is a bitmask in MASK.  Return the list of clauses found; the result
21612    of clause default goes in *pdefault.  */
21613
21614 static tree
21615 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21616                            const char *where, cp_token *pragma_tok)
21617 {
21618   tree clauses = NULL;
21619   bool first = true;
21620   cp_token *token = NULL;
21621
21622   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21623     {
21624       pragma_omp_clause c_kind;
21625       const char *c_name;
21626       tree prev = clauses;
21627
21628       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21629         cp_lexer_consume_token (parser->lexer);
21630
21631       token = cp_lexer_peek_token (parser->lexer);
21632       c_kind = cp_parser_omp_clause_name (parser);
21633       first = false;
21634
21635       switch (c_kind)
21636         {
21637         case PRAGMA_OMP_CLAUSE_COLLAPSE:
21638           clauses = cp_parser_omp_clause_collapse (parser, clauses,
21639                                                    token->location);
21640           c_name = "collapse";
21641           break;
21642         case PRAGMA_OMP_CLAUSE_COPYIN:
21643           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21644           c_name = "copyin";
21645           break;
21646         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21647           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21648                                             clauses);
21649           c_name = "copyprivate";
21650           break;
21651         case PRAGMA_OMP_CLAUSE_DEFAULT:
21652           clauses = cp_parser_omp_clause_default (parser, clauses,
21653                                                   token->location);
21654           c_name = "default";
21655           break;
21656         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21657           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21658                                             clauses);
21659           c_name = "firstprivate";
21660           break;
21661         case PRAGMA_OMP_CLAUSE_IF:
21662           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21663           c_name = "if";
21664           break;
21665         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21666           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21667                                             clauses);
21668           c_name = "lastprivate";
21669           break;
21670         case PRAGMA_OMP_CLAUSE_NOWAIT:
21671           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21672           c_name = "nowait";
21673           break;
21674         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21675           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21676                                                       token->location);
21677           c_name = "num_threads";
21678           break;
21679         case PRAGMA_OMP_CLAUSE_ORDERED:
21680           clauses = cp_parser_omp_clause_ordered (parser, clauses,
21681                                                   token->location);
21682           c_name = "ordered";
21683           break;
21684         case PRAGMA_OMP_CLAUSE_PRIVATE:
21685           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21686                                             clauses);
21687           c_name = "private";
21688           break;
21689         case PRAGMA_OMP_CLAUSE_REDUCTION:
21690           clauses = cp_parser_omp_clause_reduction (parser, clauses);
21691           c_name = "reduction";
21692           break;
21693         case PRAGMA_OMP_CLAUSE_SCHEDULE:
21694           clauses = cp_parser_omp_clause_schedule (parser, clauses,
21695                                                    token->location);
21696           c_name = "schedule";
21697           break;
21698         case PRAGMA_OMP_CLAUSE_SHARED:
21699           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21700                                             clauses);
21701           c_name = "shared";
21702           break;
21703         case PRAGMA_OMP_CLAUSE_UNTIED:
21704           clauses = cp_parser_omp_clause_untied (parser, clauses,
21705                                                  token->location);
21706           c_name = "nowait";
21707           break;
21708         default:
21709           cp_parser_error (parser, "expected %<#pragma omp%> clause");
21710           goto saw_error;
21711         }
21712
21713       if (((mask >> c_kind) & 1) == 0)
21714         {
21715           /* Remove the invalid clause(s) from the list to avoid
21716              confusing the rest of the compiler.  */
21717           clauses = prev;
21718           error_at (token->location, "%qs is not valid for %qs", c_name, where);
21719         }
21720     }
21721  saw_error:
21722   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21723   return finish_omp_clauses (clauses);
21724 }
21725
21726 /* OpenMP 2.5:
21727    structured-block:
21728      statement
21729
21730    In practice, we're also interested in adding the statement to an
21731    outer node.  So it is convenient if we work around the fact that
21732    cp_parser_statement calls add_stmt.  */
21733
21734 static unsigned
21735 cp_parser_begin_omp_structured_block (cp_parser *parser)
21736 {
21737   unsigned save = parser->in_statement;
21738
21739   /* Only move the values to IN_OMP_BLOCK if they weren't false.
21740      This preserves the "not within loop or switch" style error messages
21741      for nonsense cases like
21742         void foo() {
21743         #pragma omp single
21744           break;
21745         }
21746   */
21747   if (parser->in_statement)
21748     parser->in_statement = IN_OMP_BLOCK;
21749
21750   return save;
21751 }
21752
21753 static void
21754 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21755 {
21756   parser->in_statement = save;
21757 }
21758
21759 static tree
21760 cp_parser_omp_structured_block (cp_parser *parser)
21761 {
21762   tree stmt = begin_omp_structured_block ();
21763   unsigned int save = cp_parser_begin_omp_structured_block (parser);
21764
21765   cp_parser_statement (parser, NULL_TREE, false, NULL);
21766
21767   cp_parser_end_omp_structured_block (parser, save);
21768   return finish_omp_structured_block (stmt);
21769 }
21770
21771 /* OpenMP 2.5:
21772    # pragma omp atomic new-line
21773      expression-stmt
21774
21775    expression-stmt:
21776      x binop= expr | x++ | ++x | x-- | --x
21777    binop:
21778      +, *, -, /, &, ^, |, <<, >>
21779
21780   where x is an lvalue expression with scalar type.  */
21781
21782 static void
21783 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21784 {
21785   tree lhs, rhs;
21786   enum tree_code code;
21787
21788   cp_parser_require_pragma_eol (parser, pragma_tok);
21789
21790   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21791                                     /*cast_p=*/false, NULL);
21792   switch (TREE_CODE (lhs))
21793     {
21794     case ERROR_MARK:
21795       goto saw_error;
21796
21797     case PREINCREMENT_EXPR:
21798     case POSTINCREMENT_EXPR:
21799       lhs = TREE_OPERAND (lhs, 0);
21800       code = PLUS_EXPR;
21801       rhs = integer_one_node;
21802       break;
21803
21804     case PREDECREMENT_EXPR:
21805     case POSTDECREMENT_EXPR:
21806       lhs = TREE_OPERAND (lhs, 0);
21807       code = MINUS_EXPR;
21808       rhs = integer_one_node;
21809       break;
21810
21811     default:
21812       switch (cp_lexer_peek_token (parser->lexer)->type)
21813         {
21814         case CPP_MULT_EQ:
21815           code = MULT_EXPR;
21816           break;
21817         case CPP_DIV_EQ:
21818           code = TRUNC_DIV_EXPR;
21819           break;
21820         case CPP_PLUS_EQ:
21821           code = PLUS_EXPR;
21822           break;
21823         case CPP_MINUS_EQ:
21824           code = MINUS_EXPR;
21825           break;
21826         case CPP_LSHIFT_EQ:
21827           code = LSHIFT_EXPR;
21828           break;
21829         case CPP_RSHIFT_EQ:
21830           code = RSHIFT_EXPR;
21831           break;
21832         case CPP_AND_EQ:
21833           code = BIT_AND_EXPR;
21834           break;
21835         case CPP_OR_EQ:
21836           code = BIT_IOR_EXPR;
21837           break;
21838         case CPP_XOR_EQ:
21839           code = BIT_XOR_EXPR;
21840           break;
21841         default:
21842           cp_parser_error (parser,
21843                            "invalid operator for %<#pragma omp atomic%>");
21844           goto saw_error;
21845         }
21846       cp_lexer_consume_token (parser->lexer);
21847
21848       rhs = cp_parser_expression (parser, false, NULL);
21849       if (rhs == error_mark_node)
21850         goto saw_error;
21851       break;
21852     }
21853   finish_omp_atomic (code, lhs, rhs);
21854   cp_parser_consume_semicolon_at_end_of_statement (parser);
21855   return;
21856
21857  saw_error:
21858   cp_parser_skip_to_end_of_block_or_statement (parser);
21859 }
21860
21861
21862 /* OpenMP 2.5:
21863    # pragma omp barrier new-line  */
21864
21865 static void
21866 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21867 {
21868   cp_parser_require_pragma_eol (parser, pragma_tok);
21869   finish_omp_barrier ();
21870 }
21871
21872 /* OpenMP 2.5:
21873    # pragma omp critical [(name)] new-line
21874      structured-block  */
21875
21876 static tree
21877 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21878 {
21879   tree stmt, name = NULL;
21880
21881   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21882     {
21883       cp_lexer_consume_token (parser->lexer);
21884
21885       name = cp_parser_identifier (parser);
21886
21887       if (name == error_mark_node
21888           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21889         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21890                                                /*or_comma=*/false,
21891                                                /*consume_paren=*/true);
21892       if (name == error_mark_node)
21893         name = NULL;
21894     }
21895   cp_parser_require_pragma_eol (parser, pragma_tok);
21896
21897   stmt = cp_parser_omp_structured_block (parser);
21898   return c_finish_omp_critical (input_location, stmt, name);
21899 }
21900
21901 /* OpenMP 2.5:
21902    # pragma omp flush flush-vars[opt] new-line
21903
21904    flush-vars:
21905      ( variable-list ) */
21906
21907 static void
21908 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21909 {
21910   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21911     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21912   cp_parser_require_pragma_eol (parser, pragma_tok);
21913
21914   finish_omp_flush ();
21915 }
21916
21917 /* Helper function, to parse omp for increment expression.  */
21918
21919 static tree
21920 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21921 {
21922   tree cond = cp_parser_binary_expression (parser, false, true,
21923                                            PREC_NOT_OPERATOR, NULL);
21924   bool overloaded_p;
21925
21926   if (cond == error_mark_node
21927       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21928     {
21929       cp_parser_skip_to_end_of_statement (parser);
21930       return error_mark_node;
21931     }
21932
21933   switch (TREE_CODE (cond))
21934     {
21935     case GT_EXPR:
21936     case GE_EXPR:
21937     case LT_EXPR:
21938     case LE_EXPR:
21939       break;
21940     default:
21941       return error_mark_node;
21942     }
21943
21944   /* If decl is an iterator, preserve LHS and RHS of the relational
21945      expr until finish_omp_for.  */
21946   if (decl
21947       && (type_dependent_expression_p (decl)
21948           || CLASS_TYPE_P (TREE_TYPE (decl))))
21949     return cond;
21950
21951   return build_x_binary_op (TREE_CODE (cond),
21952                             TREE_OPERAND (cond, 0), ERROR_MARK,
21953                             TREE_OPERAND (cond, 1), ERROR_MARK,
21954                             &overloaded_p, tf_warning_or_error);
21955 }
21956
21957 /* Helper function, to parse omp for increment expression.  */
21958
21959 static tree
21960 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21961 {
21962   cp_token *token = cp_lexer_peek_token (parser->lexer);
21963   enum tree_code op;
21964   tree lhs, rhs;
21965   cp_id_kind idk;
21966   bool decl_first;
21967
21968   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21969     {
21970       op = (token->type == CPP_PLUS_PLUS
21971             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21972       cp_lexer_consume_token (parser->lexer);
21973       lhs = cp_parser_cast_expression (parser, false, false, NULL);
21974       if (lhs != decl)
21975         return error_mark_node;
21976       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21977     }
21978
21979   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21980   if (lhs != decl)
21981     return error_mark_node;
21982
21983   token = cp_lexer_peek_token (parser->lexer);
21984   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21985     {
21986       op = (token->type == CPP_PLUS_PLUS
21987             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21988       cp_lexer_consume_token (parser->lexer);
21989       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21990     }
21991
21992   op = cp_parser_assignment_operator_opt (parser);
21993   if (op == ERROR_MARK)
21994     return error_mark_node;
21995
21996   if (op != NOP_EXPR)
21997     {
21998       rhs = cp_parser_assignment_expression (parser, false, NULL);
21999       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22000       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22001     }
22002
22003   lhs = cp_parser_binary_expression (parser, false, false,
22004                                      PREC_ADDITIVE_EXPRESSION, NULL);
22005   token = cp_lexer_peek_token (parser->lexer);
22006   decl_first = lhs == decl;
22007   if (decl_first)
22008     lhs = NULL_TREE;
22009   if (token->type != CPP_PLUS
22010       && token->type != CPP_MINUS)
22011     return error_mark_node;
22012
22013   do
22014     {
22015       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22016       cp_lexer_consume_token (parser->lexer);
22017       rhs = cp_parser_binary_expression (parser, false, false,
22018                                          PREC_ADDITIVE_EXPRESSION, NULL);
22019       token = cp_lexer_peek_token (parser->lexer);
22020       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22021         {
22022           if (lhs == NULL_TREE)
22023             {
22024               if (op == PLUS_EXPR)
22025                 lhs = rhs;
22026               else
22027                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22028             }
22029           else
22030             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22031                                      NULL, tf_warning_or_error);
22032         }
22033     }
22034   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22035
22036   if (!decl_first)
22037     {
22038       if (rhs != decl || op == MINUS_EXPR)
22039         return error_mark_node;
22040       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22041     }
22042   else
22043     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22044
22045   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22046 }
22047
22048 /* Parse the restricted form of the for statement allowed by OpenMP.  */
22049
22050 static tree
22051 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22052 {
22053   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22054   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22055   tree this_pre_body, cl;
22056   location_t loc_first;
22057   bool collapse_err = false;
22058   int i, collapse = 1, nbraces = 0;
22059
22060   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22061     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22062       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22063
22064   gcc_assert (collapse >= 1);
22065
22066   declv = make_tree_vec (collapse);
22067   initv = make_tree_vec (collapse);
22068   condv = make_tree_vec (collapse);
22069   incrv = make_tree_vec (collapse);
22070
22071   loc_first = cp_lexer_peek_token (parser->lexer)->location;
22072
22073   for (i = 0; i < collapse; i++)
22074     {
22075       int bracecount = 0;
22076       bool add_private_clause = false;
22077       location_t loc;
22078
22079       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22080         {
22081           cp_parser_error (parser, "for statement expected");
22082           return NULL;
22083         }
22084       loc = cp_lexer_consume_token (parser->lexer)->location;
22085
22086       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22087         return NULL;
22088
22089       init = decl = real_decl = NULL;
22090       this_pre_body = push_stmt_list ();
22091       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22092         {
22093           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22094
22095              init-expr:
22096                        var = lb
22097                        integer-type var = lb
22098                        random-access-iterator-type var = lb
22099                        pointer-type var = lb
22100           */
22101           cp_decl_specifier_seq type_specifiers;
22102
22103           /* First, try to parse as an initialized declaration.  See
22104              cp_parser_condition, from whence the bulk of this is copied.  */
22105
22106           cp_parser_parse_tentatively (parser);
22107           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
22108                                         /*is_trailing_return=*/false,
22109                                         &type_specifiers);
22110           if (cp_parser_parse_definitely (parser))
22111             {
22112               /* If parsing a type specifier seq succeeded, then this
22113                  MUST be a initialized declaration.  */
22114               tree asm_specification, attributes;
22115               cp_declarator *declarator;
22116
22117               declarator = cp_parser_declarator (parser,
22118                                                  CP_PARSER_DECLARATOR_NAMED,
22119                                                  /*ctor_dtor_or_conv_p=*/NULL,
22120                                                  /*parenthesized_p=*/NULL,
22121                                                  /*member_p=*/false);
22122               attributes = cp_parser_attributes_opt (parser);
22123               asm_specification = cp_parser_asm_specification_opt (parser);
22124
22125               if (declarator == cp_error_declarator) 
22126                 cp_parser_skip_to_end_of_statement (parser);
22127
22128               else 
22129                 {
22130                   tree pushed_scope, auto_node;
22131
22132                   decl = start_decl (declarator, &type_specifiers,
22133                                      SD_INITIALIZED, attributes,
22134                                      /*prefix_attributes=*/NULL_TREE,
22135                                      &pushed_scope);
22136
22137                   auto_node = type_uses_auto (TREE_TYPE (decl));
22138                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22139                     {
22140                       if (cp_lexer_next_token_is (parser->lexer, 
22141                                                   CPP_OPEN_PAREN))
22142                         error ("parenthesized initialization is not allowed in "
22143                                "OpenMP %<for%> loop");
22144                       else
22145                         /* Trigger an error.  */
22146                         cp_parser_require (parser, CPP_EQ, "%<=%>");
22147
22148                       init = error_mark_node;
22149                       cp_parser_skip_to_end_of_statement (parser);
22150                     }
22151                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
22152                            || type_dependent_expression_p (decl)
22153                            || auto_node)
22154                     {
22155                       bool is_direct_init, is_non_constant_init;
22156
22157                       init = cp_parser_initializer (parser,
22158                                                     &is_direct_init,
22159                                                     &is_non_constant_init);
22160
22161                       if (auto_node && describable_type (init))
22162                         {
22163                           TREE_TYPE (decl)
22164                             = do_auto_deduction (TREE_TYPE (decl), init,
22165                                                  auto_node);
22166
22167                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
22168                               && !type_dependent_expression_p (decl))
22169                             goto non_class;
22170                         }
22171                       
22172                       cp_finish_decl (decl, init, !is_non_constant_init,
22173                                       asm_specification,
22174                                       LOOKUP_ONLYCONVERTING);
22175                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
22176                         {
22177                           for_block
22178                             = tree_cons (NULL, this_pre_body, for_block);
22179                           init = NULL_TREE;
22180                         }
22181                       else
22182                         init = pop_stmt_list (this_pre_body);
22183                       this_pre_body = NULL_TREE;
22184                     }
22185                   else
22186                     {
22187                       /* Consume '='.  */
22188                       cp_lexer_consume_token (parser->lexer);
22189                       init = cp_parser_assignment_expression (parser, false, NULL);
22190
22191                     non_class:
22192                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22193                         init = error_mark_node;
22194                       else
22195                         cp_finish_decl (decl, NULL_TREE,
22196                                         /*init_const_expr_p=*/false,
22197                                         asm_specification,
22198                                         LOOKUP_ONLYCONVERTING);
22199                     }
22200
22201                   if (pushed_scope)
22202                     pop_scope (pushed_scope);
22203                 }
22204             }
22205           else 
22206             {
22207               cp_id_kind idk;
22208               /* If parsing a type specifier sequence failed, then
22209                  this MUST be a simple expression.  */
22210               cp_parser_parse_tentatively (parser);
22211               decl = cp_parser_primary_expression (parser, false, false,
22212                                                    false, &idk);
22213               if (!cp_parser_error_occurred (parser)
22214                   && decl
22215                   && DECL_P (decl)
22216                   && CLASS_TYPE_P (TREE_TYPE (decl)))
22217                 {
22218                   tree rhs;
22219
22220                   cp_parser_parse_definitely (parser);
22221                   cp_parser_require (parser, CPP_EQ, "%<=%>");
22222                   rhs = cp_parser_assignment_expression (parser, false, NULL);
22223                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22224                                                          rhs,
22225                                                          tf_warning_or_error));
22226                   add_private_clause = true;
22227                 }
22228               else
22229                 {
22230                   decl = NULL;
22231                   cp_parser_abort_tentative_parse (parser);
22232                   init = cp_parser_expression (parser, false, NULL);
22233                   if (init)
22234                     {
22235                       if (TREE_CODE (init) == MODIFY_EXPR
22236                           || TREE_CODE (init) == MODOP_EXPR)
22237                         real_decl = TREE_OPERAND (init, 0);
22238                     }
22239                 }
22240             }
22241         }
22242       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22243       if (this_pre_body)
22244         {
22245           this_pre_body = pop_stmt_list (this_pre_body);
22246           if (pre_body)
22247             {
22248               tree t = pre_body;
22249               pre_body = push_stmt_list ();
22250               add_stmt (t);
22251               add_stmt (this_pre_body);
22252               pre_body = pop_stmt_list (pre_body);
22253             }
22254           else
22255             pre_body = this_pre_body;
22256         }
22257
22258       if (decl)
22259         real_decl = decl;
22260       if (par_clauses != NULL && real_decl != NULL_TREE)
22261         {
22262           tree *c;
22263           for (c = par_clauses; *c ; )
22264             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22265                 && OMP_CLAUSE_DECL (*c) == real_decl)
22266               {
22267                 error_at (loc, "iteration variable %qD"
22268                           " should not be firstprivate", real_decl);
22269                 *c = OMP_CLAUSE_CHAIN (*c);
22270               }
22271             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22272                      && OMP_CLAUSE_DECL (*c) == real_decl)
22273               {
22274                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22275                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
22276                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22277                 OMP_CLAUSE_DECL (l) = real_decl;
22278                 OMP_CLAUSE_CHAIN (l) = clauses;
22279                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22280                 clauses = l;
22281                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22282                 CP_OMP_CLAUSE_INFO (*c) = NULL;
22283                 add_private_clause = false;
22284               }
22285             else
22286               {
22287                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22288                     && OMP_CLAUSE_DECL (*c) == real_decl)
22289                   add_private_clause = false;
22290                 c = &OMP_CLAUSE_CHAIN (*c);
22291               }
22292         }
22293
22294       if (add_private_clause)
22295         {
22296           tree c;
22297           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22298             {
22299               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22300                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22301                   && OMP_CLAUSE_DECL (c) == decl)
22302                 break;
22303               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22304                        && OMP_CLAUSE_DECL (c) == decl)
22305                 error_at (loc, "iteration variable %qD "
22306                           "should not be firstprivate",
22307                           decl);
22308               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22309                        && OMP_CLAUSE_DECL (c) == decl)
22310                 error_at (loc, "iteration variable %qD should not be reduction",
22311                           decl);
22312             }
22313           if (c == NULL)
22314             {
22315               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22316               OMP_CLAUSE_DECL (c) = decl;
22317               c = finish_omp_clauses (c);
22318               if (c)
22319                 {
22320                   OMP_CLAUSE_CHAIN (c) = clauses;
22321                   clauses = c;
22322                 }
22323             }
22324         }
22325
22326       cond = NULL;
22327       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22328         cond = cp_parser_omp_for_cond (parser, decl);
22329       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22330
22331       incr = NULL;
22332       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22333         {
22334           /* If decl is an iterator, preserve the operator on decl
22335              until finish_omp_for.  */
22336           if (decl
22337               && (type_dependent_expression_p (decl)
22338                   || CLASS_TYPE_P (TREE_TYPE (decl))))
22339             incr = cp_parser_omp_for_incr (parser, decl);
22340           else
22341             incr = cp_parser_expression (parser, false, NULL);
22342         }
22343
22344       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22345         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22346                                                /*or_comma=*/false,
22347                                                /*consume_paren=*/true);
22348
22349       TREE_VEC_ELT (declv, i) = decl;
22350       TREE_VEC_ELT (initv, i) = init;
22351       TREE_VEC_ELT (condv, i) = cond;
22352       TREE_VEC_ELT (incrv, i) = incr;
22353
22354       if (i == collapse - 1)
22355         break;
22356
22357       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22358          in between the collapsed for loops to be still considered perfectly
22359          nested.  Hopefully the final version clarifies this.
22360          For now handle (multiple) {'s and empty statements.  */
22361       cp_parser_parse_tentatively (parser);
22362       do
22363         {
22364           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22365             break;
22366           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22367             {
22368               cp_lexer_consume_token (parser->lexer);
22369               bracecount++;
22370             }
22371           else if (bracecount
22372                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22373             cp_lexer_consume_token (parser->lexer);
22374           else
22375             {
22376               loc = cp_lexer_peek_token (parser->lexer)->location;
22377               error_at (loc, "not enough collapsed for loops");
22378               collapse_err = true;
22379               cp_parser_abort_tentative_parse (parser);
22380               declv = NULL_TREE;
22381               break;
22382             }
22383         }
22384       while (1);
22385
22386       if (declv)
22387         {
22388           cp_parser_parse_definitely (parser);
22389           nbraces += bracecount;
22390         }
22391     }
22392
22393   /* Note that we saved the original contents of this flag when we entered
22394      the structured block, and so we don't need to re-save it here.  */
22395   parser->in_statement = IN_OMP_FOR;
22396
22397   /* Note that the grammar doesn't call for a structured block here,
22398      though the loop as a whole is a structured block.  */
22399   body = push_stmt_list ();
22400   cp_parser_statement (parser, NULL_TREE, false, NULL);
22401   body = pop_stmt_list (body);
22402
22403   if (declv == NULL_TREE)
22404     ret = NULL_TREE;
22405   else
22406     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22407                           pre_body, clauses);
22408
22409   while (nbraces)
22410     {
22411       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22412         {
22413           cp_lexer_consume_token (parser->lexer);
22414           nbraces--;
22415         }
22416       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22417         cp_lexer_consume_token (parser->lexer);
22418       else
22419         {
22420           if (!collapse_err)
22421             {
22422               error_at (cp_lexer_peek_token (parser->lexer)->location,
22423                         "collapsed loops not perfectly nested");
22424             }
22425           collapse_err = true;
22426           cp_parser_statement_seq_opt (parser, NULL);
22427           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22428         }
22429     }
22430
22431   while (for_block)
22432     {
22433       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22434       for_block = TREE_CHAIN (for_block);
22435     }
22436
22437   return ret;
22438 }
22439
22440 /* OpenMP 2.5:
22441    #pragma omp for for-clause[optseq] new-line
22442      for-loop  */
22443
22444 #define OMP_FOR_CLAUSE_MASK                             \
22445         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22446         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22447         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22448         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22449         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
22450         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
22451         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
22452         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22453
22454 static tree
22455 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22456 {
22457   tree clauses, sb, ret;
22458   unsigned int save;
22459
22460   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22461                                        "#pragma omp for", pragma_tok);
22462
22463   sb = begin_omp_structured_block ();
22464   save = cp_parser_begin_omp_structured_block (parser);
22465
22466   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22467
22468   cp_parser_end_omp_structured_block (parser, save);
22469   add_stmt (finish_omp_structured_block (sb));
22470
22471   return ret;
22472 }
22473
22474 /* OpenMP 2.5:
22475    # pragma omp master new-line
22476      structured-block  */
22477
22478 static tree
22479 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22480 {
22481   cp_parser_require_pragma_eol (parser, pragma_tok);
22482   return c_finish_omp_master (input_location,
22483                               cp_parser_omp_structured_block (parser));
22484 }
22485
22486 /* OpenMP 2.5:
22487    # pragma omp ordered new-line
22488      structured-block  */
22489
22490 static tree
22491 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22492 {
22493   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22494   cp_parser_require_pragma_eol (parser, pragma_tok);
22495   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22496 }
22497
22498 /* OpenMP 2.5:
22499
22500    section-scope:
22501      { section-sequence }
22502
22503    section-sequence:
22504      section-directive[opt] structured-block
22505      section-sequence section-directive structured-block  */
22506
22507 static tree
22508 cp_parser_omp_sections_scope (cp_parser *parser)
22509 {
22510   tree stmt, substmt;
22511   bool error_suppress = false;
22512   cp_token *tok;
22513
22514   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22515     return NULL_TREE;
22516
22517   stmt = push_stmt_list ();
22518
22519   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22520     {
22521       unsigned save;
22522
22523       substmt = begin_omp_structured_block ();
22524       save = cp_parser_begin_omp_structured_block (parser);
22525
22526       while (1)
22527         {
22528           cp_parser_statement (parser, NULL_TREE, false, NULL);
22529
22530           tok = cp_lexer_peek_token (parser->lexer);
22531           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22532             break;
22533           if (tok->type == CPP_CLOSE_BRACE)
22534             break;
22535           if (tok->type == CPP_EOF)
22536             break;
22537         }
22538
22539       cp_parser_end_omp_structured_block (parser, save);
22540       substmt = finish_omp_structured_block (substmt);
22541       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22542       add_stmt (substmt);
22543     }
22544
22545   while (1)
22546     {
22547       tok = cp_lexer_peek_token (parser->lexer);
22548       if (tok->type == CPP_CLOSE_BRACE)
22549         break;
22550       if (tok->type == CPP_EOF)
22551         break;
22552
22553       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22554         {
22555           cp_lexer_consume_token (parser->lexer);
22556           cp_parser_require_pragma_eol (parser, tok);
22557           error_suppress = false;
22558         }
22559       else if (!error_suppress)
22560         {
22561           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22562           error_suppress = true;
22563         }
22564
22565       substmt = cp_parser_omp_structured_block (parser);
22566       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22567       add_stmt (substmt);
22568     }
22569   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22570
22571   substmt = pop_stmt_list (stmt);
22572
22573   stmt = make_node (OMP_SECTIONS);
22574   TREE_TYPE (stmt) = void_type_node;
22575   OMP_SECTIONS_BODY (stmt) = substmt;
22576
22577   add_stmt (stmt);
22578   return stmt;
22579 }
22580
22581 /* OpenMP 2.5:
22582    # pragma omp sections sections-clause[optseq] newline
22583      sections-scope  */
22584
22585 #define OMP_SECTIONS_CLAUSE_MASK                        \
22586         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22587         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22588         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22589         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22590         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22591
22592 static tree
22593 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22594 {
22595   tree clauses, ret;
22596
22597   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22598                                        "#pragma omp sections", pragma_tok);
22599
22600   ret = cp_parser_omp_sections_scope (parser);
22601   if (ret)
22602     OMP_SECTIONS_CLAUSES (ret) = clauses;
22603
22604   return ret;
22605 }
22606
22607 /* OpenMP 2.5:
22608    # pragma parallel parallel-clause new-line
22609    # pragma parallel for parallel-for-clause new-line
22610    # pragma parallel sections parallel-sections-clause new-line  */
22611
22612 #define OMP_PARALLEL_CLAUSE_MASK                        \
22613         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22614         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22615         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22616         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22617         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
22618         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
22619         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22620         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22621
22622 static tree
22623 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22624 {
22625   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22626   const char *p_name = "#pragma omp parallel";
22627   tree stmt, clauses, par_clause, ws_clause, block;
22628   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22629   unsigned int save;
22630   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22631
22632   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22633     {
22634       cp_lexer_consume_token (parser->lexer);
22635       p_kind = PRAGMA_OMP_PARALLEL_FOR;
22636       p_name = "#pragma omp parallel for";
22637       mask |= OMP_FOR_CLAUSE_MASK;
22638       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22639     }
22640   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22641     {
22642       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22643       const char *p = IDENTIFIER_POINTER (id);
22644       if (strcmp (p, "sections") == 0)
22645         {
22646           cp_lexer_consume_token (parser->lexer);
22647           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22648           p_name = "#pragma omp parallel sections";
22649           mask |= OMP_SECTIONS_CLAUSE_MASK;
22650           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22651         }
22652     }
22653
22654   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22655   block = begin_omp_parallel ();
22656   save = cp_parser_begin_omp_structured_block (parser);
22657
22658   switch (p_kind)
22659     {
22660     case PRAGMA_OMP_PARALLEL:
22661       cp_parser_statement (parser, NULL_TREE, false, NULL);
22662       par_clause = clauses;
22663       break;
22664
22665     case PRAGMA_OMP_PARALLEL_FOR:
22666       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22667       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22668       break;
22669
22670     case PRAGMA_OMP_PARALLEL_SECTIONS:
22671       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22672       stmt = cp_parser_omp_sections_scope (parser);
22673       if (stmt)
22674         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22675       break;
22676
22677     default:
22678       gcc_unreachable ();
22679     }
22680
22681   cp_parser_end_omp_structured_block (parser, save);
22682   stmt = finish_omp_parallel (par_clause, block);
22683   if (p_kind != PRAGMA_OMP_PARALLEL)
22684     OMP_PARALLEL_COMBINED (stmt) = 1;
22685   return stmt;
22686 }
22687
22688 /* OpenMP 2.5:
22689    # pragma omp single single-clause[optseq] new-line
22690      structured-block  */
22691
22692 #define OMP_SINGLE_CLAUSE_MASK                          \
22693         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22694         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22695         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
22696         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22697
22698 static tree
22699 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22700 {
22701   tree stmt = make_node (OMP_SINGLE);
22702   TREE_TYPE (stmt) = void_type_node;
22703
22704   OMP_SINGLE_CLAUSES (stmt)
22705     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22706                                  "#pragma omp single", pragma_tok);
22707   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22708
22709   return add_stmt (stmt);
22710 }
22711
22712 /* OpenMP 3.0:
22713    # pragma omp task task-clause[optseq] new-line
22714      structured-block  */
22715
22716 #define OMP_TASK_CLAUSE_MASK                            \
22717         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22718         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
22719         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22720         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22721         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22722         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22723
22724 static tree
22725 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22726 {
22727   tree clauses, block;
22728   unsigned int save;
22729
22730   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22731                                        "#pragma omp task", pragma_tok);
22732   block = begin_omp_task ();
22733   save = cp_parser_begin_omp_structured_block (parser);
22734   cp_parser_statement (parser, NULL_TREE, false, NULL);
22735   cp_parser_end_omp_structured_block (parser, save);
22736   return finish_omp_task (clauses, block);
22737 }
22738
22739 /* OpenMP 3.0:
22740    # pragma omp taskwait new-line  */
22741
22742 static void
22743 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22744 {
22745   cp_parser_require_pragma_eol (parser, pragma_tok);
22746   finish_omp_taskwait ();
22747 }
22748
22749 /* OpenMP 2.5:
22750    # pragma omp threadprivate (variable-list) */
22751
22752 static void
22753 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22754 {
22755   tree vars;
22756
22757   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22758   cp_parser_require_pragma_eol (parser, pragma_tok);
22759
22760   finish_omp_threadprivate (vars);
22761 }
22762
22763 /* Main entry point to OpenMP statement pragmas.  */
22764
22765 static void
22766 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22767 {
22768   tree stmt;
22769
22770   switch (pragma_tok->pragma_kind)
22771     {
22772     case PRAGMA_OMP_ATOMIC:
22773       cp_parser_omp_atomic (parser, pragma_tok);
22774       return;
22775     case PRAGMA_OMP_CRITICAL:
22776       stmt = cp_parser_omp_critical (parser, pragma_tok);
22777       break;
22778     case PRAGMA_OMP_FOR:
22779       stmt = cp_parser_omp_for (parser, pragma_tok);
22780       break;
22781     case PRAGMA_OMP_MASTER:
22782       stmt = cp_parser_omp_master (parser, pragma_tok);
22783       break;
22784     case PRAGMA_OMP_ORDERED:
22785       stmt = cp_parser_omp_ordered (parser, pragma_tok);
22786       break;
22787     case PRAGMA_OMP_PARALLEL:
22788       stmt = cp_parser_omp_parallel (parser, pragma_tok);
22789       break;
22790     case PRAGMA_OMP_SECTIONS:
22791       stmt = cp_parser_omp_sections (parser, pragma_tok);
22792       break;
22793     case PRAGMA_OMP_SINGLE:
22794       stmt = cp_parser_omp_single (parser, pragma_tok);
22795       break;
22796     case PRAGMA_OMP_TASK:
22797       stmt = cp_parser_omp_task (parser, pragma_tok);
22798       break;
22799     default:
22800       gcc_unreachable ();
22801     }
22802
22803   if (stmt)
22804     SET_EXPR_LOCATION (stmt, pragma_tok->location);
22805 }
22806 \f
22807 /* The parser.  */
22808
22809 static GTY (()) cp_parser *the_parser;
22810
22811 \f
22812 /* Special handling for the first token or line in the file.  The first
22813    thing in the file might be #pragma GCC pch_preprocess, which loads a
22814    PCH file, which is a GC collection point.  So we need to handle this
22815    first pragma without benefit of an existing lexer structure.
22816
22817    Always returns one token to the caller in *FIRST_TOKEN.  This is
22818    either the true first token of the file, or the first token after
22819    the initial pragma.  */
22820
22821 static void
22822 cp_parser_initial_pragma (cp_token *first_token)
22823 {
22824   tree name = NULL;
22825
22826   cp_lexer_get_preprocessor_token (NULL, first_token);
22827   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22828     return;
22829
22830   cp_lexer_get_preprocessor_token (NULL, first_token);
22831   if (first_token->type == CPP_STRING)
22832     {
22833       name = first_token->u.value;
22834
22835       cp_lexer_get_preprocessor_token (NULL, first_token);
22836       if (first_token->type != CPP_PRAGMA_EOL)
22837         error_at (first_token->location,
22838                   "junk at end of %<#pragma GCC pch_preprocess%>");
22839     }
22840   else
22841     error_at (first_token->location, "expected string literal");
22842
22843   /* Skip to the end of the pragma.  */
22844   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22845     cp_lexer_get_preprocessor_token (NULL, first_token);
22846
22847   /* Now actually load the PCH file.  */
22848   if (name)
22849     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22850
22851   /* Read one more token to return to our caller.  We have to do this
22852      after reading the PCH file in, since its pointers have to be
22853      live.  */
22854   cp_lexer_get_preprocessor_token (NULL, first_token);
22855 }
22856
22857 /* Normal parsing of a pragma token.  Here we can (and must) use the
22858    regular lexer.  */
22859
22860 static bool
22861 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22862 {
22863   cp_token *pragma_tok;
22864   unsigned int id;
22865
22866   pragma_tok = cp_lexer_consume_token (parser->lexer);
22867   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22868   parser->lexer->in_pragma = true;
22869
22870   id = pragma_tok->pragma_kind;
22871   switch (id)
22872     {
22873     case PRAGMA_GCC_PCH_PREPROCESS:
22874       error_at (pragma_tok->location,
22875                 "%<#pragma GCC pch_preprocess%> must be first");
22876       break;
22877
22878     case PRAGMA_OMP_BARRIER:
22879       switch (context)
22880         {
22881         case pragma_compound:
22882           cp_parser_omp_barrier (parser, pragma_tok);
22883           return false;
22884         case pragma_stmt:
22885           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22886                     "used in compound statements");
22887           break;
22888         default:
22889           goto bad_stmt;
22890         }
22891       break;
22892
22893     case PRAGMA_OMP_FLUSH:
22894       switch (context)
22895         {
22896         case pragma_compound:
22897           cp_parser_omp_flush (parser, pragma_tok);
22898           return false;
22899         case pragma_stmt:
22900           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
22901                     "used in compound statements");
22902           break;
22903         default:
22904           goto bad_stmt;
22905         }
22906       break;
22907
22908     case PRAGMA_OMP_TASKWAIT:
22909       switch (context)
22910         {
22911         case pragma_compound:
22912           cp_parser_omp_taskwait (parser, pragma_tok);
22913           return false;
22914         case pragma_stmt:
22915           error_at (pragma_tok->location,
22916                     "%<#pragma omp taskwait%> may only be "
22917                     "used in compound statements");
22918           break;
22919         default:
22920           goto bad_stmt;
22921         }
22922       break;
22923
22924     case PRAGMA_OMP_THREADPRIVATE:
22925       cp_parser_omp_threadprivate (parser, pragma_tok);
22926       return false;
22927
22928     case PRAGMA_OMP_ATOMIC:
22929     case PRAGMA_OMP_CRITICAL:
22930     case PRAGMA_OMP_FOR:
22931     case PRAGMA_OMP_MASTER:
22932     case PRAGMA_OMP_ORDERED:
22933     case PRAGMA_OMP_PARALLEL:
22934     case PRAGMA_OMP_SECTIONS:
22935     case PRAGMA_OMP_SINGLE:
22936     case PRAGMA_OMP_TASK:
22937       if (context == pragma_external)
22938         goto bad_stmt;
22939       cp_parser_omp_construct (parser, pragma_tok);
22940       return true;
22941
22942     case PRAGMA_OMP_SECTION:
22943       error_at (pragma_tok->location, 
22944                 "%<#pragma omp section%> may only be used in "
22945                 "%<#pragma omp sections%> construct");
22946       break;
22947
22948     default:
22949       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22950       c_invoke_pragma_handler (id);
22951       break;
22952
22953     bad_stmt:
22954       cp_parser_error (parser, "expected declaration specifiers");
22955       break;
22956     }
22957
22958   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22959   return false;
22960 }
22961
22962 /* The interface the pragma parsers have to the lexer.  */
22963
22964 enum cpp_ttype
22965 pragma_lex (tree *value)
22966 {
22967   cp_token *tok;
22968   enum cpp_ttype ret;
22969
22970   tok = cp_lexer_peek_token (the_parser->lexer);
22971
22972   ret = tok->type;
22973   *value = tok->u.value;
22974
22975   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22976     ret = CPP_EOF;
22977   else if (ret == CPP_STRING)
22978     *value = cp_parser_string_literal (the_parser, false, false);
22979   else
22980     {
22981       cp_lexer_consume_token (the_parser->lexer);
22982       if (ret == CPP_KEYWORD)
22983         ret = CPP_NAME;
22984     }
22985
22986   return ret;
22987 }
22988
22989 \f
22990 /* External interface.  */
22991
22992 /* Parse one entire translation unit.  */
22993
22994 void
22995 c_parse_file (void)
22996 {
22997   bool error_occurred;
22998   static bool already_called = false;
22999
23000   if (already_called)
23001     {
23002       sorry ("inter-module optimizations not implemented for C++");
23003       return;
23004     }
23005   already_called = true;
23006
23007   the_parser = cp_parser_new ();
23008   push_deferring_access_checks (flag_access_control
23009                                 ? dk_no_deferred : dk_no_check);
23010   error_occurred = cp_parser_translation_unit (the_parser);
23011   the_parser = NULL;
23012 }
23013
23014 #include "gt-cp-parser.h"