OSDN Git Service

remove utf8
[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_RAW_STRINGS);
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       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
796       break;
797
798     default:
799       break;
800     }
801 }
802
803 /* Start emitting debugging information.  */
804
805 static void
806 cp_lexer_start_debugging (cp_lexer* lexer)
807 {
808   lexer->debugging_p = true;
809 }
810
811 /* Stop emitting debugging information.  */
812
813 static void
814 cp_lexer_stop_debugging (cp_lexer* lexer)
815 {
816   lexer->debugging_p = false;
817 }
818
819 #endif /* ENABLE_CHECKING */
820
821 /* Create a new cp_token_cache, representing a range of tokens.  */
822
823 static cp_token_cache *
824 cp_token_cache_new (cp_token *first, cp_token *last)
825 {
826   cp_token_cache *cache = GGC_NEW (cp_token_cache);
827   cache->first = first;
828   cache->last = last;
829   return cache;
830 }
831
832 \f
833 /* Decl-specifiers.  */
834
835 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
836
837 static void
838 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
839 {
840   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
841 }
842
843 /* Declarators.  */
844
845 /* Nothing other than the parser should be creating declarators;
846    declarators are a semi-syntactic representation of C++ entities.
847    Other parts of the front end that need to create entities (like
848    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
849
850 static cp_declarator *make_call_declarator
851   (cp_declarator *, tree, cp_cv_quals, tree, tree);
852 static cp_declarator *make_array_declarator
853   (cp_declarator *, tree);
854 static cp_declarator *make_pointer_declarator
855   (cp_cv_quals, cp_declarator *);
856 static cp_declarator *make_reference_declarator
857   (cp_cv_quals, cp_declarator *, bool);
858 static cp_parameter_declarator *make_parameter_declarator
859   (cp_decl_specifier_seq *, cp_declarator *, tree);
860 static cp_declarator *make_ptrmem_declarator
861   (cp_cv_quals, tree, cp_declarator *);
862
863 /* An erroneous declarator.  */
864 static cp_declarator *cp_error_declarator;
865
866 /* The obstack on which declarators and related data structures are
867    allocated.  */
868 static struct obstack declarator_obstack;
869
870 /* Alloc BYTES from the declarator memory pool.  */
871
872 static inline void *
873 alloc_declarator (size_t bytes)
874 {
875   return obstack_alloc (&declarator_obstack, bytes);
876 }
877
878 /* Allocate a declarator of the indicated KIND.  Clear fields that are
879    common to all declarators.  */
880
881 static cp_declarator *
882 make_declarator (cp_declarator_kind kind)
883 {
884   cp_declarator *declarator;
885
886   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
887   declarator->kind = kind;
888   declarator->attributes = NULL_TREE;
889   declarator->declarator = NULL;
890   declarator->parameter_pack_p = false;
891
892   return declarator;
893 }
894
895 /* Make a declarator for a generalized identifier.  If
896    QUALIFYING_SCOPE is non-NULL, the identifier is
897    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
898    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
899    is, if any.   */
900
901 static cp_declarator *
902 make_id_declarator (tree qualifying_scope, tree unqualified_name,
903                     special_function_kind sfk)
904 {
905   cp_declarator *declarator;
906
907   /* It is valid to write:
908
909        class C { void f(); };
910        typedef C D;
911        void D::f();
912
913      The standard is not clear about whether `typedef const C D' is
914      legal; as of 2002-09-15 the committee is considering that
915      question.  EDG 3.0 allows that syntax.  Therefore, we do as
916      well.  */
917   if (qualifying_scope && TYPE_P (qualifying_scope))
918     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
919
920   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
921               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
922               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
923
924   declarator = make_declarator (cdk_id);
925   declarator->u.id.qualifying_scope = qualifying_scope;
926   declarator->u.id.unqualified_name = unqualified_name;
927   declarator->u.id.sfk = sfk;
928   
929   return declarator;
930 }
931
932 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
933    of modifiers such as const or volatile to apply to the pointer
934    type, represented as identifiers.  */
935
936 cp_declarator *
937 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
938 {
939   cp_declarator *declarator;
940
941   declarator = make_declarator (cdk_pointer);
942   declarator->declarator = target;
943   declarator->u.pointer.qualifiers = cv_qualifiers;
944   declarator->u.pointer.class_type = NULL_TREE;
945   if (target)
946     {
947       declarator->parameter_pack_p = target->parameter_pack_p;
948       target->parameter_pack_p = false;
949     }
950   else
951     declarator->parameter_pack_p = false;
952
953   return declarator;
954 }
955
956 /* Like make_pointer_declarator -- but for references.  */
957
958 cp_declarator *
959 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
960                            bool rvalue_ref)
961 {
962   cp_declarator *declarator;
963
964   declarator = make_declarator (cdk_reference);
965   declarator->declarator = target;
966   declarator->u.reference.qualifiers = cv_qualifiers;
967   declarator->u.reference.rvalue_ref = rvalue_ref;
968   if (target)
969     {
970       declarator->parameter_pack_p = target->parameter_pack_p;
971       target->parameter_pack_p = false;
972     }
973   else
974     declarator->parameter_pack_p = false;
975
976   return declarator;
977 }
978
979 /* Like make_pointer_declarator -- but for a pointer to a non-static
980    member of CLASS_TYPE.  */
981
982 cp_declarator *
983 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
984                         cp_declarator *pointee)
985 {
986   cp_declarator *declarator;
987
988   declarator = make_declarator (cdk_ptrmem);
989   declarator->declarator = pointee;
990   declarator->u.pointer.qualifiers = cv_qualifiers;
991   declarator->u.pointer.class_type = class_type;
992
993   if (pointee)
994     {
995       declarator->parameter_pack_p = pointee->parameter_pack_p;
996       pointee->parameter_pack_p = false;
997     }
998   else
999     declarator->parameter_pack_p = false;
1000
1001   return declarator;
1002 }
1003
1004 /* Make a declarator for the function given by TARGET, with the
1005    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1006    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1007    indicates what exceptions can be thrown.  */
1008
1009 cp_declarator *
1010 make_call_declarator (cp_declarator *target,
1011                       tree parms,
1012                       cp_cv_quals cv_qualifiers,
1013                       tree exception_specification,
1014                       tree late_return_type)
1015 {
1016   cp_declarator *declarator;
1017
1018   declarator = make_declarator (cdk_function);
1019   declarator->declarator = target;
1020   declarator->u.function.parameters = parms;
1021   declarator->u.function.qualifiers = cv_qualifiers;
1022   declarator->u.function.exception_specification = exception_specification;
1023   declarator->u.function.late_return_type = late_return_type;
1024   if (target)
1025     {
1026       declarator->parameter_pack_p = target->parameter_pack_p;
1027       target->parameter_pack_p = false;
1028     }
1029   else
1030     declarator->parameter_pack_p = false;
1031
1032   return declarator;
1033 }
1034
1035 /* Make a declarator for an array of BOUNDS elements, each of which is
1036    defined by ELEMENT.  */
1037
1038 cp_declarator *
1039 make_array_declarator (cp_declarator *element, tree bounds)
1040 {
1041   cp_declarator *declarator;
1042
1043   declarator = make_declarator (cdk_array);
1044   declarator->declarator = element;
1045   declarator->u.array.bounds = bounds;
1046   if (element)
1047     {
1048       declarator->parameter_pack_p = element->parameter_pack_p;
1049       element->parameter_pack_p = false;
1050     }
1051   else
1052     declarator->parameter_pack_p = false;
1053
1054   return declarator;
1055 }
1056
1057 /* Determine whether the declarator we've seen so far can be a
1058    parameter pack, when followed by an ellipsis.  */
1059 static bool 
1060 declarator_can_be_parameter_pack (cp_declarator *declarator)
1061 {
1062   /* Search for a declarator name, or any other declarator that goes
1063      after the point where the ellipsis could appear in a parameter
1064      pack. If we find any of these, then this declarator can not be
1065      made into a parameter pack.  */
1066   bool found = false;
1067   while (declarator && !found)
1068     {
1069       switch ((int)declarator->kind)
1070         {
1071         case cdk_id:
1072         case cdk_array:
1073           found = true;
1074           break;
1075
1076         case cdk_error:
1077           return true;
1078
1079         default:
1080           declarator = declarator->declarator;
1081           break;
1082         }
1083     }
1084
1085   return !found;
1086 }
1087
1088 cp_parameter_declarator *no_parameters;
1089
1090 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1091    DECLARATOR and DEFAULT_ARGUMENT.  */
1092
1093 cp_parameter_declarator *
1094 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1095                            cp_declarator *declarator,
1096                            tree default_argument)
1097 {
1098   cp_parameter_declarator *parameter;
1099
1100   parameter = ((cp_parameter_declarator *)
1101                alloc_declarator (sizeof (cp_parameter_declarator)));
1102   parameter->next = NULL;
1103   if (decl_specifiers)
1104     parameter->decl_specifiers = *decl_specifiers;
1105   else
1106     clear_decl_specs (&parameter->decl_specifiers);
1107   parameter->declarator = declarator;
1108   parameter->default_argument = default_argument;
1109   parameter->ellipsis_p = false;
1110
1111   return parameter;
1112 }
1113
1114 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1115
1116 static bool
1117 function_declarator_p (const cp_declarator *declarator)
1118 {
1119   while (declarator)
1120     {
1121       if (declarator->kind == cdk_function
1122           && declarator->declarator->kind == cdk_id)
1123         return true;
1124       if (declarator->kind == cdk_id
1125           || declarator->kind == cdk_error)
1126         return false;
1127       declarator = declarator->declarator;
1128     }
1129   return false;
1130 }
1131  
1132 /* The parser.  */
1133
1134 /* Overview
1135    --------
1136
1137    A cp_parser parses the token stream as specified by the C++
1138    grammar.  Its job is purely parsing, not semantic analysis.  For
1139    example, the parser breaks the token stream into declarators,
1140    expressions, statements, and other similar syntactic constructs.
1141    It does not check that the types of the expressions on either side
1142    of an assignment-statement are compatible, or that a function is
1143    not declared with a parameter of type `void'.
1144
1145    The parser invokes routines elsewhere in the compiler to perform
1146    semantic analysis and to build up the abstract syntax tree for the
1147    code processed.
1148
1149    The parser (and the template instantiation code, which is, in a
1150    way, a close relative of parsing) are the only parts of the
1151    compiler that should be calling push_scope and pop_scope, or
1152    related functions.  The parser (and template instantiation code)
1153    keeps track of what scope is presently active; everything else
1154    should simply honor that.  (The code that generates static
1155    initializers may also need to set the scope, in order to check
1156    access control correctly when emitting the initializers.)
1157
1158    Methodology
1159    -----------
1160
1161    The parser is of the standard recursive-descent variety.  Upcoming
1162    tokens in the token stream are examined in order to determine which
1163    production to use when parsing a non-terminal.  Some C++ constructs
1164    require arbitrary look ahead to disambiguate.  For example, it is
1165    impossible, in the general case, to tell whether a statement is an
1166    expression or declaration without scanning the entire statement.
1167    Therefore, the parser is capable of "parsing tentatively."  When the
1168    parser is not sure what construct comes next, it enters this mode.
1169    Then, while we attempt to parse the construct, the parser queues up
1170    error messages, rather than issuing them immediately, and saves the
1171    tokens it consumes.  If the construct is parsed successfully, the
1172    parser "commits", i.e., it issues any queued error messages and
1173    the tokens that were being preserved are permanently discarded.
1174    If, however, the construct is not parsed successfully, the parser
1175    rolls back its state completely so that it can resume parsing using
1176    a different alternative.
1177
1178    Future Improvements
1179    -------------------
1180
1181    The performance of the parser could probably be improved substantially.
1182    We could often eliminate the need to parse tentatively by looking ahead
1183    a little bit.  In some places, this approach might not entirely eliminate
1184    the need to parse tentatively, but it might still speed up the average
1185    case.  */
1186
1187 /* Flags that are passed to some parsing functions.  These values can
1188    be bitwise-ored together.  */
1189
1190 enum
1191 {
1192   /* No flags.  */
1193   CP_PARSER_FLAGS_NONE = 0x0,
1194   /* The construct is optional.  If it is not present, then no error
1195      should be issued.  */
1196   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1197   /* When parsing a type-specifier, do not allow user-defined types.  */
1198   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1199 };
1200
1201 /* This type is used for parameters and variables which hold
1202    combinations of the above flags.  */
1203 typedef int cp_parser_flags;
1204
1205 /* The different kinds of declarators we want to parse.  */
1206
1207 typedef enum cp_parser_declarator_kind
1208 {
1209   /* We want an abstract declarator.  */
1210   CP_PARSER_DECLARATOR_ABSTRACT,
1211   /* We want a named declarator.  */
1212   CP_PARSER_DECLARATOR_NAMED,
1213   /* We don't mind, but the name must be an unqualified-id.  */
1214   CP_PARSER_DECLARATOR_EITHER
1215 } cp_parser_declarator_kind;
1216
1217 /* The precedence values used to parse binary expressions.  The minimum value
1218    of PREC must be 1, because zero is reserved to quickly discriminate
1219    binary operators from other tokens.  */
1220
1221 enum cp_parser_prec
1222 {
1223   PREC_NOT_OPERATOR,
1224   PREC_LOGICAL_OR_EXPRESSION,
1225   PREC_LOGICAL_AND_EXPRESSION,
1226   PREC_INCLUSIVE_OR_EXPRESSION,
1227   PREC_EXCLUSIVE_OR_EXPRESSION,
1228   PREC_AND_EXPRESSION,
1229   PREC_EQUALITY_EXPRESSION,
1230   PREC_RELATIONAL_EXPRESSION,
1231   PREC_SHIFT_EXPRESSION,
1232   PREC_ADDITIVE_EXPRESSION,
1233   PREC_MULTIPLICATIVE_EXPRESSION,
1234   PREC_PM_EXPRESSION,
1235   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1236 };
1237
1238 /* A mapping from a token type to a corresponding tree node type, with a
1239    precedence value.  */
1240
1241 typedef struct cp_parser_binary_operations_map_node
1242 {
1243   /* The token type.  */
1244   enum cpp_ttype token_type;
1245   /* The corresponding tree code.  */
1246   enum tree_code tree_type;
1247   /* The precedence of this operator.  */
1248   enum cp_parser_prec prec;
1249 } cp_parser_binary_operations_map_node;
1250
1251 /* The status of a tentative parse.  */
1252
1253 typedef enum cp_parser_status_kind
1254 {
1255   /* No errors have occurred.  */
1256   CP_PARSER_STATUS_KIND_NO_ERROR,
1257   /* An error has occurred.  */
1258   CP_PARSER_STATUS_KIND_ERROR,
1259   /* We are committed to this tentative parse, whether or not an error
1260      has occurred.  */
1261   CP_PARSER_STATUS_KIND_COMMITTED
1262 } cp_parser_status_kind;
1263
1264 typedef struct cp_parser_expression_stack_entry
1265 {
1266   /* Left hand side of the binary operation we are currently
1267      parsing.  */
1268   tree lhs;
1269   /* Original tree code for left hand side, if it was a binary
1270      expression itself (used for -Wparentheses).  */
1271   enum tree_code lhs_type;
1272   /* Tree code for the binary operation we are parsing.  */
1273   enum tree_code tree_type;
1274   /* Precedence of the binary operation we are parsing.  */
1275   enum cp_parser_prec prec;
1276 } cp_parser_expression_stack_entry;
1277
1278 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1279    entries because precedence levels on the stack are monotonically
1280    increasing.  */
1281 typedef struct cp_parser_expression_stack_entry
1282   cp_parser_expression_stack[NUM_PREC_VALUES];
1283
1284 /* Context that is saved and restored when parsing tentatively.  */
1285 typedef struct GTY (()) cp_parser_context {
1286   /* If this is a tentative parsing context, the status of the
1287      tentative parse.  */
1288   enum cp_parser_status_kind status;
1289   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1290      that are looked up in this context must be looked up both in the
1291      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1292      the context of the containing expression.  */
1293   tree object_type;
1294
1295   /* The next parsing context in the stack.  */
1296   struct cp_parser_context *next;
1297 } cp_parser_context;
1298
1299 /* Prototypes.  */
1300
1301 /* Constructors and destructors.  */
1302
1303 static cp_parser_context *cp_parser_context_new
1304   (cp_parser_context *);
1305
1306 /* Class variables.  */
1307
1308 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1309
1310 /* The operator-precedence table used by cp_parser_binary_expression.
1311    Transformed into an associative array (binops_by_token) by
1312    cp_parser_new.  */
1313
1314 static const cp_parser_binary_operations_map_node binops[] = {
1315   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1316   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1317
1318   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321
1322   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1323   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1324
1325   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1326   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1327
1328   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1329   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1330   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1331   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1332
1333   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1334   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1335
1336   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1337
1338   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1339
1340   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1341
1342   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1343
1344   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1345 };
1346
1347 /* The same as binops, but initialized by cp_parser_new so that
1348    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1349    for speed.  */
1350 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1351
1352 /* Constructors and destructors.  */
1353
1354 /* Construct a new context.  The context below this one on the stack
1355    is given by NEXT.  */
1356
1357 static cp_parser_context *
1358 cp_parser_context_new (cp_parser_context* next)
1359 {
1360   cp_parser_context *context;
1361
1362   /* Allocate the storage.  */
1363   if (cp_parser_context_free_list != NULL)
1364     {
1365       /* Pull the first entry from the free list.  */
1366       context = cp_parser_context_free_list;
1367       cp_parser_context_free_list = context->next;
1368       memset (context, 0, sizeof (*context));
1369     }
1370   else
1371     context = GGC_CNEW (cp_parser_context);
1372
1373   /* No errors have occurred yet in this context.  */
1374   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1375   /* If this is not the bottommost context, copy information that we
1376      need from the previous context.  */
1377   if (next)
1378     {
1379       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1380          expression, then we are parsing one in this context, too.  */
1381       context->object_type = next->object_type;
1382       /* Thread the stack.  */
1383       context->next = next;
1384     }
1385
1386   return context;
1387 }
1388
1389 /* The cp_parser structure represents the C++ parser.  */
1390
1391 typedef struct GTY(()) cp_parser {
1392   /* The lexer from which we are obtaining tokens.  */
1393   cp_lexer *lexer;
1394
1395   /* The scope in which names should be looked up.  If NULL_TREE, then
1396      we look up names in the scope that is currently open in the
1397      source program.  If non-NULL, this is either a TYPE or
1398      NAMESPACE_DECL for the scope in which we should look.  It can
1399      also be ERROR_MARK, when we've parsed a bogus scope.
1400
1401      This value is not cleared automatically after a name is looked
1402      up, so we must be careful to clear it before starting a new look
1403      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1404      will look up `Z' in the scope of `X', rather than the current
1405      scope.)  Unfortunately, it is difficult to tell when name lookup
1406      is complete, because we sometimes peek at a token, look it up,
1407      and then decide not to consume it.   */
1408   tree scope;
1409
1410   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1411      last lookup took place.  OBJECT_SCOPE is used if an expression
1412      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1413      respectively.  QUALIFYING_SCOPE is used for an expression of the
1414      form "X::Y"; it refers to X.  */
1415   tree object_scope;
1416   tree qualifying_scope;
1417
1418   /* A stack of parsing contexts.  All but the bottom entry on the
1419      stack will be tentative contexts.
1420
1421      We parse tentatively in order to determine which construct is in
1422      use in some situations.  For example, in order to determine
1423      whether a statement is an expression-statement or a
1424      declaration-statement we parse it tentatively as a
1425      declaration-statement.  If that fails, we then reparse the same
1426      token stream as an expression-statement.  */
1427   cp_parser_context *context;
1428
1429   /* True if we are parsing GNU C++.  If this flag is not set, then
1430      GNU extensions are not recognized.  */
1431   bool allow_gnu_extensions_p;
1432
1433   /* TRUE if the `>' token should be interpreted as the greater-than
1434      operator.  FALSE if it is the end of a template-id or
1435      template-parameter-list. In C++0x mode, this flag also applies to
1436      `>>' tokens, which are viewed as two consecutive `>' tokens when
1437      this flag is FALSE.  */
1438   bool greater_than_is_operator_p;
1439
1440   /* TRUE if default arguments are allowed within a parameter list
1441      that starts at this point. FALSE if only a gnu extension makes
1442      them permissible.  */
1443   bool default_arg_ok_p;
1444
1445   /* TRUE if we are parsing an integral constant-expression.  See
1446      [expr.const] for a precise definition.  */
1447   bool integral_constant_expression_p;
1448
1449   /* TRUE if we are parsing an integral constant-expression -- but a
1450      non-constant expression should be permitted as well.  This flag
1451      is used when parsing an array bound so that GNU variable-length
1452      arrays are tolerated.  */
1453   bool allow_non_integral_constant_expression_p;
1454
1455   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1456      been seen that makes the expression non-constant.  */
1457   bool non_integral_constant_expression_p;
1458
1459   /* TRUE if local variable names and `this' are forbidden in the
1460      current context.  */
1461   bool local_variables_forbidden_p;
1462
1463   /* TRUE if the declaration we are parsing is part of a
1464      linkage-specification of the form `extern string-literal
1465      declaration'.  */
1466   bool in_unbraced_linkage_specification_p;
1467
1468   /* TRUE if we are presently parsing a declarator, after the
1469      direct-declarator.  */
1470   bool in_declarator_p;
1471
1472   /* TRUE if we are presently parsing a template-argument-list.  */
1473   bool in_template_argument_list_p;
1474
1475   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1476      to IN_OMP_BLOCK if parsing OpenMP structured block and
1477      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1478      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1479      iteration-statement, OpenMP block or loop within that switch.  */
1480 #define IN_SWITCH_STMT          1
1481 #define IN_ITERATION_STMT       2
1482 #define IN_OMP_BLOCK            4
1483 #define IN_OMP_FOR              8
1484 #define IN_IF_STMT             16
1485   unsigned char in_statement;
1486
1487   /* TRUE if we are presently parsing the body of a switch statement.
1488      Note that this doesn't quite overlap with in_statement above.
1489      The difference relates to giving the right sets of error messages:
1490      "case not in switch" vs "break statement used with OpenMP...".  */
1491   bool in_switch_statement_p;
1492
1493   /* TRUE if we are parsing a type-id in an expression context.  In
1494      such a situation, both "type (expr)" and "type (type)" are valid
1495      alternatives.  */
1496   bool in_type_id_in_expr_p;
1497
1498   /* TRUE if we are currently in a header file where declarations are
1499      implicitly extern "C".  */
1500   bool implicit_extern_c;
1501
1502   /* TRUE if strings in expressions should be translated to the execution
1503      character set.  */
1504   bool translate_strings_p;
1505
1506   /* TRUE if we are presently parsing the body of a function, but not
1507      a local class.  */
1508   bool in_function_body;
1509
1510   /* If non-NULL, then we are parsing a construct where new type
1511      definitions are not permitted.  The string stored here will be
1512      issued as an error message if a type is defined.  */
1513   const char *type_definition_forbidden_message;
1514
1515   /* A list of lists. The outer list is a stack, used for member
1516      functions of local classes. At each level there are two sub-list,
1517      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1518      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1519      TREE_VALUE's. The functions are chained in reverse declaration
1520      order.
1521
1522      The TREE_PURPOSE sublist contains those functions with default
1523      arguments that need post processing, and the TREE_VALUE sublist
1524      contains those functions with definitions that need post
1525      processing.
1526
1527      These lists can only be processed once the outermost class being
1528      defined is complete.  */
1529   tree unparsed_functions_queues;
1530
1531   /* The number of classes whose definitions are currently in
1532      progress.  */
1533   unsigned num_classes_being_defined;
1534
1535   /* The number of template parameter lists that apply directly to the
1536      current declaration.  */
1537   unsigned num_template_parameter_lists;
1538 } cp_parser;
1539
1540 /* Prototypes.  */
1541
1542 /* Constructors and destructors.  */
1543
1544 static cp_parser *cp_parser_new
1545   (void);
1546
1547 /* Routines to parse various constructs.
1548
1549    Those that return `tree' will return the error_mark_node (rather
1550    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1551    Sometimes, they will return an ordinary node if error-recovery was
1552    attempted, even though a parse error occurred.  So, to check
1553    whether or not a parse error occurred, you should always use
1554    cp_parser_error_occurred.  If the construct is optional (indicated
1555    either by an `_opt' in the name of the function that does the
1556    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1557    the construct is not present.  */
1558
1559 /* Lexical conventions [gram.lex]  */
1560
1561 static tree cp_parser_identifier
1562   (cp_parser *);
1563 static tree cp_parser_string_literal
1564   (cp_parser *, bool, bool);
1565
1566 /* Basic concepts [gram.basic]  */
1567
1568 static bool cp_parser_translation_unit
1569   (cp_parser *);
1570
1571 /* Expressions [gram.expr]  */
1572
1573 static tree cp_parser_primary_expression
1574   (cp_parser *, bool, bool, bool, cp_id_kind *);
1575 static tree cp_parser_id_expression
1576   (cp_parser *, bool, bool, bool *, bool, bool);
1577 static tree cp_parser_unqualified_id
1578   (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_nested_name_specifier_opt
1580   (cp_parser *, bool, bool, bool, bool);
1581 static tree cp_parser_nested_name_specifier
1582   (cp_parser *, bool, bool, bool, bool);
1583 static tree cp_parser_qualifying_entity
1584   (cp_parser *, bool, bool, bool, bool, bool);
1585 static tree cp_parser_postfix_expression
1586   (cp_parser *, bool, bool, bool, cp_id_kind *);
1587 static tree cp_parser_postfix_open_square_expression
1588   (cp_parser *, tree, bool);
1589 static tree cp_parser_postfix_dot_deref_expression
1590   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1591 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1592   (cp_parser *, bool, bool, bool, bool *);
1593 static void cp_parser_pseudo_destructor_name
1594   (cp_parser *, tree *, tree *);
1595 static tree cp_parser_unary_expression
1596   (cp_parser *, bool, bool, cp_id_kind *);
1597 static enum tree_code cp_parser_unary_operator
1598   (cp_token *);
1599 static tree cp_parser_new_expression
1600   (cp_parser *);
1601 static VEC(tree,gc) *cp_parser_new_placement
1602   (cp_parser *);
1603 static tree cp_parser_new_type_id
1604   (cp_parser *, tree *);
1605 static cp_declarator *cp_parser_new_declarator_opt
1606   (cp_parser *);
1607 static cp_declarator *cp_parser_direct_new_declarator
1608   (cp_parser *);
1609 static VEC(tree,gc) *cp_parser_new_initializer
1610   (cp_parser *);
1611 static tree cp_parser_delete_expression
1612   (cp_parser *);
1613 static tree cp_parser_cast_expression
1614   (cp_parser *, bool, bool, cp_id_kind *);
1615 static tree cp_parser_binary_expression
1616   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1617 static tree cp_parser_question_colon_clause
1618   (cp_parser *, tree);
1619 static tree cp_parser_assignment_expression
1620   (cp_parser *, bool, cp_id_kind *);
1621 static enum tree_code cp_parser_assignment_operator_opt
1622   (cp_parser *);
1623 static tree cp_parser_expression
1624   (cp_parser *, bool, cp_id_kind *);
1625 static tree cp_parser_constant_expression
1626   (cp_parser *, bool, bool *);
1627 static tree cp_parser_builtin_offsetof
1628   (cp_parser *);
1629 static tree cp_parser_lambda_expression
1630   (cp_parser *);
1631 static void cp_parser_lambda_introducer
1632   (cp_parser *, tree);
1633 static void cp_parser_lambda_declarator_opt
1634   (cp_parser *, tree);
1635 static void cp_parser_lambda_body
1636   (cp_parser *, tree);
1637
1638 /* Statements [gram.stmt.stmt]  */
1639
1640 static void cp_parser_statement
1641   (cp_parser *, tree, bool, bool *);
1642 static void cp_parser_label_for_labeled_statement
1643   (cp_parser *);
1644 static tree cp_parser_expression_statement
1645   (cp_parser *, tree);
1646 static tree cp_parser_compound_statement
1647   (cp_parser *, tree, bool);
1648 static void cp_parser_statement_seq_opt
1649   (cp_parser *, tree);
1650 static tree cp_parser_selection_statement
1651   (cp_parser *, bool *);
1652 static tree cp_parser_condition
1653   (cp_parser *);
1654 static tree cp_parser_iteration_statement
1655   (cp_parser *);
1656 static void cp_parser_for_init_statement
1657   (cp_parser *);
1658 static tree cp_parser_jump_statement
1659   (cp_parser *);
1660 static void cp_parser_declaration_statement
1661   (cp_parser *);
1662
1663 static tree cp_parser_implicitly_scoped_statement
1664   (cp_parser *, bool *);
1665 static void cp_parser_already_scoped_statement
1666   (cp_parser *);
1667
1668 /* Declarations [gram.dcl.dcl] */
1669
1670 static void cp_parser_declaration_seq_opt
1671   (cp_parser *);
1672 static void cp_parser_declaration
1673   (cp_parser *);
1674 static void cp_parser_block_declaration
1675   (cp_parser *, bool);
1676 static void cp_parser_simple_declaration
1677   (cp_parser *, bool);
1678 static void cp_parser_decl_specifier_seq
1679   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1680 static tree cp_parser_storage_class_specifier_opt
1681   (cp_parser *);
1682 static tree cp_parser_function_specifier_opt
1683   (cp_parser *, cp_decl_specifier_seq *);
1684 static tree cp_parser_type_specifier
1685   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1686    int *, bool *);
1687 static tree cp_parser_simple_type_specifier
1688   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1689 static tree cp_parser_type_name
1690   (cp_parser *);
1691 static tree cp_parser_nonclass_name 
1692   (cp_parser* parser);
1693 static tree cp_parser_elaborated_type_specifier
1694   (cp_parser *, bool, bool);
1695 static tree cp_parser_enum_specifier
1696   (cp_parser *);
1697 static void cp_parser_enumerator_list
1698   (cp_parser *, tree);
1699 static void cp_parser_enumerator_definition
1700   (cp_parser *, tree);
1701 static tree cp_parser_namespace_name
1702   (cp_parser *);
1703 static void cp_parser_namespace_definition
1704   (cp_parser *);
1705 static void cp_parser_namespace_body
1706   (cp_parser *);
1707 static tree cp_parser_qualified_namespace_specifier
1708   (cp_parser *);
1709 static void cp_parser_namespace_alias_definition
1710   (cp_parser *);
1711 static bool cp_parser_using_declaration
1712   (cp_parser *, bool);
1713 static void cp_parser_using_directive
1714   (cp_parser *);
1715 static void cp_parser_asm_definition
1716   (cp_parser *);
1717 static void cp_parser_linkage_specification
1718   (cp_parser *);
1719 static void cp_parser_static_assert
1720   (cp_parser *, bool);
1721 static tree cp_parser_decltype
1722   (cp_parser *);
1723
1724 /* Declarators [gram.dcl.decl] */
1725
1726 static tree cp_parser_init_declarator
1727   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1728 static cp_declarator *cp_parser_declarator
1729   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1730 static cp_declarator *cp_parser_direct_declarator
1731   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1732 static enum tree_code cp_parser_ptr_operator
1733   (cp_parser *, tree *, cp_cv_quals *);
1734 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1735   (cp_parser *);
1736 static tree cp_parser_late_return_type_opt
1737   (cp_parser *);
1738 static tree cp_parser_declarator_id
1739   (cp_parser *, bool);
1740 static tree cp_parser_type_id
1741   (cp_parser *);
1742 static tree cp_parser_template_type_arg
1743   (cp_parser *);
1744 static tree cp_parser_type_id_1
1745   (cp_parser *, bool);
1746 static void cp_parser_type_specifier_seq
1747   (cp_parser *, bool, cp_decl_specifier_seq *);
1748 static tree cp_parser_parameter_declaration_clause
1749   (cp_parser *);
1750 static tree cp_parser_parameter_declaration_list
1751   (cp_parser *, bool *);
1752 static cp_parameter_declarator *cp_parser_parameter_declaration
1753   (cp_parser *, bool, bool *);
1754 static tree cp_parser_default_argument 
1755   (cp_parser *, bool);
1756 static void cp_parser_function_body
1757   (cp_parser *);
1758 static tree cp_parser_initializer
1759   (cp_parser *, bool *, bool *);
1760 static tree cp_parser_initializer_clause
1761   (cp_parser *, bool *);
1762 static tree cp_parser_braced_list
1763   (cp_parser*, bool*);
1764 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1765   (cp_parser *, bool *);
1766
1767 static bool cp_parser_ctor_initializer_opt_and_function_body
1768   (cp_parser *);
1769
1770 /* Classes [gram.class] */
1771
1772 static tree cp_parser_class_name
1773   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1774 static tree cp_parser_class_specifier
1775   (cp_parser *);
1776 static tree cp_parser_class_head
1777   (cp_parser *, bool *, tree *, tree *);
1778 static enum tag_types cp_parser_class_key
1779   (cp_parser *);
1780 static void cp_parser_member_specification_opt
1781   (cp_parser *);
1782 static void cp_parser_member_declaration
1783   (cp_parser *);
1784 static tree cp_parser_pure_specifier
1785   (cp_parser *);
1786 static tree cp_parser_constant_initializer
1787   (cp_parser *);
1788
1789 /* Derived classes [gram.class.derived] */
1790
1791 static tree cp_parser_base_clause
1792   (cp_parser *);
1793 static tree cp_parser_base_specifier
1794   (cp_parser *);
1795
1796 /* Special member functions [gram.special] */
1797
1798 static tree cp_parser_conversion_function_id
1799   (cp_parser *);
1800 static tree cp_parser_conversion_type_id
1801   (cp_parser *);
1802 static cp_declarator *cp_parser_conversion_declarator_opt
1803   (cp_parser *);
1804 static bool cp_parser_ctor_initializer_opt
1805   (cp_parser *);
1806 static void cp_parser_mem_initializer_list
1807   (cp_parser *);
1808 static tree cp_parser_mem_initializer
1809   (cp_parser *);
1810 static tree cp_parser_mem_initializer_id
1811   (cp_parser *);
1812
1813 /* Overloading [gram.over] */
1814
1815 static tree cp_parser_operator_function_id
1816   (cp_parser *);
1817 static tree cp_parser_operator
1818   (cp_parser *);
1819
1820 /* Templates [gram.temp] */
1821
1822 static void cp_parser_template_declaration
1823   (cp_parser *, bool);
1824 static tree cp_parser_template_parameter_list
1825   (cp_parser *);
1826 static tree cp_parser_template_parameter
1827   (cp_parser *, bool *, bool *);
1828 static tree cp_parser_type_parameter
1829   (cp_parser *, bool *);
1830 static tree cp_parser_template_id
1831   (cp_parser *, bool, bool, bool);
1832 static tree cp_parser_template_name
1833   (cp_parser *, bool, bool, bool, bool *);
1834 static tree cp_parser_template_argument_list
1835   (cp_parser *);
1836 static tree cp_parser_template_argument
1837   (cp_parser *);
1838 static void cp_parser_explicit_instantiation
1839   (cp_parser *);
1840 static void cp_parser_explicit_specialization
1841   (cp_parser *);
1842
1843 /* Exception handling [gram.exception] */
1844
1845 static tree cp_parser_try_block
1846   (cp_parser *);
1847 static bool cp_parser_function_try_block
1848   (cp_parser *);
1849 static void cp_parser_handler_seq
1850   (cp_parser *);
1851 static void cp_parser_handler
1852   (cp_parser *);
1853 static tree cp_parser_exception_declaration
1854   (cp_parser *);
1855 static tree cp_parser_throw_expression
1856   (cp_parser *);
1857 static tree cp_parser_exception_specification_opt
1858   (cp_parser *);
1859 static tree cp_parser_type_id_list
1860   (cp_parser *);
1861
1862 /* GNU Extensions */
1863
1864 static tree cp_parser_asm_specification_opt
1865   (cp_parser *);
1866 static tree cp_parser_asm_operand_list
1867   (cp_parser *);
1868 static tree cp_parser_asm_clobber_list
1869   (cp_parser *);
1870 static tree cp_parser_asm_label_list
1871   (cp_parser *);
1872 static tree cp_parser_attributes_opt
1873   (cp_parser *);
1874 static tree cp_parser_attribute_list
1875   (cp_parser *);
1876 static bool cp_parser_extension_opt
1877   (cp_parser *, int *);
1878 static void cp_parser_label_declaration
1879   (cp_parser *);
1880
1881 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1882 static bool cp_parser_pragma
1883   (cp_parser *, enum pragma_context);
1884
1885 /* Objective-C++ Productions */
1886
1887 static tree cp_parser_objc_message_receiver
1888   (cp_parser *);
1889 static tree cp_parser_objc_message_args
1890   (cp_parser *);
1891 static tree cp_parser_objc_message_expression
1892   (cp_parser *);
1893 static tree cp_parser_objc_encode_expression
1894   (cp_parser *);
1895 static tree cp_parser_objc_defs_expression
1896   (cp_parser *);
1897 static tree cp_parser_objc_protocol_expression
1898   (cp_parser *);
1899 static tree cp_parser_objc_selector_expression
1900   (cp_parser *);
1901 static tree cp_parser_objc_expression
1902   (cp_parser *);
1903 static bool cp_parser_objc_selector_p
1904   (enum cpp_ttype);
1905 static tree cp_parser_objc_selector
1906   (cp_parser *);
1907 static tree cp_parser_objc_protocol_refs_opt
1908   (cp_parser *);
1909 static void cp_parser_objc_declaration
1910   (cp_parser *);
1911 static tree cp_parser_objc_statement
1912   (cp_parser *);
1913
1914 /* Utility Routines */
1915
1916 static tree cp_parser_lookup_name
1917   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1918 static tree cp_parser_lookup_name_simple
1919   (cp_parser *, tree, location_t);
1920 static tree cp_parser_maybe_treat_template_as_class
1921   (tree, bool);
1922 static bool cp_parser_check_declarator_template_parameters
1923   (cp_parser *, cp_declarator *, location_t);
1924 static bool cp_parser_check_template_parameters
1925   (cp_parser *, unsigned, location_t, cp_declarator *);
1926 static tree cp_parser_simple_cast_expression
1927   (cp_parser *);
1928 static tree cp_parser_global_scope_opt
1929   (cp_parser *, bool);
1930 static bool cp_parser_constructor_declarator_p
1931   (cp_parser *, bool);
1932 static tree cp_parser_function_definition_from_specifiers_and_declarator
1933   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1934 static tree cp_parser_function_definition_after_declarator
1935   (cp_parser *, bool);
1936 static void cp_parser_template_declaration_after_export
1937   (cp_parser *, bool);
1938 static void cp_parser_perform_template_parameter_access_checks
1939   (VEC (deferred_access_check,gc)*);
1940 static tree cp_parser_single_declaration
1941   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1942 static tree cp_parser_functional_cast
1943   (cp_parser *, tree);
1944 static tree cp_parser_save_member_function_body
1945   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1946 static tree cp_parser_enclosed_template_argument_list
1947   (cp_parser *);
1948 static void cp_parser_save_default_args
1949   (cp_parser *, tree);
1950 static void cp_parser_late_parsing_for_member
1951   (cp_parser *, tree);
1952 static void cp_parser_late_parsing_default_args
1953   (cp_parser *, tree);
1954 static tree cp_parser_sizeof_operand
1955   (cp_parser *, enum rid);
1956 static tree cp_parser_trait_expr
1957   (cp_parser *, enum rid);
1958 static bool cp_parser_declares_only_class_p
1959   (cp_parser *);
1960 static void cp_parser_set_storage_class
1961   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1962 static void cp_parser_set_decl_spec_type
1963   (cp_decl_specifier_seq *, tree, location_t, bool);
1964 static bool cp_parser_friend_p
1965   (const cp_decl_specifier_seq *);
1966 static cp_token *cp_parser_require
1967   (cp_parser *, enum cpp_ttype, const char *);
1968 static cp_token *cp_parser_require_keyword
1969   (cp_parser *, enum rid, const char *);
1970 static bool cp_parser_token_starts_function_definition_p
1971   (cp_token *);
1972 static bool cp_parser_next_token_starts_class_definition_p
1973   (cp_parser *);
1974 static bool cp_parser_next_token_ends_template_argument_p
1975   (cp_parser *);
1976 static bool cp_parser_nth_token_starts_template_argument_list_p
1977   (cp_parser *, size_t);
1978 static enum tag_types cp_parser_token_is_class_key
1979   (cp_token *);
1980 static void cp_parser_check_class_key
1981   (enum tag_types, tree type);
1982 static void cp_parser_check_access_in_redeclaration
1983   (tree type, location_t location);
1984 static bool cp_parser_optional_template_keyword
1985   (cp_parser *);
1986 static void cp_parser_pre_parsed_nested_name_specifier
1987   (cp_parser *);
1988 static bool cp_parser_cache_group
1989   (cp_parser *, enum cpp_ttype, unsigned);
1990 static void cp_parser_parse_tentatively
1991   (cp_parser *);
1992 static void cp_parser_commit_to_tentative_parse
1993   (cp_parser *);
1994 static void cp_parser_abort_tentative_parse
1995   (cp_parser *);
1996 static bool cp_parser_parse_definitely
1997   (cp_parser *);
1998 static inline bool cp_parser_parsing_tentatively
1999   (cp_parser *);
2000 static bool cp_parser_uncommitted_to_tentative_parse_p
2001   (cp_parser *);
2002 static void cp_parser_error
2003   (cp_parser *, const char *);
2004 static void cp_parser_name_lookup_error
2005   (cp_parser *, tree, tree, const char *, location_t);
2006 static bool cp_parser_simulate_error
2007   (cp_parser *);
2008 static bool cp_parser_check_type_definition
2009   (cp_parser *);
2010 static void cp_parser_check_for_definition_in_return_type
2011   (cp_declarator *, tree, location_t type_location);
2012 static void cp_parser_check_for_invalid_template_id
2013   (cp_parser *, tree, location_t location);
2014 static bool cp_parser_non_integral_constant_expression
2015   (cp_parser *, const char *);
2016 static void cp_parser_diagnose_invalid_type_name
2017   (cp_parser *, tree, tree, location_t);
2018 static bool cp_parser_parse_and_diagnose_invalid_type_name
2019   (cp_parser *);
2020 static int cp_parser_skip_to_closing_parenthesis
2021   (cp_parser *, bool, bool, bool);
2022 static void cp_parser_skip_to_end_of_statement
2023   (cp_parser *);
2024 static void cp_parser_consume_semicolon_at_end_of_statement
2025   (cp_parser *);
2026 static void cp_parser_skip_to_end_of_block_or_statement
2027   (cp_parser *);
2028 static bool cp_parser_skip_to_closing_brace
2029   (cp_parser *);
2030 static void cp_parser_skip_to_end_of_template_parameter_list
2031   (cp_parser *);
2032 static void cp_parser_skip_to_pragma_eol
2033   (cp_parser*, cp_token *);
2034 static bool cp_parser_error_occurred
2035   (cp_parser *);
2036 static bool cp_parser_allow_gnu_extensions_p
2037   (cp_parser *);
2038 static bool cp_parser_is_string_literal
2039   (cp_token *);
2040 static bool cp_parser_is_keyword
2041   (cp_token *, enum rid);
2042 static tree cp_parser_make_typename_type
2043   (cp_parser *, tree, tree, location_t location);
2044 static cp_declarator * cp_parser_make_indirect_declarator
2045   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2046
2047 /* Returns nonzero if we are parsing tentatively.  */
2048
2049 static inline bool
2050 cp_parser_parsing_tentatively (cp_parser* parser)
2051 {
2052   return parser->context->next != NULL;
2053 }
2054
2055 /* Returns nonzero if TOKEN is a string literal.  */
2056
2057 static bool
2058 cp_parser_is_string_literal (cp_token* token)
2059 {
2060   return (token->type == CPP_STRING ||
2061           token->type == CPP_STRING16 ||
2062           token->type == CPP_STRING32 ||
2063           token->type == CPP_WSTRING);
2064 }
2065
2066 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2067
2068 static bool
2069 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2070 {
2071   return token->keyword == keyword;
2072 }
2073
2074 /* If not parsing tentatively, issue a diagnostic of the form
2075       FILE:LINE: MESSAGE before TOKEN
2076    where TOKEN is the next token in the input stream.  MESSAGE
2077    (specified by the caller) is usually of the form "expected
2078    OTHER-TOKEN".  */
2079
2080 static void
2081 cp_parser_error (cp_parser* parser, const char* message)
2082 {
2083   if (!cp_parser_simulate_error (parser))
2084     {
2085       cp_token *token = cp_lexer_peek_token (parser->lexer);
2086       /* This diagnostic makes more sense if it is tagged to the line
2087          of the token we just peeked at.  */
2088       cp_lexer_set_source_position_from_token (token);
2089
2090       if (token->type == CPP_PRAGMA)
2091         {
2092           error_at (token->location,
2093                     "%<#pragma%> is not allowed here");
2094           cp_parser_skip_to_pragma_eol (parser, token);
2095           return;
2096         }
2097
2098       c_parse_error (message,
2099                      /* Because c_parser_error does not understand
2100                         CPP_KEYWORD, keywords are treated like
2101                         identifiers.  */
2102                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2103                      token->u.value, token->flags);
2104     }
2105 }
2106
2107 /* Issue an error about name-lookup failing.  NAME is the
2108    IDENTIFIER_NODE DECL is the result of
2109    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2110    the thing that we hoped to find.  */
2111
2112 static void
2113 cp_parser_name_lookup_error (cp_parser* parser,
2114                              tree name,
2115                              tree decl,
2116                              const char* desired,
2117                              location_t location)
2118 {
2119   /* If name lookup completely failed, tell the user that NAME was not
2120      declared.  */
2121   if (decl == error_mark_node)
2122     {
2123       if (parser->scope && parser->scope != global_namespace)
2124         error_at (location, "%<%E::%E%> has not been declared",
2125                   parser->scope, name);
2126       else if (parser->scope == global_namespace)
2127         error_at (location, "%<::%E%> has not been declared", name);
2128       else if (parser->object_scope
2129                && !CLASS_TYPE_P (parser->object_scope))
2130         error_at (location, "request for member %qE in non-class type %qT",
2131                   name, parser->object_scope);
2132       else if (parser->object_scope)
2133         error_at (location, "%<%T::%E%> has not been declared",
2134                   parser->object_scope, name);
2135       else
2136         error_at (location, "%qE has not been declared", name);
2137     }
2138   else if (parser->scope && parser->scope != global_namespace)
2139     error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2140   else if (parser->scope == global_namespace)
2141     error_at (location, "%<::%E%> %s", name, desired);
2142   else
2143     error_at (location, "%qE %s", name, desired);
2144 }
2145
2146 /* If we are parsing tentatively, remember that an error has occurred
2147    during this tentative parse.  Returns true if the error was
2148    simulated; false if a message should be issued by the caller.  */
2149
2150 static bool
2151 cp_parser_simulate_error (cp_parser* parser)
2152 {
2153   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2154     {
2155       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2156       return true;
2157     }
2158   return false;
2159 }
2160
2161 /* Check for repeated decl-specifiers.  */
2162
2163 static void
2164 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2165                            location_t location)
2166 {
2167   int ds;
2168
2169   for (ds = ds_first; ds != ds_last; ++ds)
2170     {
2171       unsigned count = decl_specs->specs[ds];
2172       if (count < 2)
2173         continue;
2174       /* The "long" specifier is a special case because of "long long".  */
2175       if (ds == ds_long)
2176         {
2177           if (count > 2)
2178             error_at (location, "%<long long long%> is too long for GCC");
2179           else 
2180             pedwarn_cxx98 (location, OPT_Wlong_long, 
2181                            "ISO C++ 1998 does not support %<long long%>");
2182         }
2183       else if (count > 1)
2184         {
2185           static const char *const decl_spec_names[] = {
2186             "signed",
2187             "unsigned",
2188             "short",
2189             "long",
2190             "const",
2191             "volatile",
2192             "restrict",
2193             "inline",
2194             "virtual",
2195             "explicit",
2196             "friend",
2197             "typedef",
2198             "__complex",
2199             "__thread"
2200           };
2201           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2202         }
2203     }
2204 }
2205
2206 /* This function is called when a type is defined.  If type
2207    definitions are forbidden at this point, an error message is
2208    issued.  */
2209
2210 static bool
2211 cp_parser_check_type_definition (cp_parser* parser)
2212 {
2213   /* If types are forbidden here, issue a message.  */
2214   if (parser->type_definition_forbidden_message)
2215     {
2216       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2217          in the message need to be interpreted.  */
2218       error (parser->type_definition_forbidden_message);
2219       return false;
2220     }
2221   return true;
2222 }
2223
2224 /* This function is called when the DECLARATOR is processed.  The TYPE
2225    was a type defined in the decl-specifiers.  If it is invalid to
2226    define a type in the decl-specifiers for DECLARATOR, an error is
2227    issued. TYPE_LOCATION is the location of TYPE and is used
2228    for error reporting.  */
2229
2230 static void
2231 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2232                                                tree type, location_t type_location)
2233 {
2234   /* [dcl.fct] forbids type definitions in return types.
2235      Unfortunately, it's not easy to know whether or not we are
2236      processing a return type until after the fact.  */
2237   while (declarator
2238          && (declarator->kind == cdk_pointer
2239              || declarator->kind == cdk_reference
2240              || declarator->kind == cdk_ptrmem))
2241     declarator = declarator->declarator;
2242   if (declarator
2243       && declarator->kind == cdk_function)
2244     {
2245       error_at (type_location,
2246                 "new types may not be defined in a return type");
2247       inform (type_location, 
2248               "(perhaps a semicolon is missing after the definition of %qT)",
2249               type);
2250     }
2251 }
2252
2253 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2254    "<" in any valid C++ program.  If the next token is indeed "<",
2255    issue a message warning the user about what appears to be an
2256    invalid attempt to form a template-id. LOCATION is the location
2257    of the type-specifier (TYPE) */
2258
2259 static void
2260 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2261                                          tree type, location_t location)
2262 {
2263   cp_token_position start = 0;
2264
2265   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2266     {
2267       if (TYPE_P (type))
2268         error_at (location, "%qT is not a template", type);
2269       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2270         error_at (location, "%qE is not a template", type);
2271       else
2272         error_at (location, "invalid template-id");
2273       /* Remember the location of the invalid "<".  */
2274       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2275         start = cp_lexer_token_position (parser->lexer, true);
2276       /* Consume the "<".  */
2277       cp_lexer_consume_token (parser->lexer);
2278       /* Parse the template arguments.  */
2279       cp_parser_enclosed_template_argument_list (parser);
2280       /* Permanently remove the invalid template arguments so that
2281          this error message is not issued again.  */
2282       if (start)
2283         cp_lexer_purge_tokens_after (parser->lexer, start);
2284     }
2285 }
2286
2287 /* If parsing an integral constant-expression, issue an error message
2288    about the fact that THING appeared and return true.  Otherwise,
2289    return false.  In either case, set
2290    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2291
2292 static bool
2293 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2294                                             const char *thing)
2295 {
2296   parser->non_integral_constant_expression_p = true;
2297   if (parser->integral_constant_expression_p)
2298     {
2299       if (!parser->allow_non_integral_constant_expression_p)
2300         {
2301           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2302              in the message need to be interpreted.  */
2303           char *message = concat (thing,
2304                                   " cannot appear in a constant-expression",
2305                                   NULL);
2306           error (message);
2307           free (message);
2308           return true;
2309         }
2310     }
2311   return false;
2312 }
2313
2314 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2315    qualifying scope (or NULL, if none) for ID.  This function commits
2316    to the current active tentative parse, if any.  (Otherwise, the
2317    problematic construct might be encountered again later, resulting
2318    in duplicate error messages.) LOCATION is the location of ID.  */
2319
2320 static void
2321 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2322                                       tree scope, tree id,
2323                                       location_t location)
2324 {
2325   tree decl, old_scope;
2326   /* Try to lookup the identifier.  */
2327   old_scope = parser->scope;
2328   parser->scope = scope;
2329   decl = cp_parser_lookup_name_simple (parser, id, location);
2330   parser->scope = old_scope;
2331   /* If the lookup found a template-name, it means that the user forgot
2332   to specify an argument list. Emit a useful error message.  */
2333   if (TREE_CODE (decl) == TEMPLATE_DECL)
2334     error_at (location,
2335               "invalid use of template-name %qE without an argument list",
2336               decl);
2337   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2338     error_at (location, "invalid use of destructor %qD as a type", id);
2339   else if (TREE_CODE (decl) == TYPE_DECL)
2340     /* Something like 'unsigned A a;'  */
2341     error_at (location, "invalid combination of multiple type-specifiers");
2342   else if (!parser->scope)
2343     {
2344       /* Issue an error message.  */
2345       error_at (location, "%qE does not name a type", id);
2346       /* If we're in a template class, it's possible that the user was
2347          referring to a type from a base class.  For example:
2348
2349            template <typename T> struct A { typedef T X; };
2350            template <typename T> struct B : public A<T> { X x; };
2351
2352          The user should have said "typename A<T>::X".  */
2353       if (processing_template_decl && current_class_type
2354           && TYPE_BINFO (current_class_type))
2355         {
2356           tree b;
2357
2358           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2359                b;
2360                b = TREE_CHAIN (b))
2361             {
2362               tree base_type = BINFO_TYPE (b);
2363               if (CLASS_TYPE_P (base_type)
2364                   && dependent_type_p (base_type))
2365                 {
2366                   tree field;
2367                   /* Go from a particular instantiation of the
2368                      template (which will have an empty TYPE_FIELDs),
2369                      to the main version.  */
2370                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2371                   for (field = TYPE_FIELDS (base_type);
2372                        field;
2373                        field = TREE_CHAIN (field))
2374                     if (TREE_CODE (field) == TYPE_DECL
2375                         && DECL_NAME (field) == id)
2376                       {
2377                         inform (location, 
2378                                 "(perhaps %<typename %T::%E%> was intended)",
2379                                 BINFO_TYPE (b), id);
2380                         break;
2381                       }
2382                   if (field)
2383                     break;
2384                 }
2385             }
2386         }
2387     }
2388   /* Here we diagnose qualified-ids where the scope is actually correct,
2389      but the identifier does not resolve to a valid type name.  */
2390   else if (parser->scope != error_mark_node)
2391     {
2392       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2393         error_at (location, "%qE in namespace %qE does not name a type",
2394                   id, parser->scope);
2395       else if (TYPE_P (parser->scope))
2396         error_at (location, "%qE in class %qT does not name a type",
2397                   id, parser->scope);
2398       else
2399         gcc_unreachable ();
2400     }
2401   cp_parser_commit_to_tentative_parse (parser);
2402 }
2403
2404 /* Check for a common situation where a type-name should be present,
2405    but is not, and issue a sensible error message.  Returns true if an
2406    invalid type-name was detected.
2407
2408    The situation handled by this function are variable declarations of the
2409    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2410    Usually, `ID' should name a type, but if we got here it means that it
2411    does not. We try to emit the best possible error message depending on
2412    how exactly the id-expression looks like.  */
2413
2414 static bool
2415 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2416 {
2417   tree id;
2418   cp_token *token = cp_lexer_peek_token (parser->lexer);
2419
2420   cp_parser_parse_tentatively (parser);
2421   id = cp_parser_id_expression (parser,
2422                                 /*template_keyword_p=*/false,
2423                                 /*check_dependency_p=*/true,
2424                                 /*template_p=*/NULL,
2425                                 /*declarator_p=*/true,
2426                                 /*optional_p=*/false);
2427   /* After the id-expression, there should be a plain identifier,
2428      otherwise this is not a simple variable declaration. Also, if
2429      the scope is dependent, we cannot do much.  */
2430   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2431       || (parser->scope && TYPE_P (parser->scope)
2432           && dependent_type_p (parser->scope))
2433       || TREE_CODE (id) == TYPE_DECL)
2434     {
2435       cp_parser_abort_tentative_parse (parser);
2436       return false;
2437     }
2438   if (!cp_parser_parse_definitely (parser))
2439     return false;
2440
2441   /* Emit a diagnostic for the invalid type.  */
2442   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2443                                         id, token->location);
2444   /* Skip to the end of the declaration; there's no point in
2445      trying to process it.  */
2446   cp_parser_skip_to_end_of_block_or_statement (parser);
2447   return true;
2448 }
2449
2450 /* Consume tokens up to, and including, the next non-nested closing `)'.
2451    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2452    are doing error recovery. Returns -1 if OR_COMMA is true and we
2453    found an unnested comma.  */
2454
2455 static int
2456 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2457                                        bool recovering,
2458                                        bool or_comma,
2459                                        bool consume_paren)
2460 {
2461   unsigned paren_depth = 0;
2462   unsigned brace_depth = 0;
2463   unsigned square_depth = 0;
2464
2465   if (recovering && !or_comma
2466       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2467     return 0;
2468
2469   while (true)
2470     {
2471       cp_token * token = cp_lexer_peek_token (parser->lexer);
2472
2473       switch (token->type)
2474         {
2475         case CPP_EOF:
2476         case CPP_PRAGMA_EOL:
2477           /* If we've run out of tokens, then there is no closing `)'.  */
2478           return 0;
2479
2480         /* This is good for lambda expression capture-lists.  */
2481         case CPP_OPEN_SQUARE:
2482           ++square_depth;
2483           break;
2484         case CPP_CLOSE_SQUARE:
2485           if (!square_depth--)
2486             return 0;
2487           break;
2488
2489         case CPP_SEMICOLON:
2490           /* This matches the processing in skip_to_end_of_statement.  */
2491           if (!brace_depth)
2492             return 0;
2493           break;
2494
2495         case CPP_OPEN_BRACE:
2496           ++brace_depth;
2497           break;
2498         case CPP_CLOSE_BRACE:
2499           if (!brace_depth--)
2500             return 0;
2501           break;
2502
2503         case CPP_COMMA:
2504           if (recovering && or_comma && !brace_depth && !paren_depth
2505               && !square_depth)
2506             return -1;
2507           break;
2508
2509         case CPP_OPEN_PAREN:
2510           if (!brace_depth)
2511             ++paren_depth;
2512           break;
2513
2514         case CPP_CLOSE_PAREN:
2515           if (!brace_depth && !paren_depth--)
2516             {
2517               if (consume_paren)
2518                 cp_lexer_consume_token (parser->lexer);
2519               return 1;
2520             }
2521           break;
2522
2523         default:
2524           break;
2525         }
2526
2527       /* Consume the token.  */
2528       cp_lexer_consume_token (parser->lexer);
2529     }
2530 }
2531
2532 /* Consume tokens until we reach the end of the current statement.
2533    Normally, that will be just before consuming a `;'.  However, if a
2534    non-nested `}' comes first, then we stop before consuming that.  */
2535
2536 static void
2537 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2538 {
2539   unsigned nesting_depth = 0;
2540
2541   while (true)
2542     {
2543       cp_token *token = cp_lexer_peek_token (parser->lexer);
2544
2545       switch (token->type)
2546         {
2547         case CPP_EOF:
2548         case CPP_PRAGMA_EOL:
2549           /* If we've run out of tokens, stop.  */
2550           return;
2551
2552         case CPP_SEMICOLON:
2553           /* If the next token is a `;', we have reached the end of the
2554              statement.  */
2555           if (!nesting_depth)
2556             return;
2557           break;
2558
2559         case CPP_CLOSE_BRACE:
2560           /* If this is a non-nested '}', stop before consuming it.
2561              That way, when confronted with something like:
2562
2563                { 3 + }
2564
2565              we stop before consuming the closing '}', even though we
2566              have not yet reached a `;'.  */
2567           if (nesting_depth == 0)
2568             return;
2569
2570           /* If it is the closing '}' for a block that we have
2571              scanned, stop -- but only after consuming the token.
2572              That way given:
2573
2574                 void f g () { ... }
2575                 typedef int I;
2576
2577              we will stop after the body of the erroneously declared
2578              function, but before consuming the following `typedef'
2579              declaration.  */
2580           if (--nesting_depth == 0)
2581             {
2582               cp_lexer_consume_token (parser->lexer);
2583               return;
2584             }
2585
2586         case CPP_OPEN_BRACE:
2587           ++nesting_depth;
2588           break;
2589
2590         default:
2591           break;
2592         }
2593
2594       /* Consume the token.  */
2595       cp_lexer_consume_token (parser->lexer);
2596     }
2597 }
2598
2599 /* This function is called at the end of a statement or declaration.
2600    If the next token is a semicolon, it is consumed; otherwise, error
2601    recovery is attempted.  */
2602
2603 static void
2604 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2605 {
2606   /* Look for the trailing `;'.  */
2607   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2608     {
2609       /* If there is additional (erroneous) input, skip to the end of
2610          the statement.  */
2611       cp_parser_skip_to_end_of_statement (parser);
2612       /* If the next token is now a `;', consume it.  */
2613       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2614         cp_lexer_consume_token (parser->lexer);
2615     }
2616 }
2617
2618 /* Skip tokens until we have consumed an entire block, or until we
2619    have consumed a non-nested `;'.  */
2620
2621 static void
2622 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2623 {
2624   int nesting_depth = 0;
2625
2626   while (nesting_depth >= 0)
2627     {
2628       cp_token *token = cp_lexer_peek_token (parser->lexer);
2629
2630       switch (token->type)
2631         {
2632         case CPP_EOF:
2633         case CPP_PRAGMA_EOL:
2634           /* If we've run out of tokens, stop.  */
2635           return;
2636
2637         case CPP_SEMICOLON:
2638           /* Stop if this is an unnested ';'. */
2639           if (!nesting_depth)
2640             nesting_depth = -1;
2641           break;
2642
2643         case CPP_CLOSE_BRACE:
2644           /* Stop if this is an unnested '}', or closes the outermost
2645              nesting level.  */
2646           nesting_depth--;
2647           if (nesting_depth < 0)
2648             return;
2649           if (!nesting_depth)
2650             nesting_depth = -1;
2651           break;
2652
2653         case CPP_OPEN_BRACE:
2654           /* Nest. */
2655           nesting_depth++;
2656           break;
2657
2658         default:
2659           break;
2660         }
2661
2662       /* Consume the token.  */
2663       cp_lexer_consume_token (parser->lexer);
2664     }
2665 }
2666
2667 /* Skip tokens until a non-nested closing curly brace is the next
2668    token, or there are no more tokens. Return true in the first case,
2669    false otherwise.  */
2670
2671 static bool
2672 cp_parser_skip_to_closing_brace (cp_parser *parser)
2673 {
2674   unsigned nesting_depth = 0;
2675
2676   while (true)
2677     {
2678       cp_token *token = cp_lexer_peek_token (parser->lexer);
2679
2680       switch (token->type)
2681         {
2682         case CPP_EOF:
2683         case CPP_PRAGMA_EOL:
2684           /* If we've run out of tokens, stop.  */
2685           return false;
2686
2687         case CPP_CLOSE_BRACE:
2688           /* If the next token is a non-nested `}', then we have reached
2689              the end of the current block.  */
2690           if (nesting_depth-- == 0)
2691             return true;
2692           break;
2693
2694         case CPP_OPEN_BRACE:
2695           /* If it the next token is a `{', then we are entering a new
2696              block.  Consume the entire block.  */
2697           ++nesting_depth;
2698           break;
2699
2700         default:
2701           break;
2702         }
2703
2704       /* Consume the token.  */
2705       cp_lexer_consume_token (parser->lexer);
2706     }
2707 }
2708
2709 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2710    parameter is the PRAGMA token, allowing us to purge the entire pragma
2711    sequence.  */
2712
2713 static void
2714 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2715 {
2716   cp_token *token;
2717
2718   parser->lexer->in_pragma = false;
2719
2720   do
2721     token = cp_lexer_consume_token (parser->lexer);
2722   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2723
2724   /* Ensure that the pragma is not parsed again.  */
2725   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2726 }
2727
2728 /* Require pragma end of line, resyncing with it as necessary.  The
2729    arguments are as for cp_parser_skip_to_pragma_eol.  */
2730
2731 static void
2732 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2733 {
2734   parser->lexer->in_pragma = false;
2735   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2736     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2737 }
2738
2739 /* This is a simple wrapper around make_typename_type. When the id is
2740    an unresolved identifier node, we can provide a superior diagnostic
2741    using cp_parser_diagnose_invalid_type_name.  */
2742
2743 static tree
2744 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2745                               tree id, location_t id_location)
2746 {
2747   tree result;
2748   if (TREE_CODE (id) == IDENTIFIER_NODE)
2749     {
2750       result = make_typename_type (scope, id, typename_type,
2751                                    /*complain=*/tf_none);
2752       if (result == error_mark_node)
2753         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2754       return result;
2755     }
2756   return make_typename_type (scope, id, typename_type, tf_error);
2757 }
2758
2759 /* This is a wrapper around the
2760    make_{pointer,ptrmem,reference}_declarator functions that decides
2761    which one to call based on the CODE and CLASS_TYPE arguments. The
2762    CODE argument should be one of the values returned by
2763    cp_parser_ptr_operator. */
2764 static cp_declarator *
2765 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2766                                     cp_cv_quals cv_qualifiers,
2767                                     cp_declarator *target)
2768 {
2769   if (code == ERROR_MARK)
2770     return cp_error_declarator;
2771
2772   if (code == INDIRECT_REF)
2773     if (class_type == NULL_TREE)
2774       return make_pointer_declarator (cv_qualifiers, target);
2775     else
2776       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2777   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2778     return make_reference_declarator (cv_qualifiers, target, false);
2779   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2780     return make_reference_declarator (cv_qualifiers, target, true);
2781   gcc_unreachable ();
2782 }
2783
2784 /* Create a new C++ parser.  */
2785
2786 static cp_parser *
2787 cp_parser_new (void)
2788 {
2789   cp_parser *parser;
2790   cp_lexer *lexer;
2791   unsigned i;
2792
2793   /* cp_lexer_new_main is called before calling ggc_alloc because
2794      cp_lexer_new_main might load a PCH file.  */
2795   lexer = cp_lexer_new_main ();
2796
2797   /* Initialize the binops_by_token so that we can get the tree
2798      directly from the token.  */
2799   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2800     binops_by_token[binops[i].token_type] = binops[i];
2801
2802   parser = GGC_CNEW (cp_parser);
2803   parser->lexer = lexer;
2804   parser->context = cp_parser_context_new (NULL);
2805
2806   /* For now, we always accept GNU extensions.  */
2807   parser->allow_gnu_extensions_p = 1;
2808
2809   /* The `>' token is a greater-than operator, not the end of a
2810      template-id.  */
2811   parser->greater_than_is_operator_p = true;
2812
2813   parser->default_arg_ok_p = true;
2814
2815   /* We are not parsing a constant-expression.  */
2816   parser->integral_constant_expression_p = false;
2817   parser->allow_non_integral_constant_expression_p = false;
2818   parser->non_integral_constant_expression_p = false;
2819
2820   /* Local variable names are not forbidden.  */
2821   parser->local_variables_forbidden_p = false;
2822
2823   /* We are not processing an `extern "C"' declaration.  */
2824   parser->in_unbraced_linkage_specification_p = false;
2825
2826   /* We are not processing a declarator.  */
2827   parser->in_declarator_p = false;
2828
2829   /* We are not processing a template-argument-list.  */
2830   parser->in_template_argument_list_p = false;
2831
2832   /* We are not in an iteration statement.  */
2833   parser->in_statement = 0;
2834
2835   /* We are not in a switch statement.  */
2836   parser->in_switch_statement_p = false;
2837
2838   /* We are not parsing a type-id inside an expression.  */
2839   parser->in_type_id_in_expr_p = false;
2840
2841   /* Declarations aren't implicitly extern "C".  */
2842   parser->implicit_extern_c = false;
2843
2844   /* String literals should be translated to the execution character set.  */
2845   parser->translate_strings_p = true;
2846
2847   /* We are not parsing a function body.  */
2848   parser->in_function_body = false;
2849
2850   /* The unparsed function queue is empty.  */
2851   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2852
2853   /* There are no classes being defined.  */
2854   parser->num_classes_being_defined = 0;
2855
2856   /* No template parameters apply.  */
2857   parser->num_template_parameter_lists = 0;
2858
2859   return parser;
2860 }
2861
2862 /* Create a cp_lexer structure which will emit the tokens in CACHE
2863    and push it onto the parser's lexer stack.  This is used for delayed
2864    parsing of in-class method bodies and default arguments, and should
2865    not be confused with tentative parsing.  */
2866 static void
2867 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2868 {
2869   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2870   lexer->next = parser->lexer;
2871   parser->lexer = lexer;
2872
2873   /* Move the current source position to that of the first token in the
2874      new lexer.  */
2875   cp_lexer_set_source_position_from_token (lexer->next_token);
2876 }
2877
2878 /* Pop the top lexer off the parser stack.  This is never used for the
2879    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2880 static void
2881 cp_parser_pop_lexer (cp_parser *parser)
2882 {
2883   cp_lexer *lexer = parser->lexer;
2884   parser->lexer = lexer->next;
2885   cp_lexer_destroy (lexer);
2886
2887   /* Put the current source position back where it was before this
2888      lexer was pushed.  */
2889   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2890 }
2891
2892 /* Lexical conventions [gram.lex]  */
2893
2894 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2895    identifier.  */
2896
2897 static tree
2898 cp_parser_identifier (cp_parser* parser)
2899 {
2900   cp_token *token;
2901
2902   /* Look for the identifier.  */
2903   token = cp_parser_require (parser, CPP_NAME, "identifier");
2904   /* Return the value.  */
2905   return token ? token->u.value : error_mark_node;
2906 }
2907
2908 /* Parse a sequence of adjacent string constants.  Returns a
2909    TREE_STRING representing the combined, nul-terminated string
2910    constant.  If TRANSLATE is true, translate the string to the
2911    execution character set.  If WIDE_OK is true, a wide string is
2912    invalid here.
2913
2914    C++98 [lex.string] says that if a narrow string literal token is
2915    adjacent to a wide string literal token, the behavior is undefined.
2916    However, C99 6.4.5p4 says that this results in a wide string literal.
2917    We follow C99 here, for consistency with the C front end.
2918
2919    This code is largely lifted from lex_string() in c-lex.c.
2920
2921    FUTURE: ObjC++ will need to handle @-strings here.  */
2922 static tree
2923 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2924 {
2925   tree value;
2926   size_t count;
2927   struct obstack str_ob;
2928   cpp_string str, istr, *strs;
2929   cp_token *tok;
2930   enum cpp_ttype type;
2931
2932   tok = cp_lexer_peek_token (parser->lexer);
2933   if (!cp_parser_is_string_literal (tok))
2934     {
2935       cp_parser_error (parser, "expected string-literal");
2936       return error_mark_node;
2937     }
2938
2939   type = tok->type;
2940
2941   /* Try to avoid the overhead of creating and destroying an obstack
2942      for the common case of just one string.  */
2943   if (!cp_parser_is_string_literal
2944       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2945     {
2946       cp_lexer_consume_token (parser->lexer);
2947
2948       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2949       str.len = TREE_STRING_LENGTH (tok->u.value);
2950       count = 1;
2951
2952       strs = &str;
2953     }
2954   else
2955     {
2956       gcc_obstack_init (&str_ob);
2957       count = 0;
2958
2959       do
2960         {
2961           cp_lexer_consume_token (parser->lexer);
2962           count++;
2963           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2964           str.len = TREE_STRING_LENGTH (tok->u.value);
2965
2966           if (type != tok->type)
2967             {
2968               if (type == CPP_STRING)
2969                 type = tok->type;
2970               else if (tok->type != CPP_STRING)
2971                 error_at (tok->location,
2972                           "unsupported non-standard concatenation "
2973                           "of string literals");
2974             }
2975
2976           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2977
2978           tok = cp_lexer_peek_token (parser->lexer);
2979         }
2980       while (cp_parser_is_string_literal (tok));
2981
2982       strs = (cpp_string *) obstack_finish (&str_ob);
2983     }
2984
2985   if (type != CPP_STRING && !wide_ok)
2986     {
2987       cp_parser_error (parser, "a wide string is invalid in this context");
2988       type = CPP_STRING;
2989     }
2990
2991   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2992       (parse_in, strs, count, &istr, type))
2993     {
2994       value = build_string (istr.len, (const char *)istr.text);
2995       free (CONST_CAST (unsigned char *, istr.text));
2996
2997       switch (type)
2998         {
2999         default:
3000         case CPP_STRING:
3001           TREE_TYPE (value) = char_array_type_node;
3002           break;
3003         case CPP_STRING16:
3004           TREE_TYPE (value) = char16_array_type_node;
3005           break;
3006         case CPP_STRING32:
3007           TREE_TYPE (value) = char32_array_type_node;
3008           break;
3009         case CPP_WSTRING:
3010           TREE_TYPE (value) = wchar_array_type_node;
3011           break;
3012         }
3013
3014       value = fix_string_type (value);
3015     }
3016   else
3017     /* cpp_interpret_string has issued an error.  */
3018     value = error_mark_node;
3019
3020   if (count > 1)
3021     obstack_free (&str_ob, 0);
3022
3023   return value;
3024 }
3025
3026
3027 /* Basic concepts [gram.basic]  */
3028
3029 /* Parse a translation-unit.
3030
3031    translation-unit:
3032      declaration-seq [opt]
3033
3034    Returns TRUE if all went well.  */
3035
3036 static bool
3037 cp_parser_translation_unit (cp_parser* parser)
3038 {
3039   /* The address of the first non-permanent object on the declarator
3040      obstack.  */
3041   static void *declarator_obstack_base;
3042
3043   bool success;
3044
3045   /* Create the declarator obstack, if necessary.  */
3046   if (!cp_error_declarator)
3047     {
3048       gcc_obstack_init (&declarator_obstack);
3049       /* Create the error declarator.  */
3050       cp_error_declarator = make_declarator (cdk_error);
3051       /* Create the empty parameter list.  */
3052       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3053       /* Remember where the base of the declarator obstack lies.  */
3054       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3055     }
3056
3057   cp_parser_declaration_seq_opt (parser);
3058
3059   /* If there are no tokens left then all went well.  */
3060   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3061     {
3062       /* Get rid of the token array; we don't need it any more.  */
3063       cp_lexer_destroy (parser->lexer);
3064       parser->lexer = NULL;
3065
3066       /* This file might have been a context that's implicitly extern
3067          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3068       if (parser->implicit_extern_c)
3069         {
3070           pop_lang_context ();
3071           parser->implicit_extern_c = false;
3072         }
3073
3074       /* Finish up.  */
3075       finish_translation_unit ();
3076
3077       success = true;
3078     }
3079   else
3080     {
3081       cp_parser_error (parser, "expected declaration");
3082       success = false;
3083     }
3084
3085   /* Make sure the declarator obstack was fully cleaned up.  */
3086   gcc_assert (obstack_next_free (&declarator_obstack)
3087               == declarator_obstack_base);
3088
3089   /* All went well.  */
3090   return success;
3091 }
3092
3093 /* Expressions [gram.expr] */
3094
3095 /* Parse a primary-expression.
3096
3097    primary-expression:
3098      literal
3099      this
3100      ( expression )
3101      id-expression
3102
3103    GNU Extensions:
3104
3105    primary-expression:
3106      ( compound-statement )
3107      __builtin_va_arg ( assignment-expression , type-id )
3108      __builtin_offsetof ( type-id , offsetof-expression )
3109
3110    C++ Extensions:
3111      __has_nothrow_assign ( type-id )   
3112      __has_nothrow_constructor ( type-id )
3113      __has_nothrow_copy ( type-id )
3114      __has_trivial_assign ( type-id )   
3115      __has_trivial_constructor ( type-id )
3116      __has_trivial_copy ( type-id )
3117      __has_trivial_destructor ( type-id )
3118      __has_virtual_destructor ( type-id )     
3119      __is_abstract ( type-id )
3120      __is_base_of ( type-id , type-id )
3121      __is_class ( type-id )
3122      __is_convertible_to ( type-id , type-id )     
3123      __is_empty ( type-id )
3124      __is_enum ( type-id )
3125      __is_pod ( type-id )
3126      __is_polymorphic ( type-id )
3127      __is_union ( type-id )
3128
3129    Objective-C++ Extension:
3130
3131    primary-expression:
3132      objc-expression
3133
3134    literal:
3135      __null
3136
3137    ADDRESS_P is true iff this expression was immediately preceded by
3138    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3139    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3140    true iff this expression is a template argument.
3141
3142    Returns a representation of the expression.  Upon return, *IDK
3143    indicates what kind of id-expression (if any) was present.  */
3144
3145 static tree
3146 cp_parser_primary_expression (cp_parser *parser,
3147                               bool address_p,
3148                               bool cast_p,
3149                               bool template_arg_p,
3150                               cp_id_kind *idk)
3151 {
3152   cp_token *token = NULL;
3153
3154   /* Assume the primary expression is not an id-expression.  */
3155   *idk = CP_ID_KIND_NONE;
3156
3157   /* Peek at the next token.  */
3158   token = cp_lexer_peek_token (parser->lexer);
3159   switch (token->type)
3160     {
3161       /* literal:
3162            integer-literal
3163            character-literal
3164            floating-literal
3165            string-literal
3166            boolean-literal  */
3167     case CPP_CHAR:
3168     case CPP_CHAR16:
3169     case CPP_CHAR32:
3170     case CPP_WCHAR:
3171     case CPP_NUMBER:
3172       token = cp_lexer_consume_token (parser->lexer);
3173       if (TREE_CODE (token->u.value) == FIXED_CST)
3174         {
3175           error_at (token->location,
3176                     "fixed-point types not supported in C++");
3177           return error_mark_node;
3178         }
3179       /* Floating-point literals are only allowed in an integral
3180          constant expression if they are cast to an integral or
3181          enumeration type.  */
3182       if (TREE_CODE (token->u.value) == REAL_CST
3183           && parser->integral_constant_expression_p
3184           && pedantic)
3185         {
3186           /* CAST_P will be set even in invalid code like "int(2.7 +
3187              ...)".   Therefore, we have to check that the next token
3188              is sure to end the cast.  */
3189           if (cast_p)
3190             {
3191               cp_token *next_token;
3192
3193               next_token = cp_lexer_peek_token (parser->lexer);
3194               if (/* The comma at the end of an
3195                      enumerator-definition.  */
3196                   next_token->type != CPP_COMMA
3197                   /* The curly brace at the end of an enum-specifier.  */
3198                   && next_token->type != CPP_CLOSE_BRACE
3199                   /* The end of a statement.  */
3200                   && next_token->type != CPP_SEMICOLON
3201                   /* The end of the cast-expression.  */
3202                   && next_token->type != CPP_CLOSE_PAREN
3203                   /* The end of an array bound.  */
3204                   && next_token->type != CPP_CLOSE_SQUARE
3205                   /* The closing ">" in a template-argument-list.  */
3206                   && (next_token->type != CPP_GREATER
3207                       || parser->greater_than_is_operator_p)
3208                   /* C++0x only: A ">>" treated like two ">" tokens,
3209                      in a template-argument-list.  */
3210                   && (next_token->type != CPP_RSHIFT
3211                       || (cxx_dialect == cxx98)
3212                       || parser->greater_than_is_operator_p))
3213                 cast_p = false;
3214             }
3215
3216           /* If we are within a cast, then the constraint that the
3217              cast is to an integral or enumeration type will be
3218              checked at that point.  If we are not within a cast, then
3219              this code is invalid.  */
3220           if (!cast_p)
3221             cp_parser_non_integral_constant_expression
3222               (parser, "floating-point literal");
3223         }
3224       return token->u.value;
3225
3226     case CPP_STRING:
3227     case CPP_STRING16:
3228     case CPP_STRING32:
3229     case CPP_WSTRING:
3230       /* ??? Should wide strings be allowed when parser->translate_strings_p
3231          is false (i.e. in attributes)?  If not, we can kill the third
3232          argument to cp_parser_string_literal.  */
3233       return cp_parser_string_literal (parser,
3234                                        parser->translate_strings_p,
3235                                        true);
3236
3237     case CPP_OPEN_PAREN:
3238       {
3239         tree expr;
3240         bool saved_greater_than_is_operator_p;
3241
3242         /* Consume the `('.  */
3243         cp_lexer_consume_token (parser->lexer);
3244         /* Within a parenthesized expression, a `>' token is always
3245            the greater-than operator.  */
3246         saved_greater_than_is_operator_p
3247           = parser->greater_than_is_operator_p;
3248         parser->greater_than_is_operator_p = true;
3249         /* If we see `( { ' then we are looking at the beginning of
3250            a GNU statement-expression.  */
3251         if (cp_parser_allow_gnu_extensions_p (parser)
3252             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3253           {
3254             /* Statement-expressions are not allowed by the standard.  */
3255             pedwarn (token->location, OPT_pedantic, 
3256                      "ISO C++ forbids braced-groups within expressions");
3257
3258             /* And they're not allowed outside of a function-body; you
3259                cannot, for example, write:
3260
3261                  int i = ({ int j = 3; j + 1; });
3262
3263                at class or namespace scope.  */
3264             if (!parser->in_function_body
3265                 || parser->in_template_argument_list_p)
3266               {
3267                 error_at (token->location,
3268                           "statement-expressions are not allowed outside "
3269                           "functions nor in template-argument lists");
3270                 cp_parser_skip_to_end_of_block_or_statement (parser);
3271                 expr = error_mark_node;
3272               }
3273             else
3274               {
3275                 /* Start the statement-expression.  */
3276                 expr = begin_stmt_expr ();
3277                 /* Parse the compound-statement.  */
3278                 cp_parser_compound_statement (parser, expr, false);
3279                 /* Finish up.  */
3280                 expr = finish_stmt_expr (expr, false);
3281               }
3282           }
3283         else
3284           {
3285             /* Parse the parenthesized expression.  */
3286             expr = cp_parser_expression (parser, cast_p, idk);
3287             /* Let the front end know that this expression was
3288                enclosed in parentheses. This matters in case, for
3289                example, the expression is of the form `A::B', since
3290                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3291                not.  */
3292             finish_parenthesized_expr (expr);
3293           }
3294         /* The `>' token might be the end of a template-id or
3295            template-parameter-list now.  */
3296         parser->greater_than_is_operator_p
3297           = saved_greater_than_is_operator_p;
3298         /* Consume the `)'.  */
3299         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3300           cp_parser_skip_to_end_of_statement (parser);
3301
3302         return expr;
3303       }
3304
3305     case CPP_OPEN_SQUARE:
3306       if (c_dialect_objc ())
3307         /* We have an Objective-C++ message. */
3308         return cp_parser_objc_expression (parser);
3309       maybe_warn_cpp0x ("lambda expressions");
3310       return cp_parser_lambda_expression (parser);
3311
3312     case CPP_OBJC_STRING:
3313       if (c_dialect_objc ())
3314         /* We have an Objective-C++ string literal. */
3315         return cp_parser_objc_expression (parser);
3316       cp_parser_error (parser, "expected primary-expression");
3317       return error_mark_node;
3318
3319     case CPP_KEYWORD:
3320       switch (token->keyword)
3321         {
3322           /* These two are the boolean literals.  */
3323         case RID_TRUE:
3324           cp_lexer_consume_token (parser->lexer);
3325           return boolean_true_node;
3326         case RID_FALSE:
3327           cp_lexer_consume_token (parser->lexer);
3328           return boolean_false_node;
3329
3330           /* The `__null' literal.  */
3331         case RID_NULL:
3332           cp_lexer_consume_token (parser->lexer);
3333           return null_node;
3334
3335           /* Recognize the `this' keyword.  */
3336         case RID_THIS:
3337           cp_lexer_consume_token (parser->lexer);
3338           if (parser->local_variables_forbidden_p)
3339             {
3340               error_at (token->location,
3341                         "%<this%> may not be used in this context");
3342               return error_mark_node;
3343             }
3344           /* Pointers cannot appear in constant-expressions.  */
3345           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3346             return error_mark_node;
3347           return finish_this_expr ();
3348
3349           /* The `operator' keyword can be the beginning of an
3350              id-expression.  */
3351         case RID_OPERATOR:
3352           goto id_expression;
3353
3354         case RID_FUNCTION_NAME:
3355         case RID_PRETTY_FUNCTION_NAME:
3356         case RID_C99_FUNCTION_NAME:
3357           {
3358             const char *name;
3359
3360             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3361                __func__ are the names of variables -- but they are
3362                treated specially.  Therefore, they are handled here,
3363                rather than relying on the generic id-expression logic
3364                below.  Grammatically, these names are id-expressions.
3365
3366                Consume the token.  */
3367             token = cp_lexer_consume_token (parser->lexer);
3368
3369             switch (token->keyword)
3370               {
3371               case RID_FUNCTION_NAME:
3372                 name = "%<__FUNCTION__%>";
3373                 break;
3374               case RID_PRETTY_FUNCTION_NAME:
3375                 name = "%<__PRETTY_FUNCTION__%>";
3376                 break;
3377               case RID_C99_FUNCTION_NAME:
3378                 name = "%<__func__%>";
3379                 break;
3380               default:
3381                 gcc_unreachable ();
3382               }
3383
3384             if (cp_parser_non_integral_constant_expression (parser, name))
3385               return error_mark_node;
3386
3387             /* Look up the name.  */
3388             return finish_fname (token->u.value);
3389           }
3390
3391         case RID_VA_ARG:
3392           {
3393             tree expression;
3394             tree type;
3395
3396             /* The `__builtin_va_arg' construct is used to handle
3397                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3398             cp_lexer_consume_token (parser->lexer);
3399             /* Look for the opening `('.  */
3400             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3401             /* Now, parse the assignment-expression.  */
3402             expression = cp_parser_assignment_expression (parser,
3403                                                           /*cast_p=*/false, NULL);
3404             /* Look for the `,'.  */
3405             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3406             /* Parse the type-id.  */
3407             type = cp_parser_type_id (parser);
3408             /* Look for the closing `)'.  */
3409             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3410             /* Using `va_arg' in a constant-expression is not
3411                allowed.  */
3412             if (cp_parser_non_integral_constant_expression (parser,
3413                                                             "%<va_arg%>"))
3414               return error_mark_node;
3415             return build_x_va_arg (expression, type);
3416           }
3417
3418         case RID_OFFSETOF:
3419           return cp_parser_builtin_offsetof (parser);
3420
3421         case RID_HAS_NOTHROW_ASSIGN:
3422         case RID_HAS_NOTHROW_CONSTRUCTOR:
3423         case RID_HAS_NOTHROW_COPY:        
3424         case RID_HAS_TRIVIAL_ASSIGN:
3425         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3426         case RID_HAS_TRIVIAL_COPY:        
3427         case RID_HAS_TRIVIAL_DESTRUCTOR:
3428         case RID_HAS_VIRTUAL_DESTRUCTOR:
3429         case RID_IS_ABSTRACT:
3430         case RID_IS_BASE_OF:
3431         case RID_IS_CLASS:
3432         case RID_IS_CONVERTIBLE_TO:
3433         case RID_IS_EMPTY:
3434         case RID_IS_ENUM:
3435         case RID_IS_POD:
3436         case RID_IS_POLYMORPHIC:
3437         case RID_IS_STD_LAYOUT:
3438         case RID_IS_TRIVIAL:
3439         case RID_IS_UNION:
3440           return cp_parser_trait_expr (parser, token->keyword);
3441
3442         /* Objective-C++ expressions.  */
3443         case RID_AT_ENCODE:
3444         case RID_AT_PROTOCOL:
3445         case RID_AT_SELECTOR:
3446           return cp_parser_objc_expression (parser);
3447
3448         default:
3449           cp_parser_error (parser, "expected primary-expression");
3450           return error_mark_node;
3451         }
3452
3453       /* An id-expression can start with either an identifier, a
3454          `::' as the beginning of a qualified-id, or the "operator"
3455          keyword.  */
3456     case CPP_NAME:
3457     case CPP_SCOPE:
3458     case CPP_TEMPLATE_ID:
3459     case CPP_NESTED_NAME_SPECIFIER:
3460       {
3461         tree id_expression;
3462         tree decl;
3463         const char *error_msg;
3464         bool template_p;
3465         bool done;
3466         cp_token *id_expr_token;
3467
3468       id_expression:
3469         /* Parse the id-expression.  */
3470         id_expression
3471           = cp_parser_id_expression (parser,
3472                                      /*template_keyword_p=*/false,
3473                                      /*check_dependency_p=*/true,
3474                                      &template_p,
3475                                      /*declarator_p=*/false,
3476                                      /*optional_p=*/false);
3477         if (id_expression == error_mark_node)
3478           return error_mark_node;
3479         id_expr_token = token;
3480         token = cp_lexer_peek_token (parser->lexer);
3481         done = (token->type != CPP_OPEN_SQUARE
3482                 && token->type != CPP_OPEN_PAREN
3483                 && token->type != CPP_DOT
3484                 && token->type != CPP_DEREF
3485                 && token->type != CPP_PLUS_PLUS
3486                 && token->type != CPP_MINUS_MINUS);
3487         /* If we have a template-id, then no further lookup is
3488            required.  If the template-id was for a template-class, we
3489            will sometimes have a TYPE_DECL at this point.  */
3490         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3491                  || TREE_CODE (id_expression) == TYPE_DECL)
3492           decl = id_expression;
3493         /* Look up the name.  */
3494         else
3495           {
3496             tree ambiguous_decls;
3497
3498             decl = cp_parser_lookup_name (parser, id_expression,
3499                                           none_type,
3500                                           template_p,
3501                                           /*is_namespace=*/false,
3502                                           /*check_dependency=*/true,
3503                                           &ambiguous_decls,
3504                                           id_expr_token->location);
3505             /* If the lookup was ambiguous, an error will already have
3506                been issued.  */
3507             if (ambiguous_decls)
3508               return error_mark_node;
3509
3510             /* In Objective-C++, an instance variable (ivar) may be preferred
3511                to whatever cp_parser_lookup_name() found.  */
3512             decl = objc_lookup_ivar (decl, id_expression);
3513
3514             /* If name lookup gives us a SCOPE_REF, then the
3515                qualifying scope was dependent.  */
3516             if (TREE_CODE (decl) == SCOPE_REF)
3517               {
3518                 /* At this point, we do not know if DECL is a valid
3519                    integral constant expression.  We assume that it is
3520                    in fact such an expression, so that code like:
3521
3522                       template <int N> struct A {
3523                         int a[B<N>::i];
3524                       };
3525                      
3526                    is accepted.  At template-instantiation time, we
3527                    will check that B<N>::i is actually a constant.  */
3528                 return decl;
3529               }
3530             /* Check to see if DECL is a local variable in a context
3531                where that is forbidden.  */
3532             if (parser->local_variables_forbidden_p
3533                 && local_variable_p (decl))
3534               {
3535                 /* It might be that we only found DECL because we are
3536                    trying to be generous with pre-ISO scoping rules.
3537                    For example, consider:
3538
3539                      int i;
3540                      void g() {
3541                        for (int i = 0; i < 10; ++i) {}
3542                        extern void f(int j = i);
3543                      }
3544
3545                    Here, name look up will originally find the out
3546                    of scope `i'.  We need to issue a warning message,
3547                    but then use the global `i'.  */
3548                 decl = check_for_out_of_scope_variable (decl);
3549                 if (local_variable_p (decl))
3550                   {
3551                     error_at (id_expr_token->location,
3552                               "local variable %qD may not appear in this context",
3553                               decl);
3554                     return error_mark_node;
3555                   }
3556               }
3557           }
3558
3559         decl = (finish_id_expression
3560                 (id_expression, decl, parser->scope,
3561                  idk,
3562                  parser->integral_constant_expression_p,
3563                  parser->allow_non_integral_constant_expression_p,
3564                  &parser->non_integral_constant_expression_p,
3565                  template_p, done, address_p,
3566                  template_arg_p,
3567                  &error_msg,
3568                  id_expr_token->location));
3569         if (error_msg)
3570           cp_parser_error (parser, error_msg);
3571         return decl;
3572       }
3573
3574       /* Anything else is an error.  */
3575     default:
3576       cp_parser_error (parser, "expected primary-expression");
3577       return error_mark_node;
3578     }
3579 }
3580
3581 /* Parse an id-expression.
3582
3583    id-expression:
3584      unqualified-id
3585      qualified-id
3586
3587    qualified-id:
3588      :: [opt] nested-name-specifier template [opt] unqualified-id
3589      :: identifier
3590      :: operator-function-id
3591      :: template-id
3592
3593    Return a representation of the unqualified portion of the
3594    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3595    a `::' or nested-name-specifier.
3596
3597    Often, if the id-expression was a qualified-id, the caller will
3598    want to make a SCOPE_REF to represent the qualified-id.  This
3599    function does not do this in order to avoid wastefully creating
3600    SCOPE_REFs when they are not required.
3601
3602    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3603    `template' keyword.
3604
3605    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3606    uninstantiated templates.
3607
3608    If *TEMPLATE_P is non-NULL, it is set to true iff the
3609    `template' keyword is used to explicitly indicate that the entity
3610    named is a template.
3611
3612    If DECLARATOR_P is true, the id-expression is appearing as part of
3613    a declarator, rather than as part of an expression.  */
3614
3615 static tree
3616 cp_parser_id_expression (cp_parser *parser,
3617                          bool template_keyword_p,
3618                          bool check_dependency_p,
3619                          bool *template_p,
3620                          bool declarator_p,
3621                          bool optional_p)
3622 {
3623   bool global_scope_p;
3624   bool nested_name_specifier_p;
3625
3626   /* Assume the `template' keyword was not used.  */
3627   if (template_p)
3628     *template_p = template_keyword_p;
3629
3630   /* Look for the optional `::' operator.  */
3631   global_scope_p
3632     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3633        != NULL_TREE);
3634   /* Look for the optional nested-name-specifier.  */
3635   nested_name_specifier_p
3636     = (cp_parser_nested_name_specifier_opt (parser,
3637                                             /*typename_keyword_p=*/false,
3638                                             check_dependency_p,
3639                                             /*type_p=*/false,
3640                                             declarator_p)
3641        != NULL_TREE);
3642   /* If there is a nested-name-specifier, then we are looking at
3643      the first qualified-id production.  */
3644   if (nested_name_specifier_p)
3645     {
3646       tree saved_scope;
3647       tree saved_object_scope;
3648       tree saved_qualifying_scope;
3649       tree unqualified_id;
3650       bool is_template;
3651
3652       /* See if the next token is the `template' keyword.  */
3653       if (!template_p)
3654         template_p = &is_template;
3655       *template_p = cp_parser_optional_template_keyword (parser);
3656       /* Name lookup we do during the processing of the
3657          unqualified-id might obliterate SCOPE.  */
3658       saved_scope = parser->scope;
3659       saved_object_scope = parser->object_scope;
3660       saved_qualifying_scope = parser->qualifying_scope;
3661       /* Process the final unqualified-id.  */
3662       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3663                                                  check_dependency_p,
3664                                                  declarator_p,
3665                                                  /*optional_p=*/false);
3666       /* Restore the SAVED_SCOPE for our caller.  */
3667       parser->scope = saved_scope;
3668       parser->object_scope = saved_object_scope;
3669       parser->qualifying_scope = saved_qualifying_scope;
3670
3671       return unqualified_id;
3672     }
3673   /* Otherwise, if we are in global scope, then we are looking at one
3674      of the other qualified-id productions.  */
3675   else if (global_scope_p)
3676     {
3677       cp_token *token;
3678       tree id;
3679
3680       /* Peek at the next token.  */
3681       token = cp_lexer_peek_token (parser->lexer);
3682
3683       /* If it's an identifier, and the next token is not a "<", then
3684          we can avoid the template-id case.  This is an optimization
3685          for this common case.  */
3686       if (token->type == CPP_NAME
3687           && !cp_parser_nth_token_starts_template_argument_list_p
3688                (parser, 2))
3689         return cp_parser_identifier (parser);
3690
3691       cp_parser_parse_tentatively (parser);
3692       /* Try a template-id.  */
3693       id = cp_parser_template_id (parser,
3694                                   /*template_keyword_p=*/false,
3695                                   /*check_dependency_p=*/true,
3696                                   declarator_p);
3697       /* If that worked, we're done.  */
3698       if (cp_parser_parse_definitely (parser))
3699         return id;
3700
3701       /* Peek at the next token.  (Changes in the token buffer may
3702          have invalidated the pointer obtained above.)  */
3703       token = cp_lexer_peek_token (parser->lexer);
3704
3705       switch (token->type)
3706         {
3707         case CPP_NAME:
3708           return cp_parser_identifier (parser);
3709
3710         case CPP_KEYWORD:
3711           if (token->keyword == RID_OPERATOR)
3712             return cp_parser_operator_function_id (parser);
3713           /* Fall through.  */
3714
3715         default:
3716           cp_parser_error (parser, "expected id-expression");
3717           return error_mark_node;
3718         }
3719     }
3720   else
3721     return cp_parser_unqualified_id (parser, template_keyword_p,
3722                                      /*check_dependency_p=*/true,
3723                                      declarator_p,
3724                                      optional_p);
3725 }
3726
3727 /* Parse an unqualified-id.
3728
3729    unqualified-id:
3730      identifier
3731      operator-function-id
3732      conversion-function-id
3733      ~ class-name
3734      template-id
3735
3736    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3737    keyword, in a construct like `A::template ...'.
3738
3739    Returns a representation of unqualified-id.  For the `identifier'
3740    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3741    production a BIT_NOT_EXPR is returned; the operand of the
3742    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3743    other productions, see the documentation accompanying the
3744    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3745    names are looked up in uninstantiated templates.  If DECLARATOR_P
3746    is true, the unqualified-id is appearing as part of a declarator,
3747    rather than as part of an expression.  */
3748
3749 static tree
3750 cp_parser_unqualified_id (cp_parser* parser,
3751                           bool template_keyword_p,
3752                           bool check_dependency_p,
3753                           bool declarator_p,
3754                           bool optional_p)
3755 {
3756   cp_token *token;
3757
3758   /* Peek at the next token.  */
3759   token = cp_lexer_peek_token (parser->lexer);
3760
3761   switch (token->type)
3762     {
3763     case CPP_NAME:
3764       {
3765         tree id;
3766
3767         /* We don't know yet whether or not this will be a
3768            template-id.  */
3769         cp_parser_parse_tentatively (parser);
3770         /* Try a template-id.  */
3771         id = cp_parser_template_id (parser, template_keyword_p,
3772                                     check_dependency_p,
3773                                     declarator_p);
3774         /* If it worked, we're done.  */
3775         if (cp_parser_parse_definitely (parser))
3776           return id;
3777         /* Otherwise, it's an ordinary identifier.  */
3778         return cp_parser_identifier (parser);
3779       }
3780
3781     case CPP_TEMPLATE_ID:
3782       return cp_parser_template_id (parser, template_keyword_p,
3783                                     check_dependency_p,
3784                                     declarator_p);
3785
3786     case CPP_COMPL:
3787       {
3788         tree type_decl;
3789         tree qualifying_scope;
3790         tree object_scope;
3791         tree scope;
3792         bool done;
3793
3794         /* Consume the `~' token.  */
3795         cp_lexer_consume_token (parser->lexer);
3796         /* Parse the class-name.  The standard, as written, seems to
3797            say that:
3798
3799              template <typename T> struct S { ~S (); };
3800              template <typename T> S<T>::~S() {}
3801
3802            is invalid, since `~' must be followed by a class-name, but
3803            `S<T>' is dependent, and so not known to be a class.
3804            That's not right; we need to look in uninstantiated
3805            templates.  A further complication arises from:
3806
3807              template <typename T> void f(T t) {
3808                t.T::~T();
3809              }
3810
3811            Here, it is not possible to look up `T' in the scope of `T'
3812            itself.  We must look in both the current scope, and the
3813            scope of the containing complete expression.
3814
3815            Yet another issue is:
3816
3817              struct S {
3818                int S;
3819                ~S();
3820              };
3821
3822              S::~S() {}
3823
3824            The standard does not seem to say that the `S' in `~S'
3825            should refer to the type `S' and not the data member
3826            `S::S'.  */
3827
3828         /* DR 244 says that we look up the name after the "~" in the
3829            same scope as we looked up the qualifying name.  That idea
3830            isn't fully worked out; it's more complicated than that.  */
3831         scope = parser->scope;
3832         object_scope = parser->object_scope;
3833         qualifying_scope = parser->qualifying_scope;
3834
3835         /* Check for invalid scopes.  */
3836         if (scope == error_mark_node)
3837           {
3838             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3839               cp_lexer_consume_token (parser->lexer);
3840             return error_mark_node;
3841           }
3842         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3843           {
3844             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3845               error_at (token->location,
3846                         "scope %qT before %<~%> is not a class-name",
3847                         scope);
3848             cp_parser_simulate_error (parser);
3849             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3850               cp_lexer_consume_token (parser->lexer);
3851             return error_mark_node;
3852           }
3853         gcc_assert (!scope || TYPE_P (scope));
3854
3855         /* If the name is of the form "X::~X" it's OK.  */
3856         token = cp_lexer_peek_token (parser->lexer);
3857         if (scope
3858             && token->type == CPP_NAME
3859             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3860                 == CPP_OPEN_PAREN)
3861             && constructor_name_p (token->u.value, scope))
3862           {
3863             cp_lexer_consume_token (parser->lexer);
3864             return build_nt (BIT_NOT_EXPR, scope);
3865           }
3866
3867         /* If there was an explicit qualification (S::~T), first look
3868            in the scope given by the qualification (i.e., S).  */
3869         done = false;
3870         type_decl = NULL_TREE;
3871         if (scope)
3872           {
3873             cp_parser_parse_tentatively (parser);
3874             type_decl = cp_parser_class_name (parser,
3875                                               /*typename_keyword_p=*/false,
3876                                               /*template_keyword_p=*/false,
3877                                               none_type,
3878                                               /*check_dependency=*/false,
3879                                               /*class_head_p=*/false,
3880                                               declarator_p);
3881             if (cp_parser_parse_definitely (parser))
3882               done = true;
3883           }
3884         /* In "N::S::~S", look in "N" as well.  */
3885         if (!done && scope && qualifying_scope)
3886           {
3887             cp_parser_parse_tentatively (parser);
3888             parser->scope = qualifying_scope;
3889             parser->object_scope = NULL_TREE;
3890             parser->qualifying_scope = NULL_TREE;
3891             type_decl
3892               = cp_parser_class_name (parser,
3893                                       /*typename_keyword_p=*/false,
3894                                       /*template_keyword_p=*/false,
3895                                       none_type,
3896                                       /*check_dependency=*/false,
3897                                       /*class_head_p=*/false,
3898                                       declarator_p);
3899             if (cp_parser_parse_definitely (parser))
3900               done = true;
3901           }
3902         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3903         else if (!done && object_scope)
3904           {
3905             cp_parser_parse_tentatively (parser);
3906             parser->scope = object_scope;
3907             parser->object_scope = NULL_TREE;
3908             parser->qualifying_scope = NULL_TREE;
3909             type_decl
3910               = cp_parser_class_name (parser,
3911                                       /*typename_keyword_p=*/false,
3912                                       /*template_keyword_p=*/false,
3913                                       none_type,
3914                                       /*check_dependency=*/false,
3915                                       /*class_head_p=*/false,
3916                                       declarator_p);
3917             if (cp_parser_parse_definitely (parser))
3918               done = true;
3919           }
3920         /* Look in the surrounding context.  */
3921         if (!done)
3922           {
3923             parser->scope = NULL_TREE;
3924             parser->object_scope = NULL_TREE;
3925             parser->qualifying_scope = NULL_TREE;
3926             if (processing_template_decl)
3927               cp_parser_parse_tentatively (parser);
3928             type_decl
3929               = cp_parser_class_name (parser,
3930                                       /*typename_keyword_p=*/false,
3931                                       /*template_keyword_p=*/false,
3932                                       none_type,
3933                                       /*check_dependency=*/false,
3934                                       /*class_head_p=*/false,
3935                                       declarator_p);
3936             if (processing_template_decl
3937                 && ! cp_parser_parse_definitely (parser))
3938               {
3939                 /* We couldn't find a type with this name, so just accept
3940                    it and check for a match at instantiation time.  */
3941                 type_decl = cp_parser_identifier (parser);
3942                 if (type_decl != error_mark_node)
3943                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3944                 return type_decl;
3945               }
3946           }
3947         /* If an error occurred, assume that the name of the
3948            destructor is the same as the name of the qualifying
3949            class.  That allows us to keep parsing after running
3950            into ill-formed destructor names.  */
3951         if (type_decl == error_mark_node && scope)
3952           return build_nt (BIT_NOT_EXPR, scope);
3953         else if (type_decl == error_mark_node)
3954           return error_mark_node;
3955
3956         /* Check that destructor name and scope match.  */
3957         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3958           {
3959             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3960               error_at (token->location,
3961                         "declaration of %<~%T%> as member of %qT",
3962                         type_decl, scope);
3963             cp_parser_simulate_error (parser);
3964             return error_mark_node;
3965           }
3966
3967         /* [class.dtor]
3968
3969            A typedef-name that names a class shall not be used as the
3970            identifier in the declarator for a destructor declaration.  */
3971         if (declarator_p
3972             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3973             && !DECL_SELF_REFERENCE_P (type_decl)
3974             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3975           error_at (token->location,
3976                     "typedef-name %qD used as destructor declarator",
3977                     type_decl);
3978
3979         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3980       }
3981
3982     case CPP_KEYWORD:
3983       if (token->keyword == RID_OPERATOR)
3984         {
3985           tree id;
3986
3987           /* This could be a template-id, so we try that first.  */
3988           cp_parser_parse_tentatively (parser);
3989           /* Try a template-id.  */
3990           id = cp_parser_template_id (parser, template_keyword_p,
3991                                       /*check_dependency_p=*/true,
3992                                       declarator_p);
3993           /* If that worked, we're done.  */
3994           if (cp_parser_parse_definitely (parser))
3995             return id;
3996           /* We still don't know whether we're looking at an
3997              operator-function-id or a conversion-function-id.  */
3998           cp_parser_parse_tentatively (parser);
3999           /* Try an operator-function-id.  */
4000           id = cp_parser_operator_function_id (parser);
4001           /* If that didn't work, try a conversion-function-id.  */
4002           if (!cp_parser_parse_definitely (parser))
4003             id = cp_parser_conversion_function_id (parser);
4004
4005           return id;
4006         }
4007       /* Fall through.  */
4008
4009     default:
4010       if (optional_p)
4011         return NULL_TREE;
4012       cp_parser_error (parser, "expected unqualified-id");
4013       return error_mark_node;
4014     }
4015 }
4016
4017 /* Parse an (optional) nested-name-specifier.
4018
4019    nested-name-specifier: [C++98]
4020      class-or-namespace-name :: nested-name-specifier [opt]
4021      class-or-namespace-name :: template nested-name-specifier [opt]
4022
4023    nested-name-specifier: [C++0x]
4024      type-name ::
4025      namespace-name ::
4026      nested-name-specifier identifier ::
4027      nested-name-specifier template [opt] simple-template-id ::
4028
4029    PARSER->SCOPE should be set appropriately before this function is
4030    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4031    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4032    in name lookups.
4033
4034    Sets PARSER->SCOPE to the class (TYPE) or namespace
4035    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4036    it unchanged if there is no nested-name-specifier.  Returns the new
4037    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4038
4039    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4040    part of a declaration and/or decl-specifier.  */
4041
4042 static tree
4043 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4044                                      bool typename_keyword_p,
4045                                      bool check_dependency_p,
4046                                      bool type_p,
4047                                      bool is_declaration)
4048 {
4049   bool success = false;
4050   cp_token_position start = 0;
4051   cp_token *token;
4052
4053   /* Remember where the nested-name-specifier starts.  */
4054   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4055     {
4056       start = cp_lexer_token_position (parser->lexer, false);
4057       push_deferring_access_checks (dk_deferred);
4058     }
4059
4060   while (true)
4061     {
4062       tree new_scope;
4063       tree old_scope;
4064       tree saved_qualifying_scope;
4065       bool template_keyword_p;
4066
4067       /* Spot cases that cannot be the beginning of a
4068          nested-name-specifier.  */
4069       token = cp_lexer_peek_token (parser->lexer);
4070
4071       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4072          the already parsed nested-name-specifier.  */
4073       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4074         {
4075           /* Grab the nested-name-specifier and continue the loop.  */
4076           cp_parser_pre_parsed_nested_name_specifier (parser);
4077           /* If we originally encountered this nested-name-specifier
4078              with IS_DECLARATION set to false, we will not have
4079              resolved TYPENAME_TYPEs, so we must do so here.  */
4080           if (is_declaration
4081               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4082             {
4083               new_scope = resolve_typename_type (parser->scope,
4084                                                  /*only_current_p=*/false);
4085               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4086                 parser->scope = new_scope;
4087             }
4088           success = true;
4089           continue;
4090         }
4091
4092       /* Spot cases that cannot be the beginning of a
4093          nested-name-specifier.  On the second and subsequent times
4094          through the loop, we look for the `template' keyword.  */
4095       if (success && token->keyword == RID_TEMPLATE)
4096         ;
4097       /* A template-id can start a nested-name-specifier.  */
4098       else if (token->type == CPP_TEMPLATE_ID)
4099         ;
4100       else
4101         {
4102           /* If the next token is not an identifier, then it is
4103              definitely not a type-name or namespace-name.  */
4104           if (token->type != CPP_NAME)
4105             break;
4106           /* If the following token is neither a `<' (to begin a
4107              template-id), nor a `::', then we are not looking at a
4108              nested-name-specifier.  */
4109           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4110           if (token->type != CPP_SCOPE
4111               && !cp_parser_nth_token_starts_template_argument_list_p
4112                   (parser, 2))
4113             break;
4114         }
4115
4116       /* The nested-name-specifier is optional, so we parse
4117          tentatively.  */
4118       cp_parser_parse_tentatively (parser);
4119
4120       /* Look for the optional `template' keyword, if this isn't the
4121          first time through the loop.  */
4122       if (success)
4123         template_keyword_p = cp_parser_optional_template_keyword (parser);
4124       else
4125         template_keyword_p = false;
4126
4127       /* Save the old scope since the name lookup we are about to do
4128          might destroy it.  */
4129       old_scope = parser->scope;
4130       saved_qualifying_scope = parser->qualifying_scope;
4131       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4132          look up names in "X<T>::I" in order to determine that "Y" is
4133          a template.  So, if we have a typename at this point, we make
4134          an effort to look through it.  */
4135       if (is_declaration
4136           && !typename_keyword_p
4137           && parser->scope
4138           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4139         parser->scope = resolve_typename_type (parser->scope,
4140                                                /*only_current_p=*/false);
4141       /* Parse the qualifying entity.  */
4142       new_scope
4143         = cp_parser_qualifying_entity (parser,
4144                                        typename_keyword_p,
4145                                        template_keyword_p,
4146                                        check_dependency_p,
4147                                        type_p,
4148                                        is_declaration);
4149       /* Look for the `::' token.  */
4150       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4151
4152       /* If we found what we wanted, we keep going; otherwise, we're
4153          done.  */
4154       if (!cp_parser_parse_definitely (parser))
4155         {
4156           bool error_p = false;
4157
4158           /* Restore the OLD_SCOPE since it was valid before the
4159              failed attempt at finding the last
4160              class-or-namespace-name.  */
4161           parser->scope = old_scope;
4162           parser->qualifying_scope = saved_qualifying_scope;
4163           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4164             break;
4165           /* If the next token is an identifier, and the one after
4166              that is a `::', then any valid interpretation would have
4167              found a class-or-namespace-name.  */
4168           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4169                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4170                      == CPP_SCOPE)
4171                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4172                      != CPP_COMPL))
4173             {
4174               token = cp_lexer_consume_token (parser->lexer);
4175               if (!error_p)
4176                 {
4177                   if (!token->ambiguous_p)
4178                     {
4179                       tree decl;
4180                       tree ambiguous_decls;
4181
4182                       decl = cp_parser_lookup_name (parser, token->u.value,
4183                                                     none_type,
4184                                                     /*is_template=*/false,
4185                                                     /*is_namespace=*/false,
4186                                                     /*check_dependency=*/true,
4187                                                     &ambiguous_decls,
4188                                                     token->location);
4189                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4190                         error_at (token->location,
4191                                   "%qD used without template parameters",
4192                                   decl);
4193                       else if (ambiguous_decls)
4194                         {
4195                           error_at (token->location,
4196                                     "reference to %qD is ambiguous",
4197                                     token->u.value);
4198                           print_candidates (ambiguous_decls);
4199                           decl = error_mark_node;
4200                         }
4201                       else
4202                         {
4203                           const char* msg = "is not a class or namespace";
4204                           if (cxx_dialect != cxx98)
4205                             msg = "is not a class, namespace, or enumeration";
4206                           cp_parser_name_lookup_error
4207                             (parser, token->u.value, decl, msg,
4208                              token->location);
4209                         }
4210                     }
4211                   parser->scope = error_mark_node;
4212                   error_p = true;
4213                   /* Treat this as a successful nested-name-specifier
4214                      due to:
4215
4216                      [basic.lookup.qual]
4217
4218                      If the name found is not a class-name (clause
4219                      _class_) or namespace-name (_namespace.def_), the
4220                      program is ill-formed.  */
4221                   success = true;
4222                 }
4223               cp_lexer_consume_token (parser->lexer);
4224             }
4225           break;
4226         }
4227       /* We've found one valid nested-name-specifier.  */
4228       success = true;
4229       /* Name lookup always gives us a DECL.  */
4230       if (TREE_CODE (new_scope) == TYPE_DECL)
4231         new_scope = TREE_TYPE (new_scope);
4232       /* Uses of "template" must be followed by actual templates.  */
4233       if (template_keyword_p
4234           && !(CLASS_TYPE_P (new_scope)
4235                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4236                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4237                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4238           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4239                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4240                    == TEMPLATE_ID_EXPR)))
4241         permerror (input_location, TYPE_P (new_scope)
4242                    ? "%qT is not a template"
4243                    : "%qD is not a template",
4244                    new_scope);
4245       /* If it is a class scope, try to complete it; we are about to
4246          be looking up names inside the class.  */
4247       if (TYPE_P (new_scope)
4248           /* Since checking types for dependency can be expensive,
4249              avoid doing it if the type is already complete.  */
4250           && !COMPLETE_TYPE_P (new_scope)
4251           /* Do not try to complete dependent types.  */
4252           && !dependent_type_p (new_scope))
4253         {
4254           new_scope = complete_type (new_scope);
4255           /* If it is a typedef to current class, use the current
4256              class instead, as the typedef won't have any names inside
4257              it yet.  */
4258           if (!COMPLETE_TYPE_P (new_scope)
4259               && currently_open_class (new_scope))
4260             new_scope = TYPE_MAIN_VARIANT (new_scope);
4261         }
4262       /* Make sure we look in the right scope the next time through
4263          the loop.  */
4264       parser->scope = new_scope;
4265     }
4266
4267   /* If parsing tentatively, replace the sequence of tokens that makes
4268      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4269      token.  That way, should we re-parse the token stream, we will
4270      not have to repeat the effort required to do the parse, nor will
4271      we issue duplicate error messages.  */
4272   if (success && start)
4273     {
4274       cp_token *token;
4275
4276       token = cp_lexer_token_at (parser->lexer, start);
4277       /* Reset the contents of the START token.  */
4278       token->type = CPP_NESTED_NAME_SPECIFIER;
4279       /* Retrieve any deferred checks.  Do not pop this access checks yet
4280          so the memory will not be reclaimed during token replacing below.  */
4281       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4282       token->u.tree_check_value->value = parser->scope;
4283       token->u.tree_check_value->checks = get_deferred_access_checks ();
4284       token->u.tree_check_value->qualifying_scope =
4285         parser->qualifying_scope;
4286       token->keyword = RID_MAX;
4287
4288       /* Purge all subsequent tokens.  */
4289       cp_lexer_purge_tokens_after (parser->lexer, start);
4290     }
4291
4292   if (start)
4293     pop_to_parent_deferring_access_checks ();
4294
4295   return success ? parser->scope : NULL_TREE;
4296 }
4297
4298 /* Parse a nested-name-specifier.  See
4299    cp_parser_nested_name_specifier_opt for details.  This function
4300    behaves identically, except that it will an issue an error if no
4301    nested-name-specifier is present.  */
4302
4303 static tree
4304 cp_parser_nested_name_specifier (cp_parser *parser,
4305                                  bool typename_keyword_p,
4306                                  bool check_dependency_p,
4307                                  bool type_p,
4308                                  bool is_declaration)
4309 {
4310   tree scope;
4311
4312   /* Look for the nested-name-specifier.  */
4313   scope = cp_parser_nested_name_specifier_opt (parser,
4314                                                typename_keyword_p,
4315                                                check_dependency_p,
4316                                                type_p,
4317                                                is_declaration);
4318   /* If it was not present, issue an error message.  */
4319   if (!scope)
4320     {
4321       cp_parser_error (parser, "expected nested-name-specifier");
4322       parser->scope = NULL_TREE;
4323     }
4324
4325   return scope;
4326 }
4327
4328 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4329    this is either a class-name or a namespace-name (which corresponds
4330    to the class-or-namespace-name production in the grammar). For
4331    C++0x, it can also be a type-name that refers to an enumeration
4332    type.
4333
4334    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4335    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4336    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4337    TYPE_P is TRUE iff the next name should be taken as a class-name,
4338    even the same name is declared to be another entity in the same
4339    scope.
4340
4341    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4342    specified by the class-or-namespace-name.  If neither is found the
4343    ERROR_MARK_NODE is returned.  */
4344
4345 static tree
4346 cp_parser_qualifying_entity (cp_parser *parser,
4347                              bool typename_keyword_p,
4348                              bool template_keyword_p,
4349                              bool check_dependency_p,
4350                              bool type_p,
4351                              bool is_declaration)
4352 {
4353   tree saved_scope;
4354   tree saved_qualifying_scope;
4355   tree saved_object_scope;
4356   tree scope;
4357   bool only_class_p;
4358   bool successful_parse_p;
4359
4360   /* Before we try to parse the class-name, we must save away the
4361      current PARSER->SCOPE since cp_parser_class_name will destroy
4362      it.  */
4363   saved_scope = parser->scope;
4364   saved_qualifying_scope = parser->qualifying_scope;
4365   saved_object_scope = parser->object_scope;
4366   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4367      there is no need to look for a namespace-name.  */
4368   only_class_p = template_keyword_p 
4369     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4370   if (!only_class_p)
4371     cp_parser_parse_tentatively (parser);
4372   scope = cp_parser_class_name (parser,
4373                                 typename_keyword_p,
4374                                 template_keyword_p,
4375                                 type_p ? class_type : none_type,
4376                                 check_dependency_p,
4377                                 /*class_head_p=*/false,
4378                                 is_declaration);
4379   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4380   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4381   if (!only_class_p 
4382       && cxx_dialect != cxx98
4383       && !successful_parse_p)
4384     {
4385       /* Restore the saved scope.  */
4386       parser->scope = saved_scope;
4387       parser->qualifying_scope = saved_qualifying_scope;
4388       parser->object_scope = saved_object_scope;
4389
4390       /* Parse tentatively.  */
4391       cp_parser_parse_tentatively (parser);
4392      
4393       /* Parse a typedef-name or enum-name.  */
4394       scope = cp_parser_nonclass_name (parser);
4395       successful_parse_p = cp_parser_parse_definitely (parser);
4396     }
4397   /* If that didn't work, try for a namespace-name.  */
4398   if (!only_class_p && !successful_parse_p)
4399     {
4400       /* Restore the saved scope.  */
4401       parser->scope = saved_scope;
4402       parser->qualifying_scope = saved_qualifying_scope;
4403       parser->object_scope = saved_object_scope;
4404       /* If we are not looking at an identifier followed by the scope
4405          resolution operator, then this is not part of a
4406          nested-name-specifier.  (Note that this function is only used
4407          to parse the components of a nested-name-specifier.)  */
4408       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4409           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4410         return error_mark_node;
4411       scope = cp_parser_namespace_name (parser);
4412     }
4413
4414   return scope;
4415 }
4416
4417 /* Parse a postfix-expression.
4418
4419    postfix-expression:
4420      primary-expression
4421      postfix-expression [ expression ]
4422      postfix-expression ( expression-list [opt] )
4423      simple-type-specifier ( expression-list [opt] )
4424      typename :: [opt] nested-name-specifier identifier
4425        ( expression-list [opt] )
4426      typename :: [opt] nested-name-specifier template [opt] template-id
4427        ( expression-list [opt] )
4428      postfix-expression . template [opt] id-expression
4429      postfix-expression -> template [opt] id-expression
4430      postfix-expression . pseudo-destructor-name
4431      postfix-expression -> pseudo-destructor-name
4432      postfix-expression ++
4433      postfix-expression --
4434      dynamic_cast < type-id > ( expression )
4435      static_cast < type-id > ( expression )
4436      reinterpret_cast < type-id > ( expression )
4437      const_cast < type-id > ( expression )
4438      typeid ( expression )
4439      typeid ( type-id )
4440
4441    GNU Extension:
4442
4443    postfix-expression:
4444      ( type-id ) { initializer-list , [opt] }
4445
4446    This extension is a GNU version of the C99 compound-literal
4447    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4448    but they are essentially the same concept.)
4449
4450    If ADDRESS_P is true, the postfix expression is the operand of the
4451    `&' operator.  CAST_P is true if this expression is the target of a
4452    cast.
4453
4454    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4455    class member access expressions [expr.ref].
4456
4457    Returns a representation of the expression.  */
4458
4459 static tree
4460 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4461                               bool member_access_only_p,
4462                               cp_id_kind * pidk_return)
4463 {
4464   cp_token *token;
4465   enum rid keyword;
4466   cp_id_kind idk = CP_ID_KIND_NONE;
4467   tree postfix_expression = NULL_TREE;
4468   bool is_member_access = false;
4469
4470   /* Peek at the next token.  */
4471   token = cp_lexer_peek_token (parser->lexer);
4472   /* Some of the productions are determined by keywords.  */
4473   keyword = token->keyword;
4474   switch (keyword)
4475     {
4476     case RID_DYNCAST:
4477     case RID_STATCAST:
4478     case RID_REINTCAST:
4479     case RID_CONSTCAST:
4480       {
4481         tree type;
4482         tree expression;
4483         const char *saved_message;
4484
4485         /* All of these can be handled in the same way from the point
4486            of view of parsing.  Begin by consuming the token
4487            identifying the cast.  */
4488         cp_lexer_consume_token (parser->lexer);
4489
4490         /* New types cannot be defined in the cast.  */
4491         saved_message = parser->type_definition_forbidden_message;
4492         parser->type_definition_forbidden_message
4493           = "types may not be defined in casts";
4494
4495         /* Look for the opening `<'.  */
4496         cp_parser_require (parser, CPP_LESS, "%<<%>");
4497         /* Parse the type to which we are casting.  */
4498         type = cp_parser_type_id (parser);
4499         /* Look for the closing `>'.  */
4500         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4501         /* Restore the old message.  */
4502         parser->type_definition_forbidden_message = saved_message;
4503
4504         /* And the expression which is being cast.  */
4505         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4506         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4507         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4508
4509         /* Only type conversions to integral or enumeration types
4510            can be used in constant-expressions.  */
4511         if (!cast_valid_in_integral_constant_expression_p (type)
4512             && (cp_parser_non_integral_constant_expression
4513                 (parser,
4514                  "a cast to a type other than an integral or "
4515                  "enumeration type")))
4516           return error_mark_node;
4517
4518         switch (keyword)
4519           {
4520           case RID_DYNCAST:
4521             postfix_expression
4522               = build_dynamic_cast (type, expression, tf_warning_or_error);
4523             break;
4524           case RID_STATCAST:
4525             postfix_expression
4526               = build_static_cast (type, expression, tf_warning_or_error);
4527             break;
4528           case RID_REINTCAST:
4529             postfix_expression
4530               = build_reinterpret_cast (type, expression, 
4531                                         tf_warning_or_error);
4532             break;
4533           case RID_CONSTCAST:
4534             postfix_expression
4535               = build_const_cast (type, expression, tf_warning_or_error);
4536             break;
4537           default:
4538             gcc_unreachable ();
4539           }
4540       }
4541       break;
4542
4543     case RID_TYPEID:
4544       {
4545         tree type;
4546         const char *saved_message;
4547         bool saved_in_type_id_in_expr_p;
4548
4549         /* Consume the `typeid' token.  */
4550         cp_lexer_consume_token (parser->lexer);
4551         /* Look for the `(' token.  */
4552         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4553         /* Types cannot be defined in a `typeid' expression.  */
4554         saved_message = parser->type_definition_forbidden_message;
4555         parser->type_definition_forbidden_message
4556           = "types may not be defined in a %<typeid%> expression";
4557         /* We can't be sure yet whether we're looking at a type-id or an
4558            expression.  */
4559         cp_parser_parse_tentatively (parser);
4560         /* Try a type-id first.  */
4561         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4562         parser->in_type_id_in_expr_p = true;
4563         type = cp_parser_type_id (parser);
4564         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4565         /* Look for the `)' token.  Otherwise, we can't be sure that
4566            we're not looking at an expression: consider `typeid (int
4567            (3))', for example.  */
4568         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4569         /* If all went well, simply lookup the type-id.  */
4570         if (cp_parser_parse_definitely (parser))
4571           postfix_expression = get_typeid (type);
4572         /* Otherwise, fall back to the expression variant.  */
4573         else
4574           {
4575             tree expression;
4576
4577             /* Look for an expression.  */
4578             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4579             /* Compute its typeid.  */
4580             postfix_expression = build_typeid (expression);
4581             /* Look for the `)' token.  */
4582             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4583           }
4584         /* Restore the saved message.  */
4585         parser->type_definition_forbidden_message = saved_message;
4586         /* `typeid' may not appear in an integral constant expression.  */
4587         if (cp_parser_non_integral_constant_expression(parser,
4588                                                        "%<typeid%> operator"))
4589           return error_mark_node;
4590       }
4591       break;
4592
4593     case RID_TYPENAME:
4594       {
4595         tree type;
4596         /* The syntax permitted here is the same permitted for an
4597            elaborated-type-specifier.  */
4598         type = cp_parser_elaborated_type_specifier (parser,
4599                                                     /*is_friend=*/false,
4600                                                     /*is_declaration=*/false);
4601         postfix_expression = cp_parser_functional_cast (parser, type);
4602       }
4603       break;
4604
4605     default:
4606       {
4607         tree type;
4608
4609         /* If the next thing is a simple-type-specifier, we may be
4610            looking at a functional cast.  We could also be looking at
4611            an id-expression.  So, we try the functional cast, and if
4612            that doesn't work we fall back to the primary-expression.  */
4613         cp_parser_parse_tentatively (parser);
4614         /* Look for the simple-type-specifier.  */
4615         type = cp_parser_simple_type_specifier (parser,
4616                                                 /*decl_specs=*/NULL,
4617                                                 CP_PARSER_FLAGS_NONE);
4618         /* Parse the cast itself.  */
4619         if (!cp_parser_error_occurred (parser))
4620           postfix_expression
4621             = cp_parser_functional_cast (parser, type);
4622         /* If that worked, we're done.  */
4623         if (cp_parser_parse_definitely (parser))
4624           break;
4625
4626         /* If the functional-cast didn't work out, try a
4627            compound-literal.  */
4628         if (cp_parser_allow_gnu_extensions_p (parser)
4629             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4630           {
4631             VEC(constructor_elt,gc) *initializer_list = NULL;
4632             bool saved_in_type_id_in_expr_p;
4633
4634             cp_parser_parse_tentatively (parser);
4635             /* Consume the `('.  */
4636             cp_lexer_consume_token (parser->lexer);
4637             /* Parse the type.  */
4638             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4639             parser->in_type_id_in_expr_p = true;
4640             type = cp_parser_type_id (parser);
4641             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4642             /* Look for the `)'.  */
4643             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4644             /* Look for the `{'.  */
4645             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4646             /* If things aren't going well, there's no need to
4647                keep going.  */
4648             if (!cp_parser_error_occurred (parser))
4649               {
4650                 bool non_constant_p;
4651                 /* Parse the initializer-list.  */
4652                 initializer_list
4653                   = cp_parser_initializer_list (parser, &non_constant_p);
4654                 /* Allow a trailing `,'.  */
4655                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4656                   cp_lexer_consume_token (parser->lexer);
4657                 /* Look for the final `}'.  */
4658                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4659               }
4660             /* If that worked, we're definitely looking at a
4661                compound-literal expression.  */
4662             if (cp_parser_parse_definitely (parser))
4663               {
4664                 /* Warn the user that a compound literal is not
4665                    allowed in standard C++.  */
4666                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4667                 /* For simplicity, we disallow compound literals in
4668                    constant-expressions.  We could
4669                    allow compound literals of integer type, whose
4670                    initializer was a constant, in constant
4671                    expressions.  Permitting that usage, as a further
4672                    extension, would not change the meaning of any
4673                    currently accepted programs.  (Of course, as
4674                    compound literals are not part of ISO C++, the
4675                    standard has nothing to say.)  */
4676                 if (cp_parser_non_integral_constant_expression 
4677                     (parser, "non-constant compound literals"))
4678                   {
4679                     postfix_expression = error_mark_node;
4680                     break;
4681                   }
4682                 /* Form the representation of the compound-literal.  */
4683                 postfix_expression
4684                   = (finish_compound_literal
4685                      (type, build_constructor (init_list_type_node,
4686                                                initializer_list)));
4687                 break;
4688               }
4689           }
4690
4691         /* It must be a primary-expression.  */
4692         postfix_expression
4693           = cp_parser_primary_expression (parser, address_p, cast_p,
4694                                           /*template_arg_p=*/false,
4695                                           &idk);
4696       }
4697       break;
4698     }
4699
4700   /* Keep looping until the postfix-expression is complete.  */
4701   while (true)
4702     {
4703       if (idk == CP_ID_KIND_UNQUALIFIED
4704           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4705           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4706         /* It is not a Koenig lookup function call.  */
4707         postfix_expression
4708           = unqualified_name_lookup_error (postfix_expression);
4709
4710       /* Peek at the next token.  */
4711       token = cp_lexer_peek_token (parser->lexer);
4712
4713       switch (token->type)
4714         {
4715         case CPP_OPEN_SQUARE:
4716           postfix_expression
4717             = cp_parser_postfix_open_square_expression (parser,
4718                                                         postfix_expression,
4719                                                         false);
4720           idk = CP_ID_KIND_NONE;
4721           is_member_access = false;
4722           break;
4723
4724         case CPP_OPEN_PAREN:
4725           /* postfix-expression ( expression-list [opt] ) */
4726           {
4727             bool koenig_p;
4728             bool is_builtin_constant_p;
4729             bool saved_integral_constant_expression_p = false;
4730             bool saved_non_integral_constant_expression_p = false;
4731             VEC(tree,gc) *args;
4732
4733             is_member_access = false;
4734
4735             is_builtin_constant_p
4736               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4737             if (is_builtin_constant_p)
4738               {
4739                 /* The whole point of __builtin_constant_p is to allow
4740                    non-constant expressions to appear as arguments.  */
4741                 saved_integral_constant_expression_p
4742                   = parser->integral_constant_expression_p;
4743                 saved_non_integral_constant_expression_p
4744                   = parser->non_integral_constant_expression_p;
4745                 parser->integral_constant_expression_p = false;
4746               }
4747             args = (cp_parser_parenthesized_expression_list
4748                     (parser, /*is_attribute_list=*/false,
4749                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4750                      /*non_constant_p=*/NULL));
4751             if (is_builtin_constant_p)
4752               {
4753                 parser->integral_constant_expression_p
4754                   = saved_integral_constant_expression_p;
4755                 parser->non_integral_constant_expression_p
4756                   = saved_non_integral_constant_expression_p;
4757               }
4758
4759             if (args == NULL)
4760               {
4761                 postfix_expression = error_mark_node;
4762                 break;
4763               }
4764
4765             /* Function calls are not permitted in
4766                constant-expressions.  */
4767             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4768                 && cp_parser_non_integral_constant_expression (parser,
4769                                                                "a function call"))
4770               {
4771                 postfix_expression = error_mark_node;
4772                 release_tree_vector (args);
4773                 break;
4774               }
4775
4776             koenig_p = false;
4777             if (idk == CP_ID_KIND_UNQUALIFIED
4778                 || idk == CP_ID_KIND_TEMPLATE_ID)
4779               {
4780                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4781                   {
4782                     if (!VEC_empty (tree, args))
4783                       {
4784                         koenig_p = true;
4785                         if (!any_type_dependent_arguments_p (args))
4786                           postfix_expression
4787                             = perform_koenig_lookup (postfix_expression, args);
4788                       }
4789                     else
4790                       postfix_expression
4791                         = unqualified_fn_lookup_error (postfix_expression);
4792                   }
4793                 /* We do not perform argument-dependent lookup if
4794                    normal lookup finds a non-function, in accordance
4795                    with the expected resolution of DR 218.  */
4796                 else if (!VEC_empty (tree, args)
4797                          && is_overloaded_fn (postfix_expression))
4798                   {
4799                     tree fn = get_first_fn (postfix_expression);
4800
4801                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4802                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4803
4804                     /* Only do argument dependent lookup if regular
4805                        lookup does not find a set of member functions.
4806                        [basic.lookup.koenig]/2a  */
4807                     if (!DECL_FUNCTION_MEMBER_P (fn))
4808                       {
4809                         koenig_p = true;
4810                         if (!any_type_dependent_arguments_p (args))
4811                           postfix_expression
4812                             = perform_koenig_lookup (postfix_expression, args);
4813                       }
4814                   }
4815               }
4816
4817             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4818               {
4819                 tree instance = TREE_OPERAND (postfix_expression, 0);
4820                 tree fn = TREE_OPERAND (postfix_expression, 1);
4821
4822                 if (processing_template_decl
4823                     && (type_dependent_expression_p (instance)
4824                         || (!BASELINK_P (fn)
4825                             && TREE_CODE (fn) != FIELD_DECL)
4826                         || type_dependent_expression_p (fn)
4827                         || any_type_dependent_arguments_p (args)))
4828                   {
4829                     postfix_expression
4830                       = build_nt_call_vec (postfix_expression, args);
4831                     release_tree_vector (args);
4832                     break;
4833                   }
4834
4835                 if (BASELINK_P (fn))
4836                   {
4837                   postfix_expression
4838                     = (build_new_method_call
4839                        (instance, fn, &args, NULL_TREE,
4840                         (idk == CP_ID_KIND_QUALIFIED
4841                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4842                         /*fn_p=*/NULL,
4843                         tf_warning_or_error));
4844                   }
4845                 else
4846                   postfix_expression
4847                     = finish_call_expr (postfix_expression, &args,
4848                                         /*disallow_virtual=*/false,
4849                                         /*koenig_p=*/false,
4850                                         tf_warning_or_error);
4851               }
4852             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4853                      || TREE_CODE (postfix_expression) == MEMBER_REF
4854                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4855               postfix_expression = (build_offset_ref_call_from_tree
4856                                     (postfix_expression, &args));
4857             else if (idk == CP_ID_KIND_QUALIFIED)
4858               /* A call to a static class member, or a namespace-scope
4859                  function.  */
4860               postfix_expression
4861                 = finish_call_expr (postfix_expression, &args,
4862                                     /*disallow_virtual=*/true,
4863                                     koenig_p,
4864                                     tf_warning_or_error);
4865             else
4866               /* All other function calls.  */
4867               postfix_expression
4868                 = finish_call_expr (postfix_expression, &args,
4869                                     /*disallow_virtual=*/false,
4870                                     koenig_p,
4871                                     tf_warning_or_error);
4872
4873             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4874             idk = CP_ID_KIND_NONE;
4875
4876             release_tree_vector (args);
4877           }
4878           break;
4879
4880         case CPP_DOT:
4881         case CPP_DEREF:
4882           /* postfix-expression . template [opt] id-expression
4883              postfix-expression . pseudo-destructor-name
4884              postfix-expression -> template [opt] id-expression
4885              postfix-expression -> pseudo-destructor-name */
4886
4887           /* Consume the `.' or `->' operator.  */
4888           cp_lexer_consume_token (parser->lexer);
4889
4890           postfix_expression
4891             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4892                                                       postfix_expression,
4893                                                       false, &idk,
4894                                                       token->location);
4895
4896           is_member_access = true;
4897           break;
4898
4899         case CPP_PLUS_PLUS:
4900           /* postfix-expression ++  */
4901           /* Consume the `++' token.  */
4902           cp_lexer_consume_token (parser->lexer);
4903           /* Generate a representation for the complete expression.  */
4904           postfix_expression
4905             = finish_increment_expr (postfix_expression,
4906                                      POSTINCREMENT_EXPR);
4907           /* Increments may not appear in constant-expressions.  */
4908           if (cp_parser_non_integral_constant_expression (parser,
4909                                                           "an increment"))
4910             postfix_expression = error_mark_node;
4911           idk = CP_ID_KIND_NONE;
4912           is_member_access = false;
4913           break;
4914
4915         case CPP_MINUS_MINUS:
4916           /* postfix-expression -- */
4917           /* Consume the `--' token.  */
4918           cp_lexer_consume_token (parser->lexer);
4919           /* Generate a representation for the complete expression.  */
4920           postfix_expression
4921             = finish_increment_expr (postfix_expression,
4922                                      POSTDECREMENT_EXPR);
4923           /* Decrements may not appear in constant-expressions.  */
4924           if (cp_parser_non_integral_constant_expression (parser,
4925                                                           "a decrement"))
4926             postfix_expression = error_mark_node;
4927           idk = CP_ID_KIND_NONE;
4928           is_member_access = false;
4929           break;
4930
4931         default:
4932           if (pidk_return != NULL)
4933             * pidk_return = idk;
4934           if (member_access_only_p)
4935             return is_member_access? postfix_expression : error_mark_node;
4936           else
4937             return postfix_expression;
4938         }
4939     }
4940
4941   /* We should never get here.  */
4942   gcc_unreachable ();
4943   return error_mark_node;
4944 }
4945
4946 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4947    by cp_parser_builtin_offsetof.  We're looking for
4948
4949      postfix-expression [ expression ]
4950
4951    FOR_OFFSETOF is set if we're being called in that context, which
4952    changes how we deal with integer constant expressions.  */
4953
4954 static tree
4955 cp_parser_postfix_open_square_expression (cp_parser *parser,
4956                                           tree postfix_expression,
4957                                           bool for_offsetof)
4958 {
4959   tree index;
4960
4961   /* Consume the `[' token.  */
4962   cp_lexer_consume_token (parser->lexer);
4963
4964   /* Parse the index expression.  */
4965   /* ??? For offsetof, there is a question of what to allow here.  If
4966      offsetof is not being used in an integral constant expression context,
4967      then we *could* get the right answer by computing the value at runtime.
4968      If we are in an integral constant expression context, then we might
4969      could accept any constant expression; hard to say without analysis.
4970      Rather than open the barn door too wide right away, allow only integer
4971      constant expressions here.  */
4972   if (for_offsetof)
4973     index = cp_parser_constant_expression (parser, false, NULL);
4974   else
4975     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4976
4977   /* Look for the closing `]'.  */
4978   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4979
4980   /* Build the ARRAY_REF.  */
4981   postfix_expression = grok_array_decl (postfix_expression, index);
4982
4983   /* When not doing offsetof, array references are not permitted in
4984      constant-expressions.  */
4985   if (!for_offsetof
4986       && (cp_parser_non_integral_constant_expression
4987           (parser, "an array reference")))
4988     postfix_expression = error_mark_node;
4989
4990   return postfix_expression;
4991 }
4992
4993 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4994    by cp_parser_builtin_offsetof.  We're looking for
4995
4996      postfix-expression . template [opt] id-expression
4997      postfix-expression . pseudo-destructor-name
4998      postfix-expression -> template [opt] id-expression
4999      postfix-expression -> pseudo-destructor-name
5000
5001    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5002    limits what of the above we'll actually accept, but nevermind.
5003    TOKEN_TYPE is the "." or "->" token, which will already have been
5004    removed from the stream.  */
5005
5006 static tree
5007 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5008                                         enum cpp_ttype token_type,
5009                                         tree postfix_expression,
5010                                         bool for_offsetof, cp_id_kind *idk,
5011                                         location_t location)
5012 {
5013   tree name;
5014   bool dependent_p;
5015   bool pseudo_destructor_p;
5016   tree scope = NULL_TREE;
5017
5018   /* If this is a `->' operator, dereference the pointer.  */
5019   if (token_type == CPP_DEREF)
5020     postfix_expression = build_x_arrow (postfix_expression);
5021   /* Check to see whether or not the expression is type-dependent.  */
5022   dependent_p = type_dependent_expression_p (postfix_expression);
5023   /* The identifier following the `->' or `.' is not qualified.  */
5024   parser->scope = NULL_TREE;
5025   parser->qualifying_scope = NULL_TREE;
5026   parser->object_scope = NULL_TREE;
5027   *idk = CP_ID_KIND_NONE;
5028
5029   /* Enter the scope corresponding to the type of the object
5030      given by the POSTFIX_EXPRESSION.  */
5031   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5032     {
5033       scope = TREE_TYPE (postfix_expression);
5034       /* According to the standard, no expression should ever have
5035          reference type.  Unfortunately, we do not currently match
5036          the standard in this respect in that our internal representation
5037          of an expression may have reference type even when the standard
5038          says it does not.  Therefore, we have to manually obtain the
5039          underlying type here.  */
5040       scope = non_reference (scope);
5041       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5042       if (scope == unknown_type_node)
5043         {
5044           error_at (location, "%qE does not have class type",
5045                     postfix_expression);
5046           scope = NULL_TREE;
5047         }
5048       else
5049         scope = complete_type_or_else (scope, NULL_TREE);
5050       /* Let the name lookup machinery know that we are processing a
5051          class member access expression.  */
5052       parser->context->object_type = scope;
5053       /* If something went wrong, we want to be able to discern that case,
5054          as opposed to the case where there was no SCOPE due to the type
5055          of expression being dependent.  */
5056       if (!scope)
5057         scope = error_mark_node;
5058       /* If the SCOPE was erroneous, make the various semantic analysis
5059          functions exit quickly -- and without issuing additional error
5060          messages.  */
5061       if (scope == error_mark_node)
5062         postfix_expression = error_mark_node;
5063     }
5064
5065   /* Assume this expression is not a pseudo-destructor access.  */
5066   pseudo_destructor_p = false;
5067
5068   /* If the SCOPE is a scalar type, then, if this is a valid program,
5069      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5070      is type dependent, it can be pseudo-destructor-name or something else.
5071      Try to parse it as pseudo-destructor-name first.  */
5072   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5073     {
5074       tree s;
5075       tree type;
5076
5077       cp_parser_parse_tentatively (parser);
5078       /* Parse the pseudo-destructor-name.  */
5079       s = NULL_TREE;
5080       cp_parser_pseudo_destructor_name (parser, &s, &type);
5081       if (dependent_p
5082           && (cp_parser_error_occurred (parser)
5083               || TREE_CODE (type) != TYPE_DECL
5084               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5085         cp_parser_abort_tentative_parse (parser);
5086       else if (cp_parser_parse_definitely (parser))
5087         {
5088           pseudo_destructor_p = true;
5089           postfix_expression
5090             = finish_pseudo_destructor_expr (postfix_expression,
5091                                              s, TREE_TYPE (type));
5092         }
5093     }
5094
5095   if (!pseudo_destructor_p)
5096     {
5097       /* If the SCOPE is not a scalar type, we are looking at an
5098          ordinary class member access expression, rather than a
5099          pseudo-destructor-name.  */
5100       bool template_p;
5101       cp_token *token = cp_lexer_peek_token (parser->lexer);
5102       /* Parse the id-expression.  */
5103       name = (cp_parser_id_expression
5104               (parser,
5105                cp_parser_optional_template_keyword (parser),
5106                /*check_dependency_p=*/true,
5107                &template_p,
5108                /*declarator_p=*/false,
5109                /*optional_p=*/false));
5110       /* In general, build a SCOPE_REF if the member name is qualified.
5111          However, if the name was not dependent and has already been
5112          resolved; there is no need to build the SCOPE_REF.  For example;
5113
5114              struct X { void f(); };
5115              template <typename T> void f(T* t) { t->X::f(); }
5116
5117          Even though "t" is dependent, "X::f" is not and has been resolved
5118          to a BASELINK; there is no need to include scope information.  */
5119
5120       /* But we do need to remember that there was an explicit scope for
5121          virtual function calls.  */
5122       if (parser->scope)
5123         *idk = CP_ID_KIND_QUALIFIED;
5124
5125       /* If the name is a template-id that names a type, we will get a
5126          TYPE_DECL here.  That is invalid code.  */
5127       if (TREE_CODE (name) == TYPE_DECL)
5128         {
5129           error_at (token->location, "invalid use of %qD", name);
5130           postfix_expression = error_mark_node;
5131         }
5132       else
5133         {
5134           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5135             {
5136               name = build_qualified_name (/*type=*/NULL_TREE,
5137                                            parser->scope,
5138                                            name,
5139                                            template_p);
5140               parser->scope = NULL_TREE;
5141               parser->qualifying_scope = NULL_TREE;
5142               parser->object_scope = NULL_TREE;
5143             }
5144           if (scope && name && BASELINK_P (name))
5145             adjust_result_of_qualified_name_lookup
5146               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5147           postfix_expression
5148             = finish_class_member_access_expr (postfix_expression, name,
5149                                                template_p, 
5150                                                tf_warning_or_error);
5151         }
5152     }
5153
5154   /* We no longer need to look up names in the scope of the object on
5155      the left-hand side of the `.' or `->' operator.  */
5156   parser->context->object_type = NULL_TREE;
5157
5158   /* Outside of offsetof, these operators may not appear in
5159      constant-expressions.  */
5160   if (!for_offsetof
5161       && (cp_parser_non_integral_constant_expression
5162           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5163     postfix_expression = error_mark_node;
5164
5165   return postfix_expression;
5166 }
5167
5168 /* Parse a parenthesized expression-list.
5169
5170    expression-list:
5171      assignment-expression
5172      expression-list, assignment-expression
5173
5174    attribute-list:
5175      expression-list
5176      identifier
5177      identifier, expression-list
5178
5179    CAST_P is true if this expression is the target of a cast.
5180
5181    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5182    argument pack.
5183
5184    Returns a vector of trees.  Each element is a representation of an
5185    assignment-expression.  NULL is returned if the ( and or ) are
5186    missing.  An empty, but allocated, vector is returned on no
5187    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is true
5188    if this is really an attribute list being parsed.  If
5189    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5190    not all of the expressions in the list were constant.  */
5191
5192 static VEC(tree,gc) *
5193 cp_parser_parenthesized_expression_list (cp_parser* parser,
5194                                          bool is_attribute_list,
5195                                          bool cast_p,
5196                                          bool allow_expansion_p,
5197                                          bool *non_constant_p)
5198 {
5199   VEC(tree,gc) *expression_list;
5200   bool fold_expr_p = is_attribute_list;
5201   tree identifier = NULL_TREE;
5202   bool saved_greater_than_is_operator_p;
5203
5204   /* Assume all the expressions will be constant.  */
5205   if (non_constant_p)
5206     *non_constant_p = false;
5207
5208   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5209     return NULL;
5210
5211   expression_list = make_tree_vector ();
5212
5213   /* Within a parenthesized expression, a `>' token is always
5214      the greater-than operator.  */
5215   saved_greater_than_is_operator_p
5216     = parser->greater_than_is_operator_p;
5217   parser->greater_than_is_operator_p = true;
5218
5219   /* Consume expressions until there are no more.  */
5220   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5221     while (true)
5222       {
5223         tree expr;
5224
5225         /* At the beginning of attribute lists, check to see if the
5226            next token is an identifier.  */
5227         if (is_attribute_list
5228             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5229           {
5230             cp_token *token;
5231
5232             /* Consume the identifier.  */
5233             token = cp_lexer_consume_token (parser->lexer);
5234             /* Save the identifier.  */
5235             identifier = token->u.value;
5236           }
5237         else
5238           {
5239             bool expr_non_constant_p;
5240
5241             /* Parse the next assignment-expression.  */
5242             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5243               {
5244                 /* A braced-init-list.  */
5245                 maybe_warn_cpp0x ("extended initializer lists");
5246                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5247                 if (non_constant_p && expr_non_constant_p)
5248                   *non_constant_p = true;
5249               }
5250             else if (non_constant_p)
5251               {
5252                 expr = (cp_parser_constant_expression
5253                         (parser, /*allow_non_constant_p=*/true,
5254                          &expr_non_constant_p));
5255                 if (expr_non_constant_p)
5256                   *non_constant_p = true;
5257               }
5258             else
5259               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5260
5261             if (fold_expr_p)
5262               expr = fold_non_dependent_expr (expr);
5263
5264             /* If we have an ellipsis, then this is an expression
5265                expansion.  */
5266             if (allow_expansion_p
5267                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5268               {
5269                 /* Consume the `...'.  */
5270                 cp_lexer_consume_token (parser->lexer);
5271
5272                 /* Build the argument pack.  */
5273                 expr = make_pack_expansion (expr);
5274               }
5275
5276              /* Add it to the list.  We add error_mark_node
5277                 expressions to the list, so that we can still tell if
5278                 the correct form for a parenthesized expression-list
5279                 is found. That gives better errors.  */
5280             VEC_safe_push (tree, gc, expression_list, expr);
5281
5282             if (expr == error_mark_node)
5283               goto skip_comma;
5284           }
5285
5286         /* After the first item, attribute lists look the same as
5287            expression lists.  */
5288         is_attribute_list = false;
5289
5290       get_comma:;
5291         /* If the next token isn't a `,', then we are done.  */
5292         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5293           break;
5294
5295         /* Otherwise, consume the `,' and keep going.  */
5296         cp_lexer_consume_token (parser->lexer);
5297       }
5298
5299   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5300     {
5301       int ending;
5302
5303     skip_comma:;
5304       /* We try and resync to an unnested comma, as that will give the
5305          user better diagnostics.  */
5306       ending = cp_parser_skip_to_closing_parenthesis (parser,
5307                                                       /*recovering=*/true,
5308                                                       /*or_comma=*/true,
5309                                                       /*consume_paren=*/true);
5310       if (ending < 0)
5311         goto get_comma;
5312       if (!ending)
5313         {
5314           parser->greater_than_is_operator_p
5315             = saved_greater_than_is_operator_p;
5316           return NULL;
5317         }
5318     }
5319
5320   parser->greater_than_is_operator_p
5321     = saved_greater_than_is_operator_p;
5322
5323   if (identifier)
5324     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5325
5326   return expression_list;
5327 }
5328
5329 /* Parse a pseudo-destructor-name.
5330
5331    pseudo-destructor-name:
5332      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5333      :: [opt] nested-name-specifier template template-id :: ~ type-name
5334      :: [opt] nested-name-specifier [opt] ~ type-name
5335
5336    If either of the first two productions is used, sets *SCOPE to the
5337    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5338    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5339    or ERROR_MARK_NODE if the parse fails.  */
5340
5341 static void
5342 cp_parser_pseudo_destructor_name (cp_parser* parser,
5343                                   tree* scope,
5344                                   tree* type)
5345 {
5346   bool nested_name_specifier_p;
5347
5348   /* Assume that things will not work out.  */
5349   *type = error_mark_node;
5350
5351   /* Look for the optional `::' operator.  */
5352   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5353   /* Look for the optional nested-name-specifier.  */
5354   nested_name_specifier_p
5355     = (cp_parser_nested_name_specifier_opt (parser,
5356                                             /*typename_keyword_p=*/false,
5357                                             /*check_dependency_p=*/true,
5358                                             /*type_p=*/false,
5359                                             /*is_declaration=*/false)
5360        != NULL_TREE);
5361   /* Now, if we saw a nested-name-specifier, we might be doing the
5362      second production.  */
5363   if (nested_name_specifier_p
5364       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5365     {
5366       /* Consume the `template' keyword.  */
5367       cp_lexer_consume_token (parser->lexer);
5368       /* Parse the template-id.  */
5369       cp_parser_template_id (parser,
5370                              /*template_keyword_p=*/true,
5371                              /*check_dependency_p=*/false,
5372                              /*is_declaration=*/true);
5373       /* Look for the `::' token.  */
5374       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5375     }
5376   /* If the next token is not a `~', then there might be some
5377      additional qualification.  */
5378   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5379     {
5380       /* At this point, we're looking for "type-name :: ~".  The type-name
5381          must not be a class-name, since this is a pseudo-destructor.  So,
5382          it must be either an enum-name, or a typedef-name -- both of which
5383          are just identifiers.  So, we peek ahead to check that the "::"
5384          and "~" tokens are present; if they are not, then we can avoid
5385          calling type_name.  */
5386       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5387           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5388           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5389         {
5390           cp_parser_error (parser, "non-scalar type");
5391           return;
5392         }
5393
5394       /* Look for the type-name.  */
5395       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5396       if (*scope == error_mark_node)
5397         return;
5398
5399       /* Look for the `::' token.  */
5400       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5401     }
5402   else
5403     *scope = NULL_TREE;
5404
5405   /* Look for the `~'.  */
5406   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5407   /* Look for the type-name again.  We are not responsible for
5408      checking that it matches the first type-name.  */
5409   *type = cp_parser_nonclass_name (parser);
5410 }
5411
5412 /* Parse a unary-expression.
5413
5414    unary-expression:
5415      postfix-expression
5416      ++ cast-expression
5417      -- cast-expression
5418      unary-operator cast-expression
5419      sizeof unary-expression
5420      sizeof ( type-id )
5421      new-expression
5422      delete-expression
5423
5424    GNU Extensions:
5425
5426    unary-expression:
5427      __extension__ cast-expression
5428      __alignof__ unary-expression
5429      __alignof__ ( type-id )
5430      __real__ cast-expression
5431      __imag__ cast-expression
5432      && identifier
5433
5434    ADDRESS_P is true iff the unary-expression is appearing as the
5435    operand of the `&' operator.   CAST_P is true if this expression is
5436    the target of a cast.
5437
5438    Returns a representation of the expression.  */
5439
5440 static tree
5441 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5442                             cp_id_kind * pidk)
5443 {
5444   cp_token *token;
5445   enum tree_code unary_operator;
5446
5447   /* Peek at the next token.  */
5448   token = cp_lexer_peek_token (parser->lexer);
5449   /* Some keywords give away the kind of expression.  */
5450   if (token->type == CPP_KEYWORD)
5451     {
5452       enum rid keyword = token->keyword;
5453
5454       switch (keyword)
5455         {
5456         case RID_ALIGNOF:
5457         case RID_SIZEOF:
5458           {
5459             tree operand;
5460             enum tree_code op;
5461
5462             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5463             /* Consume the token.  */
5464             cp_lexer_consume_token (parser->lexer);
5465             /* Parse the operand.  */
5466             operand = cp_parser_sizeof_operand (parser, keyword);
5467
5468             if (TYPE_P (operand))
5469               return cxx_sizeof_or_alignof_type (operand, op, true);
5470             else
5471               return cxx_sizeof_or_alignof_expr (operand, op, true);
5472           }
5473
5474         case RID_NEW:
5475           return cp_parser_new_expression (parser);
5476
5477         case RID_DELETE:
5478           return cp_parser_delete_expression (parser);
5479
5480         case RID_EXTENSION:
5481           {
5482             /* The saved value of the PEDANTIC flag.  */
5483             int saved_pedantic;
5484             tree expr;
5485
5486             /* Save away the PEDANTIC flag.  */
5487             cp_parser_extension_opt (parser, &saved_pedantic);
5488             /* Parse the cast-expression.  */
5489             expr = cp_parser_simple_cast_expression (parser);
5490             /* Restore the PEDANTIC flag.  */
5491             pedantic = saved_pedantic;
5492
5493             return expr;
5494           }
5495
5496         case RID_REALPART:
5497         case RID_IMAGPART:
5498           {
5499             tree expression;
5500
5501             /* Consume the `__real__' or `__imag__' token.  */
5502             cp_lexer_consume_token (parser->lexer);
5503             /* Parse the cast-expression.  */
5504             expression = cp_parser_simple_cast_expression (parser);
5505             /* Create the complete representation.  */
5506             return build_x_unary_op ((keyword == RID_REALPART
5507                                       ? REALPART_EXPR : IMAGPART_EXPR),
5508                                      expression,
5509                                      tf_warning_or_error);
5510           }
5511           break;
5512
5513         default:
5514           break;
5515         }
5516     }
5517
5518   /* Look for the `:: new' and `:: delete', which also signal the
5519      beginning of a new-expression, or delete-expression,
5520      respectively.  If the next token is `::', then it might be one of
5521      these.  */
5522   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5523     {
5524       enum rid keyword;
5525
5526       /* See if the token after the `::' is one of the keywords in
5527          which we're interested.  */
5528       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5529       /* If it's `new', we have a new-expression.  */
5530       if (keyword == RID_NEW)
5531         return cp_parser_new_expression (parser);
5532       /* Similarly, for `delete'.  */
5533       else if (keyword == RID_DELETE)
5534         return cp_parser_delete_expression (parser);
5535     }
5536
5537   /* Look for a unary operator.  */
5538   unary_operator = cp_parser_unary_operator (token);
5539   /* The `++' and `--' operators can be handled similarly, even though
5540      they are not technically unary-operators in the grammar.  */
5541   if (unary_operator == ERROR_MARK)
5542     {
5543       if (token->type == CPP_PLUS_PLUS)
5544         unary_operator = PREINCREMENT_EXPR;
5545       else if (token->type == CPP_MINUS_MINUS)
5546         unary_operator = PREDECREMENT_EXPR;
5547       /* Handle the GNU address-of-label extension.  */
5548       else if (cp_parser_allow_gnu_extensions_p (parser)
5549                && token->type == CPP_AND_AND)
5550         {
5551           tree identifier;
5552           tree expression;
5553           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5554
5555           /* Consume the '&&' token.  */
5556           cp_lexer_consume_token (parser->lexer);
5557           /* Look for the identifier.  */
5558           identifier = cp_parser_identifier (parser);
5559           /* Create an expression representing the address.  */
5560           expression = finish_label_address_expr (identifier, loc);
5561           if (cp_parser_non_integral_constant_expression (parser,
5562                                                 "the address of a label"))
5563             expression = error_mark_node;
5564           return expression;
5565         }
5566     }
5567   if (unary_operator != ERROR_MARK)
5568     {
5569       tree cast_expression;
5570       tree expression = error_mark_node;
5571       const char *non_constant_p = NULL;
5572
5573       /* Consume the operator token.  */
5574       token = cp_lexer_consume_token (parser->lexer);
5575       /* Parse the cast-expression.  */
5576       cast_expression
5577         = cp_parser_cast_expression (parser,
5578                                      unary_operator == ADDR_EXPR,
5579                                      /*cast_p=*/false, pidk);
5580       /* Now, build an appropriate representation.  */
5581       switch (unary_operator)
5582         {
5583         case INDIRECT_REF:
5584           non_constant_p = "%<*%>";
5585           expression = build_x_indirect_ref (cast_expression, "unary *",
5586                                              tf_warning_or_error);
5587           break;
5588
5589         case ADDR_EXPR:
5590           non_constant_p = "%<&%>";
5591           /* Fall through.  */
5592         case BIT_NOT_EXPR:
5593           expression = build_x_unary_op (unary_operator, cast_expression,
5594                                          tf_warning_or_error);
5595           break;
5596
5597         case PREINCREMENT_EXPR:
5598         case PREDECREMENT_EXPR:
5599           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5600                             ? "%<++%>" : "%<--%>");
5601           /* Fall through.  */
5602         case UNARY_PLUS_EXPR:
5603         case NEGATE_EXPR:
5604         case TRUTH_NOT_EXPR:
5605           expression = finish_unary_op_expr (unary_operator, cast_expression);
5606           break;
5607
5608         default:
5609           gcc_unreachable ();
5610         }
5611
5612       if (non_constant_p
5613           && cp_parser_non_integral_constant_expression (parser,
5614                                                          non_constant_p))
5615         expression = error_mark_node;
5616
5617       return expression;
5618     }
5619
5620   return cp_parser_postfix_expression (parser, address_p, cast_p,
5621                                        /*member_access_only_p=*/false,
5622                                        pidk);
5623 }
5624
5625 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5626    unary-operator, the corresponding tree code is returned.  */
5627
5628 static enum tree_code
5629 cp_parser_unary_operator (cp_token* token)
5630 {
5631   switch (token->type)
5632     {
5633     case CPP_MULT:
5634       return INDIRECT_REF;
5635
5636     case CPP_AND:
5637       return ADDR_EXPR;
5638
5639     case CPP_PLUS:
5640       return UNARY_PLUS_EXPR;
5641
5642     case CPP_MINUS:
5643       return NEGATE_EXPR;
5644
5645     case CPP_NOT:
5646       return TRUTH_NOT_EXPR;
5647
5648     case CPP_COMPL:
5649       return BIT_NOT_EXPR;
5650
5651     default:
5652       return ERROR_MARK;
5653     }
5654 }
5655
5656 /* Parse a new-expression.
5657
5658    new-expression:
5659      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5660      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5661
5662    Returns a representation of the expression.  */
5663
5664 static tree
5665 cp_parser_new_expression (cp_parser* parser)
5666 {
5667   bool global_scope_p;
5668   VEC(tree,gc) *placement;
5669   tree type;
5670   VEC(tree,gc) *initializer;
5671   tree nelts;
5672   tree ret;
5673
5674   /* Look for the optional `::' operator.  */
5675   global_scope_p
5676     = (cp_parser_global_scope_opt (parser,
5677                                    /*current_scope_valid_p=*/false)
5678        != NULL_TREE);
5679   /* Look for the `new' operator.  */
5680   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5681   /* There's no easy way to tell a new-placement from the
5682      `( type-id )' construct.  */
5683   cp_parser_parse_tentatively (parser);
5684   /* Look for a new-placement.  */
5685   placement = cp_parser_new_placement (parser);
5686   /* If that didn't work out, there's no new-placement.  */
5687   if (!cp_parser_parse_definitely (parser))
5688     {
5689       if (placement != NULL)
5690         release_tree_vector (placement);
5691       placement = NULL;
5692     }
5693
5694   /* If the next token is a `(', then we have a parenthesized
5695      type-id.  */
5696   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5697     {
5698       cp_token *token;
5699       /* Consume the `('.  */
5700       cp_lexer_consume_token (parser->lexer);
5701       /* Parse the type-id.  */
5702       type = cp_parser_type_id (parser);
5703       /* Look for the closing `)'.  */
5704       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5705       token = cp_lexer_peek_token (parser->lexer);
5706       /* There should not be a direct-new-declarator in this production,
5707          but GCC used to allowed this, so we check and emit a sensible error
5708          message for this case.  */
5709       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5710         {
5711           error_at (token->location,
5712                     "array bound forbidden after parenthesized type-id");
5713           inform (token->location, 
5714                   "try removing the parentheses around the type-id");
5715           cp_parser_direct_new_declarator (parser);
5716         }
5717       nelts = NULL_TREE;
5718     }
5719   /* Otherwise, there must be a new-type-id.  */
5720   else
5721     type = cp_parser_new_type_id (parser, &nelts);
5722
5723   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5724   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5725       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5726     initializer = cp_parser_new_initializer (parser);
5727   else
5728     initializer = NULL;
5729
5730   /* A new-expression may not appear in an integral constant
5731      expression.  */
5732   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5733     ret = error_mark_node;
5734   else
5735     {
5736       /* Create a representation of the new-expression.  */
5737       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5738                        tf_warning_or_error);
5739     }
5740
5741   if (placement != NULL)
5742     release_tree_vector (placement);
5743   if (initializer != NULL)
5744     release_tree_vector (initializer);
5745
5746   return ret;
5747 }
5748
5749 /* Parse a new-placement.
5750
5751    new-placement:
5752      ( expression-list )
5753
5754    Returns the same representation as for an expression-list.  */
5755
5756 static VEC(tree,gc) *
5757 cp_parser_new_placement (cp_parser* parser)
5758 {
5759   VEC(tree,gc) *expression_list;
5760
5761   /* Parse the expression-list.  */
5762   expression_list = (cp_parser_parenthesized_expression_list
5763                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5764                       /*non_constant_p=*/NULL));
5765
5766   return expression_list;
5767 }
5768
5769 /* Parse a new-type-id.
5770
5771    new-type-id:
5772      type-specifier-seq new-declarator [opt]
5773
5774    Returns the TYPE allocated.  If the new-type-id indicates an array
5775    type, *NELTS is set to the number of elements in the last array
5776    bound; the TYPE will not include the last array bound.  */
5777
5778 static tree
5779 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5780 {
5781   cp_decl_specifier_seq type_specifier_seq;
5782   cp_declarator *new_declarator;
5783   cp_declarator *declarator;
5784   cp_declarator *outer_declarator;
5785   const char *saved_message;
5786   tree type;
5787
5788   /* The type-specifier sequence must not contain type definitions.
5789      (It cannot contain declarations of new types either, but if they
5790      are not definitions we will catch that because they are not
5791      complete.)  */
5792   saved_message = parser->type_definition_forbidden_message;
5793   parser->type_definition_forbidden_message
5794     = "types may not be defined in a new-type-id";
5795   /* Parse the type-specifier-seq.  */
5796   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5797                                 &type_specifier_seq);
5798   /* Restore the old message.  */
5799   parser->type_definition_forbidden_message = saved_message;
5800   /* Parse the new-declarator.  */
5801   new_declarator = cp_parser_new_declarator_opt (parser);
5802
5803   /* Determine the number of elements in the last array dimension, if
5804      any.  */
5805   *nelts = NULL_TREE;
5806   /* Skip down to the last array dimension.  */
5807   declarator = new_declarator;
5808   outer_declarator = NULL;
5809   while (declarator && (declarator->kind == cdk_pointer
5810                         || declarator->kind == cdk_ptrmem))
5811     {
5812       outer_declarator = declarator;
5813       declarator = declarator->declarator;
5814     }
5815   while (declarator
5816          && declarator->kind == cdk_array
5817          && declarator->declarator
5818          && declarator->declarator->kind == cdk_array)
5819     {
5820       outer_declarator = declarator;
5821       declarator = declarator->declarator;
5822     }
5823
5824   if (declarator && declarator->kind == cdk_array)
5825     {
5826       *nelts = declarator->u.array.bounds;
5827       if (*nelts == error_mark_node)
5828         *nelts = integer_one_node;
5829
5830       if (outer_declarator)
5831         outer_declarator->declarator = declarator->declarator;
5832       else
5833         new_declarator = NULL;
5834     }
5835
5836   type = groktypename (&type_specifier_seq, new_declarator, false);
5837   return type;
5838 }
5839
5840 /* Parse an (optional) new-declarator.
5841
5842    new-declarator:
5843      ptr-operator new-declarator [opt]
5844      direct-new-declarator
5845
5846    Returns the declarator.  */
5847
5848 static cp_declarator *
5849 cp_parser_new_declarator_opt (cp_parser* parser)
5850 {
5851   enum tree_code code;
5852   tree type;
5853   cp_cv_quals cv_quals;
5854
5855   /* We don't know if there's a ptr-operator next, or not.  */
5856   cp_parser_parse_tentatively (parser);
5857   /* Look for a ptr-operator.  */
5858   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5859   /* If that worked, look for more new-declarators.  */
5860   if (cp_parser_parse_definitely (parser))
5861     {
5862       cp_declarator *declarator;
5863
5864       /* Parse another optional declarator.  */
5865       declarator = cp_parser_new_declarator_opt (parser);
5866
5867       return cp_parser_make_indirect_declarator
5868         (code, type, cv_quals, declarator);
5869     }
5870
5871   /* If the next token is a `[', there is a direct-new-declarator.  */
5872   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5873     return cp_parser_direct_new_declarator (parser);
5874
5875   return NULL;
5876 }
5877
5878 /* Parse a direct-new-declarator.
5879
5880    direct-new-declarator:
5881      [ expression ]
5882      direct-new-declarator [constant-expression]
5883
5884    */
5885
5886 static cp_declarator *
5887 cp_parser_direct_new_declarator (cp_parser* parser)
5888 {
5889   cp_declarator *declarator = NULL;
5890
5891   while (true)
5892     {
5893       tree expression;
5894
5895       /* Look for the opening `['.  */
5896       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5897       /* The first expression is not required to be constant.  */
5898       if (!declarator)
5899         {
5900           cp_token *token = cp_lexer_peek_token (parser->lexer);
5901           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5902           /* The standard requires that the expression have integral
5903              type.  DR 74 adds enumeration types.  We believe that the
5904              real intent is that these expressions be handled like the
5905              expression in a `switch' condition, which also allows
5906              classes with a single conversion to integral or
5907              enumeration type.  */
5908           if (!processing_template_decl)
5909             {
5910               expression
5911                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5912                                               expression,
5913                                               /*complain=*/true);
5914               if (!expression)
5915                 {
5916                   error_at (token->location,
5917                             "expression in new-declarator must have integral "
5918                             "or enumeration type");
5919                   expression = error_mark_node;
5920                 }
5921             }
5922         }
5923       /* But all the other expressions must be.  */
5924       else
5925         expression
5926           = cp_parser_constant_expression (parser,
5927                                            /*allow_non_constant=*/false,
5928                                            NULL);
5929       /* Look for the closing `]'.  */
5930       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5931
5932       /* Add this bound to the declarator.  */
5933       declarator = make_array_declarator (declarator, expression);
5934
5935       /* If the next token is not a `[', then there are no more
5936          bounds.  */
5937       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5938         break;
5939     }
5940
5941   return declarator;
5942 }
5943
5944 /* Parse a new-initializer.
5945
5946    new-initializer:
5947      ( expression-list [opt] )
5948      braced-init-list
5949
5950    Returns a representation of the expression-list.  */
5951
5952 static VEC(tree,gc) *
5953 cp_parser_new_initializer (cp_parser* parser)
5954 {
5955   VEC(tree,gc) *expression_list;
5956
5957   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5958     {
5959       tree t;
5960       bool expr_non_constant_p;
5961       maybe_warn_cpp0x ("extended initializer lists");
5962       t = cp_parser_braced_list (parser, &expr_non_constant_p);
5963       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5964       expression_list = make_tree_vector_single (t);
5965     }
5966   else
5967     expression_list = (cp_parser_parenthesized_expression_list
5968                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5969                         /*non_constant_p=*/NULL));
5970
5971   return expression_list;
5972 }
5973
5974 /* Parse a delete-expression.
5975
5976    delete-expression:
5977      :: [opt] delete cast-expression
5978      :: [opt] delete [ ] cast-expression
5979
5980    Returns a representation of the expression.  */
5981
5982 static tree
5983 cp_parser_delete_expression (cp_parser* parser)
5984 {
5985   bool global_scope_p;
5986   bool array_p;
5987   tree expression;
5988
5989   /* Look for the optional `::' operator.  */
5990   global_scope_p
5991     = (cp_parser_global_scope_opt (parser,
5992                                    /*current_scope_valid_p=*/false)
5993        != NULL_TREE);
5994   /* Look for the `delete' keyword.  */
5995   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5996   /* See if the array syntax is in use.  */
5997   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5998     {
5999       /* Consume the `[' token.  */
6000       cp_lexer_consume_token (parser->lexer);
6001       /* Look for the `]' token.  */
6002       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6003       /* Remember that this is the `[]' construct.  */
6004       array_p = true;
6005     }
6006   else
6007     array_p = false;
6008
6009   /* Parse the cast-expression.  */
6010   expression = cp_parser_simple_cast_expression (parser);
6011
6012   /* A delete-expression may not appear in an integral constant
6013      expression.  */
6014   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6015     return error_mark_node;
6016
6017   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6018 }
6019
6020 /* Returns true if TOKEN may start a cast-expression and false
6021    otherwise.  */
6022
6023 static bool
6024 cp_parser_token_starts_cast_expression (cp_token *token)
6025 {
6026   switch (token->type)
6027     {
6028     case CPP_COMMA:
6029     case CPP_SEMICOLON:
6030     case CPP_QUERY:
6031     case CPP_COLON:
6032     case CPP_CLOSE_SQUARE:
6033     case CPP_CLOSE_PAREN:
6034     case CPP_CLOSE_BRACE:
6035     case CPP_DOT:
6036     case CPP_DOT_STAR:
6037     case CPP_DEREF:
6038     case CPP_DEREF_STAR:
6039     case CPP_DIV:
6040     case CPP_MOD:
6041     case CPP_LSHIFT:
6042     case CPP_RSHIFT:
6043     case CPP_LESS:
6044     case CPP_GREATER:
6045     case CPP_LESS_EQ:
6046     case CPP_GREATER_EQ:
6047     case CPP_EQ_EQ:
6048     case CPP_NOT_EQ:
6049     case CPP_EQ:
6050     case CPP_MULT_EQ:
6051     case CPP_DIV_EQ:
6052     case CPP_MOD_EQ:
6053     case CPP_PLUS_EQ:
6054     case CPP_MINUS_EQ:
6055     case CPP_RSHIFT_EQ:
6056     case CPP_LSHIFT_EQ:
6057     case CPP_AND_EQ:
6058     case CPP_XOR_EQ:
6059     case CPP_OR_EQ:
6060     case CPP_XOR:
6061     case CPP_OR:
6062     case CPP_OR_OR:
6063     case CPP_EOF:
6064       return false;
6065
6066       /* '[' may start a primary-expression in obj-c++.  */
6067     case CPP_OPEN_SQUARE:
6068       return c_dialect_objc ();
6069
6070     default:
6071       return true;
6072     }
6073 }
6074
6075 /* Parse a cast-expression.
6076
6077    cast-expression:
6078      unary-expression
6079      ( type-id ) cast-expression
6080
6081    ADDRESS_P is true iff the unary-expression is appearing as the
6082    operand of the `&' operator.   CAST_P is true if this expression is
6083    the target of a cast.
6084
6085    Returns a representation of the expression.  */
6086
6087 static tree
6088 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6089                            cp_id_kind * pidk)
6090 {
6091   /* If it's a `(', then we might be looking at a cast.  */
6092   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6093     {
6094       tree type = NULL_TREE;
6095       tree expr = NULL_TREE;
6096       bool compound_literal_p;
6097       const char *saved_message;
6098
6099       /* There's no way to know yet whether or not this is a cast.
6100          For example, `(int (3))' is a unary-expression, while `(int)
6101          3' is a cast.  So, we resort to parsing tentatively.  */
6102       cp_parser_parse_tentatively (parser);
6103       /* Types may not be defined in a cast.  */
6104       saved_message = parser->type_definition_forbidden_message;
6105       parser->type_definition_forbidden_message
6106         = "types may not be defined in casts";
6107       /* Consume the `('.  */
6108       cp_lexer_consume_token (parser->lexer);
6109       /* A very tricky bit is that `(struct S) { 3 }' is a
6110          compound-literal (which we permit in C++ as an extension).
6111          But, that construct is not a cast-expression -- it is a
6112          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6113          is legal; if the compound-literal were a cast-expression,
6114          you'd need an extra set of parentheses.)  But, if we parse
6115          the type-id, and it happens to be a class-specifier, then we
6116          will commit to the parse at that point, because we cannot
6117          undo the action that is done when creating a new class.  So,
6118          then we cannot back up and do a postfix-expression.
6119
6120          Therefore, we scan ahead to the closing `)', and check to see
6121          if the token after the `)' is a `{'.  If so, we are not
6122          looking at a cast-expression.
6123
6124          Save tokens so that we can put them back.  */
6125       cp_lexer_save_tokens (parser->lexer);
6126       /* Skip tokens until the next token is a closing parenthesis.
6127          If we find the closing `)', and the next token is a `{', then
6128          we are looking at a compound-literal.  */
6129       compound_literal_p
6130         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6131                                                   /*consume_paren=*/true)
6132            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6133       /* Roll back the tokens we skipped.  */
6134       cp_lexer_rollback_tokens (parser->lexer);
6135       /* If we were looking at a compound-literal, simulate an error
6136          so that the call to cp_parser_parse_definitely below will
6137          fail.  */
6138       if (compound_literal_p)
6139         cp_parser_simulate_error (parser);
6140       else
6141         {
6142           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6143           parser->in_type_id_in_expr_p = true;
6144           /* Look for the type-id.  */
6145           type = cp_parser_type_id (parser);
6146           /* Look for the closing `)'.  */
6147           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6148           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6149         }
6150
6151       /* Restore the saved message.  */
6152       parser->type_definition_forbidden_message = saved_message;
6153
6154       /* At this point this can only be either a cast or a
6155          parenthesized ctor such as `(T ())' that looks like a cast to
6156          function returning T.  */
6157       if (!cp_parser_error_occurred (parser)
6158           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6159                                                      (parser->lexer)))
6160         {
6161           cp_parser_parse_definitely (parser);
6162           expr = cp_parser_cast_expression (parser,
6163                                             /*address_p=*/false,
6164                                             /*cast_p=*/true, pidk);
6165
6166           /* Warn about old-style casts, if so requested.  */
6167           if (warn_old_style_cast
6168               && !in_system_header
6169               && !VOID_TYPE_P (type)
6170               && current_lang_name != lang_name_c)
6171             warning (OPT_Wold_style_cast, "use of old-style cast");
6172
6173           /* Only type conversions to integral or enumeration types
6174              can be used in constant-expressions.  */
6175           if (!cast_valid_in_integral_constant_expression_p (type)
6176               && (cp_parser_non_integral_constant_expression
6177                   (parser,
6178                    "a cast to a type other than an integral or "
6179                    "enumeration type")))
6180             return error_mark_node;
6181
6182           /* Perform the cast.  */
6183           expr = build_c_cast (input_location, type, expr);
6184           return expr;
6185         }
6186       else 
6187         cp_parser_abort_tentative_parse (parser);
6188     }
6189
6190   /* If we get here, then it's not a cast, so it must be a
6191      unary-expression.  */
6192   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6193 }
6194
6195 /* Parse a binary expression of the general form:
6196
6197    pm-expression:
6198      cast-expression
6199      pm-expression .* cast-expression
6200      pm-expression ->* cast-expression
6201
6202    multiplicative-expression:
6203      pm-expression
6204      multiplicative-expression * pm-expression
6205      multiplicative-expression / pm-expression
6206      multiplicative-expression % pm-expression
6207
6208    additive-expression:
6209      multiplicative-expression
6210      additive-expression + multiplicative-expression
6211      additive-expression - multiplicative-expression
6212
6213    shift-expression:
6214      additive-expression
6215      shift-expression << additive-expression
6216      shift-expression >> additive-expression
6217
6218    relational-expression:
6219      shift-expression
6220      relational-expression < shift-expression
6221      relational-expression > shift-expression
6222      relational-expression <= shift-expression
6223      relational-expression >= shift-expression
6224
6225   GNU Extension:
6226
6227    relational-expression:
6228      relational-expression <? shift-expression
6229      relational-expression >? shift-expression
6230
6231    equality-expression:
6232      relational-expression
6233      equality-expression == relational-expression
6234      equality-expression != relational-expression
6235
6236    and-expression:
6237      equality-expression
6238      and-expression & equality-expression
6239
6240    exclusive-or-expression:
6241      and-expression
6242      exclusive-or-expression ^ and-expression
6243
6244    inclusive-or-expression:
6245      exclusive-or-expression
6246      inclusive-or-expression | exclusive-or-expression
6247
6248    logical-and-expression:
6249      inclusive-or-expression
6250      logical-and-expression && inclusive-or-expression
6251
6252    logical-or-expression:
6253      logical-and-expression
6254      logical-or-expression || logical-and-expression
6255
6256    All these are implemented with a single function like:
6257
6258    binary-expression:
6259      simple-cast-expression
6260      binary-expression <token> binary-expression
6261
6262    CAST_P is true if this expression is the target of a cast.
6263
6264    The binops_by_token map is used to get the tree codes for each <token> type.
6265    binary-expressions are associated according to a precedence table.  */
6266
6267 #define TOKEN_PRECEDENCE(token)                              \
6268 (((token->type == CPP_GREATER                                \
6269    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6270   && !parser->greater_than_is_operator_p)                    \
6271  ? PREC_NOT_OPERATOR                                         \
6272  : binops_by_token[token->type].prec)
6273
6274 static tree
6275 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6276                              bool no_toplevel_fold_p,
6277                              enum cp_parser_prec prec,
6278                              cp_id_kind * pidk)
6279 {
6280   cp_parser_expression_stack stack;
6281   cp_parser_expression_stack_entry *sp = &stack[0];
6282   tree lhs, rhs;
6283   cp_token *token;
6284   enum tree_code tree_type, lhs_type, rhs_type;
6285   enum cp_parser_prec new_prec, lookahead_prec;
6286   bool overloaded_p;
6287
6288   /* Parse the first expression.  */
6289   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6290   lhs_type = ERROR_MARK;
6291
6292   for (;;)
6293     {
6294       /* Get an operator token.  */
6295       token = cp_lexer_peek_token (parser->lexer);
6296
6297       if (warn_cxx0x_compat
6298           && token->type == CPP_RSHIFT
6299           && !parser->greater_than_is_operator_p)
6300         {
6301           if (warning_at (token->location, OPT_Wc__0x_compat, 
6302                           "%<>>%> operator will be treated as"
6303                           " two right angle brackets in C++0x"))
6304             inform (token->location,
6305                     "suggest parentheses around %<>>%> expression");
6306         }
6307
6308       new_prec = TOKEN_PRECEDENCE (token);
6309
6310       /* Popping an entry off the stack means we completed a subexpression:
6311          - either we found a token which is not an operator (`>' where it is not
6312            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6313            will happen repeatedly;
6314          - or, we found an operator which has lower priority.  This is the case
6315            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6316            parsing `3 * 4'.  */
6317       if (new_prec <= prec)
6318         {
6319           if (sp == stack)
6320             break;
6321           else
6322             goto pop;
6323         }
6324
6325      get_rhs:
6326       tree_type = binops_by_token[token->type].tree_type;
6327
6328       /* We used the operator token.  */
6329       cp_lexer_consume_token (parser->lexer);
6330
6331       /* For "false && x" or "true || x", x will never be executed;
6332          disable warnings while evaluating it.  */
6333       if (tree_type == TRUTH_ANDIF_EXPR)
6334         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6335       else if (tree_type == TRUTH_ORIF_EXPR)
6336         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6337
6338       /* Extract another operand.  It may be the RHS of this expression
6339          or the LHS of a new, higher priority expression.  */
6340       rhs = cp_parser_simple_cast_expression (parser);
6341       rhs_type = ERROR_MARK;
6342
6343       /* Get another operator token.  Look up its precedence to avoid
6344          building a useless (immediately popped) stack entry for common
6345          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6346       token = cp_lexer_peek_token (parser->lexer);
6347       lookahead_prec = TOKEN_PRECEDENCE (token);
6348       if (lookahead_prec > new_prec)
6349         {
6350           /* ... and prepare to parse the RHS of the new, higher priority
6351              expression.  Since precedence levels on the stack are
6352              monotonically increasing, we do not have to care about
6353              stack overflows.  */
6354           sp->prec = prec;
6355           sp->tree_type = tree_type;
6356           sp->lhs = lhs;
6357           sp->lhs_type = lhs_type;
6358           sp++;
6359           lhs = rhs;
6360           lhs_type = rhs_type;
6361           prec = new_prec;
6362           new_prec = lookahead_prec;
6363           goto get_rhs;
6364
6365          pop:
6366           lookahead_prec = new_prec;
6367           /* If the stack is not empty, we have parsed into LHS the right side
6368              (`4' in the example above) of an expression we had suspended.
6369              We can use the information on the stack to recover the LHS (`3')
6370              from the stack together with the tree code (`MULT_EXPR'), and
6371              the precedence of the higher level subexpression
6372              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6373              which will be used to actually build the additive expression.  */
6374           --sp;
6375           prec = sp->prec;
6376           tree_type = sp->tree_type;
6377           rhs = lhs;
6378           rhs_type = lhs_type;
6379           lhs = sp->lhs;
6380           lhs_type = sp->lhs_type;
6381         }
6382
6383       /* Undo the disabling of warnings done above.  */
6384       if (tree_type == TRUTH_ANDIF_EXPR)
6385         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6386       else if (tree_type == TRUTH_ORIF_EXPR)
6387         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6388
6389       overloaded_p = false;
6390       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6391          ERROR_MARK for everything that is not a binary expression.
6392          This makes warn_about_parentheses miss some warnings that
6393          involve unary operators.  For unary expressions we should
6394          pass the correct tree_code unless the unary expression was
6395          surrounded by parentheses.
6396       */
6397       if (no_toplevel_fold_p
6398           && lookahead_prec <= prec
6399           && sp == stack
6400           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6401         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6402       else
6403         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6404                                  &overloaded_p, tf_warning_or_error);
6405       lhs_type = tree_type;
6406
6407       /* If the binary operator required the use of an overloaded operator,
6408          then this expression cannot be an integral constant-expression.
6409          An overloaded operator can be used even if both operands are
6410          otherwise permissible in an integral constant-expression if at
6411          least one of the operands is of enumeration type.  */
6412
6413       if (overloaded_p
6414           && (cp_parser_non_integral_constant_expression
6415               (parser, "calls to overloaded operators")))
6416         return error_mark_node;
6417     }
6418
6419   return lhs;
6420 }
6421
6422
6423 /* Parse the `? expression : assignment-expression' part of a
6424    conditional-expression.  The LOGICAL_OR_EXPR is the
6425    logical-or-expression that started the conditional-expression.
6426    Returns a representation of the entire conditional-expression.
6427
6428    This routine is used by cp_parser_assignment_expression.
6429
6430      ? expression : assignment-expression
6431
6432    GNU Extensions:
6433
6434      ? : assignment-expression */
6435
6436 static tree
6437 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6438 {
6439   tree expr;
6440   tree assignment_expr;
6441
6442   /* Consume the `?' token.  */
6443   cp_lexer_consume_token (parser->lexer);
6444   if (cp_parser_allow_gnu_extensions_p (parser)
6445       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6446     {
6447       /* Implicit true clause.  */
6448       expr = NULL_TREE;
6449       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6450     }
6451   else
6452     {
6453       /* Parse the expression.  */
6454       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6455       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6456       c_inhibit_evaluation_warnings +=
6457         ((logical_or_expr == truthvalue_true_node)
6458          - (logical_or_expr == truthvalue_false_node));
6459     }
6460
6461   /* The next token should be a `:'.  */
6462   cp_parser_require (parser, CPP_COLON, "%<:%>");
6463   /* Parse the assignment-expression.  */
6464   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6465   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6466
6467   /* Build the conditional-expression.  */
6468   return build_x_conditional_expr (logical_or_expr,
6469                                    expr,
6470                                    assignment_expr,
6471                                    tf_warning_or_error);
6472 }
6473
6474 /* Parse an assignment-expression.
6475
6476    assignment-expression:
6477      conditional-expression
6478      logical-or-expression assignment-operator assignment_expression
6479      throw-expression
6480
6481    CAST_P is true if this expression is the target of a cast.
6482
6483    Returns a representation for the expression.  */
6484
6485 static tree
6486 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6487                                  cp_id_kind * pidk)
6488 {
6489   tree expr;
6490
6491   /* If the next token is the `throw' keyword, then we're looking at
6492      a throw-expression.  */
6493   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6494     expr = cp_parser_throw_expression (parser);
6495   /* Otherwise, it must be that we are looking at a
6496      logical-or-expression.  */
6497   else
6498     {
6499       /* Parse the binary expressions (logical-or-expression).  */
6500       expr = cp_parser_binary_expression (parser, cast_p, false,
6501                                           PREC_NOT_OPERATOR, pidk);
6502       /* If the next token is a `?' then we're actually looking at a
6503          conditional-expression.  */
6504       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6505         return cp_parser_question_colon_clause (parser, expr);
6506       else
6507         {
6508           enum tree_code assignment_operator;
6509
6510           /* If it's an assignment-operator, we're using the second
6511              production.  */
6512           assignment_operator
6513             = cp_parser_assignment_operator_opt (parser);
6514           if (assignment_operator != ERROR_MARK)
6515             {
6516               bool non_constant_p;
6517
6518               /* Parse the right-hand side of the assignment.  */
6519               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6520
6521               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6522                 maybe_warn_cpp0x ("extended initializer lists");
6523
6524               /* An assignment may not appear in a
6525                  constant-expression.  */
6526               if (cp_parser_non_integral_constant_expression (parser,
6527                                                               "an assignment"))
6528                 return error_mark_node;
6529               /* Build the assignment expression.  */
6530               expr = build_x_modify_expr (expr,
6531                                           assignment_operator,
6532                                           rhs,
6533                                           tf_warning_or_error);
6534             }
6535         }
6536     }
6537
6538   return expr;
6539 }
6540
6541 /* Parse an (optional) assignment-operator.
6542
6543    assignment-operator: one of
6544      = *= /= %= += -= >>= <<= &= ^= |=
6545
6546    GNU Extension:
6547
6548    assignment-operator: one of
6549      <?= >?=
6550
6551    If the next token is an assignment operator, the corresponding tree
6552    code is returned, and the token is consumed.  For example, for
6553    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6554    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6555    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6556    operator, ERROR_MARK is returned.  */
6557
6558 static enum tree_code
6559 cp_parser_assignment_operator_opt (cp_parser* parser)
6560 {
6561   enum tree_code op;
6562   cp_token *token;
6563
6564   /* Peek at the next token.  */
6565   token = cp_lexer_peek_token (parser->lexer);
6566
6567   switch (token->type)
6568     {
6569     case CPP_EQ:
6570       op = NOP_EXPR;
6571       break;
6572
6573     case CPP_MULT_EQ:
6574       op = MULT_EXPR;
6575       break;
6576
6577     case CPP_DIV_EQ:
6578       op = TRUNC_DIV_EXPR;
6579       break;
6580
6581     case CPP_MOD_EQ:
6582       op = TRUNC_MOD_EXPR;
6583       break;
6584
6585     case CPP_PLUS_EQ:
6586       op = PLUS_EXPR;
6587       break;
6588
6589     case CPP_MINUS_EQ:
6590       op = MINUS_EXPR;
6591       break;
6592
6593     case CPP_RSHIFT_EQ:
6594       op = RSHIFT_EXPR;
6595       break;
6596
6597     case CPP_LSHIFT_EQ:
6598       op = LSHIFT_EXPR;
6599       break;
6600
6601     case CPP_AND_EQ:
6602       op = BIT_AND_EXPR;
6603       break;
6604
6605     case CPP_XOR_EQ:
6606       op = BIT_XOR_EXPR;
6607       break;
6608
6609     case CPP_OR_EQ:
6610       op = BIT_IOR_EXPR;
6611       break;
6612
6613     default:
6614       /* Nothing else is an assignment operator.  */
6615       op = ERROR_MARK;
6616     }
6617
6618   /* If it was an assignment operator, consume it.  */
6619   if (op != ERROR_MARK)
6620     cp_lexer_consume_token (parser->lexer);
6621
6622   return op;
6623 }
6624
6625 /* Parse an expression.
6626
6627    expression:
6628      assignment-expression
6629      expression , assignment-expression
6630
6631    CAST_P is true if this expression is the target of a cast.
6632
6633    Returns a representation of the expression.  */
6634
6635 static tree
6636 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6637 {
6638   tree expression = NULL_TREE;
6639
6640   while (true)
6641     {
6642       tree assignment_expression;
6643
6644       /* Parse the next assignment-expression.  */
6645       assignment_expression
6646         = cp_parser_assignment_expression (parser, cast_p, pidk);
6647       /* If this is the first assignment-expression, we can just
6648          save it away.  */
6649       if (!expression)
6650         expression = assignment_expression;
6651       else
6652         expression = build_x_compound_expr (expression,
6653                                             assignment_expression,
6654                                             tf_warning_or_error);
6655       /* If the next token is not a comma, then we are done with the
6656          expression.  */
6657       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6658         break;
6659       /* Consume the `,'.  */
6660       cp_lexer_consume_token (parser->lexer);
6661       /* A comma operator cannot appear in a constant-expression.  */
6662       if (cp_parser_non_integral_constant_expression (parser,
6663                                                       "a comma operator"))
6664         expression = error_mark_node;
6665     }
6666
6667   return expression;
6668 }
6669
6670 /* Parse a constant-expression.
6671
6672    constant-expression:
6673      conditional-expression
6674
6675   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6676   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6677   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6678   is false, NON_CONSTANT_P should be NULL.  */
6679
6680 static tree
6681 cp_parser_constant_expression (cp_parser* parser,
6682                                bool allow_non_constant_p,
6683                                bool *non_constant_p)
6684 {
6685   bool saved_integral_constant_expression_p;
6686   bool saved_allow_non_integral_constant_expression_p;
6687   bool saved_non_integral_constant_expression_p;
6688   tree expression;
6689
6690   /* It might seem that we could simply parse the
6691      conditional-expression, and then check to see if it were
6692      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6693      one that the compiler can figure out is constant, possibly after
6694      doing some simplifications or optimizations.  The standard has a
6695      precise definition of constant-expression, and we must honor
6696      that, even though it is somewhat more restrictive.
6697
6698      For example:
6699
6700        int i[(2, 3)];
6701
6702      is not a legal declaration, because `(2, 3)' is not a
6703      constant-expression.  The `,' operator is forbidden in a
6704      constant-expression.  However, GCC's constant-folding machinery
6705      will fold this operation to an INTEGER_CST for `3'.  */
6706
6707   /* Save the old settings.  */
6708   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6709   saved_allow_non_integral_constant_expression_p
6710     = parser->allow_non_integral_constant_expression_p;
6711   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6712   /* We are now parsing a constant-expression.  */
6713   parser->integral_constant_expression_p = true;
6714   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6715   parser->non_integral_constant_expression_p = false;
6716   /* Although the grammar says "conditional-expression", we parse an
6717      "assignment-expression", which also permits "throw-expression"
6718      and the use of assignment operators.  In the case that
6719      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6720      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6721      actually essential that we look for an assignment-expression.
6722      For example, cp_parser_initializer_clauses uses this function to
6723      determine whether a particular assignment-expression is in fact
6724      constant.  */
6725   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6726   /* Restore the old settings.  */
6727   parser->integral_constant_expression_p
6728     = saved_integral_constant_expression_p;
6729   parser->allow_non_integral_constant_expression_p
6730     = saved_allow_non_integral_constant_expression_p;
6731   if (allow_non_constant_p)
6732     *non_constant_p = parser->non_integral_constant_expression_p;
6733   else if (parser->non_integral_constant_expression_p)
6734     expression = error_mark_node;
6735   parser->non_integral_constant_expression_p
6736     = saved_non_integral_constant_expression_p;
6737
6738   return expression;
6739 }
6740
6741 /* Parse __builtin_offsetof.
6742
6743    offsetof-expression:
6744      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6745
6746    offsetof-member-designator:
6747      id-expression
6748      | offsetof-member-designator "." id-expression
6749      | offsetof-member-designator "[" expression "]"
6750      | offsetof-member-designator "->" id-expression  */
6751
6752 static tree
6753 cp_parser_builtin_offsetof (cp_parser *parser)
6754 {
6755   int save_ice_p, save_non_ice_p;
6756   tree type, expr;
6757   cp_id_kind dummy;
6758   cp_token *token;
6759
6760   /* We're about to accept non-integral-constant things, but will
6761      definitely yield an integral constant expression.  Save and
6762      restore these values around our local parsing.  */
6763   save_ice_p = parser->integral_constant_expression_p;
6764   save_non_ice_p = parser->non_integral_constant_expression_p;
6765
6766   /* Consume the "__builtin_offsetof" token.  */
6767   cp_lexer_consume_token (parser->lexer);
6768   /* Consume the opening `('.  */
6769   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6770   /* Parse the type-id.  */
6771   type = cp_parser_type_id (parser);
6772   /* Look for the `,'.  */
6773   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6774   token = cp_lexer_peek_token (parser->lexer);
6775
6776   /* Build the (type *)null that begins the traditional offsetof macro.  */
6777   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6778                             tf_warning_or_error);
6779
6780   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6781   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6782                                                  true, &dummy, token->location);
6783   while (true)
6784     {
6785       token = cp_lexer_peek_token (parser->lexer);
6786       switch (token->type)
6787         {
6788         case CPP_OPEN_SQUARE:
6789           /* offsetof-member-designator "[" expression "]" */
6790           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6791           break;
6792
6793         case CPP_DEREF:
6794           /* offsetof-member-designator "->" identifier */
6795           expr = grok_array_decl (expr, integer_zero_node);
6796           /* FALLTHRU */
6797
6798         case CPP_DOT:
6799           /* offsetof-member-designator "." identifier */
6800           cp_lexer_consume_token (parser->lexer);
6801           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6802                                                          expr, true, &dummy,
6803                                                          token->location);
6804           break;
6805
6806         case CPP_CLOSE_PAREN:
6807           /* Consume the ")" token.  */
6808           cp_lexer_consume_token (parser->lexer);
6809           goto success;
6810
6811         default:
6812           /* Error.  We know the following require will fail, but
6813              that gives the proper error message.  */
6814           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6815           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6816           expr = error_mark_node;
6817           goto failure;
6818         }
6819     }
6820
6821  success:
6822   /* If we're processing a template, we can't finish the semantics yet.
6823      Otherwise we can fold the entire expression now.  */
6824   if (processing_template_decl)
6825     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6826   else
6827     expr = finish_offsetof (expr);
6828
6829  failure:
6830   parser->integral_constant_expression_p = save_ice_p;
6831   parser->non_integral_constant_expression_p = save_non_ice_p;
6832
6833   return expr;
6834 }
6835
6836 /* Parse a trait expression.  */
6837
6838 static tree
6839 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6840 {
6841   cp_trait_kind kind;
6842   tree type1, type2 = NULL_TREE;
6843   bool binary = false;
6844   cp_decl_specifier_seq decl_specs;
6845
6846   switch (keyword)
6847     {
6848     case RID_HAS_NOTHROW_ASSIGN:
6849       kind = CPTK_HAS_NOTHROW_ASSIGN;
6850       break;
6851     case RID_HAS_NOTHROW_CONSTRUCTOR:
6852       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6853       break;
6854     case RID_HAS_NOTHROW_COPY:
6855       kind = CPTK_HAS_NOTHROW_COPY;
6856       break;
6857     case RID_HAS_TRIVIAL_ASSIGN:
6858       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6859       break;
6860     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6861       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6862       break;
6863     case RID_HAS_TRIVIAL_COPY:
6864       kind = CPTK_HAS_TRIVIAL_COPY;
6865       break;
6866     case RID_HAS_TRIVIAL_DESTRUCTOR:
6867       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6868       break;
6869     case RID_HAS_VIRTUAL_DESTRUCTOR:
6870       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6871       break;
6872     case RID_IS_ABSTRACT:
6873       kind = CPTK_IS_ABSTRACT;
6874       break;
6875     case RID_IS_BASE_OF:
6876       kind = CPTK_IS_BASE_OF;
6877       binary = true;
6878       break;
6879     case RID_IS_CLASS:
6880       kind = CPTK_IS_CLASS;
6881       break;
6882     case RID_IS_CONVERTIBLE_TO:
6883       kind = CPTK_IS_CONVERTIBLE_TO;
6884       binary = true;
6885       break;
6886     case RID_IS_EMPTY:
6887       kind = CPTK_IS_EMPTY;
6888       break;
6889     case RID_IS_ENUM:
6890       kind = CPTK_IS_ENUM;
6891       break;
6892     case RID_IS_POD:
6893       kind = CPTK_IS_POD;
6894       break;
6895     case RID_IS_POLYMORPHIC:
6896       kind = CPTK_IS_POLYMORPHIC;
6897       break;
6898     case RID_IS_STD_LAYOUT:
6899       kind = CPTK_IS_STD_LAYOUT;
6900       break;
6901     case RID_IS_TRIVIAL:
6902       kind = CPTK_IS_TRIVIAL;
6903       break;
6904     case RID_IS_UNION:
6905       kind = CPTK_IS_UNION;
6906       break;
6907     default:
6908       gcc_unreachable ();
6909     }
6910
6911   /* Consume the token.  */
6912   cp_lexer_consume_token (parser->lexer);
6913
6914   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6915
6916   type1 = cp_parser_type_id (parser);
6917
6918   if (type1 == error_mark_node)
6919     return error_mark_node;
6920
6921   /* Build a trivial decl-specifier-seq.  */
6922   clear_decl_specs (&decl_specs);
6923   decl_specs.type = type1;
6924
6925   /* Call grokdeclarator to figure out what type this is.  */
6926   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6927                           /*initialized=*/0, /*attrlist=*/NULL);
6928
6929   if (binary)
6930     {
6931       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6932  
6933       type2 = cp_parser_type_id (parser);
6934
6935       if (type2 == error_mark_node)
6936         return error_mark_node;
6937
6938       /* Build a trivial decl-specifier-seq.  */
6939       clear_decl_specs (&decl_specs);
6940       decl_specs.type = type2;
6941
6942       /* Call grokdeclarator to figure out what type this is.  */
6943       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6944                               /*initialized=*/0, /*attrlist=*/NULL);
6945     }
6946
6947   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6948
6949   /* Complete the trait expression, which may mean either processing
6950      the trait expr now or saving it for template instantiation.  */
6951   return finish_trait_expr (kind, type1, type2);
6952 }
6953
6954 /* Lambdas that appear in variable initializer or default argument scope
6955    get that in their mangling, so we need to record it.  We might as well
6956    use the count for function and namespace scopes as well.  */
6957 static tree lambda_scope;
6958 static int lambda_count;
6959 typedef struct GTY(()) tree_int
6960 {
6961   tree t;
6962   int i;
6963 } tree_int;
6964 DEF_VEC_O(tree_int);
6965 DEF_VEC_ALLOC_O(tree_int,gc);
6966 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
6967
6968 static void
6969 start_lambda_scope (tree decl)
6970 {
6971   tree_int ti;
6972   gcc_assert (decl);
6973   /* Once we're inside a function, we ignore other scopes and just push
6974      the function again so that popping works properly.  */
6975   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
6976     decl = current_function_decl;
6977   ti.t = lambda_scope;
6978   ti.i = lambda_count;
6979   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
6980   if (lambda_scope != decl)
6981     {
6982       /* Don't reset the count if we're still in the same function.  */
6983       lambda_scope = decl;
6984       lambda_count = 0;
6985     }
6986 }
6987
6988 static void
6989 record_lambda_scope (tree lambda)
6990 {
6991   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
6992   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
6993 }
6994
6995 static void
6996 finish_lambda_scope (void)
6997 {
6998   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
6999   if (lambda_scope != p->t)
7000     {
7001       lambda_scope = p->t;
7002       lambda_count = p->i;
7003     }
7004   VEC_pop (tree_int, lambda_scope_stack);
7005 }
7006
7007 /* We want to determine the linkage of a lambda type at pushtag time,
7008    before CLASSTYPE_LAMBDA_EXPR has been set.  So this callback allows us
7009    to find out whether the current lambda mangling scope will give us
7010    linkage or not.  */
7011
7012 bool
7013 no_linkage_lambda_type_p (tree type)
7014 {
7015   tree lambda, scope;
7016   if (!LAMBDA_TYPE_P (type))
7017     return false;
7018
7019   lambda = CLASSTYPE_LAMBDA_EXPR (type);
7020   if (lambda)
7021     scope = LAMBDA_EXPR_EXTRA_SCOPE (lambda);
7022   else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
7023     /* We can't use lambda_scope, and CLASSTYPE_TEMPLATE_INFO won't be set
7024        yet either, so guess it's public for now.  */
7025     return false;
7026   else
7027     scope = lambda_scope;
7028
7029   return (scope == NULL_TREE);
7030 }
7031
7032 /* Parse a lambda expression.
7033
7034    lambda-expression:
7035      lambda-introducer lambda-declarator [opt] compound-statement
7036
7037    Returns a representation of the expression.  */
7038
7039 static tree
7040 cp_parser_lambda_expression (cp_parser* parser)
7041 {
7042   tree lambda_expr = build_lambda_expr ();
7043   tree type;
7044
7045   LAMBDA_EXPR_LOCATION (lambda_expr)
7046     = cp_lexer_peek_token (parser->lexer)->location;
7047
7048   /* We may be in the middle of deferred access check.  Disable
7049      it now.  */
7050   push_deferring_access_checks (dk_no_deferred);
7051
7052   type = begin_lambda_type (lambda_expr);
7053
7054   record_lambda_scope (lambda_expr);
7055
7056   {
7057     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7058     unsigned int saved_num_template_parameter_lists
7059         = parser->num_template_parameter_lists;
7060
7061     parser->num_template_parameter_lists = 0;
7062
7063     cp_parser_lambda_introducer (parser, lambda_expr);
7064
7065     /* By virtue of defining a local class, a lambda expression has access to
7066        the private variables of enclosing classes.  */
7067
7068     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7069
7070     cp_parser_lambda_body (parser, lambda_expr);
7071
7072     /* The capture list was built up in reverse order; fix that now.  */
7073     {
7074       tree newlist = NULL_TREE;
7075       tree elt, next;
7076
7077       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7078            elt; elt = next)
7079         {
7080           tree field = TREE_PURPOSE (elt);
7081           char *buf;
7082
7083           next = TREE_CHAIN (elt);
7084           TREE_CHAIN (elt) = newlist;
7085           newlist = elt;
7086
7087           /* Also add __ to the beginning of the field name so that code
7088              outside the lambda body can't see the captured name.  We could
7089              just remove the name entirely, but this is more useful for
7090              debugging.  */
7091           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7092             /* The 'this' capture already starts with __.  */
7093             continue;
7094
7095           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7096           buf[1] = buf[0] = '_';
7097           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7098                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7099           DECL_NAME (field) = get_identifier (buf);
7100         }
7101       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7102     }
7103
7104     type = finish_struct (type, /*attributes=*/NULL_TREE);
7105
7106     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7107   }
7108
7109   pop_deferring_access_checks ();
7110
7111   return build_lambda_object (lambda_expr);
7112 }
7113
7114 /* Parse the beginning of a lambda expression.
7115
7116    lambda-introducer:
7117      [ lambda-capture [opt] ]
7118
7119    LAMBDA_EXPR is the current representation of the lambda expression.  */
7120
7121 static void
7122 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7123 {
7124   /* Need commas after the first capture.  */
7125   bool first = true;
7126
7127   /* Eat the leading `['.  */
7128   cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7129
7130   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7131   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7132       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7133     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7134   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7135     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7136
7137   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7138     {
7139       cp_lexer_consume_token (parser->lexer);
7140       first = false;
7141     }
7142
7143   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7144     {
7145       cp_token* capture_token;
7146       tree capture_id;
7147       tree capture_init_expr;
7148       cp_id_kind idk = CP_ID_KIND_NONE;
7149
7150       enum capture_kind_type
7151       {
7152         BY_COPY,
7153         BY_REFERENCE
7154       };
7155       enum capture_kind_type capture_kind = BY_COPY;
7156
7157       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7158         {
7159           error ("expected end of capture-list");
7160           return;
7161         }
7162
7163       if (first)
7164         first = false;
7165       else
7166         cp_parser_require (parser, CPP_COMMA, "%<,%>");
7167
7168       /* Possibly capture `this'.  */
7169       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7170         {
7171           cp_lexer_consume_token (parser->lexer);
7172           add_capture (lambda_expr,
7173                        /*id=*/get_identifier ("__this"),
7174                        /*initializer=*/finish_this_expr(),
7175                        /*by_reference_p=*/false);
7176           continue;
7177         }
7178
7179       /* Remember whether we want to capture as a reference or not.  */
7180       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7181         {
7182           capture_kind = BY_REFERENCE;
7183           cp_lexer_consume_token (parser->lexer);
7184         }
7185
7186       /* Get the identifier.  */
7187       capture_token = cp_lexer_peek_token (parser->lexer);
7188       capture_id = cp_parser_identifier (parser);
7189
7190       if (capture_id == error_mark_node)
7191         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7192            delimiters, but I modified this to stop on unnested ']' as well.  It
7193            was already changed to stop on unnested '}', so the
7194            "closing_parenthesis" name is no more misleading with my change.  */
7195         {
7196           cp_parser_skip_to_closing_parenthesis (parser,
7197                                                  /*recovering=*/true,
7198                                                  /*or_comma=*/true,
7199                                                  /*consume_paren=*/true);
7200           continue;
7201         }
7202
7203       /* Find the initializer for this capture.  */
7204       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7205         {
7206           /* An explicit expression exists.  */
7207           cp_lexer_consume_token (parser->lexer);
7208           pedwarn (input_location, OPT_pedantic,
7209                    "ISO C++ does not allow initializers "
7210                    "in lambda expression capture lists");
7211           capture_init_expr = cp_parser_assignment_expression (parser,
7212                                                                /*cast_p=*/true,
7213                                                                &idk);
7214         }
7215       else
7216         {
7217           const char* error_msg;
7218
7219           /* Turn the identifier into an id-expression.  */
7220           capture_init_expr
7221             = cp_parser_lookup_name
7222                 (parser,
7223                  capture_id,
7224                  none_type,
7225                  /*is_template=*/false,
7226                  /*is_namespace=*/false,
7227                  /*check_dependency=*/true,
7228                  /*ambiguous_decls=*/NULL,
7229                  capture_token->location);
7230
7231           capture_init_expr
7232             = finish_id_expression
7233                 (capture_id,
7234                  capture_init_expr,
7235                  parser->scope,
7236                  &idk,
7237                  /*integral_constant_expression_p=*/false,
7238                  /*allow_non_integral_constant_expression_p=*/false,
7239                  /*non_integral_constant_expression_p=*/NULL,
7240                  /*template_p=*/false,
7241                  /*done=*/true,
7242                  /*address_p=*/false,
7243                  /*template_arg_p=*/false,
7244                  &error_msg,
7245                  capture_token->location);
7246         }
7247
7248       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7249         capture_init_expr
7250           = unqualified_name_lookup_error (capture_init_expr);
7251
7252       add_capture (lambda_expr,
7253                    capture_id,
7254                    capture_init_expr,
7255                    /*by_reference_p=*/capture_kind == BY_REFERENCE);
7256     }
7257
7258   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7259 }
7260
7261 /* Parse the (optional) middle of a lambda expression.
7262
7263    lambda-declarator:
7264      ( parameter-declaration-clause [opt] )
7265        attribute-specifier [opt]
7266        mutable [opt]
7267        exception-specification [opt]
7268        lambda-return-type-clause [opt]
7269
7270    LAMBDA_EXPR is the current representation of the lambda expression.  */
7271
7272 static void
7273 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7274 {
7275   /* 5.1.1.4 of the standard says:
7276        If a lambda-expression does not include a lambda-declarator, it is as if
7277        the lambda-declarator were ().
7278      This means an empty parameter list, no attributes, and no exception
7279      specification.  */
7280   tree param_list = void_list_node;
7281   tree attributes = NULL_TREE;
7282   tree exception_spec = NULL_TREE;
7283   tree t;
7284
7285   /* The lambda-declarator is optional, but must begin with an opening
7286      parenthesis if present.  */
7287   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7288     {
7289       cp_lexer_consume_token (parser->lexer);
7290
7291       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7292
7293       /* Parse parameters.  */
7294       param_list = cp_parser_parameter_declaration_clause (parser);
7295
7296       /* Default arguments shall not be specified in the
7297          parameter-declaration-clause of a lambda-declarator.  */
7298       for (t = param_list; t; t = TREE_CHAIN (t))
7299         if (TREE_PURPOSE (t))
7300           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7301                    "default argument specified for lambda parameter");
7302
7303       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7304
7305       attributes = cp_parser_attributes_opt (parser);
7306
7307       /* Parse optional `mutable' keyword.  */
7308       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7309         {
7310           cp_lexer_consume_token (parser->lexer);
7311           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7312         }
7313
7314       /* Parse optional exception specification.  */
7315       exception_spec = cp_parser_exception_specification_opt (parser);
7316
7317       /* Parse optional trailing return type.  */
7318       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7319         {
7320           cp_lexer_consume_token (parser->lexer);
7321           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7322         }
7323
7324       /* The function parameters must be in scope all the way until after the
7325          trailing-return-type in case of decltype.  */
7326       for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7327         pop_binding (DECL_NAME (t), t);
7328
7329       leave_scope ();
7330     }
7331
7332   /* Create the function call operator.
7333
7334      Messing with declarators like this is no uglier than building up the
7335      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7336      other code.  */
7337   {
7338     cp_decl_specifier_seq return_type_specs;
7339     cp_declarator* declarator;
7340     tree fco;
7341     int quals;
7342     void *p;
7343
7344     clear_decl_specs (&return_type_specs);
7345     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7346       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7347     else
7348       /* Maybe we will deduce the return type later, but we can use void
7349          as a placeholder return type anyways.  */
7350       return_type_specs.type = void_type_node;
7351
7352     p = obstack_alloc (&declarator_obstack, 0);
7353
7354     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7355                                      sfk_none);
7356
7357     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7358              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7359     declarator = make_call_declarator (declarator, param_list, quals,
7360                                        exception_spec,
7361                                        /*late_return_type=*/NULL_TREE);
7362
7363     fco = grokmethod (&return_type_specs,
7364                         declarator,
7365                         attributes);
7366     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7367     DECL_ARTIFICIAL (fco) = 1;
7368
7369     finish_member_declaration (fco);
7370
7371     obstack_free (&declarator_obstack, p);
7372   }
7373 }
7374
7375 /* Parse the body of a lambda expression, which is simply
7376
7377    compound-statement
7378
7379    but which requires special handling.
7380    LAMBDA_EXPR is the current representation of the lambda expression.  */
7381
7382 static void
7383 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7384 {
7385   bool nested = (current_function_decl != NULL_TREE);
7386   if (nested)
7387     push_function_context ();
7388
7389   /* Finish the function call operator
7390      - class_specifier
7391      + late_parsing_for_member
7392      + function_definition_after_declarator
7393      + ctor_initializer_opt_and_function_body  */
7394   {
7395     tree fco = lambda_function (lambda_expr);
7396     tree body;
7397     bool done = false;
7398
7399     /* Let the front end know that we are going to be defining this
7400        function.  */
7401     start_preparsed_function (fco,
7402                               NULL_TREE,
7403                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7404
7405     start_lambda_scope (fco);
7406     body = begin_function_body ();
7407
7408     /* 5.1.1.4 of the standard says:
7409          If a lambda-expression does not include a trailing-return-type, it
7410          is as if the trailing-return-type denotes the following type:
7411           * if the compound-statement is of the form
7412                { return attribute-specifier [opt] expression ; }
7413              the type of the returned expression after lvalue-to-rvalue
7414              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7415              (_conv.array_ 4.2), and function-to-pointer conversion
7416              (_conv.func_ 4.3);
7417           * otherwise, void.  */
7418
7419     /* In a lambda that has neither a lambda-return-type-clause
7420        nor a deducible form, errors should be reported for return statements
7421        in the body.  Since we used void as the placeholder return type, parsing
7422        the body as usual will give such desired behavior.  */
7423     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7424         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7425         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7426         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7427       {
7428         tree compound_stmt;
7429         tree expr = NULL_TREE;
7430         cp_id_kind idk = CP_ID_KIND_NONE;
7431
7432         /* Parse tentatively in case there's more after the initial return
7433            statement.  */
7434         cp_parser_parse_tentatively (parser);
7435
7436         cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7437         cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7438
7439         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7440
7441         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7442         cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7443
7444         if (cp_parser_parse_definitely (parser))
7445           {
7446             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7447
7448             compound_stmt = begin_compound_stmt (0);
7449             /* Will get error here if type not deduced yet.  */
7450             finish_return_stmt (expr);
7451             finish_compound_stmt (compound_stmt);
7452
7453             done = true;
7454           }
7455       }
7456
7457     if (!done)
7458       {
7459         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7460           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7461         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7462            cp_parser_compound_stmt does not pass it.  */
7463         cp_parser_function_body (parser);
7464         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7465       }
7466
7467     finish_function_body (body);
7468     finish_lambda_scope ();
7469
7470     /* Finish the function and generate code for it if necessary.  */
7471     expand_or_defer_fn (finish_function (/*inline*/2));
7472   }
7473
7474   if (nested)
7475     pop_function_context();
7476 }
7477
7478 /* Statements [gram.stmt.stmt]  */
7479
7480 /* Parse a statement.
7481
7482    statement:
7483      labeled-statement
7484      expression-statement
7485      compound-statement
7486      selection-statement
7487      iteration-statement
7488      jump-statement
7489      declaration-statement
7490      try-block
7491
7492   IN_COMPOUND is true when the statement is nested inside a
7493   cp_parser_compound_statement; this matters for certain pragmas.
7494
7495   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7496   is a (possibly labeled) if statement which is not enclosed in braces
7497   and has an else clause.  This is used to implement -Wparentheses.  */
7498
7499 static void
7500 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7501                      bool in_compound, bool *if_p)
7502 {
7503   tree statement;
7504   cp_token *token;
7505   location_t statement_location;
7506
7507  restart:
7508   if (if_p != NULL)
7509     *if_p = false;
7510   /* There is no statement yet.  */
7511   statement = NULL_TREE;
7512   /* Peek at the next token.  */
7513   token = cp_lexer_peek_token (parser->lexer);
7514   /* Remember the location of the first token in the statement.  */
7515   statement_location = token->location;
7516   /* If this is a keyword, then that will often determine what kind of
7517      statement we have.  */
7518   if (token->type == CPP_KEYWORD)
7519     {
7520       enum rid keyword = token->keyword;
7521
7522       switch (keyword)
7523         {
7524         case RID_CASE:
7525         case RID_DEFAULT:
7526           /* Looks like a labeled-statement with a case label.
7527              Parse the label, and then use tail recursion to parse
7528              the statement.  */
7529           cp_parser_label_for_labeled_statement (parser);
7530           goto restart;
7531
7532         case RID_IF:
7533         case RID_SWITCH:
7534           statement = cp_parser_selection_statement (parser, if_p);
7535           break;
7536
7537         case RID_WHILE:
7538         case RID_DO:
7539         case RID_FOR:
7540           statement = cp_parser_iteration_statement (parser);
7541           break;
7542
7543         case RID_BREAK:
7544         case RID_CONTINUE:
7545         case RID_RETURN:
7546         case RID_GOTO:
7547           statement = cp_parser_jump_statement (parser);
7548           break;
7549
7550           /* Objective-C++ exception-handling constructs.  */
7551         case RID_AT_TRY:
7552         case RID_AT_CATCH:
7553         case RID_AT_FINALLY:
7554         case RID_AT_SYNCHRONIZED:
7555         case RID_AT_THROW:
7556           statement = cp_parser_objc_statement (parser);
7557           break;
7558
7559         case RID_TRY:
7560           statement = cp_parser_try_block (parser);
7561           break;
7562
7563         case RID_NAMESPACE:
7564           /* This must be a namespace alias definition.  */
7565           cp_parser_declaration_statement (parser);
7566           return;
7567           
7568         default:
7569           /* It might be a keyword like `int' that can start a
7570              declaration-statement.  */
7571           break;
7572         }
7573     }
7574   else if (token->type == CPP_NAME)
7575     {
7576       /* If the next token is a `:', then we are looking at a
7577          labeled-statement.  */
7578       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7579       if (token->type == CPP_COLON)
7580         {
7581           /* Looks like a labeled-statement with an ordinary label.
7582              Parse the label, and then use tail recursion to parse
7583              the statement.  */
7584           cp_parser_label_for_labeled_statement (parser);
7585           goto restart;
7586         }
7587     }
7588   /* Anything that starts with a `{' must be a compound-statement.  */
7589   else if (token->type == CPP_OPEN_BRACE)
7590     statement = cp_parser_compound_statement (parser, NULL, false);
7591   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7592      a statement all its own.  */
7593   else if (token->type == CPP_PRAGMA)
7594     {
7595       /* Only certain OpenMP pragmas are attached to statements, and thus
7596          are considered statements themselves.  All others are not.  In
7597          the context of a compound, accept the pragma as a "statement" and
7598          return so that we can check for a close brace.  Otherwise we
7599          require a real statement and must go back and read one.  */
7600       if (in_compound)
7601         cp_parser_pragma (parser, pragma_compound);
7602       else if (!cp_parser_pragma (parser, pragma_stmt))
7603         goto restart;
7604       return;
7605     }
7606   else if (token->type == CPP_EOF)
7607     {
7608       cp_parser_error (parser, "expected statement");
7609       return;
7610     }
7611
7612   /* Everything else must be a declaration-statement or an
7613      expression-statement.  Try for the declaration-statement
7614      first, unless we are looking at a `;', in which case we know that
7615      we have an expression-statement.  */
7616   if (!statement)
7617     {
7618       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7619         {
7620           cp_parser_parse_tentatively (parser);
7621           /* Try to parse the declaration-statement.  */
7622           cp_parser_declaration_statement (parser);
7623           /* If that worked, we're done.  */
7624           if (cp_parser_parse_definitely (parser))
7625             return;
7626         }
7627       /* Look for an expression-statement instead.  */
7628       statement = cp_parser_expression_statement (parser, in_statement_expr);
7629     }
7630
7631   /* Set the line number for the statement.  */
7632   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7633     SET_EXPR_LOCATION (statement, statement_location);
7634 }
7635
7636 /* Parse the label for a labeled-statement, i.e.
7637
7638    identifier :
7639    case constant-expression :
7640    default :
7641
7642    GNU Extension:
7643    case constant-expression ... constant-expression : statement
7644
7645    When a label is parsed without errors, the label is added to the
7646    parse tree by the finish_* functions, so this function doesn't
7647    have to return the label.  */
7648
7649 static void
7650 cp_parser_label_for_labeled_statement (cp_parser* parser)
7651 {
7652   cp_token *token;
7653   tree label = NULL_TREE;
7654
7655   /* The next token should be an identifier.  */
7656   token = cp_lexer_peek_token (parser->lexer);
7657   if (token->type != CPP_NAME
7658       && token->type != CPP_KEYWORD)
7659     {
7660       cp_parser_error (parser, "expected labeled-statement");
7661       return;
7662     }
7663
7664   switch (token->keyword)
7665     {
7666     case RID_CASE:
7667       {
7668         tree expr, expr_hi;
7669         cp_token *ellipsis;
7670
7671         /* Consume the `case' token.  */
7672         cp_lexer_consume_token (parser->lexer);
7673         /* Parse the constant-expression.  */
7674         expr = cp_parser_constant_expression (parser,
7675                                               /*allow_non_constant_p=*/false,
7676                                               NULL);
7677
7678         ellipsis = cp_lexer_peek_token (parser->lexer);
7679         if (ellipsis->type == CPP_ELLIPSIS)
7680           {
7681             /* Consume the `...' token.  */
7682             cp_lexer_consume_token (parser->lexer);
7683             expr_hi =
7684               cp_parser_constant_expression (parser,
7685                                              /*allow_non_constant_p=*/false,
7686                                              NULL);
7687             /* We don't need to emit warnings here, as the common code
7688                will do this for us.  */
7689           }
7690         else
7691           expr_hi = NULL_TREE;
7692
7693         if (parser->in_switch_statement_p)
7694           finish_case_label (token->location, expr, expr_hi);
7695         else
7696           error_at (token->location,
7697                     "case label %qE not within a switch statement",
7698                     expr);
7699       }
7700       break;
7701
7702     case RID_DEFAULT:
7703       /* Consume the `default' token.  */
7704       cp_lexer_consume_token (parser->lexer);
7705
7706       if (parser->in_switch_statement_p)
7707         finish_case_label (token->location, NULL_TREE, NULL_TREE);
7708       else
7709         error_at (token->location, "case label not within a switch statement");
7710       break;
7711
7712     default:
7713       /* Anything else must be an ordinary label.  */
7714       label = finish_label_stmt (cp_parser_identifier (parser));
7715       break;
7716     }
7717
7718   /* Require the `:' token.  */
7719   cp_parser_require (parser, CPP_COLON, "%<:%>");
7720
7721   /* An ordinary label may optionally be followed by attributes.
7722      However, this is only permitted if the attributes are then
7723      followed by a semicolon.  This is because, for backward
7724      compatibility, when parsing
7725        lab: __attribute__ ((unused)) int i;
7726      we want the attribute to attach to "i", not "lab".  */
7727   if (label != NULL_TREE
7728       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7729     {
7730       tree attrs;
7731
7732       cp_parser_parse_tentatively (parser);
7733       attrs = cp_parser_attributes_opt (parser);
7734       if (attrs == NULL_TREE
7735           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7736         cp_parser_abort_tentative_parse (parser);
7737       else if (!cp_parser_parse_definitely (parser))
7738         ;
7739       else
7740         cplus_decl_attributes (&label, attrs, 0);
7741     }
7742 }
7743
7744 /* Parse an expression-statement.
7745
7746    expression-statement:
7747      expression [opt] ;
7748
7749    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7750    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7751    indicates whether this expression-statement is part of an
7752    expression statement.  */
7753
7754 static tree
7755 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7756 {
7757   tree statement = NULL_TREE;
7758
7759   /* If the next token is a ';', then there is no expression
7760      statement.  */
7761   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7762     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7763
7764   /* Consume the final `;'.  */
7765   cp_parser_consume_semicolon_at_end_of_statement (parser);
7766
7767   if (in_statement_expr
7768       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7769     /* This is the final expression statement of a statement
7770        expression.  */
7771     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7772   else if (statement)
7773     statement = finish_expr_stmt (statement);
7774   else
7775     finish_stmt ();
7776
7777   return statement;
7778 }
7779
7780 /* Parse a compound-statement.
7781
7782    compound-statement:
7783      { statement-seq [opt] }
7784
7785    GNU extension:
7786
7787    compound-statement:
7788      { label-declaration-seq [opt] statement-seq [opt] }
7789
7790    label-declaration-seq:
7791      label-declaration
7792      label-declaration-seq label-declaration
7793
7794    Returns a tree representing the statement.  */
7795
7796 static tree
7797 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7798                               bool in_try)
7799 {
7800   tree compound_stmt;
7801
7802   /* Consume the `{'.  */
7803   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7804     return error_mark_node;
7805   /* Begin the compound-statement.  */
7806   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7807   /* If the next keyword is `__label__' we have a label declaration.  */
7808   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7809     cp_parser_label_declaration (parser);
7810   /* Parse an (optional) statement-seq.  */
7811   cp_parser_statement_seq_opt (parser, in_statement_expr);
7812   /* Finish the compound-statement.  */
7813   finish_compound_stmt (compound_stmt);
7814   /* Consume the `}'.  */
7815   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7816
7817   return compound_stmt;
7818 }
7819
7820 /* Parse an (optional) statement-seq.
7821
7822    statement-seq:
7823      statement
7824      statement-seq [opt] statement  */
7825
7826 static void
7827 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7828 {
7829   /* Scan statements until there aren't any more.  */
7830   while (true)
7831     {
7832       cp_token *token = cp_lexer_peek_token (parser->lexer);
7833
7834       /* If we're looking at a `}', then we've run out of statements.  */
7835       if (token->type == CPP_CLOSE_BRACE
7836           || token->type == CPP_EOF
7837           || token->type == CPP_PRAGMA_EOL)
7838         break;
7839       
7840       /* If we are in a compound statement and find 'else' then
7841          something went wrong.  */
7842       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7843         {
7844           if (parser->in_statement & IN_IF_STMT) 
7845             break;
7846           else
7847             {
7848               token = cp_lexer_consume_token (parser->lexer);
7849               error_at (token->location, "%<else%> without a previous %<if%>");
7850             }
7851         }
7852
7853       /* Parse the statement.  */
7854       cp_parser_statement (parser, in_statement_expr, true, NULL);
7855     }
7856 }
7857
7858 /* Parse a selection-statement.
7859
7860    selection-statement:
7861      if ( condition ) statement
7862      if ( condition ) statement else statement
7863      switch ( condition ) statement
7864
7865    Returns the new IF_STMT or SWITCH_STMT.
7866
7867    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7868    is a (possibly labeled) if statement which is not enclosed in
7869    braces and has an else clause.  This is used to implement
7870    -Wparentheses.  */
7871
7872 static tree
7873 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7874 {
7875   cp_token *token;
7876   enum rid keyword;
7877
7878   if (if_p != NULL)
7879     *if_p = false;
7880
7881   /* Peek at the next token.  */
7882   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7883
7884   /* See what kind of keyword it is.  */
7885   keyword = token->keyword;
7886   switch (keyword)
7887     {
7888     case RID_IF:
7889     case RID_SWITCH:
7890       {
7891         tree statement;
7892         tree condition;
7893
7894         /* Look for the `('.  */
7895         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7896           {
7897             cp_parser_skip_to_end_of_statement (parser);
7898             return error_mark_node;
7899           }
7900
7901         /* Begin the selection-statement.  */
7902         if (keyword == RID_IF)
7903           statement = begin_if_stmt ();
7904         else
7905           statement = begin_switch_stmt ();
7906
7907         /* Parse the condition.  */
7908         condition = cp_parser_condition (parser);
7909         /* Look for the `)'.  */
7910         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7911           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7912                                                  /*consume_paren=*/true);
7913
7914         if (keyword == RID_IF)
7915           {
7916             bool nested_if;
7917             unsigned char in_statement;
7918
7919             /* Add the condition.  */
7920             finish_if_stmt_cond (condition, statement);
7921
7922             /* Parse the then-clause.  */
7923             in_statement = parser->in_statement;
7924             parser->in_statement |= IN_IF_STMT;
7925             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7926               {
7927                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7928                 add_stmt (build_empty_stmt (loc));
7929                 cp_lexer_consume_token (parser->lexer);
7930                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7931                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7932                               "empty body in an %<if%> statement");
7933                 nested_if = false;
7934               }
7935             else
7936               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7937             parser->in_statement = in_statement;
7938
7939             finish_then_clause (statement);
7940
7941             /* If the next token is `else', parse the else-clause.  */
7942             if (cp_lexer_next_token_is_keyword (parser->lexer,
7943                                                 RID_ELSE))
7944               {
7945                 /* Consume the `else' keyword.  */
7946                 cp_lexer_consume_token (parser->lexer);
7947                 begin_else_clause (statement);
7948                 /* Parse the else-clause.  */
7949                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7950                   {
7951                     location_t loc;
7952                     loc = cp_lexer_peek_token (parser->lexer)->location;
7953                     warning_at (loc,
7954                                 OPT_Wempty_body, "suggest braces around "
7955                                 "empty body in an %<else%> statement");
7956                     add_stmt (build_empty_stmt (loc));
7957                     cp_lexer_consume_token (parser->lexer);
7958                   }
7959                 else
7960                   cp_parser_implicitly_scoped_statement (parser, NULL);
7961
7962                 finish_else_clause (statement);
7963
7964                 /* If we are currently parsing a then-clause, then
7965                    IF_P will not be NULL.  We set it to true to
7966                    indicate that this if statement has an else clause.
7967                    This may trigger the Wparentheses warning below
7968                    when we get back up to the parent if statement.  */
7969                 if (if_p != NULL)
7970                   *if_p = true;
7971               }
7972             else
7973               {
7974                 /* This if statement does not have an else clause.  If
7975                    NESTED_IF is true, then the then-clause is an if
7976                    statement which does have an else clause.  We warn
7977                    about the potential ambiguity.  */
7978                 if (nested_if)
7979                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
7980                               "suggest explicit braces to avoid ambiguous"
7981                               " %<else%>");
7982               }
7983
7984             /* Now we're all done with the if-statement.  */
7985             finish_if_stmt (statement);
7986           }
7987         else
7988           {
7989             bool in_switch_statement_p;
7990             unsigned char in_statement;
7991
7992             /* Add the condition.  */
7993             finish_switch_cond (condition, statement);
7994
7995             /* Parse the body of the switch-statement.  */
7996             in_switch_statement_p = parser->in_switch_statement_p;
7997             in_statement = parser->in_statement;
7998             parser->in_switch_statement_p = true;
7999             parser->in_statement |= IN_SWITCH_STMT;
8000             cp_parser_implicitly_scoped_statement (parser, NULL);
8001             parser->in_switch_statement_p = in_switch_statement_p;
8002             parser->in_statement = in_statement;
8003
8004             /* Now we're all done with the switch-statement.  */
8005             finish_switch_stmt (statement);
8006           }
8007
8008         return statement;
8009       }
8010       break;
8011
8012     default:
8013       cp_parser_error (parser, "expected selection-statement");
8014       return error_mark_node;
8015     }
8016 }
8017
8018 /* Parse a condition.
8019
8020    condition:
8021      expression
8022      type-specifier-seq declarator = initializer-clause
8023      type-specifier-seq declarator braced-init-list
8024
8025    GNU Extension:
8026
8027    condition:
8028      type-specifier-seq declarator asm-specification [opt]
8029        attributes [opt] = assignment-expression
8030
8031    Returns the expression that should be tested.  */
8032
8033 static tree
8034 cp_parser_condition (cp_parser* parser)
8035 {
8036   cp_decl_specifier_seq type_specifiers;
8037   const char *saved_message;
8038
8039   /* Try the declaration first.  */
8040   cp_parser_parse_tentatively (parser);
8041   /* New types are not allowed in the type-specifier-seq for a
8042      condition.  */
8043   saved_message = parser->type_definition_forbidden_message;
8044   parser->type_definition_forbidden_message
8045     = "types may not be defined in conditions";
8046   /* Parse the type-specifier-seq.  */
8047   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
8048                                 &type_specifiers);
8049   /* Restore the saved message.  */
8050   parser->type_definition_forbidden_message = saved_message;
8051   /* If all is well, we might be looking at a declaration.  */
8052   if (!cp_parser_error_occurred (parser))
8053     {
8054       tree decl;
8055       tree asm_specification;
8056       tree attributes;
8057       cp_declarator *declarator;
8058       tree initializer = NULL_TREE;
8059
8060       /* Parse the declarator.  */
8061       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8062                                          /*ctor_dtor_or_conv_p=*/NULL,
8063                                          /*parenthesized_p=*/NULL,
8064                                          /*member_p=*/false);
8065       /* Parse the attributes.  */
8066       attributes = cp_parser_attributes_opt (parser);
8067       /* Parse the asm-specification.  */
8068       asm_specification = cp_parser_asm_specification_opt (parser);
8069       /* If the next token is not an `=' or '{', then we might still be
8070          looking at an expression.  For example:
8071
8072            if (A(a).x)
8073
8074          looks like a decl-specifier-seq and a declarator -- but then
8075          there is no `=', so this is an expression.  */
8076       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8077           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8078         cp_parser_simulate_error (parser);
8079         
8080       /* If we did see an `=' or '{', then we are looking at a declaration
8081          for sure.  */
8082       if (cp_parser_parse_definitely (parser))
8083         {
8084           tree pushed_scope;
8085           bool non_constant_p;
8086           bool flags = LOOKUP_ONLYCONVERTING;
8087
8088           /* Create the declaration.  */
8089           decl = start_decl (declarator, &type_specifiers,
8090                              /*initialized_p=*/true,
8091                              attributes, /*prefix_attributes=*/NULL_TREE,
8092                              &pushed_scope);
8093
8094           /* Parse the initializer.  */
8095           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8096             {
8097               initializer = cp_parser_braced_list (parser, &non_constant_p);
8098               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8099               flags = 0;
8100             }
8101           else
8102             {
8103               /* Consume the `='.  */
8104               cp_parser_require (parser, CPP_EQ, "%<=%>");
8105               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8106             }
8107           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8108             maybe_warn_cpp0x ("extended initializer lists");
8109
8110           if (!non_constant_p)
8111             initializer = fold_non_dependent_expr (initializer);
8112
8113           /* Process the initializer.  */
8114           cp_finish_decl (decl,
8115                           initializer, !non_constant_p,
8116                           asm_specification,
8117                           flags);
8118
8119           if (pushed_scope)
8120             pop_scope (pushed_scope);
8121
8122           return convert_from_reference (decl);
8123         }
8124     }
8125   /* If we didn't even get past the declarator successfully, we are
8126      definitely not looking at a declaration.  */
8127   else
8128     cp_parser_abort_tentative_parse (parser);
8129
8130   /* Otherwise, we are looking at an expression.  */
8131   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8132 }
8133
8134 /* Parse an iteration-statement.
8135
8136    iteration-statement:
8137      while ( condition ) statement
8138      do statement while ( expression ) ;
8139      for ( for-init-statement condition [opt] ; expression [opt] )
8140        statement
8141
8142    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
8143
8144 static tree
8145 cp_parser_iteration_statement (cp_parser* parser)
8146 {
8147   cp_token *token;
8148   enum rid keyword;
8149   tree statement;
8150   unsigned char in_statement;
8151
8152   /* Peek at the next token.  */
8153   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8154   if (!token)
8155     return error_mark_node;
8156
8157   /* Remember whether or not we are already within an iteration
8158      statement.  */
8159   in_statement = parser->in_statement;
8160
8161   /* See what kind of keyword it is.  */
8162   keyword = token->keyword;
8163   switch (keyword)
8164     {
8165     case RID_WHILE:
8166       {
8167         tree condition;
8168
8169         /* Begin the while-statement.  */
8170         statement = begin_while_stmt ();
8171         /* Look for the `('.  */
8172         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8173         /* Parse the condition.  */
8174         condition = cp_parser_condition (parser);
8175         finish_while_stmt_cond (condition, statement);
8176         /* Look for the `)'.  */
8177         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8178         /* Parse the dependent statement.  */
8179         parser->in_statement = IN_ITERATION_STMT;
8180         cp_parser_already_scoped_statement (parser);
8181         parser->in_statement = in_statement;
8182         /* We're done with the while-statement.  */
8183         finish_while_stmt (statement);
8184       }
8185       break;
8186
8187     case RID_DO:
8188       {
8189         tree expression;
8190
8191         /* Begin the do-statement.  */
8192         statement = begin_do_stmt ();
8193         /* Parse the body of the do-statement.  */
8194         parser->in_statement = IN_ITERATION_STMT;
8195         cp_parser_implicitly_scoped_statement (parser, NULL);
8196         parser->in_statement = in_statement;
8197         finish_do_body (statement);
8198         /* Look for the `while' keyword.  */
8199         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8200         /* Look for the `('.  */
8201         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8202         /* Parse the expression.  */
8203         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8204         /* We're done with the do-statement.  */
8205         finish_do_stmt (expression, statement);
8206         /* Look for the `)'.  */
8207         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8208         /* Look for the `;'.  */
8209         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8210       }
8211       break;
8212
8213     case RID_FOR:
8214       {
8215         tree condition = NULL_TREE;
8216         tree expression = NULL_TREE;
8217
8218         /* Begin the for-statement.  */
8219         statement = begin_for_stmt ();
8220         /* Look for the `('.  */
8221         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8222         /* Parse the initialization.  */
8223         cp_parser_for_init_statement (parser);
8224         finish_for_init_stmt (statement);
8225
8226         /* If there's a condition, process it.  */
8227         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8228           condition = cp_parser_condition (parser);
8229         finish_for_cond (condition, statement);
8230         /* Look for the `;'.  */
8231         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8232
8233         /* If there's an expression, process it.  */
8234         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8235           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8236         finish_for_expr (expression, statement);
8237         /* Look for the `)'.  */
8238         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8239
8240         /* Parse the body of the for-statement.  */
8241         parser->in_statement = IN_ITERATION_STMT;
8242         cp_parser_already_scoped_statement (parser);
8243         parser->in_statement = in_statement;
8244
8245         /* We're done with the for-statement.  */
8246         finish_for_stmt (statement);
8247       }
8248       break;
8249
8250     default:
8251       cp_parser_error (parser, "expected iteration-statement");
8252       statement = error_mark_node;
8253       break;
8254     }
8255
8256   return statement;
8257 }
8258
8259 /* Parse a for-init-statement.
8260
8261    for-init-statement:
8262      expression-statement
8263      simple-declaration  */
8264
8265 static void
8266 cp_parser_for_init_statement (cp_parser* parser)
8267 {
8268   /* If the next token is a `;', then we have an empty
8269      expression-statement.  Grammatically, this is also a
8270      simple-declaration, but an invalid one, because it does not
8271      declare anything.  Therefore, if we did not handle this case
8272      specially, we would issue an error message about an invalid
8273      declaration.  */
8274   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8275     {
8276       /* We're going to speculatively look for a declaration, falling back
8277          to an expression, if necessary.  */
8278       cp_parser_parse_tentatively (parser);
8279       /* Parse the declaration.  */
8280       cp_parser_simple_declaration (parser,
8281                                     /*function_definition_allowed_p=*/false);
8282       /* If the tentative parse failed, then we shall need to look for an
8283          expression-statement.  */
8284       if (cp_parser_parse_definitely (parser))
8285         return;
8286     }
8287
8288   cp_parser_expression_statement (parser, false);
8289 }
8290
8291 /* Parse a jump-statement.
8292
8293    jump-statement:
8294      break ;
8295      continue ;
8296      return expression [opt] ;
8297      return braced-init-list ;
8298      goto identifier ;
8299
8300    GNU extension:
8301
8302    jump-statement:
8303      goto * expression ;
8304
8305    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
8306
8307 static tree
8308 cp_parser_jump_statement (cp_parser* parser)
8309 {
8310   tree statement = error_mark_node;
8311   cp_token *token;
8312   enum rid keyword;
8313   unsigned char in_statement;
8314
8315   /* Peek at the next token.  */
8316   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8317   if (!token)
8318     return error_mark_node;
8319
8320   /* See what kind of keyword it is.  */
8321   keyword = token->keyword;
8322   switch (keyword)
8323     {
8324     case RID_BREAK:
8325       in_statement = parser->in_statement & ~IN_IF_STMT;      
8326       switch (in_statement)
8327         {
8328         case 0:
8329           error_at (token->location, "break statement not within loop or switch");
8330           break;
8331         default:
8332           gcc_assert ((in_statement & IN_SWITCH_STMT)
8333                       || in_statement == IN_ITERATION_STMT);
8334           statement = finish_break_stmt ();
8335           break;
8336         case IN_OMP_BLOCK:
8337           error_at (token->location, "invalid exit from OpenMP structured block");
8338           break;
8339         case IN_OMP_FOR:
8340           error_at (token->location, "break statement used with OpenMP for loop");
8341           break;
8342         }
8343       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8344       break;
8345
8346     case RID_CONTINUE:
8347       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8348         {
8349         case 0:
8350           error_at (token->location, "continue statement not within a loop");
8351           break;
8352         case IN_ITERATION_STMT:
8353         case IN_OMP_FOR:
8354           statement = finish_continue_stmt ();
8355           break;
8356         case IN_OMP_BLOCK:
8357           error_at (token->location, "invalid exit from OpenMP structured block");
8358           break;
8359         default:
8360           gcc_unreachable ();
8361         }
8362       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8363       break;
8364
8365     case RID_RETURN:
8366       {
8367         tree expr;
8368         bool expr_non_constant_p;
8369
8370         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8371           {
8372             maybe_warn_cpp0x ("extended initializer lists");
8373             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8374           }
8375         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8376           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8377         else
8378           /* If the next token is a `;', then there is no
8379              expression.  */
8380           expr = NULL_TREE;
8381         /* Build the return-statement.  */
8382         statement = finish_return_stmt (expr);
8383         /* Look for the final `;'.  */
8384         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8385       }
8386       break;
8387
8388     case RID_GOTO:
8389       /* Create the goto-statement.  */
8390       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8391         {
8392           /* Issue a warning about this use of a GNU extension.  */
8393           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8394           /* Consume the '*' token.  */
8395           cp_lexer_consume_token (parser->lexer);
8396           /* Parse the dependent expression.  */
8397           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8398         }
8399       else
8400         finish_goto_stmt (cp_parser_identifier (parser));
8401       /* Look for the final `;'.  */
8402       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8403       break;
8404
8405     default:
8406       cp_parser_error (parser, "expected jump-statement");
8407       break;
8408     }
8409
8410   return statement;
8411 }
8412
8413 /* Parse a declaration-statement.
8414
8415    declaration-statement:
8416      block-declaration  */
8417
8418 static void
8419 cp_parser_declaration_statement (cp_parser* parser)
8420 {
8421   void *p;
8422
8423   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8424   p = obstack_alloc (&declarator_obstack, 0);
8425
8426  /* Parse the block-declaration.  */
8427   cp_parser_block_declaration (parser, /*statement_p=*/true);
8428
8429   /* Free any declarators allocated.  */
8430   obstack_free (&declarator_obstack, p);
8431
8432   /* Finish off the statement.  */
8433   finish_stmt ();
8434 }
8435
8436 /* Some dependent statements (like `if (cond) statement'), are
8437    implicitly in their own scope.  In other words, if the statement is
8438    a single statement (as opposed to a compound-statement), it is
8439    none-the-less treated as if it were enclosed in braces.  Any
8440    declarations appearing in the dependent statement are out of scope
8441    after control passes that point.  This function parses a statement,
8442    but ensures that is in its own scope, even if it is not a
8443    compound-statement.
8444
8445    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8446    is a (possibly labeled) if statement which is not enclosed in
8447    braces and has an else clause.  This is used to implement
8448    -Wparentheses.
8449
8450    Returns the new statement.  */
8451
8452 static tree
8453 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8454 {
8455   tree statement;
8456
8457   if (if_p != NULL)
8458     *if_p = false;
8459
8460   /* Mark if () ; with a special NOP_EXPR.  */
8461   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8462     {
8463       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8464       cp_lexer_consume_token (parser->lexer);
8465       statement = add_stmt (build_empty_stmt (loc));
8466     }
8467   /* if a compound is opened, we simply parse the statement directly.  */
8468   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8469     statement = cp_parser_compound_statement (parser, NULL, false);
8470   /* If the token is not a `{', then we must take special action.  */
8471   else
8472     {
8473       /* Create a compound-statement.  */
8474       statement = begin_compound_stmt (0);
8475       /* Parse the dependent-statement.  */
8476       cp_parser_statement (parser, NULL_TREE, false, if_p);
8477       /* Finish the dummy compound-statement.  */
8478       finish_compound_stmt (statement);
8479     }
8480
8481   /* Return the statement.  */
8482   return statement;
8483 }
8484
8485 /* For some dependent statements (like `while (cond) statement'), we
8486    have already created a scope.  Therefore, even if the dependent
8487    statement is a compound-statement, we do not want to create another
8488    scope.  */
8489
8490 static void
8491 cp_parser_already_scoped_statement (cp_parser* parser)
8492 {
8493   /* If the token is a `{', then we must take special action.  */
8494   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8495     cp_parser_statement (parser, NULL_TREE, false, NULL);
8496   else
8497     {
8498       /* Avoid calling cp_parser_compound_statement, so that we
8499          don't create a new scope.  Do everything else by hand.  */
8500       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8501       /* If the next keyword is `__label__' we have a label declaration.  */
8502       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8503         cp_parser_label_declaration (parser);
8504       /* Parse an (optional) statement-seq.  */
8505       cp_parser_statement_seq_opt (parser, NULL_TREE);
8506       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8507     }
8508 }
8509
8510 /* Declarations [gram.dcl.dcl] */
8511
8512 /* Parse an optional declaration-sequence.
8513
8514    declaration-seq:
8515      declaration
8516      declaration-seq declaration  */
8517
8518 static void
8519 cp_parser_declaration_seq_opt (cp_parser* parser)
8520 {
8521   while (true)
8522     {
8523       cp_token *token;
8524
8525       token = cp_lexer_peek_token (parser->lexer);
8526
8527       if (token->type == CPP_CLOSE_BRACE
8528           || token->type == CPP_EOF
8529           || token->type == CPP_PRAGMA_EOL)
8530         break;
8531
8532       if (token->type == CPP_SEMICOLON)
8533         {
8534           /* A declaration consisting of a single semicolon is
8535              invalid.  Allow it unless we're being pedantic.  */
8536           cp_lexer_consume_token (parser->lexer);
8537           if (!in_system_header)
8538             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8539           continue;
8540         }
8541
8542       /* If we're entering or exiting a region that's implicitly
8543          extern "C", modify the lang context appropriately.  */
8544       if (!parser->implicit_extern_c && token->implicit_extern_c)
8545         {
8546           push_lang_context (lang_name_c);
8547           parser->implicit_extern_c = true;
8548         }
8549       else if (parser->implicit_extern_c && !token->implicit_extern_c)
8550         {
8551           pop_lang_context ();
8552           parser->implicit_extern_c = false;
8553         }
8554
8555       if (token->type == CPP_PRAGMA)
8556         {
8557           /* A top-level declaration can consist solely of a #pragma.
8558              A nested declaration cannot, so this is done here and not
8559              in cp_parser_declaration.  (A #pragma at block scope is
8560              handled in cp_parser_statement.)  */
8561           cp_parser_pragma (parser, pragma_external);
8562           continue;
8563         }
8564
8565       /* Parse the declaration itself.  */
8566       cp_parser_declaration (parser);
8567     }
8568 }
8569
8570 /* Parse a declaration.
8571
8572    declaration:
8573      block-declaration
8574      function-definition
8575      template-declaration
8576      explicit-instantiation
8577      explicit-specialization
8578      linkage-specification
8579      namespace-definition
8580
8581    GNU extension:
8582
8583    declaration:
8584       __extension__ declaration */
8585
8586 static void
8587 cp_parser_declaration (cp_parser* parser)
8588 {
8589   cp_token token1;
8590   cp_token token2;
8591   int saved_pedantic;
8592   void *p;
8593
8594   /* Check for the `__extension__' keyword.  */
8595   if (cp_parser_extension_opt (parser, &saved_pedantic))
8596     {
8597       /* Parse the qualified declaration.  */
8598       cp_parser_declaration (parser);
8599       /* Restore the PEDANTIC flag.  */
8600       pedantic = saved_pedantic;
8601
8602       return;
8603     }
8604
8605   /* Try to figure out what kind of declaration is present.  */
8606   token1 = *cp_lexer_peek_token (parser->lexer);
8607
8608   if (token1.type != CPP_EOF)
8609     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8610   else
8611     {
8612       token2.type = CPP_EOF;
8613       token2.keyword = RID_MAX;
8614     }
8615
8616   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8617   p = obstack_alloc (&declarator_obstack, 0);
8618
8619   /* If the next token is `extern' and the following token is a string
8620      literal, then we have a linkage specification.  */
8621   if (token1.keyword == RID_EXTERN
8622       && cp_parser_is_string_literal (&token2))
8623     cp_parser_linkage_specification (parser);
8624   /* If the next token is `template', then we have either a template
8625      declaration, an explicit instantiation, or an explicit
8626      specialization.  */
8627   else if (token1.keyword == RID_TEMPLATE)
8628     {
8629       /* `template <>' indicates a template specialization.  */
8630       if (token2.type == CPP_LESS
8631           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8632         cp_parser_explicit_specialization (parser);
8633       /* `template <' indicates a template declaration.  */
8634       else if (token2.type == CPP_LESS)
8635         cp_parser_template_declaration (parser, /*member_p=*/false);
8636       /* Anything else must be an explicit instantiation.  */
8637       else
8638         cp_parser_explicit_instantiation (parser);
8639     }
8640   /* If the next token is `export', then we have a template
8641      declaration.  */
8642   else if (token1.keyword == RID_EXPORT)
8643     cp_parser_template_declaration (parser, /*member_p=*/false);
8644   /* If the next token is `extern', 'static' or 'inline' and the one
8645      after that is `template', we have a GNU extended explicit
8646      instantiation directive.  */
8647   else if (cp_parser_allow_gnu_extensions_p (parser)
8648            && (token1.keyword == RID_EXTERN
8649                || token1.keyword == RID_STATIC
8650                || token1.keyword == RID_INLINE)
8651            && token2.keyword == RID_TEMPLATE)
8652     cp_parser_explicit_instantiation (parser);
8653   /* If the next token is `namespace', check for a named or unnamed
8654      namespace definition.  */
8655   else if (token1.keyword == RID_NAMESPACE
8656            && (/* A named namespace definition.  */
8657                (token2.type == CPP_NAME
8658                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8659                     != CPP_EQ))
8660                /* An unnamed namespace definition.  */
8661                || token2.type == CPP_OPEN_BRACE
8662                || token2.keyword == RID_ATTRIBUTE))
8663     cp_parser_namespace_definition (parser);
8664   /* An inline (associated) namespace definition.  */
8665   else if (token1.keyword == RID_INLINE
8666            && token2.keyword == RID_NAMESPACE)
8667     cp_parser_namespace_definition (parser);
8668   /* Objective-C++ declaration/definition.  */
8669   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8670     cp_parser_objc_declaration (parser);
8671   /* We must have either a block declaration or a function
8672      definition.  */
8673   else
8674     /* Try to parse a block-declaration, or a function-definition.  */
8675     cp_parser_block_declaration (parser, /*statement_p=*/false);
8676
8677   /* Free any declarators allocated.  */
8678   obstack_free (&declarator_obstack, p);
8679 }
8680
8681 /* Parse a block-declaration.
8682
8683    block-declaration:
8684      simple-declaration
8685      asm-definition
8686      namespace-alias-definition
8687      using-declaration
8688      using-directive
8689
8690    GNU Extension:
8691
8692    block-declaration:
8693      __extension__ block-declaration
8694
8695    C++0x Extension:
8696
8697    block-declaration:
8698      static_assert-declaration
8699
8700    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8701    part of a declaration-statement.  */
8702
8703 static void
8704 cp_parser_block_declaration (cp_parser *parser,
8705                              bool      statement_p)
8706 {
8707   cp_token *token1;
8708   int saved_pedantic;
8709
8710   /* Check for the `__extension__' keyword.  */
8711   if (cp_parser_extension_opt (parser, &saved_pedantic))
8712     {
8713       /* Parse the qualified declaration.  */
8714       cp_parser_block_declaration (parser, statement_p);
8715       /* Restore the PEDANTIC flag.  */
8716       pedantic = saved_pedantic;
8717
8718       return;
8719     }
8720
8721   /* Peek at the next token to figure out which kind of declaration is
8722      present.  */
8723   token1 = cp_lexer_peek_token (parser->lexer);
8724
8725   /* If the next keyword is `asm', we have an asm-definition.  */
8726   if (token1->keyword == RID_ASM)
8727     {
8728       if (statement_p)
8729         cp_parser_commit_to_tentative_parse (parser);
8730       cp_parser_asm_definition (parser);
8731     }
8732   /* If the next keyword is `namespace', we have a
8733      namespace-alias-definition.  */
8734   else if (token1->keyword == RID_NAMESPACE)
8735     cp_parser_namespace_alias_definition (parser);
8736   /* If the next keyword is `using', we have either a
8737      using-declaration or a using-directive.  */
8738   else if (token1->keyword == RID_USING)
8739     {
8740       cp_token *token2;
8741
8742       if (statement_p)
8743         cp_parser_commit_to_tentative_parse (parser);
8744       /* If the token after `using' is `namespace', then we have a
8745          using-directive.  */
8746       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8747       if (token2->keyword == RID_NAMESPACE)
8748         cp_parser_using_directive (parser);
8749       /* Otherwise, it's a using-declaration.  */
8750       else
8751         cp_parser_using_declaration (parser,
8752                                      /*access_declaration_p=*/false);
8753     }
8754   /* If the next keyword is `__label__' we have a misplaced label
8755      declaration.  */
8756   else if (token1->keyword == RID_LABEL)
8757     {
8758       cp_lexer_consume_token (parser->lexer);
8759       error_at (token1->location, "%<__label__%> not at the beginning of a block");
8760       cp_parser_skip_to_end_of_statement (parser);
8761       /* If the next token is now a `;', consume it.  */
8762       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8763         cp_lexer_consume_token (parser->lexer);
8764     }
8765   /* If the next token is `static_assert' we have a static assertion.  */
8766   else if (token1->keyword == RID_STATIC_ASSERT)
8767     cp_parser_static_assert (parser, /*member_p=*/false);
8768   /* Anything else must be a simple-declaration.  */
8769   else
8770     cp_parser_simple_declaration (parser, !statement_p);
8771 }
8772
8773 /* Parse a simple-declaration.
8774
8775    simple-declaration:
8776      decl-specifier-seq [opt] init-declarator-list [opt] ;
8777
8778    init-declarator-list:
8779      init-declarator
8780      init-declarator-list , init-declarator
8781
8782    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8783    function-definition as a simple-declaration.  */
8784
8785 static void
8786 cp_parser_simple_declaration (cp_parser* parser,
8787                               bool function_definition_allowed_p)
8788 {
8789   cp_decl_specifier_seq decl_specifiers;
8790   int declares_class_or_enum;
8791   bool saw_declarator;
8792
8793   /* Defer access checks until we know what is being declared; the
8794      checks for names appearing in the decl-specifier-seq should be
8795      done as if we were in the scope of the thing being declared.  */
8796   push_deferring_access_checks (dk_deferred);
8797
8798   /* Parse the decl-specifier-seq.  We have to keep track of whether
8799      or not the decl-specifier-seq declares a named class or
8800      enumeration type, since that is the only case in which the
8801      init-declarator-list is allowed to be empty.
8802
8803      [dcl.dcl]
8804
8805      In a simple-declaration, the optional init-declarator-list can be
8806      omitted only when declaring a class or enumeration, that is when
8807      the decl-specifier-seq contains either a class-specifier, an
8808      elaborated-type-specifier, or an enum-specifier.  */
8809   cp_parser_decl_specifier_seq (parser,
8810                                 CP_PARSER_FLAGS_OPTIONAL,
8811                                 &decl_specifiers,
8812                                 &declares_class_or_enum);
8813   /* We no longer need to defer access checks.  */
8814   stop_deferring_access_checks ();
8815
8816   /* In a block scope, a valid declaration must always have a
8817      decl-specifier-seq.  By not trying to parse declarators, we can
8818      resolve the declaration/expression ambiguity more quickly.  */
8819   if (!function_definition_allowed_p
8820       && !decl_specifiers.any_specifiers_p)
8821     {
8822       cp_parser_error (parser, "expected declaration");
8823       goto done;
8824     }
8825
8826   /* If the next two tokens are both identifiers, the code is
8827      erroneous. The usual cause of this situation is code like:
8828
8829        T t;
8830
8831      where "T" should name a type -- but does not.  */
8832   if (!decl_specifiers.type
8833       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8834     {
8835       /* If parsing tentatively, we should commit; we really are
8836          looking at a declaration.  */
8837       cp_parser_commit_to_tentative_parse (parser);
8838       /* Give up.  */
8839       goto done;
8840     }
8841
8842   /* If we have seen at least one decl-specifier, and the next token
8843      is not a parenthesis, then we must be looking at a declaration.
8844      (After "int (" we might be looking at a functional cast.)  */
8845   if (decl_specifiers.any_specifiers_p
8846       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8847       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8848       && !cp_parser_error_occurred (parser))
8849     cp_parser_commit_to_tentative_parse (parser);
8850
8851   /* Keep going until we hit the `;' at the end of the simple
8852      declaration.  */
8853   saw_declarator = false;
8854   while (cp_lexer_next_token_is_not (parser->lexer,
8855                                      CPP_SEMICOLON))
8856     {
8857       cp_token *token;
8858       bool function_definition_p;
8859       tree decl;
8860
8861       if (saw_declarator)
8862         {
8863           /* If we are processing next declarator, coma is expected */
8864           token = cp_lexer_peek_token (parser->lexer);
8865           gcc_assert (token->type == CPP_COMMA);
8866           cp_lexer_consume_token (parser->lexer);
8867         }
8868       else
8869         saw_declarator = true;
8870
8871       /* Parse the init-declarator.  */
8872       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8873                                         /*checks=*/NULL,
8874                                         function_definition_allowed_p,
8875                                         /*member_p=*/false,
8876                                         declares_class_or_enum,
8877                                         &function_definition_p);
8878       /* If an error occurred while parsing tentatively, exit quickly.
8879          (That usually happens when in the body of a function; each
8880          statement is treated as a declaration-statement until proven
8881          otherwise.)  */
8882       if (cp_parser_error_occurred (parser))
8883         goto done;
8884       /* Handle function definitions specially.  */
8885       if (function_definition_p)
8886         {
8887           /* If the next token is a `,', then we are probably
8888              processing something like:
8889
8890                void f() {}, *p;
8891
8892              which is erroneous.  */
8893           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8894             {
8895               cp_token *token = cp_lexer_peek_token (parser->lexer);
8896               error_at (token->location,
8897                         "mixing"
8898                         " declarations and function-definitions is forbidden");
8899             }
8900           /* Otherwise, we're done with the list of declarators.  */
8901           else
8902             {
8903               pop_deferring_access_checks ();
8904               return;
8905             }
8906         }
8907       /* The next token should be either a `,' or a `;'.  */
8908       token = cp_lexer_peek_token (parser->lexer);
8909       /* If it's a `,', there are more declarators to come.  */
8910       if (token->type == CPP_COMMA)
8911         /* will be consumed next time around */;
8912       /* If it's a `;', we are done.  */
8913       else if (token->type == CPP_SEMICOLON)
8914         break;
8915       /* Anything else is an error.  */
8916       else
8917         {
8918           /* If we have already issued an error message we don't need
8919              to issue another one.  */
8920           if (decl != error_mark_node
8921               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8922             cp_parser_error (parser, "expected %<,%> or %<;%>");
8923           /* Skip tokens until we reach the end of the statement.  */
8924           cp_parser_skip_to_end_of_statement (parser);
8925           /* If the next token is now a `;', consume it.  */
8926           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8927             cp_lexer_consume_token (parser->lexer);
8928           goto done;
8929         }
8930       /* After the first time around, a function-definition is not
8931          allowed -- even if it was OK at first.  For example:
8932
8933            int i, f() {}
8934
8935          is not valid.  */
8936       function_definition_allowed_p = false;
8937     }
8938
8939   /* Issue an error message if no declarators are present, and the
8940      decl-specifier-seq does not itself declare a class or
8941      enumeration.  */
8942   if (!saw_declarator)
8943     {
8944       if (cp_parser_declares_only_class_p (parser))
8945         shadow_tag (&decl_specifiers);
8946       /* Perform any deferred access checks.  */
8947       perform_deferred_access_checks ();
8948     }
8949
8950   /* Consume the `;'.  */
8951   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8952
8953  done:
8954   pop_deferring_access_checks ();
8955 }
8956
8957 /* Parse a decl-specifier-seq.
8958
8959    decl-specifier-seq:
8960      decl-specifier-seq [opt] decl-specifier
8961
8962    decl-specifier:
8963      storage-class-specifier
8964      type-specifier
8965      function-specifier
8966      friend
8967      typedef
8968
8969    GNU Extension:
8970
8971    decl-specifier:
8972      attributes
8973
8974    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8975
8976    The parser flags FLAGS is used to control type-specifier parsing.
8977
8978    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8979    flags:
8980
8981      1: one of the decl-specifiers is an elaborated-type-specifier
8982         (i.e., a type declaration)
8983      2: one of the decl-specifiers is an enum-specifier or a
8984         class-specifier (i.e., a type definition)
8985
8986    */
8987
8988 static void
8989 cp_parser_decl_specifier_seq (cp_parser* parser,
8990                               cp_parser_flags flags,
8991                               cp_decl_specifier_seq *decl_specs,
8992                               int* declares_class_or_enum)
8993 {
8994   bool constructor_possible_p = !parser->in_declarator_p;
8995   cp_token *start_token = NULL;
8996
8997   /* Clear DECL_SPECS.  */
8998   clear_decl_specs (decl_specs);
8999
9000   /* Assume no class or enumeration type is declared.  */
9001   *declares_class_or_enum = 0;
9002
9003   /* Keep reading specifiers until there are no more to read.  */
9004   while (true)
9005     {
9006       bool constructor_p;
9007       bool found_decl_spec;
9008       cp_token *token;
9009
9010       /* Peek at the next token.  */
9011       token = cp_lexer_peek_token (parser->lexer);
9012
9013       /* Save the first token of the decl spec list for error
9014          reporting.  */
9015       if (!start_token)
9016         start_token = token;
9017       /* Handle attributes.  */
9018       if (token->keyword == RID_ATTRIBUTE)
9019         {
9020           /* Parse the attributes.  */
9021           decl_specs->attributes
9022             = chainon (decl_specs->attributes,
9023                        cp_parser_attributes_opt (parser));
9024           continue;
9025         }
9026       /* Assume we will find a decl-specifier keyword.  */
9027       found_decl_spec = true;
9028       /* If the next token is an appropriate keyword, we can simply
9029          add it to the list.  */
9030       switch (token->keyword)
9031         {
9032           /* decl-specifier:
9033                friend  */
9034         case RID_FRIEND:
9035           if (!at_class_scope_p ())
9036             {
9037               error_at (token->location, "%<friend%> used outside of class");
9038               cp_lexer_purge_token (parser->lexer);
9039             }
9040           else
9041             {
9042               ++decl_specs->specs[(int) ds_friend];
9043               /* Consume the token.  */
9044               cp_lexer_consume_token (parser->lexer);
9045             }
9046           break;
9047
9048           /* function-specifier:
9049                inline
9050                virtual
9051                explicit  */
9052         case RID_INLINE:
9053         case RID_VIRTUAL:
9054         case RID_EXPLICIT:
9055           cp_parser_function_specifier_opt (parser, decl_specs);
9056           break;
9057
9058           /* decl-specifier:
9059                typedef  */
9060         case RID_TYPEDEF:
9061           ++decl_specs->specs[(int) ds_typedef];
9062           /* Consume the token.  */
9063           cp_lexer_consume_token (parser->lexer);
9064           /* A constructor declarator cannot appear in a typedef.  */
9065           constructor_possible_p = false;
9066           /* The "typedef" keyword can only occur in a declaration; we
9067              may as well commit at this point.  */
9068           cp_parser_commit_to_tentative_parse (parser);
9069
9070           if (decl_specs->storage_class != sc_none)
9071             decl_specs->conflicting_specifiers_p = true;
9072           break;
9073
9074           /* storage-class-specifier:
9075                auto
9076                register
9077                static
9078                extern
9079                mutable
9080
9081              GNU Extension:
9082                thread  */
9083         case RID_AUTO:
9084           if (cxx_dialect == cxx98) 
9085             {
9086               /* Consume the token.  */
9087               cp_lexer_consume_token (parser->lexer);
9088
9089               /* Complain about `auto' as a storage specifier, if
9090                  we're complaining about C++0x compatibility.  */
9091               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9092                           " will change meaning in C++0x; please remove it");
9093
9094               /* Set the storage class anyway.  */
9095               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9096                                            token->location);
9097             }
9098           else
9099             /* C++0x auto type-specifier.  */
9100             found_decl_spec = false;
9101           break;
9102
9103         case RID_REGISTER:
9104         case RID_STATIC:
9105         case RID_EXTERN:
9106         case RID_MUTABLE:
9107           /* Consume the token.  */
9108           cp_lexer_consume_token (parser->lexer);
9109           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9110                                        token->location);
9111           break;
9112         case RID_THREAD:
9113           /* Consume the token.  */
9114           cp_lexer_consume_token (parser->lexer);
9115           ++decl_specs->specs[(int) ds_thread];
9116           break;
9117
9118         default:
9119           /* We did not yet find a decl-specifier yet.  */
9120           found_decl_spec = false;
9121           break;
9122         }
9123
9124       /* Constructors are a special case.  The `S' in `S()' is not a
9125          decl-specifier; it is the beginning of the declarator.  */
9126       constructor_p
9127         = (!found_decl_spec
9128            && constructor_possible_p
9129            && (cp_parser_constructor_declarator_p
9130                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9131
9132       /* If we don't have a DECL_SPEC yet, then we must be looking at
9133          a type-specifier.  */
9134       if (!found_decl_spec && !constructor_p)
9135         {
9136           int decl_spec_declares_class_or_enum;
9137           bool is_cv_qualifier;
9138           tree type_spec;
9139
9140           type_spec
9141             = cp_parser_type_specifier (parser, flags,
9142                                         decl_specs,
9143                                         /*is_declaration=*/true,
9144                                         &decl_spec_declares_class_or_enum,
9145                                         &is_cv_qualifier);
9146           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9147
9148           /* If this type-specifier referenced a user-defined type
9149              (a typedef, class-name, etc.), then we can't allow any
9150              more such type-specifiers henceforth.
9151
9152              [dcl.spec]
9153
9154              The longest sequence of decl-specifiers that could
9155              possibly be a type name is taken as the
9156              decl-specifier-seq of a declaration.  The sequence shall
9157              be self-consistent as described below.
9158
9159              [dcl.type]
9160
9161              As a general rule, at most one type-specifier is allowed
9162              in the complete decl-specifier-seq of a declaration.  The
9163              only exceptions are the following:
9164
9165              -- const or volatile can be combined with any other
9166                 type-specifier.
9167
9168              -- signed or unsigned can be combined with char, long,
9169                 short, or int.
9170
9171              -- ..
9172
9173              Example:
9174
9175                typedef char* Pc;
9176                void g (const int Pc);
9177
9178              Here, Pc is *not* part of the decl-specifier seq; it's
9179              the declarator.  Therefore, once we see a type-specifier
9180              (other than a cv-qualifier), we forbid any additional
9181              user-defined types.  We *do* still allow things like `int
9182              int' to be considered a decl-specifier-seq, and issue the
9183              error message later.  */
9184           if (type_spec && !is_cv_qualifier)
9185             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9186           /* A constructor declarator cannot follow a type-specifier.  */
9187           if (type_spec)
9188             {
9189               constructor_possible_p = false;
9190               found_decl_spec = true;
9191             }
9192         }
9193
9194       /* If we still do not have a DECL_SPEC, then there are no more
9195          decl-specifiers.  */
9196       if (!found_decl_spec)
9197         break;
9198
9199       decl_specs->any_specifiers_p = true;
9200       /* After we see one decl-specifier, further decl-specifiers are
9201          always optional.  */
9202       flags |= CP_PARSER_FLAGS_OPTIONAL;
9203     }
9204
9205   cp_parser_check_decl_spec (decl_specs, start_token->location);
9206
9207   /* Don't allow a friend specifier with a class definition.  */
9208   if (decl_specs->specs[(int) ds_friend] != 0
9209       && (*declares_class_or_enum & 2))
9210     error_at (start_token->location,
9211               "class definition may not be declared a friend");
9212 }
9213
9214 /* Parse an (optional) storage-class-specifier.
9215
9216    storage-class-specifier:
9217      auto
9218      register
9219      static
9220      extern
9221      mutable
9222
9223    GNU Extension:
9224
9225    storage-class-specifier:
9226      thread
9227
9228    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9229
9230 static tree
9231 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9232 {
9233   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9234     {
9235     case RID_AUTO:
9236       if (cxx_dialect != cxx98)
9237         return NULL_TREE;
9238       /* Fall through for C++98.  */
9239
9240     case RID_REGISTER:
9241     case RID_STATIC:
9242     case RID_EXTERN:
9243     case RID_MUTABLE:
9244     case RID_THREAD:
9245       /* Consume the token.  */
9246       return cp_lexer_consume_token (parser->lexer)->u.value;
9247
9248     default:
9249       return NULL_TREE;
9250     }
9251 }
9252
9253 /* Parse an (optional) function-specifier.
9254
9255    function-specifier:
9256      inline
9257      virtual
9258      explicit
9259
9260    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9261    Updates DECL_SPECS, if it is non-NULL.  */
9262
9263 static tree
9264 cp_parser_function_specifier_opt (cp_parser* parser,
9265                                   cp_decl_specifier_seq *decl_specs)
9266 {
9267   cp_token *token = cp_lexer_peek_token (parser->lexer);
9268   switch (token->keyword)
9269     {
9270     case RID_INLINE:
9271       if (decl_specs)
9272         ++decl_specs->specs[(int) ds_inline];
9273       break;
9274
9275     case RID_VIRTUAL:
9276       /* 14.5.2.3 [temp.mem]
9277
9278          A member function template shall not be virtual.  */
9279       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9280         error_at (token->location, "templates may not be %<virtual%>");
9281       else if (decl_specs)
9282         ++decl_specs->specs[(int) ds_virtual];
9283       break;
9284
9285     case RID_EXPLICIT:
9286       if (decl_specs)
9287         ++decl_specs->specs[(int) ds_explicit];
9288       break;
9289
9290     default:
9291       return NULL_TREE;
9292     }
9293
9294   /* Consume the token.  */
9295   return cp_lexer_consume_token (parser->lexer)->u.value;
9296 }
9297
9298 /* Parse a linkage-specification.
9299
9300    linkage-specification:
9301      extern string-literal { declaration-seq [opt] }
9302      extern string-literal declaration  */
9303
9304 static void
9305 cp_parser_linkage_specification (cp_parser* parser)
9306 {
9307   tree linkage;
9308
9309   /* Look for the `extern' keyword.  */
9310   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9311
9312   /* Look for the string-literal.  */
9313   linkage = cp_parser_string_literal (parser, false, false);
9314
9315   /* Transform the literal into an identifier.  If the literal is a
9316      wide-character string, or contains embedded NULs, then we can't
9317      handle it as the user wants.  */
9318   if (strlen (TREE_STRING_POINTER (linkage))
9319       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9320     {
9321       cp_parser_error (parser, "invalid linkage-specification");
9322       /* Assume C++ linkage.  */
9323       linkage = lang_name_cplusplus;
9324     }
9325   else
9326     linkage = get_identifier (TREE_STRING_POINTER (linkage));
9327
9328   /* We're now using the new linkage.  */
9329   push_lang_context (linkage);
9330
9331   /* If the next token is a `{', then we're using the first
9332      production.  */
9333   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9334     {
9335       /* Consume the `{' token.  */
9336       cp_lexer_consume_token (parser->lexer);
9337       /* Parse the declarations.  */
9338       cp_parser_declaration_seq_opt (parser);
9339       /* Look for the closing `}'.  */
9340       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9341     }
9342   /* Otherwise, there's just one declaration.  */
9343   else
9344     {
9345       bool saved_in_unbraced_linkage_specification_p;
9346
9347       saved_in_unbraced_linkage_specification_p
9348         = parser->in_unbraced_linkage_specification_p;
9349       parser->in_unbraced_linkage_specification_p = true;
9350       cp_parser_declaration (parser);
9351       parser->in_unbraced_linkage_specification_p
9352         = saved_in_unbraced_linkage_specification_p;
9353     }
9354
9355   /* We're done with the linkage-specification.  */
9356   pop_lang_context ();
9357 }
9358
9359 /* Parse a static_assert-declaration.
9360
9361    static_assert-declaration:
9362      static_assert ( constant-expression , string-literal ) ; 
9363
9364    If MEMBER_P, this static_assert is a class member.  */
9365
9366 static void 
9367 cp_parser_static_assert(cp_parser *parser, bool member_p)
9368 {
9369   tree condition;
9370   tree message;
9371   cp_token *token;
9372   location_t saved_loc;
9373
9374   /* Peek at the `static_assert' token so we can keep track of exactly
9375      where the static assertion started.  */
9376   token = cp_lexer_peek_token (parser->lexer);
9377   saved_loc = token->location;
9378
9379   /* Look for the `static_assert' keyword.  */
9380   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
9381                                   "%<static_assert%>"))
9382     return;
9383
9384   /*  We know we are in a static assertion; commit to any tentative
9385       parse.  */
9386   if (cp_parser_parsing_tentatively (parser))
9387     cp_parser_commit_to_tentative_parse (parser);
9388
9389   /* Parse the `(' starting the static assertion condition.  */
9390   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9391
9392   /* Parse the constant-expression.  */
9393   condition = 
9394     cp_parser_constant_expression (parser,
9395                                    /*allow_non_constant_p=*/false,
9396                                    /*non_constant_p=*/NULL);
9397
9398   /* Parse the separating `,'.  */
9399   cp_parser_require (parser, CPP_COMMA, "%<,%>");
9400
9401   /* Parse the string-literal message.  */
9402   message = cp_parser_string_literal (parser, 
9403                                       /*translate=*/false,
9404                                       /*wide_ok=*/true);
9405
9406   /* A `)' completes the static assertion.  */
9407   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9408     cp_parser_skip_to_closing_parenthesis (parser, 
9409                                            /*recovering=*/true, 
9410                                            /*or_comma=*/false,
9411                                            /*consume_paren=*/true);
9412
9413   /* A semicolon terminates the declaration.  */
9414   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9415
9416   /* Complete the static assertion, which may mean either processing 
9417      the static assert now or saving it for template instantiation.  */
9418   finish_static_assert (condition, message, saved_loc, member_p);
9419 }
9420
9421 /* Parse a `decltype' type. Returns the type. 
9422
9423    simple-type-specifier:
9424      decltype ( expression )  */
9425
9426 static tree
9427 cp_parser_decltype (cp_parser *parser)
9428 {
9429   tree expr;
9430   bool id_expression_or_member_access_p = false;
9431   const char *saved_message;
9432   bool saved_integral_constant_expression_p;
9433   bool saved_non_integral_constant_expression_p;
9434   cp_token *id_expr_start_token;
9435
9436   /* Look for the `decltype' token.  */
9437   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9438     return error_mark_node;
9439
9440   /* Types cannot be defined in a `decltype' expression.  Save away the
9441      old message.  */
9442   saved_message = parser->type_definition_forbidden_message;
9443
9444   /* And create the new one.  */
9445   parser->type_definition_forbidden_message
9446     = "types may not be defined in %<decltype%> expressions";
9447
9448   /* The restrictions on constant-expressions do not apply inside
9449      decltype expressions.  */
9450   saved_integral_constant_expression_p
9451     = parser->integral_constant_expression_p;
9452   saved_non_integral_constant_expression_p
9453     = parser->non_integral_constant_expression_p;
9454   parser->integral_constant_expression_p = false;
9455
9456   /* Do not actually evaluate the expression.  */
9457   ++cp_unevaluated_operand;
9458
9459   /* Do not warn about problems with the expression.  */
9460   ++c_inhibit_evaluation_warnings;
9461
9462   /* Parse the opening `('.  */
9463   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9464     return error_mark_node;
9465   
9466   /* First, try parsing an id-expression.  */
9467   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9468   cp_parser_parse_tentatively (parser);
9469   expr = cp_parser_id_expression (parser,
9470                                   /*template_keyword_p=*/false,
9471                                   /*check_dependency_p=*/true,
9472                                   /*template_p=*/NULL,
9473                                   /*declarator_p=*/false,
9474                                   /*optional_p=*/false);
9475
9476   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9477     {
9478       bool non_integral_constant_expression_p = false;
9479       tree id_expression = expr;
9480       cp_id_kind idk;
9481       const char *error_msg;
9482
9483       if (TREE_CODE (expr) == IDENTIFIER_NODE)
9484         /* Lookup the name we got back from the id-expression.  */
9485         expr = cp_parser_lookup_name (parser, expr,
9486                                       none_type,
9487                                       /*is_template=*/false,
9488                                       /*is_namespace=*/false,
9489                                       /*check_dependency=*/true,
9490                                       /*ambiguous_decls=*/NULL,
9491                                       id_expr_start_token->location);
9492
9493       if (expr
9494           && expr != error_mark_node
9495           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9496           && TREE_CODE (expr) != TYPE_DECL
9497           && (TREE_CODE (expr) != BIT_NOT_EXPR
9498               || !TYPE_P (TREE_OPERAND (expr, 0)))
9499           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9500         {
9501           /* Complete lookup of the id-expression.  */
9502           expr = (finish_id_expression
9503                   (id_expression, expr, parser->scope, &idk,
9504                    /*integral_constant_expression_p=*/false,
9505                    /*allow_non_integral_constant_expression_p=*/true,
9506                    &non_integral_constant_expression_p,
9507                    /*template_p=*/false,
9508                    /*done=*/true,
9509                    /*address_p=*/false,
9510                    /*template_arg_p=*/false,
9511                    &error_msg,
9512                    id_expr_start_token->location));
9513
9514           if (expr == error_mark_node)
9515             /* We found an id-expression, but it was something that we
9516                should not have found. This is an error, not something
9517                we can recover from, so note that we found an
9518                id-expression and we'll recover as gracefully as
9519                possible.  */
9520             id_expression_or_member_access_p = true;
9521         }
9522
9523       if (expr 
9524           && expr != error_mark_node
9525           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9526         /* We have an id-expression.  */
9527         id_expression_or_member_access_p = true;
9528     }
9529
9530   if (!id_expression_or_member_access_p)
9531     {
9532       /* Abort the id-expression parse.  */
9533       cp_parser_abort_tentative_parse (parser);
9534
9535       /* Parsing tentatively, again.  */
9536       cp_parser_parse_tentatively (parser);
9537
9538       /* Parse a class member access.  */
9539       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9540                                            /*cast_p=*/false,
9541                                            /*member_access_only_p=*/true, NULL);
9542
9543       if (expr 
9544           && expr != error_mark_node
9545           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9546         /* We have an id-expression.  */
9547         id_expression_or_member_access_p = true;
9548     }
9549
9550   if (id_expression_or_member_access_p)
9551     /* We have parsed the complete id-expression or member access.  */
9552     cp_parser_parse_definitely (parser);
9553   else
9554     {
9555       /* Abort our attempt to parse an id-expression or member access
9556          expression.  */
9557       cp_parser_abort_tentative_parse (parser);
9558
9559       /* Parse a full expression.  */
9560       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9561     }
9562
9563   /* Go back to evaluating expressions.  */
9564   --cp_unevaluated_operand;
9565   --c_inhibit_evaluation_warnings;
9566
9567   /* Restore the old message and the integral constant expression
9568      flags.  */
9569   parser->type_definition_forbidden_message = saved_message;
9570   parser->integral_constant_expression_p
9571     = saved_integral_constant_expression_p;
9572   parser->non_integral_constant_expression_p
9573     = saved_non_integral_constant_expression_p;
9574
9575   if (expr == error_mark_node)
9576     {
9577       /* Skip everything up to the closing `)'.  */
9578       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9579                                              /*consume_paren=*/true);
9580       return error_mark_node;
9581     }
9582   
9583   /* Parse to the closing `)'.  */
9584   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9585     {
9586       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9587                                              /*consume_paren=*/true);
9588       return error_mark_node;
9589     }
9590
9591   return finish_decltype_type (expr, id_expression_or_member_access_p);
9592 }
9593
9594 /* Special member functions [gram.special] */
9595
9596 /* Parse a conversion-function-id.
9597
9598    conversion-function-id:
9599      operator conversion-type-id
9600
9601    Returns an IDENTIFIER_NODE representing the operator.  */
9602
9603 static tree
9604 cp_parser_conversion_function_id (cp_parser* parser)
9605 {
9606   tree type;
9607   tree saved_scope;
9608   tree saved_qualifying_scope;
9609   tree saved_object_scope;
9610   tree pushed_scope = NULL_TREE;
9611
9612   /* Look for the `operator' token.  */
9613   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9614     return error_mark_node;
9615   /* When we parse the conversion-type-id, the current scope will be
9616      reset.  However, we need that information in able to look up the
9617      conversion function later, so we save it here.  */
9618   saved_scope = parser->scope;
9619   saved_qualifying_scope = parser->qualifying_scope;
9620   saved_object_scope = parser->object_scope;
9621   /* We must enter the scope of the class so that the names of
9622      entities declared within the class are available in the
9623      conversion-type-id.  For example, consider:
9624
9625        struct S {
9626          typedef int I;
9627          operator I();
9628        };
9629
9630        S::operator I() { ... }
9631
9632      In order to see that `I' is a type-name in the definition, we
9633      must be in the scope of `S'.  */
9634   if (saved_scope)
9635     pushed_scope = push_scope (saved_scope);
9636   /* Parse the conversion-type-id.  */
9637   type = cp_parser_conversion_type_id (parser);
9638   /* Leave the scope of the class, if any.  */
9639   if (pushed_scope)
9640     pop_scope (pushed_scope);
9641   /* Restore the saved scope.  */
9642   parser->scope = saved_scope;
9643   parser->qualifying_scope = saved_qualifying_scope;
9644   parser->object_scope = saved_object_scope;
9645   /* If the TYPE is invalid, indicate failure.  */
9646   if (type == error_mark_node)
9647     return error_mark_node;
9648   return mangle_conv_op_name_for_type (type);
9649 }
9650
9651 /* Parse a conversion-type-id:
9652
9653    conversion-type-id:
9654      type-specifier-seq conversion-declarator [opt]
9655
9656    Returns the TYPE specified.  */
9657
9658 static tree
9659 cp_parser_conversion_type_id (cp_parser* parser)
9660 {
9661   tree attributes;
9662   cp_decl_specifier_seq type_specifiers;
9663   cp_declarator *declarator;
9664   tree type_specified;
9665
9666   /* Parse the attributes.  */
9667   attributes = cp_parser_attributes_opt (parser);
9668   /* Parse the type-specifiers.  */
9669   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9670                                 &type_specifiers);
9671   /* If that didn't work, stop.  */
9672   if (type_specifiers.type == error_mark_node)
9673     return error_mark_node;
9674   /* Parse the conversion-declarator.  */
9675   declarator = cp_parser_conversion_declarator_opt (parser);
9676
9677   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9678                                     /*initialized=*/0, &attributes);
9679   if (attributes)
9680     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9681
9682   /* Don't give this error when parsing tentatively.  This happens to
9683      work because we always parse this definitively once.  */
9684   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9685       && type_uses_auto (type_specified))
9686     {
9687       error ("invalid use of %<auto%> in conversion operator");
9688       return error_mark_node;
9689     }
9690
9691   return type_specified;
9692 }
9693
9694 /* Parse an (optional) conversion-declarator.
9695
9696    conversion-declarator:
9697      ptr-operator conversion-declarator [opt]
9698
9699    */
9700
9701 static cp_declarator *
9702 cp_parser_conversion_declarator_opt (cp_parser* parser)
9703 {
9704   enum tree_code code;
9705   tree class_type;
9706   cp_cv_quals cv_quals;
9707
9708   /* We don't know if there's a ptr-operator next, or not.  */
9709   cp_parser_parse_tentatively (parser);
9710   /* Try the ptr-operator.  */
9711   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9712   /* If it worked, look for more conversion-declarators.  */
9713   if (cp_parser_parse_definitely (parser))
9714     {
9715       cp_declarator *declarator;
9716
9717       /* Parse another optional declarator.  */
9718       declarator = cp_parser_conversion_declarator_opt (parser);
9719
9720       return cp_parser_make_indirect_declarator
9721         (code, class_type, cv_quals, declarator);
9722    }
9723
9724   return NULL;
9725 }
9726
9727 /* Parse an (optional) ctor-initializer.
9728
9729    ctor-initializer:
9730      : mem-initializer-list
9731
9732    Returns TRUE iff the ctor-initializer was actually present.  */
9733
9734 static bool
9735 cp_parser_ctor_initializer_opt (cp_parser* parser)
9736 {
9737   /* If the next token is not a `:', then there is no
9738      ctor-initializer.  */
9739   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9740     {
9741       /* Do default initialization of any bases and members.  */
9742       if (DECL_CONSTRUCTOR_P (current_function_decl))
9743         finish_mem_initializers (NULL_TREE);
9744
9745       return false;
9746     }
9747
9748   /* Consume the `:' token.  */
9749   cp_lexer_consume_token (parser->lexer);
9750   /* And the mem-initializer-list.  */
9751   cp_parser_mem_initializer_list (parser);
9752
9753   return true;
9754 }
9755
9756 /* Parse a mem-initializer-list.
9757
9758    mem-initializer-list:
9759      mem-initializer ... [opt]
9760      mem-initializer ... [opt] , mem-initializer-list  */
9761
9762 static void
9763 cp_parser_mem_initializer_list (cp_parser* parser)
9764 {
9765   tree mem_initializer_list = NULL_TREE;
9766   cp_token *token = cp_lexer_peek_token (parser->lexer);
9767
9768   /* Let the semantic analysis code know that we are starting the
9769      mem-initializer-list.  */
9770   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9771     error_at (token->location,
9772               "only constructors take base initializers");
9773
9774   /* Loop through the list.  */
9775   while (true)
9776     {
9777       tree mem_initializer;
9778
9779       token = cp_lexer_peek_token (parser->lexer);
9780       /* Parse the mem-initializer.  */
9781       mem_initializer = cp_parser_mem_initializer (parser);
9782       /* If the next token is a `...', we're expanding member initializers. */
9783       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9784         {
9785           /* Consume the `...'. */
9786           cp_lexer_consume_token (parser->lexer);
9787
9788           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9789              can be expanded but members cannot. */
9790           if (mem_initializer != error_mark_node
9791               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9792             {
9793               error_at (token->location,
9794                         "cannot expand initializer for member %<%D%>",
9795                         TREE_PURPOSE (mem_initializer));
9796               mem_initializer = error_mark_node;
9797             }
9798
9799           /* Construct the pack expansion type. */
9800           if (mem_initializer != error_mark_node)
9801             mem_initializer = make_pack_expansion (mem_initializer);
9802         }
9803       /* Add it to the list, unless it was erroneous.  */
9804       if (mem_initializer != error_mark_node)
9805         {
9806           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9807           mem_initializer_list = mem_initializer;
9808         }
9809       /* If the next token is not a `,', we're done.  */
9810       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9811         break;
9812       /* Consume the `,' token.  */
9813       cp_lexer_consume_token (parser->lexer);
9814     }
9815
9816   /* Perform semantic analysis.  */
9817   if (DECL_CONSTRUCTOR_P (current_function_decl))
9818     finish_mem_initializers (mem_initializer_list);
9819 }
9820
9821 /* Parse a mem-initializer.
9822
9823    mem-initializer:
9824      mem-initializer-id ( expression-list [opt] )
9825      mem-initializer-id braced-init-list
9826
9827    GNU extension:
9828
9829    mem-initializer:
9830      ( expression-list [opt] )
9831
9832    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9833    class) or FIELD_DECL (for a non-static data member) to initialize;
9834    the TREE_VALUE is the expression-list.  An empty initialization
9835    list is represented by void_list_node.  */
9836
9837 static tree
9838 cp_parser_mem_initializer (cp_parser* parser)
9839 {
9840   tree mem_initializer_id;
9841   tree expression_list;
9842   tree member;
9843   cp_token *token = cp_lexer_peek_token (parser->lexer);
9844
9845   /* Find out what is being initialized.  */
9846   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9847     {
9848       permerror (token->location,
9849                  "anachronistic old-style base class initializer");
9850       mem_initializer_id = NULL_TREE;
9851     }
9852   else
9853     {
9854       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9855       if (mem_initializer_id == error_mark_node)
9856         return mem_initializer_id;
9857     }
9858   member = expand_member_init (mem_initializer_id);
9859   if (member && !DECL_P (member))
9860     in_base_initializer = 1;
9861
9862   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9863     {
9864       bool expr_non_constant_p;
9865       maybe_warn_cpp0x ("extended initializer lists");
9866       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9867       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9868       expression_list = build_tree_list (NULL_TREE, expression_list);
9869     }
9870   else
9871     {
9872       VEC(tree,gc)* vec;
9873       vec = cp_parser_parenthesized_expression_list (parser, false,
9874                                                      /*cast_p=*/false,
9875                                                      /*allow_expansion_p=*/true,
9876                                                      /*non_constant_p=*/NULL);
9877       if (vec == NULL)
9878         return error_mark_node;
9879       expression_list = build_tree_list_vec (vec);
9880       release_tree_vector (vec);
9881     }
9882
9883   if (expression_list == error_mark_node)
9884     return error_mark_node;
9885   if (!expression_list)
9886     expression_list = void_type_node;
9887
9888   in_base_initializer = 0;
9889
9890   return member ? build_tree_list (member, expression_list) : error_mark_node;
9891 }
9892
9893 /* Parse a mem-initializer-id.
9894
9895    mem-initializer-id:
9896      :: [opt] nested-name-specifier [opt] class-name
9897      identifier
9898
9899    Returns a TYPE indicating the class to be initializer for the first
9900    production.  Returns an IDENTIFIER_NODE indicating the data member
9901    to be initialized for the second production.  */
9902
9903 static tree
9904 cp_parser_mem_initializer_id (cp_parser* parser)
9905 {
9906   bool global_scope_p;
9907   bool nested_name_specifier_p;
9908   bool template_p = false;
9909   tree id;
9910
9911   cp_token *token = cp_lexer_peek_token (parser->lexer);
9912
9913   /* `typename' is not allowed in this context ([temp.res]).  */
9914   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9915     {
9916       error_at (token->location, 
9917                 "keyword %<typename%> not allowed in this context (a qualified "
9918                 "member initializer is implicitly a type)");
9919       cp_lexer_consume_token (parser->lexer);
9920     }
9921   /* Look for the optional `::' operator.  */
9922   global_scope_p
9923     = (cp_parser_global_scope_opt (parser,
9924                                    /*current_scope_valid_p=*/false)
9925        != NULL_TREE);
9926   /* Look for the optional nested-name-specifier.  The simplest way to
9927      implement:
9928
9929        [temp.res]
9930
9931        The keyword `typename' is not permitted in a base-specifier or
9932        mem-initializer; in these contexts a qualified name that
9933        depends on a template-parameter is implicitly assumed to be a
9934        type name.
9935
9936      is to assume that we have seen the `typename' keyword at this
9937      point.  */
9938   nested_name_specifier_p
9939     = (cp_parser_nested_name_specifier_opt (parser,
9940                                             /*typename_keyword_p=*/true,
9941                                             /*check_dependency_p=*/true,
9942                                             /*type_p=*/true,
9943                                             /*is_declaration=*/true)
9944        != NULL_TREE);
9945   if (nested_name_specifier_p)
9946     template_p = cp_parser_optional_template_keyword (parser);
9947   /* If there is a `::' operator or a nested-name-specifier, then we
9948      are definitely looking for a class-name.  */
9949   if (global_scope_p || nested_name_specifier_p)
9950     return cp_parser_class_name (parser,
9951                                  /*typename_keyword_p=*/true,
9952                                  /*template_keyword_p=*/template_p,
9953                                  none_type,
9954                                  /*check_dependency_p=*/true,
9955                                  /*class_head_p=*/false,
9956                                  /*is_declaration=*/true);
9957   /* Otherwise, we could also be looking for an ordinary identifier.  */
9958   cp_parser_parse_tentatively (parser);
9959   /* Try a class-name.  */
9960   id = cp_parser_class_name (parser,
9961                              /*typename_keyword_p=*/true,
9962                              /*template_keyword_p=*/false,
9963                              none_type,
9964                              /*check_dependency_p=*/true,
9965                              /*class_head_p=*/false,
9966                              /*is_declaration=*/true);
9967   /* If we found one, we're done.  */
9968   if (cp_parser_parse_definitely (parser))
9969     return id;
9970   /* Otherwise, look for an ordinary identifier.  */
9971   return cp_parser_identifier (parser);
9972 }
9973
9974 /* Overloading [gram.over] */
9975
9976 /* Parse an operator-function-id.
9977
9978    operator-function-id:
9979      operator operator
9980
9981    Returns an IDENTIFIER_NODE for the operator which is a
9982    human-readable spelling of the identifier, e.g., `operator +'.  */
9983
9984 static tree
9985 cp_parser_operator_function_id (cp_parser* parser)
9986 {
9987   /* Look for the `operator' keyword.  */
9988   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9989     return error_mark_node;
9990   /* And then the name of the operator itself.  */
9991   return cp_parser_operator (parser);
9992 }
9993
9994 /* Parse an operator.
9995
9996    operator:
9997      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9998      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9999      || ++ -- , ->* -> () []
10000
10001    GNU Extensions:
10002
10003    operator:
10004      <? >? <?= >?=
10005
10006    Returns an IDENTIFIER_NODE for the operator which is a
10007    human-readable spelling of the identifier, e.g., `operator +'.  */
10008
10009 static tree
10010 cp_parser_operator (cp_parser* parser)
10011 {
10012   tree id = NULL_TREE;
10013   cp_token *token;
10014
10015   /* Peek at the next token.  */
10016   token = cp_lexer_peek_token (parser->lexer);
10017   /* Figure out which operator we have.  */
10018   switch (token->type)
10019     {
10020     case CPP_KEYWORD:
10021       {
10022         enum tree_code op;
10023
10024         /* The keyword should be either `new' or `delete'.  */
10025         if (token->keyword == RID_NEW)
10026           op = NEW_EXPR;
10027         else if (token->keyword == RID_DELETE)
10028           op = DELETE_EXPR;
10029         else
10030           break;
10031
10032         /* Consume the `new' or `delete' token.  */
10033         cp_lexer_consume_token (parser->lexer);
10034
10035         /* Peek at the next token.  */
10036         token = cp_lexer_peek_token (parser->lexer);
10037         /* If it's a `[' token then this is the array variant of the
10038            operator.  */
10039         if (token->type == CPP_OPEN_SQUARE)
10040           {
10041             /* Consume the `[' token.  */
10042             cp_lexer_consume_token (parser->lexer);
10043             /* Look for the `]' token.  */
10044             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10045             id = ansi_opname (op == NEW_EXPR
10046                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10047           }
10048         /* Otherwise, we have the non-array variant.  */
10049         else
10050           id = ansi_opname (op);
10051
10052         return id;
10053       }
10054
10055     case CPP_PLUS:
10056       id = ansi_opname (PLUS_EXPR);
10057       break;
10058
10059     case CPP_MINUS:
10060       id = ansi_opname (MINUS_EXPR);
10061       break;
10062
10063     case CPP_MULT:
10064       id = ansi_opname (MULT_EXPR);
10065       break;
10066
10067     case CPP_DIV:
10068       id = ansi_opname (TRUNC_DIV_EXPR);
10069       break;
10070
10071     case CPP_MOD:
10072       id = ansi_opname (TRUNC_MOD_EXPR);
10073       break;
10074
10075     case CPP_XOR:
10076       id = ansi_opname (BIT_XOR_EXPR);
10077       break;
10078
10079     case CPP_AND:
10080       id = ansi_opname (BIT_AND_EXPR);
10081       break;
10082
10083     case CPP_OR:
10084       id = ansi_opname (BIT_IOR_EXPR);
10085       break;
10086
10087     case CPP_COMPL:
10088       id = ansi_opname (BIT_NOT_EXPR);
10089       break;
10090
10091     case CPP_NOT:
10092       id = ansi_opname (TRUTH_NOT_EXPR);
10093       break;
10094
10095     case CPP_EQ:
10096       id = ansi_assopname (NOP_EXPR);
10097       break;
10098
10099     case CPP_LESS:
10100       id = ansi_opname (LT_EXPR);
10101       break;
10102
10103     case CPP_GREATER:
10104       id = ansi_opname (GT_EXPR);
10105       break;
10106
10107     case CPP_PLUS_EQ:
10108       id = ansi_assopname (PLUS_EXPR);
10109       break;
10110
10111     case CPP_MINUS_EQ:
10112       id = ansi_assopname (MINUS_EXPR);
10113       break;
10114
10115     case CPP_MULT_EQ:
10116       id = ansi_assopname (MULT_EXPR);
10117       break;
10118
10119     case CPP_DIV_EQ:
10120       id = ansi_assopname (TRUNC_DIV_EXPR);
10121       break;
10122
10123     case CPP_MOD_EQ:
10124       id = ansi_assopname (TRUNC_MOD_EXPR);
10125       break;
10126
10127     case CPP_XOR_EQ:
10128       id = ansi_assopname (BIT_XOR_EXPR);
10129       break;
10130
10131     case CPP_AND_EQ:
10132       id = ansi_assopname (BIT_AND_EXPR);
10133       break;
10134
10135     case CPP_OR_EQ:
10136       id = ansi_assopname (BIT_IOR_EXPR);
10137       break;
10138
10139     case CPP_LSHIFT:
10140       id = ansi_opname (LSHIFT_EXPR);
10141       break;
10142
10143     case CPP_RSHIFT:
10144       id = ansi_opname (RSHIFT_EXPR);
10145       break;
10146
10147     case CPP_LSHIFT_EQ:
10148       id = ansi_assopname (LSHIFT_EXPR);
10149       break;
10150
10151     case CPP_RSHIFT_EQ:
10152       id = ansi_assopname (RSHIFT_EXPR);
10153       break;
10154
10155     case CPP_EQ_EQ:
10156       id = ansi_opname (EQ_EXPR);
10157       break;
10158
10159     case CPP_NOT_EQ:
10160       id = ansi_opname (NE_EXPR);
10161       break;
10162
10163     case CPP_LESS_EQ:
10164       id = ansi_opname (LE_EXPR);
10165       break;
10166
10167     case CPP_GREATER_EQ:
10168       id = ansi_opname (GE_EXPR);
10169       break;
10170
10171     case CPP_AND_AND:
10172       id = ansi_opname (TRUTH_ANDIF_EXPR);
10173       break;
10174
10175     case CPP_OR_OR:
10176       id = ansi_opname (TRUTH_ORIF_EXPR);
10177       break;
10178
10179     case CPP_PLUS_PLUS:
10180       id = ansi_opname (POSTINCREMENT_EXPR);
10181       break;
10182
10183     case CPP_MINUS_MINUS:
10184       id = ansi_opname (PREDECREMENT_EXPR);
10185       break;
10186
10187     case CPP_COMMA:
10188       id = ansi_opname (COMPOUND_EXPR);
10189       break;
10190
10191     case CPP_DEREF_STAR:
10192       id = ansi_opname (MEMBER_REF);
10193       break;
10194
10195     case CPP_DEREF:
10196       id = ansi_opname (COMPONENT_REF);
10197       break;
10198
10199     case CPP_OPEN_PAREN:
10200       /* Consume the `('.  */
10201       cp_lexer_consume_token (parser->lexer);
10202       /* Look for the matching `)'.  */
10203       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10204       return ansi_opname (CALL_EXPR);
10205
10206     case CPP_OPEN_SQUARE:
10207       /* Consume the `['.  */
10208       cp_lexer_consume_token (parser->lexer);
10209       /* Look for the matching `]'.  */
10210       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10211       return ansi_opname (ARRAY_REF);
10212
10213     default:
10214       /* Anything else is an error.  */
10215       break;
10216     }
10217
10218   /* If we have selected an identifier, we need to consume the
10219      operator token.  */
10220   if (id)
10221     cp_lexer_consume_token (parser->lexer);
10222   /* Otherwise, no valid operator name was present.  */
10223   else
10224     {
10225       cp_parser_error (parser, "expected operator");
10226       id = error_mark_node;
10227     }
10228
10229   return id;
10230 }
10231
10232 /* Parse a template-declaration.
10233
10234    template-declaration:
10235      export [opt] template < template-parameter-list > declaration
10236
10237    If MEMBER_P is TRUE, this template-declaration occurs within a
10238    class-specifier.
10239
10240    The grammar rule given by the standard isn't correct.  What
10241    is really meant is:
10242
10243    template-declaration:
10244      export [opt] template-parameter-list-seq
10245        decl-specifier-seq [opt] init-declarator [opt] ;
10246      export [opt] template-parameter-list-seq
10247        function-definition
10248
10249    template-parameter-list-seq:
10250      template-parameter-list-seq [opt]
10251      template < template-parameter-list >  */
10252
10253 static void
10254 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10255 {
10256   /* Check for `export'.  */
10257   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10258     {
10259       /* Consume the `export' token.  */
10260       cp_lexer_consume_token (parser->lexer);
10261       /* Warn that we do not support `export'.  */
10262       warning (0, "keyword %<export%> not implemented, and will be ignored");
10263     }
10264
10265   cp_parser_template_declaration_after_export (parser, member_p);
10266 }
10267
10268 /* Parse a template-parameter-list.
10269
10270    template-parameter-list:
10271      template-parameter
10272      template-parameter-list , template-parameter
10273
10274    Returns a TREE_LIST.  Each node represents a template parameter.
10275    The nodes are connected via their TREE_CHAINs.  */
10276
10277 static tree
10278 cp_parser_template_parameter_list (cp_parser* parser)
10279 {
10280   tree parameter_list = NULL_TREE;
10281
10282   begin_template_parm_list ();
10283   while (true)
10284     {
10285       tree parameter;
10286       bool is_non_type;
10287       bool is_parameter_pack;
10288       location_t parm_loc;
10289
10290       /* Parse the template-parameter.  */
10291       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10292       parameter = cp_parser_template_parameter (parser, 
10293                                                 &is_non_type,
10294                                                 &is_parameter_pack);
10295       /* Add it to the list.  */
10296       if (parameter != error_mark_node)
10297         parameter_list = process_template_parm (parameter_list,
10298                                                 parm_loc,
10299                                                 parameter,
10300                                                 is_non_type,
10301                                                 is_parameter_pack);
10302       else
10303        {
10304          tree err_parm = build_tree_list (parameter, parameter);
10305          TREE_VALUE (err_parm) = error_mark_node;
10306          parameter_list = chainon (parameter_list, err_parm);
10307        }
10308
10309       /* If the next token is not a `,', we're done.  */
10310       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10311         break;
10312       /* Otherwise, consume the `,' token.  */
10313       cp_lexer_consume_token (parser->lexer);
10314     }
10315
10316   return end_template_parm_list (parameter_list);
10317 }
10318
10319 /* Parse a template-parameter.
10320
10321    template-parameter:
10322      type-parameter
10323      parameter-declaration
10324
10325    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
10326    the parameter.  The TREE_PURPOSE is the default value, if any.
10327    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
10328    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
10329    set to true iff this parameter is a parameter pack. */
10330
10331 static tree
10332 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10333                               bool *is_parameter_pack)
10334 {
10335   cp_token *token;
10336   cp_parameter_declarator *parameter_declarator;
10337   cp_declarator *id_declarator;
10338   tree parm;
10339
10340   /* Assume it is a type parameter or a template parameter.  */
10341   *is_non_type = false;
10342   /* Assume it not a parameter pack. */
10343   *is_parameter_pack = false;
10344   /* Peek at the next token.  */
10345   token = cp_lexer_peek_token (parser->lexer);
10346   /* If it is `class' or `template', we have a type-parameter.  */
10347   if (token->keyword == RID_TEMPLATE)
10348     return cp_parser_type_parameter (parser, is_parameter_pack);
10349   /* If it is `class' or `typename' we do not know yet whether it is a
10350      type parameter or a non-type parameter.  Consider:
10351
10352        template <typename T, typename T::X X> ...
10353
10354      or:
10355
10356        template <class C, class D*> ...
10357
10358      Here, the first parameter is a type parameter, and the second is
10359      a non-type parameter.  We can tell by looking at the token after
10360      the identifier -- if it is a `,', `=', or `>' then we have a type
10361      parameter.  */
10362   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10363     {
10364       /* Peek at the token after `class' or `typename'.  */
10365       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10366       /* If it's an ellipsis, we have a template type parameter
10367          pack. */
10368       if (token->type == CPP_ELLIPSIS)
10369         return cp_parser_type_parameter (parser, is_parameter_pack);
10370       /* If it's an identifier, skip it.  */
10371       if (token->type == CPP_NAME)
10372         token = cp_lexer_peek_nth_token (parser->lexer, 3);
10373       /* Now, see if the token looks like the end of a template
10374          parameter.  */
10375       if (token->type == CPP_COMMA
10376           || token->type == CPP_EQ
10377           || token->type == CPP_GREATER)
10378         return cp_parser_type_parameter (parser, is_parameter_pack);
10379     }
10380
10381   /* Otherwise, it is a non-type parameter.
10382
10383      [temp.param]
10384
10385      When parsing a default template-argument for a non-type
10386      template-parameter, the first non-nested `>' is taken as the end
10387      of the template parameter-list rather than a greater-than
10388      operator.  */
10389   *is_non_type = true;
10390   parameter_declarator
10391      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10392                                         /*parenthesized_p=*/NULL);
10393
10394   /* If the parameter declaration is marked as a parameter pack, set
10395      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10396      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10397      grokdeclarator. */
10398   if (parameter_declarator
10399       && parameter_declarator->declarator
10400       && parameter_declarator->declarator->parameter_pack_p)
10401     {
10402       *is_parameter_pack = true;
10403       parameter_declarator->declarator->parameter_pack_p = false;
10404     }
10405
10406   /* If the next token is an ellipsis, and we don't already have it
10407      marked as a parameter pack, then we have a parameter pack (that
10408      has no declarator).  */
10409   if (!*is_parameter_pack
10410       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10411       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10412     {
10413       /* Consume the `...'.  */
10414       cp_lexer_consume_token (parser->lexer);
10415       maybe_warn_variadic_templates ();
10416       
10417       *is_parameter_pack = true;
10418     }
10419   /* We might end up with a pack expansion as the type of the non-type
10420      template parameter, in which case this is a non-type template
10421      parameter pack.  */
10422   else if (parameter_declarator
10423            && parameter_declarator->decl_specifiers.type
10424            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10425     {
10426       *is_parameter_pack = true;
10427       parameter_declarator->decl_specifiers.type = 
10428         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10429     }
10430
10431   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10432     {
10433       /* Parameter packs cannot have default arguments.  However, a
10434          user may try to do so, so we'll parse them and give an
10435          appropriate diagnostic here.  */
10436
10437       /* Consume the `='.  */
10438       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10439       cp_lexer_consume_token (parser->lexer);
10440       
10441       /* Find the name of the parameter pack.  */     
10442       id_declarator = parameter_declarator->declarator;
10443       while (id_declarator && id_declarator->kind != cdk_id)
10444         id_declarator = id_declarator->declarator;
10445       
10446       if (id_declarator && id_declarator->kind == cdk_id)
10447         error_at (start_token->location,
10448                   "template parameter pack %qD cannot have a default argument",
10449                   id_declarator->u.id.unqualified_name);
10450       else
10451         error_at (start_token->location,
10452                   "template parameter pack cannot have a default argument");
10453       
10454       /* Parse the default argument, but throw away the result.  */
10455       cp_parser_default_argument (parser, /*template_parm_p=*/true);
10456     }
10457
10458   parm = grokdeclarator (parameter_declarator->declarator,
10459                          &parameter_declarator->decl_specifiers,
10460                          PARM, /*initialized=*/0,
10461                          /*attrlist=*/NULL);
10462   if (parm == error_mark_node)
10463     return error_mark_node;
10464
10465   return build_tree_list (parameter_declarator->default_argument, parm);
10466 }
10467
10468 /* Parse a type-parameter.
10469
10470    type-parameter:
10471      class identifier [opt]
10472      class identifier [opt] = type-id
10473      typename identifier [opt]
10474      typename identifier [opt] = type-id
10475      template < template-parameter-list > class identifier [opt]
10476      template < template-parameter-list > class identifier [opt]
10477        = id-expression
10478
10479    GNU Extension (variadic templates):
10480
10481    type-parameter:
10482      class ... identifier [opt]
10483      typename ... identifier [opt]
10484
10485    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
10486    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
10487    the declaration of the parameter.
10488
10489    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10490
10491 static tree
10492 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10493 {
10494   cp_token *token;
10495   tree parameter;
10496
10497   /* Look for a keyword to tell us what kind of parameter this is.  */
10498   token = cp_parser_require (parser, CPP_KEYWORD,
10499                              "%<class%>, %<typename%>, or %<template%>");
10500   if (!token)
10501     return error_mark_node;
10502
10503   switch (token->keyword)
10504     {
10505     case RID_CLASS:
10506     case RID_TYPENAME:
10507       {
10508         tree identifier;
10509         tree default_argument;
10510
10511         /* If the next token is an ellipsis, we have a template
10512            argument pack. */
10513         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10514           {
10515             /* Consume the `...' token. */
10516             cp_lexer_consume_token (parser->lexer);
10517             maybe_warn_variadic_templates ();
10518
10519             *is_parameter_pack = true;
10520           }
10521
10522         /* If the next token is an identifier, then it names the
10523            parameter.  */
10524         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10525           identifier = cp_parser_identifier (parser);
10526         else
10527           identifier = NULL_TREE;
10528
10529         /* Create the parameter.  */
10530         parameter = finish_template_type_parm (class_type_node, identifier);
10531
10532         /* If the next token is an `=', we have a default argument.  */
10533         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10534           {
10535             /* Consume the `=' token.  */
10536             cp_lexer_consume_token (parser->lexer);
10537             /* Parse the default-argument.  */
10538             push_deferring_access_checks (dk_no_deferred);
10539             default_argument = cp_parser_type_id (parser);
10540
10541             /* Template parameter packs cannot have default
10542                arguments. */
10543             if (*is_parameter_pack)
10544               {
10545                 if (identifier)
10546                   error_at (token->location,
10547                             "template parameter pack %qD cannot have a "
10548                             "default argument", identifier);
10549                 else
10550                   error_at (token->location,
10551                             "template parameter packs cannot have "
10552                             "default arguments");
10553                 default_argument = NULL_TREE;
10554               }
10555             pop_deferring_access_checks ();
10556           }
10557         else
10558           default_argument = NULL_TREE;
10559
10560         /* Create the combined representation of the parameter and the
10561            default argument.  */
10562         parameter = build_tree_list (default_argument, parameter);
10563       }
10564       break;
10565
10566     case RID_TEMPLATE:
10567       {
10568         tree parameter_list;
10569         tree identifier;
10570         tree default_argument;
10571
10572         /* Look for the `<'.  */
10573         cp_parser_require (parser, CPP_LESS, "%<<%>");
10574         /* Parse the template-parameter-list.  */
10575         parameter_list = cp_parser_template_parameter_list (parser);
10576         /* Look for the `>'.  */
10577         cp_parser_require (parser, CPP_GREATER, "%<>%>");
10578         /* Look for the `class' keyword.  */
10579         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10580         /* If the next token is an ellipsis, we have a template
10581            argument pack. */
10582         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10583           {
10584             /* Consume the `...' token. */
10585             cp_lexer_consume_token (parser->lexer);
10586             maybe_warn_variadic_templates ();
10587
10588             *is_parameter_pack = true;
10589           }
10590         /* If the next token is an `=', then there is a
10591            default-argument.  If the next token is a `>', we are at
10592            the end of the parameter-list.  If the next token is a `,',
10593            then we are at the end of this parameter.  */
10594         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10595             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10596             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10597           {
10598             identifier = cp_parser_identifier (parser);
10599             /* Treat invalid names as if the parameter were nameless.  */
10600             if (identifier == error_mark_node)
10601               identifier = NULL_TREE;
10602           }
10603         else
10604           identifier = NULL_TREE;
10605
10606         /* Create the template parameter.  */
10607         parameter = finish_template_template_parm (class_type_node,
10608                                                    identifier);
10609
10610         /* If the next token is an `=', then there is a
10611            default-argument.  */
10612         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10613           {
10614             bool is_template;
10615
10616             /* Consume the `='.  */
10617             cp_lexer_consume_token (parser->lexer);
10618             /* Parse the id-expression.  */
10619             push_deferring_access_checks (dk_no_deferred);
10620             /* save token before parsing the id-expression, for error
10621                reporting */
10622             token = cp_lexer_peek_token (parser->lexer);
10623             default_argument
10624               = cp_parser_id_expression (parser,
10625                                          /*template_keyword_p=*/false,
10626                                          /*check_dependency_p=*/true,
10627                                          /*template_p=*/&is_template,
10628                                          /*declarator_p=*/false,
10629                                          /*optional_p=*/false);
10630             if (TREE_CODE (default_argument) == TYPE_DECL)
10631               /* If the id-expression was a template-id that refers to
10632                  a template-class, we already have the declaration here,
10633                  so no further lookup is needed.  */
10634                  ;
10635             else
10636               /* Look up the name.  */
10637               default_argument
10638                 = cp_parser_lookup_name (parser, default_argument,
10639                                          none_type,
10640                                          /*is_template=*/is_template,
10641                                          /*is_namespace=*/false,
10642                                          /*check_dependency=*/true,
10643                                          /*ambiguous_decls=*/NULL,
10644                                          token->location);
10645             /* See if the default argument is valid.  */
10646             default_argument
10647               = check_template_template_default_arg (default_argument);
10648
10649             /* Template parameter packs cannot have default
10650                arguments. */
10651             if (*is_parameter_pack)
10652               {
10653                 if (identifier)
10654                   error_at (token->location,
10655                             "template parameter pack %qD cannot "
10656                             "have a default argument",
10657                             identifier);
10658                 else
10659                   error_at (token->location, "template parameter packs cannot "
10660                             "have default arguments");
10661                 default_argument = NULL_TREE;
10662               }
10663             pop_deferring_access_checks ();
10664           }
10665         else
10666           default_argument = NULL_TREE;
10667
10668         /* Create the combined representation of the parameter and the
10669            default argument.  */
10670         parameter = build_tree_list (default_argument, parameter);
10671       }
10672       break;
10673
10674     default:
10675       gcc_unreachable ();
10676       break;
10677     }
10678
10679   return parameter;
10680 }
10681
10682 /* Parse a template-id.
10683
10684    template-id:
10685      template-name < template-argument-list [opt] >
10686
10687    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10688    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10689    returned.  Otherwise, if the template-name names a function, or set
10690    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10691    names a class, returns a TYPE_DECL for the specialization.
10692
10693    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10694    uninstantiated templates.  */
10695
10696 static tree
10697 cp_parser_template_id (cp_parser *parser,
10698                        bool template_keyword_p,
10699                        bool check_dependency_p,
10700                        bool is_declaration)
10701 {
10702   int i;
10703   tree templ;
10704   tree arguments;
10705   tree template_id;
10706   cp_token_position start_of_id = 0;
10707   deferred_access_check *chk;
10708   VEC (deferred_access_check,gc) *access_check;
10709   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10710   bool is_identifier;
10711
10712   /* If the next token corresponds to a template-id, there is no need
10713      to reparse it.  */
10714   next_token = cp_lexer_peek_token (parser->lexer);
10715   if (next_token->type == CPP_TEMPLATE_ID)
10716     {
10717       struct tree_check *check_value;
10718
10719       /* Get the stored value.  */
10720       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10721       /* Perform any access checks that were deferred.  */
10722       access_check = check_value->checks;
10723       if (access_check)
10724         {
10725           for (i = 0 ;
10726                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10727                ++i)
10728             {
10729               perform_or_defer_access_check (chk->binfo,
10730                                              chk->decl,
10731                                              chk->diag_decl);
10732             }
10733         }
10734       /* Return the stored value.  */
10735       return check_value->value;
10736     }
10737
10738   /* Avoid performing name lookup if there is no possibility of
10739      finding a template-id.  */
10740   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10741       || (next_token->type == CPP_NAME
10742           && !cp_parser_nth_token_starts_template_argument_list_p
10743                (parser, 2)))
10744     {
10745       cp_parser_error (parser, "expected template-id");
10746       return error_mark_node;
10747     }
10748
10749   /* Remember where the template-id starts.  */
10750   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10751     start_of_id = cp_lexer_token_position (parser->lexer, false);
10752
10753   push_deferring_access_checks (dk_deferred);
10754
10755   /* Parse the template-name.  */
10756   is_identifier = false;
10757   token = cp_lexer_peek_token (parser->lexer);
10758   templ = cp_parser_template_name (parser, template_keyword_p,
10759                                    check_dependency_p,
10760                                    is_declaration,
10761                                    &is_identifier);
10762   if (templ == error_mark_node || is_identifier)
10763     {
10764       pop_deferring_access_checks ();
10765       return templ;
10766     }
10767
10768   /* If we find the sequence `[:' after a template-name, it's probably
10769      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10770      parse correctly the argument list.  */
10771   next_token = cp_lexer_peek_token (parser->lexer);
10772   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10773   if (next_token->type == CPP_OPEN_SQUARE
10774       && next_token->flags & DIGRAPH
10775       && next_token_2->type == CPP_COLON
10776       && !(next_token_2->flags & PREV_WHITE))
10777     {
10778       cp_parser_parse_tentatively (parser);
10779       /* Change `:' into `::'.  */
10780       next_token_2->type = CPP_SCOPE;
10781       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10782          CPP_LESS.  */
10783       cp_lexer_consume_token (parser->lexer);
10784
10785       /* Parse the arguments.  */
10786       arguments = cp_parser_enclosed_template_argument_list (parser);
10787       if (!cp_parser_parse_definitely (parser))
10788         {
10789           /* If we couldn't parse an argument list, then we revert our changes
10790              and return simply an error. Maybe this is not a template-id
10791              after all.  */
10792           next_token_2->type = CPP_COLON;
10793           cp_parser_error (parser, "expected %<<%>");
10794           pop_deferring_access_checks ();
10795           return error_mark_node;
10796         }
10797       /* Otherwise, emit an error about the invalid digraph, but continue
10798          parsing because we got our argument list.  */
10799       if (permerror (next_token->location,
10800                      "%<<::%> cannot begin a template-argument list"))
10801         {
10802           static bool hint = false;
10803           inform (next_token->location,
10804                   "%<<:%> is an alternate spelling for %<[%>."
10805                   " Insert whitespace between %<<%> and %<::%>");
10806           if (!hint && !flag_permissive)
10807             {
10808               inform (next_token->location, "(if you use %<-fpermissive%>"
10809                       " G++ will accept your code)");
10810               hint = true;
10811             }
10812         }
10813     }
10814   else
10815     {
10816       /* Look for the `<' that starts the template-argument-list.  */
10817       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10818         {
10819           pop_deferring_access_checks ();
10820           return error_mark_node;
10821         }
10822       /* Parse the arguments.  */
10823       arguments = cp_parser_enclosed_template_argument_list (parser);
10824     }
10825
10826   /* Build a representation of the specialization.  */
10827   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10828     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10829   else if (DECL_CLASS_TEMPLATE_P (templ)
10830            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10831     {
10832       bool entering_scope;
10833       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10834          template (rather than some instantiation thereof) only if
10835          is not nested within some other construct.  For example, in
10836          "template <typename T> void f(T) { A<T>::", A<T> is just an
10837          instantiation of A.  */
10838       entering_scope = (template_parm_scope_p ()
10839                         && cp_lexer_next_token_is (parser->lexer,
10840                                                    CPP_SCOPE));
10841       template_id
10842         = finish_template_type (templ, arguments, entering_scope);
10843     }
10844   else
10845     {
10846       /* If it's not a class-template or a template-template, it should be
10847          a function-template.  */
10848       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10849                    || TREE_CODE (templ) == OVERLOAD
10850                    || BASELINK_P (templ)));
10851
10852       template_id = lookup_template_function (templ, arguments);
10853     }
10854
10855   /* If parsing tentatively, replace the sequence of tokens that makes
10856      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10857      should we re-parse the token stream, we will not have to repeat
10858      the effort required to do the parse, nor will we issue duplicate
10859      error messages about problems during instantiation of the
10860      template.  */
10861   if (start_of_id)
10862     {
10863       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10864
10865       /* Reset the contents of the START_OF_ID token.  */
10866       token->type = CPP_TEMPLATE_ID;
10867       /* Retrieve any deferred checks.  Do not pop this access checks yet
10868          so the memory will not be reclaimed during token replacing below.  */
10869       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10870       token->u.tree_check_value->value = template_id;
10871       token->u.tree_check_value->checks = get_deferred_access_checks ();
10872       token->keyword = RID_MAX;
10873
10874       /* Purge all subsequent tokens.  */
10875       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10876
10877       /* ??? Can we actually assume that, if template_id ==
10878          error_mark_node, we will have issued a diagnostic to the
10879          user, as opposed to simply marking the tentative parse as
10880          failed?  */
10881       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10882         error_at (token->location, "parse error in template argument list");
10883     }
10884
10885   pop_deferring_access_checks ();
10886   return template_id;
10887 }
10888
10889 /* Parse a template-name.
10890
10891    template-name:
10892      identifier
10893
10894    The standard should actually say:
10895
10896    template-name:
10897      identifier
10898      operator-function-id
10899
10900    A defect report has been filed about this issue.
10901
10902    A conversion-function-id cannot be a template name because they cannot
10903    be part of a template-id. In fact, looking at this code:
10904
10905    a.operator K<int>()
10906
10907    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10908    It is impossible to call a templated conversion-function-id with an
10909    explicit argument list, since the only allowed template parameter is
10910    the type to which it is converting.
10911
10912    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10913    `template' keyword, in a construction like:
10914
10915      T::template f<3>()
10916
10917    In that case `f' is taken to be a template-name, even though there
10918    is no way of knowing for sure.
10919
10920    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10921    name refers to a set of overloaded functions, at least one of which
10922    is a template, or an IDENTIFIER_NODE with the name of the template,
10923    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10924    names are looked up inside uninstantiated templates.  */
10925
10926 static tree
10927 cp_parser_template_name (cp_parser* parser,
10928                          bool template_keyword_p,
10929                          bool check_dependency_p,
10930                          bool is_declaration,
10931                          bool *is_identifier)
10932 {
10933   tree identifier;
10934   tree decl;
10935   tree fns;
10936   cp_token *token = cp_lexer_peek_token (parser->lexer);
10937
10938   /* If the next token is `operator', then we have either an
10939      operator-function-id or a conversion-function-id.  */
10940   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10941     {
10942       /* We don't know whether we're looking at an
10943          operator-function-id or a conversion-function-id.  */
10944       cp_parser_parse_tentatively (parser);
10945       /* Try an operator-function-id.  */
10946       identifier = cp_parser_operator_function_id (parser);
10947       /* If that didn't work, try a conversion-function-id.  */
10948       if (!cp_parser_parse_definitely (parser))
10949         {
10950           cp_parser_error (parser, "expected template-name");
10951           return error_mark_node;
10952         }
10953     }
10954   /* Look for the identifier.  */
10955   else
10956     identifier = cp_parser_identifier (parser);
10957
10958   /* If we didn't find an identifier, we don't have a template-id.  */
10959   if (identifier == error_mark_node)
10960     return error_mark_node;
10961
10962   /* If the name immediately followed the `template' keyword, then it
10963      is a template-name.  However, if the next token is not `<', then
10964      we do not treat it as a template-name, since it is not being used
10965      as part of a template-id.  This enables us to handle constructs
10966      like:
10967
10968        template <typename T> struct S { S(); };
10969        template <typename T> S<T>::S();
10970
10971      correctly.  We would treat `S' as a template -- if it were `S<T>'
10972      -- but we do not if there is no `<'.  */
10973
10974   if (processing_template_decl
10975       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10976     {
10977       /* In a declaration, in a dependent context, we pretend that the
10978          "template" keyword was present in order to improve error
10979          recovery.  For example, given:
10980
10981            template <typename T> void f(T::X<int>);
10982
10983          we want to treat "X<int>" as a template-id.  */
10984       if (is_declaration
10985           && !template_keyword_p
10986           && parser->scope && TYPE_P (parser->scope)
10987           && check_dependency_p
10988           && dependent_scope_p (parser->scope)
10989           /* Do not do this for dtors (or ctors), since they never
10990              need the template keyword before their name.  */
10991           && !constructor_name_p (identifier, parser->scope))
10992         {
10993           cp_token_position start = 0;
10994
10995           /* Explain what went wrong.  */
10996           error_at (token->location, "non-template %qD used as template",
10997                     identifier);
10998           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
10999                   parser->scope, identifier);
11000           /* If parsing tentatively, find the location of the "<" token.  */
11001           if (cp_parser_simulate_error (parser))
11002             start = cp_lexer_token_position (parser->lexer, true);
11003           /* Parse the template arguments so that we can issue error
11004              messages about them.  */
11005           cp_lexer_consume_token (parser->lexer);
11006           cp_parser_enclosed_template_argument_list (parser);
11007           /* Skip tokens until we find a good place from which to
11008              continue parsing.  */
11009           cp_parser_skip_to_closing_parenthesis (parser,
11010                                                  /*recovering=*/true,
11011                                                  /*or_comma=*/true,
11012                                                  /*consume_paren=*/false);
11013           /* If parsing tentatively, permanently remove the
11014              template argument list.  That will prevent duplicate
11015              error messages from being issued about the missing
11016              "template" keyword.  */
11017           if (start)
11018             cp_lexer_purge_tokens_after (parser->lexer, start);
11019           if (is_identifier)
11020             *is_identifier = true;
11021           return identifier;
11022         }
11023
11024       /* If the "template" keyword is present, then there is generally
11025          no point in doing name-lookup, so we just return IDENTIFIER.
11026          But, if the qualifying scope is non-dependent then we can
11027          (and must) do name-lookup normally.  */
11028       if (template_keyword_p
11029           && (!parser->scope
11030               || (TYPE_P (parser->scope)
11031                   && dependent_type_p (parser->scope))))
11032         return identifier;
11033     }
11034
11035   /* Look up the name.  */
11036   decl = cp_parser_lookup_name (parser, identifier,
11037                                 none_type,
11038                                 /*is_template=*/false,
11039                                 /*is_namespace=*/false,
11040                                 check_dependency_p,
11041                                 /*ambiguous_decls=*/NULL,
11042                                 token->location);
11043   decl = maybe_get_template_decl_from_type_decl (decl);
11044
11045   /* If DECL is a template, then the name was a template-name.  */
11046   if (TREE_CODE (decl) == TEMPLATE_DECL)
11047     ;
11048   else
11049     {
11050       tree fn = NULL_TREE;
11051
11052       /* The standard does not explicitly indicate whether a name that
11053          names a set of overloaded declarations, some of which are
11054          templates, is a template-name.  However, such a name should
11055          be a template-name; otherwise, there is no way to form a
11056          template-id for the overloaded templates.  */
11057       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11058       if (TREE_CODE (fns) == OVERLOAD)
11059         for (fn = fns; fn; fn = OVL_NEXT (fn))
11060           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11061             break;
11062
11063       if (!fn)
11064         {
11065           /* The name does not name a template.  */
11066           cp_parser_error (parser, "expected template-name");
11067           return error_mark_node;
11068         }
11069     }
11070
11071   /* If DECL is dependent, and refers to a function, then just return
11072      its name; we will look it up again during template instantiation.  */
11073   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11074     {
11075       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11076       if (TYPE_P (scope) && dependent_type_p (scope))
11077         return identifier;
11078     }
11079
11080   return decl;
11081 }
11082
11083 /* Parse a template-argument-list.
11084
11085    template-argument-list:
11086      template-argument ... [opt]
11087      template-argument-list , template-argument ... [opt]
11088
11089    Returns a TREE_VEC containing the arguments.  */
11090
11091 static tree
11092 cp_parser_template_argument_list (cp_parser* parser)
11093 {
11094   tree fixed_args[10];
11095   unsigned n_args = 0;
11096   unsigned alloced = 10;
11097   tree *arg_ary = fixed_args;
11098   tree vec;
11099   bool saved_in_template_argument_list_p;
11100   bool saved_ice_p;
11101   bool saved_non_ice_p;
11102
11103   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11104   parser->in_template_argument_list_p = true;
11105   /* Even if the template-id appears in an integral
11106      constant-expression, the contents of the argument list do
11107      not.  */
11108   saved_ice_p = parser->integral_constant_expression_p;
11109   parser->integral_constant_expression_p = false;
11110   saved_non_ice_p = parser->non_integral_constant_expression_p;
11111   parser->non_integral_constant_expression_p = false;
11112   /* Parse the arguments.  */
11113   do
11114     {
11115       tree argument;
11116
11117       if (n_args)
11118         /* Consume the comma.  */
11119         cp_lexer_consume_token (parser->lexer);
11120
11121       /* Parse the template-argument.  */
11122       argument = cp_parser_template_argument (parser);
11123
11124       /* If the next token is an ellipsis, we're expanding a template
11125          argument pack. */
11126       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11127         {
11128           if (argument == error_mark_node)
11129             {
11130               cp_token *token = cp_lexer_peek_token (parser->lexer);
11131               error_at (token->location,
11132                         "expected parameter pack before %<...%>");
11133             }
11134           /* Consume the `...' token. */
11135           cp_lexer_consume_token (parser->lexer);
11136
11137           /* Make the argument into a TYPE_PACK_EXPANSION or
11138              EXPR_PACK_EXPANSION. */
11139           argument = make_pack_expansion (argument);
11140         }
11141
11142       if (n_args == alloced)
11143         {
11144           alloced *= 2;
11145
11146           if (arg_ary == fixed_args)
11147             {
11148               arg_ary = XNEWVEC (tree, alloced);
11149               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11150             }
11151           else
11152             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11153         }
11154       arg_ary[n_args++] = argument;
11155     }
11156   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11157
11158   vec = make_tree_vec (n_args);
11159
11160   while (n_args--)
11161     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11162
11163   if (arg_ary != fixed_args)
11164     free (arg_ary);
11165   parser->non_integral_constant_expression_p = saved_non_ice_p;
11166   parser->integral_constant_expression_p = saved_ice_p;
11167   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11168   return vec;
11169 }
11170
11171 /* Parse a template-argument.
11172
11173    template-argument:
11174      assignment-expression
11175      type-id
11176      id-expression
11177
11178    The representation is that of an assignment-expression, type-id, or
11179    id-expression -- except that the qualified id-expression is
11180    evaluated, so that the value returned is either a DECL or an
11181    OVERLOAD.
11182
11183    Although the standard says "assignment-expression", it forbids
11184    throw-expressions or assignments in the template argument.
11185    Therefore, we use "conditional-expression" instead.  */
11186
11187 static tree
11188 cp_parser_template_argument (cp_parser* parser)
11189 {
11190   tree argument;
11191   bool template_p;
11192   bool address_p;
11193   bool maybe_type_id = false;
11194   cp_token *token = NULL, *argument_start_token = NULL;
11195   cp_id_kind idk;
11196
11197   /* There's really no way to know what we're looking at, so we just
11198      try each alternative in order.
11199
11200        [temp.arg]
11201
11202        In a template-argument, an ambiguity between a type-id and an
11203        expression is resolved to a type-id, regardless of the form of
11204        the corresponding template-parameter.
11205
11206      Therefore, we try a type-id first.  */
11207   cp_parser_parse_tentatively (parser);
11208   argument = cp_parser_template_type_arg (parser);
11209   /* If there was no error parsing the type-id but the next token is a
11210      '>>', our behavior depends on which dialect of C++ we're
11211      parsing. In C++98, we probably found a typo for '> >'. But there
11212      are type-id which are also valid expressions. For instance:
11213
11214      struct X { int operator >> (int); };
11215      template <int V> struct Foo {};
11216      Foo<X () >> 5> r;
11217
11218      Here 'X()' is a valid type-id of a function type, but the user just
11219      wanted to write the expression "X() >> 5". Thus, we remember that we
11220      found a valid type-id, but we still try to parse the argument as an
11221      expression to see what happens. 
11222
11223      In C++0x, the '>>' will be considered two separate '>'
11224      tokens.  */
11225   if (!cp_parser_error_occurred (parser)
11226       && cxx_dialect == cxx98
11227       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11228     {
11229       maybe_type_id = true;
11230       cp_parser_abort_tentative_parse (parser);
11231     }
11232   else
11233     {
11234       /* If the next token isn't a `,' or a `>', then this argument wasn't
11235       really finished. This means that the argument is not a valid
11236       type-id.  */
11237       if (!cp_parser_next_token_ends_template_argument_p (parser))
11238         cp_parser_error (parser, "expected template-argument");
11239       /* If that worked, we're done.  */
11240       if (cp_parser_parse_definitely (parser))
11241         return argument;
11242     }
11243   /* We're still not sure what the argument will be.  */
11244   cp_parser_parse_tentatively (parser);
11245   /* Try a template.  */
11246   argument_start_token = cp_lexer_peek_token (parser->lexer);
11247   argument = cp_parser_id_expression (parser,
11248                                       /*template_keyword_p=*/false,
11249                                       /*check_dependency_p=*/true,
11250                                       &template_p,
11251                                       /*declarator_p=*/false,
11252                                       /*optional_p=*/false);
11253   /* If the next token isn't a `,' or a `>', then this argument wasn't
11254      really finished.  */
11255   if (!cp_parser_next_token_ends_template_argument_p (parser))
11256     cp_parser_error (parser, "expected template-argument");
11257   if (!cp_parser_error_occurred (parser))
11258     {
11259       /* Figure out what is being referred to.  If the id-expression
11260          was for a class template specialization, then we will have a
11261          TYPE_DECL at this point.  There is no need to do name lookup
11262          at this point in that case.  */
11263       if (TREE_CODE (argument) != TYPE_DECL)
11264         argument = cp_parser_lookup_name (parser, argument,
11265                                           none_type,
11266                                           /*is_template=*/template_p,
11267                                           /*is_namespace=*/false,
11268                                           /*check_dependency=*/true,
11269                                           /*ambiguous_decls=*/NULL,
11270                                           argument_start_token->location);
11271       if (TREE_CODE (argument) != TEMPLATE_DECL
11272           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11273         cp_parser_error (parser, "expected template-name");
11274     }
11275   if (cp_parser_parse_definitely (parser))
11276     return argument;
11277   /* It must be a non-type argument.  There permitted cases are given
11278      in [temp.arg.nontype]:
11279
11280      -- an integral constant-expression of integral or enumeration
11281         type; or
11282
11283      -- the name of a non-type template-parameter; or
11284
11285      -- the name of an object or function with external linkage...
11286
11287      -- the address of an object or function with external linkage...
11288
11289      -- a pointer to member...  */
11290   /* Look for a non-type template parameter.  */
11291   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11292     {
11293       cp_parser_parse_tentatively (parser);
11294       argument = cp_parser_primary_expression (parser,
11295                                                /*address_p=*/false,
11296                                                /*cast_p=*/false,
11297                                                /*template_arg_p=*/true,
11298                                                &idk);
11299       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11300           || !cp_parser_next_token_ends_template_argument_p (parser))
11301         cp_parser_simulate_error (parser);
11302       if (cp_parser_parse_definitely (parser))
11303         return argument;
11304     }
11305
11306   /* If the next token is "&", the argument must be the address of an
11307      object or function with external linkage.  */
11308   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11309   if (address_p)
11310     cp_lexer_consume_token (parser->lexer);
11311   /* See if we might have an id-expression.  */
11312   token = cp_lexer_peek_token (parser->lexer);
11313   if (token->type == CPP_NAME
11314       || token->keyword == RID_OPERATOR
11315       || token->type == CPP_SCOPE
11316       || token->type == CPP_TEMPLATE_ID
11317       || token->type == CPP_NESTED_NAME_SPECIFIER)
11318     {
11319       cp_parser_parse_tentatively (parser);
11320       argument = cp_parser_primary_expression (parser,
11321                                                address_p,
11322                                                /*cast_p=*/false,
11323                                                /*template_arg_p=*/true,
11324                                                &idk);
11325       if (cp_parser_error_occurred (parser)
11326           || !cp_parser_next_token_ends_template_argument_p (parser))
11327         cp_parser_abort_tentative_parse (parser);
11328       else
11329         {
11330           if (TREE_CODE (argument) == INDIRECT_REF)
11331             {
11332               gcc_assert (REFERENCE_REF_P (argument));
11333               argument = TREE_OPERAND (argument, 0);
11334             }
11335
11336           if (TREE_CODE (argument) == VAR_DECL)
11337             {
11338               /* A variable without external linkage might still be a
11339                  valid constant-expression, so no error is issued here
11340                  if the external-linkage check fails.  */
11341               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
11342                 cp_parser_simulate_error (parser);
11343             }
11344           else if (is_overloaded_fn (argument))
11345             /* All overloaded functions are allowed; if the external
11346                linkage test does not pass, an error will be issued
11347                later.  */
11348             ;
11349           else if (address_p
11350                    && (TREE_CODE (argument) == OFFSET_REF
11351                        || TREE_CODE (argument) == SCOPE_REF))
11352             /* A pointer-to-member.  */
11353             ;
11354           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11355             ;
11356           else
11357             cp_parser_simulate_error (parser);
11358
11359           if (cp_parser_parse_definitely (parser))
11360             {
11361               if (address_p)
11362                 argument = build_x_unary_op (ADDR_EXPR, argument,
11363                                              tf_warning_or_error);
11364               return argument;
11365             }
11366         }
11367     }
11368   /* If the argument started with "&", there are no other valid
11369      alternatives at this point.  */
11370   if (address_p)
11371     {
11372       cp_parser_error (parser, "invalid non-type template argument");
11373       return error_mark_node;
11374     }
11375
11376   /* If the argument wasn't successfully parsed as a type-id followed
11377      by '>>', the argument can only be a constant expression now.
11378      Otherwise, we try parsing the constant-expression tentatively,
11379      because the argument could really be a type-id.  */
11380   if (maybe_type_id)
11381     cp_parser_parse_tentatively (parser);
11382   argument = cp_parser_constant_expression (parser,
11383                                             /*allow_non_constant_p=*/false,
11384                                             /*non_constant_p=*/NULL);
11385   argument = fold_non_dependent_expr (argument);
11386   if (!maybe_type_id)
11387     return argument;
11388   if (!cp_parser_next_token_ends_template_argument_p (parser))
11389     cp_parser_error (parser, "expected template-argument");
11390   if (cp_parser_parse_definitely (parser))
11391     return argument;
11392   /* We did our best to parse the argument as a non type-id, but that
11393      was the only alternative that matched (albeit with a '>' after
11394      it). We can assume it's just a typo from the user, and a
11395      diagnostic will then be issued.  */
11396   return cp_parser_template_type_arg (parser);
11397 }
11398
11399 /* Parse an explicit-instantiation.
11400
11401    explicit-instantiation:
11402      template declaration
11403
11404    Although the standard says `declaration', what it really means is:
11405
11406    explicit-instantiation:
11407      template decl-specifier-seq [opt] declarator [opt] ;
11408
11409    Things like `template int S<int>::i = 5, int S<double>::j;' are not
11410    supposed to be allowed.  A defect report has been filed about this
11411    issue.
11412
11413    GNU Extension:
11414
11415    explicit-instantiation:
11416      storage-class-specifier template
11417        decl-specifier-seq [opt] declarator [opt] ;
11418      function-specifier template
11419        decl-specifier-seq [opt] declarator [opt] ;  */
11420
11421 static void
11422 cp_parser_explicit_instantiation (cp_parser* parser)
11423 {
11424   int declares_class_or_enum;
11425   cp_decl_specifier_seq decl_specifiers;
11426   tree extension_specifier = NULL_TREE;
11427   cp_token *token;
11428
11429   /* Look for an (optional) storage-class-specifier or
11430      function-specifier.  */
11431   if (cp_parser_allow_gnu_extensions_p (parser))
11432     {
11433       extension_specifier
11434         = cp_parser_storage_class_specifier_opt (parser);
11435       if (!extension_specifier)
11436         extension_specifier
11437           = cp_parser_function_specifier_opt (parser,
11438                                               /*decl_specs=*/NULL);
11439     }
11440
11441   /* Look for the `template' keyword.  */
11442   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11443   /* Let the front end know that we are processing an explicit
11444      instantiation.  */
11445   begin_explicit_instantiation ();
11446   /* [temp.explicit] says that we are supposed to ignore access
11447      control while processing explicit instantiation directives.  */
11448   push_deferring_access_checks (dk_no_check);
11449   /* Parse a decl-specifier-seq.  */
11450   token = cp_lexer_peek_token (parser->lexer);
11451   cp_parser_decl_specifier_seq (parser,
11452                                 CP_PARSER_FLAGS_OPTIONAL,
11453                                 &decl_specifiers,
11454                                 &declares_class_or_enum);
11455   /* If there was exactly one decl-specifier, and it declared a class,
11456      and there's no declarator, then we have an explicit type
11457      instantiation.  */
11458   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11459     {
11460       tree type;
11461
11462       type = check_tag_decl (&decl_specifiers);
11463       /* Turn access control back on for names used during
11464          template instantiation.  */
11465       pop_deferring_access_checks ();
11466       if (type)
11467         do_type_instantiation (type, extension_specifier,
11468                                /*complain=*/tf_error);
11469     }
11470   else
11471     {
11472       cp_declarator *declarator;
11473       tree decl;
11474
11475       /* Parse the declarator.  */
11476       declarator
11477         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11478                                 /*ctor_dtor_or_conv_p=*/NULL,
11479                                 /*parenthesized_p=*/NULL,
11480                                 /*member_p=*/false);
11481       if (declares_class_or_enum & 2)
11482         cp_parser_check_for_definition_in_return_type (declarator,
11483                                                        decl_specifiers.type,
11484                                                        decl_specifiers.type_location);
11485       if (declarator != cp_error_declarator)
11486         {
11487           decl = grokdeclarator (declarator, &decl_specifiers,
11488                                  NORMAL, 0, &decl_specifiers.attributes);
11489           /* Turn access control back on for names used during
11490              template instantiation.  */
11491           pop_deferring_access_checks ();
11492           /* Do the explicit instantiation.  */
11493           do_decl_instantiation (decl, extension_specifier);
11494         }
11495       else
11496         {
11497           pop_deferring_access_checks ();
11498           /* Skip the body of the explicit instantiation.  */
11499           cp_parser_skip_to_end_of_statement (parser);
11500         }
11501     }
11502   /* We're done with the instantiation.  */
11503   end_explicit_instantiation ();
11504
11505   cp_parser_consume_semicolon_at_end_of_statement (parser);
11506 }
11507
11508 /* Parse an explicit-specialization.
11509
11510    explicit-specialization:
11511      template < > declaration
11512
11513    Although the standard says `declaration', what it really means is:
11514
11515    explicit-specialization:
11516      template <> decl-specifier [opt] init-declarator [opt] ;
11517      template <> function-definition
11518      template <> explicit-specialization
11519      template <> template-declaration  */
11520
11521 static void
11522 cp_parser_explicit_specialization (cp_parser* parser)
11523 {
11524   bool need_lang_pop;
11525   cp_token *token = cp_lexer_peek_token (parser->lexer);
11526
11527   /* Look for the `template' keyword.  */
11528   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11529   /* Look for the `<'.  */
11530   cp_parser_require (parser, CPP_LESS, "%<<%>");
11531   /* Look for the `>'.  */
11532   cp_parser_require (parser, CPP_GREATER, "%<>%>");
11533   /* We have processed another parameter list.  */
11534   ++parser->num_template_parameter_lists;
11535   /* [temp]
11536
11537      A template ... explicit specialization ... shall not have C
11538      linkage.  */
11539   if (current_lang_name == lang_name_c)
11540     {
11541       error_at (token->location, "template specialization with C linkage");
11542       /* Give it C++ linkage to avoid confusing other parts of the
11543          front end.  */
11544       push_lang_context (lang_name_cplusplus);
11545       need_lang_pop = true;
11546     }
11547   else
11548     need_lang_pop = false;
11549   /* Let the front end know that we are beginning a specialization.  */
11550   if (!begin_specialization ())
11551     {
11552       end_specialization ();
11553       return;
11554     }
11555
11556   /* If the next keyword is `template', we need to figure out whether
11557      or not we're looking a template-declaration.  */
11558   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11559     {
11560       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11561           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11562         cp_parser_template_declaration_after_export (parser,
11563                                                      /*member_p=*/false);
11564       else
11565         cp_parser_explicit_specialization (parser);
11566     }
11567   else
11568     /* Parse the dependent declaration.  */
11569     cp_parser_single_declaration (parser,
11570                                   /*checks=*/NULL,
11571                                   /*member_p=*/false,
11572                                   /*explicit_specialization_p=*/true,
11573                                   /*friend_p=*/NULL);
11574   /* We're done with the specialization.  */
11575   end_specialization ();
11576   /* For the erroneous case of a template with C linkage, we pushed an
11577      implicit C++ linkage scope; exit that scope now.  */
11578   if (need_lang_pop)
11579     pop_lang_context ();
11580   /* We're done with this parameter list.  */
11581   --parser->num_template_parameter_lists;
11582 }
11583
11584 /* Parse a type-specifier.
11585
11586    type-specifier:
11587      simple-type-specifier
11588      class-specifier
11589      enum-specifier
11590      elaborated-type-specifier
11591      cv-qualifier
11592
11593    GNU Extension:
11594
11595    type-specifier:
11596      __complex__
11597
11598    Returns a representation of the type-specifier.  For a
11599    class-specifier, enum-specifier, or elaborated-type-specifier, a
11600    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11601
11602    The parser flags FLAGS is used to control type-specifier parsing.
11603
11604    If IS_DECLARATION is TRUE, then this type-specifier is appearing
11605    in a decl-specifier-seq.
11606
11607    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11608    class-specifier, enum-specifier, or elaborated-type-specifier, then
11609    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
11610    if a type is declared; 2 if it is defined.  Otherwise, it is set to
11611    zero.
11612
11613    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11614    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11615    is set to FALSE.  */
11616
11617 static tree
11618 cp_parser_type_specifier (cp_parser* parser,
11619                           cp_parser_flags flags,
11620                           cp_decl_specifier_seq *decl_specs,
11621                           bool is_declaration,
11622                           int* declares_class_or_enum,
11623                           bool* is_cv_qualifier)
11624 {
11625   tree type_spec = NULL_TREE;
11626   cp_token *token;
11627   enum rid keyword;
11628   cp_decl_spec ds = ds_last;
11629
11630   /* Assume this type-specifier does not declare a new type.  */
11631   if (declares_class_or_enum)
11632     *declares_class_or_enum = 0;
11633   /* And that it does not specify a cv-qualifier.  */
11634   if (is_cv_qualifier)
11635     *is_cv_qualifier = false;
11636   /* Peek at the next token.  */
11637   token = cp_lexer_peek_token (parser->lexer);
11638
11639   /* If we're looking at a keyword, we can use that to guide the
11640      production we choose.  */
11641   keyword = token->keyword;
11642   switch (keyword)
11643     {
11644     case RID_ENUM:
11645       /* Look for the enum-specifier.  */
11646       type_spec = cp_parser_enum_specifier (parser);
11647       /* If that worked, we're done.  */
11648       if (type_spec)
11649         {
11650           if (declares_class_or_enum)
11651             *declares_class_or_enum = 2;
11652           if (decl_specs)
11653             cp_parser_set_decl_spec_type (decl_specs,
11654                                           type_spec,
11655                                           token->location,
11656                                           /*user_defined_p=*/true);
11657           return type_spec;
11658         }
11659       else
11660         goto elaborated_type_specifier;
11661
11662       /* Any of these indicate either a class-specifier, or an
11663          elaborated-type-specifier.  */
11664     case RID_CLASS:
11665     case RID_STRUCT:
11666     case RID_UNION:
11667       /* Parse tentatively so that we can back up if we don't find a
11668          class-specifier.  */
11669       cp_parser_parse_tentatively (parser);
11670       /* Look for the class-specifier.  */
11671       type_spec = cp_parser_class_specifier (parser);
11672       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11673       /* If that worked, we're done.  */
11674       if (cp_parser_parse_definitely (parser))
11675         {
11676           if (declares_class_or_enum)
11677             *declares_class_or_enum = 2;
11678           if (decl_specs)
11679             cp_parser_set_decl_spec_type (decl_specs,
11680                                           type_spec,
11681                                           token->location,
11682                                           /*user_defined_p=*/true);
11683           return type_spec;
11684         }
11685
11686       /* Fall through.  */
11687     elaborated_type_specifier:
11688       /* We're declaring (not defining) a class or enum.  */
11689       if (declares_class_or_enum)
11690         *declares_class_or_enum = 1;
11691
11692       /* Fall through.  */
11693     case RID_TYPENAME:
11694       /* Look for an elaborated-type-specifier.  */
11695       type_spec
11696         = (cp_parser_elaborated_type_specifier
11697            (parser,
11698             decl_specs && decl_specs->specs[(int) ds_friend],
11699             is_declaration));
11700       if (decl_specs)
11701         cp_parser_set_decl_spec_type (decl_specs,
11702                                       type_spec,
11703                                       token->location,
11704                                       /*user_defined_p=*/true);
11705       return type_spec;
11706
11707     case RID_CONST:
11708       ds = ds_const;
11709       if (is_cv_qualifier)
11710         *is_cv_qualifier = true;
11711       break;
11712
11713     case RID_VOLATILE:
11714       ds = ds_volatile;
11715       if (is_cv_qualifier)
11716         *is_cv_qualifier = true;
11717       break;
11718
11719     case RID_RESTRICT:
11720       ds = ds_restrict;
11721       if (is_cv_qualifier)
11722         *is_cv_qualifier = true;
11723       break;
11724
11725     case RID_COMPLEX:
11726       /* The `__complex__' keyword is a GNU extension.  */
11727       ds = ds_complex;
11728       break;
11729
11730     default:
11731       break;
11732     }
11733
11734   /* Handle simple keywords.  */
11735   if (ds != ds_last)
11736     {
11737       if (decl_specs)
11738         {
11739           ++decl_specs->specs[(int)ds];
11740           decl_specs->any_specifiers_p = true;
11741         }
11742       return cp_lexer_consume_token (parser->lexer)->u.value;
11743     }
11744
11745   /* If we do not already have a type-specifier, assume we are looking
11746      at a simple-type-specifier.  */
11747   type_spec = cp_parser_simple_type_specifier (parser,
11748                                                decl_specs,
11749                                                flags);
11750
11751   /* If we didn't find a type-specifier, and a type-specifier was not
11752      optional in this context, issue an error message.  */
11753   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11754     {
11755       cp_parser_error (parser, "expected type specifier");
11756       return error_mark_node;
11757     }
11758
11759   return type_spec;
11760 }
11761
11762 /* Parse a simple-type-specifier.
11763
11764    simple-type-specifier:
11765      :: [opt] nested-name-specifier [opt] type-name
11766      :: [opt] nested-name-specifier template template-id
11767      char
11768      wchar_t
11769      bool
11770      short
11771      int
11772      long
11773      signed
11774      unsigned
11775      float
11776      double
11777      void
11778
11779    C++0x Extension:
11780
11781    simple-type-specifier:
11782      auto
11783      decltype ( expression )   
11784      char16_t
11785      char32_t
11786
11787    GNU Extension:
11788
11789    simple-type-specifier:
11790      __typeof__ unary-expression
11791      __typeof__ ( type-id )
11792
11793    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11794    appropriately updated.  */
11795
11796 static tree
11797 cp_parser_simple_type_specifier (cp_parser* parser,
11798                                  cp_decl_specifier_seq *decl_specs,
11799                                  cp_parser_flags flags)
11800 {
11801   tree type = NULL_TREE;
11802   cp_token *token;
11803
11804   /* Peek at the next token.  */
11805   token = cp_lexer_peek_token (parser->lexer);
11806
11807   /* If we're looking at a keyword, things are easy.  */
11808   switch (token->keyword)
11809     {
11810     case RID_CHAR:
11811       if (decl_specs)
11812         decl_specs->explicit_char_p = true;
11813       type = char_type_node;
11814       break;
11815     case RID_CHAR16:
11816       type = char16_type_node;
11817       break;
11818     case RID_CHAR32:
11819       type = char32_type_node;
11820       break;
11821     case RID_WCHAR:
11822       type = wchar_type_node;
11823       break;
11824     case RID_BOOL:
11825       type = boolean_type_node;
11826       break;
11827     case RID_SHORT:
11828       if (decl_specs)
11829         ++decl_specs->specs[(int) ds_short];
11830       type = short_integer_type_node;
11831       break;
11832     case RID_INT:
11833       if (decl_specs)
11834         decl_specs->explicit_int_p = true;
11835       type = integer_type_node;
11836       break;
11837     case RID_LONG:
11838       if (decl_specs)
11839         ++decl_specs->specs[(int) ds_long];
11840       type = long_integer_type_node;
11841       break;
11842     case RID_SIGNED:
11843       if (decl_specs)
11844         ++decl_specs->specs[(int) ds_signed];
11845       type = integer_type_node;
11846       break;
11847     case RID_UNSIGNED:
11848       if (decl_specs)
11849         ++decl_specs->specs[(int) ds_unsigned];
11850       type = unsigned_type_node;
11851       break;
11852     case RID_FLOAT:
11853       type = float_type_node;
11854       break;
11855     case RID_DOUBLE:
11856       type = double_type_node;
11857       break;
11858     case RID_VOID:
11859       type = void_type_node;
11860       break;
11861       
11862     case RID_AUTO:
11863       maybe_warn_cpp0x ("C++0x auto");
11864       type = make_auto ();
11865       break;
11866
11867     case RID_DECLTYPE:
11868       /* Parse the `decltype' type.  */
11869       type = cp_parser_decltype (parser);
11870
11871       if (decl_specs)
11872         cp_parser_set_decl_spec_type (decl_specs, type,
11873                                       token->location,
11874                                       /*user_defined_p=*/true);
11875
11876       return type;
11877
11878     case RID_TYPEOF:
11879       /* Consume the `typeof' token.  */
11880       cp_lexer_consume_token (parser->lexer);
11881       /* Parse the operand to `typeof'.  */
11882       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11883       /* If it is not already a TYPE, take its type.  */
11884       if (!TYPE_P (type))
11885         type = finish_typeof (type);
11886
11887       if (decl_specs)
11888         cp_parser_set_decl_spec_type (decl_specs, type,
11889                                       token->location,
11890                                       /*user_defined_p=*/true);
11891
11892       return type;
11893
11894     default:
11895       break;
11896     }
11897
11898   /* If the type-specifier was for a built-in type, we're done.  */
11899   if (type)
11900     {
11901       tree id;
11902
11903       /* Record the type.  */
11904       if (decl_specs
11905           && (token->keyword != RID_SIGNED
11906               && token->keyword != RID_UNSIGNED
11907               && token->keyword != RID_SHORT
11908               && token->keyword != RID_LONG))
11909         cp_parser_set_decl_spec_type (decl_specs,
11910                                       type,
11911                                       token->location,
11912                                       /*user_defined=*/false);
11913       if (decl_specs)
11914         decl_specs->any_specifiers_p = true;
11915
11916       /* Consume the token.  */
11917       id = cp_lexer_consume_token (parser->lexer)->u.value;
11918
11919       /* There is no valid C++ program where a non-template type is
11920          followed by a "<".  That usually indicates that the user thought
11921          that the type was a template.  */
11922       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11923
11924       return TYPE_NAME (type);
11925     }
11926
11927   /* The type-specifier must be a user-defined type.  */
11928   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11929     {
11930       bool qualified_p;
11931       bool global_p;
11932
11933       /* Don't gobble tokens or issue error messages if this is an
11934          optional type-specifier.  */
11935       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11936         cp_parser_parse_tentatively (parser);
11937
11938       /* Look for the optional `::' operator.  */
11939       global_p
11940         = (cp_parser_global_scope_opt (parser,
11941                                        /*current_scope_valid_p=*/false)
11942            != NULL_TREE);
11943       /* Look for the nested-name specifier.  */
11944       qualified_p
11945         = (cp_parser_nested_name_specifier_opt (parser,
11946                                                 /*typename_keyword_p=*/false,
11947                                                 /*check_dependency_p=*/true,
11948                                                 /*type_p=*/false,
11949                                                 /*is_declaration=*/false)
11950            != NULL_TREE);
11951       token = cp_lexer_peek_token (parser->lexer);
11952       /* If we have seen a nested-name-specifier, and the next token
11953          is `template', then we are using the template-id production.  */
11954       if (parser->scope
11955           && cp_parser_optional_template_keyword (parser))
11956         {
11957           /* Look for the template-id.  */
11958           type = cp_parser_template_id (parser,
11959                                         /*template_keyword_p=*/true,
11960                                         /*check_dependency_p=*/true,
11961                                         /*is_declaration=*/false);
11962           /* If the template-id did not name a type, we are out of
11963              luck.  */
11964           if (TREE_CODE (type) != TYPE_DECL)
11965             {
11966               cp_parser_error (parser, "expected template-id for type");
11967               type = NULL_TREE;
11968             }
11969         }
11970       /* Otherwise, look for a type-name.  */
11971       else
11972         type = cp_parser_type_name (parser);
11973       /* Keep track of all name-lookups performed in class scopes.  */
11974       if (type
11975           && !global_p
11976           && !qualified_p
11977           && TREE_CODE (type) == TYPE_DECL
11978           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11979         maybe_note_name_used_in_class (DECL_NAME (type), type);
11980       /* If it didn't work out, we don't have a TYPE.  */
11981       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11982           && !cp_parser_parse_definitely (parser))
11983         type = NULL_TREE;
11984       if (type && decl_specs)
11985         cp_parser_set_decl_spec_type (decl_specs, type,
11986                                       token->location,
11987                                       /*user_defined=*/true);
11988     }
11989
11990   /* If we didn't get a type-name, issue an error message.  */
11991   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11992     {
11993       cp_parser_error (parser, "expected type-name");
11994       return error_mark_node;
11995     }
11996
11997   /* There is no valid C++ program where a non-template type is
11998      followed by a "<".  That usually indicates that the user thought
11999      that the type was a template.  */
12000   if (type && type != error_mark_node)
12001     {
12002       /* As a last-ditch effort, see if TYPE is an Objective-C type.
12003          If it is, then the '<'...'>' enclose protocol names rather than
12004          template arguments, and so everything is fine.  */
12005       if (c_dialect_objc ()
12006           && (objc_is_id (type) || objc_is_class_name (type)))
12007         {
12008           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12009           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12010
12011           /* Clobber the "unqualified" type previously entered into
12012              DECL_SPECS with the new, improved protocol-qualified version.  */
12013           if (decl_specs)
12014             decl_specs->type = qual_type;
12015
12016           return qual_type;
12017         }
12018
12019       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12020                                                token->location);
12021     }
12022
12023   return type;
12024 }
12025
12026 /* Parse a type-name.
12027
12028    type-name:
12029      class-name
12030      enum-name
12031      typedef-name
12032
12033    enum-name:
12034      identifier
12035
12036    typedef-name:
12037      identifier
12038
12039    Returns a TYPE_DECL for the type.  */
12040
12041 static tree
12042 cp_parser_type_name (cp_parser* parser)
12043 {
12044   tree type_decl;
12045
12046   /* We can't know yet whether it is a class-name or not.  */
12047   cp_parser_parse_tentatively (parser);
12048   /* Try a class-name.  */
12049   type_decl = cp_parser_class_name (parser,
12050                                     /*typename_keyword_p=*/false,
12051                                     /*template_keyword_p=*/false,
12052                                     none_type,
12053                                     /*check_dependency_p=*/true,
12054                                     /*class_head_p=*/false,
12055                                     /*is_declaration=*/false);
12056   /* If it's not a class-name, keep looking.  */
12057   if (!cp_parser_parse_definitely (parser))
12058     {
12059       /* It must be a typedef-name or an enum-name.  */
12060       return cp_parser_nonclass_name (parser);
12061     }
12062
12063   return type_decl;
12064 }
12065
12066 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12067
12068    enum-name:
12069      identifier
12070
12071    typedef-name:
12072      identifier
12073
12074    Returns a TYPE_DECL for the type.  */
12075
12076 static tree
12077 cp_parser_nonclass_name (cp_parser* parser)
12078 {
12079   tree type_decl;
12080   tree identifier;
12081
12082   cp_token *token = cp_lexer_peek_token (parser->lexer);
12083   identifier = cp_parser_identifier (parser);
12084   if (identifier == error_mark_node)
12085     return error_mark_node;
12086
12087   /* Look up the type-name.  */
12088   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12089
12090   if (TREE_CODE (type_decl) != TYPE_DECL
12091       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12092     {
12093       /* See if this is an Objective-C type.  */
12094       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12095       tree type = objc_get_protocol_qualified_type (identifier, protos);
12096       if (type)
12097         type_decl = TYPE_NAME (type);
12098     }
12099   
12100   /* Issue an error if we did not find a type-name.  */
12101   if (TREE_CODE (type_decl) != TYPE_DECL)
12102     {
12103       if (!cp_parser_simulate_error (parser))
12104         cp_parser_name_lookup_error (parser, identifier, type_decl,
12105                                      "is not a type", token->location);
12106       return error_mark_node;
12107     }
12108   /* Remember that the name was used in the definition of the
12109      current class so that we can check later to see if the
12110      meaning would have been different after the class was
12111      entirely defined.  */
12112   else if (type_decl != error_mark_node
12113            && !parser->scope)
12114     maybe_note_name_used_in_class (identifier, type_decl);
12115   
12116   return type_decl;
12117 }
12118
12119 /* Parse an elaborated-type-specifier.  Note that the grammar given
12120    here incorporates the resolution to DR68.
12121
12122    elaborated-type-specifier:
12123      class-key :: [opt] nested-name-specifier [opt] identifier
12124      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12125      enum-key :: [opt] nested-name-specifier [opt] identifier
12126      typename :: [opt] nested-name-specifier identifier
12127      typename :: [opt] nested-name-specifier template [opt]
12128        template-id
12129
12130    GNU extension:
12131
12132    elaborated-type-specifier:
12133      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12134      class-key attributes :: [opt] nested-name-specifier [opt]
12135                template [opt] template-id
12136      enum attributes :: [opt] nested-name-specifier [opt] identifier
12137
12138    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12139    declared `friend'.  If IS_DECLARATION is TRUE, then this
12140    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12141    something is being declared.
12142
12143    Returns the TYPE specified.  */
12144
12145 static tree
12146 cp_parser_elaborated_type_specifier (cp_parser* parser,
12147                                      bool is_friend,
12148                                      bool is_declaration)
12149 {
12150   enum tag_types tag_type;
12151   tree identifier;
12152   tree type = NULL_TREE;
12153   tree attributes = NULL_TREE;
12154   tree globalscope;
12155   cp_token *token = NULL;
12156
12157   /* See if we're looking at the `enum' keyword.  */
12158   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12159     {
12160       /* Consume the `enum' token.  */
12161       cp_lexer_consume_token (parser->lexer);
12162       /* Remember that it's an enumeration type.  */
12163       tag_type = enum_type;
12164       /* Parse the optional `struct' or `class' key (for C++0x scoped
12165          enums).  */
12166       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12167           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12168         {
12169           if (cxx_dialect == cxx98)
12170             maybe_warn_cpp0x ("scoped enums");
12171
12172           /* Consume the `struct' or `class'.  */
12173           cp_lexer_consume_token (parser->lexer);
12174         }
12175       /* Parse the attributes.  */
12176       attributes = cp_parser_attributes_opt (parser);
12177     }
12178   /* Or, it might be `typename'.  */
12179   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12180                                            RID_TYPENAME))
12181     {
12182       /* Consume the `typename' token.  */
12183       cp_lexer_consume_token (parser->lexer);
12184       /* Remember that it's a `typename' type.  */
12185       tag_type = typename_type;
12186     }
12187   /* Otherwise it must be a class-key.  */
12188   else
12189     {
12190       tag_type = cp_parser_class_key (parser);
12191       if (tag_type == none_type)
12192         return error_mark_node;
12193       /* Parse the attributes.  */
12194       attributes = cp_parser_attributes_opt (parser);
12195     }
12196
12197   /* Look for the `::' operator.  */
12198   globalscope =  cp_parser_global_scope_opt (parser,
12199                                              /*current_scope_valid_p=*/false);
12200   /* Look for the nested-name-specifier.  */
12201   if (tag_type == typename_type && !globalscope)
12202     {
12203       if (!cp_parser_nested_name_specifier (parser,
12204                                            /*typename_keyword_p=*/true,
12205                                            /*check_dependency_p=*/true,
12206                                            /*type_p=*/true,
12207                                             is_declaration))
12208         return error_mark_node;
12209     }
12210   else
12211     /* Even though `typename' is not present, the proposed resolution
12212        to Core Issue 180 says that in `class A<T>::B', `B' should be
12213        considered a type-name, even if `A<T>' is dependent.  */
12214     cp_parser_nested_name_specifier_opt (parser,
12215                                          /*typename_keyword_p=*/true,
12216                                          /*check_dependency_p=*/true,
12217                                          /*type_p=*/true,
12218                                          is_declaration);
12219  /* For everything but enumeration types, consider a template-id.
12220     For an enumeration type, consider only a plain identifier.  */
12221   if (tag_type != enum_type)
12222     {
12223       bool template_p = false;
12224       tree decl;
12225
12226       /* Allow the `template' keyword.  */
12227       template_p = cp_parser_optional_template_keyword (parser);
12228       /* If we didn't see `template', we don't know if there's a
12229          template-id or not.  */
12230       if (!template_p)
12231         cp_parser_parse_tentatively (parser);
12232       /* Parse the template-id.  */
12233       token = cp_lexer_peek_token (parser->lexer);
12234       decl = cp_parser_template_id (parser, template_p,
12235                                     /*check_dependency_p=*/true,
12236                                     is_declaration);
12237       /* If we didn't find a template-id, look for an ordinary
12238          identifier.  */
12239       if (!template_p && !cp_parser_parse_definitely (parser))
12240         ;
12241       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12242          in effect, then we must assume that, upon instantiation, the
12243          template will correspond to a class.  */
12244       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12245                && tag_type == typename_type)
12246         type = make_typename_type (parser->scope, decl,
12247                                    typename_type,
12248                                    /*complain=*/tf_error);
12249       /* If the `typename' keyword is in effect and DECL is not a type
12250          decl. Then type is non existant.   */
12251       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12252         type = NULL_TREE; 
12253       else 
12254         type = TREE_TYPE (decl);
12255     }
12256
12257   if (!type)
12258     {
12259       token = cp_lexer_peek_token (parser->lexer);
12260       identifier = cp_parser_identifier (parser);
12261
12262       if (identifier == error_mark_node)
12263         {
12264           parser->scope = NULL_TREE;
12265           return error_mark_node;
12266         }
12267
12268       /* For a `typename', we needn't call xref_tag.  */
12269       if (tag_type == typename_type
12270           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12271         return cp_parser_make_typename_type (parser, parser->scope,
12272                                              identifier,
12273                                              token->location);
12274       /* Look up a qualified name in the usual way.  */
12275       if (parser->scope)
12276         {
12277           tree decl;
12278           tree ambiguous_decls;
12279
12280           decl = cp_parser_lookup_name (parser, identifier,
12281                                         tag_type,
12282                                         /*is_template=*/false,
12283                                         /*is_namespace=*/false,
12284                                         /*check_dependency=*/true,
12285                                         &ambiguous_decls,
12286                                         token->location);
12287
12288           /* If the lookup was ambiguous, an error will already have been
12289              issued.  */
12290           if (ambiguous_decls)
12291             return error_mark_node;
12292
12293           /* If we are parsing friend declaration, DECL may be a
12294              TEMPLATE_DECL tree node here.  However, we need to check
12295              whether this TEMPLATE_DECL results in valid code.  Consider
12296              the following example:
12297
12298                namespace N {
12299                  template <class T> class C {};
12300                }
12301                class X {
12302                  template <class T> friend class N::C; // #1, valid code
12303                };
12304                template <class T> class Y {
12305                  friend class N::C;                    // #2, invalid code
12306                };
12307
12308              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12309              name lookup of `N::C'.  We see that friend declaration must
12310              be template for the code to be valid.  Note that
12311              processing_template_decl does not work here since it is
12312              always 1 for the above two cases.  */
12313
12314           decl = (cp_parser_maybe_treat_template_as_class
12315                   (decl, /*tag_name_p=*/is_friend
12316                          && parser->num_template_parameter_lists));
12317
12318           if (TREE_CODE (decl) != TYPE_DECL)
12319             {
12320               cp_parser_diagnose_invalid_type_name (parser,
12321                                                     parser->scope,
12322                                                     identifier,
12323                                                     token->location);
12324               return error_mark_node;
12325             }
12326
12327           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12328             {
12329               bool allow_template = (parser->num_template_parameter_lists
12330                                       || DECL_SELF_REFERENCE_P (decl));
12331               type = check_elaborated_type_specifier (tag_type, decl, 
12332                                                       allow_template);
12333
12334               if (type == error_mark_node)
12335                 return error_mark_node;
12336             }
12337
12338           /* Forward declarations of nested types, such as
12339
12340                class C1::C2;
12341                class C1::C2::C3;
12342
12343              are invalid unless all components preceding the final '::'
12344              are complete.  If all enclosing types are complete, these
12345              declarations become merely pointless.
12346
12347              Invalid forward declarations of nested types are errors
12348              caught elsewhere in parsing.  Those that are pointless arrive
12349              here.  */
12350
12351           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12352               && !is_friend && !processing_explicit_instantiation)
12353             warning (0, "declaration %qD does not declare anything", decl);
12354
12355           type = TREE_TYPE (decl);
12356         }
12357       else
12358         {
12359           /* An elaborated-type-specifier sometimes introduces a new type and
12360              sometimes names an existing type.  Normally, the rule is that it
12361              introduces a new type only if there is not an existing type of
12362              the same name already in scope.  For example, given:
12363
12364                struct S {};
12365                void f() { struct S s; }
12366
12367              the `struct S' in the body of `f' is the same `struct S' as in
12368              the global scope; the existing definition is used.  However, if
12369              there were no global declaration, this would introduce a new
12370              local class named `S'.
12371
12372              An exception to this rule applies to the following code:
12373
12374                namespace N { struct S; }
12375
12376              Here, the elaborated-type-specifier names a new type
12377              unconditionally; even if there is already an `S' in the
12378              containing scope this declaration names a new type.
12379              This exception only applies if the elaborated-type-specifier
12380              forms the complete declaration:
12381
12382                [class.name]
12383
12384                A declaration consisting solely of `class-key identifier ;' is
12385                either a redeclaration of the name in the current scope or a
12386                forward declaration of the identifier as a class name.  It
12387                introduces the name into the current scope.
12388
12389              We are in this situation precisely when the next token is a `;'.
12390
12391              An exception to the exception is that a `friend' declaration does
12392              *not* name a new type; i.e., given:
12393
12394                struct S { friend struct T; };
12395
12396              `T' is not a new type in the scope of `S'.
12397
12398              Also, `new struct S' or `sizeof (struct S)' never results in the
12399              definition of a new type; a new type can only be declared in a
12400              declaration context.  */
12401
12402           tag_scope ts;
12403           bool template_p;
12404
12405           if (is_friend)
12406             /* Friends have special name lookup rules.  */
12407             ts = ts_within_enclosing_non_class;
12408           else if (is_declaration
12409                    && cp_lexer_next_token_is (parser->lexer,
12410                                               CPP_SEMICOLON))
12411             /* This is a `class-key identifier ;' */
12412             ts = ts_current;
12413           else
12414             ts = ts_global;
12415
12416           template_p =
12417             (parser->num_template_parameter_lists
12418              && (cp_parser_next_token_starts_class_definition_p (parser)
12419                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12420           /* An unqualified name was used to reference this type, so
12421              there were no qualifying templates.  */
12422           if (!cp_parser_check_template_parameters (parser,
12423                                                     /*num_templates=*/0,
12424                                                     token->location,
12425                                                     /*declarator=*/NULL))
12426             return error_mark_node;
12427           type = xref_tag (tag_type, identifier, ts, template_p);
12428         }
12429     }
12430
12431   if (type == error_mark_node)
12432     return error_mark_node;
12433
12434   /* Allow attributes on forward declarations of classes.  */
12435   if (attributes)
12436     {
12437       if (TREE_CODE (type) == TYPENAME_TYPE)
12438         warning (OPT_Wattributes,
12439                  "attributes ignored on uninstantiated type");
12440       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12441                && ! processing_explicit_instantiation)
12442         warning (OPT_Wattributes,
12443                  "attributes ignored on template instantiation");
12444       else if (is_declaration && cp_parser_declares_only_class_p (parser))
12445         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12446       else
12447         warning (OPT_Wattributes,
12448                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12449     }
12450
12451   if (tag_type != enum_type)
12452     cp_parser_check_class_key (tag_type, type);
12453
12454   /* A "<" cannot follow an elaborated type specifier.  If that
12455      happens, the user was probably trying to form a template-id.  */
12456   cp_parser_check_for_invalid_template_id (parser, type, token->location);
12457
12458   return type;
12459 }
12460
12461 /* Parse an enum-specifier.
12462
12463    enum-specifier:
12464      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12465
12466    enum-key:
12467      enum
12468      enum class   [C++0x]
12469      enum struct  [C++0x]
12470
12471    enum-base:   [C++0x]
12472      : type-specifier-seq
12473
12474    GNU Extensions:
12475      enum-key attributes[opt] identifier [opt] enum-base [opt] 
12476        { enumerator-list [opt] }attributes[opt]
12477
12478    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12479    if the token stream isn't an enum-specifier after all.  */
12480
12481 static tree
12482 cp_parser_enum_specifier (cp_parser* parser)
12483 {
12484   tree identifier;
12485   tree type;
12486   tree attributes;
12487   bool scoped_enum_p = false;
12488   bool has_underlying_type = false;
12489   tree underlying_type = NULL_TREE;
12490
12491   /* Parse tentatively so that we can back up if we don't find a
12492      enum-specifier.  */
12493   cp_parser_parse_tentatively (parser);
12494
12495   /* Caller guarantees that the current token is 'enum', an identifier
12496      possibly follows, and the token after that is an opening brace.
12497      If we don't have an identifier, fabricate an anonymous name for
12498      the enumeration being defined.  */
12499   cp_lexer_consume_token (parser->lexer);
12500
12501   /* Parse the "class" or "struct", which indicates a scoped
12502      enumeration type in C++0x.  */
12503   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12504       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12505     {
12506       if (cxx_dialect == cxx98)
12507         maybe_warn_cpp0x ("scoped enums");
12508
12509       /* Consume the `struct' or `class' token.  */
12510       cp_lexer_consume_token (parser->lexer);
12511
12512       scoped_enum_p = true;
12513     }
12514
12515   attributes = cp_parser_attributes_opt (parser);
12516
12517   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12518     identifier = cp_parser_identifier (parser);
12519   else
12520     identifier = make_anon_name ();
12521
12522   /* Check for the `:' that denotes a specified underlying type in C++0x.
12523      Note that a ':' could also indicate a bitfield width, however.  */
12524   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12525     {
12526       cp_decl_specifier_seq type_specifiers;
12527
12528       /* Consume the `:'.  */
12529       cp_lexer_consume_token (parser->lexer);
12530
12531       /* Parse the type-specifier-seq.  */
12532       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12533                                     &type_specifiers);
12534
12535       /* At this point this is surely not elaborated type specifier.  */
12536       if (!cp_parser_parse_definitely (parser))
12537         return NULL_TREE;
12538
12539       if (cxx_dialect == cxx98)
12540         maybe_warn_cpp0x ("scoped enums");
12541
12542       has_underlying_type = true;
12543
12544       /* If that didn't work, stop.  */
12545       if (type_specifiers.type != error_mark_node)
12546         {
12547           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12548                                             /*initialized=*/0, NULL);
12549           if (underlying_type == error_mark_node)
12550             underlying_type = NULL_TREE;
12551         }
12552     }
12553
12554   /* Look for the `{' but don't consume it yet.  */
12555   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12556     {
12557       cp_parser_error (parser, "expected %<{%>");
12558       if (has_underlying_type)
12559         return NULL_TREE;
12560     }
12561
12562   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12563     return NULL_TREE;
12564
12565   /* Issue an error message if type-definitions are forbidden here.  */
12566   if (!cp_parser_check_type_definition (parser))
12567     type = error_mark_node;
12568   else
12569     /* Create the new type.  We do this before consuming the opening
12570        brace so the enum will be recorded as being on the line of its
12571        tag (or the 'enum' keyword, if there is no tag).  */
12572     type = start_enum (identifier, underlying_type, scoped_enum_p);
12573   
12574   /* Consume the opening brace.  */
12575   cp_lexer_consume_token (parser->lexer);
12576
12577   if (type == error_mark_node)
12578     {
12579       cp_parser_skip_to_end_of_block_or_statement (parser);
12580       return error_mark_node;
12581     }
12582
12583   /* If the next token is not '}', then there are some enumerators.  */
12584   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12585     cp_parser_enumerator_list (parser, type);
12586
12587   /* Consume the final '}'.  */
12588   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12589
12590   /* Look for trailing attributes to apply to this enumeration, and
12591      apply them if appropriate.  */
12592   if (cp_parser_allow_gnu_extensions_p (parser))
12593     {
12594       tree trailing_attr = cp_parser_attributes_opt (parser);
12595       trailing_attr = chainon (trailing_attr, attributes);
12596       cplus_decl_attributes (&type,
12597                              trailing_attr,
12598                              (int) ATTR_FLAG_TYPE_IN_PLACE);
12599     }
12600
12601   /* Finish up the enumeration.  */
12602   finish_enum (type);
12603
12604   return type;
12605 }
12606
12607 /* Parse an enumerator-list.  The enumerators all have the indicated
12608    TYPE.
12609
12610    enumerator-list:
12611      enumerator-definition
12612      enumerator-list , enumerator-definition  */
12613
12614 static void
12615 cp_parser_enumerator_list (cp_parser* parser, tree type)
12616 {
12617   while (true)
12618     {
12619       /* Parse an enumerator-definition.  */
12620       cp_parser_enumerator_definition (parser, type);
12621
12622       /* If the next token is not a ',', we've reached the end of
12623          the list.  */
12624       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12625         break;
12626       /* Otherwise, consume the `,' and keep going.  */
12627       cp_lexer_consume_token (parser->lexer);
12628       /* If the next token is a `}', there is a trailing comma.  */
12629       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12630         {
12631           if (!in_system_header)
12632             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12633           break;
12634         }
12635     }
12636 }
12637
12638 /* Parse an enumerator-definition.  The enumerator has the indicated
12639    TYPE.
12640
12641    enumerator-definition:
12642      enumerator
12643      enumerator = constant-expression
12644
12645    enumerator:
12646      identifier  */
12647
12648 static void
12649 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12650 {
12651   tree identifier;
12652   tree value;
12653
12654   /* Look for the identifier.  */
12655   identifier = cp_parser_identifier (parser);
12656   if (identifier == error_mark_node)
12657     return;
12658
12659   /* If the next token is an '=', then there is an explicit value.  */
12660   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12661     {
12662       /* Consume the `=' token.  */
12663       cp_lexer_consume_token (parser->lexer);
12664       /* Parse the value.  */
12665       value = cp_parser_constant_expression (parser,
12666                                              /*allow_non_constant_p=*/false,
12667                                              NULL);
12668     }
12669   else
12670     value = NULL_TREE;
12671
12672   /* If we are processing a template, make sure the initializer of the
12673      enumerator doesn't contain any bare template parameter pack.  */
12674   if (check_for_bare_parameter_packs (value))
12675     value = error_mark_node;
12676
12677   /* Create the enumerator.  */
12678   build_enumerator (identifier, value, type);
12679 }
12680
12681 /* Parse a namespace-name.
12682
12683    namespace-name:
12684      original-namespace-name
12685      namespace-alias
12686
12687    Returns the NAMESPACE_DECL for the namespace.  */
12688
12689 static tree
12690 cp_parser_namespace_name (cp_parser* parser)
12691 {
12692   tree identifier;
12693   tree namespace_decl;
12694
12695   cp_token *token = cp_lexer_peek_token (parser->lexer);
12696
12697   /* Get the name of the namespace.  */
12698   identifier = cp_parser_identifier (parser);
12699   if (identifier == error_mark_node)
12700     return error_mark_node;
12701
12702   /* Look up the identifier in the currently active scope.  Look only
12703      for namespaces, due to:
12704
12705        [basic.lookup.udir]
12706
12707        When looking up a namespace-name in a using-directive or alias
12708        definition, only namespace names are considered.
12709
12710      And:
12711
12712        [basic.lookup.qual]
12713
12714        During the lookup of a name preceding the :: scope resolution
12715        operator, object, function, and enumerator names are ignored.
12716
12717      (Note that cp_parser_qualifying_entity only calls this
12718      function if the token after the name is the scope resolution
12719      operator.)  */
12720   namespace_decl = cp_parser_lookup_name (parser, identifier,
12721                                           none_type,
12722                                           /*is_template=*/false,
12723                                           /*is_namespace=*/true,
12724                                           /*check_dependency=*/true,
12725                                           /*ambiguous_decls=*/NULL,
12726                                           token->location);
12727   /* If it's not a namespace, issue an error.  */
12728   if (namespace_decl == error_mark_node
12729       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12730     {
12731       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12732         error_at (token->location, "%qD is not a namespace-name", identifier);
12733       cp_parser_error (parser, "expected namespace-name");
12734       namespace_decl = error_mark_node;
12735     }
12736
12737   return namespace_decl;
12738 }
12739
12740 /* Parse a namespace-definition.
12741
12742    namespace-definition:
12743      named-namespace-definition
12744      unnamed-namespace-definition
12745
12746    named-namespace-definition:
12747      original-namespace-definition
12748      extension-namespace-definition
12749
12750    original-namespace-definition:
12751      namespace identifier { namespace-body }
12752
12753    extension-namespace-definition:
12754      namespace original-namespace-name { namespace-body }
12755
12756    unnamed-namespace-definition:
12757      namespace { namespace-body } */
12758
12759 static void
12760 cp_parser_namespace_definition (cp_parser* parser)
12761 {
12762   tree identifier, attribs;
12763   bool has_visibility;
12764   bool is_inline;
12765
12766   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12767     {
12768       is_inline = true;
12769       cp_lexer_consume_token (parser->lexer);
12770     }
12771   else
12772     is_inline = false;
12773
12774   /* Look for the `namespace' keyword.  */
12775   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12776
12777   /* Get the name of the namespace.  We do not attempt to distinguish
12778      between an original-namespace-definition and an
12779      extension-namespace-definition at this point.  The semantic
12780      analysis routines are responsible for that.  */
12781   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12782     identifier = cp_parser_identifier (parser);
12783   else
12784     identifier = NULL_TREE;
12785
12786   /* Parse any specified attributes.  */
12787   attribs = cp_parser_attributes_opt (parser);
12788
12789   /* Look for the `{' to start the namespace.  */
12790   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12791   /* Start the namespace.  */
12792   push_namespace (identifier);
12793
12794   /* "inline namespace" is equivalent to a stub namespace definition
12795      followed by a strong using directive.  */
12796   if (is_inline)
12797     {
12798       tree name_space = current_namespace;
12799       /* Set up namespace association.  */
12800       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12801         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12802                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12803       /* Import the contents of the inline namespace.  */
12804       pop_namespace ();
12805       do_using_directive (name_space);
12806       push_namespace (identifier);
12807     }
12808
12809   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12810
12811   /* Parse the body of the namespace.  */
12812   cp_parser_namespace_body (parser);
12813
12814 #ifdef HANDLE_PRAGMA_VISIBILITY
12815   if (has_visibility)
12816     pop_visibility ();
12817 #endif
12818
12819   /* Finish the namespace.  */
12820   pop_namespace ();
12821   /* Look for the final `}'.  */
12822   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12823 }
12824
12825 /* Parse a namespace-body.
12826
12827    namespace-body:
12828      declaration-seq [opt]  */
12829
12830 static void
12831 cp_parser_namespace_body (cp_parser* parser)
12832 {
12833   cp_parser_declaration_seq_opt (parser);
12834 }
12835
12836 /* Parse a namespace-alias-definition.
12837
12838    namespace-alias-definition:
12839      namespace identifier = qualified-namespace-specifier ;  */
12840
12841 static void
12842 cp_parser_namespace_alias_definition (cp_parser* parser)
12843 {
12844   tree identifier;
12845   tree namespace_specifier;
12846
12847   cp_token *token = cp_lexer_peek_token (parser->lexer);
12848
12849   /* Look for the `namespace' keyword.  */
12850   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12851   /* Look for the identifier.  */
12852   identifier = cp_parser_identifier (parser);
12853   if (identifier == error_mark_node)
12854     return;
12855   /* Look for the `=' token.  */
12856   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12857       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12858     {
12859       error_at (token->location, "%<namespace%> definition is not allowed here");
12860       /* Skip the definition.  */
12861       cp_lexer_consume_token (parser->lexer);
12862       if (cp_parser_skip_to_closing_brace (parser))
12863         cp_lexer_consume_token (parser->lexer);
12864       return;
12865     }
12866   cp_parser_require (parser, CPP_EQ, "%<=%>");
12867   /* Look for the qualified-namespace-specifier.  */
12868   namespace_specifier
12869     = cp_parser_qualified_namespace_specifier (parser);
12870   /* Look for the `;' token.  */
12871   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12872
12873   /* Register the alias in the symbol table.  */
12874   do_namespace_alias (identifier, namespace_specifier);
12875 }
12876
12877 /* Parse a qualified-namespace-specifier.
12878
12879    qualified-namespace-specifier:
12880      :: [opt] nested-name-specifier [opt] namespace-name
12881
12882    Returns a NAMESPACE_DECL corresponding to the specified
12883    namespace.  */
12884
12885 static tree
12886 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12887 {
12888   /* Look for the optional `::'.  */
12889   cp_parser_global_scope_opt (parser,
12890                               /*current_scope_valid_p=*/false);
12891
12892   /* Look for the optional nested-name-specifier.  */
12893   cp_parser_nested_name_specifier_opt (parser,
12894                                        /*typename_keyword_p=*/false,
12895                                        /*check_dependency_p=*/true,
12896                                        /*type_p=*/false,
12897                                        /*is_declaration=*/true);
12898
12899   return cp_parser_namespace_name (parser);
12900 }
12901
12902 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12903    access declaration.
12904
12905    using-declaration:
12906      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12907      using :: unqualified-id ;  
12908
12909    access-declaration:
12910      qualified-id ;  
12911
12912    */
12913
12914 static bool
12915 cp_parser_using_declaration (cp_parser* parser, 
12916                              bool access_declaration_p)
12917 {
12918   cp_token *token;
12919   bool typename_p = false;
12920   bool global_scope_p;
12921   tree decl;
12922   tree identifier;
12923   tree qscope;
12924
12925   if (access_declaration_p)
12926     cp_parser_parse_tentatively (parser);
12927   else
12928     {
12929       /* Look for the `using' keyword.  */
12930       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12931       
12932       /* Peek at the next token.  */
12933       token = cp_lexer_peek_token (parser->lexer);
12934       /* See if it's `typename'.  */
12935       if (token->keyword == RID_TYPENAME)
12936         {
12937           /* Remember that we've seen it.  */
12938           typename_p = true;
12939           /* Consume the `typename' token.  */
12940           cp_lexer_consume_token (parser->lexer);
12941         }
12942     }
12943
12944   /* Look for the optional global scope qualification.  */
12945   global_scope_p
12946     = (cp_parser_global_scope_opt (parser,
12947                                    /*current_scope_valid_p=*/false)
12948        != NULL_TREE);
12949
12950   /* If we saw `typename', or didn't see `::', then there must be a
12951      nested-name-specifier present.  */
12952   if (typename_p || !global_scope_p)
12953     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12954                                               /*check_dependency_p=*/true,
12955                                               /*type_p=*/false,
12956                                               /*is_declaration=*/true);
12957   /* Otherwise, we could be in either of the two productions.  In that
12958      case, treat the nested-name-specifier as optional.  */
12959   else
12960     qscope = cp_parser_nested_name_specifier_opt (parser,
12961                                                   /*typename_keyword_p=*/false,
12962                                                   /*check_dependency_p=*/true,
12963                                                   /*type_p=*/false,
12964                                                   /*is_declaration=*/true);
12965   if (!qscope)
12966     qscope = global_namespace;
12967
12968   if (access_declaration_p && cp_parser_error_occurred (parser))
12969     /* Something has already gone wrong; there's no need to parse
12970        further.  Since an error has occurred, the return value of
12971        cp_parser_parse_definitely will be false, as required.  */
12972     return cp_parser_parse_definitely (parser);
12973
12974   token = cp_lexer_peek_token (parser->lexer);
12975   /* Parse the unqualified-id.  */
12976   identifier = cp_parser_unqualified_id (parser,
12977                                          /*template_keyword_p=*/false,
12978                                          /*check_dependency_p=*/true,
12979                                          /*declarator_p=*/true,
12980                                          /*optional_p=*/false);
12981
12982   if (access_declaration_p)
12983     {
12984       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12985         cp_parser_simulate_error (parser);
12986       if (!cp_parser_parse_definitely (parser))
12987         return false;
12988     }
12989
12990   /* The function we call to handle a using-declaration is different
12991      depending on what scope we are in.  */
12992   if (qscope == error_mark_node || identifier == error_mark_node)
12993     ;
12994   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12995            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12996     /* [namespace.udecl]
12997
12998        A using declaration shall not name a template-id.  */
12999     error_at (token->location,
13000               "a template-id may not appear in a using-declaration");
13001   else
13002     {
13003       if (at_class_scope_p ())
13004         {
13005           /* Create the USING_DECL.  */
13006           decl = do_class_using_decl (parser->scope, identifier);
13007
13008           if (check_for_bare_parameter_packs (decl))
13009             return false;
13010           else
13011             /* Add it to the list of members in this class.  */
13012             finish_member_declaration (decl);
13013         }
13014       else
13015         {
13016           decl = cp_parser_lookup_name_simple (parser,
13017                                                identifier,
13018                                                token->location);
13019           if (decl == error_mark_node)
13020             cp_parser_name_lookup_error (parser, identifier,
13021                                          decl, NULL,
13022                                          token->location);
13023           else if (check_for_bare_parameter_packs (decl))
13024             return false;
13025           else if (!at_namespace_scope_p ())
13026             do_local_using_decl (decl, qscope, identifier);
13027           else
13028             do_toplevel_using_decl (decl, qscope, identifier);
13029         }
13030     }
13031
13032   /* Look for the final `;'.  */
13033   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13034   
13035   return true;
13036 }
13037
13038 /* Parse a using-directive.
13039
13040    using-directive:
13041      using namespace :: [opt] nested-name-specifier [opt]
13042        namespace-name ;  */
13043
13044 static void
13045 cp_parser_using_directive (cp_parser* parser)
13046 {
13047   tree namespace_decl;
13048   tree attribs;
13049
13050   /* Look for the `using' keyword.  */
13051   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13052   /* And the `namespace' keyword.  */
13053   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13054   /* Look for the optional `::' operator.  */
13055   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13056   /* And the optional nested-name-specifier.  */
13057   cp_parser_nested_name_specifier_opt (parser,
13058                                        /*typename_keyword_p=*/false,
13059                                        /*check_dependency_p=*/true,
13060                                        /*type_p=*/false,
13061                                        /*is_declaration=*/true);
13062   /* Get the namespace being used.  */
13063   namespace_decl = cp_parser_namespace_name (parser);
13064   /* And any specified attributes.  */
13065   attribs = cp_parser_attributes_opt (parser);
13066   /* Update the symbol table.  */
13067   parse_using_directive (namespace_decl, attribs);
13068   /* Look for the final `;'.  */
13069   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13070 }
13071
13072 /* Parse an asm-definition.
13073
13074    asm-definition:
13075      asm ( string-literal ) ;
13076
13077    GNU Extension:
13078
13079    asm-definition:
13080      asm volatile [opt] ( string-literal ) ;
13081      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13082      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13083                           : asm-operand-list [opt] ) ;
13084      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13085                           : asm-operand-list [opt]
13086                           : asm-clobber-list [opt] ) ;
13087      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13088                                : asm-clobber-list [opt]
13089                                : asm-goto-list ) ;  */
13090
13091 static void
13092 cp_parser_asm_definition (cp_parser* parser)
13093 {
13094   tree string;
13095   tree outputs = NULL_TREE;
13096   tree inputs = NULL_TREE;
13097   tree clobbers = NULL_TREE;
13098   tree labels = NULL_TREE;
13099   tree asm_stmt;
13100   bool volatile_p = false;
13101   bool extended_p = false;
13102   bool invalid_inputs_p = false;
13103   bool invalid_outputs_p = false;
13104   bool goto_p = false;
13105   const char *missing = NULL;
13106
13107   /* Look for the `asm' keyword.  */
13108   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13109   /* See if the next token is `volatile'.  */
13110   if (cp_parser_allow_gnu_extensions_p (parser)
13111       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13112     {
13113       /* Remember that we saw the `volatile' keyword.  */
13114       volatile_p = true;
13115       /* Consume the token.  */
13116       cp_lexer_consume_token (parser->lexer);
13117     }
13118   if (cp_parser_allow_gnu_extensions_p (parser)
13119       && parser->in_function_body
13120       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13121     {
13122       /* Remember that we saw the `goto' keyword.  */
13123       goto_p = true;
13124       /* Consume the token.  */
13125       cp_lexer_consume_token (parser->lexer);
13126     }
13127   /* Look for the opening `('.  */
13128   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13129     return;
13130   /* Look for the string.  */
13131   string = cp_parser_string_literal (parser, false, false);
13132   if (string == error_mark_node)
13133     {
13134       cp_parser_skip_to_closing_parenthesis (parser, true, false,
13135                                              /*consume_paren=*/true);
13136       return;
13137     }
13138
13139   /* If we're allowing GNU extensions, check for the extended assembly
13140      syntax.  Unfortunately, the `:' tokens need not be separated by
13141      a space in C, and so, for compatibility, we tolerate that here
13142      too.  Doing that means that we have to treat the `::' operator as
13143      two `:' tokens.  */
13144   if (cp_parser_allow_gnu_extensions_p (parser)
13145       && parser->in_function_body
13146       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13147           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13148     {
13149       bool inputs_p = false;
13150       bool clobbers_p = false;
13151       bool labels_p = false;
13152
13153       /* The extended syntax was used.  */
13154       extended_p = true;
13155
13156       /* Look for outputs.  */
13157       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13158         {
13159           /* Consume the `:'.  */
13160           cp_lexer_consume_token (parser->lexer);
13161           /* Parse the output-operands.  */
13162           if (cp_lexer_next_token_is_not (parser->lexer,
13163                                           CPP_COLON)
13164               && cp_lexer_next_token_is_not (parser->lexer,
13165                                              CPP_SCOPE)
13166               && cp_lexer_next_token_is_not (parser->lexer,
13167                                              CPP_CLOSE_PAREN)
13168               && !goto_p)
13169             outputs = cp_parser_asm_operand_list (parser);
13170
13171             if (outputs == error_mark_node)
13172               invalid_outputs_p = true;
13173         }
13174       /* If the next token is `::', there are no outputs, and the
13175          next token is the beginning of the inputs.  */
13176       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13177         /* The inputs are coming next.  */
13178         inputs_p = true;
13179
13180       /* Look for inputs.  */
13181       if (inputs_p
13182           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13183         {
13184           /* Consume the `:' or `::'.  */
13185           cp_lexer_consume_token (parser->lexer);
13186           /* Parse the output-operands.  */
13187           if (cp_lexer_next_token_is_not (parser->lexer,
13188                                           CPP_COLON)
13189               && cp_lexer_next_token_is_not (parser->lexer,
13190                                              CPP_SCOPE)
13191               && cp_lexer_next_token_is_not (parser->lexer,
13192                                              CPP_CLOSE_PAREN))
13193             inputs = cp_parser_asm_operand_list (parser);
13194
13195             if (inputs == error_mark_node)
13196               invalid_inputs_p = true;
13197         }
13198       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13199         /* The clobbers are coming next.  */
13200         clobbers_p = true;
13201
13202       /* Look for clobbers.  */
13203       if (clobbers_p
13204           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13205         {
13206           clobbers_p = true;
13207           /* Consume the `:' or `::'.  */
13208           cp_lexer_consume_token (parser->lexer);
13209           /* Parse the clobbers.  */
13210           if (cp_lexer_next_token_is_not (parser->lexer,
13211                                           CPP_COLON)
13212               && cp_lexer_next_token_is_not (parser->lexer,
13213                                              CPP_CLOSE_PAREN))
13214             clobbers = cp_parser_asm_clobber_list (parser);
13215         }
13216       else if (goto_p
13217                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13218         /* The labels are coming next.  */
13219         labels_p = true;
13220
13221       /* Look for labels.  */
13222       if (labels_p
13223           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13224         {
13225           labels_p = true;
13226           /* Consume the `:' or `::'.  */
13227           cp_lexer_consume_token (parser->lexer);
13228           /* Parse the labels.  */
13229           labels = cp_parser_asm_label_list (parser);
13230         }
13231
13232       if (goto_p && !labels_p)
13233         missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13234     }
13235   else if (goto_p)
13236     missing = "%<:%> or %<::%>";
13237
13238   /* Look for the closing `)'.  */
13239   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13240                           missing ? missing : "%<)%>"))
13241     cp_parser_skip_to_closing_parenthesis (parser, true, false,
13242                                            /*consume_paren=*/true);
13243   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13244
13245   if (!invalid_inputs_p && !invalid_outputs_p)
13246     {
13247       /* Create the ASM_EXPR.  */
13248       if (parser->in_function_body)
13249         {
13250           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13251                                       inputs, clobbers, labels);
13252           /* If the extended syntax was not used, mark the ASM_EXPR.  */
13253           if (!extended_p)
13254             {
13255               tree temp = asm_stmt;
13256               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13257                 temp = TREE_OPERAND (temp, 0);
13258
13259               ASM_INPUT_P (temp) = 1;
13260             }
13261         }
13262       else
13263         cgraph_add_asm_node (string);
13264     }
13265 }
13266
13267 /* Declarators [gram.dcl.decl] */
13268
13269 /* Parse an init-declarator.
13270
13271    init-declarator:
13272      declarator initializer [opt]
13273
13274    GNU Extension:
13275
13276    init-declarator:
13277      declarator asm-specification [opt] attributes [opt] initializer [opt]
13278
13279    function-definition:
13280      decl-specifier-seq [opt] declarator ctor-initializer [opt]
13281        function-body
13282      decl-specifier-seq [opt] declarator function-try-block
13283
13284    GNU Extension:
13285
13286    function-definition:
13287      __extension__ function-definition
13288
13289    The DECL_SPECIFIERS apply to this declarator.  Returns a
13290    representation of the entity declared.  If MEMBER_P is TRUE, then
13291    this declarator appears in a class scope.  The new DECL created by
13292    this declarator is returned.
13293
13294    The CHECKS are access checks that should be performed once we know
13295    what entity is being declared (and, therefore, what classes have
13296    befriended it).
13297
13298    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13299    for a function-definition here as well.  If the declarator is a
13300    declarator for a function-definition, *FUNCTION_DEFINITION_P will
13301    be TRUE upon return.  By that point, the function-definition will
13302    have been completely parsed.
13303
13304    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13305    is FALSE.  */
13306
13307 static tree
13308 cp_parser_init_declarator (cp_parser* parser,
13309                            cp_decl_specifier_seq *decl_specifiers,
13310                            VEC (deferred_access_check,gc)* checks,
13311                            bool function_definition_allowed_p,
13312                            bool member_p,
13313                            int declares_class_or_enum,
13314                            bool* function_definition_p)
13315 {
13316   cp_token *token = NULL, *asm_spec_start_token = NULL,
13317            *attributes_start_token = NULL;
13318   cp_declarator *declarator;
13319   tree prefix_attributes;
13320   tree attributes;
13321   tree asm_specification;
13322   tree initializer;
13323   tree decl = NULL_TREE;
13324   tree scope;
13325   int is_initialized;
13326   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
13327      initialized with "= ..", CPP_OPEN_PAREN if initialized with
13328      "(...)".  */
13329   enum cpp_ttype initialization_kind;
13330   bool is_direct_init = false;
13331   bool is_non_constant_init;
13332   int ctor_dtor_or_conv_p;
13333   bool friend_p;
13334   tree pushed_scope = NULL;
13335
13336   /* Gather the attributes that were provided with the
13337      decl-specifiers.  */
13338   prefix_attributes = decl_specifiers->attributes;
13339
13340   /* Assume that this is not the declarator for a function
13341      definition.  */
13342   if (function_definition_p)
13343     *function_definition_p = false;
13344
13345   /* Defer access checks while parsing the declarator; we cannot know
13346      what names are accessible until we know what is being
13347      declared.  */
13348   resume_deferring_access_checks ();
13349
13350   /* Parse the declarator.  */
13351   token = cp_lexer_peek_token (parser->lexer);
13352   declarator
13353     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13354                             &ctor_dtor_or_conv_p,
13355                             /*parenthesized_p=*/NULL,
13356                             /*member_p=*/false);
13357   /* Gather up the deferred checks.  */
13358   stop_deferring_access_checks ();
13359
13360   /* If the DECLARATOR was erroneous, there's no need to go
13361      further.  */
13362   if (declarator == cp_error_declarator)
13363     return error_mark_node;
13364
13365   /* Check that the number of template-parameter-lists is OK.  */
13366   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13367                                                        token->location))
13368     return error_mark_node;
13369
13370   if (declares_class_or_enum & 2)
13371     cp_parser_check_for_definition_in_return_type (declarator,
13372                                                    decl_specifiers->type,
13373                                                    decl_specifiers->type_location);
13374
13375   /* Figure out what scope the entity declared by the DECLARATOR is
13376      located in.  `grokdeclarator' sometimes changes the scope, so
13377      we compute it now.  */
13378   scope = get_scope_of_declarator (declarator);
13379
13380   /* If we're allowing GNU extensions, look for an asm-specification
13381      and attributes.  */
13382   if (cp_parser_allow_gnu_extensions_p (parser))
13383     {
13384       /* Look for an asm-specification.  */
13385       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13386       asm_specification = cp_parser_asm_specification_opt (parser);
13387       /* And attributes.  */
13388       attributes_start_token = cp_lexer_peek_token (parser->lexer);
13389       attributes = cp_parser_attributes_opt (parser);
13390     }
13391   else
13392     {
13393       asm_specification = NULL_TREE;
13394       attributes = NULL_TREE;
13395     }
13396
13397   /* Peek at the next token.  */
13398   token = cp_lexer_peek_token (parser->lexer);
13399   /* Check to see if the token indicates the start of a
13400      function-definition.  */
13401   if (function_declarator_p (declarator)
13402       && cp_parser_token_starts_function_definition_p (token))
13403     {
13404       if (!function_definition_allowed_p)
13405         {
13406           /* If a function-definition should not appear here, issue an
13407              error message.  */
13408           cp_parser_error (parser,
13409                            "a function-definition is not allowed here");
13410           return error_mark_node;
13411         }
13412       else
13413         {
13414           location_t func_brace_location
13415             = cp_lexer_peek_token (parser->lexer)->location;
13416
13417           /* Neither attributes nor an asm-specification are allowed
13418              on a function-definition.  */
13419           if (asm_specification)
13420             error_at (asm_spec_start_token->location,
13421                       "an asm-specification is not allowed "
13422                       "on a function-definition");
13423           if (attributes)
13424             error_at (attributes_start_token->location,
13425                       "attributes are not allowed on a function-definition");
13426           /* This is a function-definition.  */
13427           *function_definition_p = true;
13428
13429           /* Parse the function definition.  */
13430           if (member_p)
13431             decl = cp_parser_save_member_function_body (parser,
13432                                                         decl_specifiers,
13433                                                         declarator,
13434                                                         prefix_attributes);
13435           else
13436             decl
13437               = (cp_parser_function_definition_from_specifiers_and_declarator
13438                  (parser, decl_specifiers, prefix_attributes, declarator));
13439
13440           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13441             {
13442               /* This is where the prologue starts...  */
13443               DECL_STRUCT_FUNCTION (decl)->function_start_locus
13444                 = func_brace_location;
13445             }
13446
13447           return decl;
13448         }
13449     }
13450
13451   /* [dcl.dcl]
13452
13453      Only in function declarations for constructors, destructors, and
13454      type conversions can the decl-specifier-seq be omitted.
13455
13456      We explicitly postpone this check past the point where we handle
13457      function-definitions because we tolerate function-definitions
13458      that are missing their return types in some modes.  */
13459   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13460     {
13461       cp_parser_error (parser,
13462                        "expected constructor, destructor, or type conversion");
13463       return error_mark_node;
13464     }
13465
13466   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
13467   if (token->type == CPP_EQ
13468       || token->type == CPP_OPEN_PAREN
13469       || token->type == CPP_OPEN_BRACE)
13470     {
13471       is_initialized = SD_INITIALIZED;
13472       initialization_kind = token->type;
13473
13474       if (token->type == CPP_EQ
13475           && function_declarator_p (declarator))
13476         {
13477           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13478           if (t2->keyword == RID_DEFAULT)
13479             is_initialized = SD_DEFAULTED;
13480           else if (t2->keyword == RID_DELETE)
13481             is_initialized = SD_DELETED;
13482         }
13483     }
13484   else
13485     {
13486       /* If the init-declarator isn't initialized and isn't followed by a
13487          `,' or `;', it's not a valid init-declarator.  */
13488       if (token->type != CPP_COMMA
13489           && token->type != CPP_SEMICOLON)
13490         {
13491           cp_parser_error (parser, "expected initializer");
13492           return error_mark_node;
13493         }
13494       is_initialized = SD_UNINITIALIZED;
13495       initialization_kind = CPP_EOF;
13496     }
13497
13498   /* Because start_decl has side-effects, we should only call it if we
13499      know we're going ahead.  By this point, we know that we cannot
13500      possibly be looking at any other construct.  */
13501   cp_parser_commit_to_tentative_parse (parser);
13502
13503   /* If the decl specifiers were bad, issue an error now that we're
13504      sure this was intended to be a declarator.  Then continue
13505      declaring the variable(s), as int, to try to cut down on further
13506      errors.  */
13507   if (decl_specifiers->any_specifiers_p
13508       && decl_specifiers->type == error_mark_node)
13509     {
13510       cp_parser_error (parser, "invalid type in declaration");
13511       decl_specifiers->type = integer_type_node;
13512     }
13513
13514   /* Check to see whether or not this declaration is a friend.  */
13515   friend_p = cp_parser_friend_p (decl_specifiers);
13516
13517   /* Enter the newly declared entry in the symbol table.  If we're
13518      processing a declaration in a class-specifier, we wait until
13519      after processing the initializer.  */
13520   if (!member_p)
13521     {
13522       if (parser->in_unbraced_linkage_specification_p)
13523         decl_specifiers->storage_class = sc_extern;
13524       decl = start_decl (declarator, decl_specifiers,
13525                          is_initialized, attributes, prefix_attributes,
13526                          &pushed_scope);
13527     }
13528   else if (scope)
13529     /* Enter the SCOPE.  That way unqualified names appearing in the
13530        initializer will be looked up in SCOPE.  */
13531     pushed_scope = push_scope (scope);
13532
13533   /* Perform deferred access control checks, now that we know in which
13534      SCOPE the declared entity resides.  */
13535   if (!member_p && decl)
13536     {
13537       tree saved_current_function_decl = NULL_TREE;
13538
13539       /* If the entity being declared is a function, pretend that we
13540          are in its scope.  If it is a `friend', it may have access to
13541          things that would not otherwise be accessible.  */
13542       if (TREE_CODE (decl) == FUNCTION_DECL)
13543         {
13544           saved_current_function_decl = current_function_decl;
13545           current_function_decl = decl;
13546         }
13547
13548       /* Perform access checks for template parameters.  */
13549       cp_parser_perform_template_parameter_access_checks (checks);
13550
13551       /* Perform the access control checks for the declarator and the
13552          decl-specifiers.  */
13553       perform_deferred_access_checks ();
13554
13555       /* Restore the saved value.  */
13556       if (TREE_CODE (decl) == FUNCTION_DECL)
13557         current_function_decl = saved_current_function_decl;
13558     }
13559
13560   /* Parse the initializer.  */
13561   initializer = NULL_TREE;
13562   is_direct_init = false;
13563   is_non_constant_init = true;
13564   if (is_initialized)
13565     {
13566       if (function_declarator_p (declarator))
13567         {
13568           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13569            if (initialization_kind == CPP_EQ)
13570              initializer = cp_parser_pure_specifier (parser);
13571            else
13572              {
13573                /* If the declaration was erroneous, we don't really
13574                   know what the user intended, so just silently
13575                   consume the initializer.  */
13576                if (decl != error_mark_node)
13577                  error_at (initializer_start_token->location,
13578                            "initializer provided for function");
13579                cp_parser_skip_to_closing_parenthesis (parser,
13580                                                       /*recovering=*/true,
13581                                                       /*or_comma=*/false,
13582                                                       /*consume_paren=*/true);
13583              }
13584         }
13585       else
13586         {
13587           /* We want to record the extra mangling scope for in-class
13588              initializers of class members and initializers of static data
13589              member templates.  The former is a C++0x feature which isn't
13590              implemented yet, and I expect it will involve deferring
13591              parsing of the initializer until end of class as with default
13592              arguments.  So right here we only handle the latter.  */
13593           if (!member_p && processing_template_decl)
13594             start_lambda_scope (decl);
13595           initializer = cp_parser_initializer (parser,
13596                                                &is_direct_init,
13597                                                &is_non_constant_init);
13598           if (!member_p && processing_template_decl)
13599             finish_lambda_scope ();
13600         }
13601     }
13602
13603   /* The old parser allows attributes to appear after a parenthesized
13604      initializer.  Mark Mitchell proposed removing this functionality
13605      on the GCC mailing lists on 2002-08-13.  This parser accepts the
13606      attributes -- but ignores them.  */
13607   if (cp_parser_allow_gnu_extensions_p (parser)
13608       && initialization_kind == CPP_OPEN_PAREN)
13609     if (cp_parser_attributes_opt (parser))
13610       warning (OPT_Wattributes,
13611                "attributes after parenthesized initializer ignored");
13612
13613   /* For an in-class declaration, use `grokfield' to create the
13614      declaration.  */
13615   if (member_p)
13616     {
13617       if (pushed_scope)
13618         {
13619           pop_scope (pushed_scope);
13620           pushed_scope = false;
13621         }
13622       decl = grokfield (declarator, decl_specifiers,
13623                         initializer, !is_non_constant_init,
13624                         /*asmspec=*/NULL_TREE,
13625                         prefix_attributes);
13626       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13627         cp_parser_save_default_args (parser, decl);
13628     }
13629
13630   /* Finish processing the declaration.  But, skip friend
13631      declarations.  */
13632   if (!friend_p && decl && decl != error_mark_node)
13633     {
13634       cp_finish_decl (decl,
13635                       initializer, !is_non_constant_init,
13636                       asm_specification,
13637                       /* If the initializer is in parentheses, then this is
13638                          a direct-initialization, which means that an
13639                          `explicit' constructor is OK.  Otherwise, an
13640                          `explicit' constructor cannot be used.  */
13641                       ((is_direct_init || !is_initialized)
13642                        ? 0 : LOOKUP_ONLYCONVERTING));
13643     }
13644   else if ((cxx_dialect != cxx98) && friend_p
13645            && decl && TREE_CODE (decl) == FUNCTION_DECL)
13646     /* Core issue #226 (C++0x only): A default template-argument
13647        shall not be specified in a friend class template
13648        declaration. */
13649     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
13650                              /*is_partial=*/0, /*is_friend_decl=*/1);
13651
13652   if (!friend_p && pushed_scope)
13653     pop_scope (pushed_scope);
13654
13655   return decl;
13656 }
13657
13658 /* Parse a declarator.
13659
13660    declarator:
13661      direct-declarator
13662      ptr-operator declarator
13663
13664    abstract-declarator:
13665      ptr-operator abstract-declarator [opt]
13666      direct-abstract-declarator
13667
13668    GNU Extensions:
13669
13670    declarator:
13671      attributes [opt] direct-declarator
13672      attributes [opt] ptr-operator declarator
13673
13674    abstract-declarator:
13675      attributes [opt] ptr-operator abstract-declarator [opt]
13676      attributes [opt] direct-abstract-declarator
13677
13678    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13679    detect constructor, destructor or conversion operators. It is set
13680    to -1 if the declarator is a name, and +1 if it is a
13681    function. Otherwise it is set to zero. Usually you just want to
13682    test for >0, but internally the negative value is used.
13683
13684    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13685    a decl-specifier-seq unless it declares a constructor, destructor,
13686    or conversion.  It might seem that we could check this condition in
13687    semantic analysis, rather than parsing, but that makes it difficult
13688    to handle something like `f()'.  We want to notice that there are
13689    no decl-specifiers, and therefore realize that this is an
13690    expression, not a declaration.)
13691
13692    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13693    the declarator is a direct-declarator of the form "(...)".
13694
13695    MEMBER_P is true iff this declarator is a member-declarator.  */
13696
13697 static cp_declarator *
13698 cp_parser_declarator (cp_parser* parser,
13699                       cp_parser_declarator_kind dcl_kind,
13700                       int* ctor_dtor_or_conv_p,
13701                       bool* parenthesized_p,
13702                       bool member_p)
13703 {
13704   cp_token *token;
13705   cp_declarator *declarator;
13706   enum tree_code code;
13707   cp_cv_quals cv_quals;
13708   tree class_type;
13709   tree attributes = NULL_TREE;
13710
13711   /* Assume this is not a constructor, destructor, or type-conversion
13712      operator.  */
13713   if (ctor_dtor_or_conv_p)
13714     *ctor_dtor_or_conv_p = 0;
13715
13716   if (cp_parser_allow_gnu_extensions_p (parser))
13717     attributes = cp_parser_attributes_opt (parser);
13718
13719   /* Peek at the next token.  */
13720   token = cp_lexer_peek_token (parser->lexer);
13721
13722   /* Check for the ptr-operator production.  */
13723   cp_parser_parse_tentatively (parser);
13724   /* Parse the ptr-operator.  */
13725   code = cp_parser_ptr_operator (parser,
13726                                  &class_type,
13727                                  &cv_quals);
13728   /* If that worked, then we have a ptr-operator.  */
13729   if (cp_parser_parse_definitely (parser))
13730     {
13731       /* If a ptr-operator was found, then this declarator was not
13732          parenthesized.  */
13733       if (parenthesized_p)
13734         *parenthesized_p = true;
13735       /* The dependent declarator is optional if we are parsing an
13736          abstract-declarator.  */
13737       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13738         cp_parser_parse_tentatively (parser);
13739
13740       /* Parse the dependent declarator.  */
13741       declarator = cp_parser_declarator (parser, dcl_kind,
13742                                          /*ctor_dtor_or_conv_p=*/NULL,
13743                                          /*parenthesized_p=*/NULL,
13744                                          /*member_p=*/false);
13745
13746       /* If we are parsing an abstract-declarator, we must handle the
13747          case where the dependent declarator is absent.  */
13748       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13749           && !cp_parser_parse_definitely (parser))
13750         declarator = NULL;
13751
13752       declarator = cp_parser_make_indirect_declarator
13753         (code, class_type, cv_quals, declarator);
13754     }
13755   /* Everything else is a direct-declarator.  */
13756   else
13757     {
13758       if (parenthesized_p)
13759         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13760                                                    CPP_OPEN_PAREN);
13761       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13762                                                 ctor_dtor_or_conv_p,
13763                                                 member_p);
13764     }
13765
13766   if (attributes && declarator && declarator != cp_error_declarator)
13767     declarator->attributes = attributes;
13768
13769   return declarator;
13770 }
13771
13772 /* Parse a direct-declarator or direct-abstract-declarator.
13773
13774    direct-declarator:
13775      declarator-id
13776      direct-declarator ( parameter-declaration-clause )
13777        cv-qualifier-seq [opt]
13778        exception-specification [opt]
13779      direct-declarator [ constant-expression [opt] ]
13780      ( declarator )
13781
13782    direct-abstract-declarator:
13783      direct-abstract-declarator [opt]
13784        ( parameter-declaration-clause )
13785        cv-qualifier-seq [opt]
13786        exception-specification [opt]
13787      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13788      ( abstract-declarator )
13789
13790    Returns a representation of the declarator.  DCL_KIND is
13791    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13792    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13793    we are parsing a direct-declarator.  It is
13794    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13795    of ambiguity we prefer an abstract declarator, as per
13796    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13797    cp_parser_declarator.  */
13798
13799 static cp_declarator *
13800 cp_parser_direct_declarator (cp_parser* parser,
13801                              cp_parser_declarator_kind dcl_kind,
13802                              int* ctor_dtor_or_conv_p,
13803                              bool member_p)
13804 {
13805   cp_token *token;
13806   cp_declarator *declarator = NULL;
13807   tree scope = NULL_TREE;
13808   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13809   bool saved_in_declarator_p = parser->in_declarator_p;
13810   bool first = true;
13811   tree pushed_scope = NULL_TREE;
13812
13813   while (true)
13814     {
13815       /* Peek at the next token.  */
13816       token = cp_lexer_peek_token (parser->lexer);
13817       if (token->type == CPP_OPEN_PAREN)
13818         {
13819           /* This is either a parameter-declaration-clause, or a
13820              parenthesized declarator. When we know we are parsing a
13821              named declarator, it must be a parenthesized declarator
13822              if FIRST is true. For instance, `(int)' is a
13823              parameter-declaration-clause, with an omitted
13824              direct-abstract-declarator. But `((*))', is a
13825              parenthesized abstract declarator. Finally, when T is a
13826              template parameter `(T)' is a
13827              parameter-declaration-clause, and not a parenthesized
13828              named declarator.
13829
13830              We first try and parse a parameter-declaration-clause,
13831              and then try a nested declarator (if FIRST is true).
13832
13833              It is not an error for it not to be a
13834              parameter-declaration-clause, even when FIRST is
13835              false. Consider,
13836
13837                int i (int);
13838                int i (3);
13839
13840              The first is the declaration of a function while the
13841              second is the definition of a variable, including its
13842              initializer.
13843
13844              Having seen only the parenthesis, we cannot know which of
13845              these two alternatives should be selected.  Even more
13846              complex are examples like:
13847
13848                int i (int (a));
13849                int i (int (3));
13850
13851              The former is a function-declaration; the latter is a
13852              variable initialization.
13853
13854              Thus again, we try a parameter-declaration-clause, and if
13855              that fails, we back out and return.  */
13856
13857           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13858             {
13859               tree params;
13860               unsigned saved_num_template_parameter_lists;
13861               bool is_declarator = false;
13862               tree t;
13863
13864               /* In a member-declarator, the only valid interpretation
13865                  of a parenthesis is the start of a
13866                  parameter-declaration-clause.  (It is invalid to
13867                  initialize a static data member with a parenthesized
13868                  initializer; only the "=" form of initialization is
13869                  permitted.)  */
13870               if (!member_p)
13871                 cp_parser_parse_tentatively (parser);
13872
13873               /* Consume the `('.  */
13874               cp_lexer_consume_token (parser->lexer);
13875               if (first)
13876                 {
13877                   /* If this is going to be an abstract declarator, we're
13878                      in a declarator and we can't have default args.  */
13879                   parser->default_arg_ok_p = false;
13880                   parser->in_declarator_p = true;
13881                 }
13882
13883               /* Inside the function parameter list, surrounding
13884                  template-parameter-lists do not apply.  */
13885               saved_num_template_parameter_lists
13886                 = parser->num_template_parameter_lists;
13887               parser->num_template_parameter_lists = 0;
13888
13889               begin_scope (sk_function_parms, NULL_TREE);
13890
13891               /* Parse the parameter-declaration-clause.  */
13892               params = cp_parser_parameter_declaration_clause (parser);
13893
13894               parser->num_template_parameter_lists
13895                 = saved_num_template_parameter_lists;
13896
13897               /* If all went well, parse the cv-qualifier-seq and the
13898                  exception-specification.  */
13899               if (member_p || cp_parser_parse_definitely (parser))
13900                 {
13901                   cp_cv_quals cv_quals;
13902                   tree exception_specification;
13903                   tree late_return;
13904
13905                   is_declarator = true;
13906
13907                   if (ctor_dtor_or_conv_p)
13908                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13909                   first = false;
13910                   /* Consume the `)'.  */
13911                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13912
13913                   /* Parse the cv-qualifier-seq.  */
13914                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13915                   /* And the exception-specification.  */
13916                   exception_specification
13917                     = cp_parser_exception_specification_opt (parser);
13918
13919                   late_return
13920                     = cp_parser_late_return_type_opt (parser);
13921
13922                   /* Create the function-declarator.  */
13923                   declarator = make_call_declarator (declarator,
13924                                                      params,
13925                                                      cv_quals,
13926                                                      exception_specification,
13927                                                      late_return);
13928                   /* Any subsequent parameter lists are to do with
13929                      return type, so are not those of the declared
13930                      function.  */
13931                   parser->default_arg_ok_p = false;
13932                 }
13933
13934               /* Remove the function parms from scope.  */
13935               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13936                 pop_binding (DECL_NAME (t), t);
13937               leave_scope();
13938
13939               if (is_declarator)
13940                 /* Repeat the main loop.  */
13941                 continue;
13942             }
13943
13944           /* If this is the first, we can try a parenthesized
13945              declarator.  */
13946           if (first)
13947             {
13948               bool saved_in_type_id_in_expr_p;
13949
13950               parser->default_arg_ok_p = saved_default_arg_ok_p;
13951               parser->in_declarator_p = saved_in_declarator_p;
13952
13953               /* Consume the `('.  */
13954               cp_lexer_consume_token (parser->lexer);
13955               /* Parse the nested declarator.  */
13956               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13957               parser->in_type_id_in_expr_p = true;
13958               declarator
13959                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13960                                         /*parenthesized_p=*/NULL,
13961                                         member_p);
13962               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13963               first = false;
13964               /* Expect a `)'.  */
13965               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13966                 declarator = cp_error_declarator;
13967               if (declarator == cp_error_declarator)
13968                 break;
13969
13970               goto handle_declarator;
13971             }
13972           /* Otherwise, we must be done.  */
13973           else
13974             break;
13975         }
13976       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13977                && token->type == CPP_OPEN_SQUARE)
13978         {
13979           /* Parse an array-declarator.  */
13980           tree bounds;
13981
13982           if (ctor_dtor_or_conv_p)
13983             *ctor_dtor_or_conv_p = 0;
13984
13985           first = false;
13986           parser->default_arg_ok_p = false;
13987           parser->in_declarator_p = true;
13988           /* Consume the `['.  */
13989           cp_lexer_consume_token (parser->lexer);
13990           /* Peek at the next token.  */
13991           token = cp_lexer_peek_token (parser->lexer);
13992           /* If the next token is `]', then there is no
13993              constant-expression.  */
13994           if (token->type != CPP_CLOSE_SQUARE)
13995             {
13996               bool non_constant_p;
13997
13998               bounds
13999                 = cp_parser_constant_expression (parser,
14000                                                  /*allow_non_constant=*/true,
14001                                                  &non_constant_p);
14002               if (!non_constant_p)
14003                 bounds = fold_non_dependent_expr (bounds);
14004               /* Normally, the array bound must be an integral constant
14005                  expression.  However, as an extension, we allow VLAs
14006                  in function scopes.  */
14007               else if (!parser->in_function_body)
14008                 {
14009                   error_at (token->location,
14010                             "array bound is not an integer constant");
14011                   bounds = error_mark_node;
14012                 }
14013               else if (processing_template_decl && !error_operand_p (bounds))
14014                 {
14015                   /* Remember this wasn't a constant-expression.  */
14016                   bounds = build_nop (TREE_TYPE (bounds), bounds);
14017                   TREE_SIDE_EFFECTS (bounds) = 1;
14018                 }
14019             }
14020           else
14021             bounds = NULL_TREE;
14022           /* Look for the closing `]'.  */
14023           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14024             {
14025               declarator = cp_error_declarator;
14026               break;
14027             }
14028
14029           declarator = make_array_declarator (declarator, bounds);
14030         }
14031       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14032         {
14033           {
14034             tree qualifying_scope;
14035             tree unqualified_name;
14036             special_function_kind sfk;
14037             bool abstract_ok;
14038             bool pack_expansion_p = false;
14039             cp_token *declarator_id_start_token;
14040
14041             /* Parse a declarator-id */
14042             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14043             if (abstract_ok)
14044               {
14045                 cp_parser_parse_tentatively (parser);
14046
14047                 /* If we see an ellipsis, we should be looking at a
14048                    parameter pack. */
14049                 if (token->type == CPP_ELLIPSIS)
14050                   {
14051                     /* Consume the `...' */
14052                     cp_lexer_consume_token (parser->lexer);
14053
14054                     pack_expansion_p = true;
14055                   }
14056               }
14057
14058             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14059             unqualified_name
14060               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14061             qualifying_scope = parser->scope;
14062             if (abstract_ok)
14063               {
14064                 bool okay = false;
14065
14066                 if (!unqualified_name && pack_expansion_p)
14067                   {
14068                     /* Check whether an error occurred. */
14069                     okay = !cp_parser_error_occurred (parser);
14070
14071                     /* We already consumed the ellipsis to mark a
14072                        parameter pack, but we have no way to report it,
14073                        so abort the tentative parse. We will be exiting
14074                        immediately anyway. */
14075                     cp_parser_abort_tentative_parse (parser);
14076                   }
14077                 else
14078                   okay = cp_parser_parse_definitely (parser);
14079
14080                 if (!okay)
14081                   unqualified_name = error_mark_node;
14082                 else if (unqualified_name
14083                          && (qualifying_scope
14084                              || (TREE_CODE (unqualified_name)
14085                                  != IDENTIFIER_NODE)))
14086                   {
14087                     cp_parser_error (parser, "expected unqualified-id");
14088                     unqualified_name = error_mark_node;
14089                   }
14090               }
14091
14092             if (!unqualified_name)
14093               return NULL;
14094             if (unqualified_name == error_mark_node)
14095               {
14096                 declarator = cp_error_declarator;
14097                 pack_expansion_p = false;
14098                 declarator->parameter_pack_p = false;
14099                 break;
14100               }
14101
14102             if (qualifying_scope && at_namespace_scope_p ()
14103                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14104               {
14105                 /* In the declaration of a member of a template class
14106                    outside of the class itself, the SCOPE will sometimes
14107                    be a TYPENAME_TYPE.  For example, given:
14108
14109                    template <typename T>
14110                    int S<T>::R::i = 3;
14111
14112                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
14113                    this context, we must resolve S<T>::R to an ordinary
14114                    type, rather than a typename type.
14115
14116                    The reason we normally avoid resolving TYPENAME_TYPEs
14117                    is that a specialization of `S' might render
14118                    `S<T>::R' not a type.  However, if `S' is
14119                    specialized, then this `i' will not be used, so there
14120                    is no harm in resolving the types here.  */
14121                 tree type;
14122
14123                 /* Resolve the TYPENAME_TYPE.  */
14124                 type = resolve_typename_type (qualifying_scope,
14125                                               /*only_current_p=*/false);
14126                 /* If that failed, the declarator is invalid.  */
14127                 if (TREE_CODE (type) == TYPENAME_TYPE)
14128                   error_at (declarator_id_start_token->location,
14129                             "%<%T::%E%> is not a type",
14130                             TYPE_CONTEXT (qualifying_scope),
14131                             TYPE_IDENTIFIER (qualifying_scope));
14132                 qualifying_scope = type;
14133               }
14134
14135             sfk = sfk_none;
14136
14137             if (unqualified_name)
14138               {
14139                 tree class_type;
14140
14141                 if (qualifying_scope
14142                     && CLASS_TYPE_P (qualifying_scope))
14143                   class_type = qualifying_scope;
14144                 else
14145                   class_type = current_class_type;
14146
14147                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14148                   {
14149                     tree name_type = TREE_TYPE (unqualified_name);
14150                     if (class_type && same_type_p (name_type, class_type))
14151                       {
14152                         if (qualifying_scope
14153                             && CLASSTYPE_USE_TEMPLATE (name_type))
14154                           {
14155                             error_at (declarator_id_start_token->location,
14156                                       "invalid use of constructor as a template");
14157                             inform (declarator_id_start_token->location,
14158                                     "use %<%T::%D%> instead of %<%T::%D%> to "
14159                                     "name the constructor in a qualified name",
14160                                     class_type,
14161                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14162                                     class_type, name_type);
14163                             declarator = cp_error_declarator;
14164                             break;
14165                           }
14166                         else
14167                           unqualified_name = constructor_name (class_type);
14168                       }
14169                     else
14170                       {
14171                         /* We do not attempt to print the declarator
14172                            here because we do not have enough
14173                            information about its original syntactic
14174                            form.  */
14175                         cp_parser_error (parser, "invalid declarator");
14176                         declarator = cp_error_declarator;
14177                         break;
14178                       }
14179                   }
14180
14181                 if (class_type)
14182                   {
14183                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14184                       sfk = sfk_destructor;
14185                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14186                       sfk = sfk_conversion;
14187                     else if (/* There's no way to declare a constructor
14188                                 for an anonymous type, even if the type
14189                                 got a name for linkage purposes.  */
14190                              !TYPE_WAS_ANONYMOUS (class_type)
14191                              && constructor_name_p (unqualified_name,
14192                                                     class_type))
14193                       {
14194                         unqualified_name = constructor_name (class_type);
14195                         sfk = sfk_constructor;
14196                       }
14197
14198                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
14199                       *ctor_dtor_or_conv_p = -1;
14200                   }
14201               }
14202             declarator = make_id_declarator (qualifying_scope,
14203                                              unqualified_name,
14204                                              sfk);
14205             declarator->id_loc = token->location;
14206             declarator->parameter_pack_p = pack_expansion_p;
14207
14208             if (pack_expansion_p)
14209               maybe_warn_variadic_templates ();
14210           }
14211
14212         handle_declarator:;
14213           scope = get_scope_of_declarator (declarator);
14214           if (scope)
14215             /* Any names that appear after the declarator-id for a
14216                member are looked up in the containing scope.  */
14217             pushed_scope = push_scope (scope);
14218           parser->in_declarator_p = true;
14219           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14220               || (declarator && declarator->kind == cdk_id))
14221             /* Default args are only allowed on function
14222                declarations.  */
14223             parser->default_arg_ok_p = saved_default_arg_ok_p;
14224           else
14225             parser->default_arg_ok_p = false;
14226
14227           first = false;
14228         }
14229       /* We're done.  */
14230       else
14231         break;
14232     }
14233
14234   /* For an abstract declarator, we might wind up with nothing at this
14235      point.  That's an error; the declarator is not optional.  */
14236   if (!declarator)
14237     cp_parser_error (parser, "expected declarator");
14238
14239   /* If we entered a scope, we must exit it now.  */
14240   if (pushed_scope)
14241     pop_scope (pushed_scope);
14242
14243   parser->default_arg_ok_p = saved_default_arg_ok_p;
14244   parser->in_declarator_p = saved_in_declarator_p;
14245
14246   return declarator;
14247 }
14248
14249 /* Parse a ptr-operator.
14250
14251    ptr-operator:
14252      * cv-qualifier-seq [opt]
14253      &
14254      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14255
14256    GNU Extension:
14257
14258    ptr-operator:
14259      & cv-qualifier-seq [opt]
14260
14261    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14262    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14263    an rvalue reference. In the case of a pointer-to-member, *TYPE is
14264    filled in with the TYPE containing the member.  *CV_QUALS is
14265    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14266    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
14267    Note that the tree codes returned by this function have nothing
14268    to do with the types of trees that will be eventually be created
14269    to represent the pointer or reference type being parsed. They are
14270    just constants with suggestive names. */
14271 static enum tree_code
14272 cp_parser_ptr_operator (cp_parser* parser,
14273                         tree* type,
14274                         cp_cv_quals *cv_quals)
14275 {
14276   enum tree_code code = ERROR_MARK;
14277   cp_token *token;
14278
14279   /* Assume that it's not a pointer-to-member.  */
14280   *type = NULL_TREE;
14281   /* And that there are no cv-qualifiers.  */
14282   *cv_quals = TYPE_UNQUALIFIED;
14283
14284   /* Peek at the next token.  */
14285   token = cp_lexer_peek_token (parser->lexer);
14286
14287   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
14288   if (token->type == CPP_MULT)
14289     code = INDIRECT_REF;
14290   else if (token->type == CPP_AND)
14291     code = ADDR_EXPR;
14292   else if ((cxx_dialect != cxx98) &&
14293            token->type == CPP_AND_AND) /* C++0x only */
14294     code = NON_LVALUE_EXPR;
14295
14296   if (code != ERROR_MARK)
14297     {
14298       /* Consume the `*', `&' or `&&'.  */
14299       cp_lexer_consume_token (parser->lexer);
14300
14301       /* A `*' can be followed by a cv-qualifier-seq, and so can a
14302          `&', if we are allowing GNU extensions.  (The only qualifier
14303          that can legally appear after `&' is `restrict', but that is
14304          enforced during semantic analysis.  */
14305       if (code == INDIRECT_REF
14306           || cp_parser_allow_gnu_extensions_p (parser))
14307         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14308     }
14309   else
14310     {
14311       /* Try the pointer-to-member case.  */
14312       cp_parser_parse_tentatively (parser);
14313       /* Look for the optional `::' operator.  */
14314       cp_parser_global_scope_opt (parser,
14315                                   /*current_scope_valid_p=*/false);
14316       /* Look for the nested-name specifier.  */
14317       token = cp_lexer_peek_token (parser->lexer);
14318       cp_parser_nested_name_specifier (parser,
14319                                        /*typename_keyword_p=*/false,
14320                                        /*check_dependency_p=*/true,
14321                                        /*type_p=*/false,
14322                                        /*is_declaration=*/false);
14323       /* If we found it, and the next token is a `*', then we are
14324          indeed looking at a pointer-to-member operator.  */
14325       if (!cp_parser_error_occurred (parser)
14326           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14327         {
14328           /* Indicate that the `*' operator was used.  */
14329           code = INDIRECT_REF;
14330
14331           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14332             error_at (token->location, "%qD is a namespace", parser->scope);
14333           else
14334             {
14335               /* The type of which the member is a member is given by the
14336                  current SCOPE.  */
14337               *type = parser->scope;
14338               /* The next name will not be qualified.  */
14339               parser->scope = NULL_TREE;
14340               parser->qualifying_scope = NULL_TREE;
14341               parser->object_scope = NULL_TREE;
14342               /* Look for the optional cv-qualifier-seq.  */
14343               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14344             }
14345         }
14346       /* If that didn't work we don't have a ptr-operator.  */
14347       if (!cp_parser_parse_definitely (parser))
14348         cp_parser_error (parser, "expected ptr-operator");
14349     }
14350
14351   return code;
14352 }
14353
14354 /* Parse an (optional) cv-qualifier-seq.
14355
14356    cv-qualifier-seq:
14357      cv-qualifier cv-qualifier-seq [opt]
14358
14359    cv-qualifier:
14360      const
14361      volatile
14362
14363    GNU Extension:
14364
14365    cv-qualifier:
14366      __restrict__
14367
14368    Returns a bitmask representing the cv-qualifiers.  */
14369
14370 static cp_cv_quals
14371 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14372 {
14373   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14374
14375   while (true)
14376     {
14377       cp_token *token;
14378       cp_cv_quals cv_qualifier;
14379
14380       /* Peek at the next token.  */
14381       token = cp_lexer_peek_token (parser->lexer);
14382       /* See if it's a cv-qualifier.  */
14383       switch (token->keyword)
14384         {
14385         case RID_CONST:
14386           cv_qualifier = TYPE_QUAL_CONST;
14387           break;
14388
14389         case RID_VOLATILE:
14390           cv_qualifier = TYPE_QUAL_VOLATILE;
14391           break;
14392
14393         case RID_RESTRICT:
14394           cv_qualifier = TYPE_QUAL_RESTRICT;
14395           break;
14396
14397         default:
14398           cv_qualifier = TYPE_UNQUALIFIED;
14399           break;
14400         }
14401
14402       if (!cv_qualifier)
14403         break;
14404
14405       if (cv_quals & cv_qualifier)
14406         {
14407           error_at (token->location, "duplicate cv-qualifier");
14408           cp_lexer_purge_token (parser->lexer);
14409         }
14410       else
14411         {
14412           cp_lexer_consume_token (parser->lexer);
14413           cv_quals |= cv_qualifier;
14414         }
14415     }
14416
14417   return cv_quals;
14418 }
14419
14420 /* Parse a late-specified return type, if any.  This is not a separate
14421    non-terminal, but part of a function declarator, which looks like
14422
14423    -> type-id
14424
14425    Returns the type indicated by the type-id.  */
14426
14427 static tree
14428 cp_parser_late_return_type_opt (cp_parser* parser)
14429 {
14430   cp_token *token;
14431
14432   /* Peek at the next token.  */
14433   token = cp_lexer_peek_token (parser->lexer);
14434   /* A late-specified return type is indicated by an initial '->'. */
14435   if (token->type != CPP_DEREF)
14436     return NULL_TREE;
14437
14438   /* Consume the ->.  */
14439   cp_lexer_consume_token (parser->lexer);
14440
14441   return cp_parser_type_id (parser);
14442 }
14443
14444 /* Parse a declarator-id.
14445
14446    declarator-id:
14447      id-expression
14448      :: [opt] nested-name-specifier [opt] type-name
14449
14450    In the `id-expression' case, the value returned is as for
14451    cp_parser_id_expression if the id-expression was an unqualified-id.
14452    If the id-expression was a qualified-id, then a SCOPE_REF is
14453    returned.  The first operand is the scope (either a NAMESPACE_DECL
14454    or TREE_TYPE), but the second is still just a representation of an
14455    unqualified-id.  */
14456
14457 static tree
14458 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14459 {
14460   tree id;
14461   /* The expression must be an id-expression.  Assume that qualified
14462      names are the names of types so that:
14463
14464        template <class T>
14465        int S<T>::R::i = 3;
14466
14467      will work; we must treat `S<T>::R' as the name of a type.
14468      Similarly, assume that qualified names are templates, where
14469      required, so that:
14470
14471        template <class T>
14472        int S<T>::R<T>::i = 3;
14473
14474      will work, too.  */
14475   id = cp_parser_id_expression (parser,
14476                                 /*template_keyword_p=*/false,
14477                                 /*check_dependency_p=*/false,
14478                                 /*template_p=*/NULL,
14479                                 /*declarator_p=*/true,
14480                                 optional_p);
14481   if (id && BASELINK_P (id))
14482     id = BASELINK_FUNCTIONS (id);
14483   return id;
14484 }
14485
14486 /* Parse a type-id.
14487
14488    type-id:
14489      type-specifier-seq abstract-declarator [opt]
14490
14491    Returns the TYPE specified.  */
14492
14493 static tree
14494 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
14495 {
14496   cp_decl_specifier_seq type_specifier_seq;
14497   cp_declarator *abstract_declarator;
14498
14499   /* Parse the type-specifier-seq.  */
14500   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14501                                 &type_specifier_seq);
14502   if (type_specifier_seq.type == error_mark_node)
14503     return error_mark_node;
14504
14505   /* There might or might not be an abstract declarator.  */
14506   cp_parser_parse_tentatively (parser);
14507   /* Look for the declarator.  */
14508   abstract_declarator
14509     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14510                             /*parenthesized_p=*/NULL,
14511                             /*member_p=*/false);
14512   /* Check to see if there really was a declarator.  */
14513   if (!cp_parser_parse_definitely (parser))
14514     abstract_declarator = NULL;
14515
14516   if (type_specifier_seq.type
14517       && type_uses_auto (type_specifier_seq.type))
14518     {
14519       /* A type-id with type 'auto' is only ok if the abstract declarator
14520          is a function declarator with a late-specified return type.  */
14521       if (abstract_declarator
14522           && abstract_declarator->kind == cdk_function
14523           && abstract_declarator->u.function.late_return_type)
14524         /* OK */;
14525       else
14526         {
14527           error ("invalid use of %<auto%>");
14528           return error_mark_node;
14529         }
14530     }
14531   
14532   return groktypename (&type_specifier_seq, abstract_declarator,
14533                        is_template_arg);
14534 }
14535
14536 static tree cp_parser_type_id (cp_parser *parser)
14537 {
14538   return cp_parser_type_id_1 (parser, false);
14539 }
14540
14541 static tree cp_parser_template_type_arg (cp_parser *parser)
14542 {
14543   return cp_parser_type_id_1 (parser, true);
14544 }
14545
14546 /* Parse a type-specifier-seq.
14547
14548    type-specifier-seq:
14549      type-specifier type-specifier-seq [opt]
14550
14551    GNU extension:
14552
14553    type-specifier-seq:
14554      attributes type-specifier-seq [opt]
14555
14556    If IS_CONDITION is true, we are at the start of a "condition",
14557    e.g., we've just seen "if (".
14558
14559    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
14560
14561 static void
14562 cp_parser_type_specifier_seq (cp_parser* parser,
14563                               bool is_condition,
14564                               cp_decl_specifier_seq *type_specifier_seq)
14565 {
14566   bool seen_type_specifier = false;
14567   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14568   cp_token *start_token = NULL;
14569
14570   /* Clear the TYPE_SPECIFIER_SEQ.  */
14571   clear_decl_specs (type_specifier_seq);
14572
14573   /* Parse the type-specifiers and attributes.  */
14574   while (true)
14575     {
14576       tree type_specifier;
14577       bool is_cv_qualifier;
14578
14579       /* Check for attributes first.  */
14580       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14581         {
14582           type_specifier_seq->attributes =
14583             chainon (type_specifier_seq->attributes,
14584                      cp_parser_attributes_opt (parser));
14585           continue;
14586         }
14587
14588       /* record the token of the beginning of the type specifier seq,
14589          for error reporting purposes*/
14590      if (!start_token)
14591        start_token = cp_lexer_peek_token (parser->lexer);
14592
14593       /* Look for the type-specifier.  */
14594       type_specifier = cp_parser_type_specifier (parser,
14595                                                  flags,
14596                                                  type_specifier_seq,
14597                                                  /*is_declaration=*/false,
14598                                                  NULL,
14599                                                  &is_cv_qualifier);
14600       if (!type_specifier)
14601         {
14602           /* If the first type-specifier could not be found, this is not a
14603              type-specifier-seq at all.  */
14604           if (!seen_type_specifier)
14605             {
14606               cp_parser_error (parser, "expected type-specifier");
14607               type_specifier_seq->type = error_mark_node;
14608               return;
14609             }
14610           /* If subsequent type-specifiers could not be found, the
14611              type-specifier-seq is complete.  */
14612           break;
14613         }
14614
14615       seen_type_specifier = true;
14616       /* The standard says that a condition can be:
14617
14618             type-specifier-seq declarator = assignment-expression
14619
14620          However, given:
14621
14622            struct S {};
14623            if (int S = ...)
14624
14625          we should treat the "S" as a declarator, not as a
14626          type-specifier.  The standard doesn't say that explicitly for
14627          type-specifier-seq, but it does say that for
14628          decl-specifier-seq in an ordinary declaration.  Perhaps it
14629          would be clearer just to allow a decl-specifier-seq here, and
14630          then add a semantic restriction that if any decl-specifiers
14631          that are not type-specifiers appear, the program is invalid.  */
14632       if (is_condition && !is_cv_qualifier)
14633         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14634     }
14635
14636   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14637 }
14638
14639 /* Parse a parameter-declaration-clause.
14640
14641    parameter-declaration-clause:
14642      parameter-declaration-list [opt] ... [opt]
14643      parameter-declaration-list , ...
14644
14645    Returns a representation for the parameter declarations.  A return
14646    value of NULL indicates a parameter-declaration-clause consisting
14647    only of an ellipsis.  */
14648
14649 static tree
14650 cp_parser_parameter_declaration_clause (cp_parser* parser)
14651 {
14652   tree parameters;
14653   cp_token *token;
14654   bool ellipsis_p;
14655   bool is_error;
14656
14657   /* Peek at the next token.  */
14658   token = cp_lexer_peek_token (parser->lexer);
14659   /* Check for trivial parameter-declaration-clauses.  */
14660   if (token->type == CPP_ELLIPSIS)
14661     {
14662       /* Consume the `...' token.  */
14663       cp_lexer_consume_token (parser->lexer);
14664       return NULL_TREE;
14665     }
14666   else if (token->type == CPP_CLOSE_PAREN)
14667     /* There are no parameters.  */
14668     {
14669 #ifndef NO_IMPLICIT_EXTERN_C
14670       if (in_system_header && current_class_type == NULL
14671           && current_lang_name == lang_name_c)
14672         return NULL_TREE;
14673       else
14674 #endif
14675         return void_list_node;
14676     }
14677   /* Check for `(void)', too, which is a special case.  */
14678   else if (token->keyword == RID_VOID
14679            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14680                == CPP_CLOSE_PAREN))
14681     {
14682       /* Consume the `void' token.  */
14683       cp_lexer_consume_token (parser->lexer);
14684       /* There are no parameters.  */
14685       return void_list_node;
14686     }
14687
14688   /* Parse the parameter-declaration-list.  */
14689   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14690   /* If a parse error occurred while parsing the
14691      parameter-declaration-list, then the entire
14692      parameter-declaration-clause is erroneous.  */
14693   if (is_error)
14694     return NULL;
14695
14696   /* Peek at the next token.  */
14697   token = cp_lexer_peek_token (parser->lexer);
14698   /* If it's a `,', the clause should terminate with an ellipsis.  */
14699   if (token->type == CPP_COMMA)
14700     {
14701       /* Consume the `,'.  */
14702       cp_lexer_consume_token (parser->lexer);
14703       /* Expect an ellipsis.  */
14704       ellipsis_p
14705         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14706     }
14707   /* It might also be `...' if the optional trailing `,' was
14708      omitted.  */
14709   else if (token->type == CPP_ELLIPSIS)
14710     {
14711       /* Consume the `...' token.  */
14712       cp_lexer_consume_token (parser->lexer);
14713       /* And remember that we saw it.  */
14714       ellipsis_p = true;
14715     }
14716   else
14717     ellipsis_p = false;
14718
14719   /* Finish the parameter list.  */
14720   if (!ellipsis_p)
14721     parameters = chainon (parameters, void_list_node);
14722
14723   return parameters;
14724 }
14725
14726 /* Parse a parameter-declaration-list.
14727
14728    parameter-declaration-list:
14729      parameter-declaration
14730      parameter-declaration-list , parameter-declaration
14731
14732    Returns a representation of the parameter-declaration-list, as for
14733    cp_parser_parameter_declaration_clause.  However, the
14734    `void_list_node' is never appended to the list.  Upon return,
14735    *IS_ERROR will be true iff an error occurred.  */
14736
14737 static tree
14738 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14739 {
14740   tree parameters = NULL_TREE;
14741   tree *tail = &parameters; 
14742   bool saved_in_unbraced_linkage_specification_p;
14743   int index = 0;
14744
14745   /* Assume all will go well.  */
14746   *is_error = false;
14747   /* The special considerations that apply to a function within an
14748      unbraced linkage specifications do not apply to the parameters
14749      to the function.  */
14750   saved_in_unbraced_linkage_specification_p 
14751     = parser->in_unbraced_linkage_specification_p;
14752   parser->in_unbraced_linkage_specification_p = false;
14753
14754   /* Look for more parameters.  */
14755   while (true)
14756     {
14757       cp_parameter_declarator *parameter;
14758       tree decl = error_mark_node;
14759       bool parenthesized_p;
14760       /* Parse the parameter.  */
14761       parameter
14762         = cp_parser_parameter_declaration (parser,
14763                                            /*template_parm_p=*/false,
14764                                            &parenthesized_p);
14765
14766       /* We don't know yet if the enclosing context is deprecated, so wait
14767          and warn in grokparms if appropriate.  */
14768       deprecated_state = DEPRECATED_SUPPRESS;
14769
14770       if (parameter)
14771         decl = grokdeclarator (parameter->declarator,
14772                                &parameter->decl_specifiers,
14773                                PARM,
14774                                parameter->default_argument != NULL_TREE,
14775                                &parameter->decl_specifiers.attributes);
14776
14777       deprecated_state = DEPRECATED_NORMAL;
14778
14779       /* If a parse error occurred parsing the parameter declaration,
14780          then the entire parameter-declaration-list is erroneous.  */
14781       if (decl == error_mark_node)
14782         {
14783           *is_error = true;
14784           parameters = error_mark_node;
14785           break;
14786         }
14787
14788       if (parameter->decl_specifiers.attributes)
14789         cplus_decl_attributes (&decl,
14790                                parameter->decl_specifiers.attributes,
14791                                0);
14792       if (DECL_NAME (decl))
14793         decl = pushdecl (decl);
14794
14795       if (decl != error_mark_node)
14796         {
14797           retrofit_lang_decl (decl);
14798           DECL_PARM_INDEX (decl) = ++index;
14799         }
14800
14801       /* Add the new parameter to the list.  */
14802       *tail = build_tree_list (parameter->default_argument, decl);
14803       tail = &TREE_CHAIN (*tail);
14804
14805       /* Peek at the next token.  */
14806       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14807           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14808           /* These are for Objective-C++ */
14809           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14810           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14811         /* The parameter-declaration-list is complete.  */
14812         break;
14813       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14814         {
14815           cp_token *token;
14816
14817           /* Peek at the next token.  */
14818           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14819           /* If it's an ellipsis, then the list is complete.  */
14820           if (token->type == CPP_ELLIPSIS)
14821             break;
14822           /* Otherwise, there must be more parameters.  Consume the
14823              `,'.  */
14824           cp_lexer_consume_token (parser->lexer);
14825           /* When parsing something like:
14826
14827                 int i(float f, double d)
14828
14829              we can tell after seeing the declaration for "f" that we
14830              are not looking at an initialization of a variable "i",
14831              but rather at the declaration of a function "i".
14832
14833              Due to the fact that the parsing of template arguments
14834              (as specified to a template-id) requires backtracking we
14835              cannot use this technique when inside a template argument
14836              list.  */
14837           if (!parser->in_template_argument_list_p
14838               && !parser->in_type_id_in_expr_p
14839               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14840               /* However, a parameter-declaration of the form
14841                  "foat(f)" (which is a valid declaration of a
14842                  parameter "f") can also be interpreted as an
14843                  expression (the conversion of "f" to "float").  */
14844               && !parenthesized_p)
14845             cp_parser_commit_to_tentative_parse (parser);
14846         }
14847       else
14848         {
14849           cp_parser_error (parser, "expected %<,%> or %<...%>");
14850           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14851             cp_parser_skip_to_closing_parenthesis (parser,
14852                                                    /*recovering=*/true,
14853                                                    /*or_comma=*/false,
14854                                                    /*consume_paren=*/false);
14855           break;
14856         }
14857     }
14858
14859   parser->in_unbraced_linkage_specification_p
14860     = saved_in_unbraced_linkage_specification_p;
14861
14862   return parameters;
14863 }
14864
14865 /* Parse a parameter declaration.
14866
14867    parameter-declaration:
14868      decl-specifier-seq ... [opt] declarator
14869      decl-specifier-seq declarator = assignment-expression
14870      decl-specifier-seq ... [opt] abstract-declarator [opt]
14871      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14872
14873    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14874    declares a template parameter.  (In that case, a non-nested `>'
14875    token encountered during the parsing of the assignment-expression
14876    is not interpreted as a greater-than operator.)
14877
14878    Returns a representation of the parameter, or NULL if an error
14879    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14880    true iff the declarator is of the form "(p)".  */
14881
14882 static cp_parameter_declarator *
14883 cp_parser_parameter_declaration (cp_parser *parser,
14884                                  bool template_parm_p,
14885                                  bool *parenthesized_p)
14886 {
14887   int declares_class_or_enum;
14888   bool greater_than_is_operator_p;
14889   cp_decl_specifier_seq decl_specifiers;
14890   cp_declarator *declarator;
14891   tree default_argument;
14892   cp_token *token = NULL, *declarator_token_start = NULL;
14893   const char *saved_message;
14894
14895   /* In a template parameter, `>' is not an operator.
14896
14897      [temp.param]
14898
14899      When parsing a default template-argument for a non-type
14900      template-parameter, the first non-nested `>' is taken as the end
14901      of the template parameter-list rather than a greater-than
14902      operator.  */
14903   greater_than_is_operator_p = !template_parm_p;
14904
14905   /* Type definitions may not appear in parameter types.  */
14906   saved_message = parser->type_definition_forbidden_message;
14907   parser->type_definition_forbidden_message
14908     = "types may not be defined in parameter types";
14909
14910   /* Parse the declaration-specifiers.  */
14911   cp_parser_decl_specifier_seq (parser,
14912                                 CP_PARSER_FLAGS_NONE,
14913                                 &decl_specifiers,
14914                                 &declares_class_or_enum);
14915   /* If an error occurred, there's no reason to attempt to parse the
14916      rest of the declaration.  */
14917   if (cp_parser_error_occurred (parser))
14918     {
14919       parser->type_definition_forbidden_message = saved_message;
14920       return NULL;
14921     }
14922
14923   /* Peek at the next token.  */
14924   token = cp_lexer_peek_token (parser->lexer);
14925
14926   /* If the next token is a `)', `,', `=', `>', or `...', then there
14927      is no declarator. However, when variadic templates are enabled,
14928      there may be a declarator following `...'.  */
14929   if (token->type == CPP_CLOSE_PAREN
14930       || token->type == CPP_COMMA
14931       || token->type == CPP_EQ
14932       || token->type == CPP_GREATER)
14933     {
14934       declarator = NULL;
14935       if (parenthesized_p)
14936         *parenthesized_p = false;
14937     }
14938   /* Otherwise, there should be a declarator.  */
14939   else
14940     {
14941       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14942       parser->default_arg_ok_p = false;
14943
14944       /* After seeing a decl-specifier-seq, if the next token is not a
14945          "(", there is no possibility that the code is a valid
14946          expression.  Therefore, if parsing tentatively, we commit at
14947          this point.  */
14948       if (!parser->in_template_argument_list_p
14949           /* In an expression context, having seen:
14950
14951                (int((char ...
14952
14953              we cannot be sure whether we are looking at a
14954              function-type (taking a "char" as a parameter) or a cast
14955              of some object of type "char" to "int".  */
14956           && !parser->in_type_id_in_expr_p
14957           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14958           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14959         cp_parser_commit_to_tentative_parse (parser);
14960       /* Parse the declarator.  */
14961       declarator_token_start = token;
14962       declarator = cp_parser_declarator (parser,
14963                                          CP_PARSER_DECLARATOR_EITHER,
14964                                          /*ctor_dtor_or_conv_p=*/NULL,
14965                                          parenthesized_p,
14966                                          /*member_p=*/false);
14967       parser->default_arg_ok_p = saved_default_arg_ok_p;
14968       /* After the declarator, allow more attributes.  */
14969       decl_specifiers.attributes
14970         = chainon (decl_specifiers.attributes,
14971                    cp_parser_attributes_opt (parser));
14972     }
14973
14974   /* If the next token is an ellipsis, and we have not seen a
14975      declarator name, and the type of the declarator contains parameter
14976      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14977      a parameter pack expansion expression. Otherwise, leave the
14978      ellipsis for a C-style variadic function. */
14979   token = cp_lexer_peek_token (parser->lexer);
14980   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14981     {
14982       tree type = decl_specifiers.type;
14983
14984       if (type && DECL_P (type))
14985         type = TREE_TYPE (type);
14986
14987       if (type
14988           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14989           && declarator_can_be_parameter_pack (declarator)
14990           && (!declarator || !declarator->parameter_pack_p)
14991           && uses_parameter_packs (type))
14992         {
14993           /* Consume the `...'. */
14994           cp_lexer_consume_token (parser->lexer);
14995           maybe_warn_variadic_templates ();
14996           
14997           /* Build a pack expansion type */
14998           if (declarator)
14999             declarator->parameter_pack_p = true;
15000           else
15001             decl_specifiers.type = make_pack_expansion (type);
15002         }
15003     }
15004
15005   /* The restriction on defining new types applies only to the type
15006      of the parameter, not to the default argument.  */
15007   parser->type_definition_forbidden_message = saved_message;
15008
15009   /* If the next token is `=', then process a default argument.  */
15010   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15011     {
15012       /* Consume the `='.  */
15013       cp_lexer_consume_token (parser->lexer);
15014
15015       /* If we are defining a class, then the tokens that make up the
15016          default argument must be saved and processed later.  */
15017       if (!template_parm_p && at_class_scope_p ()
15018           && TYPE_BEING_DEFINED (current_class_type)
15019           && !LAMBDA_TYPE_P (current_class_type))
15020         {
15021           unsigned depth = 0;
15022           int maybe_template_id = 0;
15023           cp_token *first_token;
15024           cp_token *token;
15025
15026           /* Add tokens until we have processed the entire default
15027              argument.  We add the range [first_token, token).  */
15028           first_token = cp_lexer_peek_token (parser->lexer);
15029           while (true)
15030             {
15031               bool done = false;
15032
15033               /* Peek at the next token.  */
15034               token = cp_lexer_peek_token (parser->lexer);
15035               /* What we do depends on what token we have.  */
15036               switch (token->type)
15037                 {
15038                   /* In valid code, a default argument must be
15039                      immediately followed by a `,' `)', or `...'.  */
15040                 case CPP_COMMA:
15041                   if (depth == 0 && maybe_template_id)
15042                     {
15043                       /* If we've seen a '<', we might be in a
15044                          template-argument-list.  Until Core issue 325 is
15045                          resolved, we don't know how this situation ought
15046                          to be handled, so try to DTRT.  We check whether
15047                          what comes after the comma is a valid parameter
15048                          declaration list.  If it is, then the comma ends
15049                          the default argument; otherwise the default
15050                          argument continues.  */
15051                       bool error = false;
15052
15053                       /* Set ITALP so cp_parser_parameter_declaration_list
15054                          doesn't decide to commit to this parse.  */
15055                       bool saved_italp = parser->in_template_argument_list_p;
15056                       parser->in_template_argument_list_p = true;
15057
15058                       cp_parser_parse_tentatively (parser);
15059                       cp_lexer_consume_token (parser->lexer);
15060                       cp_parser_parameter_declaration_list (parser, &error);
15061                       if (!cp_parser_error_occurred (parser) && !error)
15062                         done = true;
15063                       cp_parser_abort_tentative_parse (parser);
15064
15065                       parser->in_template_argument_list_p = saved_italp;
15066                       break;
15067                     }
15068                 case CPP_CLOSE_PAREN:
15069                 case CPP_ELLIPSIS:
15070                   /* If we run into a non-nested `;', `}', or `]',
15071                      then the code is invalid -- but the default
15072                      argument is certainly over.  */
15073                 case CPP_SEMICOLON:
15074                 case CPP_CLOSE_BRACE:
15075                 case CPP_CLOSE_SQUARE:
15076                   if (depth == 0)
15077                     done = true;
15078                   /* Update DEPTH, if necessary.  */
15079                   else if (token->type == CPP_CLOSE_PAREN
15080                            || token->type == CPP_CLOSE_BRACE
15081                            || token->type == CPP_CLOSE_SQUARE)
15082                     --depth;
15083                   break;
15084
15085                 case CPP_OPEN_PAREN:
15086                 case CPP_OPEN_SQUARE:
15087                 case CPP_OPEN_BRACE:
15088                   ++depth;
15089                   break;
15090
15091                 case CPP_LESS:
15092                   if (depth == 0)
15093                     /* This might be the comparison operator, or it might
15094                        start a template argument list.  */
15095                     ++maybe_template_id;
15096                   break;
15097
15098                 case CPP_RSHIFT:
15099                   if (cxx_dialect == cxx98)
15100                     break;
15101                   /* Fall through for C++0x, which treats the `>>'
15102                      operator like two `>' tokens in certain
15103                      cases.  */
15104
15105                 case CPP_GREATER:
15106                   if (depth == 0)
15107                     {
15108                       /* This might be an operator, or it might close a
15109                          template argument list.  But if a previous '<'
15110                          started a template argument list, this will have
15111                          closed it, so we can't be in one anymore.  */
15112                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15113                       if (maybe_template_id < 0)
15114                         maybe_template_id = 0;
15115                     }
15116                   break;
15117
15118                   /* If we run out of tokens, issue an error message.  */
15119                 case CPP_EOF:
15120                 case CPP_PRAGMA_EOL:
15121                   error_at (token->location, "file ends in default argument");
15122                   done = true;
15123                   break;
15124
15125                 case CPP_NAME:
15126                 case CPP_SCOPE:
15127                   /* In these cases, we should look for template-ids.
15128                      For example, if the default argument is
15129                      `X<int, double>()', we need to do name lookup to
15130                      figure out whether or not `X' is a template; if
15131                      so, the `,' does not end the default argument.
15132
15133                      That is not yet done.  */
15134                   break;
15135
15136                 default:
15137                   break;
15138                 }
15139
15140               /* If we've reached the end, stop.  */
15141               if (done)
15142                 break;
15143
15144               /* Add the token to the token block.  */
15145               token = cp_lexer_consume_token (parser->lexer);
15146             }
15147
15148           /* Create a DEFAULT_ARG to represent the unparsed default
15149              argument.  */
15150           default_argument = make_node (DEFAULT_ARG);
15151           DEFARG_TOKENS (default_argument)
15152             = cp_token_cache_new (first_token, token);
15153           DEFARG_INSTANTIATIONS (default_argument) = NULL;
15154         }
15155       /* Outside of a class definition, we can just parse the
15156          assignment-expression.  */
15157       else
15158         {
15159           token = cp_lexer_peek_token (parser->lexer);
15160           default_argument 
15161             = cp_parser_default_argument (parser, template_parm_p);
15162         }
15163
15164       if (!parser->default_arg_ok_p)
15165         {
15166           if (flag_permissive)
15167             warning (0, "deprecated use of default argument for parameter of non-function");
15168           else
15169             {
15170               error_at (token->location,
15171                         "default arguments are only "
15172                         "permitted for function parameters");
15173               default_argument = NULL_TREE;
15174             }
15175         }
15176       else if ((declarator && declarator->parameter_pack_p)
15177                || (decl_specifiers.type
15178                    && PACK_EXPANSION_P (decl_specifiers.type)))
15179         {
15180           /* Find the name of the parameter pack.  */     
15181           cp_declarator *id_declarator = declarator;
15182           while (id_declarator && id_declarator->kind != cdk_id)
15183             id_declarator = id_declarator->declarator;
15184           
15185           if (id_declarator && id_declarator->kind == cdk_id)
15186             error_at (declarator_token_start->location,
15187                       template_parm_p 
15188                       ? "template parameter pack %qD"
15189                       " cannot have a default argument"
15190                       : "parameter pack %qD cannot have a default argument",
15191                       id_declarator->u.id.unqualified_name);
15192           else
15193             error_at (declarator_token_start->location,
15194                       template_parm_p 
15195                       ? "template parameter pack cannot have a default argument"
15196                       : "parameter pack cannot have a default argument");
15197           
15198           default_argument = NULL_TREE;
15199         }
15200     }
15201   else
15202     default_argument = NULL_TREE;
15203
15204   return make_parameter_declarator (&decl_specifiers,
15205                                     declarator,
15206                                     default_argument);
15207 }
15208
15209 /* Parse a default argument and return it.
15210
15211    TEMPLATE_PARM_P is true if this is a default argument for a
15212    non-type template parameter.  */
15213 static tree
15214 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15215 {
15216   tree default_argument = NULL_TREE;
15217   bool saved_greater_than_is_operator_p;
15218   bool saved_local_variables_forbidden_p;
15219
15220   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15221      set correctly.  */
15222   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15223   parser->greater_than_is_operator_p = !template_parm_p;
15224   /* Local variable names (and the `this' keyword) may not
15225      appear in a default argument.  */
15226   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15227   parser->local_variables_forbidden_p = true;
15228   /* Parse the assignment-expression.  */
15229   if (template_parm_p)
15230     push_deferring_access_checks (dk_no_deferred);
15231   default_argument
15232     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15233   if (template_parm_p)
15234     pop_deferring_access_checks ();
15235   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15236   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15237
15238   return default_argument;
15239 }
15240
15241 /* Parse a function-body.
15242
15243    function-body:
15244      compound_statement  */
15245
15246 static void
15247 cp_parser_function_body (cp_parser *parser)
15248 {
15249   cp_parser_compound_statement (parser, NULL, false);
15250 }
15251
15252 /* Parse a ctor-initializer-opt followed by a function-body.  Return
15253    true if a ctor-initializer was present.  */
15254
15255 static bool
15256 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15257 {
15258   tree body;
15259   bool ctor_initializer_p;
15260
15261   /* Begin the function body.  */
15262   body = begin_function_body ();
15263   /* Parse the optional ctor-initializer.  */
15264   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15265   /* Parse the function-body.  */
15266   cp_parser_function_body (parser);
15267   /* Finish the function body.  */
15268   finish_function_body (body);
15269
15270   return ctor_initializer_p;
15271 }
15272
15273 /* Parse an initializer.
15274
15275    initializer:
15276      = initializer-clause
15277      ( expression-list )
15278
15279    Returns an expression representing the initializer.  If no
15280    initializer is present, NULL_TREE is returned.
15281
15282    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15283    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
15284    set to TRUE if there is no initializer present.  If there is an
15285    initializer, and it is not a constant-expression, *NON_CONSTANT_P
15286    is set to true; otherwise it is set to false.  */
15287
15288 static tree
15289 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15290                        bool* non_constant_p)
15291 {
15292   cp_token *token;
15293   tree init;
15294
15295   /* Peek at the next token.  */
15296   token = cp_lexer_peek_token (parser->lexer);
15297
15298   /* Let our caller know whether or not this initializer was
15299      parenthesized.  */
15300   *is_direct_init = (token->type != CPP_EQ);
15301   /* Assume that the initializer is constant.  */
15302   *non_constant_p = false;
15303
15304   if (token->type == CPP_EQ)
15305     {
15306       /* Consume the `='.  */
15307       cp_lexer_consume_token (parser->lexer);
15308       /* Parse the initializer-clause.  */
15309       init = cp_parser_initializer_clause (parser, non_constant_p);
15310     }
15311   else if (token->type == CPP_OPEN_PAREN)
15312     {
15313       VEC(tree,gc) *vec;
15314       vec = cp_parser_parenthesized_expression_list (parser, false,
15315                                                      /*cast_p=*/false,
15316                                                      /*allow_expansion_p=*/true,
15317                                                      non_constant_p);
15318       if (vec == NULL)
15319         return error_mark_node;
15320       init = build_tree_list_vec (vec);
15321       release_tree_vector (vec);
15322     }
15323   else if (token->type == CPP_OPEN_BRACE)
15324     {
15325       maybe_warn_cpp0x ("extended initializer lists");
15326       init = cp_parser_braced_list (parser, non_constant_p);
15327       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15328     }
15329   else
15330     {
15331       /* Anything else is an error.  */
15332       cp_parser_error (parser, "expected initializer");
15333       init = error_mark_node;
15334     }
15335
15336   return init;
15337 }
15338
15339 /* Parse an initializer-clause.
15340
15341    initializer-clause:
15342      assignment-expression
15343      braced-init-list
15344
15345    Returns an expression representing the initializer.
15346
15347    If the `assignment-expression' production is used the value
15348    returned is simply a representation for the expression.
15349
15350    Otherwise, calls cp_parser_braced_list.  */
15351
15352 static tree
15353 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15354 {
15355   tree initializer;
15356
15357   /* Assume the expression is constant.  */
15358   *non_constant_p = false;
15359
15360   /* If it is not a `{', then we are looking at an
15361      assignment-expression.  */
15362   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15363     {
15364       initializer
15365         = cp_parser_constant_expression (parser,
15366                                         /*allow_non_constant_p=*/true,
15367                                         non_constant_p);
15368       if (!*non_constant_p)
15369         initializer = fold_non_dependent_expr (initializer);
15370     }
15371   else
15372     initializer = cp_parser_braced_list (parser, non_constant_p);
15373
15374   return initializer;
15375 }
15376
15377 /* Parse a brace-enclosed initializer list.
15378
15379    braced-init-list:
15380      { initializer-list , [opt] }
15381      { }
15382
15383    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
15384    the elements of the initializer-list (or NULL, if the last
15385    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
15386    NULL_TREE.  There is no way to detect whether or not the optional
15387    trailing `,' was provided.  NON_CONSTANT_P is as for
15388    cp_parser_initializer.  */     
15389
15390 static tree
15391 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15392 {
15393   tree initializer;
15394
15395   /* Consume the `{' token.  */
15396   cp_lexer_consume_token (parser->lexer);
15397   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
15398   initializer = make_node (CONSTRUCTOR);
15399   /* If it's not a `}', then there is a non-trivial initializer.  */
15400   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15401     {
15402       /* Parse the initializer list.  */
15403       CONSTRUCTOR_ELTS (initializer)
15404         = cp_parser_initializer_list (parser, non_constant_p);
15405       /* A trailing `,' token is allowed.  */
15406       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15407         cp_lexer_consume_token (parser->lexer);
15408     }
15409   /* Now, there should be a trailing `}'.  */
15410   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15411   TREE_TYPE (initializer) = init_list_type_node;
15412   return initializer;
15413 }
15414
15415 /* Parse an initializer-list.
15416
15417    initializer-list:
15418      initializer-clause ... [opt]
15419      initializer-list , initializer-clause ... [opt]
15420
15421    GNU Extension:
15422
15423    initializer-list:
15424      identifier : initializer-clause
15425      initializer-list, identifier : initializer-clause
15426
15427    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
15428    for the initializer.  If the INDEX of the elt is non-NULL, it is the
15429    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
15430    as for cp_parser_initializer.  */
15431
15432 static VEC(constructor_elt,gc) *
15433 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15434 {
15435   VEC(constructor_elt,gc) *v = NULL;
15436
15437   /* Assume all of the expressions are constant.  */
15438   *non_constant_p = false;
15439
15440   /* Parse the rest of the list.  */
15441   while (true)
15442     {
15443       cp_token *token;
15444       tree identifier;
15445       tree initializer;
15446       bool clause_non_constant_p;
15447
15448       /* If the next token is an identifier and the following one is a
15449          colon, we are looking at the GNU designated-initializer
15450          syntax.  */
15451       if (cp_parser_allow_gnu_extensions_p (parser)
15452           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15453           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15454         {
15455           /* Warn the user that they are using an extension.  */
15456           pedwarn (input_location, OPT_pedantic, 
15457                    "ISO C++ does not allow designated initializers");
15458           /* Consume the identifier.  */
15459           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15460           /* Consume the `:'.  */
15461           cp_lexer_consume_token (parser->lexer);
15462         }
15463       else
15464         identifier = NULL_TREE;
15465
15466       /* Parse the initializer.  */
15467       initializer = cp_parser_initializer_clause (parser,
15468                                                   &clause_non_constant_p);
15469       /* If any clause is non-constant, so is the entire initializer.  */
15470       if (clause_non_constant_p)
15471         *non_constant_p = true;
15472
15473       /* If we have an ellipsis, this is an initializer pack
15474          expansion.  */
15475       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15476         {
15477           /* Consume the `...'.  */
15478           cp_lexer_consume_token (parser->lexer);
15479
15480           /* Turn the initializer into an initializer expansion.  */
15481           initializer = make_pack_expansion (initializer);
15482         }
15483
15484       /* Add it to the vector.  */
15485       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15486
15487       /* If the next token is not a comma, we have reached the end of
15488          the list.  */
15489       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15490         break;
15491
15492       /* Peek at the next token.  */
15493       token = cp_lexer_peek_nth_token (parser->lexer, 2);
15494       /* If the next token is a `}', then we're still done.  An
15495          initializer-clause can have a trailing `,' after the
15496          initializer-list and before the closing `}'.  */
15497       if (token->type == CPP_CLOSE_BRACE)
15498         break;
15499
15500       /* Consume the `,' token.  */
15501       cp_lexer_consume_token (parser->lexer);
15502     }
15503
15504   return v;
15505 }
15506
15507 /* Classes [gram.class] */
15508
15509 /* Parse a class-name.
15510
15511    class-name:
15512      identifier
15513      template-id
15514
15515    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15516    to indicate that names looked up in dependent types should be
15517    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
15518    keyword has been used to indicate that the name that appears next
15519    is a template.  TAG_TYPE indicates the explicit tag given before
15520    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
15521    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
15522    is the class being defined in a class-head.
15523
15524    Returns the TYPE_DECL representing the class.  */
15525
15526 static tree
15527 cp_parser_class_name (cp_parser *parser,
15528                       bool typename_keyword_p,
15529                       bool template_keyword_p,
15530                       enum tag_types tag_type,
15531                       bool check_dependency_p,
15532                       bool class_head_p,
15533                       bool is_declaration)
15534 {
15535   tree decl;
15536   tree scope;
15537   bool typename_p;
15538   cp_token *token;
15539   tree identifier = NULL_TREE;
15540
15541   /* All class-names start with an identifier.  */
15542   token = cp_lexer_peek_token (parser->lexer);
15543   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15544     {
15545       cp_parser_error (parser, "expected class-name");
15546       return error_mark_node;
15547     }
15548
15549   /* PARSER->SCOPE can be cleared when parsing the template-arguments
15550      to a template-id, so we save it here.  */
15551   scope = parser->scope;
15552   if (scope == error_mark_node)
15553     return error_mark_node;
15554
15555   /* Any name names a type if we're following the `typename' keyword
15556      in a qualified name where the enclosing scope is type-dependent.  */
15557   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15558                 && dependent_type_p (scope));
15559   /* Handle the common case (an identifier, but not a template-id)
15560      efficiently.  */
15561   if (token->type == CPP_NAME
15562       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15563     {
15564       cp_token *identifier_token;
15565       bool ambiguous_p;
15566
15567       /* Look for the identifier.  */
15568       identifier_token = cp_lexer_peek_token (parser->lexer);
15569       ambiguous_p = identifier_token->ambiguous_p;
15570       identifier = cp_parser_identifier (parser);
15571       /* If the next token isn't an identifier, we are certainly not
15572          looking at a class-name.  */
15573       if (identifier == error_mark_node)
15574         decl = error_mark_node;
15575       /* If we know this is a type-name, there's no need to look it
15576          up.  */
15577       else if (typename_p)
15578         decl = identifier;
15579       else
15580         {
15581           tree ambiguous_decls;
15582           /* If we already know that this lookup is ambiguous, then
15583              we've already issued an error message; there's no reason
15584              to check again.  */
15585           if (ambiguous_p)
15586             {
15587               cp_parser_simulate_error (parser);
15588               return error_mark_node;
15589             }
15590           /* If the next token is a `::', then the name must be a type
15591              name.
15592
15593              [basic.lookup.qual]
15594
15595              During the lookup for a name preceding the :: scope
15596              resolution operator, object, function, and enumerator
15597              names are ignored.  */
15598           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15599             tag_type = typename_type;
15600           /* Look up the name.  */
15601           decl = cp_parser_lookup_name (parser, identifier,
15602                                         tag_type,
15603                                         /*is_template=*/false,
15604                                         /*is_namespace=*/false,
15605                                         check_dependency_p,
15606                                         &ambiguous_decls,
15607                                         identifier_token->location);
15608           if (ambiguous_decls)
15609             {
15610               error_at (identifier_token->location,
15611                         "reference to %qD is ambiguous", identifier);
15612               print_candidates (ambiguous_decls);
15613               if (cp_parser_parsing_tentatively (parser))
15614                 {
15615                   identifier_token->ambiguous_p = true;
15616                   cp_parser_simulate_error (parser);
15617                 }
15618               return error_mark_node;
15619             }
15620         }
15621     }
15622   else
15623     {
15624       /* Try a template-id.  */
15625       decl = cp_parser_template_id (parser, template_keyword_p,
15626                                     check_dependency_p,
15627                                     is_declaration);
15628       if (decl == error_mark_node)
15629         return error_mark_node;
15630     }
15631
15632   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15633
15634   /* If this is a typename, create a TYPENAME_TYPE.  */
15635   if (typename_p && decl != error_mark_node)
15636     {
15637       decl = make_typename_type (scope, decl, typename_type,
15638                                  /*complain=*/tf_error);
15639       if (decl != error_mark_node)
15640         decl = TYPE_NAME (decl);
15641     }
15642
15643   /* Check to see that it is really the name of a class.  */
15644   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15645       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15646       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15647     /* Situations like this:
15648
15649          template <typename T> struct A {
15650            typename T::template X<int>::I i;
15651          };
15652
15653        are problematic.  Is `T::template X<int>' a class-name?  The
15654        standard does not seem to be definitive, but there is no other
15655        valid interpretation of the following `::'.  Therefore, those
15656        names are considered class-names.  */
15657     {
15658       decl = make_typename_type (scope, decl, tag_type, tf_error);
15659       if (decl != error_mark_node)
15660         decl = TYPE_NAME (decl);
15661     }
15662   else if (TREE_CODE (decl) != TYPE_DECL
15663            || TREE_TYPE (decl) == error_mark_node
15664            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15665     decl = error_mark_node;
15666
15667   if (decl == error_mark_node)
15668     cp_parser_error (parser, "expected class-name");
15669   else if (identifier && !parser->scope)
15670     maybe_note_name_used_in_class (identifier, decl);
15671
15672   return decl;
15673 }
15674
15675 /* Parse a class-specifier.
15676
15677    class-specifier:
15678      class-head { member-specification [opt] }
15679
15680    Returns the TREE_TYPE representing the class.  */
15681
15682 static tree
15683 cp_parser_class_specifier (cp_parser* parser)
15684 {
15685   tree type;
15686   tree attributes = NULL_TREE;
15687   bool nested_name_specifier_p;
15688   unsigned saved_num_template_parameter_lists;
15689   bool saved_in_function_body;
15690   bool saved_in_unbraced_linkage_specification_p;
15691   tree old_scope = NULL_TREE;
15692   tree scope = NULL_TREE;
15693   tree bases;
15694
15695   push_deferring_access_checks (dk_no_deferred);
15696
15697   /* Parse the class-head.  */
15698   type = cp_parser_class_head (parser,
15699                                &nested_name_specifier_p,
15700                                &attributes,
15701                                &bases);
15702   /* If the class-head was a semantic disaster, skip the entire body
15703      of the class.  */
15704   if (!type)
15705     {
15706       cp_parser_skip_to_end_of_block_or_statement (parser);
15707       pop_deferring_access_checks ();
15708       return error_mark_node;
15709     }
15710
15711   /* Look for the `{'.  */
15712   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15713     {
15714       pop_deferring_access_checks ();
15715       return error_mark_node;
15716     }
15717
15718   /* Process the base classes. If they're invalid, skip the 
15719      entire class body.  */
15720   if (!xref_basetypes (type, bases))
15721     {
15722       /* Consuming the closing brace yields better error messages
15723          later on.  */
15724       if (cp_parser_skip_to_closing_brace (parser))
15725         cp_lexer_consume_token (parser->lexer);
15726       pop_deferring_access_checks ();
15727       return error_mark_node;
15728     }
15729
15730   /* Issue an error message if type-definitions are forbidden here.  */
15731   cp_parser_check_type_definition (parser);
15732   /* Remember that we are defining one more class.  */
15733   ++parser->num_classes_being_defined;
15734   /* Inside the class, surrounding template-parameter-lists do not
15735      apply.  */
15736   saved_num_template_parameter_lists
15737     = parser->num_template_parameter_lists;
15738   parser->num_template_parameter_lists = 0;
15739   /* We are not in a function body.  */
15740   saved_in_function_body = parser->in_function_body;
15741   parser->in_function_body = false;
15742   /* We are not immediately inside an extern "lang" block.  */
15743   saved_in_unbraced_linkage_specification_p
15744     = parser->in_unbraced_linkage_specification_p;
15745   parser->in_unbraced_linkage_specification_p = false;
15746
15747   /* Start the class.  */
15748   if (nested_name_specifier_p)
15749     {
15750       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15751       old_scope = push_inner_scope (scope);
15752     }
15753   type = begin_class_definition (type, attributes);
15754
15755   if (type == error_mark_node)
15756     /* If the type is erroneous, skip the entire body of the class.  */
15757     cp_parser_skip_to_closing_brace (parser);
15758   else
15759     /* Parse the member-specification.  */
15760     cp_parser_member_specification_opt (parser);
15761
15762   /* Look for the trailing `}'.  */
15763   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15764   /* Look for trailing attributes to apply to this class.  */
15765   if (cp_parser_allow_gnu_extensions_p (parser))
15766     attributes = cp_parser_attributes_opt (parser);
15767   if (type != error_mark_node)
15768     type = finish_struct (type, attributes);
15769   if (nested_name_specifier_p)
15770     pop_inner_scope (old_scope, scope);
15771   /* If this class is not itself within the scope of another class,
15772      then we need to parse the bodies of all of the queued function
15773      definitions.  Note that the queued functions defined in a class
15774      are not always processed immediately following the
15775      class-specifier for that class.  Consider:
15776
15777        struct A {
15778          struct B { void f() { sizeof (A); } };
15779        };
15780
15781      If `f' were processed before the processing of `A' were
15782      completed, there would be no way to compute the size of `A'.
15783      Note that the nesting we are interested in here is lexical --
15784      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15785      for:
15786
15787        struct A { struct B; };
15788        struct A::B { void f() { } };
15789
15790      there is no need to delay the parsing of `A::B::f'.  */
15791   if (--parser->num_classes_being_defined == 0)
15792     {
15793       tree queue_entry;
15794       tree fn;
15795       tree class_type = NULL_TREE;
15796       tree pushed_scope = NULL_TREE;
15797
15798       /* In a first pass, parse default arguments to the functions.
15799          Then, in a second pass, parse the bodies of the functions.
15800          This two-phased approach handles cases like:
15801
15802             struct S {
15803               void f() { g(); }
15804               void g(int i = 3);
15805             };
15806
15807          */
15808       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15809              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15810            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15811            TREE_PURPOSE (parser->unparsed_functions_queues)
15812              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15813         {
15814           fn = TREE_VALUE (queue_entry);
15815           /* If there are default arguments that have not yet been processed,
15816              take care of them now.  */
15817           if (class_type != TREE_PURPOSE (queue_entry))
15818             {
15819               if (pushed_scope)
15820                 pop_scope (pushed_scope);
15821               class_type = TREE_PURPOSE (queue_entry);
15822               pushed_scope = push_scope (class_type);
15823             }
15824           /* Make sure that any template parameters are in scope.  */
15825           maybe_begin_member_template_processing (fn);
15826           /* Parse the default argument expressions.  */
15827           cp_parser_late_parsing_default_args (parser, fn);
15828           /* Remove any template parameters from the symbol table.  */
15829           maybe_end_member_template_processing ();
15830         }
15831       if (pushed_scope)
15832         pop_scope (pushed_scope);
15833       /* Now parse the body of the functions.  */
15834       for (TREE_VALUE (parser->unparsed_functions_queues)
15835              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15836            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15837            TREE_VALUE (parser->unparsed_functions_queues)
15838              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15839         {
15840           /* Figure out which function we need to process.  */
15841           fn = TREE_VALUE (queue_entry);
15842           /* Parse the function.  */
15843           cp_parser_late_parsing_for_member (parser, fn);
15844         }
15845     }
15846
15847   /* Put back any saved access checks.  */
15848   pop_deferring_access_checks ();
15849
15850   /* Restore saved state.  */
15851   parser->in_function_body = saved_in_function_body;
15852   parser->num_template_parameter_lists
15853     = saved_num_template_parameter_lists;
15854   parser->in_unbraced_linkage_specification_p
15855     = saved_in_unbraced_linkage_specification_p;
15856
15857   return type;
15858 }
15859
15860 /* Parse a class-head.
15861
15862    class-head:
15863      class-key identifier [opt] base-clause [opt]
15864      class-key nested-name-specifier identifier base-clause [opt]
15865      class-key nested-name-specifier [opt] template-id
15866        base-clause [opt]
15867
15868    GNU Extensions:
15869      class-key attributes identifier [opt] base-clause [opt]
15870      class-key attributes nested-name-specifier identifier base-clause [opt]
15871      class-key attributes nested-name-specifier [opt] template-id
15872        base-clause [opt]
15873
15874    Upon return BASES is initialized to the list of base classes (or
15875    NULL, if there are none) in the same form returned by
15876    cp_parser_base_clause.
15877
15878    Returns the TYPE of the indicated class.  Sets
15879    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15880    involving a nested-name-specifier was used, and FALSE otherwise.
15881
15882    Returns error_mark_node if this is not a class-head.
15883
15884    Returns NULL_TREE if the class-head is syntactically valid, but
15885    semantically invalid in a way that means we should skip the entire
15886    body of the class.  */
15887
15888 static tree
15889 cp_parser_class_head (cp_parser* parser,
15890                       bool* nested_name_specifier_p,
15891                       tree *attributes_p,
15892                       tree *bases)
15893 {
15894   tree nested_name_specifier;
15895   enum tag_types class_key;
15896   tree id = NULL_TREE;
15897   tree type = NULL_TREE;
15898   tree attributes;
15899   bool template_id_p = false;
15900   bool qualified_p = false;
15901   bool invalid_nested_name_p = false;
15902   bool invalid_explicit_specialization_p = false;
15903   tree pushed_scope = NULL_TREE;
15904   unsigned num_templates;
15905   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15906   /* Assume no nested-name-specifier will be present.  */
15907   *nested_name_specifier_p = false;
15908   /* Assume no template parameter lists will be used in defining the
15909      type.  */
15910   num_templates = 0;
15911
15912   *bases = NULL_TREE;
15913
15914   /* Look for the class-key.  */
15915   class_key = cp_parser_class_key (parser);
15916   if (class_key == none_type)
15917     return error_mark_node;
15918
15919   /* Parse the attributes.  */
15920   attributes = cp_parser_attributes_opt (parser);
15921
15922   /* If the next token is `::', that is invalid -- but sometimes
15923      people do try to write:
15924
15925        struct ::S {};
15926
15927      Handle this gracefully by accepting the extra qualifier, and then
15928      issuing an error about it later if this really is a
15929      class-head.  If it turns out just to be an elaborated type
15930      specifier, remain silent.  */
15931   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15932     qualified_p = true;
15933
15934   push_deferring_access_checks (dk_no_check);
15935
15936   /* Determine the name of the class.  Begin by looking for an
15937      optional nested-name-specifier.  */
15938   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15939   nested_name_specifier
15940     = cp_parser_nested_name_specifier_opt (parser,
15941                                            /*typename_keyword_p=*/false,
15942                                            /*check_dependency_p=*/false,
15943                                            /*type_p=*/false,
15944                                            /*is_declaration=*/false);
15945   /* If there was a nested-name-specifier, then there *must* be an
15946      identifier.  */
15947   if (nested_name_specifier)
15948     {
15949       type_start_token = cp_lexer_peek_token (parser->lexer);
15950       /* Although the grammar says `identifier', it really means
15951          `class-name' or `template-name'.  You are only allowed to
15952          define a class that has already been declared with this
15953          syntax.
15954
15955          The proposed resolution for Core Issue 180 says that wherever
15956          you see `class T::X' you should treat `X' as a type-name.
15957
15958          It is OK to define an inaccessible class; for example:
15959
15960            class A { class B; };
15961            class A::B {};
15962
15963          We do not know if we will see a class-name, or a
15964          template-name.  We look for a class-name first, in case the
15965          class-name is a template-id; if we looked for the
15966          template-name first we would stop after the template-name.  */
15967       cp_parser_parse_tentatively (parser);
15968       type = cp_parser_class_name (parser,
15969                                    /*typename_keyword_p=*/false,
15970                                    /*template_keyword_p=*/false,
15971                                    class_type,
15972                                    /*check_dependency_p=*/false,
15973                                    /*class_head_p=*/true,
15974                                    /*is_declaration=*/false);
15975       /* If that didn't work, ignore the nested-name-specifier.  */
15976       if (!cp_parser_parse_definitely (parser))
15977         {
15978           invalid_nested_name_p = true;
15979           type_start_token = cp_lexer_peek_token (parser->lexer);
15980           id = cp_parser_identifier (parser);
15981           if (id == error_mark_node)
15982             id = NULL_TREE;
15983         }
15984       /* If we could not find a corresponding TYPE, treat this
15985          declaration like an unqualified declaration.  */
15986       if (type == error_mark_node)
15987         nested_name_specifier = NULL_TREE;
15988       /* Otherwise, count the number of templates used in TYPE and its
15989          containing scopes.  */
15990       else
15991         {
15992           tree scope;
15993
15994           for (scope = TREE_TYPE (type);
15995                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15996                scope = (TYPE_P (scope)
15997                         ? TYPE_CONTEXT (scope)
15998                         : DECL_CONTEXT (scope)))
15999             if (TYPE_P (scope)
16000                 && CLASS_TYPE_P (scope)
16001                 && CLASSTYPE_TEMPLATE_INFO (scope)
16002                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16003                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16004               ++num_templates;
16005         }
16006     }
16007   /* Otherwise, the identifier is optional.  */
16008   else
16009     {
16010       /* We don't know whether what comes next is a template-id,
16011          an identifier, or nothing at all.  */
16012       cp_parser_parse_tentatively (parser);
16013       /* Check for a template-id.  */
16014       type_start_token = cp_lexer_peek_token (parser->lexer);
16015       id = cp_parser_template_id (parser,
16016                                   /*template_keyword_p=*/false,
16017                                   /*check_dependency_p=*/true,
16018                                   /*is_declaration=*/true);
16019       /* If that didn't work, it could still be an identifier.  */
16020       if (!cp_parser_parse_definitely (parser))
16021         {
16022           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16023             {
16024               type_start_token = cp_lexer_peek_token (parser->lexer);
16025               id = cp_parser_identifier (parser);
16026             }
16027           else
16028             id = NULL_TREE;
16029         }
16030       else
16031         {
16032           template_id_p = true;
16033           ++num_templates;
16034         }
16035     }
16036
16037   pop_deferring_access_checks ();
16038
16039   if (id)
16040     cp_parser_check_for_invalid_template_id (parser, id,
16041                                              type_start_token->location);
16042
16043   /* If it's not a `:' or a `{' then we can't really be looking at a
16044      class-head, since a class-head only appears as part of a
16045      class-specifier.  We have to detect this situation before calling
16046      xref_tag, since that has irreversible side-effects.  */
16047   if (!cp_parser_next_token_starts_class_definition_p (parser))
16048     {
16049       cp_parser_error (parser, "expected %<{%> or %<:%>");
16050       return error_mark_node;
16051     }
16052
16053   /* At this point, we're going ahead with the class-specifier, even
16054      if some other problem occurs.  */
16055   cp_parser_commit_to_tentative_parse (parser);
16056   /* Issue the error about the overly-qualified name now.  */
16057   if (qualified_p)
16058     {
16059       cp_parser_error (parser,
16060                        "global qualification of class name is invalid");
16061       return error_mark_node;
16062     }
16063   else if (invalid_nested_name_p)
16064     {
16065       cp_parser_error (parser,
16066                        "qualified name does not name a class");
16067       return error_mark_node;
16068     }
16069   else if (nested_name_specifier)
16070     {
16071       tree scope;
16072
16073       /* Reject typedef-names in class heads.  */
16074       if (!DECL_IMPLICIT_TYPEDEF_P (type))
16075         {
16076           error_at (type_start_token->location,
16077                     "invalid class name in declaration of %qD",
16078                     type);
16079           type = NULL_TREE;
16080           goto done;
16081         }
16082
16083       /* Figure out in what scope the declaration is being placed.  */
16084       scope = current_scope ();
16085       /* If that scope does not contain the scope in which the
16086          class was originally declared, the program is invalid.  */
16087       if (scope && !is_ancestor (scope, nested_name_specifier))
16088         {
16089           if (at_namespace_scope_p ())
16090             error_at (type_start_token->location,
16091                       "declaration of %qD in namespace %qD which does not "
16092                       "enclose %qD",
16093                       type, scope, nested_name_specifier);
16094           else
16095             error_at (type_start_token->location,
16096                       "declaration of %qD in %qD which does not enclose %qD",
16097                       type, scope, nested_name_specifier);
16098           type = NULL_TREE;
16099           goto done;
16100         }
16101       /* [dcl.meaning]
16102
16103          A declarator-id shall not be qualified except for the
16104          definition of a ... nested class outside of its class
16105          ... [or] the definition or explicit instantiation of a
16106          class member of a namespace outside of its namespace.  */
16107       if (scope == nested_name_specifier)
16108         {
16109           permerror (nested_name_specifier_token_start->location,
16110                      "extra qualification not allowed");
16111           nested_name_specifier = NULL_TREE;
16112           num_templates = 0;
16113         }
16114     }
16115   /* An explicit-specialization must be preceded by "template <>".  If
16116      it is not, try to recover gracefully.  */
16117   if (at_namespace_scope_p ()
16118       && parser->num_template_parameter_lists == 0
16119       && template_id_p)
16120     {
16121       error_at (type_start_token->location,
16122                 "an explicit specialization must be preceded by %<template <>%>");
16123       invalid_explicit_specialization_p = true;
16124       /* Take the same action that would have been taken by
16125          cp_parser_explicit_specialization.  */
16126       ++parser->num_template_parameter_lists;
16127       begin_specialization ();
16128     }
16129   /* There must be no "return" statements between this point and the
16130      end of this function; set "type "to the correct return value and
16131      use "goto done;" to return.  */
16132   /* Make sure that the right number of template parameters were
16133      present.  */
16134   if (!cp_parser_check_template_parameters (parser, num_templates,
16135                                             type_start_token->location,
16136                                             /*declarator=*/NULL))
16137     {
16138       /* If something went wrong, there is no point in even trying to
16139          process the class-definition.  */
16140       type = NULL_TREE;
16141       goto done;
16142     }
16143
16144   /* Look up the type.  */
16145   if (template_id_p)
16146     {
16147       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16148           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16149               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16150         {
16151           error_at (type_start_token->location,
16152                     "function template %qD redeclared as a class template", id);
16153           type = error_mark_node;
16154         }
16155       else
16156         {
16157           type = TREE_TYPE (id);
16158           type = maybe_process_partial_specialization (type);
16159         }
16160       if (nested_name_specifier)
16161         pushed_scope = push_scope (nested_name_specifier);
16162     }
16163   else if (nested_name_specifier)
16164     {
16165       tree class_type;
16166
16167       /* Given:
16168
16169             template <typename T> struct S { struct T };
16170             template <typename T> struct S<T>::T { };
16171
16172          we will get a TYPENAME_TYPE when processing the definition of
16173          `S::T'.  We need to resolve it to the actual type before we
16174          try to define it.  */
16175       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16176         {
16177           class_type = resolve_typename_type (TREE_TYPE (type),
16178                                               /*only_current_p=*/false);
16179           if (TREE_CODE (class_type) != TYPENAME_TYPE)
16180             type = TYPE_NAME (class_type);
16181           else
16182             {
16183               cp_parser_error (parser, "could not resolve typename type");
16184               type = error_mark_node;
16185             }
16186         }
16187
16188       if (maybe_process_partial_specialization (TREE_TYPE (type))
16189           == error_mark_node)
16190         {
16191           type = NULL_TREE;
16192           goto done;
16193         }
16194
16195       class_type = current_class_type;
16196       /* Enter the scope indicated by the nested-name-specifier.  */
16197       pushed_scope = push_scope (nested_name_specifier);
16198       /* Get the canonical version of this type.  */
16199       type = TYPE_MAIN_DECL (TREE_TYPE (type));
16200       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16201           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16202         {
16203           type = push_template_decl (type);
16204           if (type == error_mark_node)
16205             {
16206               type = NULL_TREE;
16207               goto done;
16208             }
16209         }
16210
16211       type = TREE_TYPE (type);
16212       *nested_name_specifier_p = true;
16213     }
16214   else      /* The name is not a nested name.  */
16215     {
16216       /* If the class was unnamed, create a dummy name.  */
16217       if (!id)
16218         id = make_anon_name ();
16219       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16220                        parser->num_template_parameter_lists);
16221     }
16222
16223   /* Indicate whether this class was declared as a `class' or as a
16224      `struct'.  */
16225   if (TREE_CODE (type) == RECORD_TYPE)
16226     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16227   cp_parser_check_class_key (class_key, type);
16228
16229   /* If this type was already complete, and we see another definition,
16230      that's an error.  */
16231   if (type != error_mark_node && COMPLETE_TYPE_P (type))
16232     {
16233       error_at (type_start_token->location, "redefinition of %q#T",
16234                 type);
16235       error_at (type_start_token->location, "previous definition of %q+#T",
16236                 type);
16237       type = NULL_TREE;
16238       goto done;
16239     }
16240   else if (type == error_mark_node)
16241     type = NULL_TREE;
16242
16243   /* We will have entered the scope containing the class; the names of
16244      base classes should be looked up in that context.  For example:
16245
16246        struct A { struct B {}; struct C; };
16247        struct A::C : B {};
16248
16249      is valid.  */
16250
16251   /* Get the list of base-classes, if there is one.  */
16252   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16253     *bases = cp_parser_base_clause (parser);
16254
16255  done:
16256   /* Leave the scope given by the nested-name-specifier.  We will
16257      enter the class scope itself while processing the members.  */
16258   if (pushed_scope)
16259     pop_scope (pushed_scope);
16260
16261   if (invalid_explicit_specialization_p)
16262     {
16263       end_specialization ();
16264       --parser->num_template_parameter_lists;
16265     }
16266   *attributes_p = attributes;
16267   return type;
16268 }
16269
16270 /* Parse a class-key.
16271
16272    class-key:
16273      class
16274      struct
16275      union
16276
16277    Returns the kind of class-key specified, or none_type to indicate
16278    error.  */
16279
16280 static enum tag_types
16281 cp_parser_class_key (cp_parser* parser)
16282 {
16283   cp_token *token;
16284   enum tag_types tag_type;
16285
16286   /* Look for the class-key.  */
16287   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16288   if (!token)
16289     return none_type;
16290
16291   /* Check to see if the TOKEN is a class-key.  */
16292   tag_type = cp_parser_token_is_class_key (token);
16293   if (!tag_type)
16294     cp_parser_error (parser, "expected class-key");
16295   return tag_type;
16296 }
16297
16298 /* Parse an (optional) member-specification.
16299
16300    member-specification:
16301      member-declaration member-specification [opt]
16302      access-specifier : member-specification [opt]  */
16303
16304 static void
16305 cp_parser_member_specification_opt (cp_parser* parser)
16306 {
16307   while (true)
16308     {
16309       cp_token *token;
16310       enum rid keyword;
16311
16312       /* Peek at the next token.  */
16313       token = cp_lexer_peek_token (parser->lexer);
16314       /* If it's a `}', or EOF then we've seen all the members.  */
16315       if (token->type == CPP_CLOSE_BRACE
16316           || token->type == CPP_EOF
16317           || token->type == CPP_PRAGMA_EOL)
16318         break;
16319
16320       /* See if this token is a keyword.  */
16321       keyword = token->keyword;
16322       switch (keyword)
16323         {
16324         case RID_PUBLIC:
16325         case RID_PROTECTED:
16326         case RID_PRIVATE:
16327           /* Consume the access-specifier.  */
16328           cp_lexer_consume_token (parser->lexer);
16329           /* Remember which access-specifier is active.  */
16330           current_access_specifier = token->u.value;
16331           /* Look for the `:'.  */
16332           cp_parser_require (parser, CPP_COLON, "%<:%>");
16333           break;
16334
16335         default:
16336           /* Accept #pragmas at class scope.  */
16337           if (token->type == CPP_PRAGMA)
16338             {
16339               cp_parser_pragma (parser, pragma_external);
16340               break;
16341             }
16342
16343           /* Otherwise, the next construction must be a
16344              member-declaration.  */
16345           cp_parser_member_declaration (parser);
16346         }
16347     }
16348 }
16349
16350 /* Parse a member-declaration.
16351
16352    member-declaration:
16353      decl-specifier-seq [opt] member-declarator-list [opt] ;
16354      function-definition ; [opt]
16355      :: [opt] nested-name-specifier template [opt] unqualified-id ;
16356      using-declaration
16357      template-declaration
16358
16359    member-declarator-list:
16360      member-declarator
16361      member-declarator-list , member-declarator
16362
16363    member-declarator:
16364      declarator pure-specifier [opt]
16365      declarator constant-initializer [opt]
16366      identifier [opt] : constant-expression
16367
16368    GNU Extensions:
16369
16370    member-declaration:
16371      __extension__ member-declaration
16372
16373    member-declarator:
16374      declarator attributes [opt] pure-specifier [opt]
16375      declarator attributes [opt] constant-initializer [opt]
16376      identifier [opt] attributes [opt] : constant-expression  
16377
16378    C++0x Extensions:
16379
16380    member-declaration:
16381      static_assert-declaration  */
16382
16383 static void
16384 cp_parser_member_declaration (cp_parser* parser)
16385 {
16386   cp_decl_specifier_seq decl_specifiers;
16387   tree prefix_attributes;
16388   tree decl;
16389   int declares_class_or_enum;
16390   bool friend_p;
16391   cp_token *token = NULL;
16392   cp_token *decl_spec_token_start = NULL;
16393   cp_token *initializer_token_start = NULL;
16394   int saved_pedantic;
16395
16396   /* Check for the `__extension__' keyword.  */
16397   if (cp_parser_extension_opt (parser, &saved_pedantic))
16398     {
16399       /* Recurse.  */
16400       cp_parser_member_declaration (parser);
16401       /* Restore the old value of the PEDANTIC flag.  */
16402       pedantic = saved_pedantic;
16403
16404       return;
16405     }
16406
16407   /* Check for a template-declaration.  */
16408   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16409     {
16410       /* An explicit specialization here is an error condition, and we
16411          expect the specialization handler to detect and report this.  */
16412       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16413           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16414         cp_parser_explicit_specialization (parser);
16415       else
16416         cp_parser_template_declaration (parser, /*member_p=*/true);
16417
16418       return;
16419     }
16420
16421   /* Check for a using-declaration.  */
16422   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16423     {
16424       /* Parse the using-declaration.  */
16425       cp_parser_using_declaration (parser,
16426                                    /*access_declaration_p=*/false);
16427       return;
16428     }
16429
16430   /* Check for @defs.  */
16431   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16432     {
16433       tree ivar, member;
16434       tree ivar_chains = cp_parser_objc_defs_expression (parser);
16435       ivar = ivar_chains;
16436       while (ivar)
16437         {
16438           member = ivar;
16439           ivar = TREE_CHAIN (member);
16440           TREE_CHAIN (member) = NULL_TREE;
16441           finish_member_declaration (member);
16442         }
16443       return;
16444     }
16445
16446   /* If the next token is `static_assert' we have a static assertion.  */
16447   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16448     {
16449       cp_parser_static_assert (parser, /*member_p=*/true);
16450       return;
16451     }
16452
16453   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16454     return;
16455
16456   /* Parse the decl-specifier-seq.  */
16457   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16458   cp_parser_decl_specifier_seq (parser,
16459                                 CP_PARSER_FLAGS_OPTIONAL,
16460                                 &decl_specifiers,
16461                                 &declares_class_or_enum);
16462   prefix_attributes = decl_specifiers.attributes;
16463   decl_specifiers.attributes = NULL_TREE;
16464   /* Check for an invalid type-name.  */
16465   if (!decl_specifiers.type
16466       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16467     return;
16468   /* If there is no declarator, then the decl-specifier-seq should
16469      specify a type.  */
16470   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16471     {
16472       /* If there was no decl-specifier-seq, and the next token is a
16473          `;', then we have something like:
16474
16475            struct S { ; };
16476
16477          [class.mem]
16478
16479          Each member-declaration shall declare at least one member
16480          name of the class.  */
16481       if (!decl_specifiers.any_specifiers_p)
16482         {
16483           cp_token *token = cp_lexer_peek_token (parser->lexer);
16484           if (!in_system_header_at (token->location))
16485             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16486         }
16487       else
16488         {
16489           tree type;
16490
16491           /* See if this declaration is a friend.  */
16492           friend_p = cp_parser_friend_p (&decl_specifiers);
16493           /* If there were decl-specifiers, check to see if there was
16494              a class-declaration.  */
16495           type = check_tag_decl (&decl_specifiers);
16496           /* Nested classes have already been added to the class, but
16497              a `friend' needs to be explicitly registered.  */
16498           if (friend_p)
16499             {
16500               /* If the `friend' keyword was present, the friend must
16501                  be introduced with a class-key.  */
16502                if (!declares_class_or_enum)
16503                  error_at (decl_spec_token_start->location,
16504                            "a class-key must be used when declaring a friend");
16505                /* In this case:
16506
16507                     template <typename T> struct A {
16508                       friend struct A<T>::B;
16509                     };
16510
16511                   A<T>::B will be represented by a TYPENAME_TYPE, and
16512                   therefore not recognized by check_tag_decl.  */
16513                if (!type
16514                    && decl_specifiers.type
16515                    && TYPE_P (decl_specifiers.type))
16516                  type = decl_specifiers.type;
16517                if (!type || !TYPE_P (type))
16518                  error_at (decl_spec_token_start->location,
16519                            "friend declaration does not name a class or "
16520                            "function");
16521                else
16522                  make_friend_class (current_class_type, type,
16523                                     /*complain=*/true);
16524             }
16525           /* If there is no TYPE, an error message will already have
16526              been issued.  */
16527           else if (!type || type == error_mark_node)
16528             ;
16529           /* An anonymous aggregate has to be handled specially; such
16530              a declaration really declares a data member (with a
16531              particular type), as opposed to a nested class.  */
16532           else if (ANON_AGGR_TYPE_P (type))
16533             {
16534               /* Remove constructors and such from TYPE, now that we
16535                  know it is an anonymous aggregate.  */
16536               fixup_anonymous_aggr (type);
16537               /* And make the corresponding data member.  */
16538               decl = build_decl (decl_spec_token_start->location,
16539                                  FIELD_DECL, NULL_TREE, type);
16540               /* Add it to the class.  */
16541               finish_member_declaration (decl);
16542             }
16543           else
16544             cp_parser_check_access_in_redeclaration
16545                                               (TYPE_NAME (type),
16546                                                decl_spec_token_start->location);
16547         }
16548     }
16549   else
16550     {
16551       /* See if these declarations will be friends.  */
16552       friend_p = cp_parser_friend_p (&decl_specifiers);
16553
16554       /* Keep going until we hit the `;' at the end of the
16555          declaration.  */
16556       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16557         {
16558           tree attributes = NULL_TREE;
16559           tree first_attribute;
16560
16561           /* Peek at the next token.  */
16562           token = cp_lexer_peek_token (parser->lexer);
16563
16564           /* Check for a bitfield declaration.  */
16565           if (token->type == CPP_COLON
16566               || (token->type == CPP_NAME
16567                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16568                   == CPP_COLON))
16569             {
16570               tree identifier;
16571               tree width;
16572
16573               /* Get the name of the bitfield.  Note that we cannot just
16574                  check TOKEN here because it may have been invalidated by
16575                  the call to cp_lexer_peek_nth_token above.  */
16576               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16577                 identifier = cp_parser_identifier (parser);
16578               else
16579                 identifier = NULL_TREE;
16580
16581               /* Consume the `:' token.  */
16582               cp_lexer_consume_token (parser->lexer);
16583               /* Get the width of the bitfield.  */
16584               width
16585                 = cp_parser_constant_expression (parser,
16586                                                  /*allow_non_constant=*/false,
16587                                                  NULL);
16588
16589               /* Look for attributes that apply to the bitfield.  */
16590               attributes = cp_parser_attributes_opt (parser);
16591               /* Remember which attributes are prefix attributes and
16592                  which are not.  */
16593               first_attribute = attributes;
16594               /* Combine the attributes.  */
16595               attributes = chainon (prefix_attributes, attributes);
16596
16597               /* Create the bitfield declaration.  */
16598               decl = grokbitfield (identifier
16599                                    ? make_id_declarator (NULL_TREE,
16600                                                          identifier,
16601                                                          sfk_none)
16602                                    : NULL,
16603                                    &decl_specifiers,
16604                                    width,
16605                                    attributes);
16606             }
16607           else
16608             {
16609               cp_declarator *declarator;
16610               tree initializer;
16611               tree asm_specification;
16612               int ctor_dtor_or_conv_p;
16613
16614               /* Parse the declarator.  */
16615               declarator
16616                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16617                                         &ctor_dtor_or_conv_p,
16618                                         /*parenthesized_p=*/NULL,
16619                                         /*member_p=*/true);
16620
16621               /* If something went wrong parsing the declarator, make sure
16622                  that we at least consume some tokens.  */
16623               if (declarator == cp_error_declarator)
16624                 {
16625                   /* Skip to the end of the statement.  */
16626                   cp_parser_skip_to_end_of_statement (parser);
16627                   /* If the next token is not a semicolon, that is
16628                      probably because we just skipped over the body of
16629                      a function.  So, we consume a semicolon if
16630                      present, but do not issue an error message if it
16631                      is not present.  */
16632                   if (cp_lexer_next_token_is (parser->lexer,
16633                                               CPP_SEMICOLON))
16634                     cp_lexer_consume_token (parser->lexer);
16635                   return;
16636                 }
16637
16638               if (declares_class_or_enum & 2)
16639                 cp_parser_check_for_definition_in_return_type
16640                                             (declarator, decl_specifiers.type,
16641                                              decl_specifiers.type_location);
16642
16643               /* Look for an asm-specification.  */
16644               asm_specification = cp_parser_asm_specification_opt (parser);
16645               /* Look for attributes that apply to the declaration.  */
16646               attributes = cp_parser_attributes_opt (parser);
16647               /* Remember which attributes are prefix attributes and
16648                  which are not.  */
16649               first_attribute = attributes;
16650               /* Combine the attributes.  */
16651               attributes = chainon (prefix_attributes, attributes);
16652
16653               /* If it's an `=', then we have a constant-initializer or a
16654                  pure-specifier.  It is not correct to parse the
16655                  initializer before registering the member declaration
16656                  since the member declaration should be in scope while
16657                  its initializer is processed.  However, the rest of the
16658                  front end does not yet provide an interface that allows
16659                  us to handle this correctly.  */
16660               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16661                 {
16662                   /* In [class.mem]:
16663
16664                      A pure-specifier shall be used only in the declaration of
16665                      a virtual function.
16666
16667                      A member-declarator can contain a constant-initializer
16668                      only if it declares a static member of integral or
16669                      enumeration type.
16670
16671                      Therefore, if the DECLARATOR is for a function, we look
16672                      for a pure-specifier; otherwise, we look for a
16673                      constant-initializer.  When we call `grokfield', it will
16674                      perform more stringent semantics checks.  */
16675                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
16676                   if (function_declarator_p (declarator))
16677                     initializer = cp_parser_pure_specifier (parser);
16678                   else
16679                     /* Parse the initializer.  */
16680                     initializer = cp_parser_constant_initializer (parser);
16681                 }
16682               /* Otherwise, there is no initializer.  */
16683               else
16684                 initializer = NULL_TREE;
16685
16686               /* See if we are probably looking at a function
16687                  definition.  We are certainly not looking at a
16688                  member-declarator.  Calling `grokfield' has
16689                  side-effects, so we must not do it unless we are sure
16690                  that we are looking at a member-declarator.  */
16691               if (cp_parser_token_starts_function_definition_p
16692                   (cp_lexer_peek_token (parser->lexer)))
16693                 {
16694                   /* The grammar does not allow a pure-specifier to be
16695                      used when a member function is defined.  (It is
16696                      possible that this fact is an oversight in the
16697                      standard, since a pure function may be defined
16698                      outside of the class-specifier.  */
16699                   if (initializer)
16700                     error_at (initializer_token_start->location,
16701                               "pure-specifier on function-definition");
16702                   decl = cp_parser_save_member_function_body (parser,
16703                                                               &decl_specifiers,
16704                                                               declarator,
16705                                                               attributes);
16706                   /* If the member was not a friend, declare it here.  */
16707                   if (!friend_p)
16708                     finish_member_declaration (decl);
16709                   /* Peek at the next token.  */
16710                   token = cp_lexer_peek_token (parser->lexer);
16711                   /* If the next token is a semicolon, consume it.  */
16712                   if (token->type == CPP_SEMICOLON)
16713                     cp_lexer_consume_token (parser->lexer);
16714                   return;
16715                 }
16716               else
16717                 if (declarator->kind == cdk_function)
16718                   declarator->id_loc = token->location;
16719                 /* Create the declaration.  */
16720                 decl = grokfield (declarator, &decl_specifiers,
16721                                   initializer, /*init_const_expr_p=*/true,
16722                                   asm_specification,
16723                                   attributes);
16724             }
16725
16726           /* Reset PREFIX_ATTRIBUTES.  */
16727           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16728             attributes = TREE_CHAIN (attributes);
16729           if (attributes)
16730             TREE_CHAIN (attributes) = NULL_TREE;
16731
16732           /* If there is any qualification still in effect, clear it
16733              now; we will be starting fresh with the next declarator.  */
16734           parser->scope = NULL_TREE;
16735           parser->qualifying_scope = NULL_TREE;
16736           parser->object_scope = NULL_TREE;
16737           /* If it's a `,', then there are more declarators.  */
16738           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16739             cp_lexer_consume_token (parser->lexer);
16740           /* If the next token isn't a `;', then we have a parse error.  */
16741           else if (cp_lexer_next_token_is_not (parser->lexer,
16742                                                CPP_SEMICOLON))
16743             {
16744               cp_parser_error (parser, "expected %<;%>");
16745               /* Skip tokens until we find a `;'.  */
16746               cp_parser_skip_to_end_of_statement (parser);
16747
16748               break;
16749             }
16750
16751           if (decl)
16752             {
16753               /* Add DECL to the list of members.  */
16754               if (!friend_p)
16755                 finish_member_declaration (decl);
16756
16757               if (TREE_CODE (decl) == FUNCTION_DECL)
16758                 cp_parser_save_default_args (parser, decl);
16759             }
16760         }
16761     }
16762
16763   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16764 }
16765
16766 /* Parse a pure-specifier.
16767
16768    pure-specifier:
16769      = 0
16770
16771    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16772    Otherwise, ERROR_MARK_NODE is returned.  */
16773
16774 static tree
16775 cp_parser_pure_specifier (cp_parser* parser)
16776 {
16777   cp_token *token;
16778
16779   /* Look for the `=' token.  */
16780   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16781     return error_mark_node;
16782   /* Look for the `0' token.  */
16783   token = cp_lexer_peek_token (parser->lexer);
16784
16785   if (token->type == CPP_EOF
16786       || token->type == CPP_PRAGMA_EOL)
16787     return error_mark_node;
16788
16789   cp_lexer_consume_token (parser->lexer);
16790
16791   /* Accept = default or = delete in c++0x mode.  */
16792   if (token->keyword == RID_DEFAULT
16793       || token->keyword == RID_DELETE)
16794     {
16795       maybe_warn_cpp0x ("defaulted and deleted functions");
16796       return token->u.value;
16797     }
16798
16799   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16800   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16801     {
16802       cp_parser_error (parser,
16803                        "invalid pure specifier (only %<= 0%> is allowed)");
16804       cp_parser_skip_to_end_of_statement (parser);
16805       return error_mark_node;
16806     }
16807   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16808     {
16809       error_at (token->location, "templates may not be %<virtual%>");
16810       return error_mark_node;
16811     }
16812
16813   return integer_zero_node;
16814 }
16815
16816 /* Parse a constant-initializer.
16817
16818    constant-initializer:
16819      = constant-expression
16820
16821    Returns a representation of the constant-expression.  */
16822
16823 static tree
16824 cp_parser_constant_initializer (cp_parser* parser)
16825 {
16826   /* Look for the `=' token.  */
16827   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16828     return error_mark_node;
16829
16830   /* It is invalid to write:
16831
16832        struct S { static const int i = { 7 }; };
16833
16834      */
16835   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16836     {
16837       cp_parser_error (parser,
16838                        "a brace-enclosed initializer is not allowed here");
16839       /* Consume the opening brace.  */
16840       cp_lexer_consume_token (parser->lexer);
16841       /* Skip the initializer.  */
16842       cp_parser_skip_to_closing_brace (parser);
16843       /* Look for the trailing `}'.  */
16844       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16845
16846       return error_mark_node;
16847     }
16848
16849   return cp_parser_constant_expression (parser,
16850                                         /*allow_non_constant=*/false,
16851                                         NULL);
16852 }
16853
16854 /* Derived classes [gram.class.derived] */
16855
16856 /* Parse a base-clause.
16857
16858    base-clause:
16859      : base-specifier-list
16860
16861    base-specifier-list:
16862      base-specifier ... [opt]
16863      base-specifier-list , base-specifier ... [opt]
16864
16865    Returns a TREE_LIST representing the base-classes, in the order in
16866    which they were declared.  The representation of each node is as
16867    described by cp_parser_base_specifier.
16868
16869    In the case that no bases are specified, this function will return
16870    NULL_TREE, not ERROR_MARK_NODE.  */
16871
16872 static tree
16873 cp_parser_base_clause (cp_parser* parser)
16874 {
16875   tree bases = NULL_TREE;
16876
16877   /* Look for the `:' that begins the list.  */
16878   cp_parser_require (parser, CPP_COLON, "%<:%>");
16879
16880   /* Scan the base-specifier-list.  */
16881   while (true)
16882     {
16883       cp_token *token;
16884       tree base;
16885       bool pack_expansion_p = false;
16886
16887       /* Look for the base-specifier.  */
16888       base = cp_parser_base_specifier (parser);
16889       /* Look for the (optional) ellipsis. */
16890       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16891         {
16892           /* Consume the `...'. */
16893           cp_lexer_consume_token (parser->lexer);
16894
16895           pack_expansion_p = true;
16896         }
16897
16898       /* Add BASE to the front of the list.  */
16899       if (base != error_mark_node)
16900         {
16901           if (pack_expansion_p)
16902             /* Make this a pack expansion type. */
16903             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16904           
16905
16906           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16907             {
16908               TREE_CHAIN (base) = bases;
16909               bases = base;
16910             }
16911         }
16912       /* Peek at the next token.  */
16913       token = cp_lexer_peek_token (parser->lexer);
16914       /* If it's not a comma, then the list is complete.  */
16915       if (token->type != CPP_COMMA)
16916         break;
16917       /* Consume the `,'.  */
16918       cp_lexer_consume_token (parser->lexer);
16919     }
16920
16921   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16922      base class had a qualified name.  However, the next name that
16923      appears is certainly not qualified.  */
16924   parser->scope = NULL_TREE;
16925   parser->qualifying_scope = NULL_TREE;
16926   parser->object_scope = NULL_TREE;
16927
16928   return nreverse (bases);
16929 }
16930
16931 /* Parse a base-specifier.
16932
16933    base-specifier:
16934      :: [opt] nested-name-specifier [opt] class-name
16935      virtual access-specifier [opt] :: [opt] nested-name-specifier
16936        [opt] class-name
16937      access-specifier virtual [opt] :: [opt] nested-name-specifier
16938        [opt] class-name
16939
16940    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16941    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16942    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16943    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16944
16945 static tree
16946 cp_parser_base_specifier (cp_parser* parser)
16947 {
16948   cp_token *token;
16949   bool done = false;
16950   bool virtual_p = false;
16951   bool duplicate_virtual_error_issued_p = false;
16952   bool duplicate_access_error_issued_p = false;
16953   bool class_scope_p, template_p;
16954   tree access = access_default_node;
16955   tree type;
16956
16957   /* Process the optional `virtual' and `access-specifier'.  */
16958   while (!done)
16959     {
16960       /* Peek at the next token.  */
16961       token = cp_lexer_peek_token (parser->lexer);
16962       /* Process `virtual'.  */
16963       switch (token->keyword)
16964         {
16965         case RID_VIRTUAL:
16966           /* If `virtual' appears more than once, issue an error.  */
16967           if (virtual_p && !duplicate_virtual_error_issued_p)
16968             {
16969               cp_parser_error (parser,
16970                                "%<virtual%> specified more than once in base-specified");
16971               duplicate_virtual_error_issued_p = true;
16972             }
16973
16974           virtual_p = true;
16975
16976           /* Consume the `virtual' token.  */
16977           cp_lexer_consume_token (parser->lexer);
16978
16979           break;
16980
16981         case RID_PUBLIC:
16982         case RID_PROTECTED:
16983         case RID_PRIVATE:
16984           /* If more than one access specifier appears, issue an
16985              error.  */
16986           if (access != access_default_node
16987               && !duplicate_access_error_issued_p)
16988             {
16989               cp_parser_error (parser,
16990                                "more than one access specifier in base-specified");
16991               duplicate_access_error_issued_p = true;
16992             }
16993
16994           access = ridpointers[(int) token->keyword];
16995
16996           /* Consume the access-specifier.  */
16997           cp_lexer_consume_token (parser->lexer);
16998
16999           break;
17000
17001         default:
17002           done = true;
17003           break;
17004         }
17005     }
17006   /* It is not uncommon to see programs mechanically, erroneously, use
17007      the 'typename' keyword to denote (dependent) qualified types
17008      as base classes.  */
17009   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17010     {
17011       token = cp_lexer_peek_token (parser->lexer);
17012       if (!processing_template_decl)
17013         error_at (token->location,
17014                   "keyword %<typename%> not allowed outside of templates");
17015       else
17016         error_at (token->location,
17017                   "keyword %<typename%> not allowed in this context "
17018                   "(the base class is implicitly a type)");
17019       cp_lexer_consume_token (parser->lexer);
17020     }
17021
17022   /* Look for the optional `::' operator.  */
17023   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17024   /* Look for the nested-name-specifier.  The simplest way to
17025      implement:
17026
17027        [temp.res]
17028
17029        The keyword `typename' is not permitted in a base-specifier or
17030        mem-initializer; in these contexts a qualified name that
17031        depends on a template-parameter is implicitly assumed to be a
17032        type name.
17033
17034      is to pretend that we have seen the `typename' keyword at this
17035      point.  */
17036   cp_parser_nested_name_specifier_opt (parser,
17037                                        /*typename_keyword_p=*/true,
17038                                        /*check_dependency_p=*/true,
17039                                        typename_type,
17040                                        /*is_declaration=*/true);
17041   /* If the base class is given by a qualified name, assume that names
17042      we see are type names or templates, as appropriate.  */
17043   class_scope_p = (parser->scope && TYPE_P (parser->scope));
17044   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17045
17046   /* Finally, look for the class-name.  */
17047   type = cp_parser_class_name (parser,
17048                                class_scope_p,
17049                                template_p,
17050                                typename_type,
17051                                /*check_dependency_p=*/true,
17052                                /*class_head_p=*/false,
17053                                /*is_declaration=*/true);
17054
17055   if (type == error_mark_node)
17056     return error_mark_node;
17057
17058   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17059 }
17060
17061 /* Exception handling [gram.exception] */
17062
17063 /* Parse an (optional) exception-specification.
17064
17065    exception-specification:
17066      throw ( type-id-list [opt] )
17067
17068    Returns a TREE_LIST representing the exception-specification.  The
17069    TREE_VALUE of each node is a type.  */
17070
17071 static tree
17072 cp_parser_exception_specification_opt (cp_parser* parser)
17073 {
17074   cp_token *token;
17075   tree type_id_list;
17076
17077   /* Peek at the next token.  */
17078   token = cp_lexer_peek_token (parser->lexer);
17079   /* If it's not `throw', then there's no exception-specification.  */
17080   if (!cp_parser_is_keyword (token, RID_THROW))
17081     return NULL_TREE;
17082
17083   /* Consume the `throw'.  */
17084   cp_lexer_consume_token (parser->lexer);
17085
17086   /* Look for the `('.  */
17087   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17088
17089   /* Peek at the next token.  */
17090   token = cp_lexer_peek_token (parser->lexer);
17091   /* If it's not a `)', then there is a type-id-list.  */
17092   if (token->type != CPP_CLOSE_PAREN)
17093     {
17094       const char *saved_message;
17095
17096       /* Types may not be defined in an exception-specification.  */
17097       saved_message = parser->type_definition_forbidden_message;
17098       parser->type_definition_forbidden_message
17099         = "types may not be defined in an exception-specification";
17100       /* Parse the type-id-list.  */
17101       type_id_list = cp_parser_type_id_list (parser);
17102       /* Restore the saved message.  */
17103       parser->type_definition_forbidden_message = saved_message;
17104     }
17105   else
17106     type_id_list = empty_except_spec;
17107
17108   /* Look for the `)'.  */
17109   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17110
17111   return type_id_list;
17112 }
17113
17114 /* Parse an (optional) type-id-list.
17115
17116    type-id-list:
17117      type-id ... [opt]
17118      type-id-list , type-id ... [opt]
17119
17120    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
17121    in the order that the types were presented.  */
17122
17123 static tree
17124 cp_parser_type_id_list (cp_parser* parser)
17125 {
17126   tree types = NULL_TREE;
17127
17128   while (true)
17129     {
17130       cp_token *token;
17131       tree type;
17132
17133       /* Get the next type-id.  */
17134       type = cp_parser_type_id (parser);
17135       /* Parse the optional ellipsis. */
17136       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17137         {
17138           /* Consume the `...'. */
17139           cp_lexer_consume_token (parser->lexer);
17140
17141           /* Turn the type into a pack expansion expression. */
17142           type = make_pack_expansion (type);
17143         }
17144       /* Add it to the list.  */
17145       types = add_exception_specifier (types, type, /*complain=*/1);
17146       /* Peek at the next token.  */
17147       token = cp_lexer_peek_token (parser->lexer);
17148       /* If it is not a `,', we are done.  */
17149       if (token->type != CPP_COMMA)
17150         break;
17151       /* Consume the `,'.  */
17152       cp_lexer_consume_token (parser->lexer);
17153     }
17154
17155   return nreverse (types);
17156 }
17157
17158 /* Parse a try-block.
17159
17160    try-block:
17161      try compound-statement handler-seq  */
17162
17163 static tree
17164 cp_parser_try_block (cp_parser* parser)
17165 {
17166   tree try_block;
17167
17168   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17169   try_block = begin_try_block ();
17170   cp_parser_compound_statement (parser, NULL, true);
17171   finish_try_block (try_block);
17172   cp_parser_handler_seq (parser);
17173   finish_handler_sequence (try_block);
17174
17175   return try_block;
17176 }
17177
17178 /* Parse a function-try-block.
17179
17180    function-try-block:
17181      try ctor-initializer [opt] function-body handler-seq  */
17182
17183 static bool
17184 cp_parser_function_try_block (cp_parser* parser)
17185 {
17186   tree compound_stmt;
17187   tree try_block;
17188   bool ctor_initializer_p;
17189
17190   /* Look for the `try' keyword.  */
17191   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17192     return false;
17193   /* Let the rest of the front end know where we are.  */
17194   try_block = begin_function_try_block (&compound_stmt);
17195   /* Parse the function-body.  */
17196   ctor_initializer_p
17197     = cp_parser_ctor_initializer_opt_and_function_body (parser);
17198   /* We're done with the `try' part.  */
17199   finish_function_try_block (try_block);
17200   /* Parse the handlers.  */
17201   cp_parser_handler_seq (parser);
17202   /* We're done with the handlers.  */
17203   finish_function_handler_sequence (try_block, compound_stmt);
17204
17205   return ctor_initializer_p;
17206 }
17207
17208 /* Parse a handler-seq.
17209
17210    handler-seq:
17211      handler handler-seq [opt]  */
17212
17213 static void
17214 cp_parser_handler_seq (cp_parser* parser)
17215 {
17216   while (true)
17217     {
17218       cp_token *token;
17219
17220       /* Parse the handler.  */
17221       cp_parser_handler (parser);
17222       /* Peek at the next token.  */
17223       token = cp_lexer_peek_token (parser->lexer);
17224       /* If it's not `catch' then there are no more handlers.  */
17225       if (!cp_parser_is_keyword (token, RID_CATCH))
17226         break;
17227     }
17228 }
17229
17230 /* Parse a handler.
17231
17232    handler:
17233      catch ( exception-declaration ) compound-statement  */
17234
17235 static void
17236 cp_parser_handler (cp_parser* parser)
17237 {
17238   tree handler;
17239   tree declaration;
17240
17241   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17242   handler = begin_handler ();
17243   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17244   declaration = cp_parser_exception_declaration (parser);
17245   finish_handler_parms (declaration, handler);
17246   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17247   cp_parser_compound_statement (parser, NULL, false);
17248   finish_handler (handler);
17249 }
17250
17251 /* Parse an exception-declaration.
17252
17253    exception-declaration:
17254      type-specifier-seq declarator
17255      type-specifier-seq abstract-declarator
17256      type-specifier-seq
17257      ...
17258
17259    Returns a VAR_DECL for the declaration, or NULL_TREE if the
17260    ellipsis variant is used.  */
17261
17262 static tree
17263 cp_parser_exception_declaration (cp_parser* parser)
17264 {
17265   cp_decl_specifier_seq type_specifiers;
17266   cp_declarator *declarator;
17267   const char *saved_message;
17268
17269   /* If it's an ellipsis, it's easy to handle.  */
17270   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17271     {
17272       /* Consume the `...' token.  */
17273       cp_lexer_consume_token (parser->lexer);
17274       return NULL_TREE;
17275     }
17276
17277   /* Types may not be defined in exception-declarations.  */
17278   saved_message = parser->type_definition_forbidden_message;
17279   parser->type_definition_forbidden_message
17280     = "types may not be defined in exception-declarations";
17281
17282   /* Parse the type-specifier-seq.  */
17283   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
17284                                 &type_specifiers);
17285   /* If it's a `)', then there is no declarator.  */
17286   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17287     declarator = NULL;
17288   else
17289     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17290                                        /*ctor_dtor_or_conv_p=*/NULL,
17291                                        /*parenthesized_p=*/NULL,
17292                                        /*member_p=*/false);
17293
17294   /* Restore the saved message.  */
17295   parser->type_definition_forbidden_message = saved_message;
17296
17297   if (!type_specifiers.any_specifiers_p)
17298     return error_mark_node;
17299
17300   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17301 }
17302
17303 /* Parse a throw-expression.
17304
17305    throw-expression:
17306      throw assignment-expression [opt]
17307
17308    Returns a THROW_EXPR representing the throw-expression.  */
17309
17310 static tree
17311 cp_parser_throw_expression (cp_parser* parser)
17312 {
17313   tree expression;
17314   cp_token* token;
17315
17316   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17317   token = cp_lexer_peek_token (parser->lexer);
17318   /* Figure out whether or not there is an assignment-expression
17319      following the "throw" keyword.  */
17320   if (token->type == CPP_COMMA
17321       || token->type == CPP_SEMICOLON
17322       || token->type == CPP_CLOSE_PAREN
17323       || token->type == CPP_CLOSE_SQUARE
17324       || token->type == CPP_CLOSE_BRACE
17325       || token->type == CPP_COLON)
17326     expression = NULL_TREE;
17327   else
17328     expression = cp_parser_assignment_expression (parser,
17329                                                   /*cast_p=*/false, NULL);
17330
17331   return build_throw (expression);
17332 }
17333
17334 /* GNU Extensions */
17335
17336 /* Parse an (optional) asm-specification.
17337
17338    asm-specification:
17339      asm ( string-literal )
17340
17341    If the asm-specification is present, returns a STRING_CST
17342    corresponding to the string-literal.  Otherwise, returns
17343    NULL_TREE.  */
17344
17345 static tree
17346 cp_parser_asm_specification_opt (cp_parser* parser)
17347 {
17348   cp_token *token;
17349   tree asm_specification;
17350
17351   /* Peek at the next token.  */
17352   token = cp_lexer_peek_token (parser->lexer);
17353   /* If the next token isn't the `asm' keyword, then there's no
17354      asm-specification.  */
17355   if (!cp_parser_is_keyword (token, RID_ASM))
17356     return NULL_TREE;
17357
17358   /* Consume the `asm' token.  */
17359   cp_lexer_consume_token (parser->lexer);
17360   /* Look for the `('.  */
17361   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17362
17363   /* Look for the string-literal.  */
17364   asm_specification = cp_parser_string_literal (parser, false, false);
17365
17366   /* Look for the `)'.  */
17367   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17368
17369   return asm_specification;
17370 }
17371
17372 /* Parse an asm-operand-list.
17373
17374    asm-operand-list:
17375      asm-operand
17376      asm-operand-list , asm-operand
17377
17378    asm-operand:
17379      string-literal ( expression )
17380      [ string-literal ] string-literal ( expression )
17381
17382    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
17383    each node is the expression.  The TREE_PURPOSE is itself a
17384    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17385    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17386    is a STRING_CST for the string literal before the parenthesis. Returns
17387    ERROR_MARK_NODE if any of the operands are invalid.  */
17388
17389 static tree
17390 cp_parser_asm_operand_list (cp_parser* parser)
17391 {
17392   tree asm_operands = NULL_TREE;
17393   bool invalid_operands = false;
17394
17395   while (true)
17396     {
17397       tree string_literal;
17398       tree expression;
17399       tree name;
17400
17401       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17402         {
17403           /* Consume the `[' token.  */
17404           cp_lexer_consume_token (parser->lexer);
17405           /* Read the operand name.  */
17406           name = cp_parser_identifier (parser);
17407           if (name != error_mark_node)
17408             name = build_string (IDENTIFIER_LENGTH (name),
17409                                  IDENTIFIER_POINTER (name));
17410           /* Look for the closing `]'.  */
17411           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17412         }
17413       else
17414         name = NULL_TREE;
17415       /* Look for the string-literal.  */
17416       string_literal = cp_parser_string_literal (parser, false, false);
17417
17418       /* Look for the `('.  */
17419       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17420       /* Parse the expression.  */
17421       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17422       /* Look for the `)'.  */
17423       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17424
17425       if (name == error_mark_node 
17426           || string_literal == error_mark_node 
17427           || expression == error_mark_node)
17428         invalid_operands = true;
17429
17430       /* Add this operand to the list.  */
17431       asm_operands = tree_cons (build_tree_list (name, string_literal),
17432                                 expression,
17433                                 asm_operands);
17434       /* If the next token is not a `,', there are no more
17435          operands.  */
17436       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17437         break;
17438       /* Consume the `,'.  */
17439       cp_lexer_consume_token (parser->lexer);
17440     }
17441
17442   return invalid_operands ? error_mark_node : nreverse (asm_operands);
17443 }
17444
17445 /* Parse an asm-clobber-list.
17446
17447    asm-clobber-list:
17448      string-literal
17449      asm-clobber-list , string-literal
17450
17451    Returns a TREE_LIST, indicating the clobbers in the order that they
17452    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
17453
17454 static tree
17455 cp_parser_asm_clobber_list (cp_parser* parser)
17456 {
17457   tree clobbers = NULL_TREE;
17458
17459   while (true)
17460     {
17461       tree string_literal;
17462
17463       /* Look for the string literal.  */
17464       string_literal = cp_parser_string_literal (parser, false, false);
17465       /* Add it to the list.  */
17466       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17467       /* If the next token is not a `,', then the list is
17468          complete.  */
17469       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17470         break;
17471       /* Consume the `,' token.  */
17472       cp_lexer_consume_token (parser->lexer);
17473     }
17474
17475   return clobbers;
17476 }
17477
17478 /* Parse an asm-label-list.
17479
17480    asm-label-list:
17481      identifier
17482      asm-label-list , identifier
17483
17484    Returns a TREE_LIST, indicating the labels in the order that they
17485    appeared.  The TREE_VALUE of each node is a label.  */
17486
17487 static tree
17488 cp_parser_asm_label_list (cp_parser* parser)
17489 {
17490   tree labels = NULL_TREE;
17491
17492   while (true)
17493     {
17494       tree identifier, label, name;
17495
17496       /* Look for the identifier.  */
17497       identifier = cp_parser_identifier (parser);
17498       if (!error_operand_p (identifier))
17499         {
17500           label = lookup_label (identifier);
17501           if (TREE_CODE (label) == LABEL_DECL)
17502             {
17503               TREE_USED (label) = 1;
17504               check_goto (label);
17505               name = build_string (IDENTIFIER_LENGTH (identifier),
17506                                    IDENTIFIER_POINTER (identifier));
17507               labels = tree_cons (name, label, labels);
17508             }
17509         }
17510       /* If the next token is not a `,', then the list is
17511          complete.  */
17512       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17513         break;
17514       /* Consume the `,' token.  */
17515       cp_lexer_consume_token (parser->lexer);
17516     }
17517
17518   return nreverse (labels);
17519 }
17520
17521 /* Parse an (optional) series of attributes.
17522
17523    attributes:
17524      attributes attribute
17525
17526    attribute:
17527      __attribute__ (( attribute-list [opt] ))
17528
17529    The return value is as for cp_parser_attribute_list.  */
17530
17531 static tree
17532 cp_parser_attributes_opt (cp_parser* parser)
17533 {
17534   tree attributes = NULL_TREE;
17535
17536   while (true)
17537     {
17538       cp_token *token;
17539       tree attribute_list;
17540
17541       /* Peek at the next token.  */
17542       token = cp_lexer_peek_token (parser->lexer);
17543       /* If it's not `__attribute__', then we're done.  */
17544       if (token->keyword != RID_ATTRIBUTE)
17545         break;
17546
17547       /* Consume the `__attribute__' keyword.  */
17548       cp_lexer_consume_token (parser->lexer);
17549       /* Look for the two `(' tokens.  */
17550       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17551       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17552
17553       /* Peek at the next token.  */
17554       token = cp_lexer_peek_token (parser->lexer);
17555       if (token->type != CPP_CLOSE_PAREN)
17556         /* Parse the attribute-list.  */
17557         attribute_list = cp_parser_attribute_list (parser);
17558       else
17559         /* If the next token is a `)', then there is no attribute
17560            list.  */
17561         attribute_list = NULL;
17562
17563       /* Look for the two `)' tokens.  */
17564       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17565       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17566
17567       /* Add these new attributes to the list.  */
17568       attributes = chainon (attributes, attribute_list);
17569     }
17570
17571   return attributes;
17572 }
17573
17574 /* Parse an attribute-list.
17575
17576    attribute-list:
17577      attribute
17578      attribute-list , attribute
17579
17580    attribute:
17581      identifier
17582      identifier ( identifier )
17583      identifier ( identifier , expression-list )
17584      identifier ( expression-list )
17585
17586    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
17587    to an attribute.  The TREE_PURPOSE of each node is the identifier
17588    indicating which attribute is in use.  The TREE_VALUE represents
17589    the arguments, if any.  */
17590
17591 static tree
17592 cp_parser_attribute_list (cp_parser* parser)
17593 {
17594   tree attribute_list = NULL_TREE;
17595   bool save_translate_strings_p = parser->translate_strings_p;
17596
17597   parser->translate_strings_p = false;
17598   while (true)
17599     {
17600       cp_token *token;
17601       tree identifier;
17602       tree attribute;
17603
17604       /* Look for the identifier.  We also allow keywords here; for
17605          example `__attribute__ ((const))' is legal.  */
17606       token = cp_lexer_peek_token (parser->lexer);
17607       if (token->type == CPP_NAME
17608           || token->type == CPP_KEYWORD)
17609         {
17610           tree arguments = NULL_TREE;
17611
17612           /* Consume the token.  */
17613           token = cp_lexer_consume_token (parser->lexer);
17614
17615           /* Save away the identifier that indicates which attribute
17616              this is.  */
17617           identifier = (token->type == CPP_KEYWORD) 
17618             /* For keywords, use the canonical spelling, not the
17619                parsed identifier.  */
17620             ? ridpointers[(int) token->keyword]
17621             : token->u.value;
17622           
17623           attribute = build_tree_list (identifier, NULL_TREE);
17624
17625           /* Peek at the next token.  */
17626           token = cp_lexer_peek_token (parser->lexer);
17627           /* If it's an `(', then parse the attribute arguments.  */
17628           if (token->type == CPP_OPEN_PAREN)
17629             {
17630               VEC(tree,gc) *vec;
17631               vec = cp_parser_parenthesized_expression_list
17632                     (parser, true, /*cast_p=*/false,
17633                      /*allow_expansion_p=*/false,
17634                      /*non_constant_p=*/NULL);
17635               if (vec == NULL)
17636                 arguments = error_mark_node;
17637               else
17638                 {
17639                   arguments = build_tree_list_vec (vec);
17640                   release_tree_vector (vec);
17641                 }
17642               /* Save the arguments away.  */
17643               TREE_VALUE (attribute) = arguments;
17644             }
17645
17646           if (arguments != error_mark_node)
17647             {
17648               /* Add this attribute to the list.  */
17649               TREE_CHAIN (attribute) = attribute_list;
17650               attribute_list = attribute;
17651             }
17652
17653           token = cp_lexer_peek_token (parser->lexer);
17654         }
17655       /* Now, look for more attributes.  If the next token isn't a
17656          `,', we're done.  */
17657       if (token->type != CPP_COMMA)
17658         break;
17659
17660       /* Consume the comma and keep going.  */
17661       cp_lexer_consume_token (parser->lexer);
17662     }
17663   parser->translate_strings_p = save_translate_strings_p;
17664
17665   /* We built up the list in reverse order.  */
17666   return nreverse (attribute_list);
17667 }
17668
17669 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
17670    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
17671    current value of the PEDANTIC flag, regardless of whether or not
17672    the `__extension__' keyword is present.  The caller is responsible
17673    for restoring the value of the PEDANTIC flag.  */
17674
17675 static bool
17676 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17677 {
17678   /* Save the old value of the PEDANTIC flag.  */
17679   *saved_pedantic = pedantic;
17680
17681   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17682     {
17683       /* Consume the `__extension__' token.  */
17684       cp_lexer_consume_token (parser->lexer);
17685       /* We're not being pedantic while the `__extension__' keyword is
17686          in effect.  */
17687       pedantic = 0;
17688
17689       return true;
17690     }
17691
17692   return false;
17693 }
17694
17695 /* Parse a label declaration.
17696
17697    label-declaration:
17698      __label__ label-declarator-seq ;
17699
17700    label-declarator-seq:
17701      identifier , label-declarator-seq
17702      identifier  */
17703
17704 static void
17705 cp_parser_label_declaration (cp_parser* parser)
17706 {
17707   /* Look for the `__label__' keyword.  */
17708   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17709
17710   while (true)
17711     {
17712       tree identifier;
17713
17714       /* Look for an identifier.  */
17715       identifier = cp_parser_identifier (parser);
17716       /* If we failed, stop.  */
17717       if (identifier == error_mark_node)
17718         break;
17719       /* Declare it as a label.  */
17720       finish_label_decl (identifier);
17721       /* If the next token is a `;', stop.  */
17722       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17723         break;
17724       /* Look for the `,' separating the label declarations.  */
17725       cp_parser_require (parser, CPP_COMMA, "%<,%>");
17726     }
17727
17728   /* Look for the final `;'.  */
17729   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17730 }
17731
17732 /* Support Functions */
17733
17734 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17735    NAME should have one of the representations used for an
17736    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17737    is returned.  If PARSER->SCOPE is a dependent type, then a
17738    SCOPE_REF is returned.
17739
17740    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17741    returned; the name was already resolved when the TEMPLATE_ID_EXPR
17742    was formed.  Abstractly, such entities should not be passed to this
17743    function, because they do not need to be looked up, but it is
17744    simpler to check for this special case here, rather than at the
17745    call-sites.
17746
17747    In cases not explicitly covered above, this function returns a
17748    DECL, OVERLOAD, or baselink representing the result of the lookup.
17749    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17750    is returned.
17751
17752    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17753    (e.g., "struct") that was used.  In that case bindings that do not
17754    refer to types are ignored.
17755
17756    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17757    ignored.
17758
17759    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17760    are ignored.
17761
17762    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17763    types.
17764
17765    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17766    TREE_LIST of candidates if name-lookup results in an ambiguity, and
17767    NULL_TREE otherwise.  */
17768
17769 static tree
17770 cp_parser_lookup_name (cp_parser *parser, tree name,
17771                        enum tag_types tag_type,
17772                        bool is_template,
17773                        bool is_namespace,
17774                        bool check_dependency,
17775                        tree *ambiguous_decls,
17776                        location_t name_location)
17777 {
17778   int flags = 0;
17779   tree decl;
17780   tree object_type = parser->context->object_type;
17781
17782   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17783     flags |= LOOKUP_COMPLAIN;
17784
17785   /* Assume that the lookup will be unambiguous.  */
17786   if (ambiguous_decls)
17787     *ambiguous_decls = NULL_TREE;
17788
17789   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17790      no longer valid.  Note that if we are parsing tentatively, and
17791      the parse fails, OBJECT_TYPE will be automatically restored.  */
17792   parser->context->object_type = NULL_TREE;
17793
17794   if (name == error_mark_node)
17795     return error_mark_node;
17796
17797   /* A template-id has already been resolved; there is no lookup to
17798      do.  */
17799   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17800     return name;
17801   if (BASELINK_P (name))
17802     {
17803       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17804                   == TEMPLATE_ID_EXPR);
17805       return name;
17806     }
17807
17808   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17809      it should already have been checked to make sure that the name
17810      used matches the type being destroyed.  */
17811   if (TREE_CODE (name) == BIT_NOT_EXPR)
17812     {
17813       tree type;
17814
17815       /* Figure out to which type this destructor applies.  */
17816       if (parser->scope)
17817         type = parser->scope;
17818       else if (object_type)
17819         type = object_type;
17820       else
17821         type = current_class_type;
17822       /* If that's not a class type, there is no destructor.  */
17823       if (!type || !CLASS_TYPE_P (type))
17824         return error_mark_node;
17825       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17826         lazily_declare_fn (sfk_destructor, type);
17827       if (!CLASSTYPE_DESTRUCTORS (type))
17828           return error_mark_node;
17829       /* If it was a class type, return the destructor.  */
17830       return CLASSTYPE_DESTRUCTORS (type);
17831     }
17832
17833   /* By this point, the NAME should be an ordinary identifier.  If
17834      the id-expression was a qualified name, the qualifying scope is
17835      stored in PARSER->SCOPE at this point.  */
17836   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17837
17838   /* Perform the lookup.  */
17839   if (parser->scope)
17840     {
17841       bool dependent_p;
17842
17843       if (parser->scope == error_mark_node)
17844         return error_mark_node;
17845
17846       /* If the SCOPE is dependent, the lookup must be deferred until
17847          the template is instantiated -- unless we are explicitly
17848          looking up names in uninstantiated templates.  Even then, we
17849          cannot look up the name if the scope is not a class type; it
17850          might, for example, be a template type parameter.  */
17851       dependent_p = (TYPE_P (parser->scope)
17852                      && dependent_scope_p (parser->scope));
17853       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17854           && dependent_p)
17855         /* Defer lookup.  */
17856         decl = error_mark_node;
17857       else
17858         {
17859           tree pushed_scope = NULL_TREE;
17860
17861           /* If PARSER->SCOPE is a dependent type, then it must be a
17862              class type, and we must not be checking dependencies;
17863              otherwise, we would have processed this lookup above.  So
17864              that PARSER->SCOPE is not considered a dependent base by
17865              lookup_member, we must enter the scope here.  */
17866           if (dependent_p)
17867             pushed_scope = push_scope (parser->scope);
17868           /* If the PARSER->SCOPE is a template specialization, it
17869              may be instantiated during name lookup.  In that case,
17870              errors may be issued.  Even if we rollback the current
17871              tentative parse, those errors are valid.  */
17872           decl = lookup_qualified_name (parser->scope, name,
17873                                         tag_type != none_type,
17874                                         /*complain=*/true);
17875
17876           /* If we have a single function from a using decl, pull it out.  */
17877           if (TREE_CODE (decl) == OVERLOAD
17878               && !really_overloaded_fn (decl))
17879             decl = OVL_FUNCTION (decl);
17880
17881           if (pushed_scope)
17882             pop_scope (pushed_scope);
17883         }
17884
17885       /* If the scope is a dependent type and either we deferred lookup or
17886          we did lookup but didn't find the name, rememeber the name.  */
17887       if (decl == error_mark_node && TYPE_P (parser->scope)
17888           && dependent_type_p (parser->scope))
17889         {
17890           if (tag_type)
17891             {
17892               tree type;
17893
17894               /* The resolution to Core Issue 180 says that `struct
17895                  A::B' should be considered a type-name, even if `A'
17896                  is dependent.  */
17897               type = make_typename_type (parser->scope, name, tag_type,
17898                                          /*complain=*/tf_error);
17899               decl = TYPE_NAME (type);
17900             }
17901           else if (is_template
17902                    && (cp_parser_next_token_ends_template_argument_p (parser)
17903                        || cp_lexer_next_token_is (parser->lexer,
17904                                                   CPP_CLOSE_PAREN)))
17905             decl = make_unbound_class_template (parser->scope,
17906                                                 name, NULL_TREE,
17907                                                 /*complain=*/tf_error);
17908           else
17909             decl = build_qualified_name (/*type=*/NULL_TREE,
17910                                          parser->scope, name,
17911                                          is_template);
17912         }
17913       parser->qualifying_scope = parser->scope;
17914       parser->object_scope = NULL_TREE;
17915     }
17916   else if (object_type)
17917     {
17918       tree object_decl = NULL_TREE;
17919       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17920          OBJECT_TYPE is not a class.  */
17921       if (CLASS_TYPE_P (object_type))
17922         /* If the OBJECT_TYPE is a template specialization, it may
17923            be instantiated during name lookup.  In that case, errors
17924            may be issued.  Even if we rollback the current tentative
17925            parse, those errors are valid.  */
17926         object_decl = lookup_member (object_type,
17927                                      name,
17928                                      /*protect=*/0,
17929                                      tag_type != none_type);
17930       /* Look it up in the enclosing context, too.  */
17931       decl = lookup_name_real (name, tag_type != none_type,
17932                                /*nonclass=*/0,
17933                                /*block_p=*/true, is_namespace, flags);
17934       parser->object_scope = object_type;
17935       parser->qualifying_scope = NULL_TREE;
17936       if (object_decl)
17937         decl = object_decl;
17938     }
17939   else
17940     {
17941       decl = lookup_name_real (name, tag_type != none_type,
17942                                /*nonclass=*/0,
17943                                /*block_p=*/true, is_namespace, flags);
17944       parser->qualifying_scope = NULL_TREE;
17945       parser->object_scope = NULL_TREE;
17946     }
17947
17948   /* If the lookup failed, let our caller know.  */
17949   if (!decl || decl == error_mark_node)
17950     return error_mark_node;
17951
17952   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
17953   if (TREE_CODE (decl) == TREE_LIST)
17954     {
17955       if (ambiguous_decls)
17956         *ambiguous_decls = decl;
17957       /* The error message we have to print is too complicated for
17958          cp_parser_error, so we incorporate its actions directly.  */
17959       if (!cp_parser_simulate_error (parser))
17960         {
17961           error_at (name_location, "reference to %qD is ambiguous",
17962                     name);
17963           print_candidates (decl);
17964         }
17965       return error_mark_node;
17966     }
17967
17968   gcc_assert (DECL_P (decl)
17969               || TREE_CODE (decl) == OVERLOAD
17970               || TREE_CODE (decl) == SCOPE_REF
17971               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17972               || BASELINK_P (decl));
17973
17974   /* If we have resolved the name of a member declaration, check to
17975      see if the declaration is accessible.  When the name resolves to
17976      set of overloaded functions, accessibility is checked when
17977      overload resolution is done.
17978
17979      During an explicit instantiation, access is not checked at all,
17980      as per [temp.explicit].  */
17981   if (DECL_P (decl))
17982     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17983
17984   return decl;
17985 }
17986
17987 /* Like cp_parser_lookup_name, but for use in the typical case where
17988    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17989    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
17990
17991 static tree
17992 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17993 {
17994   return cp_parser_lookup_name (parser, name,
17995                                 none_type,
17996                                 /*is_template=*/false,
17997                                 /*is_namespace=*/false,
17998                                 /*check_dependency=*/true,
17999                                 /*ambiguous_decls=*/NULL,
18000                                 location);
18001 }
18002
18003 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18004    the current context, return the TYPE_DECL.  If TAG_NAME_P is
18005    true, the DECL indicates the class being defined in a class-head,
18006    or declared in an elaborated-type-specifier.
18007
18008    Otherwise, return DECL.  */
18009
18010 static tree
18011 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18012 {
18013   /* If the TEMPLATE_DECL is being declared as part of a class-head,
18014      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18015
18016        struct A {
18017          template <typename T> struct B;
18018        };
18019
18020        template <typename T> struct A::B {};
18021
18022      Similarly, in an elaborated-type-specifier:
18023
18024        namespace N { struct X{}; }
18025
18026        struct A {
18027          template <typename T> friend struct N::X;
18028        };
18029
18030      However, if the DECL refers to a class type, and we are in
18031      the scope of the class, then the name lookup automatically
18032      finds the TYPE_DECL created by build_self_reference rather
18033      than a TEMPLATE_DECL.  For example, in:
18034
18035        template <class T> struct S {
18036          S s;
18037        };
18038
18039      there is no need to handle such case.  */
18040
18041   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18042     return DECL_TEMPLATE_RESULT (decl);
18043
18044   return decl;
18045 }
18046
18047 /* If too many, or too few, template-parameter lists apply to the
18048    declarator, issue an error message.  Returns TRUE if all went well,
18049    and FALSE otherwise.  */
18050
18051 static bool
18052 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18053                                                 cp_declarator *declarator,
18054                                                 location_t declarator_location)
18055 {
18056   unsigned num_templates;
18057
18058   /* We haven't seen any classes that involve template parameters yet.  */
18059   num_templates = 0;
18060
18061   switch (declarator->kind)
18062     {
18063     case cdk_id:
18064       if (declarator->u.id.qualifying_scope)
18065         {
18066           tree scope;
18067           tree member;
18068
18069           scope = declarator->u.id.qualifying_scope;
18070           member = declarator->u.id.unqualified_name;
18071
18072           while (scope && CLASS_TYPE_P (scope))
18073             {
18074               /* You're supposed to have one `template <...>'
18075                  for every template class, but you don't need one
18076                  for a full specialization.  For example:
18077
18078                  template <class T> struct S{};
18079                  template <> struct S<int> { void f(); };
18080                  void S<int>::f () {}
18081
18082                  is correct; there shouldn't be a `template <>' for
18083                  the definition of `S<int>::f'.  */
18084               if (!CLASSTYPE_TEMPLATE_INFO (scope))
18085                 /* If SCOPE does not have template information of any
18086                    kind, then it is not a template, nor is it nested
18087                    within a template.  */
18088                 break;
18089               if (explicit_class_specialization_p (scope))
18090                 break;
18091               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18092                 ++num_templates;
18093
18094               scope = TYPE_CONTEXT (scope);
18095             }
18096         }
18097       else if (TREE_CODE (declarator->u.id.unqualified_name)
18098                == TEMPLATE_ID_EXPR)
18099         /* If the DECLARATOR has the form `X<y>' then it uses one
18100            additional level of template parameters.  */
18101         ++num_templates;
18102
18103       return cp_parser_check_template_parameters 
18104         (parser, num_templates, declarator_location, declarator);
18105
18106
18107     case cdk_function:
18108     case cdk_array:
18109     case cdk_pointer:
18110     case cdk_reference:
18111     case cdk_ptrmem:
18112       return (cp_parser_check_declarator_template_parameters
18113               (parser, declarator->declarator, declarator_location));
18114
18115     case cdk_error:
18116       return true;
18117
18118     default:
18119       gcc_unreachable ();
18120     }
18121   return false;
18122 }
18123
18124 /* NUM_TEMPLATES were used in the current declaration.  If that is
18125    invalid, return FALSE and issue an error messages.  Otherwise,
18126    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
18127    declarator and we can print more accurate diagnostics.  */
18128
18129 static bool
18130 cp_parser_check_template_parameters (cp_parser* parser,
18131                                      unsigned num_templates,
18132                                      location_t location,
18133                                      cp_declarator *declarator)
18134 {
18135   /* If there are the same number of template classes and parameter
18136      lists, that's OK.  */
18137   if (parser->num_template_parameter_lists == num_templates)
18138     return true;
18139   /* If there are more, but only one more, then we are referring to a
18140      member template.  That's OK too.  */
18141   if (parser->num_template_parameter_lists == num_templates + 1)
18142     return true;
18143   /* If there are more template classes than parameter lists, we have
18144      something like:
18145
18146        template <class T> void S<T>::R<T>::f ();  */
18147   if (parser->num_template_parameter_lists < num_templates)
18148     {
18149       if (declarator)
18150         error_at (location, "specializing member %<%T::%E%> "
18151                   "requires %<template<>%> syntax", 
18152                   declarator->u.id.qualifying_scope,
18153                   declarator->u.id.unqualified_name);
18154       else 
18155         error_at (location, "too few template-parameter-lists");
18156       return false;
18157     }
18158   /* Otherwise, there are too many template parameter lists.  We have
18159      something like:
18160
18161      template <class T> template <class U> void S::f();  */
18162   error_at (location, "too many template-parameter-lists");
18163   return false;
18164 }
18165
18166 /* Parse an optional `::' token indicating that the following name is
18167    from the global namespace.  If so, PARSER->SCOPE is set to the
18168    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18169    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18170    Returns the new value of PARSER->SCOPE, if the `::' token is
18171    present, and NULL_TREE otherwise.  */
18172
18173 static tree
18174 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18175 {
18176   cp_token *token;
18177
18178   /* Peek at the next token.  */
18179   token = cp_lexer_peek_token (parser->lexer);
18180   /* If we're looking at a `::' token then we're starting from the
18181      global namespace, not our current location.  */
18182   if (token->type == CPP_SCOPE)
18183     {
18184       /* Consume the `::' token.  */
18185       cp_lexer_consume_token (parser->lexer);
18186       /* Set the SCOPE so that we know where to start the lookup.  */
18187       parser->scope = global_namespace;
18188       parser->qualifying_scope = global_namespace;
18189       parser->object_scope = NULL_TREE;
18190
18191       return parser->scope;
18192     }
18193   else if (!current_scope_valid_p)
18194     {
18195       parser->scope = NULL_TREE;
18196       parser->qualifying_scope = NULL_TREE;
18197       parser->object_scope = NULL_TREE;
18198     }
18199
18200   return NULL_TREE;
18201 }
18202
18203 /* Returns TRUE if the upcoming token sequence is the start of a
18204    constructor declarator.  If FRIEND_P is true, the declarator is
18205    preceded by the `friend' specifier.  */
18206
18207 static bool
18208 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18209 {
18210   bool constructor_p;
18211   tree type_decl = NULL_TREE;
18212   bool nested_name_p;
18213   cp_token *next_token;
18214
18215   /* The common case is that this is not a constructor declarator, so
18216      try to avoid doing lots of work if at all possible.  It's not
18217      valid declare a constructor at function scope.  */
18218   if (parser->in_function_body)
18219     return false;
18220   /* And only certain tokens can begin a constructor declarator.  */
18221   next_token = cp_lexer_peek_token (parser->lexer);
18222   if (next_token->type != CPP_NAME
18223       && next_token->type != CPP_SCOPE
18224       && next_token->type != CPP_NESTED_NAME_SPECIFIER
18225       && next_token->type != CPP_TEMPLATE_ID)
18226     return false;
18227
18228   /* Parse tentatively; we are going to roll back all of the tokens
18229      consumed here.  */
18230   cp_parser_parse_tentatively (parser);
18231   /* Assume that we are looking at a constructor declarator.  */
18232   constructor_p = true;
18233
18234   /* Look for the optional `::' operator.  */
18235   cp_parser_global_scope_opt (parser,
18236                               /*current_scope_valid_p=*/false);
18237   /* Look for the nested-name-specifier.  */
18238   nested_name_p
18239     = (cp_parser_nested_name_specifier_opt (parser,
18240                                             /*typename_keyword_p=*/false,
18241                                             /*check_dependency_p=*/false,
18242                                             /*type_p=*/false,
18243                                             /*is_declaration=*/false)
18244        != NULL_TREE);
18245   /* Outside of a class-specifier, there must be a
18246      nested-name-specifier.  */
18247   if (!nested_name_p &&
18248       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18249        || friend_p))
18250     constructor_p = false;
18251   /* If we still think that this might be a constructor-declarator,
18252      look for a class-name.  */
18253   if (constructor_p)
18254     {
18255       /* If we have:
18256
18257            template <typename T> struct S { S(); };
18258            template <typename T> S<T>::S ();
18259
18260          we must recognize that the nested `S' names a class.
18261          Similarly, for:
18262
18263            template <typename T> S<T>::S<T> ();
18264
18265          we must recognize that the nested `S' names a template.  */
18266       type_decl = cp_parser_class_name (parser,
18267                                         /*typename_keyword_p=*/false,
18268                                         /*template_keyword_p=*/false,
18269                                         none_type,
18270                                         /*check_dependency_p=*/false,
18271                                         /*class_head_p=*/false,
18272                                         /*is_declaration=*/false);
18273       /* If there was no class-name, then this is not a constructor.  */
18274       constructor_p = !cp_parser_error_occurred (parser);
18275     }
18276
18277   /* If we're still considering a constructor, we have to see a `(',
18278      to begin the parameter-declaration-clause, followed by either a
18279      `)', an `...', or a decl-specifier.  We need to check for a
18280      type-specifier to avoid being fooled into thinking that:
18281
18282        S::S (f) (int);
18283
18284      is a constructor.  (It is actually a function named `f' that
18285      takes one parameter (of type `int') and returns a value of type
18286      `S::S'.  */
18287   if (constructor_p
18288       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18289     {
18290       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18291           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18292           /* A parameter declaration begins with a decl-specifier,
18293              which is either the "attribute" keyword, a storage class
18294              specifier, or (usually) a type-specifier.  */
18295           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18296         {
18297           tree type;
18298           tree pushed_scope = NULL_TREE;
18299           unsigned saved_num_template_parameter_lists;
18300
18301           /* Names appearing in the type-specifier should be looked up
18302              in the scope of the class.  */
18303           if (current_class_type)
18304             type = NULL_TREE;
18305           else
18306             {
18307               type = TREE_TYPE (type_decl);
18308               if (TREE_CODE (type) == TYPENAME_TYPE)
18309                 {
18310                   type = resolve_typename_type (type,
18311                                                 /*only_current_p=*/false);
18312                   if (TREE_CODE (type) == TYPENAME_TYPE)
18313                     {
18314                       cp_parser_abort_tentative_parse (parser);
18315                       return false;
18316                     }
18317                 }
18318               pushed_scope = push_scope (type);
18319             }
18320
18321           /* Inside the constructor parameter list, surrounding
18322              template-parameter-lists do not apply.  */
18323           saved_num_template_parameter_lists
18324             = parser->num_template_parameter_lists;
18325           parser->num_template_parameter_lists = 0;
18326
18327           /* Look for the type-specifier.  */
18328           cp_parser_type_specifier (parser,
18329                                     CP_PARSER_FLAGS_NONE,
18330                                     /*decl_specs=*/NULL,
18331                                     /*is_declarator=*/true,
18332                                     /*declares_class_or_enum=*/NULL,
18333                                     /*is_cv_qualifier=*/NULL);
18334
18335           parser->num_template_parameter_lists
18336             = saved_num_template_parameter_lists;
18337
18338           /* Leave the scope of the class.  */
18339           if (pushed_scope)
18340             pop_scope (pushed_scope);
18341
18342           constructor_p = !cp_parser_error_occurred (parser);
18343         }
18344     }
18345   else
18346     constructor_p = false;
18347   /* We did not really want to consume any tokens.  */
18348   cp_parser_abort_tentative_parse (parser);
18349
18350   return constructor_p;
18351 }
18352
18353 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18354    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
18355    they must be performed once we are in the scope of the function.
18356
18357    Returns the function defined.  */
18358
18359 static tree
18360 cp_parser_function_definition_from_specifiers_and_declarator
18361   (cp_parser* parser,
18362    cp_decl_specifier_seq *decl_specifiers,
18363    tree attributes,
18364    const cp_declarator *declarator)
18365 {
18366   tree fn;
18367   bool success_p;
18368
18369   /* Begin the function-definition.  */
18370   success_p = start_function (decl_specifiers, declarator, attributes);
18371
18372   /* The things we're about to see are not directly qualified by any
18373      template headers we've seen thus far.  */
18374   reset_specialization ();
18375
18376   /* If there were names looked up in the decl-specifier-seq that we
18377      did not check, check them now.  We must wait until we are in the
18378      scope of the function to perform the checks, since the function
18379      might be a friend.  */
18380   perform_deferred_access_checks ();
18381
18382   if (!success_p)
18383     {
18384       /* Skip the entire function.  */
18385       cp_parser_skip_to_end_of_block_or_statement (parser);
18386       fn = error_mark_node;
18387     }
18388   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18389     {
18390       /* Seen already, skip it.  An error message has already been output.  */
18391       cp_parser_skip_to_end_of_block_or_statement (parser);
18392       fn = current_function_decl;
18393       current_function_decl = NULL_TREE;
18394       /* If this is a function from a class, pop the nested class.  */
18395       if (current_class_name)
18396         pop_nested_class ();
18397     }
18398   else
18399     fn = cp_parser_function_definition_after_declarator (parser,
18400                                                          /*inline_p=*/false);
18401
18402   return fn;
18403 }
18404
18405 /* Parse the part of a function-definition that follows the
18406    declarator.  INLINE_P is TRUE iff this function is an inline
18407    function defined within a class-specifier.
18408
18409    Returns the function defined.  */
18410
18411 static tree
18412 cp_parser_function_definition_after_declarator (cp_parser* parser,
18413                                                 bool inline_p)
18414 {
18415   tree fn;
18416   bool ctor_initializer_p = false;
18417   bool saved_in_unbraced_linkage_specification_p;
18418   bool saved_in_function_body;
18419   unsigned saved_num_template_parameter_lists;
18420   cp_token *token;
18421
18422   saved_in_function_body = parser->in_function_body;
18423   parser->in_function_body = true;
18424   /* If the next token is `return', then the code may be trying to
18425      make use of the "named return value" extension that G++ used to
18426      support.  */
18427   token = cp_lexer_peek_token (parser->lexer);
18428   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18429     {
18430       /* Consume the `return' keyword.  */
18431       cp_lexer_consume_token (parser->lexer);
18432       /* Look for the identifier that indicates what value is to be
18433          returned.  */
18434       cp_parser_identifier (parser);
18435       /* Issue an error message.  */
18436       error_at (token->location,
18437                 "named return values are no longer supported");
18438       /* Skip tokens until we reach the start of the function body.  */
18439       while (true)
18440         {
18441           cp_token *token = cp_lexer_peek_token (parser->lexer);
18442           if (token->type == CPP_OPEN_BRACE
18443               || token->type == CPP_EOF
18444               || token->type == CPP_PRAGMA_EOL)
18445             break;
18446           cp_lexer_consume_token (parser->lexer);
18447         }
18448     }
18449   /* The `extern' in `extern "C" void f () { ... }' does not apply to
18450      anything declared inside `f'.  */
18451   saved_in_unbraced_linkage_specification_p
18452     = parser->in_unbraced_linkage_specification_p;
18453   parser->in_unbraced_linkage_specification_p = false;
18454   /* Inside the function, surrounding template-parameter-lists do not
18455      apply.  */
18456   saved_num_template_parameter_lists
18457     = parser->num_template_parameter_lists;
18458   parser->num_template_parameter_lists = 0;
18459
18460   start_lambda_scope (current_function_decl);
18461
18462   /* If the next token is `try', then we are looking at a
18463      function-try-block.  */
18464   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18465     ctor_initializer_p = cp_parser_function_try_block (parser);
18466   /* A function-try-block includes the function-body, so we only do
18467      this next part if we're not processing a function-try-block.  */
18468   else
18469     ctor_initializer_p
18470       = cp_parser_ctor_initializer_opt_and_function_body (parser);
18471
18472   finish_lambda_scope ();
18473
18474   /* Finish the function.  */
18475   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18476                         (inline_p ? 2 : 0));
18477   /* Generate code for it, if necessary.  */
18478   expand_or_defer_fn (fn);
18479   /* Restore the saved values.  */
18480   parser->in_unbraced_linkage_specification_p
18481     = saved_in_unbraced_linkage_specification_p;
18482   parser->num_template_parameter_lists
18483     = saved_num_template_parameter_lists;
18484   parser->in_function_body = saved_in_function_body;
18485
18486   return fn;
18487 }
18488
18489 /* Parse a template-declaration, assuming that the `export' (and
18490    `extern') keywords, if present, has already been scanned.  MEMBER_P
18491    is as for cp_parser_template_declaration.  */
18492
18493 static void
18494 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18495 {
18496   tree decl = NULL_TREE;
18497   VEC (deferred_access_check,gc) *checks;
18498   tree parameter_list;
18499   bool friend_p = false;
18500   bool need_lang_pop;
18501   cp_token *token;
18502
18503   /* Look for the `template' keyword.  */
18504   token = cp_lexer_peek_token (parser->lexer);
18505   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18506     return;
18507
18508   /* And the `<'.  */
18509   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18510     return;
18511   if (at_class_scope_p () && current_function_decl)
18512     {
18513       /* 14.5.2.2 [temp.mem]
18514
18515          A local class shall not have member templates.  */
18516       error_at (token->location,
18517                 "invalid declaration of member template in local class");
18518       cp_parser_skip_to_end_of_block_or_statement (parser);
18519       return;
18520     }
18521   /* [temp]
18522
18523      A template ... shall not have C linkage.  */
18524   if (current_lang_name == lang_name_c)
18525     {
18526       error_at (token->location, "template with C linkage");
18527       /* Give it C++ linkage to avoid confusing other parts of the
18528          front end.  */
18529       push_lang_context (lang_name_cplusplus);
18530       need_lang_pop = true;
18531     }
18532   else
18533     need_lang_pop = false;
18534
18535   /* We cannot perform access checks on the template parameter
18536      declarations until we know what is being declared, just as we
18537      cannot check the decl-specifier list.  */
18538   push_deferring_access_checks (dk_deferred);
18539
18540   /* If the next token is `>', then we have an invalid
18541      specialization.  Rather than complain about an invalid template
18542      parameter, issue an error message here.  */
18543   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18544     {
18545       cp_parser_error (parser, "invalid explicit specialization");
18546       begin_specialization ();
18547       parameter_list = NULL_TREE;
18548     }
18549   else
18550     /* Parse the template parameters.  */
18551     parameter_list = cp_parser_template_parameter_list (parser);
18552
18553   /* Get the deferred access checks from the parameter list.  These
18554      will be checked once we know what is being declared, as for a
18555      member template the checks must be performed in the scope of the
18556      class containing the member.  */
18557   checks = get_deferred_access_checks ();
18558
18559   /* Look for the `>'.  */
18560   cp_parser_skip_to_end_of_template_parameter_list (parser);
18561   /* We just processed one more parameter list.  */
18562   ++parser->num_template_parameter_lists;
18563   /* If the next token is `template', there are more template
18564      parameters.  */
18565   if (cp_lexer_next_token_is_keyword (parser->lexer,
18566                                       RID_TEMPLATE))
18567     cp_parser_template_declaration_after_export (parser, member_p);
18568   else
18569     {
18570       /* There are no access checks when parsing a template, as we do not
18571          know if a specialization will be a friend.  */
18572       push_deferring_access_checks (dk_no_check);
18573       token = cp_lexer_peek_token (parser->lexer);
18574       decl = cp_parser_single_declaration (parser,
18575                                            checks,
18576                                            member_p,
18577                                            /*explicit_specialization_p=*/false,
18578                                            &friend_p);
18579       pop_deferring_access_checks ();
18580
18581       /* If this is a member template declaration, let the front
18582          end know.  */
18583       if (member_p && !friend_p && decl)
18584         {
18585           if (TREE_CODE (decl) == TYPE_DECL)
18586             cp_parser_check_access_in_redeclaration (decl, token->location);
18587
18588           decl = finish_member_template_decl (decl);
18589         }
18590       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18591         make_friend_class (current_class_type, TREE_TYPE (decl),
18592                            /*complain=*/true);
18593     }
18594   /* We are done with the current parameter list.  */
18595   --parser->num_template_parameter_lists;
18596
18597   pop_deferring_access_checks ();
18598
18599   /* Finish up.  */
18600   finish_template_decl (parameter_list);
18601
18602   /* Register member declarations.  */
18603   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18604     finish_member_declaration (decl);
18605   /* For the erroneous case of a template with C linkage, we pushed an
18606      implicit C++ linkage scope; exit that scope now.  */
18607   if (need_lang_pop)
18608     pop_lang_context ();
18609   /* If DECL is a function template, we must return to parse it later.
18610      (Even though there is no definition, there might be default
18611      arguments that need handling.)  */
18612   if (member_p && decl
18613       && (TREE_CODE (decl) == FUNCTION_DECL
18614           || DECL_FUNCTION_TEMPLATE_P (decl)))
18615     TREE_VALUE (parser->unparsed_functions_queues)
18616       = tree_cons (NULL_TREE, decl,
18617                    TREE_VALUE (parser->unparsed_functions_queues));
18618 }
18619
18620 /* Perform the deferred access checks from a template-parameter-list.
18621    CHECKS is a TREE_LIST of access checks, as returned by
18622    get_deferred_access_checks.  */
18623
18624 static void
18625 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18626 {
18627   ++processing_template_parmlist;
18628   perform_access_checks (checks);
18629   --processing_template_parmlist;
18630 }
18631
18632 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18633    `function-definition' sequence.  MEMBER_P is true, this declaration
18634    appears in a class scope.
18635
18636    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
18637    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
18638
18639 static tree
18640 cp_parser_single_declaration (cp_parser* parser,
18641                               VEC (deferred_access_check,gc)* checks,
18642                               bool member_p,
18643                               bool explicit_specialization_p,
18644                               bool* friend_p)
18645 {
18646   int declares_class_or_enum;
18647   tree decl = NULL_TREE;
18648   cp_decl_specifier_seq decl_specifiers;
18649   bool function_definition_p = false;
18650   cp_token *decl_spec_token_start;
18651
18652   /* This function is only used when processing a template
18653      declaration.  */
18654   gcc_assert (innermost_scope_kind () == sk_template_parms
18655               || innermost_scope_kind () == sk_template_spec);
18656
18657   /* Defer access checks until we know what is being declared.  */
18658   push_deferring_access_checks (dk_deferred);
18659
18660   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18661      alternative.  */
18662   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18663   cp_parser_decl_specifier_seq (parser,
18664                                 CP_PARSER_FLAGS_OPTIONAL,
18665                                 &decl_specifiers,
18666                                 &declares_class_or_enum);
18667   if (friend_p)
18668     *friend_p = cp_parser_friend_p (&decl_specifiers);
18669
18670   /* There are no template typedefs.  */
18671   if (decl_specifiers.specs[(int) ds_typedef])
18672     {
18673       error_at (decl_spec_token_start->location,
18674                 "template declaration of %<typedef%>");
18675       decl = error_mark_node;
18676     }
18677
18678   /* Gather up the access checks that occurred the
18679      decl-specifier-seq.  */
18680   stop_deferring_access_checks ();
18681
18682   /* Check for the declaration of a template class.  */
18683   if (declares_class_or_enum)
18684     {
18685       if (cp_parser_declares_only_class_p (parser))
18686         {
18687           decl = shadow_tag (&decl_specifiers);
18688
18689           /* In this case:
18690
18691                struct C {
18692                  friend template <typename T> struct A<T>::B;
18693                };
18694
18695              A<T>::B will be represented by a TYPENAME_TYPE, and
18696              therefore not recognized by shadow_tag.  */
18697           if (friend_p && *friend_p
18698               && !decl
18699               && decl_specifiers.type
18700               && TYPE_P (decl_specifiers.type))
18701             decl = decl_specifiers.type;
18702
18703           if (decl && decl != error_mark_node)
18704             decl = TYPE_NAME (decl);
18705           else
18706             decl = error_mark_node;
18707
18708           /* Perform access checks for template parameters.  */
18709           cp_parser_perform_template_parameter_access_checks (checks);
18710         }
18711     }
18712   /* If it's not a template class, try for a template function.  If
18713      the next token is a `;', then this declaration does not declare
18714      anything.  But, if there were errors in the decl-specifiers, then
18715      the error might well have come from an attempted class-specifier.
18716      In that case, there's no need to warn about a missing declarator.  */
18717   if (!decl
18718       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18719           || decl_specifiers.type != error_mark_node))
18720     {
18721       decl = cp_parser_init_declarator (parser,
18722                                         &decl_specifiers,
18723                                         checks,
18724                                         /*function_definition_allowed_p=*/true,
18725                                         member_p,
18726                                         declares_class_or_enum,
18727                                         &function_definition_p);
18728
18729     /* 7.1.1-1 [dcl.stc]
18730
18731        A storage-class-specifier shall not be specified in an explicit
18732        specialization...  */
18733     if (decl
18734         && explicit_specialization_p
18735         && decl_specifiers.storage_class != sc_none)
18736       {
18737         error_at (decl_spec_token_start->location,
18738                   "explicit template specialization cannot have a storage class");
18739         decl = error_mark_node;
18740       }
18741     }
18742
18743   pop_deferring_access_checks ();
18744
18745   /* Clear any current qualification; whatever comes next is the start
18746      of something new.  */
18747   parser->scope = NULL_TREE;
18748   parser->qualifying_scope = NULL_TREE;
18749   parser->object_scope = NULL_TREE;
18750   /* Look for a trailing `;' after the declaration.  */
18751   if (!function_definition_p
18752       && (decl == error_mark_node
18753           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18754     cp_parser_skip_to_end_of_block_or_statement (parser);
18755
18756   return decl;
18757 }
18758
18759 /* Parse a cast-expression that is not the operand of a unary "&".  */
18760
18761 static tree
18762 cp_parser_simple_cast_expression (cp_parser *parser)
18763 {
18764   return cp_parser_cast_expression (parser, /*address_p=*/false,
18765                                     /*cast_p=*/false, NULL);
18766 }
18767
18768 /* Parse a functional cast to TYPE.  Returns an expression
18769    representing the cast.  */
18770
18771 static tree
18772 cp_parser_functional_cast (cp_parser* parser, tree type)
18773 {
18774   VEC(tree,gc) *vec;
18775   tree expression_list;
18776   tree cast;
18777   bool nonconst_p;
18778
18779   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18780     {
18781       maybe_warn_cpp0x ("extended initializer lists");
18782       expression_list = cp_parser_braced_list (parser, &nonconst_p);
18783       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18784       if (TREE_CODE (type) == TYPE_DECL)
18785         type = TREE_TYPE (type);
18786       return finish_compound_literal (type, expression_list);
18787     }
18788
18789
18790   vec = cp_parser_parenthesized_expression_list (parser, false,
18791                                                  /*cast_p=*/true,
18792                                                  /*allow_expansion_p=*/true,
18793                                                  /*non_constant_p=*/NULL);
18794   if (vec == NULL)
18795     expression_list = error_mark_node;
18796   else
18797     {
18798       expression_list = build_tree_list_vec (vec);
18799       release_tree_vector (vec);
18800     }
18801
18802   cast = build_functional_cast (type, expression_list,
18803                                 tf_warning_or_error);
18804   /* [expr.const]/1: In an integral constant expression "only type
18805      conversions to integral or enumeration type can be used".  */
18806   if (TREE_CODE (type) == TYPE_DECL)
18807     type = TREE_TYPE (type);
18808   if (cast != error_mark_node
18809       && !cast_valid_in_integral_constant_expression_p (type)
18810       && (cp_parser_non_integral_constant_expression
18811           (parser, "a call to a constructor")))
18812     return error_mark_node;
18813   return cast;
18814 }
18815
18816 /* Save the tokens that make up the body of a member function defined
18817    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18818    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18819    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18820    for the member function.  */
18821
18822 static tree
18823 cp_parser_save_member_function_body (cp_parser* parser,
18824                                      cp_decl_specifier_seq *decl_specifiers,
18825                                      cp_declarator *declarator,
18826                                      tree attributes)
18827 {
18828   cp_token *first;
18829   cp_token *last;
18830   tree fn;
18831
18832   /* Create the FUNCTION_DECL.  */
18833   fn = grokmethod (decl_specifiers, declarator, attributes);
18834   /* If something went badly wrong, bail out now.  */
18835   if (fn == error_mark_node)
18836     {
18837       /* If there's a function-body, skip it.  */
18838       if (cp_parser_token_starts_function_definition_p
18839           (cp_lexer_peek_token (parser->lexer)))
18840         cp_parser_skip_to_end_of_block_or_statement (parser);
18841       return error_mark_node;
18842     }
18843
18844   /* Remember it, if there default args to post process.  */
18845   cp_parser_save_default_args (parser, fn);
18846
18847   /* Save away the tokens that make up the body of the
18848      function.  */
18849   first = parser->lexer->next_token;
18850   /* We can have braced-init-list mem-initializers before the fn body.  */
18851   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18852     {
18853       cp_lexer_consume_token (parser->lexer);
18854       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18855              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18856         {
18857           /* cache_group will stop after an un-nested { } pair, too.  */
18858           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18859             break;
18860
18861           /* variadic mem-inits have ... after the ')'.  */
18862           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18863             cp_lexer_consume_token (parser->lexer);
18864         }
18865     }
18866   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18867   /* Handle function try blocks.  */
18868   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18869     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18870   last = parser->lexer->next_token;
18871
18872   /* Save away the inline definition; we will process it when the
18873      class is complete.  */
18874   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18875   DECL_PENDING_INLINE_P (fn) = 1;
18876
18877   /* We need to know that this was defined in the class, so that
18878      friend templates are handled correctly.  */
18879   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18880
18881   /* Add FN to the queue of functions to be parsed later.  */
18882   TREE_VALUE (parser->unparsed_functions_queues)
18883     = tree_cons (NULL_TREE, fn,
18884                  TREE_VALUE (parser->unparsed_functions_queues));
18885
18886   return fn;
18887 }
18888
18889 /* Parse a template-argument-list, as well as the trailing ">" (but
18890    not the opening ">").  See cp_parser_template_argument_list for the
18891    return value.  */
18892
18893 static tree
18894 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18895 {
18896   tree arguments;
18897   tree saved_scope;
18898   tree saved_qualifying_scope;
18899   tree saved_object_scope;
18900   bool saved_greater_than_is_operator_p;
18901   int saved_unevaluated_operand;
18902   int saved_inhibit_evaluation_warnings;
18903
18904   /* [temp.names]
18905
18906      When parsing a template-id, the first non-nested `>' is taken as
18907      the end of the template-argument-list rather than a greater-than
18908      operator.  */
18909   saved_greater_than_is_operator_p
18910     = parser->greater_than_is_operator_p;
18911   parser->greater_than_is_operator_p = false;
18912   /* Parsing the argument list may modify SCOPE, so we save it
18913      here.  */
18914   saved_scope = parser->scope;
18915   saved_qualifying_scope = parser->qualifying_scope;
18916   saved_object_scope = parser->object_scope;
18917   /* We need to evaluate the template arguments, even though this
18918      template-id may be nested within a "sizeof".  */
18919   saved_unevaluated_operand = cp_unevaluated_operand;
18920   cp_unevaluated_operand = 0;
18921   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
18922   c_inhibit_evaluation_warnings = 0;
18923   /* Parse the template-argument-list itself.  */
18924   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18925       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18926     arguments = NULL_TREE;
18927   else
18928     arguments = cp_parser_template_argument_list (parser);
18929   /* Look for the `>' that ends the template-argument-list. If we find
18930      a '>>' instead, it's probably just a typo.  */
18931   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18932     {
18933       if (cxx_dialect != cxx98)
18934         {
18935           /* In C++0x, a `>>' in a template argument list or cast
18936              expression is considered to be two separate `>'
18937              tokens. So, change the current token to a `>', but don't
18938              consume it: it will be consumed later when the outer
18939              template argument list (or cast expression) is parsed.
18940              Note that this replacement of `>' for `>>' is necessary
18941              even if we are parsing tentatively: in the tentative
18942              case, after calling
18943              cp_parser_enclosed_template_argument_list we will always
18944              throw away all of the template arguments and the first
18945              closing `>', either because the template argument list
18946              was erroneous or because we are replacing those tokens
18947              with a CPP_TEMPLATE_ID token.  The second `>' (which will
18948              not have been thrown away) is needed either to close an
18949              outer template argument list or to complete a new-style
18950              cast.  */
18951           cp_token *token = cp_lexer_peek_token (parser->lexer);
18952           token->type = CPP_GREATER;
18953         }
18954       else if (!saved_greater_than_is_operator_p)
18955         {
18956           /* If we're in a nested template argument list, the '>>' has
18957             to be a typo for '> >'. We emit the error message, but we
18958             continue parsing and we push a '>' as next token, so that
18959             the argument list will be parsed correctly.  Note that the
18960             global source location is still on the token before the
18961             '>>', so we need to say explicitly where we want it.  */
18962           cp_token *token = cp_lexer_peek_token (parser->lexer);
18963           error_at (token->location, "%<>>%> should be %<> >%> "
18964                     "within a nested template argument list");
18965
18966           token->type = CPP_GREATER;
18967         }
18968       else
18969         {
18970           /* If this is not a nested template argument list, the '>>'
18971             is a typo for '>'. Emit an error message and continue.
18972             Same deal about the token location, but here we can get it
18973             right by consuming the '>>' before issuing the diagnostic.  */
18974           cp_token *token = cp_lexer_consume_token (parser->lexer);
18975           error_at (token->location,
18976                     "spurious %<>>%>, use %<>%> to terminate "
18977                     "a template argument list");
18978         }
18979     }
18980   else
18981     cp_parser_skip_to_end_of_template_parameter_list (parser);
18982   /* The `>' token might be a greater-than operator again now.  */
18983   parser->greater_than_is_operator_p
18984     = saved_greater_than_is_operator_p;
18985   /* Restore the SAVED_SCOPE.  */
18986   parser->scope = saved_scope;
18987   parser->qualifying_scope = saved_qualifying_scope;
18988   parser->object_scope = saved_object_scope;
18989   cp_unevaluated_operand = saved_unevaluated_operand;
18990   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
18991
18992   return arguments;
18993 }
18994
18995 /* MEMBER_FUNCTION is a member function, or a friend.  If default
18996    arguments, or the body of the function have not yet been parsed,
18997    parse them now.  */
18998
18999 static void
19000 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19001 {
19002   /* If this member is a template, get the underlying
19003      FUNCTION_DECL.  */
19004   if (DECL_FUNCTION_TEMPLATE_P (member_function))
19005     member_function = DECL_TEMPLATE_RESULT (member_function);
19006
19007   /* There should not be any class definitions in progress at this
19008      point; the bodies of members are only parsed outside of all class
19009      definitions.  */
19010   gcc_assert (parser->num_classes_being_defined == 0);
19011   /* While we're parsing the member functions we might encounter more
19012      classes.  We want to handle them right away, but we don't want
19013      them getting mixed up with functions that are currently in the
19014      queue.  */
19015   parser->unparsed_functions_queues
19016     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19017
19018   /* Make sure that any template parameters are in scope.  */
19019   maybe_begin_member_template_processing (member_function);
19020
19021   /* If the body of the function has not yet been parsed, parse it
19022      now.  */
19023   if (DECL_PENDING_INLINE_P (member_function))
19024     {
19025       tree function_scope;
19026       cp_token_cache *tokens;
19027
19028       /* The function is no longer pending; we are processing it.  */
19029       tokens = DECL_PENDING_INLINE_INFO (member_function);
19030       DECL_PENDING_INLINE_INFO (member_function) = NULL;
19031       DECL_PENDING_INLINE_P (member_function) = 0;
19032
19033       /* If this is a local class, enter the scope of the containing
19034          function.  */
19035       function_scope = current_function_decl;
19036       if (function_scope)
19037         push_function_context ();
19038
19039       /* Push the body of the function onto the lexer stack.  */
19040       cp_parser_push_lexer_for_tokens (parser, tokens);
19041
19042       /* Let the front end know that we going to be defining this
19043          function.  */
19044       start_preparsed_function (member_function, NULL_TREE,
19045                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
19046
19047       /* Don't do access checking if it is a templated function.  */
19048       if (processing_template_decl)
19049         push_deferring_access_checks (dk_no_check);
19050
19051       /* Now, parse the body of the function.  */
19052       cp_parser_function_definition_after_declarator (parser,
19053                                                       /*inline_p=*/true);
19054
19055       if (processing_template_decl)
19056         pop_deferring_access_checks ();
19057
19058       /* Leave the scope of the containing function.  */
19059       if (function_scope)
19060         pop_function_context ();
19061       cp_parser_pop_lexer (parser);
19062     }
19063
19064   /* Remove any template parameters from the symbol table.  */
19065   maybe_end_member_template_processing ();
19066
19067   /* Restore the queue.  */
19068   parser->unparsed_functions_queues
19069     = TREE_CHAIN (parser->unparsed_functions_queues);
19070 }
19071
19072 /* If DECL contains any default args, remember it on the unparsed
19073    functions queue.  */
19074
19075 static void
19076 cp_parser_save_default_args (cp_parser* parser, tree decl)
19077 {
19078   tree probe;
19079
19080   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19081        probe;
19082        probe = TREE_CHAIN (probe))
19083     if (TREE_PURPOSE (probe))
19084       {
19085         TREE_PURPOSE (parser->unparsed_functions_queues)
19086           = tree_cons (current_class_type, decl,
19087                        TREE_PURPOSE (parser->unparsed_functions_queues));
19088         break;
19089       }
19090 }
19091
19092 /* FN is a FUNCTION_DECL which may contains a parameter with an
19093    unparsed DEFAULT_ARG.  Parse the default args now.  This function
19094    assumes that the current scope is the scope in which the default
19095    argument should be processed.  */
19096
19097 static void
19098 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19099 {
19100   bool saved_local_variables_forbidden_p;
19101   tree parm, parmdecl;
19102
19103   /* While we're parsing the default args, we might (due to the
19104      statement expression extension) encounter more classes.  We want
19105      to handle them right away, but we don't want them getting mixed
19106      up with default args that are currently in the queue.  */
19107   parser->unparsed_functions_queues
19108     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19109
19110   /* Local variable names (and the `this' keyword) may not appear
19111      in a default argument.  */
19112   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19113   parser->local_variables_forbidden_p = true;
19114
19115   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19116          parmdecl = DECL_ARGUMENTS (fn);
19117        parm && parm != void_list_node;
19118        parm = TREE_CHAIN (parm),
19119          parmdecl = TREE_CHAIN (parmdecl))
19120     {
19121       cp_token_cache *tokens;
19122       tree default_arg = TREE_PURPOSE (parm);
19123       tree parsed_arg;
19124       VEC(tree,gc) *insts;
19125       tree copy;
19126       unsigned ix;
19127
19128       if (!default_arg)
19129         continue;
19130
19131       if (TREE_CODE (default_arg) != DEFAULT_ARG)
19132         /* This can happen for a friend declaration for a function
19133            already declared with default arguments.  */
19134         continue;
19135
19136        /* Push the saved tokens for the default argument onto the parser's
19137           lexer stack.  */
19138       tokens = DEFARG_TOKENS (default_arg);
19139       cp_parser_push_lexer_for_tokens (parser, tokens);
19140
19141       start_lambda_scope (parmdecl);
19142
19143       /* Parse the assignment-expression.  */
19144       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19145       if (parsed_arg == error_mark_node)
19146         {
19147           cp_parser_pop_lexer (parser);
19148           continue;
19149         }
19150
19151       if (!processing_template_decl)
19152         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19153
19154       TREE_PURPOSE (parm) = parsed_arg;
19155
19156       /* Update any instantiations we've already created.  */
19157       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19158            VEC_iterate (tree, insts, ix, copy); ix++)
19159         TREE_PURPOSE (copy) = parsed_arg;
19160
19161       finish_lambda_scope ();
19162
19163       /* If the token stream has not been completely used up, then
19164          there was extra junk after the end of the default
19165          argument.  */
19166       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19167         cp_parser_error (parser, "expected %<,%>");
19168
19169       /* Revert to the main lexer.  */
19170       cp_parser_pop_lexer (parser);
19171     }
19172
19173   /* Make sure no default arg is missing.  */
19174   check_default_args (fn);
19175
19176   /* Restore the state of local_variables_forbidden_p.  */
19177   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19178
19179   /* Restore the queue.  */
19180   parser->unparsed_functions_queues
19181     = TREE_CHAIN (parser->unparsed_functions_queues);
19182 }
19183
19184 /* Parse the operand of `sizeof' (or a similar operator).  Returns
19185    either a TYPE or an expression, depending on the form of the
19186    input.  The KEYWORD indicates which kind of expression we have
19187    encountered.  */
19188
19189 static tree
19190 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19191 {
19192   tree expr = NULL_TREE;
19193   const char *saved_message;
19194   char *tmp;
19195   bool saved_integral_constant_expression_p;
19196   bool saved_non_integral_constant_expression_p;
19197   bool pack_expansion_p = false;
19198
19199   /* Types cannot be defined in a `sizeof' expression.  Save away the
19200      old message.  */
19201   saved_message = parser->type_definition_forbidden_message;
19202   /* And create the new one.  */
19203   tmp = concat ("types may not be defined in %<",
19204                 IDENTIFIER_POINTER (ridpointers[keyword]),
19205                 "%> expressions", NULL);
19206   parser->type_definition_forbidden_message = tmp;
19207
19208   /* The restrictions on constant-expressions do not apply inside
19209      sizeof expressions.  */
19210   saved_integral_constant_expression_p
19211     = parser->integral_constant_expression_p;
19212   saved_non_integral_constant_expression_p
19213     = parser->non_integral_constant_expression_p;
19214   parser->integral_constant_expression_p = false;
19215
19216   /* If it's a `...', then we are computing the length of a parameter
19217      pack.  */
19218   if (keyword == RID_SIZEOF
19219       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19220     {
19221       /* Consume the `...'.  */
19222       cp_lexer_consume_token (parser->lexer);
19223       maybe_warn_variadic_templates ();
19224
19225       /* Note that this is an expansion.  */
19226       pack_expansion_p = true;
19227     }
19228
19229   /* Do not actually evaluate the expression.  */
19230   ++cp_unevaluated_operand;
19231   ++c_inhibit_evaluation_warnings;
19232   /* If it's a `(', then we might be looking at the type-id
19233      construction.  */
19234   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19235     {
19236       tree type;
19237       bool saved_in_type_id_in_expr_p;
19238
19239       /* We can't be sure yet whether we're looking at a type-id or an
19240          expression.  */
19241       cp_parser_parse_tentatively (parser);
19242       /* Consume the `('.  */
19243       cp_lexer_consume_token (parser->lexer);
19244       /* Parse the type-id.  */
19245       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19246       parser->in_type_id_in_expr_p = true;
19247       type = cp_parser_type_id (parser);
19248       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19249       /* Now, look for the trailing `)'.  */
19250       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19251       /* If all went well, then we're done.  */
19252       if (cp_parser_parse_definitely (parser))
19253         {
19254           cp_decl_specifier_seq decl_specs;
19255
19256           /* Build a trivial decl-specifier-seq.  */
19257           clear_decl_specs (&decl_specs);
19258           decl_specs.type = type;
19259
19260           /* Call grokdeclarator to figure out what type this is.  */
19261           expr = grokdeclarator (NULL,
19262                                  &decl_specs,
19263                                  TYPENAME,
19264                                  /*initialized=*/0,
19265                                  /*attrlist=*/NULL);
19266         }
19267     }
19268
19269   /* If the type-id production did not work out, then we must be
19270      looking at the unary-expression production.  */
19271   if (!expr)
19272     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19273                                        /*cast_p=*/false, NULL);
19274
19275   if (pack_expansion_p)
19276     /* Build a pack expansion. */
19277     expr = make_pack_expansion (expr);
19278
19279   /* Go back to evaluating expressions.  */
19280   --cp_unevaluated_operand;
19281   --c_inhibit_evaluation_warnings;
19282
19283   /* Free the message we created.  */
19284   free (tmp);
19285   /* And restore the old one.  */
19286   parser->type_definition_forbidden_message = saved_message;
19287   parser->integral_constant_expression_p
19288     = saved_integral_constant_expression_p;
19289   parser->non_integral_constant_expression_p
19290     = saved_non_integral_constant_expression_p;
19291
19292   return expr;
19293 }
19294
19295 /* If the current declaration has no declarator, return true.  */
19296
19297 static bool
19298 cp_parser_declares_only_class_p (cp_parser *parser)
19299 {
19300   /* If the next token is a `;' or a `,' then there is no
19301      declarator.  */
19302   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19303           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19304 }
19305
19306 /* Update the DECL_SPECS to reflect the storage class indicated by
19307    KEYWORD.  */
19308
19309 static void
19310 cp_parser_set_storage_class (cp_parser *parser,
19311                              cp_decl_specifier_seq *decl_specs,
19312                              enum rid keyword,
19313                              location_t location)
19314 {
19315   cp_storage_class storage_class;
19316
19317   if (parser->in_unbraced_linkage_specification_p)
19318     {
19319       error_at (location, "invalid use of %qD in linkage specification",
19320                 ridpointers[keyword]);
19321       return;
19322     }
19323   else if (decl_specs->storage_class != sc_none)
19324     {
19325       decl_specs->conflicting_specifiers_p = true;
19326       return;
19327     }
19328
19329   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19330       && decl_specs->specs[(int) ds_thread])
19331     {
19332       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19333       decl_specs->specs[(int) ds_thread] = 0;
19334     }
19335
19336   switch (keyword)
19337     {
19338     case RID_AUTO:
19339       storage_class = sc_auto;
19340       break;
19341     case RID_REGISTER:
19342       storage_class = sc_register;
19343       break;
19344     case RID_STATIC:
19345       storage_class = sc_static;
19346       break;
19347     case RID_EXTERN:
19348       storage_class = sc_extern;
19349       break;
19350     case RID_MUTABLE:
19351       storage_class = sc_mutable;
19352       break;
19353     default:
19354       gcc_unreachable ();
19355     }
19356   decl_specs->storage_class = storage_class;
19357
19358   /* A storage class specifier cannot be applied alongside a typedef 
19359      specifier. If there is a typedef specifier present then set 
19360      conflicting_specifiers_p which will trigger an error later
19361      on in grokdeclarator. */
19362   if (decl_specs->specs[(int)ds_typedef])
19363     decl_specs->conflicting_specifiers_p = true;
19364 }
19365
19366 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
19367    is true, the type is a user-defined type; otherwise it is a
19368    built-in type specified by a keyword.  */
19369
19370 static void
19371 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19372                               tree type_spec,
19373                               location_t location,
19374                               bool user_defined_p)
19375 {
19376   decl_specs->any_specifiers_p = true;
19377
19378   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19379      (with, for example, in "typedef int wchar_t;") we remember that
19380      this is what happened.  In system headers, we ignore these
19381      declarations so that G++ can work with system headers that are not
19382      C++-safe.  */
19383   if (decl_specs->specs[(int) ds_typedef]
19384       && !user_defined_p
19385       && (type_spec == boolean_type_node
19386           || type_spec == char16_type_node
19387           || type_spec == char32_type_node
19388           || type_spec == wchar_type_node)
19389       && (decl_specs->type
19390           || decl_specs->specs[(int) ds_long]
19391           || decl_specs->specs[(int) ds_short]
19392           || decl_specs->specs[(int) ds_unsigned]
19393           || decl_specs->specs[(int) ds_signed]))
19394     {
19395       decl_specs->redefined_builtin_type = type_spec;
19396       if (!decl_specs->type)
19397         {
19398           decl_specs->type = type_spec;
19399           decl_specs->user_defined_type_p = false;
19400           decl_specs->type_location = location;
19401         }
19402     }
19403   else if (decl_specs->type)
19404     decl_specs->multiple_types_p = true;
19405   else
19406     {
19407       decl_specs->type = type_spec;
19408       decl_specs->user_defined_type_p = user_defined_p;
19409       decl_specs->redefined_builtin_type = NULL_TREE;
19410       decl_specs->type_location = location;
19411     }
19412 }
19413
19414 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19415    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
19416
19417 static bool
19418 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19419 {
19420   return decl_specifiers->specs[(int) ds_friend] != 0;
19421 }
19422
19423 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
19424    issue an error message indicating that TOKEN_DESC was expected.
19425
19426    Returns the token consumed, if the token had the appropriate type.
19427    Otherwise, returns NULL.  */
19428
19429 static cp_token *
19430 cp_parser_require (cp_parser* parser,
19431                    enum cpp_ttype type,
19432                    const char* token_desc)
19433 {
19434   if (cp_lexer_next_token_is (parser->lexer, type))
19435     return cp_lexer_consume_token (parser->lexer);
19436   else
19437     {
19438       /* Output the MESSAGE -- unless we're parsing tentatively.  */
19439       if (!cp_parser_simulate_error (parser))
19440         {
19441           char *message = concat ("expected ", token_desc, NULL);
19442           cp_parser_error (parser, message);
19443           free (message);
19444         }
19445       return NULL;
19446     }
19447 }
19448
19449 /* An error message is produced if the next token is not '>'.
19450    All further tokens are skipped until the desired token is
19451    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
19452
19453 static void
19454 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19455 {
19456   /* Current level of '< ... >'.  */
19457   unsigned level = 0;
19458   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
19459   unsigned nesting_depth = 0;
19460
19461   /* Are we ready, yet?  If not, issue error message.  */
19462   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19463     return;
19464
19465   /* Skip tokens until the desired token is found.  */
19466   while (true)
19467     {
19468       /* Peek at the next token.  */
19469       switch (cp_lexer_peek_token (parser->lexer)->type)
19470         {
19471         case CPP_LESS:
19472           if (!nesting_depth)
19473             ++level;
19474           break;
19475
19476         case CPP_RSHIFT:
19477           if (cxx_dialect == cxx98)
19478             /* C++0x views the `>>' operator as two `>' tokens, but
19479                C++98 does not. */
19480             break;
19481           else if (!nesting_depth && level-- == 0)
19482             {
19483               /* We've hit a `>>' where the first `>' closes the
19484                  template argument list, and the second `>' is
19485                  spurious.  Just consume the `>>' and stop; we've
19486                  already produced at least one error.  */
19487               cp_lexer_consume_token (parser->lexer);
19488               return;
19489             }
19490           /* Fall through for C++0x, so we handle the second `>' in
19491              the `>>'.  */
19492
19493         case CPP_GREATER:
19494           if (!nesting_depth && level-- == 0)
19495             {
19496               /* We've reached the token we want, consume it and stop.  */
19497               cp_lexer_consume_token (parser->lexer);
19498               return;
19499             }
19500           break;
19501
19502         case CPP_OPEN_PAREN:
19503         case CPP_OPEN_SQUARE:
19504           ++nesting_depth;
19505           break;
19506
19507         case CPP_CLOSE_PAREN:
19508         case CPP_CLOSE_SQUARE:
19509           if (nesting_depth-- == 0)
19510             return;
19511           break;
19512
19513         case CPP_EOF:
19514         case CPP_PRAGMA_EOL:
19515         case CPP_SEMICOLON:
19516         case CPP_OPEN_BRACE:
19517         case CPP_CLOSE_BRACE:
19518           /* The '>' was probably forgotten, don't look further.  */
19519           return;
19520
19521         default:
19522           break;
19523         }
19524
19525       /* Consume this token.  */
19526       cp_lexer_consume_token (parser->lexer);
19527     }
19528 }
19529
19530 /* If the next token is the indicated keyword, consume it.  Otherwise,
19531    issue an error message indicating that TOKEN_DESC was expected.
19532
19533    Returns the token consumed, if the token had the appropriate type.
19534    Otherwise, returns NULL.  */
19535
19536 static cp_token *
19537 cp_parser_require_keyword (cp_parser* parser,
19538                            enum rid keyword,
19539                            const char* token_desc)
19540 {
19541   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19542
19543   if (token && token->keyword != keyword)
19544     {
19545       dyn_string_t error_msg;
19546
19547       /* Format the error message.  */
19548       error_msg = dyn_string_new (0);
19549       dyn_string_append_cstr (error_msg, "expected ");
19550       dyn_string_append_cstr (error_msg, token_desc);
19551       cp_parser_error (parser, error_msg->s);
19552       dyn_string_delete (error_msg);
19553       return NULL;
19554     }
19555
19556   return token;
19557 }
19558
19559 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19560    function-definition.  */
19561
19562 static bool
19563 cp_parser_token_starts_function_definition_p (cp_token* token)
19564 {
19565   return (/* An ordinary function-body begins with an `{'.  */
19566           token->type == CPP_OPEN_BRACE
19567           /* A ctor-initializer begins with a `:'.  */
19568           || token->type == CPP_COLON
19569           /* A function-try-block begins with `try'.  */
19570           || token->keyword == RID_TRY
19571           /* The named return value extension begins with `return'.  */
19572           || token->keyword == RID_RETURN);
19573 }
19574
19575 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19576    definition.  */
19577
19578 static bool
19579 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19580 {
19581   cp_token *token;
19582
19583   token = cp_lexer_peek_token (parser->lexer);
19584   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19585 }
19586
19587 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19588    C++0x) ending a template-argument.  */
19589
19590 static bool
19591 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19592 {
19593   cp_token *token;
19594
19595   token = cp_lexer_peek_token (parser->lexer);
19596   return (token->type == CPP_COMMA 
19597           || token->type == CPP_GREATER
19598           || token->type == CPP_ELLIPSIS
19599           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19600 }
19601
19602 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19603    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
19604
19605 static bool
19606 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19607                                                      size_t n)
19608 {
19609   cp_token *token;
19610
19611   token = cp_lexer_peek_nth_token (parser->lexer, n);
19612   if (token->type == CPP_LESS)
19613     return true;
19614   /* Check for the sequence `<::' in the original code. It would be lexed as
19615      `[:', where `[' is a digraph, and there is no whitespace before
19616      `:'.  */
19617   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19618     {
19619       cp_token *token2;
19620       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19621       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19622         return true;
19623     }
19624   return false;
19625 }
19626
19627 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19628    or none_type otherwise.  */
19629
19630 static enum tag_types
19631 cp_parser_token_is_class_key (cp_token* token)
19632 {
19633   switch (token->keyword)
19634     {
19635     case RID_CLASS:
19636       return class_type;
19637     case RID_STRUCT:
19638       return record_type;
19639     case RID_UNION:
19640       return union_type;
19641
19642     default:
19643       return none_type;
19644     }
19645 }
19646
19647 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
19648
19649 static void
19650 cp_parser_check_class_key (enum tag_types class_key, tree type)
19651 {
19652   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19653     permerror (input_location, "%qs tag used in naming %q#T",
19654             class_key == union_type ? "union"
19655              : class_key == record_type ? "struct" : "class",
19656              type);
19657 }
19658
19659 /* Issue an error message if DECL is redeclared with different
19660    access than its original declaration [class.access.spec/3].
19661    This applies to nested classes and nested class templates.
19662    [class.mem/1].  */
19663
19664 static void
19665 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19666 {
19667   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19668     return;
19669
19670   if ((TREE_PRIVATE (decl)
19671        != (current_access_specifier == access_private_node))
19672       || (TREE_PROTECTED (decl)
19673           != (current_access_specifier == access_protected_node)))
19674     error_at (location, "%qD redeclared with different access", decl);
19675 }
19676
19677 /* Look for the `template' keyword, as a syntactic disambiguator.
19678    Return TRUE iff it is present, in which case it will be
19679    consumed.  */
19680
19681 static bool
19682 cp_parser_optional_template_keyword (cp_parser *parser)
19683 {
19684   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19685     {
19686       /* The `template' keyword can only be used within templates;
19687          outside templates the parser can always figure out what is a
19688          template and what is not.  */
19689       if (!processing_template_decl)
19690         {
19691           cp_token *token = cp_lexer_peek_token (parser->lexer);
19692           error_at (token->location,
19693                     "%<template%> (as a disambiguator) is only allowed "
19694                     "within templates");
19695           /* If this part of the token stream is rescanned, the same
19696              error message would be generated.  So, we purge the token
19697              from the stream.  */
19698           cp_lexer_purge_token (parser->lexer);
19699           return false;
19700         }
19701       else
19702         {
19703           /* Consume the `template' keyword.  */
19704           cp_lexer_consume_token (parser->lexer);
19705           return true;
19706         }
19707     }
19708
19709   return false;
19710 }
19711
19712 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
19713    set PARSER->SCOPE, and perform other related actions.  */
19714
19715 static void
19716 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19717 {
19718   int i;
19719   struct tree_check *check_value;
19720   deferred_access_check *chk;
19721   VEC (deferred_access_check,gc) *checks;
19722
19723   /* Get the stored value.  */
19724   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19725   /* Perform any access checks that were deferred.  */
19726   checks = check_value->checks;
19727   if (checks)
19728     {
19729       for (i = 0 ;
19730            VEC_iterate (deferred_access_check, checks, i, chk) ;
19731            ++i)
19732         {
19733           perform_or_defer_access_check (chk->binfo,
19734                                          chk->decl,
19735                                          chk->diag_decl);
19736         }
19737     }
19738   /* Set the scope from the stored value.  */
19739   parser->scope = check_value->value;
19740   parser->qualifying_scope = check_value->qualifying_scope;
19741   parser->object_scope = NULL_TREE;
19742 }
19743
19744 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
19745    encounter the end of a block before what we were looking for.  */
19746
19747 static bool
19748 cp_parser_cache_group (cp_parser *parser,
19749                        enum cpp_ttype end,
19750                        unsigned depth)
19751 {
19752   while (true)
19753     {
19754       cp_token *token = cp_lexer_peek_token (parser->lexer);
19755
19756       /* Abort a parenthesized expression if we encounter a semicolon.  */
19757       if ((end == CPP_CLOSE_PAREN || depth == 0)
19758           && token->type == CPP_SEMICOLON)
19759         return true;
19760       /* If we've reached the end of the file, stop.  */
19761       if (token->type == CPP_EOF
19762           || (end != CPP_PRAGMA_EOL
19763               && token->type == CPP_PRAGMA_EOL))
19764         return true;
19765       if (token->type == CPP_CLOSE_BRACE && depth == 0)
19766         /* We've hit the end of an enclosing block, so there's been some
19767            kind of syntax error.  */
19768         return true;
19769
19770       /* Consume the token.  */
19771       cp_lexer_consume_token (parser->lexer);
19772       /* See if it starts a new group.  */
19773       if (token->type == CPP_OPEN_BRACE)
19774         {
19775           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19776           /* In theory this should probably check end == '}', but
19777              cp_parser_save_member_function_body needs it to exit
19778              after either '}' or ')' when called with ')'.  */
19779           if (depth == 0)
19780             return false;
19781         }
19782       else if (token->type == CPP_OPEN_PAREN)
19783         {
19784           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19785           if (depth == 0 && end == CPP_CLOSE_PAREN)
19786             return false;
19787         }
19788       else if (token->type == CPP_PRAGMA)
19789         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19790       else if (token->type == end)
19791         return false;
19792     }
19793 }
19794
19795 /* Begin parsing tentatively.  We always save tokens while parsing
19796    tentatively so that if the tentative parsing fails we can restore the
19797    tokens.  */
19798
19799 static void
19800 cp_parser_parse_tentatively (cp_parser* parser)
19801 {
19802   /* Enter a new parsing context.  */
19803   parser->context = cp_parser_context_new (parser->context);
19804   /* Begin saving tokens.  */
19805   cp_lexer_save_tokens (parser->lexer);
19806   /* In order to avoid repetitive access control error messages,
19807      access checks are queued up until we are no longer parsing
19808      tentatively.  */
19809   push_deferring_access_checks (dk_deferred);
19810 }
19811
19812 /* Commit to the currently active tentative parse.  */
19813
19814 static void
19815 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19816 {
19817   cp_parser_context *context;
19818   cp_lexer *lexer;
19819
19820   /* Mark all of the levels as committed.  */
19821   lexer = parser->lexer;
19822   for (context = parser->context; context->next; context = context->next)
19823     {
19824       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19825         break;
19826       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19827       while (!cp_lexer_saving_tokens (lexer))
19828         lexer = lexer->next;
19829       cp_lexer_commit_tokens (lexer);
19830     }
19831 }
19832
19833 /* Abort the currently active tentative parse.  All consumed tokens
19834    will be rolled back, and no diagnostics will be issued.  */
19835
19836 static void
19837 cp_parser_abort_tentative_parse (cp_parser* parser)
19838 {
19839   cp_parser_simulate_error (parser);
19840   /* Now, pretend that we want to see if the construct was
19841      successfully parsed.  */
19842   cp_parser_parse_definitely (parser);
19843 }
19844
19845 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19846    token stream.  Otherwise, commit to the tokens we have consumed.
19847    Returns true if no error occurred; false otherwise.  */
19848
19849 static bool
19850 cp_parser_parse_definitely (cp_parser* parser)
19851 {
19852   bool error_occurred;
19853   cp_parser_context *context;
19854
19855   /* Remember whether or not an error occurred, since we are about to
19856      destroy that information.  */
19857   error_occurred = cp_parser_error_occurred (parser);
19858   /* Remove the topmost context from the stack.  */
19859   context = parser->context;
19860   parser->context = context->next;
19861   /* If no parse errors occurred, commit to the tentative parse.  */
19862   if (!error_occurred)
19863     {
19864       /* Commit to the tokens read tentatively, unless that was
19865          already done.  */
19866       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19867         cp_lexer_commit_tokens (parser->lexer);
19868
19869       pop_to_parent_deferring_access_checks ();
19870     }
19871   /* Otherwise, if errors occurred, roll back our state so that things
19872      are just as they were before we began the tentative parse.  */
19873   else
19874     {
19875       cp_lexer_rollback_tokens (parser->lexer);
19876       pop_deferring_access_checks ();
19877     }
19878   /* Add the context to the front of the free list.  */
19879   context->next = cp_parser_context_free_list;
19880   cp_parser_context_free_list = context;
19881
19882   return !error_occurred;
19883 }
19884
19885 /* Returns true if we are parsing tentatively and are not committed to
19886    this tentative parse.  */
19887
19888 static bool
19889 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19890 {
19891   return (cp_parser_parsing_tentatively (parser)
19892           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19893 }
19894
19895 /* Returns nonzero iff an error has occurred during the most recent
19896    tentative parse.  */
19897
19898 static bool
19899 cp_parser_error_occurred (cp_parser* parser)
19900 {
19901   return (cp_parser_parsing_tentatively (parser)
19902           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19903 }
19904
19905 /* Returns nonzero if GNU extensions are allowed.  */
19906
19907 static bool
19908 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19909 {
19910   return parser->allow_gnu_extensions_p;
19911 }
19912 \f
19913 /* Objective-C++ Productions */
19914
19915
19916 /* Parse an Objective-C expression, which feeds into a primary-expression
19917    above.
19918
19919    objc-expression:
19920      objc-message-expression
19921      objc-string-literal
19922      objc-encode-expression
19923      objc-protocol-expression
19924      objc-selector-expression
19925
19926   Returns a tree representation of the expression.  */
19927
19928 static tree
19929 cp_parser_objc_expression (cp_parser* parser)
19930 {
19931   /* Try to figure out what kind of declaration is present.  */
19932   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19933
19934   switch (kwd->type)
19935     {
19936     case CPP_OPEN_SQUARE:
19937       return cp_parser_objc_message_expression (parser);
19938
19939     case CPP_OBJC_STRING:
19940       kwd = cp_lexer_consume_token (parser->lexer);
19941       return objc_build_string_object (kwd->u.value);
19942
19943     case CPP_KEYWORD:
19944       switch (kwd->keyword)
19945         {
19946         case RID_AT_ENCODE:
19947           return cp_parser_objc_encode_expression (parser);
19948
19949         case RID_AT_PROTOCOL:
19950           return cp_parser_objc_protocol_expression (parser);
19951
19952         case RID_AT_SELECTOR:
19953           return cp_parser_objc_selector_expression (parser);
19954
19955         default:
19956           break;
19957         }
19958     default:
19959       error_at (kwd->location,
19960                 "misplaced %<@%D%> Objective-C++ construct",
19961                 kwd->u.value);
19962       cp_parser_skip_to_end_of_block_or_statement (parser);
19963     }
19964
19965   return error_mark_node;
19966 }
19967
19968 /* Parse an Objective-C message expression.
19969
19970    objc-message-expression:
19971      [ objc-message-receiver objc-message-args ]
19972
19973    Returns a representation of an Objective-C message.  */
19974
19975 static tree
19976 cp_parser_objc_message_expression (cp_parser* parser)
19977 {
19978   tree receiver, messageargs;
19979
19980   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
19981   receiver = cp_parser_objc_message_receiver (parser);
19982   messageargs = cp_parser_objc_message_args (parser);
19983   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19984
19985   return objc_build_message_expr (build_tree_list (receiver, messageargs));
19986 }
19987
19988 /* Parse an objc-message-receiver.
19989
19990    objc-message-receiver:
19991      expression
19992      simple-type-specifier
19993
19994   Returns a representation of the type or expression.  */
19995
19996 static tree
19997 cp_parser_objc_message_receiver (cp_parser* parser)
19998 {
19999   tree rcv;
20000
20001   /* An Objective-C message receiver may be either (1) a type
20002      or (2) an expression.  */
20003   cp_parser_parse_tentatively (parser);
20004   rcv = cp_parser_expression (parser, false, NULL);
20005
20006   if (cp_parser_parse_definitely (parser))
20007     return rcv;
20008
20009   rcv = cp_parser_simple_type_specifier (parser,
20010                                          /*decl_specs=*/NULL,
20011                                          CP_PARSER_FLAGS_NONE);
20012
20013   return objc_get_class_reference (rcv);
20014 }
20015
20016 /* Parse the arguments and selectors comprising an Objective-C message.
20017
20018    objc-message-args:
20019      objc-selector
20020      objc-selector-args
20021      objc-selector-args , objc-comma-args
20022
20023    objc-selector-args:
20024      objc-selector [opt] : assignment-expression
20025      objc-selector-args objc-selector [opt] : assignment-expression
20026
20027    objc-comma-args:
20028      assignment-expression
20029      objc-comma-args , assignment-expression
20030
20031    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20032    selector arguments and TREE_VALUE containing a list of comma
20033    arguments.  */
20034
20035 static tree
20036 cp_parser_objc_message_args (cp_parser* parser)
20037 {
20038   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20039   bool maybe_unary_selector_p = true;
20040   cp_token *token = cp_lexer_peek_token (parser->lexer);
20041
20042   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20043     {
20044       tree selector = NULL_TREE, arg;
20045
20046       if (token->type != CPP_COLON)
20047         selector = cp_parser_objc_selector (parser);
20048
20049       /* Detect if we have a unary selector.  */
20050       if (maybe_unary_selector_p
20051           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20052         return build_tree_list (selector, NULL_TREE);
20053
20054       maybe_unary_selector_p = false;
20055       cp_parser_require (parser, CPP_COLON, "%<:%>");
20056       arg = cp_parser_assignment_expression (parser, false, NULL);
20057
20058       sel_args
20059         = chainon (sel_args,
20060                    build_tree_list (selector, arg));
20061
20062       token = cp_lexer_peek_token (parser->lexer);
20063     }
20064
20065   /* Handle non-selector arguments, if any. */
20066   while (token->type == CPP_COMMA)
20067     {
20068       tree arg;
20069
20070       cp_lexer_consume_token (parser->lexer);
20071       arg = cp_parser_assignment_expression (parser, false, NULL);
20072
20073       addl_args
20074         = chainon (addl_args,
20075                    build_tree_list (NULL_TREE, arg));
20076
20077       token = cp_lexer_peek_token (parser->lexer);
20078     }
20079
20080   return build_tree_list (sel_args, addl_args);
20081 }
20082
20083 /* Parse an Objective-C encode expression.
20084
20085    objc-encode-expression:
20086      @encode objc-typename
20087
20088    Returns an encoded representation of the type argument.  */
20089
20090 static tree
20091 cp_parser_objc_encode_expression (cp_parser* parser)
20092 {
20093   tree type;
20094   cp_token *token;
20095
20096   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
20097   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20098   token = cp_lexer_peek_token (parser->lexer);
20099   type = complete_type (cp_parser_type_id (parser));
20100   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20101
20102   if (!type)
20103     {
20104       error_at (token->location, 
20105                 "%<@encode%> must specify a type as an argument");
20106       return error_mark_node;
20107     }
20108
20109   return objc_build_encode_expr (type);
20110 }
20111
20112 /* Parse an Objective-C @defs expression.  */
20113
20114 static tree
20115 cp_parser_objc_defs_expression (cp_parser *parser)
20116 {
20117   tree name;
20118
20119   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
20120   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20121   name = cp_parser_identifier (parser);
20122   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20123
20124   return objc_get_class_ivars (name);
20125 }
20126
20127 /* Parse an Objective-C protocol expression.
20128
20129   objc-protocol-expression:
20130     @protocol ( identifier )
20131
20132   Returns a representation of the protocol expression.  */
20133
20134 static tree
20135 cp_parser_objc_protocol_expression (cp_parser* parser)
20136 {
20137   tree proto;
20138
20139   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20140   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20141   proto = cp_parser_identifier (parser);
20142   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20143
20144   return objc_build_protocol_expr (proto);
20145 }
20146
20147 /* Parse an Objective-C selector expression.
20148
20149    objc-selector-expression:
20150      @selector ( objc-method-signature )
20151
20152    objc-method-signature:
20153      objc-selector
20154      objc-selector-seq
20155
20156    objc-selector-seq:
20157      objc-selector :
20158      objc-selector-seq objc-selector :
20159
20160   Returns a representation of the method selector.  */
20161
20162 static tree
20163 cp_parser_objc_selector_expression (cp_parser* parser)
20164 {
20165   tree sel_seq = NULL_TREE;
20166   bool maybe_unary_selector_p = true;
20167   cp_token *token;
20168   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20169
20170   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
20171   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20172   token = cp_lexer_peek_token (parser->lexer);
20173
20174   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20175          || token->type == CPP_SCOPE)
20176     {
20177       tree selector = NULL_TREE;
20178
20179       if (token->type != CPP_COLON
20180           || token->type == CPP_SCOPE)
20181         selector = cp_parser_objc_selector (parser);
20182
20183       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20184           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20185         {
20186           /* Detect if we have a unary selector.  */
20187           if (maybe_unary_selector_p)
20188             {
20189               sel_seq = selector;
20190               goto finish_selector;
20191             }
20192           else
20193             {
20194               cp_parser_error (parser, "expected %<:%>");
20195             }
20196         }
20197       maybe_unary_selector_p = false;
20198       token = cp_lexer_consume_token (parser->lexer);
20199
20200       if (token->type == CPP_SCOPE)
20201         {
20202           sel_seq
20203             = chainon (sel_seq,
20204                        build_tree_list (selector, NULL_TREE));
20205           sel_seq
20206             = chainon (sel_seq,
20207                        build_tree_list (NULL_TREE, NULL_TREE));
20208         }
20209       else
20210         sel_seq
20211           = chainon (sel_seq,
20212                      build_tree_list (selector, NULL_TREE));
20213
20214       token = cp_lexer_peek_token (parser->lexer);
20215     }
20216
20217  finish_selector:
20218   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20219
20220   return objc_build_selector_expr (loc, sel_seq);
20221 }
20222
20223 /* Parse a list of identifiers.
20224
20225    objc-identifier-list:
20226      identifier
20227      objc-identifier-list , identifier
20228
20229    Returns a TREE_LIST of identifier nodes.  */
20230
20231 static tree
20232 cp_parser_objc_identifier_list (cp_parser* parser)
20233 {
20234   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20235   cp_token *sep = cp_lexer_peek_token (parser->lexer);
20236
20237   while (sep->type == CPP_COMMA)
20238     {
20239       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20240       list = chainon (list,
20241                       build_tree_list (NULL_TREE,
20242                                        cp_parser_identifier (parser)));
20243       sep = cp_lexer_peek_token (parser->lexer);
20244     }
20245
20246   return list;
20247 }
20248
20249 /* Parse an Objective-C alias declaration.
20250
20251    objc-alias-declaration:
20252      @compatibility_alias identifier identifier ;
20253
20254    This function registers the alias mapping with the Objective-C front end.
20255    It returns nothing.  */
20256
20257 static void
20258 cp_parser_objc_alias_declaration (cp_parser* parser)
20259 {
20260   tree alias, orig;
20261
20262   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
20263   alias = cp_parser_identifier (parser);
20264   orig = cp_parser_identifier (parser);
20265   objc_declare_alias (alias, orig);
20266   cp_parser_consume_semicolon_at_end_of_statement (parser);
20267 }
20268
20269 /* Parse an Objective-C class forward-declaration.
20270
20271    objc-class-declaration:
20272      @class objc-identifier-list ;
20273
20274    The function registers the forward declarations with the Objective-C
20275    front end.  It returns nothing.  */
20276
20277 static void
20278 cp_parser_objc_class_declaration (cp_parser* parser)
20279 {
20280   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
20281   objc_declare_class (cp_parser_objc_identifier_list (parser));
20282   cp_parser_consume_semicolon_at_end_of_statement (parser);
20283 }
20284
20285 /* Parse a list of Objective-C protocol references.
20286
20287    objc-protocol-refs-opt:
20288      objc-protocol-refs [opt]
20289
20290    objc-protocol-refs:
20291      < objc-identifier-list >
20292
20293    Returns a TREE_LIST of identifiers, if any.  */
20294
20295 static tree
20296 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20297 {
20298   tree protorefs = NULL_TREE;
20299
20300   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20301     {
20302       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
20303       protorefs = cp_parser_objc_identifier_list (parser);
20304       cp_parser_require (parser, CPP_GREATER, "%<>%>");
20305     }
20306
20307   return protorefs;
20308 }
20309
20310 /* Parse a Objective-C visibility specification.  */
20311
20312 static void
20313 cp_parser_objc_visibility_spec (cp_parser* parser)
20314 {
20315   cp_token *vis = cp_lexer_peek_token (parser->lexer);
20316
20317   switch (vis->keyword)
20318     {
20319     case RID_AT_PRIVATE:
20320       objc_set_visibility (2);
20321       break;
20322     case RID_AT_PROTECTED:
20323       objc_set_visibility (0);
20324       break;
20325     case RID_AT_PUBLIC:
20326       objc_set_visibility (1);
20327       break;
20328     default:
20329       return;
20330     }
20331
20332   /* Eat '@private'/'@protected'/'@public'.  */
20333   cp_lexer_consume_token (parser->lexer);
20334 }
20335
20336 /* Parse an Objective-C method type.  */
20337
20338 static void
20339 cp_parser_objc_method_type (cp_parser* parser)
20340 {
20341   objc_set_method_type
20342    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20343     ? PLUS_EXPR
20344     : MINUS_EXPR);
20345 }
20346
20347 /* Parse an Objective-C protocol qualifier.  */
20348
20349 static tree
20350 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20351 {
20352   tree quals = NULL_TREE, node;
20353   cp_token *token = cp_lexer_peek_token (parser->lexer);
20354
20355   node = token->u.value;
20356
20357   while (node && TREE_CODE (node) == IDENTIFIER_NODE
20358          && (node == ridpointers [(int) RID_IN]
20359              || node == ridpointers [(int) RID_OUT]
20360              || node == ridpointers [(int) RID_INOUT]
20361              || node == ridpointers [(int) RID_BYCOPY]
20362              || node == ridpointers [(int) RID_BYREF]
20363              || node == ridpointers [(int) RID_ONEWAY]))
20364     {
20365       quals = tree_cons (NULL_TREE, node, quals);
20366       cp_lexer_consume_token (parser->lexer);
20367       token = cp_lexer_peek_token (parser->lexer);
20368       node = token->u.value;
20369     }
20370
20371   return quals;
20372 }
20373
20374 /* Parse an Objective-C typename.  */
20375
20376 static tree
20377 cp_parser_objc_typename (cp_parser* parser)
20378 {
20379   tree type_name = NULL_TREE;
20380
20381   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20382     {
20383       tree proto_quals, cp_type = NULL_TREE;
20384
20385       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20386       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20387
20388       /* An ObjC type name may consist of just protocol qualifiers, in which
20389          case the type shall default to 'id'.  */
20390       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20391         cp_type = cp_parser_type_id (parser);
20392
20393       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20394       type_name = build_tree_list (proto_quals, cp_type);
20395     }
20396
20397   return type_name;
20398 }
20399
20400 /* Check to see if TYPE refers to an Objective-C selector name.  */
20401
20402 static bool
20403 cp_parser_objc_selector_p (enum cpp_ttype type)
20404 {
20405   return (type == CPP_NAME || type == CPP_KEYWORD
20406           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20407           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20408           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20409           || type == CPP_XOR || type == CPP_XOR_EQ);
20410 }
20411
20412 /* Parse an Objective-C selector.  */
20413
20414 static tree
20415 cp_parser_objc_selector (cp_parser* parser)
20416 {
20417   cp_token *token = cp_lexer_consume_token (parser->lexer);
20418
20419   if (!cp_parser_objc_selector_p (token->type))
20420     {
20421       error_at (token->location, "invalid Objective-C++ selector name");
20422       return error_mark_node;
20423     }
20424
20425   /* C++ operator names are allowed to appear in ObjC selectors.  */
20426   switch (token->type)
20427     {
20428     case CPP_AND_AND: return get_identifier ("and");
20429     case CPP_AND_EQ: return get_identifier ("and_eq");
20430     case CPP_AND: return get_identifier ("bitand");
20431     case CPP_OR: return get_identifier ("bitor");
20432     case CPP_COMPL: return get_identifier ("compl");
20433     case CPP_NOT: return get_identifier ("not");
20434     case CPP_NOT_EQ: return get_identifier ("not_eq");
20435     case CPP_OR_OR: return get_identifier ("or");
20436     case CPP_OR_EQ: return get_identifier ("or_eq");
20437     case CPP_XOR: return get_identifier ("xor");
20438     case CPP_XOR_EQ: return get_identifier ("xor_eq");
20439     default: return token->u.value;
20440     }
20441 }
20442
20443 /* Parse an Objective-C params list.  */
20444
20445 static tree
20446 cp_parser_objc_method_keyword_params (cp_parser* parser)
20447 {
20448   tree params = NULL_TREE;
20449   bool maybe_unary_selector_p = true;
20450   cp_token *token = cp_lexer_peek_token (parser->lexer);
20451
20452   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20453     {
20454       tree selector = NULL_TREE, type_name, identifier;
20455
20456       if (token->type != CPP_COLON)
20457         selector = cp_parser_objc_selector (parser);
20458
20459       /* Detect if we have a unary selector.  */
20460       if (maybe_unary_selector_p
20461           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20462         return selector;
20463
20464       maybe_unary_selector_p = false;
20465       cp_parser_require (parser, CPP_COLON, "%<:%>");
20466       type_name = cp_parser_objc_typename (parser);
20467       identifier = cp_parser_identifier (parser);
20468
20469       params
20470         = chainon (params,
20471                    objc_build_keyword_decl (selector,
20472                                             type_name,
20473                                             identifier));
20474
20475       token = cp_lexer_peek_token (parser->lexer);
20476     }
20477
20478   return params;
20479 }
20480
20481 /* Parse the non-keyword Objective-C params.  */
20482
20483 static tree
20484 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20485 {
20486   tree params = make_node (TREE_LIST);
20487   cp_token *token = cp_lexer_peek_token (parser->lexer);
20488   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
20489
20490   while (token->type == CPP_COMMA)
20491     {
20492       cp_parameter_declarator *parmdecl;
20493       tree parm;
20494
20495       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20496       token = cp_lexer_peek_token (parser->lexer);
20497
20498       if (token->type == CPP_ELLIPSIS)
20499         {
20500           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
20501           *ellipsisp = true;
20502           break;
20503         }
20504
20505       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20506       parm = grokdeclarator (parmdecl->declarator,
20507                              &parmdecl->decl_specifiers,
20508                              PARM, /*initialized=*/0,
20509                              /*attrlist=*/NULL);
20510
20511       chainon (params, build_tree_list (NULL_TREE, parm));
20512       token = cp_lexer_peek_token (parser->lexer);
20513     }
20514
20515   return params;
20516 }
20517
20518 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
20519
20520 static void
20521 cp_parser_objc_interstitial_code (cp_parser* parser)
20522 {
20523   cp_token *token = cp_lexer_peek_token (parser->lexer);
20524
20525   /* If the next token is `extern' and the following token is a string
20526      literal, then we have a linkage specification.  */
20527   if (token->keyword == RID_EXTERN
20528       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20529     cp_parser_linkage_specification (parser);
20530   /* Handle #pragma, if any.  */
20531   else if (token->type == CPP_PRAGMA)
20532     cp_parser_pragma (parser, pragma_external);
20533   /* Allow stray semicolons.  */
20534   else if (token->type == CPP_SEMICOLON)
20535     cp_lexer_consume_token (parser->lexer);
20536   /* Finally, try to parse a block-declaration, or a function-definition.  */
20537   else
20538     cp_parser_block_declaration (parser, /*statement_p=*/false);
20539 }
20540
20541 /* Parse a method signature.  */
20542
20543 static tree
20544 cp_parser_objc_method_signature (cp_parser* parser)
20545 {
20546   tree rettype, kwdparms, optparms;
20547   bool ellipsis = false;
20548
20549   cp_parser_objc_method_type (parser);
20550   rettype = cp_parser_objc_typename (parser);
20551   kwdparms = cp_parser_objc_method_keyword_params (parser);
20552   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20553
20554   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20555 }
20556
20557 /* Pars an Objective-C method prototype list.  */
20558
20559 static void
20560 cp_parser_objc_method_prototype_list (cp_parser* parser)
20561 {
20562   cp_token *token = cp_lexer_peek_token (parser->lexer);
20563
20564   while (token->keyword != RID_AT_END)
20565     {
20566       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20567         {
20568           objc_add_method_declaration
20569            (cp_parser_objc_method_signature (parser));
20570           cp_parser_consume_semicolon_at_end_of_statement (parser);
20571         }
20572       else
20573         /* Allow for interspersed non-ObjC++ code.  */
20574         cp_parser_objc_interstitial_code (parser);
20575
20576       token = cp_lexer_peek_token (parser->lexer);
20577     }
20578
20579   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20580   objc_finish_interface ();
20581 }
20582
20583 /* Parse an Objective-C method definition list.  */
20584
20585 static void
20586 cp_parser_objc_method_definition_list (cp_parser* parser)
20587 {
20588   cp_token *token = cp_lexer_peek_token (parser->lexer);
20589
20590   while (token->keyword != RID_AT_END)
20591     {
20592       tree meth;
20593
20594       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20595         {
20596           push_deferring_access_checks (dk_deferred);
20597           objc_start_method_definition
20598            (cp_parser_objc_method_signature (parser));
20599
20600           /* For historical reasons, we accept an optional semicolon.  */
20601           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20602             cp_lexer_consume_token (parser->lexer);
20603
20604           perform_deferred_access_checks ();
20605           stop_deferring_access_checks ();
20606           meth = cp_parser_function_definition_after_declarator (parser,
20607                                                                  false);
20608           pop_deferring_access_checks ();
20609           objc_finish_method_definition (meth);
20610         }
20611       else
20612         /* Allow for interspersed non-ObjC++ code.  */
20613         cp_parser_objc_interstitial_code (parser);
20614
20615       token = cp_lexer_peek_token (parser->lexer);
20616     }
20617
20618   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20619   objc_finish_implementation ();
20620 }
20621
20622 /* Parse Objective-C ivars.  */
20623
20624 static void
20625 cp_parser_objc_class_ivars (cp_parser* parser)
20626 {
20627   cp_token *token = cp_lexer_peek_token (parser->lexer);
20628
20629   if (token->type != CPP_OPEN_BRACE)
20630     return;     /* No ivars specified.  */
20631
20632   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
20633   token = cp_lexer_peek_token (parser->lexer);
20634
20635   while (token->type != CPP_CLOSE_BRACE)
20636     {
20637       cp_decl_specifier_seq declspecs;
20638       int decl_class_or_enum_p;
20639       tree prefix_attributes;
20640
20641       cp_parser_objc_visibility_spec (parser);
20642
20643       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20644         break;
20645
20646       cp_parser_decl_specifier_seq (parser,
20647                                     CP_PARSER_FLAGS_OPTIONAL,
20648                                     &declspecs,
20649                                     &decl_class_or_enum_p);
20650       prefix_attributes = declspecs.attributes;
20651       declspecs.attributes = NULL_TREE;
20652
20653       /* Keep going until we hit the `;' at the end of the
20654          declaration.  */
20655       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20656         {
20657           tree width = NULL_TREE, attributes, first_attribute, decl;
20658           cp_declarator *declarator = NULL;
20659           int ctor_dtor_or_conv_p;
20660
20661           /* Check for a (possibly unnamed) bitfield declaration.  */
20662           token = cp_lexer_peek_token (parser->lexer);
20663           if (token->type == CPP_COLON)
20664             goto eat_colon;
20665
20666           if (token->type == CPP_NAME
20667               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20668                   == CPP_COLON))
20669             {
20670               /* Get the name of the bitfield.  */
20671               declarator = make_id_declarator (NULL_TREE,
20672                                                cp_parser_identifier (parser),
20673                                                sfk_none);
20674
20675              eat_colon:
20676               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20677               /* Get the width of the bitfield.  */
20678               width
20679                 = cp_parser_constant_expression (parser,
20680                                                  /*allow_non_constant=*/false,
20681                                                  NULL);
20682             }
20683           else
20684             {
20685               /* Parse the declarator.  */
20686               declarator
20687                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20688                                         &ctor_dtor_or_conv_p,
20689                                         /*parenthesized_p=*/NULL,
20690                                         /*member_p=*/false);
20691             }
20692
20693           /* Look for attributes that apply to the ivar.  */
20694           attributes = cp_parser_attributes_opt (parser);
20695           /* Remember which attributes are prefix attributes and
20696              which are not.  */
20697           first_attribute = attributes;
20698           /* Combine the attributes.  */
20699           attributes = chainon (prefix_attributes, attributes);
20700
20701           if (width)
20702               /* Create the bitfield declaration.  */
20703               decl = grokbitfield (declarator, &declspecs,
20704                                    width,
20705                                    attributes);
20706           else
20707             decl = grokfield (declarator, &declspecs,
20708                               NULL_TREE, /*init_const_expr_p=*/false,
20709                               NULL_TREE, attributes);
20710
20711           /* Add the instance variable.  */
20712           objc_add_instance_variable (decl);
20713
20714           /* Reset PREFIX_ATTRIBUTES.  */
20715           while (attributes && TREE_CHAIN (attributes) != first_attribute)
20716             attributes = TREE_CHAIN (attributes);
20717           if (attributes)
20718             TREE_CHAIN (attributes) = NULL_TREE;
20719
20720           token = cp_lexer_peek_token (parser->lexer);
20721
20722           if (token->type == CPP_COMMA)
20723             {
20724               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20725               continue;
20726             }
20727           break;
20728         }
20729
20730       cp_parser_consume_semicolon_at_end_of_statement (parser);
20731       token = cp_lexer_peek_token (parser->lexer);
20732     }
20733
20734   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
20735   /* For historical reasons, we accept an optional semicolon.  */
20736   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20737     cp_lexer_consume_token (parser->lexer);
20738 }
20739
20740 /* Parse an Objective-C protocol declaration.  */
20741
20742 static void
20743 cp_parser_objc_protocol_declaration (cp_parser* parser)
20744 {
20745   tree proto, protorefs;
20746   cp_token *tok;
20747
20748   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20749   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20750     {
20751       tok = cp_lexer_peek_token (parser->lexer);
20752       error_at (tok->location, "identifier expected after %<@protocol%>");
20753       goto finish;
20754     }
20755
20756   /* See if we have a forward declaration or a definition.  */
20757   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20758
20759   /* Try a forward declaration first.  */
20760   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20761     {
20762       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20763      finish:
20764       cp_parser_consume_semicolon_at_end_of_statement (parser);
20765     }
20766
20767   /* Ok, we got a full-fledged definition (or at least should).  */
20768   else
20769     {
20770       proto = cp_parser_identifier (parser);
20771       protorefs = cp_parser_objc_protocol_refs_opt (parser);
20772       objc_start_protocol (proto, protorefs);
20773       cp_parser_objc_method_prototype_list (parser);
20774     }
20775 }
20776
20777 /* Parse an Objective-C superclass or category.  */
20778
20779 static void
20780 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20781                                                           tree *categ)
20782 {
20783   cp_token *next = cp_lexer_peek_token (parser->lexer);
20784
20785   *super = *categ = NULL_TREE;
20786   if (next->type == CPP_COLON)
20787     {
20788       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20789       *super = cp_parser_identifier (parser);
20790     }
20791   else if (next->type == CPP_OPEN_PAREN)
20792     {
20793       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20794       *categ = cp_parser_identifier (parser);
20795       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20796     }
20797 }
20798
20799 /* Parse an Objective-C class interface.  */
20800
20801 static void
20802 cp_parser_objc_class_interface (cp_parser* parser)
20803 {
20804   tree name, super, categ, protos;
20805
20806   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20807   name = cp_parser_identifier (parser);
20808   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20809   protos = cp_parser_objc_protocol_refs_opt (parser);
20810
20811   /* We have either a class or a category on our hands.  */
20812   if (categ)
20813     objc_start_category_interface (name, categ, protos);
20814   else
20815     {
20816       objc_start_class_interface (name, super, protos);
20817       /* Handle instance variable declarations, if any.  */
20818       cp_parser_objc_class_ivars (parser);
20819       objc_continue_interface ();
20820     }
20821
20822   cp_parser_objc_method_prototype_list (parser);
20823 }
20824
20825 /* Parse an Objective-C class implementation.  */
20826
20827 static void
20828 cp_parser_objc_class_implementation (cp_parser* parser)
20829 {
20830   tree name, super, categ;
20831
20832   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20833   name = cp_parser_identifier (parser);
20834   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20835
20836   /* We have either a class or a category on our hands.  */
20837   if (categ)
20838     objc_start_category_implementation (name, categ);
20839   else
20840     {
20841       objc_start_class_implementation (name, super);
20842       /* Handle instance variable declarations, if any.  */
20843       cp_parser_objc_class_ivars (parser);
20844       objc_continue_implementation ();
20845     }
20846
20847   cp_parser_objc_method_definition_list (parser);
20848 }
20849
20850 /* Consume the @end token and finish off the implementation.  */
20851
20852 static void
20853 cp_parser_objc_end_implementation (cp_parser* parser)
20854 {
20855   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20856   objc_finish_implementation ();
20857 }
20858
20859 /* Parse an Objective-C declaration.  */
20860
20861 static void
20862 cp_parser_objc_declaration (cp_parser* parser)
20863 {
20864   /* Try to figure out what kind of declaration is present.  */
20865   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20866
20867   switch (kwd->keyword)
20868     {
20869     case RID_AT_ALIAS:
20870       cp_parser_objc_alias_declaration (parser);
20871       break;
20872     case RID_AT_CLASS:
20873       cp_parser_objc_class_declaration (parser);
20874       break;
20875     case RID_AT_PROTOCOL:
20876       cp_parser_objc_protocol_declaration (parser);
20877       break;
20878     case RID_AT_INTERFACE:
20879       cp_parser_objc_class_interface (parser);
20880       break;
20881     case RID_AT_IMPLEMENTATION:
20882       cp_parser_objc_class_implementation (parser);
20883       break;
20884     case RID_AT_END:
20885       cp_parser_objc_end_implementation (parser);
20886       break;
20887     default:
20888       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20889                 kwd->u.value);
20890       cp_parser_skip_to_end_of_block_or_statement (parser);
20891     }
20892 }
20893
20894 /* Parse an Objective-C try-catch-finally statement.
20895
20896    objc-try-catch-finally-stmt:
20897      @try compound-statement objc-catch-clause-seq [opt]
20898        objc-finally-clause [opt]
20899
20900    objc-catch-clause-seq:
20901      objc-catch-clause objc-catch-clause-seq [opt]
20902
20903    objc-catch-clause:
20904      @catch ( exception-declaration ) compound-statement
20905
20906    objc-finally-clause
20907      @finally compound-statement
20908
20909    Returns NULL_TREE.  */
20910
20911 static tree
20912 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20913   location_t location;
20914   tree stmt;
20915
20916   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20917   location = cp_lexer_peek_token (parser->lexer)->location;
20918   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20919      node, lest it get absorbed into the surrounding block.  */
20920   stmt = push_stmt_list ();
20921   cp_parser_compound_statement (parser, NULL, false);
20922   objc_begin_try_stmt (location, pop_stmt_list (stmt));
20923
20924   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20925     {
20926       cp_parameter_declarator *parmdecl;
20927       tree parm;
20928
20929       cp_lexer_consume_token (parser->lexer);
20930       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20931       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20932       parm = grokdeclarator (parmdecl->declarator,
20933                              &parmdecl->decl_specifiers,
20934                              PARM, /*initialized=*/0,
20935                              /*attrlist=*/NULL);
20936       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20937       objc_begin_catch_clause (parm);
20938       cp_parser_compound_statement (parser, NULL, false);
20939       objc_finish_catch_clause ();
20940     }
20941
20942   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20943     {
20944       cp_lexer_consume_token (parser->lexer);
20945       location = cp_lexer_peek_token (parser->lexer)->location;
20946       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20947          node, lest it get absorbed into the surrounding block.  */
20948       stmt = push_stmt_list ();
20949       cp_parser_compound_statement (parser, NULL, false);
20950       objc_build_finally_clause (location, pop_stmt_list (stmt));
20951     }
20952
20953   return objc_finish_try_stmt ();
20954 }
20955
20956 /* Parse an Objective-C synchronized statement.
20957
20958    objc-synchronized-stmt:
20959      @synchronized ( expression ) compound-statement
20960
20961    Returns NULL_TREE.  */
20962
20963 static tree
20964 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20965   location_t location;
20966   tree lock, stmt;
20967
20968   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20969
20970   location = cp_lexer_peek_token (parser->lexer)->location;
20971   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20972   lock = cp_parser_expression (parser, false, NULL);
20973   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20974
20975   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20976      node, lest it get absorbed into the surrounding block.  */
20977   stmt = push_stmt_list ();
20978   cp_parser_compound_statement (parser, NULL, false);
20979
20980   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20981 }
20982
20983 /* Parse an Objective-C throw statement.
20984
20985    objc-throw-stmt:
20986      @throw assignment-expression [opt] ;
20987
20988    Returns a constructed '@throw' statement.  */
20989
20990 static tree
20991 cp_parser_objc_throw_statement (cp_parser *parser) {
20992   tree expr = NULL_TREE;
20993   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20994
20995   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20996
20997   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20998     expr = cp_parser_assignment_expression (parser, false, NULL);
20999
21000   cp_parser_consume_semicolon_at_end_of_statement (parser);
21001
21002   return objc_build_throw_stmt (loc, expr);
21003 }
21004
21005 /* Parse an Objective-C statement.  */
21006
21007 static tree
21008 cp_parser_objc_statement (cp_parser * parser) {
21009   /* Try to figure out what kind of declaration is present.  */
21010   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21011
21012   switch (kwd->keyword)
21013     {
21014     case RID_AT_TRY:
21015       return cp_parser_objc_try_catch_finally_statement (parser);
21016     case RID_AT_SYNCHRONIZED:
21017       return cp_parser_objc_synchronized_statement (parser);
21018     case RID_AT_THROW:
21019       return cp_parser_objc_throw_statement (parser);
21020     default:
21021       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21022                kwd->u.value);
21023       cp_parser_skip_to_end_of_block_or_statement (parser);
21024     }
21025
21026   return error_mark_node;
21027 }
21028 \f
21029 /* OpenMP 2.5 parsing routines.  */
21030
21031 /* Returns name of the next clause.
21032    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21033    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
21034    returned and the token is consumed.  */
21035
21036 static pragma_omp_clause
21037 cp_parser_omp_clause_name (cp_parser *parser)
21038 {
21039   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21040
21041   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21042     result = PRAGMA_OMP_CLAUSE_IF;
21043   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21044     result = PRAGMA_OMP_CLAUSE_DEFAULT;
21045   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21046     result = PRAGMA_OMP_CLAUSE_PRIVATE;
21047   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21048     {
21049       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21050       const char *p = IDENTIFIER_POINTER (id);
21051
21052       switch (p[0])
21053         {
21054         case 'c':
21055           if (!strcmp ("collapse", p))
21056             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21057           else if (!strcmp ("copyin", p))
21058             result = PRAGMA_OMP_CLAUSE_COPYIN;
21059           else if (!strcmp ("copyprivate", p))
21060             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21061           break;
21062         case 'f':
21063           if (!strcmp ("firstprivate", p))
21064             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21065           break;
21066         case 'l':
21067           if (!strcmp ("lastprivate", p))
21068             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21069           break;
21070         case 'n':
21071           if (!strcmp ("nowait", p))
21072             result = PRAGMA_OMP_CLAUSE_NOWAIT;
21073           else if (!strcmp ("num_threads", p))
21074             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21075           break;
21076         case 'o':
21077           if (!strcmp ("ordered", p))
21078             result = PRAGMA_OMP_CLAUSE_ORDERED;
21079           break;
21080         case 'r':
21081           if (!strcmp ("reduction", p))
21082             result = PRAGMA_OMP_CLAUSE_REDUCTION;
21083           break;
21084         case 's':
21085           if (!strcmp ("schedule", p))
21086             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21087           else if (!strcmp ("shared", p))
21088             result = PRAGMA_OMP_CLAUSE_SHARED;
21089           break;
21090         case 'u':
21091           if (!strcmp ("untied", p))
21092             result = PRAGMA_OMP_CLAUSE_UNTIED;
21093           break;
21094         }
21095     }
21096
21097   if (result != PRAGMA_OMP_CLAUSE_NONE)
21098     cp_lexer_consume_token (parser->lexer);
21099
21100   return result;
21101 }
21102
21103 /* Validate that a clause of the given type does not already exist.  */
21104
21105 static void
21106 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21107                            const char *name, location_t location)
21108 {
21109   tree c;
21110
21111   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21112     if (OMP_CLAUSE_CODE (c) == code)
21113       {
21114         error_at (location, "too many %qs clauses", name);
21115         break;
21116       }
21117 }
21118
21119 /* OpenMP 2.5:
21120    variable-list:
21121      identifier
21122      variable-list , identifier
21123
21124    In addition, we match a closing parenthesis.  An opening parenthesis
21125    will have been consumed by the caller.
21126
21127    If KIND is nonzero, create the appropriate node and install the decl
21128    in OMP_CLAUSE_DECL and add the node to the head of the list.
21129
21130    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21131    return the list created.  */
21132
21133 static tree
21134 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21135                                 tree list)
21136 {
21137   cp_token *token;
21138   while (1)
21139     {
21140       tree name, decl;
21141
21142       token = cp_lexer_peek_token (parser->lexer);
21143       name = cp_parser_id_expression (parser, /*template_p=*/false,
21144                                       /*check_dependency_p=*/true,
21145                                       /*template_p=*/NULL,
21146                                       /*declarator_p=*/false,
21147                                       /*optional_p=*/false);
21148       if (name == error_mark_node)
21149         goto skip_comma;
21150
21151       decl = cp_parser_lookup_name_simple (parser, name, token->location);
21152       if (decl == error_mark_node)
21153         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21154       else if (kind != 0)
21155         {
21156           tree u = build_omp_clause (token->location, kind);
21157           OMP_CLAUSE_DECL (u) = decl;
21158           OMP_CLAUSE_CHAIN (u) = list;
21159           list = u;
21160         }
21161       else
21162         list = tree_cons (decl, NULL_TREE, list);
21163
21164     get_comma:
21165       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21166         break;
21167       cp_lexer_consume_token (parser->lexer);
21168     }
21169
21170   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21171     {
21172       int ending;
21173
21174       /* Try to resync to an unnested comma.  Copied from
21175          cp_parser_parenthesized_expression_list.  */
21176     skip_comma:
21177       ending = cp_parser_skip_to_closing_parenthesis (parser,
21178                                                       /*recovering=*/true,
21179                                                       /*or_comma=*/true,
21180                                                       /*consume_paren=*/true);
21181       if (ending < 0)
21182         goto get_comma;
21183     }
21184
21185   return list;
21186 }
21187
21188 /* Similarly, but expect leading and trailing parenthesis.  This is a very
21189    common case for omp clauses.  */
21190
21191 static tree
21192 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21193 {
21194   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21195     return cp_parser_omp_var_list_no_open (parser, kind, list);
21196   return list;
21197 }
21198
21199 /* OpenMP 3.0:
21200    collapse ( constant-expression ) */
21201
21202 static tree
21203 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21204 {
21205   tree c, num;
21206   location_t loc;
21207   HOST_WIDE_INT n;
21208
21209   loc = cp_lexer_peek_token (parser->lexer)->location;
21210   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21211     return list;
21212
21213   num = cp_parser_constant_expression (parser, false, NULL);
21214
21215   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21216     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21217                                            /*or_comma=*/false,
21218                                            /*consume_paren=*/true);
21219
21220   if (num == error_mark_node)
21221     return list;
21222   num = fold_non_dependent_expr (num);
21223   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21224       || !host_integerp (num, 0)
21225       || (n = tree_low_cst (num, 0)) <= 0
21226       || (int) n != n)
21227     {
21228       error_at (loc, "collapse argument needs positive constant integer expression");
21229       return list;
21230     }
21231
21232   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21233   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21234   OMP_CLAUSE_CHAIN (c) = list;
21235   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21236
21237   return c;
21238 }
21239
21240 /* OpenMP 2.5:
21241    default ( shared | none ) */
21242
21243 static tree
21244 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21245 {
21246   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21247   tree c;
21248
21249   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21250     return list;
21251   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21252     {
21253       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21254       const char *p = IDENTIFIER_POINTER (id);
21255
21256       switch (p[0])
21257         {
21258         case 'n':
21259           if (strcmp ("none", p) != 0)
21260             goto invalid_kind;
21261           kind = OMP_CLAUSE_DEFAULT_NONE;
21262           break;
21263
21264         case 's':
21265           if (strcmp ("shared", p) != 0)
21266             goto invalid_kind;
21267           kind = OMP_CLAUSE_DEFAULT_SHARED;
21268           break;
21269
21270         default:
21271           goto invalid_kind;
21272         }
21273
21274       cp_lexer_consume_token (parser->lexer);
21275     }
21276   else
21277     {
21278     invalid_kind:
21279       cp_parser_error (parser, "expected %<none%> or %<shared%>");
21280     }
21281
21282   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21283     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21284                                            /*or_comma=*/false,
21285                                            /*consume_paren=*/true);
21286
21287   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21288     return list;
21289
21290   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21291   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21292   OMP_CLAUSE_CHAIN (c) = list;
21293   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21294
21295   return c;
21296 }
21297
21298 /* OpenMP 2.5:
21299    if ( expression ) */
21300
21301 static tree
21302 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21303 {
21304   tree t, c;
21305
21306   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21307     return list;
21308
21309   t = cp_parser_condition (parser);
21310
21311   if (t == error_mark_node
21312       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21313     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21314                                            /*or_comma=*/false,
21315                                            /*consume_paren=*/true);
21316
21317   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21318
21319   c = build_omp_clause (location, OMP_CLAUSE_IF);
21320   OMP_CLAUSE_IF_EXPR (c) = t;
21321   OMP_CLAUSE_CHAIN (c) = list;
21322
21323   return c;
21324 }
21325
21326 /* OpenMP 2.5:
21327    nowait */
21328
21329 static tree
21330 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21331                              tree list, location_t location)
21332 {
21333   tree c;
21334
21335   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21336
21337   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21338   OMP_CLAUSE_CHAIN (c) = list;
21339   return c;
21340 }
21341
21342 /* OpenMP 2.5:
21343    num_threads ( expression ) */
21344
21345 static tree
21346 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21347                                   location_t location)
21348 {
21349   tree t, c;
21350
21351   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21352     return list;
21353
21354   t = cp_parser_expression (parser, false, NULL);
21355
21356   if (t == error_mark_node
21357       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21358     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21359                                            /*or_comma=*/false,
21360                                            /*consume_paren=*/true);
21361
21362   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21363                              "num_threads", location);
21364
21365   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21366   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21367   OMP_CLAUSE_CHAIN (c) = list;
21368
21369   return c;
21370 }
21371
21372 /* OpenMP 2.5:
21373    ordered */
21374
21375 static tree
21376 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21377                               tree list, location_t location)
21378 {
21379   tree c;
21380
21381   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21382                              "ordered", location);
21383
21384   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21385   OMP_CLAUSE_CHAIN (c) = list;
21386   return c;
21387 }
21388
21389 /* OpenMP 2.5:
21390    reduction ( reduction-operator : variable-list )
21391
21392    reduction-operator:
21393      One of: + * - & ^ | && || */
21394
21395 static tree
21396 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21397 {
21398   enum tree_code code;
21399   tree nlist, c;
21400
21401   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21402     return list;
21403
21404   switch (cp_lexer_peek_token (parser->lexer)->type)
21405     {
21406     case CPP_PLUS:
21407       code = PLUS_EXPR;
21408       break;
21409     case CPP_MULT:
21410       code = MULT_EXPR;
21411       break;
21412     case CPP_MINUS:
21413       code = MINUS_EXPR;
21414       break;
21415     case CPP_AND:
21416       code = BIT_AND_EXPR;
21417       break;
21418     case CPP_XOR:
21419       code = BIT_XOR_EXPR;
21420       break;
21421     case CPP_OR:
21422       code = BIT_IOR_EXPR;
21423       break;
21424     case CPP_AND_AND:
21425       code = TRUTH_ANDIF_EXPR;
21426       break;
21427     case CPP_OR_OR:
21428       code = TRUTH_ORIF_EXPR;
21429       break;
21430     default:
21431       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21432                                "%<|%>, %<&&%>, or %<||%>");
21433     resync_fail:
21434       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21435                                              /*or_comma=*/false,
21436                                              /*consume_paren=*/true);
21437       return list;
21438     }
21439   cp_lexer_consume_token (parser->lexer);
21440
21441   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21442     goto resync_fail;
21443
21444   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21445   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21446     OMP_CLAUSE_REDUCTION_CODE (c) = code;
21447
21448   return nlist;
21449 }
21450
21451 /* OpenMP 2.5:
21452    schedule ( schedule-kind )
21453    schedule ( schedule-kind , expression )
21454
21455    schedule-kind:
21456      static | dynamic | guided | runtime | auto  */
21457
21458 static tree
21459 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21460 {
21461   tree c, t;
21462
21463   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21464     return list;
21465
21466   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21467
21468   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21469     {
21470       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21471       const char *p = IDENTIFIER_POINTER (id);
21472
21473       switch (p[0])
21474         {
21475         case 'd':
21476           if (strcmp ("dynamic", p) != 0)
21477             goto invalid_kind;
21478           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21479           break;
21480
21481         case 'g':
21482           if (strcmp ("guided", p) != 0)
21483             goto invalid_kind;
21484           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21485           break;
21486
21487         case 'r':
21488           if (strcmp ("runtime", p) != 0)
21489             goto invalid_kind;
21490           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21491           break;
21492
21493         default:
21494           goto invalid_kind;
21495         }
21496     }
21497   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21498     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21499   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21500     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21501   else
21502     goto invalid_kind;
21503   cp_lexer_consume_token (parser->lexer);
21504
21505   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21506     {
21507       cp_token *token;
21508       cp_lexer_consume_token (parser->lexer);
21509
21510       token = cp_lexer_peek_token (parser->lexer);
21511       t = cp_parser_assignment_expression (parser, false, NULL);
21512
21513       if (t == error_mark_node)
21514         goto resync_fail;
21515       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21516         error_at (token->location, "schedule %<runtime%> does not take "
21517                   "a %<chunk_size%> parameter");
21518       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21519         error_at (token->location, "schedule %<auto%> does not take "
21520                   "a %<chunk_size%> parameter");
21521       else
21522         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21523
21524       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21525         goto resync_fail;
21526     }
21527   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21528     goto resync_fail;
21529
21530   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21531   OMP_CLAUSE_CHAIN (c) = list;
21532   return c;
21533
21534  invalid_kind:
21535   cp_parser_error (parser, "invalid schedule kind");
21536  resync_fail:
21537   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21538                                          /*or_comma=*/false,
21539                                          /*consume_paren=*/true);
21540   return list;
21541 }
21542
21543 /* OpenMP 3.0:
21544    untied */
21545
21546 static tree
21547 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21548                              tree list, location_t location)
21549 {
21550   tree c;
21551
21552   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21553
21554   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21555   OMP_CLAUSE_CHAIN (c) = list;
21556   return c;
21557 }
21558
21559 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
21560    is a bitmask in MASK.  Return the list of clauses found; the result
21561    of clause default goes in *pdefault.  */
21562
21563 static tree
21564 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21565                            const char *where, cp_token *pragma_tok)
21566 {
21567   tree clauses = NULL;
21568   bool first = true;
21569   cp_token *token = NULL;
21570
21571   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21572     {
21573       pragma_omp_clause c_kind;
21574       const char *c_name;
21575       tree prev = clauses;
21576
21577       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21578         cp_lexer_consume_token (parser->lexer);
21579
21580       token = cp_lexer_peek_token (parser->lexer);
21581       c_kind = cp_parser_omp_clause_name (parser);
21582       first = false;
21583
21584       switch (c_kind)
21585         {
21586         case PRAGMA_OMP_CLAUSE_COLLAPSE:
21587           clauses = cp_parser_omp_clause_collapse (parser, clauses,
21588                                                    token->location);
21589           c_name = "collapse";
21590           break;
21591         case PRAGMA_OMP_CLAUSE_COPYIN:
21592           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21593           c_name = "copyin";
21594           break;
21595         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21596           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21597                                             clauses);
21598           c_name = "copyprivate";
21599           break;
21600         case PRAGMA_OMP_CLAUSE_DEFAULT:
21601           clauses = cp_parser_omp_clause_default (parser, clauses,
21602                                                   token->location);
21603           c_name = "default";
21604           break;
21605         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21606           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21607                                             clauses);
21608           c_name = "firstprivate";
21609           break;
21610         case PRAGMA_OMP_CLAUSE_IF:
21611           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21612           c_name = "if";
21613           break;
21614         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21615           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21616                                             clauses);
21617           c_name = "lastprivate";
21618           break;
21619         case PRAGMA_OMP_CLAUSE_NOWAIT:
21620           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21621           c_name = "nowait";
21622           break;
21623         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21624           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21625                                                       token->location);
21626           c_name = "num_threads";
21627           break;
21628         case PRAGMA_OMP_CLAUSE_ORDERED:
21629           clauses = cp_parser_omp_clause_ordered (parser, clauses,
21630                                                   token->location);
21631           c_name = "ordered";
21632           break;
21633         case PRAGMA_OMP_CLAUSE_PRIVATE:
21634           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21635                                             clauses);
21636           c_name = "private";
21637           break;
21638         case PRAGMA_OMP_CLAUSE_REDUCTION:
21639           clauses = cp_parser_omp_clause_reduction (parser, clauses);
21640           c_name = "reduction";
21641           break;
21642         case PRAGMA_OMP_CLAUSE_SCHEDULE:
21643           clauses = cp_parser_omp_clause_schedule (parser, clauses,
21644                                                    token->location);
21645           c_name = "schedule";
21646           break;
21647         case PRAGMA_OMP_CLAUSE_SHARED:
21648           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21649                                             clauses);
21650           c_name = "shared";
21651           break;
21652         case PRAGMA_OMP_CLAUSE_UNTIED:
21653           clauses = cp_parser_omp_clause_untied (parser, clauses,
21654                                                  token->location);
21655           c_name = "nowait";
21656           break;
21657         default:
21658           cp_parser_error (parser, "expected %<#pragma omp%> clause");
21659           goto saw_error;
21660         }
21661
21662       if (((mask >> c_kind) & 1) == 0)
21663         {
21664           /* Remove the invalid clause(s) from the list to avoid
21665              confusing the rest of the compiler.  */
21666           clauses = prev;
21667           error_at (token->location, "%qs is not valid for %qs", c_name, where);
21668         }
21669     }
21670  saw_error:
21671   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21672   return finish_omp_clauses (clauses);
21673 }
21674
21675 /* OpenMP 2.5:
21676    structured-block:
21677      statement
21678
21679    In practice, we're also interested in adding the statement to an
21680    outer node.  So it is convenient if we work around the fact that
21681    cp_parser_statement calls add_stmt.  */
21682
21683 static unsigned
21684 cp_parser_begin_omp_structured_block (cp_parser *parser)
21685 {
21686   unsigned save = parser->in_statement;
21687
21688   /* Only move the values to IN_OMP_BLOCK if they weren't false.
21689      This preserves the "not within loop or switch" style error messages
21690      for nonsense cases like
21691         void foo() {
21692         #pragma omp single
21693           break;
21694         }
21695   */
21696   if (parser->in_statement)
21697     parser->in_statement = IN_OMP_BLOCK;
21698
21699   return save;
21700 }
21701
21702 static void
21703 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21704 {
21705   parser->in_statement = save;
21706 }
21707
21708 static tree
21709 cp_parser_omp_structured_block (cp_parser *parser)
21710 {
21711   tree stmt = begin_omp_structured_block ();
21712   unsigned int save = cp_parser_begin_omp_structured_block (parser);
21713
21714   cp_parser_statement (parser, NULL_TREE, false, NULL);
21715
21716   cp_parser_end_omp_structured_block (parser, save);
21717   return finish_omp_structured_block (stmt);
21718 }
21719
21720 /* OpenMP 2.5:
21721    # pragma omp atomic new-line
21722      expression-stmt
21723
21724    expression-stmt:
21725      x binop= expr | x++ | ++x | x-- | --x
21726    binop:
21727      +, *, -, /, &, ^, |, <<, >>
21728
21729   where x is an lvalue expression with scalar type.  */
21730
21731 static void
21732 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21733 {
21734   tree lhs, rhs;
21735   enum tree_code code;
21736
21737   cp_parser_require_pragma_eol (parser, pragma_tok);
21738
21739   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21740                                     /*cast_p=*/false, NULL);
21741   switch (TREE_CODE (lhs))
21742     {
21743     case ERROR_MARK:
21744       goto saw_error;
21745
21746     case PREINCREMENT_EXPR:
21747     case POSTINCREMENT_EXPR:
21748       lhs = TREE_OPERAND (lhs, 0);
21749       code = PLUS_EXPR;
21750       rhs = integer_one_node;
21751       break;
21752
21753     case PREDECREMENT_EXPR:
21754     case POSTDECREMENT_EXPR:
21755       lhs = TREE_OPERAND (lhs, 0);
21756       code = MINUS_EXPR;
21757       rhs = integer_one_node;
21758       break;
21759
21760     default:
21761       switch (cp_lexer_peek_token (parser->lexer)->type)
21762         {
21763         case CPP_MULT_EQ:
21764           code = MULT_EXPR;
21765           break;
21766         case CPP_DIV_EQ:
21767           code = TRUNC_DIV_EXPR;
21768           break;
21769         case CPP_PLUS_EQ:
21770           code = PLUS_EXPR;
21771           break;
21772         case CPP_MINUS_EQ:
21773           code = MINUS_EXPR;
21774           break;
21775         case CPP_LSHIFT_EQ:
21776           code = LSHIFT_EXPR;
21777           break;
21778         case CPP_RSHIFT_EQ:
21779           code = RSHIFT_EXPR;
21780           break;
21781         case CPP_AND_EQ:
21782           code = BIT_AND_EXPR;
21783           break;
21784         case CPP_OR_EQ:
21785           code = BIT_IOR_EXPR;
21786           break;
21787         case CPP_XOR_EQ:
21788           code = BIT_XOR_EXPR;
21789           break;
21790         default:
21791           cp_parser_error (parser,
21792                            "invalid operator for %<#pragma omp atomic%>");
21793           goto saw_error;
21794         }
21795       cp_lexer_consume_token (parser->lexer);
21796
21797       rhs = cp_parser_expression (parser, false, NULL);
21798       if (rhs == error_mark_node)
21799         goto saw_error;
21800       break;
21801     }
21802   finish_omp_atomic (code, lhs, rhs);
21803   cp_parser_consume_semicolon_at_end_of_statement (parser);
21804   return;
21805
21806  saw_error:
21807   cp_parser_skip_to_end_of_block_or_statement (parser);
21808 }
21809
21810
21811 /* OpenMP 2.5:
21812    # pragma omp barrier new-line  */
21813
21814 static void
21815 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21816 {
21817   cp_parser_require_pragma_eol (parser, pragma_tok);
21818   finish_omp_barrier ();
21819 }
21820
21821 /* OpenMP 2.5:
21822    # pragma omp critical [(name)] new-line
21823      structured-block  */
21824
21825 static tree
21826 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21827 {
21828   tree stmt, name = NULL;
21829
21830   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21831     {
21832       cp_lexer_consume_token (parser->lexer);
21833
21834       name = cp_parser_identifier (parser);
21835
21836       if (name == error_mark_node
21837           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21838         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21839                                                /*or_comma=*/false,
21840                                                /*consume_paren=*/true);
21841       if (name == error_mark_node)
21842         name = NULL;
21843     }
21844   cp_parser_require_pragma_eol (parser, pragma_tok);
21845
21846   stmt = cp_parser_omp_structured_block (parser);
21847   return c_finish_omp_critical (input_location, stmt, name);
21848 }
21849
21850 /* OpenMP 2.5:
21851    # pragma omp flush flush-vars[opt] new-line
21852
21853    flush-vars:
21854      ( variable-list ) */
21855
21856 static void
21857 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21858 {
21859   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21860     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21861   cp_parser_require_pragma_eol (parser, pragma_tok);
21862
21863   finish_omp_flush ();
21864 }
21865
21866 /* Helper function, to parse omp for increment expression.  */
21867
21868 static tree
21869 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21870 {
21871   tree cond = cp_parser_binary_expression (parser, false, true,
21872                                            PREC_NOT_OPERATOR, NULL);
21873   bool overloaded_p;
21874
21875   if (cond == error_mark_node
21876       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21877     {
21878       cp_parser_skip_to_end_of_statement (parser);
21879       return error_mark_node;
21880     }
21881
21882   switch (TREE_CODE (cond))
21883     {
21884     case GT_EXPR:
21885     case GE_EXPR:
21886     case LT_EXPR:
21887     case LE_EXPR:
21888       break;
21889     default:
21890       return error_mark_node;
21891     }
21892
21893   /* If decl is an iterator, preserve LHS and RHS of the relational
21894      expr until finish_omp_for.  */
21895   if (decl
21896       && (type_dependent_expression_p (decl)
21897           || CLASS_TYPE_P (TREE_TYPE (decl))))
21898     return cond;
21899
21900   return build_x_binary_op (TREE_CODE (cond),
21901                             TREE_OPERAND (cond, 0), ERROR_MARK,
21902                             TREE_OPERAND (cond, 1), ERROR_MARK,
21903                             &overloaded_p, tf_warning_or_error);
21904 }
21905
21906 /* Helper function, to parse omp for increment expression.  */
21907
21908 static tree
21909 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21910 {
21911   cp_token *token = cp_lexer_peek_token (parser->lexer);
21912   enum tree_code op;
21913   tree lhs, rhs;
21914   cp_id_kind idk;
21915   bool decl_first;
21916
21917   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21918     {
21919       op = (token->type == CPP_PLUS_PLUS
21920             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21921       cp_lexer_consume_token (parser->lexer);
21922       lhs = cp_parser_cast_expression (parser, false, false, NULL);
21923       if (lhs != decl)
21924         return error_mark_node;
21925       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21926     }
21927
21928   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21929   if (lhs != decl)
21930     return error_mark_node;
21931
21932   token = cp_lexer_peek_token (parser->lexer);
21933   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21934     {
21935       op = (token->type == CPP_PLUS_PLUS
21936             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21937       cp_lexer_consume_token (parser->lexer);
21938       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21939     }
21940
21941   op = cp_parser_assignment_operator_opt (parser);
21942   if (op == ERROR_MARK)
21943     return error_mark_node;
21944
21945   if (op != NOP_EXPR)
21946     {
21947       rhs = cp_parser_assignment_expression (parser, false, NULL);
21948       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21949       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21950     }
21951
21952   lhs = cp_parser_binary_expression (parser, false, false,
21953                                      PREC_ADDITIVE_EXPRESSION, NULL);
21954   token = cp_lexer_peek_token (parser->lexer);
21955   decl_first = lhs == decl;
21956   if (decl_first)
21957     lhs = NULL_TREE;
21958   if (token->type != CPP_PLUS
21959       && token->type != CPP_MINUS)
21960     return error_mark_node;
21961
21962   do
21963     {
21964       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21965       cp_lexer_consume_token (parser->lexer);
21966       rhs = cp_parser_binary_expression (parser, false, false,
21967                                          PREC_ADDITIVE_EXPRESSION, NULL);
21968       token = cp_lexer_peek_token (parser->lexer);
21969       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21970         {
21971           if (lhs == NULL_TREE)
21972             {
21973               if (op == PLUS_EXPR)
21974                 lhs = rhs;
21975               else
21976                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21977             }
21978           else
21979             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21980                                      NULL, tf_warning_or_error);
21981         }
21982     }
21983   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21984
21985   if (!decl_first)
21986     {
21987       if (rhs != decl || op == MINUS_EXPR)
21988         return error_mark_node;
21989       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21990     }
21991   else
21992     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21993
21994   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21995 }
21996
21997 /* Parse the restricted form of the for statement allowed by OpenMP.  */
21998
21999 static tree
22000 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22001 {
22002   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22003   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22004   tree this_pre_body, cl;
22005   location_t loc_first;
22006   bool collapse_err = false;
22007   int i, collapse = 1, nbraces = 0;
22008
22009   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22010     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22011       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22012
22013   gcc_assert (collapse >= 1);
22014
22015   declv = make_tree_vec (collapse);
22016   initv = make_tree_vec (collapse);
22017   condv = make_tree_vec (collapse);
22018   incrv = make_tree_vec (collapse);
22019
22020   loc_first = cp_lexer_peek_token (parser->lexer)->location;
22021
22022   for (i = 0; i < collapse; i++)
22023     {
22024       int bracecount = 0;
22025       bool add_private_clause = false;
22026       location_t loc;
22027
22028       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22029         {
22030           cp_parser_error (parser, "for statement expected");
22031           return NULL;
22032         }
22033       loc = cp_lexer_consume_token (parser->lexer)->location;
22034
22035       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22036         return NULL;
22037
22038       init = decl = real_decl = NULL;
22039       this_pre_body = push_stmt_list ();
22040       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22041         {
22042           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22043
22044              init-expr:
22045                        var = lb
22046                        integer-type var = lb
22047                        random-access-iterator-type var = lb
22048                        pointer-type var = lb
22049           */
22050           cp_decl_specifier_seq type_specifiers;
22051
22052           /* First, try to parse as an initialized declaration.  See
22053              cp_parser_condition, from whence the bulk of this is copied.  */
22054
22055           cp_parser_parse_tentatively (parser);
22056           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
22057                                         &type_specifiers);
22058           if (cp_parser_parse_definitely (parser))
22059             {
22060               /* If parsing a type specifier seq succeeded, then this
22061                  MUST be a initialized declaration.  */
22062               tree asm_specification, attributes;
22063               cp_declarator *declarator;
22064
22065               declarator = cp_parser_declarator (parser,
22066                                                  CP_PARSER_DECLARATOR_NAMED,
22067                                                  /*ctor_dtor_or_conv_p=*/NULL,
22068                                                  /*parenthesized_p=*/NULL,
22069                                                  /*member_p=*/false);
22070               attributes = cp_parser_attributes_opt (parser);
22071               asm_specification = cp_parser_asm_specification_opt (parser);
22072
22073               if (declarator == cp_error_declarator) 
22074                 cp_parser_skip_to_end_of_statement (parser);
22075
22076               else 
22077                 {
22078                   tree pushed_scope, auto_node;
22079
22080                   decl = start_decl (declarator, &type_specifiers,
22081                                      SD_INITIALIZED, attributes,
22082                                      /*prefix_attributes=*/NULL_TREE,
22083                                      &pushed_scope);
22084
22085                   auto_node = type_uses_auto (TREE_TYPE (decl));
22086                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22087                     {
22088                       if (cp_lexer_next_token_is (parser->lexer, 
22089                                                   CPP_OPEN_PAREN))
22090                         error ("parenthesized initialization is not allowed in "
22091                                "OpenMP %<for%> loop");
22092                       else
22093                         /* Trigger an error.  */
22094                         cp_parser_require (parser, CPP_EQ, "%<=%>");
22095
22096                       init = error_mark_node;
22097                       cp_parser_skip_to_end_of_statement (parser);
22098                     }
22099                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
22100                            || type_dependent_expression_p (decl)
22101                            || auto_node)
22102                     {
22103                       bool is_direct_init, is_non_constant_init;
22104
22105                       init = cp_parser_initializer (parser,
22106                                                     &is_direct_init,
22107                                                     &is_non_constant_init);
22108
22109                       if (auto_node && describable_type (init))
22110                         {
22111                           TREE_TYPE (decl)
22112                             = do_auto_deduction (TREE_TYPE (decl), init,
22113                                                  auto_node);
22114
22115                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
22116                               && !type_dependent_expression_p (decl))
22117                             goto non_class;
22118                         }
22119                       
22120                       cp_finish_decl (decl, init, !is_non_constant_init,
22121                                       asm_specification,
22122                                       LOOKUP_ONLYCONVERTING);
22123                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
22124                         {
22125                           for_block
22126                             = tree_cons (NULL, this_pre_body, for_block);
22127                           init = NULL_TREE;
22128                         }
22129                       else
22130                         init = pop_stmt_list (this_pre_body);
22131                       this_pre_body = NULL_TREE;
22132                     }
22133                   else
22134                     {
22135                       /* Consume '='.  */
22136                       cp_lexer_consume_token (parser->lexer);
22137                       init = cp_parser_assignment_expression (parser, false, NULL);
22138
22139                     non_class:
22140                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22141                         init = error_mark_node;
22142                       else
22143                         cp_finish_decl (decl, NULL_TREE,
22144                                         /*init_const_expr_p=*/false,
22145                                         asm_specification,
22146                                         LOOKUP_ONLYCONVERTING);
22147                     }
22148
22149                   if (pushed_scope)
22150                     pop_scope (pushed_scope);
22151                 }
22152             }
22153           else 
22154             {
22155               cp_id_kind idk;
22156               /* If parsing a type specifier sequence failed, then
22157                  this MUST be a simple expression.  */
22158               cp_parser_parse_tentatively (parser);
22159               decl = cp_parser_primary_expression (parser, false, false,
22160                                                    false, &idk);
22161               if (!cp_parser_error_occurred (parser)
22162                   && decl
22163                   && DECL_P (decl)
22164                   && CLASS_TYPE_P (TREE_TYPE (decl)))
22165                 {
22166                   tree rhs;
22167
22168                   cp_parser_parse_definitely (parser);
22169                   cp_parser_require (parser, CPP_EQ, "%<=%>");
22170                   rhs = cp_parser_assignment_expression (parser, false, NULL);
22171                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22172                                                          rhs,
22173                                                          tf_warning_or_error));
22174                   add_private_clause = true;
22175                 }
22176               else
22177                 {
22178                   decl = NULL;
22179                   cp_parser_abort_tentative_parse (parser);
22180                   init = cp_parser_expression (parser, false, NULL);
22181                   if (init)
22182                     {
22183                       if (TREE_CODE (init) == MODIFY_EXPR
22184                           || TREE_CODE (init) == MODOP_EXPR)
22185                         real_decl = TREE_OPERAND (init, 0);
22186                     }
22187                 }
22188             }
22189         }
22190       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22191       if (this_pre_body)
22192         {
22193           this_pre_body = pop_stmt_list (this_pre_body);
22194           if (pre_body)
22195             {
22196               tree t = pre_body;
22197               pre_body = push_stmt_list ();
22198               add_stmt (t);
22199               add_stmt (this_pre_body);
22200               pre_body = pop_stmt_list (pre_body);
22201             }
22202           else
22203             pre_body = this_pre_body;
22204         }
22205
22206       if (decl)
22207         real_decl = decl;
22208       if (par_clauses != NULL && real_decl != NULL_TREE)
22209         {
22210           tree *c;
22211           for (c = par_clauses; *c ; )
22212             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22213                 && OMP_CLAUSE_DECL (*c) == real_decl)
22214               {
22215                 error_at (loc, "iteration variable %qD"
22216                           " should not be firstprivate", real_decl);
22217                 *c = OMP_CLAUSE_CHAIN (*c);
22218               }
22219             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22220                      && OMP_CLAUSE_DECL (*c) == real_decl)
22221               {
22222                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22223                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
22224                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22225                 OMP_CLAUSE_DECL (l) = real_decl;
22226                 OMP_CLAUSE_CHAIN (l) = clauses;
22227                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22228                 clauses = l;
22229                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22230                 CP_OMP_CLAUSE_INFO (*c) = NULL;
22231                 add_private_clause = false;
22232               }
22233             else
22234               {
22235                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22236                     && OMP_CLAUSE_DECL (*c) == real_decl)
22237                   add_private_clause = false;
22238                 c = &OMP_CLAUSE_CHAIN (*c);
22239               }
22240         }
22241
22242       if (add_private_clause)
22243         {
22244           tree c;
22245           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22246             {
22247               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22248                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22249                   && OMP_CLAUSE_DECL (c) == decl)
22250                 break;
22251               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22252                        && OMP_CLAUSE_DECL (c) == decl)
22253                 error_at (loc, "iteration variable %qD "
22254                           "should not be firstprivate",
22255                           decl);
22256               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22257                        && OMP_CLAUSE_DECL (c) == decl)
22258                 error_at (loc, "iteration variable %qD should not be reduction",
22259                           decl);
22260             }
22261           if (c == NULL)
22262             {
22263               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22264               OMP_CLAUSE_DECL (c) = decl;
22265               c = finish_omp_clauses (c);
22266               if (c)
22267                 {
22268                   OMP_CLAUSE_CHAIN (c) = clauses;
22269                   clauses = c;
22270                 }
22271             }
22272         }
22273
22274       cond = NULL;
22275       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22276         cond = cp_parser_omp_for_cond (parser, decl);
22277       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22278
22279       incr = NULL;
22280       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22281         {
22282           /* If decl is an iterator, preserve the operator on decl
22283              until finish_omp_for.  */
22284           if (decl
22285               && (type_dependent_expression_p (decl)
22286                   || CLASS_TYPE_P (TREE_TYPE (decl))))
22287             incr = cp_parser_omp_for_incr (parser, decl);
22288           else
22289             incr = cp_parser_expression (parser, false, NULL);
22290         }
22291
22292       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22293         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22294                                                /*or_comma=*/false,
22295                                                /*consume_paren=*/true);
22296
22297       TREE_VEC_ELT (declv, i) = decl;
22298       TREE_VEC_ELT (initv, i) = init;
22299       TREE_VEC_ELT (condv, i) = cond;
22300       TREE_VEC_ELT (incrv, i) = incr;
22301
22302       if (i == collapse - 1)
22303         break;
22304
22305       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22306          in between the collapsed for loops to be still considered perfectly
22307          nested.  Hopefully the final version clarifies this.
22308          For now handle (multiple) {'s and empty statements.  */
22309       cp_parser_parse_tentatively (parser);
22310       do
22311         {
22312           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22313             break;
22314           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22315             {
22316               cp_lexer_consume_token (parser->lexer);
22317               bracecount++;
22318             }
22319           else if (bracecount
22320                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22321             cp_lexer_consume_token (parser->lexer);
22322           else
22323             {
22324               loc = cp_lexer_peek_token (parser->lexer)->location;
22325               error_at (loc, "not enough collapsed for loops");
22326               collapse_err = true;
22327               cp_parser_abort_tentative_parse (parser);
22328               declv = NULL_TREE;
22329               break;
22330             }
22331         }
22332       while (1);
22333
22334       if (declv)
22335         {
22336           cp_parser_parse_definitely (parser);
22337           nbraces += bracecount;
22338         }
22339     }
22340
22341   /* Note that we saved the original contents of this flag when we entered
22342      the structured block, and so we don't need to re-save it here.  */
22343   parser->in_statement = IN_OMP_FOR;
22344
22345   /* Note that the grammar doesn't call for a structured block here,
22346      though the loop as a whole is a structured block.  */
22347   body = push_stmt_list ();
22348   cp_parser_statement (parser, NULL_TREE, false, NULL);
22349   body = pop_stmt_list (body);
22350
22351   if (declv == NULL_TREE)
22352     ret = NULL_TREE;
22353   else
22354     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22355                           pre_body, clauses);
22356
22357   while (nbraces)
22358     {
22359       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22360         {
22361           cp_lexer_consume_token (parser->lexer);
22362           nbraces--;
22363         }
22364       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22365         cp_lexer_consume_token (parser->lexer);
22366       else
22367         {
22368           if (!collapse_err)
22369             {
22370               error_at (cp_lexer_peek_token (parser->lexer)->location,
22371                         "collapsed loops not perfectly nested");
22372             }
22373           collapse_err = true;
22374           cp_parser_statement_seq_opt (parser, NULL);
22375           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22376         }
22377     }
22378
22379   while (for_block)
22380     {
22381       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22382       for_block = TREE_CHAIN (for_block);
22383     }
22384
22385   return ret;
22386 }
22387
22388 /* OpenMP 2.5:
22389    #pragma omp for for-clause[optseq] new-line
22390      for-loop  */
22391
22392 #define OMP_FOR_CLAUSE_MASK                             \
22393         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22394         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22395         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22396         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22397         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
22398         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
22399         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
22400         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22401
22402 static tree
22403 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22404 {
22405   tree clauses, sb, ret;
22406   unsigned int save;
22407
22408   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22409                                        "#pragma omp for", pragma_tok);
22410
22411   sb = begin_omp_structured_block ();
22412   save = cp_parser_begin_omp_structured_block (parser);
22413
22414   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22415
22416   cp_parser_end_omp_structured_block (parser, save);
22417   add_stmt (finish_omp_structured_block (sb));
22418
22419   return ret;
22420 }
22421
22422 /* OpenMP 2.5:
22423    # pragma omp master new-line
22424      structured-block  */
22425
22426 static tree
22427 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22428 {
22429   cp_parser_require_pragma_eol (parser, pragma_tok);
22430   return c_finish_omp_master (input_location,
22431                               cp_parser_omp_structured_block (parser));
22432 }
22433
22434 /* OpenMP 2.5:
22435    # pragma omp ordered new-line
22436      structured-block  */
22437
22438 static tree
22439 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22440 {
22441   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22442   cp_parser_require_pragma_eol (parser, pragma_tok);
22443   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22444 }
22445
22446 /* OpenMP 2.5:
22447
22448    section-scope:
22449      { section-sequence }
22450
22451    section-sequence:
22452      section-directive[opt] structured-block
22453      section-sequence section-directive structured-block  */
22454
22455 static tree
22456 cp_parser_omp_sections_scope (cp_parser *parser)
22457 {
22458   tree stmt, substmt;
22459   bool error_suppress = false;
22460   cp_token *tok;
22461
22462   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22463     return NULL_TREE;
22464
22465   stmt = push_stmt_list ();
22466
22467   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22468     {
22469       unsigned save;
22470
22471       substmt = begin_omp_structured_block ();
22472       save = cp_parser_begin_omp_structured_block (parser);
22473
22474       while (1)
22475         {
22476           cp_parser_statement (parser, NULL_TREE, false, NULL);
22477
22478           tok = cp_lexer_peek_token (parser->lexer);
22479           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22480             break;
22481           if (tok->type == CPP_CLOSE_BRACE)
22482             break;
22483           if (tok->type == CPP_EOF)
22484             break;
22485         }
22486
22487       cp_parser_end_omp_structured_block (parser, save);
22488       substmt = finish_omp_structured_block (substmt);
22489       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22490       add_stmt (substmt);
22491     }
22492
22493   while (1)
22494     {
22495       tok = cp_lexer_peek_token (parser->lexer);
22496       if (tok->type == CPP_CLOSE_BRACE)
22497         break;
22498       if (tok->type == CPP_EOF)
22499         break;
22500
22501       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22502         {
22503           cp_lexer_consume_token (parser->lexer);
22504           cp_parser_require_pragma_eol (parser, tok);
22505           error_suppress = false;
22506         }
22507       else if (!error_suppress)
22508         {
22509           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22510           error_suppress = true;
22511         }
22512
22513       substmt = cp_parser_omp_structured_block (parser);
22514       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22515       add_stmt (substmt);
22516     }
22517   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22518
22519   substmt = pop_stmt_list (stmt);
22520
22521   stmt = make_node (OMP_SECTIONS);
22522   TREE_TYPE (stmt) = void_type_node;
22523   OMP_SECTIONS_BODY (stmt) = substmt;
22524
22525   add_stmt (stmt);
22526   return stmt;
22527 }
22528
22529 /* OpenMP 2.5:
22530    # pragma omp sections sections-clause[optseq] newline
22531      sections-scope  */
22532
22533 #define OMP_SECTIONS_CLAUSE_MASK                        \
22534         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22535         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22536         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22537         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22538         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22539
22540 static tree
22541 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22542 {
22543   tree clauses, ret;
22544
22545   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22546                                        "#pragma omp sections", pragma_tok);
22547
22548   ret = cp_parser_omp_sections_scope (parser);
22549   if (ret)
22550     OMP_SECTIONS_CLAUSES (ret) = clauses;
22551
22552   return ret;
22553 }
22554
22555 /* OpenMP 2.5:
22556    # pragma parallel parallel-clause new-line
22557    # pragma parallel for parallel-for-clause new-line
22558    # pragma parallel sections parallel-sections-clause new-line  */
22559
22560 #define OMP_PARALLEL_CLAUSE_MASK                        \
22561         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22562         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22563         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22564         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22565         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
22566         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
22567         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22568         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22569
22570 static tree
22571 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22572 {
22573   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22574   const char *p_name = "#pragma omp parallel";
22575   tree stmt, clauses, par_clause, ws_clause, block;
22576   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22577   unsigned int save;
22578   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22579
22580   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22581     {
22582       cp_lexer_consume_token (parser->lexer);
22583       p_kind = PRAGMA_OMP_PARALLEL_FOR;
22584       p_name = "#pragma omp parallel for";
22585       mask |= OMP_FOR_CLAUSE_MASK;
22586       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22587     }
22588   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22589     {
22590       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22591       const char *p = IDENTIFIER_POINTER (id);
22592       if (strcmp (p, "sections") == 0)
22593         {
22594           cp_lexer_consume_token (parser->lexer);
22595           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22596           p_name = "#pragma omp parallel sections";
22597           mask |= OMP_SECTIONS_CLAUSE_MASK;
22598           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22599         }
22600     }
22601
22602   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22603   block = begin_omp_parallel ();
22604   save = cp_parser_begin_omp_structured_block (parser);
22605
22606   switch (p_kind)
22607     {
22608     case PRAGMA_OMP_PARALLEL:
22609       cp_parser_statement (parser, NULL_TREE, false, NULL);
22610       par_clause = clauses;
22611       break;
22612
22613     case PRAGMA_OMP_PARALLEL_FOR:
22614       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22615       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22616       break;
22617
22618     case PRAGMA_OMP_PARALLEL_SECTIONS:
22619       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22620       stmt = cp_parser_omp_sections_scope (parser);
22621       if (stmt)
22622         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22623       break;
22624
22625     default:
22626       gcc_unreachable ();
22627     }
22628
22629   cp_parser_end_omp_structured_block (parser, save);
22630   stmt = finish_omp_parallel (par_clause, block);
22631   if (p_kind != PRAGMA_OMP_PARALLEL)
22632     OMP_PARALLEL_COMBINED (stmt) = 1;
22633   return stmt;
22634 }
22635
22636 /* OpenMP 2.5:
22637    # pragma omp single single-clause[optseq] new-line
22638      structured-block  */
22639
22640 #define OMP_SINGLE_CLAUSE_MASK                          \
22641         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22642         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22643         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
22644         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22645
22646 static tree
22647 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22648 {
22649   tree stmt = make_node (OMP_SINGLE);
22650   TREE_TYPE (stmt) = void_type_node;
22651
22652   OMP_SINGLE_CLAUSES (stmt)
22653     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22654                                  "#pragma omp single", pragma_tok);
22655   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22656
22657   return add_stmt (stmt);
22658 }
22659
22660 /* OpenMP 3.0:
22661    # pragma omp task task-clause[optseq] new-line
22662      structured-block  */
22663
22664 #define OMP_TASK_CLAUSE_MASK                            \
22665         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22666         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
22667         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22668         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22669         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22670         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22671
22672 static tree
22673 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22674 {
22675   tree clauses, block;
22676   unsigned int save;
22677
22678   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22679                                        "#pragma omp task", pragma_tok);
22680   block = begin_omp_task ();
22681   save = cp_parser_begin_omp_structured_block (parser);
22682   cp_parser_statement (parser, NULL_TREE, false, NULL);
22683   cp_parser_end_omp_structured_block (parser, save);
22684   return finish_omp_task (clauses, block);
22685 }
22686
22687 /* OpenMP 3.0:
22688    # pragma omp taskwait new-line  */
22689
22690 static void
22691 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22692 {
22693   cp_parser_require_pragma_eol (parser, pragma_tok);
22694   finish_omp_taskwait ();
22695 }
22696
22697 /* OpenMP 2.5:
22698    # pragma omp threadprivate (variable-list) */
22699
22700 static void
22701 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22702 {
22703   tree vars;
22704
22705   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22706   cp_parser_require_pragma_eol (parser, pragma_tok);
22707
22708   finish_omp_threadprivate (vars);
22709 }
22710
22711 /* Main entry point to OpenMP statement pragmas.  */
22712
22713 static void
22714 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22715 {
22716   tree stmt;
22717
22718   switch (pragma_tok->pragma_kind)
22719     {
22720     case PRAGMA_OMP_ATOMIC:
22721       cp_parser_omp_atomic (parser, pragma_tok);
22722       return;
22723     case PRAGMA_OMP_CRITICAL:
22724       stmt = cp_parser_omp_critical (parser, pragma_tok);
22725       break;
22726     case PRAGMA_OMP_FOR:
22727       stmt = cp_parser_omp_for (parser, pragma_tok);
22728       break;
22729     case PRAGMA_OMP_MASTER:
22730       stmt = cp_parser_omp_master (parser, pragma_tok);
22731       break;
22732     case PRAGMA_OMP_ORDERED:
22733       stmt = cp_parser_omp_ordered (parser, pragma_tok);
22734       break;
22735     case PRAGMA_OMP_PARALLEL:
22736       stmt = cp_parser_omp_parallel (parser, pragma_tok);
22737       break;
22738     case PRAGMA_OMP_SECTIONS:
22739       stmt = cp_parser_omp_sections (parser, pragma_tok);
22740       break;
22741     case PRAGMA_OMP_SINGLE:
22742       stmt = cp_parser_omp_single (parser, pragma_tok);
22743       break;
22744     case PRAGMA_OMP_TASK:
22745       stmt = cp_parser_omp_task (parser, pragma_tok);
22746       break;
22747     default:
22748       gcc_unreachable ();
22749     }
22750
22751   if (stmt)
22752     SET_EXPR_LOCATION (stmt, pragma_tok->location);
22753 }
22754 \f
22755 /* The parser.  */
22756
22757 static GTY (()) cp_parser *the_parser;
22758
22759 \f
22760 /* Special handling for the first token or line in the file.  The first
22761    thing in the file might be #pragma GCC pch_preprocess, which loads a
22762    PCH file, which is a GC collection point.  So we need to handle this
22763    first pragma without benefit of an existing lexer structure.
22764
22765    Always returns one token to the caller in *FIRST_TOKEN.  This is
22766    either the true first token of the file, or the first token after
22767    the initial pragma.  */
22768
22769 static void
22770 cp_parser_initial_pragma (cp_token *first_token)
22771 {
22772   tree name = NULL;
22773
22774   cp_lexer_get_preprocessor_token (NULL, first_token);
22775   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22776     return;
22777
22778   cp_lexer_get_preprocessor_token (NULL, first_token);
22779   if (first_token->type == CPP_STRING)
22780     {
22781       name = first_token->u.value;
22782
22783       cp_lexer_get_preprocessor_token (NULL, first_token);
22784       if (first_token->type != CPP_PRAGMA_EOL)
22785         error_at (first_token->location,
22786                   "junk at end of %<#pragma GCC pch_preprocess%>");
22787     }
22788   else
22789     error_at (first_token->location, "expected string literal");
22790
22791   /* Skip to the end of the pragma.  */
22792   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22793     cp_lexer_get_preprocessor_token (NULL, first_token);
22794
22795   /* Now actually load the PCH file.  */
22796   if (name)
22797     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22798
22799   /* Read one more token to return to our caller.  We have to do this
22800      after reading the PCH file in, since its pointers have to be
22801      live.  */
22802   cp_lexer_get_preprocessor_token (NULL, first_token);
22803 }
22804
22805 /* Normal parsing of a pragma token.  Here we can (and must) use the
22806    regular lexer.  */
22807
22808 static bool
22809 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22810 {
22811   cp_token *pragma_tok;
22812   unsigned int id;
22813
22814   pragma_tok = cp_lexer_consume_token (parser->lexer);
22815   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22816   parser->lexer->in_pragma = true;
22817
22818   id = pragma_tok->pragma_kind;
22819   switch (id)
22820     {
22821     case PRAGMA_GCC_PCH_PREPROCESS:
22822       error_at (pragma_tok->location,
22823                 "%<#pragma GCC pch_preprocess%> must be first");
22824       break;
22825
22826     case PRAGMA_OMP_BARRIER:
22827       switch (context)
22828         {
22829         case pragma_compound:
22830           cp_parser_omp_barrier (parser, pragma_tok);
22831           return false;
22832         case pragma_stmt:
22833           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22834                     "used in compound statements");
22835           break;
22836         default:
22837           goto bad_stmt;
22838         }
22839       break;
22840
22841     case PRAGMA_OMP_FLUSH:
22842       switch (context)
22843         {
22844         case pragma_compound:
22845           cp_parser_omp_flush (parser, pragma_tok);
22846           return false;
22847         case pragma_stmt:
22848           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
22849                     "used in compound statements");
22850           break;
22851         default:
22852           goto bad_stmt;
22853         }
22854       break;
22855
22856     case PRAGMA_OMP_TASKWAIT:
22857       switch (context)
22858         {
22859         case pragma_compound:
22860           cp_parser_omp_taskwait (parser, pragma_tok);
22861           return false;
22862         case pragma_stmt:
22863           error_at (pragma_tok->location,
22864                     "%<#pragma omp taskwait%> may only be "
22865                     "used in compound statements");
22866           break;
22867         default:
22868           goto bad_stmt;
22869         }
22870       break;
22871
22872     case PRAGMA_OMP_THREADPRIVATE:
22873       cp_parser_omp_threadprivate (parser, pragma_tok);
22874       return false;
22875
22876     case PRAGMA_OMP_ATOMIC:
22877     case PRAGMA_OMP_CRITICAL:
22878     case PRAGMA_OMP_FOR:
22879     case PRAGMA_OMP_MASTER:
22880     case PRAGMA_OMP_ORDERED:
22881     case PRAGMA_OMP_PARALLEL:
22882     case PRAGMA_OMP_SECTIONS:
22883     case PRAGMA_OMP_SINGLE:
22884     case PRAGMA_OMP_TASK:
22885       if (context == pragma_external)
22886         goto bad_stmt;
22887       cp_parser_omp_construct (parser, pragma_tok);
22888       return true;
22889
22890     case PRAGMA_OMP_SECTION:
22891       error_at (pragma_tok->location, 
22892                 "%<#pragma omp section%> may only be used in "
22893                 "%<#pragma omp sections%> construct");
22894       break;
22895
22896     default:
22897       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22898       c_invoke_pragma_handler (id);
22899       break;
22900
22901     bad_stmt:
22902       cp_parser_error (parser, "expected declaration specifiers");
22903       break;
22904     }
22905
22906   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22907   return false;
22908 }
22909
22910 /* The interface the pragma parsers have to the lexer.  */
22911
22912 enum cpp_ttype
22913 pragma_lex (tree *value)
22914 {
22915   cp_token *tok;
22916   enum cpp_ttype ret;
22917
22918   tok = cp_lexer_peek_token (the_parser->lexer);
22919
22920   ret = tok->type;
22921   *value = tok->u.value;
22922
22923   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22924     ret = CPP_EOF;
22925   else if (ret == CPP_STRING)
22926     *value = cp_parser_string_literal (the_parser, false, false);
22927   else
22928     {
22929       cp_lexer_consume_token (the_parser->lexer);
22930       if (ret == CPP_KEYWORD)
22931         ret = CPP_NAME;
22932     }
22933
22934   return ret;
22935 }
22936
22937 \f
22938 /* External interface.  */
22939
22940 /* Parse one entire translation unit.  */
22941
22942 void
22943 c_parse_file (void)
22944 {
22945   bool error_occurred;
22946   static bool already_called = false;
22947
22948   if (already_called)
22949     {
22950       sorry ("inter-module optimizations not implemented for C++");
22951       return;
22952     }
22953   already_called = true;
22954
22955   the_parser = cp_parser_new ();
22956   push_deferring_access_checks (flag_access_control
22957                                 ? dk_no_deferred : dk_no_check);
22958   error_occurred = cp_parser_translation_unit (the_parser);
22959   the_parser = NULL;
22960 }
22961
22962 #include "gt-cp-parser.h"