OSDN Git Service

2010-02-10 Shujing Zhao <pearly.zhao@oracle.com>
[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 "intl.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-common.h"
41 #include "plugin.h"
42
43 \f
44 /* The lexer.  */
45
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47    and c-lex.c) and the C++ parser.  */
48
49 /* A token's value and its associated deferred access checks and
50    qualifying scope.  */
51
52 struct GTY(()) tree_check {
53   /* The value associated with the token.  */
54   tree value;
55   /* The checks that have been associated with value.  */
56   VEC (deferred_access_check, gc)* checks;
57   /* The token's qualifying scope (used when it is a
58      CPP_NESTED_NAME_SPECIFIER).  */
59   tree qualifying_scope;
60 };
61
62 /* A C++ token.  */
63
64 typedef struct GTY (()) cp_token {
65   /* The kind of token.  */
66   ENUM_BITFIELD (cpp_ttype) type : 8;
67   /* If this token is a keyword, this value indicates which keyword.
68      Otherwise, this value is RID_MAX.  */
69   ENUM_BITFIELD (rid) keyword : 8;
70   /* Token flags.  */
71   unsigned char flags;
72   /* Identifier for the pragma.  */
73   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74   /* True if this token is from a context where it is implicitly extern "C" */
75   BOOL_BITFIELD implicit_extern_c : 1;
76   /* True for a CPP_NAME token that is not a keyword (i.e., for which
77      KEYWORD is RID_MAX) iff this name was looked up and found to be
78      ambiguous.  An error has already been reported.  */
79   BOOL_BITFIELD ambiguous_p : 1;
80   /* The location at which this token was found.  */
81   location_t location;
82   /* The value associated with this token, if any.  */
83   union cp_token_value {
84     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
85     struct tree_check* GTY((tag ("1"))) tree_check_value;
86     /* Use for all other tokens.  */
87     tree GTY((tag ("0"))) value;
88   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89 } cp_token;
90
91 /* We use a stack of token pointer for saving token sets.  */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
95
96 static cp_token eof_token =
97 {
98   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
99 };
100
101 /* The cp_lexer structure represents the C++ lexer.  It is responsible
102    for managing the token stream from the preprocessor and supplying
103    it to the parser.  Tokens are never added to the cp_lexer after
104    it is created.  */
105
106 typedef struct GTY (()) cp_lexer {
107   /* The memory allocated for the buffer.  NULL if this lexer does not
108      own the token buffer.  */
109   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
110   /* If the lexer owns the buffer, this is the number of tokens in the
111      buffer.  */
112   size_t buffer_length;
113
114   /* A pointer just past the last available token.  The tokens
115      in this lexer are [buffer, last_token).  */
116   cp_token_position GTY ((skip)) last_token;
117
118   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
119      no more available tokens.  */
120   cp_token_position GTY ((skip)) next_token;
121
122   /* A stack indicating positions at which cp_lexer_save_tokens was
123      called.  The top entry is the most recent position at which we
124      began saving tokens.  If the stack is non-empty, we are saving
125      tokens.  */
126   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
127
128   /* The next lexer in a linked list of lexers.  */
129   struct cp_lexer *next;
130
131   /* True if we should output debugging information.  */
132   bool debugging_p;
133
134   /* True if we're in the context of parsing a pragma, and should not
135      increment past the end-of-line marker.  */
136   bool in_pragma;
137 } cp_lexer;
138
139 /* cp_token_cache is a range of tokens.  There is no need to represent
140    allocate heap memory for it, since tokens are never removed from the
141    lexer's array.  There is also no need for the GC to walk through
142    a cp_token_cache, since everything in here is referenced through
143    a lexer.  */
144
145 typedef struct GTY(()) cp_token_cache {
146   /* The beginning of the token range.  */
147   cp_token * GTY((skip)) first;
148
149   /* Points immediately after the last token in the range.  */
150   cp_token * GTY ((skip)) last;
151 } cp_token_cache;
152
153 /* Prototypes.  */
154
155 static cp_lexer *cp_lexer_new_main
156   (void);
157 static cp_lexer *cp_lexer_new_from_tokens
158   (cp_token_cache *tokens);
159 static void cp_lexer_destroy
160   (cp_lexer *);
161 static int cp_lexer_saving_tokens
162   (const cp_lexer *);
163 static cp_token_position cp_lexer_token_position
164   (cp_lexer *, bool);
165 static cp_token *cp_lexer_token_at
166   (cp_lexer *, cp_token_position);
167 static void cp_lexer_get_preprocessor_token
168   (cp_lexer *, cp_token *);
169 static inline cp_token *cp_lexer_peek_token
170   (cp_lexer *);
171 static cp_token *cp_lexer_peek_nth_token
172   (cp_lexer *, size_t);
173 static inline bool cp_lexer_next_token_is
174   (cp_lexer *, enum cpp_ttype);
175 static bool cp_lexer_next_token_is_not
176   (cp_lexer *, enum cpp_ttype);
177 static bool cp_lexer_next_token_is_keyword
178   (cp_lexer *, enum rid);
179 static cp_token *cp_lexer_consume_token
180   (cp_lexer *);
181 static void cp_lexer_purge_token
182   (cp_lexer *);
183 static void cp_lexer_purge_tokens_after
184   (cp_lexer *, cp_token_position);
185 static void cp_lexer_save_tokens
186   (cp_lexer *);
187 static void cp_lexer_commit_tokens
188   (cp_lexer *);
189 static void cp_lexer_rollback_tokens
190   (cp_lexer *);
191 #ifdef ENABLE_CHECKING
192 static void cp_lexer_print_token
193   (FILE *, cp_token *);
194 static inline bool cp_lexer_debugging_p
195   (cp_lexer *);
196 static void cp_lexer_start_debugging
197   (cp_lexer *) ATTRIBUTE_UNUSED;
198 static void cp_lexer_stop_debugging
199   (cp_lexer *) ATTRIBUTE_UNUSED;
200 #else
201 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
202    about passing NULL to functions that require non-NULL arguments
203    (fputs, fprintf).  It will never be used, so all we need is a value
204    of the right type that's guaranteed not to be NULL.  */
205 #define cp_lexer_debug_stream stdout
206 #define cp_lexer_print_token(str, tok) (void) 0
207 #define cp_lexer_debugging_p(lexer) 0
208 #endif /* ENABLE_CHECKING */
209
210 static cp_token_cache *cp_token_cache_new
211   (cp_token *, cp_token *);
212
213 static void cp_parser_initial_pragma
214   (cp_token *);
215
216 /* Manifest constants.  */
217 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
218 #define CP_SAVED_TOKEN_STACK 5
219
220 /* A token type for keywords, as opposed to ordinary identifiers.  */
221 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
222
223 /* A token type for template-ids.  If a template-id is processed while
224    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
225    the value of the CPP_TEMPLATE_ID is whatever was returned by
226    cp_parser_template_id.  */
227 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
228
229 /* A token type for nested-name-specifiers.  If a
230    nested-name-specifier is processed while parsing tentatively, it is
231    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
232    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
233    cp_parser_nested_name_specifier_opt.  */
234 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
235
236 /* A token type for tokens that are not tokens at all; these are used
237    to represent slots in the array where there used to be a token
238    that has now been deleted.  */
239 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
240
241 /* The number of token types, including C++-specific ones.  */
242 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
243
244 /* Variables.  */
245
246 #ifdef ENABLE_CHECKING
247 /* The stream to which debugging output should be written.  */
248 static FILE *cp_lexer_debug_stream;
249 #endif /* ENABLE_CHECKING */
250
251 /* Nonzero if we are parsing an unevaluated operand: an operand to
252    sizeof, typeof, or alignof.  */
253 int cp_unevaluated_operand;
254
255 /* Create a new main C++ lexer, the lexer that gets tokens from the
256    preprocessor.  */
257
258 static cp_lexer *
259 cp_lexer_new_main (void)
260 {
261   cp_token first_token;
262   cp_lexer *lexer;
263   cp_token *pos;
264   size_t alloc;
265   size_t space;
266   cp_token *buffer;
267
268   /* It's possible that parsing the first pragma will load a PCH file,
269      which is a GC collection point.  So we have to do that before
270      allocating any memory.  */
271   cp_parser_initial_pragma (&first_token);
272
273   c_common_no_more_pch ();
274
275   /* Allocate the memory.  */
276   lexer = GGC_CNEW (cp_lexer);
277
278 #ifdef ENABLE_CHECKING
279   /* Initially we are not debugging.  */
280   lexer->debugging_p = false;
281 #endif /* ENABLE_CHECKING */
282   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
283                                    CP_SAVED_TOKEN_STACK);
284
285   /* Create the buffer.  */
286   alloc = CP_LEXER_BUFFER_SIZE;
287   buffer = GGC_NEWVEC (cp_token, alloc);
288
289   /* Put the first token in the buffer.  */
290   space = alloc;
291   pos = buffer;
292   *pos = first_token;
293
294   /* Get the remaining tokens from the preprocessor.  */
295   while (pos->type != CPP_EOF)
296     {
297       pos++;
298       if (!--space)
299         {
300           space = alloc;
301           alloc *= 2;
302           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
303           pos = buffer + space;
304         }
305       cp_lexer_get_preprocessor_token (lexer, pos);
306     }
307   lexer->buffer = buffer;
308   lexer->buffer_length = alloc - space;
309   lexer->last_token = pos;
310   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
311
312   /* Subsequent preprocessor diagnostics should use compiler
313      diagnostic functions to get the compiler source location.  */
314   done_lexing = true;
315
316   gcc_assert (lexer->next_token->type != CPP_PURGED);
317   return lexer;
318 }
319
320 /* Create a new lexer whose token stream is primed with the tokens in
321    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
322
323 static cp_lexer *
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
325 {
326   cp_token *first = cache->first;
327   cp_token *last = cache->last;
328   cp_lexer *lexer = GGC_CNEW (cp_lexer);
329
330   /* We do not own the buffer.  */
331   lexer->buffer = NULL;
332   lexer->buffer_length = 0;
333   lexer->next_token = first == last ? &eof_token : first;
334   lexer->last_token = last;
335
336   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337                                    CP_SAVED_TOKEN_STACK);
338
339 #ifdef ENABLE_CHECKING
340   /* Initially we are not debugging.  */
341   lexer->debugging_p = false;
342 #endif
343
344   gcc_assert (lexer->next_token->type != CPP_PURGED);
345   return lexer;
346 }
347
348 /* Frees all resources associated with LEXER.  */
349
350 static void
351 cp_lexer_destroy (cp_lexer *lexer)
352 {
353   if (lexer->buffer)
354     ggc_free (lexer->buffer);
355   VEC_free (cp_token_position, heap, lexer->saved_tokens);
356   ggc_free (lexer);
357 }
358
359 /* Returns nonzero if debugging information should be output.  */
360
361 #ifdef ENABLE_CHECKING
362
363 static inline bool
364 cp_lexer_debugging_p (cp_lexer *lexer)
365 {
366   return lexer->debugging_p;
367 }
368
369 #endif /* ENABLE_CHECKING */
370
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373 {
374   gcc_assert (!previous_p || lexer->next_token != &eof_token);
375
376   return lexer->next_token - previous_p;
377 }
378
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
381 {
382   return pos;
383 }
384
385 /* nonzero if we are presently saving tokens.  */
386
387 static inline int
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
389 {
390   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
391 }
392
393 /* Store the next token from the preprocessor in *TOKEN.  Return true
394    if we reach EOF.  If LEXER is NULL, assume we are handling an
395    initial #pragma pch_preprocess, and thus want the lexer to return
396    processed strings.  */
397
398 static void
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400 {
401   static int is_extern_c = 0;
402
403    /* Get a new token from the preprocessor.  */
404   token->type
405     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
407   token->keyword = RID_MAX;
408   token->pragma_kind = PRAGMA_NONE;
409
410   /* On some systems, some header files are surrounded by an
411      implicit extern "C" block.  Set a flag in the token if it
412      comes from such a header.  */
413   is_extern_c += pending_lang_change;
414   pending_lang_change = 0;
415   token->implicit_extern_c = is_extern_c > 0;
416
417   /* Check to see if this token is a keyword.  */
418   if (token->type == CPP_NAME)
419     {
420       if (C_IS_RESERVED_WORD (token->u.value))
421         {
422           /* Mark this token as a keyword.  */
423           token->type = CPP_KEYWORD;
424           /* Record which keyword.  */
425           token->keyword = C_RID_CODE (token->u.value);
426         }
427       else
428         {
429           if (warn_cxx0x_compat
430               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
431               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
432             {
433               /* Warn about the C++0x keyword (but still treat it as
434                  an identifier).  */
435               warning (OPT_Wc__0x_compat, 
436                        "identifier %qE will become a keyword in C++0x",
437                        token->u.value);
438
439               /* Clear out the C_RID_CODE so we don't warn about this
440                  particular identifier-turned-keyword again.  */
441               C_SET_RID_CODE (token->u.value, RID_MAX);
442             }
443
444           token->ambiguous_p = false;
445           token->keyword = RID_MAX;
446         }
447     }
448   /* Handle Objective-C++ keywords.  */
449   else if (token->type == CPP_AT_NAME)
450     {
451       token->type = CPP_KEYWORD;
452       switch (C_RID_CODE (token->u.value))
453         {
454         /* Map 'class' to '@class', 'private' to '@private', etc.  */
455         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
456         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
457         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
458         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
459         case RID_THROW: token->keyword = RID_AT_THROW; break;
460         case RID_TRY: token->keyword = RID_AT_TRY; break;
461         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
462         default: token->keyword = C_RID_CODE (token->u.value);
463         }
464     }
465   else if (token->type == CPP_PRAGMA)
466     {
467       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
468       token->pragma_kind = ((enum pragma_kind)
469                             TREE_INT_CST_LOW (token->u.value));
470       token->u.value = NULL_TREE;
471     }
472 }
473
474 /* Update the globals input_location and the input file stack from TOKEN.  */
475 static inline void
476 cp_lexer_set_source_position_from_token (cp_token *token)
477 {
478   if (token->type != CPP_EOF)
479     {
480       input_location = token->location;
481     }
482 }
483
484 /* Return a pointer to the next token in the token stream, but do not
485    consume it.  */
486
487 static inline cp_token *
488 cp_lexer_peek_token (cp_lexer *lexer)
489 {
490   if (cp_lexer_debugging_p (lexer))
491     {
492       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
493       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
494       putc ('\n', cp_lexer_debug_stream);
495     }
496   return lexer->next_token;
497 }
498
499 /* Return true if the next token has the indicated TYPE.  */
500
501 static inline bool
502 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
503 {
504   return cp_lexer_peek_token (lexer)->type == type;
505 }
506
507 /* Return true if the next token does not have the indicated TYPE.  */
508
509 static inline bool
510 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
511 {
512   return !cp_lexer_next_token_is (lexer, type);
513 }
514
515 /* Return true if the next token is the indicated KEYWORD.  */
516
517 static inline bool
518 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
519 {
520   return cp_lexer_peek_token (lexer)->keyword == keyword;
521 }
522
523 /* Return true if the next token is not the indicated KEYWORD.  */
524
525 static inline bool
526 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
527 {
528   return cp_lexer_peek_token (lexer)->keyword != keyword;
529 }
530
531 /* Return true if the next token is a keyword for a decl-specifier.  */
532
533 static bool
534 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
535 {
536   cp_token *token;
537
538   token = cp_lexer_peek_token (lexer);
539   switch (token->keyword) 
540     {
541       /* auto specifier: storage-class-specifier in C++,
542          simple-type-specifier in C++0x.  */
543     case RID_AUTO:
544       /* Storage classes.  */
545     case RID_REGISTER:
546     case RID_STATIC:
547     case RID_EXTERN:
548     case RID_MUTABLE:
549     case RID_THREAD:
550       /* Elaborated type specifiers.  */
551     case RID_ENUM:
552     case RID_CLASS:
553     case RID_STRUCT:
554     case RID_UNION:
555     case RID_TYPENAME:
556       /* Simple type specifiers.  */
557     case RID_CHAR:
558     case RID_CHAR16:
559     case RID_CHAR32:
560     case RID_WCHAR:
561     case RID_BOOL:
562     case RID_SHORT:
563     case RID_INT:
564     case RID_LONG:
565     case RID_SIGNED:
566     case RID_UNSIGNED:
567     case RID_FLOAT:
568     case RID_DOUBLE:
569     case RID_VOID:
570       /* GNU extensions.  */ 
571     case RID_ATTRIBUTE:
572     case RID_TYPEOF:
573       /* C++0x extensions.  */
574     case RID_DECLTYPE:
575       return true;
576
577     default:
578       return false;
579     }
580 }
581
582 /* Return a pointer to the Nth token in the token stream.  If N is 1,
583    then this is precisely equivalent to cp_lexer_peek_token (except
584    that it is not inline).  One would like to disallow that case, but
585    there is one case (cp_parser_nth_token_starts_template_id) where
586    the caller passes a variable for N and it might be 1.  */
587
588 static cp_token *
589 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
590 {
591   cp_token *token;
592
593   /* N is 1-based, not zero-based.  */
594   gcc_assert (n > 0);
595
596   if (cp_lexer_debugging_p (lexer))
597     fprintf (cp_lexer_debug_stream,
598              "cp_lexer: peeking ahead %ld at token: ", (long)n);
599
600   --n;
601   token = lexer->next_token;
602   gcc_assert (!n || token != &eof_token);
603   while (n != 0)
604     {
605       ++token;
606       if (token == lexer->last_token)
607         {
608           token = &eof_token;
609           break;
610         }
611
612       if (token->type != CPP_PURGED)
613         --n;
614     }
615
616   if (cp_lexer_debugging_p (lexer))
617     {
618       cp_lexer_print_token (cp_lexer_debug_stream, token);
619       putc ('\n', cp_lexer_debug_stream);
620     }
621
622   return token;
623 }
624
625 /* Return the next token, and advance the lexer's next_token pointer
626    to point to the next non-purged token.  */
627
628 static cp_token *
629 cp_lexer_consume_token (cp_lexer* lexer)
630 {
631   cp_token *token = lexer->next_token;
632
633   gcc_assert (token != &eof_token);
634   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
635
636   do
637     {
638       lexer->next_token++;
639       if (lexer->next_token == lexer->last_token)
640         {
641           lexer->next_token = &eof_token;
642           break;
643         }
644
645     }
646   while (lexer->next_token->type == CPP_PURGED);
647
648   cp_lexer_set_source_position_from_token (token);
649
650   /* Provide debugging output.  */
651   if (cp_lexer_debugging_p (lexer))
652     {
653       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
654       cp_lexer_print_token (cp_lexer_debug_stream, token);
655       putc ('\n', cp_lexer_debug_stream);
656     }
657
658   return token;
659 }
660
661 /* Permanently remove the next token from the token stream, and
662    advance the next_token pointer to refer to the next non-purged
663    token.  */
664
665 static void
666 cp_lexer_purge_token (cp_lexer *lexer)
667 {
668   cp_token *tok = lexer->next_token;
669
670   gcc_assert (tok != &eof_token);
671   tok->type = CPP_PURGED;
672   tok->location = UNKNOWN_LOCATION;
673   tok->u.value = NULL_TREE;
674   tok->keyword = RID_MAX;
675
676   do
677     {
678       tok++;
679       if (tok == lexer->last_token)
680         {
681           tok = &eof_token;
682           break;
683         }
684     }
685   while (tok->type == CPP_PURGED);
686   lexer->next_token = tok;
687 }
688
689 /* Permanently remove all tokens after TOK, up to, but not
690    including, the token that will be returned next by
691    cp_lexer_peek_token.  */
692
693 static void
694 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
695 {
696   cp_token *peek = lexer->next_token;
697
698   if (peek == &eof_token)
699     peek = lexer->last_token;
700
701   gcc_assert (tok < peek);
702
703   for ( tok += 1; tok != peek; tok += 1)
704     {
705       tok->type = CPP_PURGED;
706       tok->location = UNKNOWN_LOCATION;
707       tok->u.value = NULL_TREE;
708       tok->keyword = RID_MAX;
709     }
710 }
711
712 /* Begin saving tokens.  All tokens consumed after this point will be
713    preserved.  */
714
715 static void
716 cp_lexer_save_tokens (cp_lexer* lexer)
717 {
718   /* Provide debugging output.  */
719   if (cp_lexer_debugging_p (lexer))
720     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
721
722   VEC_safe_push (cp_token_position, heap,
723                  lexer->saved_tokens, lexer->next_token);
724 }
725
726 /* Commit to the portion of the token stream most recently saved.  */
727
728 static void
729 cp_lexer_commit_tokens (cp_lexer* lexer)
730 {
731   /* Provide debugging output.  */
732   if (cp_lexer_debugging_p (lexer))
733     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
734
735   VEC_pop (cp_token_position, lexer->saved_tokens);
736 }
737
738 /* Return all tokens saved since the last call to cp_lexer_save_tokens
739    to the token stream.  Stop saving tokens.  */
740
741 static void
742 cp_lexer_rollback_tokens (cp_lexer* lexer)
743 {
744   /* Provide debugging output.  */
745   if (cp_lexer_debugging_p (lexer))
746     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
747
748   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
749 }
750
751 /* Print a representation of the TOKEN on the STREAM.  */
752
753 #ifdef ENABLE_CHECKING
754
755 static void
756 cp_lexer_print_token (FILE * stream, cp_token *token)
757 {
758   /* We don't use cpp_type2name here because the parser defines
759      a few tokens of its own.  */
760   static const char *const token_names[] = {
761     /* cpplib-defined token types */
762 #define OP(e, s) #e,
763 #define TK(e, s) #e,
764     TTYPE_TABLE
765 #undef OP
766 #undef TK
767     /* C++ parser token types - see "Manifest constants", above.  */
768     "KEYWORD",
769     "TEMPLATE_ID",
770     "NESTED_NAME_SPECIFIER",
771     "PURGED"
772   };
773
774   /* If we have a name for the token, print it out.  Otherwise, we
775      simply give the numeric code.  */
776   gcc_assert (token->type < ARRAY_SIZE(token_names));
777   fputs (token_names[token->type], stream);
778
779   /* For some tokens, print the associated data.  */
780   switch (token->type)
781     {
782     case CPP_KEYWORD:
783       /* Some keywords have a value that is not an IDENTIFIER_NODE.
784          For example, `struct' is mapped to an INTEGER_CST.  */
785       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
786         break;
787       /* else fall through */
788     case CPP_NAME:
789       fputs (IDENTIFIER_POINTER (token->u.value), stream);
790       break;
791
792     case CPP_STRING:
793     case CPP_STRING16:
794     case CPP_STRING32:
795     case CPP_WSTRING:
796     case CPP_UTF8STRING:
797       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
798       break;
799
800     default:
801       break;
802     }
803 }
804
805 /* Start emitting debugging information.  */
806
807 static void
808 cp_lexer_start_debugging (cp_lexer* lexer)
809 {
810   lexer->debugging_p = true;
811 }
812
813 /* Stop emitting debugging information.  */
814
815 static void
816 cp_lexer_stop_debugging (cp_lexer* lexer)
817 {
818   lexer->debugging_p = false;
819 }
820
821 #endif /* ENABLE_CHECKING */
822
823 /* Create a new cp_token_cache, representing a range of tokens.  */
824
825 static cp_token_cache *
826 cp_token_cache_new (cp_token *first, cp_token *last)
827 {
828   cp_token_cache *cache = GGC_NEW (cp_token_cache);
829   cache->first = first;
830   cache->last = last;
831   return cache;
832 }
833
834 \f
835 /* Decl-specifiers.  */
836
837 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
838
839 static void
840 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
841 {
842   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
843 }
844
845 /* Declarators.  */
846
847 /* Nothing other than the parser should be creating declarators;
848    declarators are a semi-syntactic representation of C++ entities.
849    Other parts of the front end that need to create entities (like
850    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
851
852 static cp_declarator *make_call_declarator
853   (cp_declarator *, tree, cp_cv_quals, tree, tree);
854 static cp_declarator *make_array_declarator
855   (cp_declarator *, tree);
856 static cp_declarator *make_pointer_declarator
857   (cp_cv_quals, cp_declarator *);
858 static cp_declarator *make_reference_declarator
859   (cp_cv_quals, cp_declarator *, bool);
860 static cp_parameter_declarator *make_parameter_declarator
861   (cp_decl_specifier_seq *, cp_declarator *, tree);
862 static cp_declarator *make_ptrmem_declarator
863   (cp_cv_quals, tree, cp_declarator *);
864
865 /* An erroneous declarator.  */
866 static cp_declarator *cp_error_declarator;
867
868 /* The obstack on which declarators and related data structures are
869    allocated.  */
870 static struct obstack declarator_obstack;
871
872 /* Alloc BYTES from the declarator memory pool.  */
873
874 static inline void *
875 alloc_declarator (size_t bytes)
876 {
877   return obstack_alloc (&declarator_obstack, bytes);
878 }
879
880 /* Allocate a declarator of the indicated KIND.  Clear fields that are
881    common to all declarators.  */
882
883 static cp_declarator *
884 make_declarator (cp_declarator_kind kind)
885 {
886   cp_declarator *declarator;
887
888   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
889   declarator->kind = kind;
890   declarator->attributes = NULL_TREE;
891   declarator->declarator = NULL;
892   declarator->parameter_pack_p = false;
893
894   return declarator;
895 }
896
897 /* Make a declarator for a generalized identifier.  If
898    QUALIFYING_SCOPE is non-NULL, the identifier is
899    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
900    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
901    is, if any.   */
902
903 static cp_declarator *
904 make_id_declarator (tree qualifying_scope, tree unqualified_name,
905                     special_function_kind sfk)
906 {
907   cp_declarator *declarator;
908
909   /* It is valid to write:
910
911        class C { void f(); };
912        typedef C D;
913        void D::f();
914
915      The standard is not clear about whether `typedef const C D' is
916      legal; as of 2002-09-15 the committee is considering that
917      question.  EDG 3.0 allows that syntax.  Therefore, we do as
918      well.  */
919   if (qualifying_scope && TYPE_P (qualifying_scope))
920     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
921
922   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
923               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
924               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
925
926   declarator = make_declarator (cdk_id);
927   declarator->u.id.qualifying_scope = qualifying_scope;
928   declarator->u.id.unqualified_name = unqualified_name;
929   declarator->u.id.sfk = sfk;
930   
931   return declarator;
932 }
933
934 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
935    of modifiers such as const or volatile to apply to the pointer
936    type, represented as identifiers.  */
937
938 cp_declarator *
939 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
940 {
941   cp_declarator *declarator;
942
943   declarator = make_declarator (cdk_pointer);
944   declarator->declarator = target;
945   declarator->u.pointer.qualifiers = cv_qualifiers;
946   declarator->u.pointer.class_type = NULL_TREE;
947   if (target)
948     {
949       declarator->parameter_pack_p = target->parameter_pack_p;
950       target->parameter_pack_p = false;
951     }
952   else
953     declarator->parameter_pack_p = false;
954
955   return declarator;
956 }
957
958 /* Like make_pointer_declarator -- but for references.  */
959
960 cp_declarator *
961 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
962                            bool rvalue_ref)
963 {
964   cp_declarator *declarator;
965
966   declarator = make_declarator (cdk_reference);
967   declarator->declarator = target;
968   declarator->u.reference.qualifiers = cv_qualifiers;
969   declarator->u.reference.rvalue_ref = rvalue_ref;
970   if (target)
971     {
972       declarator->parameter_pack_p = target->parameter_pack_p;
973       target->parameter_pack_p = false;
974     }
975   else
976     declarator->parameter_pack_p = false;
977
978   return declarator;
979 }
980
981 /* Like make_pointer_declarator -- but for a pointer to a non-static
982    member of CLASS_TYPE.  */
983
984 cp_declarator *
985 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
986                         cp_declarator *pointee)
987 {
988   cp_declarator *declarator;
989
990   declarator = make_declarator (cdk_ptrmem);
991   declarator->declarator = pointee;
992   declarator->u.pointer.qualifiers = cv_qualifiers;
993   declarator->u.pointer.class_type = class_type;
994
995   if (pointee)
996     {
997       declarator->parameter_pack_p = pointee->parameter_pack_p;
998       pointee->parameter_pack_p = false;
999     }
1000   else
1001     declarator->parameter_pack_p = false;
1002
1003   return declarator;
1004 }
1005
1006 /* Make a declarator for the function given by TARGET, with the
1007    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1008    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1009    indicates what exceptions can be thrown.  */
1010
1011 cp_declarator *
1012 make_call_declarator (cp_declarator *target,
1013                       tree parms,
1014                       cp_cv_quals cv_qualifiers,
1015                       tree exception_specification,
1016                       tree late_return_type)
1017 {
1018   cp_declarator *declarator;
1019
1020   declarator = make_declarator (cdk_function);
1021   declarator->declarator = target;
1022   declarator->u.function.parameters = parms;
1023   declarator->u.function.qualifiers = cv_qualifiers;
1024   declarator->u.function.exception_specification = exception_specification;
1025   declarator->u.function.late_return_type = late_return_type;
1026   if (target)
1027     {
1028       declarator->parameter_pack_p = target->parameter_pack_p;
1029       target->parameter_pack_p = false;
1030     }
1031   else
1032     declarator->parameter_pack_p = false;
1033
1034   return declarator;
1035 }
1036
1037 /* Make a declarator for an array of BOUNDS elements, each of which is
1038    defined by ELEMENT.  */
1039
1040 cp_declarator *
1041 make_array_declarator (cp_declarator *element, tree bounds)
1042 {
1043   cp_declarator *declarator;
1044
1045   declarator = make_declarator (cdk_array);
1046   declarator->declarator = element;
1047   declarator->u.array.bounds = bounds;
1048   if (element)
1049     {
1050       declarator->parameter_pack_p = element->parameter_pack_p;
1051       element->parameter_pack_p = false;
1052     }
1053   else
1054     declarator->parameter_pack_p = false;
1055
1056   return declarator;
1057 }
1058
1059 /* Determine whether the declarator we've seen so far can be a
1060    parameter pack, when followed by an ellipsis.  */
1061 static bool 
1062 declarator_can_be_parameter_pack (cp_declarator *declarator)
1063 {
1064   /* Search for a declarator name, or any other declarator that goes
1065      after the point where the ellipsis could appear in a parameter
1066      pack. If we find any of these, then this declarator can not be
1067      made into a parameter pack.  */
1068   bool found = false;
1069   while (declarator && !found)
1070     {
1071       switch ((int)declarator->kind)
1072         {
1073         case cdk_id:
1074         case cdk_array:
1075           found = true;
1076           break;
1077
1078         case cdk_error:
1079           return true;
1080
1081         default:
1082           declarator = declarator->declarator;
1083           break;
1084         }
1085     }
1086
1087   return !found;
1088 }
1089
1090 cp_parameter_declarator *no_parameters;
1091
1092 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1093    DECLARATOR and DEFAULT_ARGUMENT.  */
1094
1095 cp_parameter_declarator *
1096 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1097                            cp_declarator *declarator,
1098                            tree default_argument)
1099 {
1100   cp_parameter_declarator *parameter;
1101
1102   parameter = ((cp_parameter_declarator *)
1103                alloc_declarator (sizeof (cp_parameter_declarator)));
1104   parameter->next = NULL;
1105   if (decl_specifiers)
1106     parameter->decl_specifiers = *decl_specifiers;
1107   else
1108     clear_decl_specs (&parameter->decl_specifiers);
1109   parameter->declarator = declarator;
1110   parameter->default_argument = default_argument;
1111   parameter->ellipsis_p = false;
1112
1113   return parameter;
1114 }
1115
1116 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1117
1118 static bool
1119 function_declarator_p (const cp_declarator *declarator)
1120 {
1121   while (declarator)
1122     {
1123       if (declarator->kind == cdk_function
1124           && declarator->declarator->kind == cdk_id)
1125         return true;
1126       if (declarator->kind == cdk_id
1127           || declarator->kind == cdk_error)
1128         return false;
1129       declarator = declarator->declarator;
1130     }
1131   return false;
1132 }
1133  
1134 /* The parser.  */
1135
1136 /* Overview
1137    --------
1138
1139    A cp_parser parses the token stream as specified by the C++
1140    grammar.  Its job is purely parsing, not semantic analysis.  For
1141    example, the parser breaks the token stream into declarators,
1142    expressions, statements, and other similar syntactic constructs.
1143    It does not check that the types of the expressions on either side
1144    of an assignment-statement are compatible, or that a function is
1145    not declared with a parameter of type `void'.
1146
1147    The parser invokes routines elsewhere in the compiler to perform
1148    semantic analysis and to build up the abstract syntax tree for the
1149    code processed.
1150
1151    The parser (and the template instantiation code, which is, in a
1152    way, a close relative of parsing) are the only parts of the
1153    compiler that should be calling push_scope and pop_scope, or
1154    related functions.  The parser (and template instantiation code)
1155    keeps track of what scope is presently active; everything else
1156    should simply honor that.  (The code that generates static
1157    initializers may also need to set the scope, in order to check
1158    access control correctly when emitting the initializers.)
1159
1160    Methodology
1161    -----------
1162
1163    The parser is of the standard recursive-descent variety.  Upcoming
1164    tokens in the token stream are examined in order to determine which
1165    production to use when parsing a non-terminal.  Some C++ constructs
1166    require arbitrary look ahead to disambiguate.  For example, it is
1167    impossible, in the general case, to tell whether a statement is an
1168    expression or declaration without scanning the entire statement.
1169    Therefore, the parser is capable of "parsing tentatively."  When the
1170    parser is not sure what construct comes next, it enters this mode.
1171    Then, while we attempt to parse the construct, the parser queues up
1172    error messages, rather than issuing them immediately, and saves the
1173    tokens it consumes.  If the construct is parsed successfully, the
1174    parser "commits", i.e., it issues any queued error messages and
1175    the tokens that were being preserved are permanently discarded.
1176    If, however, the construct is not parsed successfully, the parser
1177    rolls back its state completely so that it can resume parsing using
1178    a different alternative.
1179
1180    Future Improvements
1181    -------------------
1182
1183    The performance of the parser could probably be improved substantially.
1184    We could often eliminate the need to parse tentatively by looking ahead
1185    a little bit.  In some places, this approach might not entirely eliminate
1186    the need to parse tentatively, but it might still speed up the average
1187    case.  */
1188
1189 /* Flags that are passed to some parsing functions.  These values can
1190    be bitwise-ored together.  */
1191
1192 enum
1193 {
1194   /* No flags.  */
1195   CP_PARSER_FLAGS_NONE = 0x0,
1196   /* The construct is optional.  If it is not present, then no error
1197      should be issued.  */
1198   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1199   /* When parsing a type-specifier, treat user-defined type-names
1200      as non-type identifiers.  */
1201   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1202   /* When parsing a type-specifier, do not try to parse a class-specifier
1203      or enum-specifier.  */
1204   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1205 };
1206
1207 /* This type is used for parameters and variables which hold
1208    combinations of the above flags.  */
1209 typedef int cp_parser_flags;
1210
1211 /* The different kinds of declarators we want to parse.  */
1212
1213 typedef enum cp_parser_declarator_kind
1214 {
1215   /* We want an abstract declarator.  */
1216   CP_PARSER_DECLARATOR_ABSTRACT,
1217   /* We want a named declarator.  */
1218   CP_PARSER_DECLARATOR_NAMED,
1219   /* We don't mind, but the name must be an unqualified-id.  */
1220   CP_PARSER_DECLARATOR_EITHER
1221 } cp_parser_declarator_kind;
1222
1223 /* The precedence values used to parse binary expressions.  The minimum value
1224    of PREC must be 1, because zero is reserved to quickly discriminate
1225    binary operators from other tokens.  */
1226
1227 enum cp_parser_prec
1228 {
1229   PREC_NOT_OPERATOR,
1230   PREC_LOGICAL_OR_EXPRESSION,
1231   PREC_LOGICAL_AND_EXPRESSION,
1232   PREC_INCLUSIVE_OR_EXPRESSION,
1233   PREC_EXCLUSIVE_OR_EXPRESSION,
1234   PREC_AND_EXPRESSION,
1235   PREC_EQUALITY_EXPRESSION,
1236   PREC_RELATIONAL_EXPRESSION,
1237   PREC_SHIFT_EXPRESSION,
1238   PREC_ADDITIVE_EXPRESSION,
1239   PREC_MULTIPLICATIVE_EXPRESSION,
1240   PREC_PM_EXPRESSION,
1241   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1242 };
1243
1244 /* A mapping from a token type to a corresponding tree node type, with a
1245    precedence value.  */
1246
1247 typedef struct cp_parser_binary_operations_map_node
1248 {
1249   /* The token type.  */
1250   enum cpp_ttype token_type;
1251   /* The corresponding tree code.  */
1252   enum tree_code tree_type;
1253   /* The precedence of this operator.  */
1254   enum cp_parser_prec prec;
1255 } cp_parser_binary_operations_map_node;
1256
1257 /* The status of a tentative parse.  */
1258
1259 typedef enum cp_parser_status_kind
1260 {
1261   /* No errors have occurred.  */
1262   CP_PARSER_STATUS_KIND_NO_ERROR,
1263   /* An error has occurred.  */
1264   CP_PARSER_STATUS_KIND_ERROR,
1265   /* We are committed to this tentative parse, whether or not an error
1266      has occurred.  */
1267   CP_PARSER_STATUS_KIND_COMMITTED
1268 } cp_parser_status_kind;
1269
1270 typedef struct cp_parser_expression_stack_entry
1271 {
1272   /* Left hand side of the binary operation we are currently
1273      parsing.  */
1274   tree lhs;
1275   /* Original tree code for left hand side, if it was a binary
1276      expression itself (used for -Wparentheses).  */
1277   enum tree_code lhs_type;
1278   /* Tree code for the binary operation we are parsing.  */
1279   enum tree_code tree_type;
1280   /* Precedence of the binary operation we are parsing.  */
1281   enum cp_parser_prec prec;
1282 } cp_parser_expression_stack_entry;
1283
1284 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1285    entries because precedence levels on the stack are monotonically
1286    increasing.  */
1287 typedef struct cp_parser_expression_stack_entry
1288   cp_parser_expression_stack[NUM_PREC_VALUES];
1289
1290 /* Context that is saved and restored when parsing tentatively.  */
1291 typedef struct GTY (()) cp_parser_context {
1292   /* If this is a tentative parsing context, the status of the
1293      tentative parse.  */
1294   enum cp_parser_status_kind status;
1295   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1296      that are looked up in this context must be looked up both in the
1297      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1298      the context of the containing expression.  */
1299   tree object_type;
1300
1301   /* The next parsing context in the stack.  */
1302   struct cp_parser_context *next;
1303 } cp_parser_context;
1304
1305 /* Prototypes.  */
1306
1307 /* Constructors and destructors.  */
1308
1309 static cp_parser_context *cp_parser_context_new
1310   (cp_parser_context *);
1311
1312 /* Class variables.  */
1313
1314 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1315
1316 /* The operator-precedence table used by cp_parser_binary_expression.
1317    Transformed into an associative array (binops_by_token) by
1318    cp_parser_new.  */
1319
1320 static const cp_parser_binary_operations_map_node binops[] = {
1321   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1322   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1323
1324   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1325   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1326   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1327
1328   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1329   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1330
1331   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1332   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1333
1334   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1335   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1336   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1337   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1338
1339   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1340   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1341
1342   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1343
1344   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1345
1346   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1347
1348   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1349
1350   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1351 };
1352
1353 /* The same as binops, but initialized by cp_parser_new so that
1354    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1355    for speed.  */
1356 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1357
1358 /* Constructors and destructors.  */
1359
1360 /* Construct a new context.  The context below this one on the stack
1361    is given by NEXT.  */
1362
1363 static cp_parser_context *
1364 cp_parser_context_new (cp_parser_context* next)
1365 {
1366   cp_parser_context *context;
1367
1368   /* Allocate the storage.  */
1369   if (cp_parser_context_free_list != NULL)
1370     {
1371       /* Pull the first entry from the free list.  */
1372       context = cp_parser_context_free_list;
1373       cp_parser_context_free_list = context->next;
1374       memset (context, 0, sizeof (*context));
1375     }
1376   else
1377     context = GGC_CNEW (cp_parser_context);
1378
1379   /* No errors have occurred yet in this context.  */
1380   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1381   /* If this is not the bottommost context, copy information that we
1382      need from the previous context.  */
1383   if (next)
1384     {
1385       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1386          expression, then we are parsing one in this context, too.  */
1387       context->object_type = next->object_type;
1388       /* Thread the stack.  */
1389       context->next = next;
1390     }
1391
1392   return context;
1393 }
1394
1395 /* The cp_parser structure represents the C++ parser.  */
1396
1397 typedef struct GTY(()) cp_parser {
1398   /* The lexer from which we are obtaining tokens.  */
1399   cp_lexer *lexer;
1400
1401   /* The scope in which names should be looked up.  If NULL_TREE, then
1402      we look up names in the scope that is currently open in the
1403      source program.  If non-NULL, this is either a TYPE or
1404      NAMESPACE_DECL for the scope in which we should look.  It can
1405      also be ERROR_MARK, when we've parsed a bogus scope.
1406
1407      This value is not cleared automatically after a name is looked
1408      up, so we must be careful to clear it before starting a new look
1409      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1410      will look up `Z' in the scope of `X', rather than the current
1411      scope.)  Unfortunately, it is difficult to tell when name lookup
1412      is complete, because we sometimes peek at a token, look it up,
1413      and then decide not to consume it.   */
1414   tree scope;
1415
1416   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1417      last lookup took place.  OBJECT_SCOPE is used if an expression
1418      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1419      respectively.  QUALIFYING_SCOPE is used for an expression of the
1420      form "X::Y"; it refers to X.  */
1421   tree object_scope;
1422   tree qualifying_scope;
1423
1424   /* A stack of parsing contexts.  All but the bottom entry on the
1425      stack will be tentative contexts.
1426
1427      We parse tentatively in order to determine which construct is in
1428      use in some situations.  For example, in order to determine
1429      whether a statement is an expression-statement or a
1430      declaration-statement we parse it tentatively as a
1431      declaration-statement.  If that fails, we then reparse the same
1432      token stream as an expression-statement.  */
1433   cp_parser_context *context;
1434
1435   /* True if we are parsing GNU C++.  If this flag is not set, then
1436      GNU extensions are not recognized.  */
1437   bool allow_gnu_extensions_p;
1438
1439   /* TRUE if the `>' token should be interpreted as the greater-than
1440      operator.  FALSE if it is the end of a template-id or
1441      template-parameter-list. In C++0x mode, this flag also applies to
1442      `>>' tokens, which are viewed as two consecutive `>' tokens when
1443      this flag is FALSE.  */
1444   bool greater_than_is_operator_p;
1445
1446   /* TRUE if default arguments are allowed within a parameter list
1447      that starts at this point. FALSE if only a gnu extension makes
1448      them permissible.  */
1449   bool default_arg_ok_p;
1450
1451   /* TRUE if we are parsing an integral constant-expression.  See
1452      [expr.const] for a precise definition.  */
1453   bool integral_constant_expression_p;
1454
1455   /* TRUE if we are parsing an integral constant-expression -- but a
1456      non-constant expression should be permitted as well.  This flag
1457      is used when parsing an array bound so that GNU variable-length
1458      arrays are tolerated.  */
1459   bool allow_non_integral_constant_expression_p;
1460
1461   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1462      been seen that makes the expression non-constant.  */
1463   bool non_integral_constant_expression_p;
1464
1465   /* TRUE if local variable names and `this' are forbidden in the
1466      current context.  */
1467   bool local_variables_forbidden_p;
1468
1469   /* TRUE if the declaration we are parsing is part of a
1470      linkage-specification of the form `extern string-literal
1471      declaration'.  */
1472   bool in_unbraced_linkage_specification_p;
1473
1474   /* TRUE if we are presently parsing a declarator, after the
1475      direct-declarator.  */
1476   bool in_declarator_p;
1477
1478   /* TRUE if we are presently parsing a template-argument-list.  */
1479   bool in_template_argument_list_p;
1480
1481   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1482      to IN_OMP_BLOCK if parsing OpenMP structured block and
1483      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1484      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1485      iteration-statement, OpenMP block or loop within that switch.  */
1486 #define IN_SWITCH_STMT          1
1487 #define IN_ITERATION_STMT       2
1488 #define IN_OMP_BLOCK            4
1489 #define IN_OMP_FOR              8
1490 #define IN_IF_STMT             16
1491   unsigned char in_statement;
1492
1493   /* TRUE if we are presently parsing the body of a switch statement.
1494      Note that this doesn't quite overlap with in_statement above.
1495      The difference relates to giving the right sets of error messages:
1496      "case not in switch" vs "break statement used with OpenMP...".  */
1497   bool in_switch_statement_p;
1498
1499   /* TRUE if we are parsing a type-id in an expression context.  In
1500      such a situation, both "type (expr)" and "type (type)" are valid
1501      alternatives.  */
1502   bool in_type_id_in_expr_p;
1503
1504   /* TRUE if we are currently in a header file where declarations are
1505      implicitly extern "C".  */
1506   bool implicit_extern_c;
1507
1508   /* TRUE if strings in expressions should be translated to the execution
1509      character set.  */
1510   bool translate_strings_p;
1511
1512   /* TRUE if we are presently parsing the body of a function, but not
1513      a local class.  */
1514   bool in_function_body;
1515
1516   /* If non-NULL, then we are parsing a construct where new type
1517      definitions are not permitted.  The string stored here will be
1518      issued as an error message if a type is defined.  */
1519   const char *type_definition_forbidden_message;
1520
1521   /* A list of lists. The outer list is a stack, used for member
1522      functions of local classes. At each level there are two sub-list,
1523      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1524      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1525      TREE_VALUE's. The functions are chained in reverse declaration
1526      order.
1527
1528      The TREE_PURPOSE sublist contains those functions with default
1529      arguments that need post processing, and the TREE_VALUE sublist
1530      contains those functions with definitions that need post
1531      processing.
1532
1533      These lists can only be processed once the outermost class being
1534      defined is complete.  */
1535   tree unparsed_functions_queues;
1536
1537   /* The number of classes whose definitions are currently in
1538      progress.  */
1539   unsigned num_classes_being_defined;
1540
1541   /* The number of template parameter lists that apply directly to the
1542      current declaration.  */
1543   unsigned num_template_parameter_lists;
1544 } cp_parser;
1545
1546 /* Prototypes.  */
1547
1548 /* Constructors and destructors.  */
1549
1550 static cp_parser *cp_parser_new
1551   (void);
1552
1553 /* Routines to parse various constructs.
1554
1555    Those that return `tree' will return the error_mark_node (rather
1556    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1557    Sometimes, they will return an ordinary node if error-recovery was
1558    attempted, even though a parse error occurred.  So, to check
1559    whether or not a parse error occurred, you should always use
1560    cp_parser_error_occurred.  If the construct is optional (indicated
1561    either by an `_opt' in the name of the function that does the
1562    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1563    the construct is not present.  */
1564
1565 /* Lexical conventions [gram.lex]  */
1566
1567 static tree cp_parser_identifier
1568   (cp_parser *);
1569 static tree cp_parser_string_literal
1570   (cp_parser *, bool, bool);
1571
1572 /* Basic concepts [gram.basic]  */
1573
1574 static bool cp_parser_translation_unit
1575   (cp_parser *);
1576
1577 /* Expressions [gram.expr]  */
1578
1579 static tree cp_parser_primary_expression
1580   (cp_parser *, bool, bool, bool, cp_id_kind *);
1581 static tree cp_parser_id_expression
1582   (cp_parser *, bool, bool, bool *, bool, bool);
1583 static tree cp_parser_unqualified_id
1584   (cp_parser *, bool, bool, bool, bool);
1585 static tree cp_parser_nested_name_specifier_opt
1586   (cp_parser *, bool, bool, bool, bool);
1587 static tree cp_parser_nested_name_specifier
1588   (cp_parser *, bool, bool, bool, bool);
1589 static tree cp_parser_qualifying_entity
1590   (cp_parser *, bool, bool, bool, bool, bool);
1591 static tree cp_parser_postfix_expression
1592   (cp_parser *, bool, bool, bool, cp_id_kind *);
1593 static tree cp_parser_postfix_open_square_expression
1594   (cp_parser *, tree, bool);
1595 static tree cp_parser_postfix_dot_deref_expression
1596   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1597 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1598   (cp_parser *, bool, bool, bool, bool *);
1599 static void cp_parser_pseudo_destructor_name
1600   (cp_parser *, tree *, tree *);
1601 static tree cp_parser_unary_expression
1602   (cp_parser *, bool, bool, cp_id_kind *);
1603 static enum tree_code cp_parser_unary_operator
1604   (cp_token *);
1605 static tree cp_parser_new_expression
1606   (cp_parser *);
1607 static VEC(tree,gc) *cp_parser_new_placement
1608   (cp_parser *);
1609 static tree cp_parser_new_type_id
1610   (cp_parser *, tree *);
1611 static cp_declarator *cp_parser_new_declarator_opt
1612   (cp_parser *);
1613 static cp_declarator *cp_parser_direct_new_declarator
1614   (cp_parser *);
1615 static VEC(tree,gc) *cp_parser_new_initializer
1616   (cp_parser *);
1617 static tree cp_parser_delete_expression
1618   (cp_parser *);
1619 static tree cp_parser_cast_expression
1620   (cp_parser *, bool, bool, cp_id_kind *);
1621 static tree cp_parser_binary_expression
1622   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1623 static tree cp_parser_question_colon_clause
1624   (cp_parser *, tree);
1625 static tree cp_parser_assignment_expression
1626   (cp_parser *, bool, cp_id_kind *);
1627 static enum tree_code cp_parser_assignment_operator_opt
1628   (cp_parser *);
1629 static tree cp_parser_expression
1630   (cp_parser *, bool, cp_id_kind *);
1631 static tree cp_parser_constant_expression
1632   (cp_parser *, bool, bool *);
1633 static tree cp_parser_builtin_offsetof
1634   (cp_parser *);
1635 static tree cp_parser_lambda_expression
1636   (cp_parser *);
1637 static void cp_parser_lambda_introducer
1638   (cp_parser *, tree);
1639 static void cp_parser_lambda_declarator_opt
1640   (cp_parser *, tree);
1641 static void cp_parser_lambda_body
1642   (cp_parser *, tree);
1643
1644 /* Statements [gram.stmt.stmt]  */
1645
1646 static void cp_parser_statement
1647   (cp_parser *, tree, bool, bool *);
1648 static void cp_parser_label_for_labeled_statement
1649   (cp_parser *);
1650 static tree cp_parser_expression_statement
1651   (cp_parser *, tree);
1652 static tree cp_parser_compound_statement
1653   (cp_parser *, tree, bool);
1654 static void cp_parser_statement_seq_opt
1655   (cp_parser *, tree);
1656 static tree cp_parser_selection_statement
1657   (cp_parser *, bool *);
1658 static tree cp_parser_condition
1659   (cp_parser *);
1660 static tree cp_parser_iteration_statement
1661   (cp_parser *);
1662 static void cp_parser_for_init_statement
1663   (cp_parser *);
1664 static tree cp_parser_jump_statement
1665   (cp_parser *);
1666 static void cp_parser_declaration_statement
1667   (cp_parser *);
1668
1669 static tree cp_parser_implicitly_scoped_statement
1670   (cp_parser *, bool *);
1671 static void cp_parser_already_scoped_statement
1672   (cp_parser *);
1673
1674 /* Declarations [gram.dcl.dcl] */
1675
1676 static void cp_parser_declaration_seq_opt
1677   (cp_parser *);
1678 static void cp_parser_declaration
1679   (cp_parser *);
1680 static void cp_parser_block_declaration
1681   (cp_parser *, bool);
1682 static void cp_parser_simple_declaration
1683   (cp_parser *, bool);
1684 static void cp_parser_decl_specifier_seq
1685   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1686 static tree cp_parser_storage_class_specifier_opt
1687   (cp_parser *);
1688 static tree cp_parser_function_specifier_opt
1689   (cp_parser *, cp_decl_specifier_seq *);
1690 static tree cp_parser_type_specifier
1691   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1692    int *, bool *);
1693 static tree cp_parser_simple_type_specifier
1694   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1695 static tree cp_parser_type_name
1696   (cp_parser *);
1697 static tree cp_parser_nonclass_name 
1698   (cp_parser* parser);
1699 static tree cp_parser_elaborated_type_specifier
1700   (cp_parser *, bool, bool);
1701 static tree cp_parser_enum_specifier
1702   (cp_parser *);
1703 static void cp_parser_enumerator_list
1704   (cp_parser *, tree);
1705 static void cp_parser_enumerator_definition
1706   (cp_parser *, tree);
1707 static tree cp_parser_namespace_name
1708   (cp_parser *);
1709 static void cp_parser_namespace_definition
1710   (cp_parser *);
1711 static void cp_parser_namespace_body
1712   (cp_parser *);
1713 static tree cp_parser_qualified_namespace_specifier
1714   (cp_parser *);
1715 static void cp_parser_namespace_alias_definition
1716   (cp_parser *);
1717 static bool cp_parser_using_declaration
1718   (cp_parser *, bool);
1719 static void cp_parser_using_directive
1720   (cp_parser *);
1721 static void cp_parser_asm_definition
1722   (cp_parser *);
1723 static void cp_parser_linkage_specification
1724   (cp_parser *);
1725 static void cp_parser_static_assert
1726   (cp_parser *, bool);
1727 static tree cp_parser_decltype
1728   (cp_parser *);
1729
1730 /* Declarators [gram.dcl.decl] */
1731
1732 static tree cp_parser_init_declarator
1733   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1734 static cp_declarator *cp_parser_declarator
1735   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1736 static cp_declarator *cp_parser_direct_declarator
1737   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1738 static enum tree_code cp_parser_ptr_operator
1739   (cp_parser *, tree *, cp_cv_quals *);
1740 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1741   (cp_parser *);
1742 static tree cp_parser_late_return_type_opt
1743   (cp_parser *);
1744 static tree cp_parser_declarator_id
1745   (cp_parser *, bool);
1746 static tree cp_parser_type_id
1747   (cp_parser *);
1748 static tree cp_parser_template_type_arg
1749   (cp_parser *);
1750 static tree cp_parser_trailing_type_id (cp_parser *);
1751 static tree cp_parser_type_id_1
1752   (cp_parser *, bool, bool);
1753 static void cp_parser_type_specifier_seq
1754   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1755 static tree cp_parser_parameter_declaration_clause
1756   (cp_parser *);
1757 static tree cp_parser_parameter_declaration_list
1758   (cp_parser *, bool *);
1759 static cp_parameter_declarator *cp_parser_parameter_declaration
1760   (cp_parser *, bool, bool *);
1761 static tree cp_parser_default_argument 
1762   (cp_parser *, bool);
1763 static void cp_parser_function_body
1764   (cp_parser *);
1765 static tree cp_parser_initializer
1766   (cp_parser *, bool *, bool *);
1767 static tree cp_parser_initializer_clause
1768   (cp_parser *, bool *);
1769 static tree cp_parser_braced_list
1770   (cp_parser*, bool*);
1771 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1772   (cp_parser *, bool *);
1773
1774 static bool cp_parser_ctor_initializer_opt_and_function_body
1775   (cp_parser *);
1776
1777 /* Classes [gram.class] */
1778
1779 static tree cp_parser_class_name
1780   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1781 static tree cp_parser_class_specifier
1782   (cp_parser *);
1783 static tree cp_parser_class_head
1784   (cp_parser *, bool *, tree *, tree *);
1785 static enum tag_types cp_parser_class_key
1786   (cp_parser *);
1787 static void cp_parser_member_specification_opt
1788   (cp_parser *);
1789 static void cp_parser_member_declaration
1790   (cp_parser *);
1791 static tree cp_parser_pure_specifier
1792   (cp_parser *);
1793 static tree cp_parser_constant_initializer
1794   (cp_parser *);
1795
1796 /* Derived classes [gram.class.derived] */
1797
1798 static tree cp_parser_base_clause
1799   (cp_parser *);
1800 static tree cp_parser_base_specifier
1801   (cp_parser *);
1802
1803 /* Special member functions [gram.special] */
1804
1805 static tree cp_parser_conversion_function_id
1806   (cp_parser *);
1807 static tree cp_parser_conversion_type_id
1808   (cp_parser *);
1809 static cp_declarator *cp_parser_conversion_declarator_opt
1810   (cp_parser *);
1811 static bool cp_parser_ctor_initializer_opt
1812   (cp_parser *);
1813 static void cp_parser_mem_initializer_list
1814   (cp_parser *);
1815 static tree cp_parser_mem_initializer
1816   (cp_parser *);
1817 static tree cp_parser_mem_initializer_id
1818   (cp_parser *);
1819
1820 /* Overloading [gram.over] */
1821
1822 static tree cp_parser_operator_function_id
1823   (cp_parser *);
1824 static tree cp_parser_operator
1825   (cp_parser *);
1826
1827 /* Templates [gram.temp] */
1828
1829 static void cp_parser_template_declaration
1830   (cp_parser *, bool);
1831 static tree cp_parser_template_parameter_list
1832   (cp_parser *);
1833 static tree cp_parser_template_parameter
1834   (cp_parser *, bool *, bool *);
1835 static tree cp_parser_type_parameter
1836   (cp_parser *, bool *);
1837 static tree cp_parser_template_id
1838   (cp_parser *, bool, bool, bool);
1839 static tree cp_parser_template_name
1840   (cp_parser *, bool, bool, bool, bool *);
1841 static tree cp_parser_template_argument_list
1842   (cp_parser *);
1843 static tree cp_parser_template_argument
1844   (cp_parser *);
1845 static void cp_parser_explicit_instantiation
1846   (cp_parser *);
1847 static void cp_parser_explicit_specialization
1848   (cp_parser *);
1849
1850 /* Exception handling [gram.exception] */
1851
1852 static tree cp_parser_try_block
1853   (cp_parser *);
1854 static bool cp_parser_function_try_block
1855   (cp_parser *);
1856 static void cp_parser_handler_seq
1857   (cp_parser *);
1858 static void cp_parser_handler
1859   (cp_parser *);
1860 static tree cp_parser_exception_declaration
1861   (cp_parser *);
1862 static tree cp_parser_throw_expression
1863   (cp_parser *);
1864 static tree cp_parser_exception_specification_opt
1865   (cp_parser *);
1866 static tree cp_parser_type_id_list
1867   (cp_parser *);
1868
1869 /* GNU Extensions */
1870
1871 static tree cp_parser_asm_specification_opt
1872   (cp_parser *);
1873 static tree cp_parser_asm_operand_list
1874   (cp_parser *);
1875 static tree cp_parser_asm_clobber_list
1876   (cp_parser *);
1877 static tree cp_parser_asm_label_list
1878   (cp_parser *);
1879 static tree cp_parser_attributes_opt
1880   (cp_parser *);
1881 static tree cp_parser_attribute_list
1882   (cp_parser *);
1883 static bool cp_parser_extension_opt
1884   (cp_parser *, int *);
1885 static void cp_parser_label_declaration
1886   (cp_parser *);
1887
1888 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1889 static bool cp_parser_pragma
1890   (cp_parser *, enum pragma_context);
1891
1892 /* Objective-C++ Productions */
1893
1894 static tree cp_parser_objc_message_receiver
1895   (cp_parser *);
1896 static tree cp_parser_objc_message_args
1897   (cp_parser *);
1898 static tree cp_parser_objc_message_expression
1899   (cp_parser *);
1900 static tree cp_parser_objc_encode_expression
1901   (cp_parser *);
1902 static tree cp_parser_objc_defs_expression
1903   (cp_parser *);
1904 static tree cp_parser_objc_protocol_expression
1905   (cp_parser *);
1906 static tree cp_parser_objc_selector_expression
1907   (cp_parser *);
1908 static tree cp_parser_objc_expression
1909   (cp_parser *);
1910 static bool cp_parser_objc_selector_p
1911   (enum cpp_ttype);
1912 static tree cp_parser_objc_selector
1913   (cp_parser *);
1914 static tree cp_parser_objc_protocol_refs_opt
1915   (cp_parser *);
1916 static void cp_parser_objc_declaration
1917   (cp_parser *);
1918 static tree cp_parser_objc_statement
1919   (cp_parser *);
1920
1921 /* Utility Routines */
1922
1923 static tree cp_parser_lookup_name
1924   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1925 static tree cp_parser_lookup_name_simple
1926   (cp_parser *, tree, location_t);
1927 static tree cp_parser_maybe_treat_template_as_class
1928   (tree, bool);
1929 static bool cp_parser_check_declarator_template_parameters
1930   (cp_parser *, cp_declarator *, location_t);
1931 static bool cp_parser_check_template_parameters
1932   (cp_parser *, unsigned, location_t, cp_declarator *);
1933 static tree cp_parser_simple_cast_expression
1934   (cp_parser *);
1935 static tree cp_parser_global_scope_opt
1936   (cp_parser *, bool);
1937 static bool cp_parser_constructor_declarator_p
1938   (cp_parser *, bool);
1939 static tree cp_parser_function_definition_from_specifiers_and_declarator
1940   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1941 static tree cp_parser_function_definition_after_declarator
1942   (cp_parser *, bool);
1943 static void cp_parser_template_declaration_after_export
1944   (cp_parser *, bool);
1945 static void cp_parser_perform_template_parameter_access_checks
1946   (VEC (deferred_access_check,gc)*);
1947 static tree cp_parser_single_declaration
1948   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1949 static tree cp_parser_functional_cast
1950   (cp_parser *, tree);
1951 static tree cp_parser_save_member_function_body
1952   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1953 static tree cp_parser_enclosed_template_argument_list
1954   (cp_parser *);
1955 static void cp_parser_save_default_args
1956   (cp_parser *, tree);
1957 static void cp_parser_late_parsing_for_member
1958   (cp_parser *, tree);
1959 static void cp_parser_late_parsing_default_args
1960   (cp_parser *, tree);
1961 static tree cp_parser_sizeof_operand
1962   (cp_parser *, enum rid);
1963 static tree cp_parser_trait_expr
1964   (cp_parser *, enum rid);
1965 static bool cp_parser_declares_only_class_p
1966   (cp_parser *);
1967 static void cp_parser_set_storage_class
1968   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1969 static void cp_parser_set_decl_spec_type
1970   (cp_decl_specifier_seq *, tree, location_t, bool);
1971 static bool cp_parser_friend_p
1972   (const cp_decl_specifier_seq *);
1973 static cp_token *cp_parser_require
1974   (cp_parser *, enum cpp_ttype, const char *);
1975 static cp_token *cp_parser_require_keyword
1976   (cp_parser *, enum rid, const char *);
1977 static bool cp_parser_token_starts_function_definition_p
1978   (cp_token *);
1979 static bool cp_parser_next_token_starts_class_definition_p
1980   (cp_parser *);
1981 static bool cp_parser_next_token_ends_template_argument_p
1982   (cp_parser *);
1983 static bool cp_parser_nth_token_starts_template_argument_list_p
1984   (cp_parser *, size_t);
1985 static enum tag_types cp_parser_token_is_class_key
1986   (cp_token *);
1987 static void cp_parser_check_class_key
1988   (enum tag_types, tree type);
1989 static void cp_parser_check_access_in_redeclaration
1990   (tree type, location_t location);
1991 static bool cp_parser_optional_template_keyword
1992   (cp_parser *);
1993 static void cp_parser_pre_parsed_nested_name_specifier
1994   (cp_parser *);
1995 static bool cp_parser_cache_group
1996   (cp_parser *, enum cpp_ttype, unsigned);
1997 static void cp_parser_parse_tentatively
1998   (cp_parser *);
1999 static void cp_parser_commit_to_tentative_parse
2000   (cp_parser *);
2001 static void cp_parser_abort_tentative_parse
2002   (cp_parser *);
2003 static bool cp_parser_parse_definitely
2004   (cp_parser *);
2005 static inline bool cp_parser_parsing_tentatively
2006   (cp_parser *);
2007 static bool cp_parser_uncommitted_to_tentative_parse_p
2008   (cp_parser *);
2009 static void cp_parser_error
2010   (cp_parser *, const char *);
2011 static void cp_parser_name_lookup_error
2012   (cp_parser *, tree, tree, const char *, location_t);
2013 static bool cp_parser_simulate_error
2014   (cp_parser *);
2015 static bool cp_parser_check_type_definition
2016   (cp_parser *);
2017 static void cp_parser_check_for_definition_in_return_type
2018   (cp_declarator *, tree, location_t type_location);
2019 static void cp_parser_check_for_invalid_template_id
2020   (cp_parser *, tree, location_t location);
2021 static bool cp_parser_non_integral_constant_expression
2022   (cp_parser *, const char *);
2023 static void cp_parser_diagnose_invalid_type_name
2024   (cp_parser *, tree, tree, location_t);
2025 static bool cp_parser_parse_and_diagnose_invalid_type_name
2026   (cp_parser *);
2027 static int cp_parser_skip_to_closing_parenthesis
2028   (cp_parser *, bool, bool, bool);
2029 static void cp_parser_skip_to_end_of_statement
2030   (cp_parser *);
2031 static void cp_parser_consume_semicolon_at_end_of_statement
2032   (cp_parser *);
2033 static void cp_parser_skip_to_end_of_block_or_statement
2034   (cp_parser *);
2035 static bool cp_parser_skip_to_closing_brace
2036   (cp_parser *);
2037 static void cp_parser_skip_to_end_of_template_parameter_list
2038   (cp_parser *);
2039 static void cp_parser_skip_to_pragma_eol
2040   (cp_parser*, cp_token *);
2041 static bool cp_parser_error_occurred
2042   (cp_parser *);
2043 static bool cp_parser_allow_gnu_extensions_p
2044   (cp_parser *);
2045 static bool cp_parser_is_string_literal
2046   (cp_token *);
2047 static bool cp_parser_is_keyword
2048   (cp_token *, enum rid);
2049 static tree cp_parser_make_typename_type
2050   (cp_parser *, tree, tree, location_t location);
2051 static cp_declarator * cp_parser_make_indirect_declarator
2052   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2053
2054 /* Returns nonzero if we are parsing tentatively.  */
2055
2056 static inline bool
2057 cp_parser_parsing_tentatively (cp_parser* parser)
2058 {
2059   return parser->context->next != NULL;
2060 }
2061
2062 /* Returns nonzero if TOKEN is a string literal.  */
2063
2064 static bool
2065 cp_parser_is_string_literal (cp_token* token)
2066 {
2067   return (token->type == CPP_STRING ||
2068           token->type == CPP_STRING16 ||
2069           token->type == CPP_STRING32 ||
2070           token->type == CPP_WSTRING ||
2071           token->type == CPP_UTF8STRING);
2072 }
2073
2074 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2075
2076 static bool
2077 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2078 {
2079   return token->keyword == keyword;
2080 }
2081
2082 /* If not parsing tentatively, issue a diagnostic of the form
2083       FILE:LINE: MESSAGE before TOKEN
2084    where TOKEN is the next token in the input stream.  MESSAGE
2085    (specified by the caller) is usually of the form "expected
2086    OTHER-TOKEN".  */
2087
2088 static void
2089 cp_parser_error (cp_parser* parser, const char* message)
2090 {
2091   if (!cp_parser_simulate_error (parser))
2092     {
2093       cp_token *token = cp_lexer_peek_token (parser->lexer);
2094       /* This diagnostic makes more sense if it is tagged to the line
2095          of the token we just peeked at.  */
2096       cp_lexer_set_source_position_from_token (token);
2097
2098       if (token->type == CPP_PRAGMA)
2099         {
2100           error_at (token->location,
2101                     "%<#pragma%> is not allowed here");
2102           cp_parser_skip_to_pragma_eol (parser, token);
2103           return;
2104         }
2105
2106       c_parse_error (message,
2107                      /* Because c_parser_error does not understand
2108                         CPP_KEYWORD, keywords are treated like
2109                         identifiers.  */
2110                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2111                      token->u.value, token->flags);
2112     }
2113 }
2114
2115 /* Issue an error about name-lookup failing.  NAME is the
2116    IDENTIFIER_NODE DECL is the result of
2117    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2118    the thing that we hoped to find.  */
2119
2120 static void
2121 cp_parser_name_lookup_error (cp_parser* parser,
2122                              tree name,
2123                              tree decl,
2124                              const char* desired,
2125                              location_t location)
2126 {
2127   /* If name lookup completely failed, tell the user that NAME was not
2128      declared.  */
2129   if (decl == error_mark_node)
2130     {
2131       if (parser->scope && parser->scope != global_namespace)
2132         error_at (location, "%<%E::%E%> has not been declared",
2133                   parser->scope, name);
2134       else if (parser->scope == global_namespace)
2135         error_at (location, "%<::%E%> has not been declared", name);
2136       else if (parser->object_scope
2137                && !CLASS_TYPE_P (parser->object_scope))
2138         error_at (location, "request for member %qE in non-class type %qT",
2139                   name, parser->object_scope);
2140       else if (parser->object_scope)
2141         error_at (location, "%<%T::%E%> has not been declared",
2142                   parser->object_scope, name);
2143       else
2144         error_at (location, "%qE has not been declared", name);
2145     }
2146   else if (parser->scope && parser->scope != global_namespace)
2147     error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2148   else if (parser->scope == global_namespace)
2149     error_at (location, "%<::%E%> %s", name, desired);
2150   else
2151     error_at (location, "%qE %s", name, desired);
2152 }
2153
2154 /* If we are parsing tentatively, remember that an error has occurred
2155    during this tentative parse.  Returns true if the error was
2156    simulated; false if a message should be issued by the caller.  */
2157
2158 static bool
2159 cp_parser_simulate_error (cp_parser* parser)
2160 {
2161   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2162     {
2163       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2164       return true;
2165     }
2166   return false;
2167 }
2168
2169 /* Check for repeated decl-specifiers.  */
2170
2171 static void
2172 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2173                            location_t location)
2174 {
2175   int ds;
2176
2177   for (ds = ds_first; ds != ds_last; ++ds)
2178     {
2179       unsigned count = decl_specs->specs[ds];
2180       if (count < 2)
2181         continue;
2182       /* The "long" specifier is a special case because of "long long".  */
2183       if (ds == ds_long)
2184         {
2185           if (count > 2)
2186             error_at (location, "%<long long long%> is too long for GCC");
2187           else 
2188             pedwarn_cxx98 (location, OPT_Wlong_long, 
2189                            "ISO C++ 1998 does not support %<long long%>");
2190         }
2191       else if (count > 1)
2192         {
2193           static const char *const decl_spec_names[] = {
2194             "signed",
2195             "unsigned",
2196             "short",
2197             "long",
2198             "const",
2199             "volatile",
2200             "restrict",
2201             "inline",
2202             "virtual",
2203             "explicit",
2204             "friend",
2205             "typedef",
2206             "constexpr",
2207             "__complex",
2208             "__thread"
2209           };
2210           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2211         }
2212     }
2213 }
2214
2215 /* This function is called when a type is defined.  If type
2216    definitions are forbidden at this point, an error message is
2217    issued.  */
2218
2219 static bool
2220 cp_parser_check_type_definition (cp_parser* parser)
2221 {
2222   /* If types are forbidden here, issue a message.  */
2223   if (parser->type_definition_forbidden_message)
2224     {
2225       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2226          in the message need to be interpreted.  */
2227       error (parser->type_definition_forbidden_message);
2228       return false;
2229     }
2230   return true;
2231 }
2232
2233 /* This function is called when the DECLARATOR is processed.  The TYPE
2234    was a type defined in the decl-specifiers.  If it is invalid to
2235    define a type in the decl-specifiers for DECLARATOR, an error is
2236    issued. TYPE_LOCATION is the location of TYPE and is used
2237    for error reporting.  */
2238
2239 static void
2240 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2241                                                tree type, location_t type_location)
2242 {
2243   /* [dcl.fct] forbids type definitions in return types.
2244      Unfortunately, it's not easy to know whether or not we are
2245      processing a return type until after the fact.  */
2246   while (declarator
2247          && (declarator->kind == cdk_pointer
2248              || declarator->kind == cdk_reference
2249              || declarator->kind == cdk_ptrmem))
2250     declarator = declarator->declarator;
2251   if (declarator
2252       && declarator->kind == cdk_function)
2253     {
2254       error_at (type_location,
2255                 "new types may not be defined in a return type");
2256       inform (type_location, 
2257               "(perhaps a semicolon is missing after the definition of %qT)",
2258               type);
2259     }
2260 }
2261
2262 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2263    "<" in any valid C++ program.  If the next token is indeed "<",
2264    issue a message warning the user about what appears to be an
2265    invalid attempt to form a template-id. LOCATION is the location
2266    of the type-specifier (TYPE) */
2267
2268 static void
2269 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2270                                          tree type, location_t location)
2271 {
2272   cp_token_position start = 0;
2273
2274   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2275     {
2276       if (TYPE_P (type))
2277         error_at (location, "%qT is not a template", type);
2278       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2279         error_at (location, "%qE is not a template", type);
2280       else
2281         error_at (location, "invalid template-id");
2282       /* Remember the location of the invalid "<".  */
2283       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2284         start = cp_lexer_token_position (parser->lexer, true);
2285       /* Consume the "<".  */
2286       cp_lexer_consume_token (parser->lexer);
2287       /* Parse the template arguments.  */
2288       cp_parser_enclosed_template_argument_list (parser);
2289       /* Permanently remove the invalid template arguments so that
2290          this error message is not issued again.  */
2291       if (start)
2292         cp_lexer_purge_tokens_after (parser->lexer, start);
2293     }
2294 }
2295
2296 /* If parsing an integral constant-expression, issue an error message
2297    about the fact that THING appeared and return true.  Otherwise,
2298    return false.  In either case, set
2299    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2300
2301 static bool
2302 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2303                                             const char *thing)
2304 {
2305   parser->non_integral_constant_expression_p = true;
2306   if (parser->integral_constant_expression_p)
2307     {
2308       if (!parser->allow_non_integral_constant_expression_p)
2309         {
2310           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2311              in the message need to be interpreted.  */
2312           char *message = concat (thing,
2313                                   " cannot appear in a constant-expression",
2314                                   NULL);
2315           error (message);
2316           free (message);
2317           return true;
2318         }
2319     }
2320   return false;
2321 }
2322
2323 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2324    qualifying scope (or NULL, if none) for ID.  This function commits
2325    to the current active tentative parse, if any.  (Otherwise, the
2326    problematic construct might be encountered again later, resulting
2327    in duplicate error messages.) LOCATION is the location of ID.  */
2328
2329 static void
2330 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2331                                       tree scope, tree id,
2332                                       location_t location)
2333 {
2334   tree decl, old_scope;
2335   /* Try to lookup the identifier.  */
2336   old_scope = parser->scope;
2337   parser->scope = scope;
2338   decl = cp_parser_lookup_name_simple (parser, id, location);
2339   parser->scope = old_scope;
2340   /* If the lookup found a template-name, it means that the user forgot
2341   to specify an argument list. Emit a useful error message.  */
2342   if (TREE_CODE (decl) == TEMPLATE_DECL)
2343     error_at (location,
2344               "invalid use of template-name %qE without an argument list",
2345               decl);
2346   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2347     error_at (location, "invalid use of destructor %qD as a type", id);
2348   else if (TREE_CODE (decl) == TYPE_DECL)
2349     /* Something like 'unsigned A a;'  */
2350     error_at (location, "invalid combination of multiple type-specifiers");
2351   else if (!parser->scope)
2352     {
2353       /* Issue an error message.  */
2354       error_at (location, "%qE does not name a type", id);
2355       /* If we're in a template class, it's possible that the user was
2356          referring to a type from a base class.  For example:
2357
2358            template <typename T> struct A { typedef T X; };
2359            template <typename T> struct B : public A<T> { X x; };
2360
2361          The user should have said "typename A<T>::X".  */
2362       if (processing_template_decl && current_class_type
2363           && TYPE_BINFO (current_class_type))
2364         {
2365           tree b;
2366
2367           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2368                b;
2369                b = TREE_CHAIN (b))
2370             {
2371               tree base_type = BINFO_TYPE (b);
2372               if (CLASS_TYPE_P (base_type)
2373                   && dependent_type_p (base_type))
2374                 {
2375                   tree field;
2376                   /* Go from a particular instantiation of the
2377                      template (which will have an empty TYPE_FIELDs),
2378                      to the main version.  */
2379                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2380                   for (field = TYPE_FIELDS (base_type);
2381                        field;
2382                        field = TREE_CHAIN (field))
2383                     if (TREE_CODE (field) == TYPE_DECL
2384                         && DECL_NAME (field) == id)
2385                       {
2386                         inform (location, 
2387                                 "(perhaps %<typename %T::%E%> was intended)",
2388                                 BINFO_TYPE (b), id);
2389                         break;
2390                       }
2391                   if (field)
2392                     break;
2393                 }
2394             }
2395         }
2396     }
2397   /* Here we diagnose qualified-ids where the scope is actually correct,
2398      but the identifier does not resolve to a valid type name.  */
2399   else if (parser->scope != error_mark_node)
2400     {
2401       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2402         error_at (location, "%qE in namespace %qE does not name a type",
2403                   id, parser->scope);
2404       else if (CLASS_TYPE_P (parser->scope)
2405                && constructor_name_p (id, parser->scope))
2406         {
2407           /* A<T>::A<T>() */
2408           error_at (location, "%<%T::%E%> names the constructor, not"
2409                     " the type", parser->scope, id);
2410           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2411             error_at (location, "and %qT has no template constructors",
2412                       parser->scope);
2413         }
2414       else if (TYPE_P (parser->scope)
2415                && dependent_scope_p (parser->scope))
2416         error_at (location, "need %<typename%> before %<%T::%E%> because "
2417                   "%qT is a dependent scope",
2418                   parser->scope, id, parser->scope);
2419       else if (TYPE_P (parser->scope))
2420         error_at (location, "%qE in class %qT does not name a type",
2421                   id, parser->scope);
2422       else
2423         gcc_unreachable ();
2424     }
2425   cp_parser_commit_to_tentative_parse (parser);
2426 }
2427
2428 /* Check for a common situation where a type-name should be present,
2429    but is not, and issue a sensible error message.  Returns true if an
2430    invalid type-name was detected.
2431
2432    The situation handled by this function are variable declarations of the
2433    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2434    Usually, `ID' should name a type, but if we got here it means that it
2435    does not. We try to emit the best possible error message depending on
2436    how exactly the id-expression looks like.  */
2437
2438 static bool
2439 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2440 {
2441   tree id;
2442   cp_token *token = cp_lexer_peek_token (parser->lexer);
2443
2444   /* Avoid duplicate error about ambiguous lookup.  */
2445   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2446     {
2447       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2448       if (next->type == CPP_NAME && next->ambiguous_p)
2449         goto out;
2450     }
2451
2452   cp_parser_parse_tentatively (parser);
2453   id = cp_parser_id_expression (parser,
2454                                 /*template_keyword_p=*/false,
2455                                 /*check_dependency_p=*/true,
2456                                 /*template_p=*/NULL,
2457                                 /*declarator_p=*/true,
2458                                 /*optional_p=*/false);
2459   /* If the next token is a (, this is a function with no explicit return
2460      type, i.e. constructor, destructor or conversion op.  */
2461   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2462       || TREE_CODE (id) == TYPE_DECL)
2463     {
2464       cp_parser_abort_tentative_parse (parser);
2465       return false;
2466     }
2467   if (!cp_parser_parse_definitely (parser))
2468     return false;
2469
2470   /* Emit a diagnostic for the invalid type.  */
2471   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2472                                         id, token->location);
2473  out:
2474   /* If we aren't in the middle of a declarator (i.e. in a
2475      parameter-declaration-clause), skip to the end of the declaration;
2476      there's no point in trying to process it.  */
2477   if (!parser->in_declarator_p)
2478     cp_parser_skip_to_end_of_block_or_statement (parser);
2479   return true;
2480 }
2481
2482 /* Consume tokens up to, and including, the next non-nested closing `)'.
2483    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2484    are doing error recovery. Returns -1 if OR_COMMA is true and we
2485    found an unnested comma.  */
2486
2487 static int
2488 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2489                                        bool recovering,
2490                                        bool or_comma,
2491                                        bool consume_paren)
2492 {
2493   unsigned paren_depth = 0;
2494   unsigned brace_depth = 0;
2495   unsigned square_depth = 0;
2496
2497   if (recovering && !or_comma
2498       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2499     return 0;
2500
2501   while (true)
2502     {
2503       cp_token * token = cp_lexer_peek_token (parser->lexer);
2504
2505       switch (token->type)
2506         {
2507         case CPP_EOF:
2508         case CPP_PRAGMA_EOL:
2509           /* If we've run out of tokens, then there is no closing `)'.  */
2510           return 0;
2511
2512         /* This is good for lambda expression capture-lists.  */
2513         case CPP_OPEN_SQUARE:
2514           ++square_depth;
2515           break;
2516         case CPP_CLOSE_SQUARE:
2517           if (!square_depth--)
2518             return 0;
2519           break;
2520
2521         case CPP_SEMICOLON:
2522           /* This matches the processing in skip_to_end_of_statement.  */
2523           if (!brace_depth)
2524             return 0;
2525           break;
2526
2527         case CPP_OPEN_BRACE:
2528           ++brace_depth;
2529           break;
2530         case CPP_CLOSE_BRACE:
2531           if (!brace_depth--)
2532             return 0;
2533           break;
2534
2535         case CPP_COMMA:
2536           if (recovering && or_comma && !brace_depth && !paren_depth
2537               && !square_depth)
2538             return -1;
2539           break;
2540
2541         case CPP_OPEN_PAREN:
2542           if (!brace_depth)
2543             ++paren_depth;
2544           break;
2545
2546         case CPP_CLOSE_PAREN:
2547           if (!brace_depth && !paren_depth--)
2548             {
2549               if (consume_paren)
2550                 cp_lexer_consume_token (parser->lexer);
2551               return 1;
2552             }
2553           break;
2554
2555         default:
2556           break;
2557         }
2558
2559       /* Consume the token.  */
2560       cp_lexer_consume_token (parser->lexer);
2561     }
2562 }
2563
2564 /* Consume tokens until we reach the end of the current statement.
2565    Normally, that will be just before consuming a `;'.  However, if a
2566    non-nested `}' comes first, then we stop before consuming that.  */
2567
2568 static void
2569 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2570 {
2571   unsigned nesting_depth = 0;
2572
2573   while (true)
2574     {
2575       cp_token *token = cp_lexer_peek_token (parser->lexer);
2576
2577       switch (token->type)
2578         {
2579         case CPP_EOF:
2580         case CPP_PRAGMA_EOL:
2581           /* If we've run out of tokens, stop.  */
2582           return;
2583
2584         case CPP_SEMICOLON:
2585           /* If the next token is a `;', we have reached the end of the
2586              statement.  */
2587           if (!nesting_depth)
2588             return;
2589           break;
2590
2591         case CPP_CLOSE_BRACE:
2592           /* If this is a non-nested '}', stop before consuming it.
2593              That way, when confronted with something like:
2594
2595                { 3 + }
2596
2597              we stop before consuming the closing '}', even though we
2598              have not yet reached a `;'.  */
2599           if (nesting_depth == 0)
2600             return;
2601
2602           /* If it is the closing '}' for a block that we have
2603              scanned, stop -- but only after consuming the token.
2604              That way given:
2605
2606                 void f g () { ... }
2607                 typedef int I;
2608
2609              we will stop after the body of the erroneously declared
2610              function, but before consuming the following `typedef'
2611              declaration.  */
2612           if (--nesting_depth == 0)
2613             {
2614               cp_lexer_consume_token (parser->lexer);
2615               return;
2616             }
2617
2618         case CPP_OPEN_BRACE:
2619           ++nesting_depth;
2620           break;
2621
2622         default:
2623           break;
2624         }
2625
2626       /* Consume the token.  */
2627       cp_lexer_consume_token (parser->lexer);
2628     }
2629 }
2630
2631 /* This function is called at the end of a statement or declaration.
2632    If the next token is a semicolon, it is consumed; otherwise, error
2633    recovery is attempted.  */
2634
2635 static void
2636 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2637 {
2638   /* Look for the trailing `;'.  */
2639   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2640     {
2641       /* If there is additional (erroneous) input, skip to the end of
2642          the statement.  */
2643       cp_parser_skip_to_end_of_statement (parser);
2644       /* If the next token is now a `;', consume it.  */
2645       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2646         cp_lexer_consume_token (parser->lexer);
2647     }
2648 }
2649
2650 /* Skip tokens until we have consumed an entire block, or until we
2651    have consumed a non-nested `;'.  */
2652
2653 static void
2654 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2655 {
2656   int nesting_depth = 0;
2657
2658   while (nesting_depth >= 0)
2659     {
2660       cp_token *token = cp_lexer_peek_token (parser->lexer);
2661
2662       switch (token->type)
2663         {
2664         case CPP_EOF:
2665         case CPP_PRAGMA_EOL:
2666           /* If we've run out of tokens, stop.  */
2667           return;
2668
2669         case CPP_SEMICOLON:
2670           /* Stop if this is an unnested ';'. */
2671           if (!nesting_depth)
2672             nesting_depth = -1;
2673           break;
2674
2675         case CPP_CLOSE_BRACE:
2676           /* Stop if this is an unnested '}', or closes the outermost
2677              nesting level.  */
2678           nesting_depth--;
2679           if (nesting_depth < 0)
2680             return;
2681           if (!nesting_depth)
2682             nesting_depth = -1;
2683           break;
2684
2685         case CPP_OPEN_BRACE:
2686           /* Nest. */
2687           nesting_depth++;
2688           break;
2689
2690         default:
2691           break;
2692         }
2693
2694       /* Consume the token.  */
2695       cp_lexer_consume_token (parser->lexer);
2696     }
2697 }
2698
2699 /* Skip tokens until a non-nested closing curly brace is the next
2700    token, or there are no more tokens. Return true in the first case,
2701    false otherwise.  */
2702
2703 static bool
2704 cp_parser_skip_to_closing_brace (cp_parser *parser)
2705 {
2706   unsigned nesting_depth = 0;
2707
2708   while (true)
2709     {
2710       cp_token *token = cp_lexer_peek_token (parser->lexer);
2711
2712       switch (token->type)
2713         {
2714         case CPP_EOF:
2715         case CPP_PRAGMA_EOL:
2716           /* If we've run out of tokens, stop.  */
2717           return false;
2718
2719         case CPP_CLOSE_BRACE:
2720           /* If the next token is a non-nested `}', then we have reached
2721              the end of the current block.  */
2722           if (nesting_depth-- == 0)
2723             return true;
2724           break;
2725
2726         case CPP_OPEN_BRACE:
2727           /* If it the next token is a `{', then we are entering a new
2728              block.  Consume the entire block.  */
2729           ++nesting_depth;
2730           break;
2731
2732         default:
2733           break;
2734         }
2735
2736       /* Consume the token.  */
2737       cp_lexer_consume_token (parser->lexer);
2738     }
2739 }
2740
2741 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2742    parameter is the PRAGMA token, allowing us to purge the entire pragma
2743    sequence.  */
2744
2745 static void
2746 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2747 {
2748   cp_token *token;
2749
2750   parser->lexer->in_pragma = false;
2751
2752   do
2753     token = cp_lexer_consume_token (parser->lexer);
2754   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2755
2756   /* Ensure that the pragma is not parsed again.  */
2757   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2758 }
2759
2760 /* Require pragma end of line, resyncing with it as necessary.  The
2761    arguments are as for cp_parser_skip_to_pragma_eol.  */
2762
2763 static void
2764 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2765 {
2766   parser->lexer->in_pragma = false;
2767   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2768     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2769 }
2770
2771 /* This is a simple wrapper around make_typename_type. When the id is
2772    an unresolved identifier node, we can provide a superior diagnostic
2773    using cp_parser_diagnose_invalid_type_name.  */
2774
2775 static tree
2776 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2777                               tree id, location_t id_location)
2778 {
2779   tree result;
2780   if (TREE_CODE (id) == IDENTIFIER_NODE)
2781     {
2782       result = make_typename_type (scope, id, typename_type,
2783                                    /*complain=*/tf_none);
2784       if (result == error_mark_node)
2785         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2786       return result;
2787     }
2788   return make_typename_type (scope, id, typename_type, tf_error);
2789 }
2790
2791 /* This is a wrapper around the
2792    make_{pointer,ptrmem,reference}_declarator functions that decides
2793    which one to call based on the CODE and CLASS_TYPE arguments. The
2794    CODE argument should be one of the values returned by
2795    cp_parser_ptr_operator. */
2796 static cp_declarator *
2797 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2798                                     cp_cv_quals cv_qualifiers,
2799                                     cp_declarator *target)
2800 {
2801   if (code == ERROR_MARK)
2802     return cp_error_declarator;
2803
2804   if (code == INDIRECT_REF)
2805     if (class_type == NULL_TREE)
2806       return make_pointer_declarator (cv_qualifiers, target);
2807     else
2808       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2809   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2810     return make_reference_declarator (cv_qualifiers, target, false);
2811   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2812     return make_reference_declarator (cv_qualifiers, target, true);
2813   gcc_unreachable ();
2814 }
2815
2816 /* Create a new C++ parser.  */
2817
2818 static cp_parser *
2819 cp_parser_new (void)
2820 {
2821   cp_parser *parser;
2822   cp_lexer *lexer;
2823   unsigned i;
2824
2825   /* cp_lexer_new_main is called before calling ggc_alloc because
2826      cp_lexer_new_main might load a PCH file.  */
2827   lexer = cp_lexer_new_main ();
2828
2829   /* Initialize the binops_by_token so that we can get the tree
2830      directly from the token.  */
2831   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2832     binops_by_token[binops[i].token_type] = binops[i];
2833
2834   parser = GGC_CNEW (cp_parser);
2835   parser->lexer = lexer;
2836   parser->context = cp_parser_context_new (NULL);
2837
2838   /* For now, we always accept GNU extensions.  */
2839   parser->allow_gnu_extensions_p = 1;
2840
2841   /* The `>' token is a greater-than operator, not the end of a
2842      template-id.  */
2843   parser->greater_than_is_operator_p = true;
2844
2845   parser->default_arg_ok_p = true;
2846
2847   /* We are not parsing a constant-expression.  */
2848   parser->integral_constant_expression_p = false;
2849   parser->allow_non_integral_constant_expression_p = false;
2850   parser->non_integral_constant_expression_p = false;
2851
2852   /* Local variable names are not forbidden.  */
2853   parser->local_variables_forbidden_p = false;
2854
2855   /* We are not processing an `extern "C"' declaration.  */
2856   parser->in_unbraced_linkage_specification_p = false;
2857
2858   /* We are not processing a declarator.  */
2859   parser->in_declarator_p = false;
2860
2861   /* We are not processing a template-argument-list.  */
2862   parser->in_template_argument_list_p = false;
2863
2864   /* We are not in an iteration statement.  */
2865   parser->in_statement = 0;
2866
2867   /* We are not in a switch statement.  */
2868   parser->in_switch_statement_p = false;
2869
2870   /* We are not parsing a type-id inside an expression.  */
2871   parser->in_type_id_in_expr_p = false;
2872
2873   /* Declarations aren't implicitly extern "C".  */
2874   parser->implicit_extern_c = false;
2875
2876   /* String literals should be translated to the execution character set.  */
2877   parser->translate_strings_p = true;
2878
2879   /* We are not parsing a function body.  */
2880   parser->in_function_body = false;
2881
2882   /* The unparsed function queue is empty.  */
2883   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2884
2885   /* There are no classes being defined.  */
2886   parser->num_classes_being_defined = 0;
2887
2888   /* No template parameters apply.  */
2889   parser->num_template_parameter_lists = 0;
2890
2891   return parser;
2892 }
2893
2894 /* Create a cp_lexer structure which will emit the tokens in CACHE
2895    and push it onto the parser's lexer stack.  This is used for delayed
2896    parsing of in-class method bodies and default arguments, and should
2897    not be confused with tentative parsing.  */
2898 static void
2899 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2900 {
2901   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2902   lexer->next = parser->lexer;
2903   parser->lexer = lexer;
2904
2905   /* Move the current source position to that of the first token in the
2906      new lexer.  */
2907   cp_lexer_set_source_position_from_token (lexer->next_token);
2908 }
2909
2910 /* Pop the top lexer off the parser stack.  This is never used for the
2911    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2912 static void
2913 cp_parser_pop_lexer (cp_parser *parser)
2914 {
2915   cp_lexer *lexer = parser->lexer;
2916   parser->lexer = lexer->next;
2917   cp_lexer_destroy (lexer);
2918
2919   /* Put the current source position back where it was before this
2920      lexer was pushed.  */
2921   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2922 }
2923
2924 /* Lexical conventions [gram.lex]  */
2925
2926 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2927    identifier.  */
2928
2929 static tree
2930 cp_parser_identifier (cp_parser* parser)
2931 {
2932   cp_token *token;
2933
2934   /* Look for the identifier.  */
2935   token = cp_parser_require (parser, CPP_NAME, "identifier");
2936   /* Return the value.  */
2937   return token ? token->u.value : error_mark_node;
2938 }
2939
2940 /* Parse a sequence of adjacent string constants.  Returns a
2941    TREE_STRING representing the combined, nul-terminated string
2942    constant.  If TRANSLATE is true, translate the string to the
2943    execution character set.  If WIDE_OK is true, a wide string is
2944    invalid here.
2945
2946    C++98 [lex.string] says that if a narrow string literal token is
2947    adjacent to a wide string literal token, the behavior is undefined.
2948    However, C99 6.4.5p4 says that this results in a wide string literal.
2949    We follow C99 here, for consistency with the C front end.
2950
2951    This code is largely lifted from lex_string() in c-lex.c.
2952
2953    FUTURE: ObjC++ will need to handle @-strings here.  */
2954 static tree
2955 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2956 {
2957   tree value;
2958   size_t count;
2959   struct obstack str_ob;
2960   cpp_string str, istr, *strs;
2961   cp_token *tok;
2962   enum cpp_ttype type;
2963
2964   tok = cp_lexer_peek_token (parser->lexer);
2965   if (!cp_parser_is_string_literal (tok))
2966     {
2967       cp_parser_error (parser, "expected string-literal");
2968       return error_mark_node;
2969     }
2970
2971   type = tok->type;
2972
2973   /* Try to avoid the overhead of creating and destroying an obstack
2974      for the common case of just one string.  */
2975   if (!cp_parser_is_string_literal
2976       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2977     {
2978       cp_lexer_consume_token (parser->lexer);
2979
2980       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2981       str.len = TREE_STRING_LENGTH (tok->u.value);
2982       count = 1;
2983
2984       strs = &str;
2985     }
2986   else
2987     {
2988       gcc_obstack_init (&str_ob);
2989       count = 0;
2990
2991       do
2992         {
2993           cp_lexer_consume_token (parser->lexer);
2994           count++;
2995           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2996           str.len = TREE_STRING_LENGTH (tok->u.value);
2997
2998           if (type != tok->type)
2999             {
3000               if (type == CPP_STRING)
3001                 type = tok->type;
3002               else if (tok->type != CPP_STRING)
3003                 error_at (tok->location,
3004                           "unsupported non-standard concatenation "
3005                           "of string literals");
3006             }
3007
3008           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3009
3010           tok = cp_lexer_peek_token (parser->lexer);
3011         }
3012       while (cp_parser_is_string_literal (tok));
3013
3014       strs = (cpp_string *) obstack_finish (&str_ob);
3015     }
3016
3017   if (type != CPP_STRING && !wide_ok)
3018     {
3019       cp_parser_error (parser, "a wide string is invalid in this context");
3020       type = CPP_STRING;
3021     }
3022
3023   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3024       (parse_in, strs, count, &istr, type))
3025     {
3026       value = build_string (istr.len, (const char *)istr.text);
3027       free (CONST_CAST (unsigned char *, istr.text));
3028
3029       switch (type)
3030         {
3031         default:
3032         case CPP_STRING:
3033         case CPP_UTF8STRING:
3034           TREE_TYPE (value) = char_array_type_node;
3035           break;
3036         case CPP_STRING16:
3037           TREE_TYPE (value) = char16_array_type_node;
3038           break;
3039         case CPP_STRING32:
3040           TREE_TYPE (value) = char32_array_type_node;
3041           break;
3042         case CPP_WSTRING:
3043           TREE_TYPE (value) = wchar_array_type_node;
3044           break;
3045         }
3046
3047       value = fix_string_type (value);
3048     }
3049   else
3050     /* cpp_interpret_string has issued an error.  */
3051     value = error_mark_node;
3052
3053   if (count > 1)
3054     obstack_free (&str_ob, 0);
3055
3056   return value;
3057 }
3058
3059
3060 /* Basic concepts [gram.basic]  */
3061
3062 /* Parse a translation-unit.
3063
3064    translation-unit:
3065      declaration-seq [opt]
3066
3067    Returns TRUE if all went well.  */
3068
3069 static bool
3070 cp_parser_translation_unit (cp_parser* parser)
3071 {
3072   /* The address of the first non-permanent object on the declarator
3073      obstack.  */
3074   static void *declarator_obstack_base;
3075
3076   bool success;
3077
3078   /* Create the declarator obstack, if necessary.  */
3079   if (!cp_error_declarator)
3080     {
3081       gcc_obstack_init (&declarator_obstack);
3082       /* Create the error declarator.  */
3083       cp_error_declarator = make_declarator (cdk_error);
3084       /* Create the empty parameter list.  */
3085       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3086       /* Remember where the base of the declarator obstack lies.  */
3087       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3088     }
3089
3090   cp_parser_declaration_seq_opt (parser);
3091
3092   /* If there are no tokens left then all went well.  */
3093   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3094     {
3095       /* Get rid of the token array; we don't need it any more.  */
3096       cp_lexer_destroy (parser->lexer);
3097       parser->lexer = NULL;
3098
3099       /* This file might have been a context that's implicitly extern
3100          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3101       if (parser->implicit_extern_c)
3102         {
3103           pop_lang_context ();
3104           parser->implicit_extern_c = false;
3105         }
3106
3107       /* Finish up.  */
3108       finish_translation_unit ();
3109
3110       success = true;
3111     }
3112   else
3113     {
3114       cp_parser_error (parser, "expected declaration");
3115       success = false;
3116     }
3117
3118   /* Make sure the declarator obstack was fully cleaned up.  */
3119   gcc_assert (obstack_next_free (&declarator_obstack)
3120               == declarator_obstack_base);
3121
3122   /* All went well.  */
3123   return success;
3124 }
3125
3126 /* Expressions [gram.expr] */
3127
3128 /* Parse a primary-expression.
3129
3130    primary-expression:
3131      literal
3132      this
3133      ( expression )
3134      id-expression
3135
3136    GNU Extensions:
3137
3138    primary-expression:
3139      ( compound-statement )
3140      __builtin_va_arg ( assignment-expression , type-id )
3141      __builtin_offsetof ( type-id , offsetof-expression )
3142
3143    C++ Extensions:
3144      __has_nothrow_assign ( type-id )   
3145      __has_nothrow_constructor ( type-id )
3146      __has_nothrow_copy ( type-id )
3147      __has_trivial_assign ( type-id )   
3148      __has_trivial_constructor ( type-id )
3149      __has_trivial_copy ( type-id )
3150      __has_trivial_destructor ( type-id )
3151      __has_virtual_destructor ( type-id )     
3152      __is_abstract ( type-id )
3153      __is_base_of ( type-id , type-id )
3154      __is_class ( type-id )
3155      __is_convertible_to ( type-id , type-id )     
3156      __is_empty ( type-id )
3157      __is_enum ( type-id )
3158      __is_pod ( type-id )
3159      __is_polymorphic ( type-id )
3160      __is_union ( type-id )
3161
3162    Objective-C++ Extension:
3163
3164    primary-expression:
3165      objc-expression
3166
3167    literal:
3168      __null
3169
3170    ADDRESS_P is true iff this expression was immediately preceded by
3171    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3172    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3173    true iff this expression is a template argument.
3174
3175    Returns a representation of the expression.  Upon return, *IDK
3176    indicates what kind of id-expression (if any) was present.  */
3177
3178 static tree
3179 cp_parser_primary_expression (cp_parser *parser,
3180                               bool address_p,
3181                               bool cast_p,
3182                               bool template_arg_p,
3183                               cp_id_kind *idk)
3184 {
3185   cp_token *token = NULL;
3186
3187   /* Assume the primary expression is not an id-expression.  */
3188   *idk = CP_ID_KIND_NONE;
3189
3190   /* Peek at the next token.  */
3191   token = cp_lexer_peek_token (parser->lexer);
3192   switch (token->type)
3193     {
3194       /* literal:
3195            integer-literal
3196            character-literal
3197            floating-literal
3198            string-literal
3199            boolean-literal  */
3200     case CPP_CHAR:
3201     case CPP_CHAR16:
3202     case CPP_CHAR32:
3203     case CPP_WCHAR:
3204     case CPP_NUMBER:
3205       token = cp_lexer_consume_token (parser->lexer);
3206       if (TREE_CODE (token->u.value) == FIXED_CST)
3207         {
3208           error_at (token->location,
3209                     "fixed-point types not supported in C++");
3210           return error_mark_node;
3211         }
3212       /* Floating-point literals are only allowed in an integral
3213          constant expression if they are cast to an integral or
3214          enumeration type.  */
3215       if (TREE_CODE (token->u.value) == REAL_CST
3216           && parser->integral_constant_expression_p
3217           && pedantic)
3218         {
3219           /* CAST_P will be set even in invalid code like "int(2.7 +
3220              ...)".   Therefore, we have to check that the next token
3221              is sure to end the cast.  */
3222           if (cast_p)
3223             {
3224               cp_token *next_token;
3225
3226               next_token = cp_lexer_peek_token (parser->lexer);
3227               if (/* The comma at the end of an
3228                      enumerator-definition.  */
3229                   next_token->type != CPP_COMMA
3230                   /* The curly brace at the end of an enum-specifier.  */
3231                   && next_token->type != CPP_CLOSE_BRACE
3232                   /* The end of a statement.  */
3233                   && next_token->type != CPP_SEMICOLON
3234                   /* The end of the cast-expression.  */
3235                   && next_token->type != CPP_CLOSE_PAREN
3236                   /* The end of an array bound.  */
3237                   && next_token->type != CPP_CLOSE_SQUARE
3238                   /* The closing ">" in a template-argument-list.  */
3239                   && (next_token->type != CPP_GREATER
3240                       || parser->greater_than_is_operator_p)
3241                   /* C++0x only: A ">>" treated like two ">" tokens,
3242                      in a template-argument-list.  */
3243                   && (next_token->type != CPP_RSHIFT
3244                       || (cxx_dialect == cxx98)
3245                       || parser->greater_than_is_operator_p))
3246                 cast_p = false;
3247             }
3248
3249           /* If we are within a cast, then the constraint that the
3250              cast is to an integral or enumeration type will be
3251              checked at that point.  If we are not within a cast, then
3252              this code is invalid.  */
3253           if (!cast_p)
3254             cp_parser_non_integral_constant_expression
3255               (parser, "floating-point literal");
3256         }
3257       return token->u.value;
3258
3259     case CPP_STRING:
3260     case CPP_STRING16:
3261     case CPP_STRING32:
3262     case CPP_WSTRING:
3263     case CPP_UTF8STRING:
3264       /* ??? Should wide strings be allowed when parser->translate_strings_p
3265          is false (i.e. in attributes)?  If not, we can kill the third
3266          argument to cp_parser_string_literal.  */
3267       return cp_parser_string_literal (parser,
3268                                        parser->translate_strings_p,
3269                                        true);
3270
3271     case CPP_OPEN_PAREN:
3272       {
3273         tree expr;
3274         bool saved_greater_than_is_operator_p;
3275
3276         /* Consume the `('.  */
3277         cp_lexer_consume_token (parser->lexer);
3278         /* Within a parenthesized expression, a `>' token is always
3279            the greater-than operator.  */
3280         saved_greater_than_is_operator_p
3281           = parser->greater_than_is_operator_p;
3282         parser->greater_than_is_operator_p = true;
3283         /* If we see `( { ' then we are looking at the beginning of
3284            a GNU statement-expression.  */
3285         if (cp_parser_allow_gnu_extensions_p (parser)
3286             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3287           {
3288             /* Statement-expressions are not allowed by the standard.  */
3289             pedwarn (token->location, OPT_pedantic, 
3290                      "ISO C++ forbids braced-groups within expressions");
3291
3292             /* And they're not allowed outside of a function-body; you
3293                cannot, for example, write:
3294
3295                  int i = ({ int j = 3; j + 1; });
3296
3297                at class or namespace scope.  */
3298             if (!parser->in_function_body
3299                 || parser->in_template_argument_list_p)
3300               {
3301                 error_at (token->location,
3302                           "statement-expressions are not allowed outside "
3303                           "functions nor in template-argument lists");
3304                 cp_parser_skip_to_end_of_block_or_statement (parser);
3305                 expr = error_mark_node;
3306               }
3307             else
3308               {
3309                 /* Start the statement-expression.  */
3310                 expr = begin_stmt_expr ();
3311                 /* Parse the compound-statement.  */
3312                 cp_parser_compound_statement (parser, expr, false);
3313                 /* Finish up.  */
3314                 expr = finish_stmt_expr (expr, false);
3315               }
3316           }
3317         else
3318           {
3319             /* Parse the parenthesized expression.  */
3320             expr = cp_parser_expression (parser, cast_p, idk);
3321             /* Let the front end know that this expression was
3322                enclosed in parentheses. This matters in case, for
3323                example, the expression is of the form `A::B', since
3324                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3325                not.  */
3326             finish_parenthesized_expr (expr);
3327           }
3328         /* The `>' token might be the end of a template-id or
3329            template-parameter-list now.  */
3330         parser->greater_than_is_operator_p
3331           = saved_greater_than_is_operator_p;
3332         /* Consume the `)'.  */
3333         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3334           cp_parser_skip_to_end_of_statement (parser);
3335
3336         return expr;
3337       }
3338
3339     case CPP_OPEN_SQUARE:
3340       if (c_dialect_objc ())
3341         /* We have an Objective-C++ message. */
3342         return cp_parser_objc_expression (parser);
3343       maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3344       return cp_parser_lambda_expression (parser);
3345
3346     case CPP_OBJC_STRING:
3347       if (c_dialect_objc ())
3348         /* We have an Objective-C++ string literal. */
3349         return cp_parser_objc_expression (parser);
3350       cp_parser_error (parser, "expected primary-expression");
3351       return error_mark_node;
3352
3353     case CPP_KEYWORD:
3354       switch (token->keyword)
3355         {
3356           /* These two are the boolean literals.  */
3357         case RID_TRUE:
3358           cp_lexer_consume_token (parser->lexer);
3359           return boolean_true_node;
3360         case RID_FALSE:
3361           cp_lexer_consume_token (parser->lexer);
3362           return boolean_false_node;
3363
3364           /* The `__null' literal.  */
3365         case RID_NULL:
3366           cp_lexer_consume_token (parser->lexer);
3367           return null_node;
3368
3369           /* Recognize the `this' keyword.  */
3370         case RID_THIS:
3371           cp_lexer_consume_token (parser->lexer);
3372           if (parser->local_variables_forbidden_p)
3373             {
3374               error_at (token->location,
3375                         "%<this%> may not be used in this context");
3376               return error_mark_node;
3377             }
3378           /* Pointers cannot appear in constant-expressions.  */
3379           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3380             return error_mark_node;
3381           return finish_this_expr ();
3382
3383           /* The `operator' keyword can be the beginning of an
3384              id-expression.  */
3385         case RID_OPERATOR:
3386           goto id_expression;
3387
3388         case RID_FUNCTION_NAME:
3389         case RID_PRETTY_FUNCTION_NAME:
3390         case RID_C99_FUNCTION_NAME:
3391           {
3392             const char *name;
3393
3394             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3395                __func__ are the names of variables -- but they are
3396                treated specially.  Therefore, they are handled here,
3397                rather than relying on the generic id-expression logic
3398                below.  Grammatically, these names are id-expressions.
3399
3400                Consume the token.  */
3401             token = cp_lexer_consume_token (parser->lexer);
3402
3403             switch (token->keyword)
3404               {
3405               case RID_FUNCTION_NAME:
3406                 name = "%<__FUNCTION__%>";
3407                 break;
3408               case RID_PRETTY_FUNCTION_NAME:
3409                 name = "%<__PRETTY_FUNCTION__%>";
3410                 break;
3411               case RID_C99_FUNCTION_NAME:
3412                 name = "%<__func__%>";
3413                 break;
3414               default:
3415                 gcc_unreachable ();
3416               }
3417
3418             if (cp_parser_non_integral_constant_expression (parser, name))
3419               return error_mark_node;
3420
3421             /* Look up the name.  */
3422             return finish_fname (token->u.value);
3423           }
3424
3425         case RID_VA_ARG:
3426           {
3427             tree expression;
3428             tree type;
3429
3430             /* The `__builtin_va_arg' construct is used to handle
3431                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3432             cp_lexer_consume_token (parser->lexer);
3433             /* Look for the opening `('.  */
3434             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3435             /* Now, parse the assignment-expression.  */
3436             expression = cp_parser_assignment_expression (parser,
3437                                                           /*cast_p=*/false, NULL);
3438             /* Look for the `,'.  */
3439             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3440             /* Parse the type-id.  */
3441             type = cp_parser_type_id (parser);
3442             /* Look for the closing `)'.  */
3443             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3444             /* Using `va_arg' in a constant-expression is not
3445                allowed.  */
3446             if (cp_parser_non_integral_constant_expression (parser,
3447                                                             "%<va_arg%>"))
3448               return error_mark_node;
3449             return build_x_va_arg (expression, type);
3450           }
3451
3452         case RID_OFFSETOF:
3453           return cp_parser_builtin_offsetof (parser);
3454
3455         case RID_HAS_NOTHROW_ASSIGN:
3456         case RID_HAS_NOTHROW_CONSTRUCTOR:
3457         case RID_HAS_NOTHROW_COPY:        
3458         case RID_HAS_TRIVIAL_ASSIGN:
3459         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3460         case RID_HAS_TRIVIAL_COPY:        
3461         case RID_HAS_TRIVIAL_DESTRUCTOR:
3462         case RID_HAS_VIRTUAL_DESTRUCTOR:
3463         case RID_IS_ABSTRACT:
3464         case RID_IS_BASE_OF:
3465         case RID_IS_CLASS:
3466         case RID_IS_CONVERTIBLE_TO:
3467         case RID_IS_EMPTY:
3468         case RID_IS_ENUM:
3469         case RID_IS_POD:
3470         case RID_IS_POLYMORPHIC:
3471         case RID_IS_STD_LAYOUT:
3472         case RID_IS_TRIVIAL:
3473         case RID_IS_UNION:
3474           return cp_parser_trait_expr (parser, token->keyword);
3475
3476         /* Objective-C++ expressions.  */
3477         case RID_AT_ENCODE:
3478         case RID_AT_PROTOCOL:
3479         case RID_AT_SELECTOR:
3480           return cp_parser_objc_expression (parser);
3481
3482         default:
3483           cp_parser_error (parser, "expected primary-expression");
3484           return error_mark_node;
3485         }
3486
3487       /* An id-expression can start with either an identifier, a
3488          `::' as the beginning of a qualified-id, or the "operator"
3489          keyword.  */
3490     case CPP_NAME:
3491     case CPP_SCOPE:
3492     case CPP_TEMPLATE_ID:
3493     case CPP_NESTED_NAME_SPECIFIER:
3494       {
3495         tree id_expression;
3496         tree decl;
3497         const char *error_msg;
3498         bool template_p;
3499         bool done;
3500         cp_token *id_expr_token;
3501
3502       id_expression:
3503         /* Parse the id-expression.  */
3504         id_expression
3505           = cp_parser_id_expression (parser,
3506                                      /*template_keyword_p=*/false,
3507                                      /*check_dependency_p=*/true,
3508                                      &template_p,
3509                                      /*declarator_p=*/false,
3510                                      /*optional_p=*/false);
3511         if (id_expression == error_mark_node)
3512           return error_mark_node;
3513         id_expr_token = token;
3514         token = cp_lexer_peek_token (parser->lexer);
3515         done = (token->type != CPP_OPEN_SQUARE
3516                 && token->type != CPP_OPEN_PAREN
3517                 && token->type != CPP_DOT
3518                 && token->type != CPP_DEREF
3519                 && token->type != CPP_PLUS_PLUS
3520                 && token->type != CPP_MINUS_MINUS);
3521         /* If we have a template-id, then no further lookup is
3522            required.  If the template-id was for a template-class, we
3523            will sometimes have a TYPE_DECL at this point.  */
3524         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3525                  || TREE_CODE (id_expression) == TYPE_DECL)
3526           decl = id_expression;
3527         /* Look up the name.  */
3528         else
3529           {
3530             tree ambiguous_decls;
3531
3532             /* If we already know that this lookup is ambiguous, then
3533                we've already issued an error message; there's no reason
3534                to check again.  */
3535             if (id_expr_token->type == CPP_NAME
3536                 && id_expr_token->ambiguous_p)
3537               {
3538                 cp_parser_simulate_error (parser);
3539                 return error_mark_node;
3540               }
3541
3542             decl = cp_parser_lookup_name (parser, id_expression,
3543                                           none_type,
3544                                           template_p,
3545                                           /*is_namespace=*/false,
3546                                           /*check_dependency=*/true,
3547                                           &ambiguous_decls,
3548                                           id_expr_token->location);
3549             /* If the lookup was ambiguous, an error will already have
3550                been issued.  */
3551             if (ambiguous_decls)
3552               return error_mark_node;
3553
3554             /* In Objective-C++, an instance variable (ivar) may be preferred
3555                to whatever cp_parser_lookup_name() found.  */
3556             decl = objc_lookup_ivar (decl, id_expression);
3557
3558             /* If name lookup gives us a SCOPE_REF, then the
3559                qualifying scope was dependent.  */
3560             if (TREE_CODE (decl) == SCOPE_REF)
3561               {
3562                 /* At this point, we do not know if DECL is a valid
3563                    integral constant expression.  We assume that it is
3564                    in fact such an expression, so that code like:
3565
3566                       template <int N> struct A {
3567                         int a[B<N>::i];
3568                       };
3569                      
3570                    is accepted.  At template-instantiation time, we
3571                    will check that B<N>::i is actually a constant.  */
3572                 return decl;
3573               }
3574             /* Check to see if DECL is a local variable in a context
3575                where that is forbidden.  */
3576             if (parser->local_variables_forbidden_p
3577                 && local_variable_p (decl))
3578               {
3579                 /* It might be that we only found DECL because we are
3580                    trying to be generous with pre-ISO scoping rules.
3581                    For example, consider:
3582
3583                      int i;
3584                      void g() {
3585                        for (int i = 0; i < 10; ++i) {}
3586                        extern void f(int j = i);
3587                      }
3588
3589                    Here, name look up will originally find the out
3590                    of scope `i'.  We need to issue a warning message,
3591                    but then use the global `i'.  */
3592                 decl = check_for_out_of_scope_variable (decl);
3593                 if (local_variable_p (decl))
3594                   {
3595                     error_at (id_expr_token->location,
3596                               "local variable %qD may not appear in this context",
3597                               decl);
3598                     return error_mark_node;
3599                   }
3600               }
3601           }
3602
3603         decl = (finish_id_expression
3604                 (id_expression, decl, parser->scope,
3605                  idk,
3606                  parser->integral_constant_expression_p,
3607                  parser->allow_non_integral_constant_expression_p,
3608                  &parser->non_integral_constant_expression_p,
3609                  template_p, done, address_p,
3610                  template_arg_p,
3611                  &error_msg,
3612                  id_expr_token->location));
3613         if (error_msg)
3614           cp_parser_error (parser, error_msg);
3615         return decl;
3616       }
3617
3618       /* Anything else is an error.  */
3619     default:
3620       cp_parser_error (parser, "expected primary-expression");
3621       return error_mark_node;
3622     }
3623 }
3624
3625 /* Parse an id-expression.
3626
3627    id-expression:
3628      unqualified-id
3629      qualified-id
3630
3631    qualified-id:
3632      :: [opt] nested-name-specifier template [opt] unqualified-id
3633      :: identifier
3634      :: operator-function-id
3635      :: template-id
3636
3637    Return a representation of the unqualified portion of the
3638    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3639    a `::' or nested-name-specifier.
3640
3641    Often, if the id-expression was a qualified-id, the caller will
3642    want to make a SCOPE_REF to represent the qualified-id.  This
3643    function does not do this in order to avoid wastefully creating
3644    SCOPE_REFs when they are not required.
3645
3646    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3647    `template' keyword.
3648
3649    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3650    uninstantiated templates.
3651
3652    If *TEMPLATE_P is non-NULL, it is set to true iff the
3653    `template' keyword is used to explicitly indicate that the entity
3654    named is a template.
3655
3656    If DECLARATOR_P is true, the id-expression is appearing as part of
3657    a declarator, rather than as part of an expression.  */
3658
3659 static tree
3660 cp_parser_id_expression (cp_parser *parser,
3661                          bool template_keyword_p,
3662                          bool check_dependency_p,
3663                          bool *template_p,
3664                          bool declarator_p,
3665                          bool optional_p)
3666 {
3667   bool global_scope_p;
3668   bool nested_name_specifier_p;
3669
3670   /* Assume the `template' keyword was not used.  */
3671   if (template_p)
3672     *template_p = template_keyword_p;
3673
3674   /* Look for the optional `::' operator.  */
3675   global_scope_p
3676     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3677        != NULL_TREE);
3678   /* Look for the optional nested-name-specifier.  */
3679   nested_name_specifier_p
3680     = (cp_parser_nested_name_specifier_opt (parser,
3681                                             /*typename_keyword_p=*/false,
3682                                             check_dependency_p,
3683                                             /*type_p=*/false,
3684                                             declarator_p)
3685        != NULL_TREE);
3686   /* If there is a nested-name-specifier, then we are looking at
3687      the first qualified-id production.  */
3688   if (nested_name_specifier_p)
3689     {
3690       tree saved_scope;
3691       tree saved_object_scope;
3692       tree saved_qualifying_scope;
3693       tree unqualified_id;
3694       bool is_template;
3695
3696       /* See if the next token is the `template' keyword.  */
3697       if (!template_p)
3698         template_p = &is_template;
3699       *template_p = cp_parser_optional_template_keyword (parser);
3700       /* Name lookup we do during the processing of the
3701          unqualified-id might obliterate SCOPE.  */
3702       saved_scope = parser->scope;
3703       saved_object_scope = parser->object_scope;
3704       saved_qualifying_scope = parser->qualifying_scope;
3705       /* Process the final unqualified-id.  */
3706       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3707                                                  check_dependency_p,
3708                                                  declarator_p,
3709                                                  /*optional_p=*/false);
3710       /* Restore the SAVED_SCOPE for our caller.  */
3711       parser->scope = saved_scope;
3712       parser->object_scope = saved_object_scope;
3713       parser->qualifying_scope = saved_qualifying_scope;
3714
3715       return unqualified_id;
3716     }
3717   /* Otherwise, if we are in global scope, then we are looking at one
3718      of the other qualified-id productions.  */
3719   else if (global_scope_p)
3720     {
3721       cp_token *token;
3722       tree id;
3723
3724       /* Peek at the next token.  */
3725       token = cp_lexer_peek_token (parser->lexer);
3726
3727       /* If it's an identifier, and the next token is not a "<", then
3728          we can avoid the template-id case.  This is an optimization
3729          for this common case.  */
3730       if (token->type == CPP_NAME
3731           && !cp_parser_nth_token_starts_template_argument_list_p
3732                (parser, 2))
3733         return cp_parser_identifier (parser);
3734
3735       cp_parser_parse_tentatively (parser);
3736       /* Try a template-id.  */
3737       id = cp_parser_template_id (parser,
3738                                   /*template_keyword_p=*/false,
3739                                   /*check_dependency_p=*/true,
3740                                   declarator_p);
3741       /* If that worked, we're done.  */
3742       if (cp_parser_parse_definitely (parser))
3743         return id;
3744
3745       /* Peek at the next token.  (Changes in the token buffer may
3746          have invalidated the pointer obtained above.)  */
3747       token = cp_lexer_peek_token (parser->lexer);
3748
3749       switch (token->type)
3750         {
3751         case CPP_NAME:
3752           return cp_parser_identifier (parser);
3753
3754         case CPP_KEYWORD:
3755           if (token->keyword == RID_OPERATOR)
3756             return cp_parser_operator_function_id (parser);
3757           /* Fall through.  */
3758
3759         default:
3760           cp_parser_error (parser, "expected id-expression");
3761           return error_mark_node;
3762         }
3763     }
3764   else
3765     return cp_parser_unqualified_id (parser, template_keyword_p,
3766                                      /*check_dependency_p=*/true,
3767                                      declarator_p,
3768                                      optional_p);
3769 }
3770
3771 /* Parse an unqualified-id.
3772
3773    unqualified-id:
3774      identifier
3775      operator-function-id
3776      conversion-function-id
3777      ~ class-name
3778      template-id
3779
3780    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3781    keyword, in a construct like `A::template ...'.
3782
3783    Returns a representation of unqualified-id.  For the `identifier'
3784    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3785    production a BIT_NOT_EXPR is returned; the operand of the
3786    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3787    other productions, see the documentation accompanying the
3788    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3789    names are looked up in uninstantiated templates.  If DECLARATOR_P
3790    is true, the unqualified-id is appearing as part of a declarator,
3791    rather than as part of an expression.  */
3792
3793 static tree
3794 cp_parser_unqualified_id (cp_parser* parser,
3795                           bool template_keyword_p,
3796                           bool check_dependency_p,
3797                           bool declarator_p,
3798                           bool optional_p)
3799 {
3800   cp_token *token;
3801
3802   /* Peek at the next token.  */
3803   token = cp_lexer_peek_token (parser->lexer);
3804
3805   switch (token->type)
3806     {
3807     case CPP_NAME:
3808       {
3809         tree id;
3810
3811         /* We don't know yet whether or not this will be a
3812            template-id.  */
3813         cp_parser_parse_tentatively (parser);
3814         /* Try a template-id.  */
3815         id = cp_parser_template_id (parser, template_keyword_p,
3816                                     check_dependency_p,
3817                                     declarator_p);
3818         /* If it worked, we're done.  */
3819         if (cp_parser_parse_definitely (parser))
3820           return id;
3821         /* Otherwise, it's an ordinary identifier.  */
3822         return cp_parser_identifier (parser);
3823       }
3824
3825     case CPP_TEMPLATE_ID:
3826       return cp_parser_template_id (parser, template_keyword_p,
3827                                     check_dependency_p,
3828                                     declarator_p);
3829
3830     case CPP_COMPL:
3831       {
3832         tree type_decl;
3833         tree qualifying_scope;
3834         tree object_scope;
3835         tree scope;
3836         bool done;
3837
3838         /* Consume the `~' token.  */
3839         cp_lexer_consume_token (parser->lexer);
3840         /* Parse the class-name.  The standard, as written, seems to
3841            say that:
3842
3843              template <typename T> struct S { ~S (); };
3844              template <typename T> S<T>::~S() {}
3845
3846            is invalid, since `~' must be followed by a class-name, but
3847            `S<T>' is dependent, and so not known to be a class.
3848            That's not right; we need to look in uninstantiated
3849            templates.  A further complication arises from:
3850
3851              template <typename T> void f(T t) {
3852                t.T::~T();
3853              }
3854
3855            Here, it is not possible to look up `T' in the scope of `T'
3856            itself.  We must look in both the current scope, and the
3857            scope of the containing complete expression.
3858
3859            Yet another issue is:
3860
3861              struct S {
3862                int S;
3863                ~S();
3864              };
3865
3866              S::~S() {}
3867
3868            The standard does not seem to say that the `S' in `~S'
3869            should refer to the type `S' and not the data member
3870            `S::S'.  */
3871
3872         /* DR 244 says that we look up the name after the "~" in the
3873            same scope as we looked up the qualifying name.  That idea
3874            isn't fully worked out; it's more complicated than that.  */
3875         scope = parser->scope;
3876         object_scope = parser->object_scope;
3877         qualifying_scope = parser->qualifying_scope;
3878
3879         /* Check for invalid scopes.  */
3880         if (scope == error_mark_node)
3881           {
3882             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3883               cp_lexer_consume_token (parser->lexer);
3884             return error_mark_node;
3885           }
3886         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3887           {
3888             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3889               error_at (token->location,
3890                         "scope %qT before %<~%> is not a class-name",
3891                         scope);
3892             cp_parser_simulate_error (parser);
3893             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3894               cp_lexer_consume_token (parser->lexer);
3895             return error_mark_node;
3896           }
3897         gcc_assert (!scope || TYPE_P (scope));
3898
3899         /* If the name is of the form "X::~X" it's OK.  */
3900         token = cp_lexer_peek_token (parser->lexer);
3901         if (scope
3902             && token->type == CPP_NAME
3903             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3904                 != CPP_LESS)
3905             && constructor_name_p (token->u.value, scope))
3906           {
3907             cp_lexer_consume_token (parser->lexer);
3908             return build_nt (BIT_NOT_EXPR, scope);
3909           }
3910
3911         /* If there was an explicit qualification (S::~T), first look
3912            in the scope given by the qualification (i.e., S).
3913
3914            Note: in the calls to cp_parser_class_name below we pass
3915            typename_type so that lookup finds the injected-class-name
3916            rather than the constructor.  */
3917         done = false;
3918         type_decl = NULL_TREE;
3919         if (scope)
3920           {
3921             cp_parser_parse_tentatively (parser);
3922             type_decl = cp_parser_class_name (parser,
3923                                               /*typename_keyword_p=*/false,
3924                                               /*template_keyword_p=*/false,
3925                                               typename_type,
3926                                               /*check_dependency=*/false,
3927                                               /*class_head_p=*/false,
3928                                               declarator_p);
3929             if (cp_parser_parse_definitely (parser))
3930               done = true;
3931           }
3932         /* In "N::S::~S", look in "N" as well.  */
3933         if (!done && scope && qualifying_scope)
3934           {
3935             cp_parser_parse_tentatively (parser);
3936             parser->scope = qualifying_scope;
3937             parser->object_scope = NULL_TREE;
3938             parser->qualifying_scope = NULL_TREE;
3939             type_decl
3940               = cp_parser_class_name (parser,
3941                                       /*typename_keyword_p=*/false,
3942                                       /*template_keyword_p=*/false,
3943                                       typename_type,
3944                                       /*check_dependency=*/false,
3945                                       /*class_head_p=*/false,
3946                                       declarator_p);
3947             if (cp_parser_parse_definitely (parser))
3948               done = true;
3949           }
3950         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3951         else if (!done && object_scope)
3952           {
3953             cp_parser_parse_tentatively (parser);
3954             parser->scope = object_scope;
3955             parser->object_scope = NULL_TREE;
3956             parser->qualifying_scope = NULL_TREE;
3957             type_decl
3958               = cp_parser_class_name (parser,
3959                                       /*typename_keyword_p=*/false,
3960                                       /*template_keyword_p=*/false,
3961                                       typename_type,
3962                                       /*check_dependency=*/false,
3963                                       /*class_head_p=*/false,
3964                                       declarator_p);
3965             if (cp_parser_parse_definitely (parser))
3966               done = true;
3967           }
3968         /* Look in the surrounding context.  */
3969         if (!done)
3970           {
3971             parser->scope = NULL_TREE;
3972             parser->object_scope = NULL_TREE;
3973             parser->qualifying_scope = NULL_TREE;
3974             if (processing_template_decl)
3975               cp_parser_parse_tentatively (parser);
3976             type_decl
3977               = cp_parser_class_name (parser,
3978                                       /*typename_keyword_p=*/false,
3979                                       /*template_keyword_p=*/false,
3980                                       typename_type,
3981                                       /*check_dependency=*/false,
3982                                       /*class_head_p=*/false,
3983                                       declarator_p);
3984             if (processing_template_decl
3985                 && ! cp_parser_parse_definitely (parser))
3986               {
3987                 /* We couldn't find a type with this name, so just accept
3988                    it and check for a match at instantiation time.  */
3989                 type_decl = cp_parser_identifier (parser);
3990                 if (type_decl != error_mark_node)
3991                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3992                 return type_decl;
3993               }
3994           }
3995         /* If an error occurred, assume that the name of the
3996            destructor is the same as the name of the qualifying
3997            class.  That allows us to keep parsing after running
3998            into ill-formed destructor names.  */
3999         if (type_decl == error_mark_node && scope)
4000           return build_nt (BIT_NOT_EXPR, scope);
4001         else if (type_decl == error_mark_node)
4002           return error_mark_node;
4003
4004         /* Check that destructor name and scope match.  */
4005         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4006           {
4007             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4008               error_at (token->location,
4009                         "declaration of %<~%T%> as member of %qT",
4010                         type_decl, scope);
4011             cp_parser_simulate_error (parser);
4012             return error_mark_node;
4013           }
4014
4015         /* [class.dtor]
4016
4017            A typedef-name that names a class shall not be used as the
4018            identifier in the declarator for a destructor declaration.  */
4019         if (declarator_p
4020             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4021             && !DECL_SELF_REFERENCE_P (type_decl)
4022             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4023           error_at (token->location,
4024                     "typedef-name %qD used as destructor declarator",
4025                     type_decl);
4026
4027         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4028       }
4029
4030     case CPP_KEYWORD:
4031       if (token->keyword == RID_OPERATOR)
4032         {
4033           tree id;
4034
4035           /* This could be a template-id, so we try that first.  */
4036           cp_parser_parse_tentatively (parser);
4037           /* Try a template-id.  */
4038           id = cp_parser_template_id (parser, template_keyword_p,
4039                                       /*check_dependency_p=*/true,
4040                                       declarator_p);
4041           /* If that worked, we're done.  */
4042           if (cp_parser_parse_definitely (parser))
4043             return id;
4044           /* We still don't know whether we're looking at an
4045              operator-function-id or a conversion-function-id.  */
4046           cp_parser_parse_tentatively (parser);
4047           /* Try an operator-function-id.  */
4048           id = cp_parser_operator_function_id (parser);
4049           /* If that didn't work, try a conversion-function-id.  */
4050           if (!cp_parser_parse_definitely (parser))
4051             id = cp_parser_conversion_function_id (parser);
4052
4053           return id;
4054         }
4055       /* Fall through.  */
4056
4057     default:
4058       if (optional_p)
4059         return NULL_TREE;
4060       cp_parser_error (parser, "expected unqualified-id");
4061       return error_mark_node;
4062     }
4063 }
4064
4065 /* Parse an (optional) nested-name-specifier.
4066
4067    nested-name-specifier: [C++98]
4068      class-or-namespace-name :: nested-name-specifier [opt]
4069      class-or-namespace-name :: template nested-name-specifier [opt]
4070
4071    nested-name-specifier: [C++0x]
4072      type-name ::
4073      namespace-name ::
4074      nested-name-specifier identifier ::
4075      nested-name-specifier template [opt] simple-template-id ::
4076
4077    PARSER->SCOPE should be set appropriately before this function is
4078    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4079    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4080    in name lookups.
4081
4082    Sets PARSER->SCOPE to the class (TYPE) or namespace
4083    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4084    it unchanged if there is no nested-name-specifier.  Returns the new
4085    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4086
4087    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4088    part of a declaration and/or decl-specifier.  */
4089
4090 static tree
4091 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4092                                      bool typename_keyword_p,
4093                                      bool check_dependency_p,
4094                                      bool type_p,
4095                                      bool is_declaration)
4096 {
4097   bool success = false;
4098   cp_token_position start = 0;
4099   cp_token *token;
4100
4101   /* Remember where the nested-name-specifier starts.  */
4102   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4103     {
4104       start = cp_lexer_token_position (parser->lexer, false);
4105       push_deferring_access_checks (dk_deferred);
4106     }
4107
4108   while (true)
4109     {
4110       tree new_scope;
4111       tree old_scope;
4112       tree saved_qualifying_scope;
4113       bool template_keyword_p;
4114
4115       /* Spot cases that cannot be the beginning of a
4116          nested-name-specifier.  */
4117       token = cp_lexer_peek_token (parser->lexer);
4118
4119       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4120          the already parsed nested-name-specifier.  */
4121       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4122         {
4123           /* Grab the nested-name-specifier and continue the loop.  */
4124           cp_parser_pre_parsed_nested_name_specifier (parser);
4125           /* If we originally encountered this nested-name-specifier
4126              with IS_DECLARATION set to false, we will not have
4127              resolved TYPENAME_TYPEs, so we must do so here.  */
4128           if (is_declaration
4129               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4130             {
4131               new_scope = resolve_typename_type (parser->scope,
4132                                                  /*only_current_p=*/false);
4133               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4134                 parser->scope = new_scope;
4135             }
4136           success = true;
4137           continue;
4138         }
4139
4140       /* Spot cases that cannot be the beginning of a
4141          nested-name-specifier.  On the second and subsequent times
4142          through the loop, we look for the `template' keyword.  */
4143       if (success && token->keyword == RID_TEMPLATE)
4144         ;
4145       /* A template-id can start a nested-name-specifier.  */
4146       else if (token->type == CPP_TEMPLATE_ID)
4147         ;
4148       else
4149         {
4150           /* If the next token is not an identifier, then it is
4151              definitely not a type-name or namespace-name.  */
4152           if (token->type != CPP_NAME)
4153             break;
4154           /* If the following token is neither a `<' (to begin a
4155              template-id), nor a `::', then we are not looking at a
4156              nested-name-specifier.  */
4157           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4158           if (token->type != CPP_SCOPE
4159               && !cp_parser_nth_token_starts_template_argument_list_p
4160                   (parser, 2))
4161             break;
4162         }
4163
4164       /* The nested-name-specifier is optional, so we parse
4165          tentatively.  */
4166       cp_parser_parse_tentatively (parser);
4167
4168       /* Look for the optional `template' keyword, if this isn't the
4169          first time through the loop.  */
4170       if (success)
4171         template_keyword_p = cp_parser_optional_template_keyword (parser);
4172       else
4173         template_keyword_p = false;
4174
4175       /* Save the old scope since the name lookup we are about to do
4176          might destroy it.  */
4177       old_scope = parser->scope;
4178       saved_qualifying_scope = parser->qualifying_scope;
4179       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4180          look up names in "X<T>::I" in order to determine that "Y" is
4181          a template.  So, if we have a typename at this point, we make
4182          an effort to look through it.  */
4183       if (is_declaration
4184           && !typename_keyword_p
4185           && parser->scope
4186           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4187         parser->scope = resolve_typename_type (parser->scope,
4188                                                /*only_current_p=*/false);
4189       /* Parse the qualifying entity.  */
4190       new_scope
4191         = cp_parser_qualifying_entity (parser,
4192                                        typename_keyword_p,
4193                                        template_keyword_p,
4194                                        check_dependency_p,
4195                                        type_p,
4196                                        is_declaration);
4197       /* Look for the `::' token.  */
4198       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4199
4200       /* If we found what we wanted, we keep going; otherwise, we're
4201          done.  */
4202       if (!cp_parser_parse_definitely (parser))
4203         {
4204           bool error_p = false;
4205
4206           /* Restore the OLD_SCOPE since it was valid before the
4207              failed attempt at finding the last
4208              class-or-namespace-name.  */
4209           parser->scope = old_scope;
4210           parser->qualifying_scope = saved_qualifying_scope;
4211           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4212             break;
4213           /* If the next token is an identifier, and the one after
4214              that is a `::', then any valid interpretation would have
4215              found a class-or-namespace-name.  */
4216           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4217                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4218                      == CPP_SCOPE)
4219                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4220                      != CPP_COMPL))
4221             {
4222               token = cp_lexer_consume_token (parser->lexer);
4223               if (!error_p)
4224                 {
4225                   if (!token->ambiguous_p)
4226                     {
4227                       tree decl;
4228                       tree ambiguous_decls;
4229
4230                       decl = cp_parser_lookup_name (parser, token->u.value,
4231                                                     none_type,
4232                                                     /*is_template=*/false,
4233                                                     /*is_namespace=*/false,
4234                                                     /*check_dependency=*/true,
4235                                                     &ambiguous_decls,
4236                                                     token->location);
4237                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4238                         error_at (token->location,
4239                                   "%qD used without template parameters",
4240                                   decl);
4241                       else if (ambiguous_decls)
4242                         {
4243                           error_at (token->location,
4244                                     "reference to %qD is ambiguous",
4245                                     token->u.value);
4246                           print_candidates (ambiguous_decls);
4247                           decl = error_mark_node;
4248                         }
4249                       else
4250                         {
4251                           const char* msg = "is not a class or namespace";
4252                           if (cxx_dialect != cxx98)
4253                             msg = "is not a class, namespace, or enumeration";
4254                           cp_parser_name_lookup_error
4255                             (parser, token->u.value, decl, msg,
4256                              token->location);
4257                         }
4258                     }
4259                   parser->scope = error_mark_node;
4260                   error_p = true;
4261                   /* Treat this as a successful nested-name-specifier
4262                      due to:
4263
4264                      [basic.lookup.qual]
4265
4266                      If the name found is not a class-name (clause
4267                      _class_) or namespace-name (_namespace.def_), the
4268                      program is ill-formed.  */
4269                   success = true;
4270                 }
4271               cp_lexer_consume_token (parser->lexer);
4272             }
4273           break;
4274         }
4275       /* We've found one valid nested-name-specifier.  */
4276       success = true;
4277       /* Name lookup always gives us a DECL.  */
4278       if (TREE_CODE (new_scope) == TYPE_DECL)
4279         new_scope = TREE_TYPE (new_scope);
4280       /* Uses of "template" must be followed by actual templates.  */
4281       if (template_keyword_p
4282           && !(CLASS_TYPE_P (new_scope)
4283                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4284                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4285                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4286           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4287                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4288                    == TEMPLATE_ID_EXPR)))
4289         permerror (input_location, TYPE_P (new_scope)
4290                    ? "%qT is not a template"
4291                    : "%qD is not a template",
4292                    new_scope);
4293       /* If it is a class scope, try to complete it; we are about to
4294          be looking up names inside the class.  */
4295       if (TYPE_P (new_scope)
4296           /* Since checking types for dependency can be expensive,
4297              avoid doing it if the type is already complete.  */
4298           && !COMPLETE_TYPE_P (new_scope)
4299           /* Do not try to complete dependent types.  */
4300           && !dependent_type_p (new_scope))
4301         {
4302           new_scope = complete_type (new_scope);
4303           /* If it is a typedef to current class, use the current
4304              class instead, as the typedef won't have any names inside
4305              it yet.  */
4306           if (!COMPLETE_TYPE_P (new_scope)
4307               && currently_open_class (new_scope))
4308             new_scope = TYPE_MAIN_VARIANT (new_scope);
4309         }
4310       /* Make sure we look in the right scope the next time through
4311          the loop.  */
4312       parser->scope = new_scope;
4313     }
4314
4315   /* If parsing tentatively, replace the sequence of tokens that makes
4316      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4317      token.  That way, should we re-parse the token stream, we will
4318      not have to repeat the effort required to do the parse, nor will
4319      we issue duplicate error messages.  */
4320   if (success && start)
4321     {
4322       cp_token *token;
4323
4324       token = cp_lexer_token_at (parser->lexer, start);
4325       /* Reset the contents of the START token.  */
4326       token->type = CPP_NESTED_NAME_SPECIFIER;
4327       /* Retrieve any deferred checks.  Do not pop this access checks yet
4328          so the memory will not be reclaimed during token replacing below.  */
4329       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4330       token->u.tree_check_value->value = parser->scope;
4331       token->u.tree_check_value->checks = get_deferred_access_checks ();
4332       token->u.tree_check_value->qualifying_scope =
4333         parser->qualifying_scope;
4334       token->keyword = RID_MAX;
4335
4336       /* Purge all subsequent tokens.  */
4337       cp_lexer_purge_tokens_after (parser->lexer, start);
4338     }
4339
4340   if (start)
4341     pop_to_parent_deferring_access_checks ();
4342
4343   return success ? parser->scope : NULL_TREE;
4344 }
4345
4346 /* Parse a nested-name-specifier.  See
4347    cp_parser_nested_name_specifier_opt for details.  This function
4348    behaves identically, except that it will an issue an error if no
4349    nested-name-specifier is present.  */
4350
4351 static tree
4352 cp_parser_nested_name_specifier (cp_parser *parser,
4353                                  bool typename_keyword_p,
4354                                  bool check_dependency_p,
4355                                  bool type_p,
4356                                  bool is_declaration)
4357 {
4358   tree scope;
4359
4360   /* Look for the nested-name-specifier.  */
4361   scope = cp_parser_nested_name_specifier_opt (parser,
4362                                                typename_keyword_p,
4363                                                check_dependency_p,
4364                                                type_p,
4365                                                is_declaration);
4366   /* If it was not present, issue an error message.  */
4367   if (!scope)
4368     {
4369       cp_parser_error (parser, "expected nested-name-specifier");
4370       parser->scope = NULL_TREE;
4371     }
4372
4373   return scope;
4374 }
4375
4376 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4377    this is either a class-name or a namespace-name (which corresponds
4378    to the class-or-namespace-name production in the grammar). For
4379    C++0x, it can also be a type-name that refers to an enumeration
4380    type.
4381
4382    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4383    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4384    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4385    TYPE_P is TRUE iff the next name should be taken as a class-name,
4386    even the same name is declared to be another entity in the same
4387    scope.
4388
4389    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4390    specified by the class-or-namespace-name.  If neither is found the
4391    ERROR_MARK_NODE is returned.  */
4392
4393 static tree
4394 cp_parser_qualifying_entity (cp_parser *parser,
4395                              bool typename_keyword_p,
4396                              bool template_keyword_p,
4397                              bool check_dependency_p,
4398                              bool type_p,
4399                              bool is_declaration)
4400 {
4401   tree saved_scope;
4402   tree saved_qualifying_scope;
4403   tree saved_object_scope;
4404   tree scope;
4405   bool only_class_p;
4406   bool successful_parse_p;
4407
4408   /* Before we try to parse the class-name, we must save away the
4409      current PARSER->SCOPE since cp_parser_class_name will destroy
4410      it.  */
4411   saved_scope = parser->scope;
4412   saved_qualifying_scope = parser->qualifying_scope;
4413   saved_object_scope = parser->object_scope;
4414   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4415      there is no need to look for a namespace-name.  */
4416   only_class_p = template_keyword_p 
4417     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4418   if (!only_class_p)
4419     cp_parser_parse_tentatively (parser);
4420   scope = cp_parser_class_name (parser,
4421                                 typename_keyword_p,
4422                                 template_keyword_p,
4423                                 type_p ? class_type : none_type,
4424                                 check_dependency_p,
4425                                 /*class_head_p=*/false,
4426                                 is_declaration);
4427   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4428   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4429   if (!only_class_p 
4430       && cxx_dialect != cxx98
4431       && !successful_parse_p)
4432     {
4433       /* Restore the saved scope.  */
4434       parser->scope = saved_scope;
4435       parser->qualifying_scope = saved_qualifying_scope;
4436       parser->object_scope = saved_object_scope;
4437
4438       /* Parse tentatively.  */
4439       cp_parser_parse_tentatively (parser);
4440      
4441       /* Parse a typedef-name or enum-name.  */
4442       scope = cp_parser_nonclass_name (parser);
4443       successful_parse_p = cp_parser_parse_definitely (parser);
4444     }
4445   /* If that didn't work, try for a namespace-name.  */
4446   if (!only_class_p && !successful_parse_p)
4447     {
4448       /* Restore the saved scope.  */
4449       parser->scope = saved_scope;
4450       parser->qualifying_scope = saved_qualifying_scope;
4451       parser->object_scope = saved_object_scope;
4452       /* If we are not looking at an identifier followed by the scope
4453          resolution operator, then this is not part of a
4454          nested-name-specifier.  (Note that this function is only used
4455          to parse the components of a nested-name-specifier.)  */
4456       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4457           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4458         return error_mark_node;
4459       scope = cp_parser_namespace_name (parser);
4460     }
4461
4462   return scope;
4463 }
4464
4465 /* Parse a postfix-expression.
4466
4467    postfix-expression:
4468      primary-expression
4469      postfix-expression [ expression ]
4470      postfix-expression ( expression-list [opt] )
4471      simple-type-specifier ( expression-list [opt] )
4472      typename :: [opt] nested-name-specifier identifier
4473        ( expression-list [opt] )
4474      typename :: [opt] nested-name-specifier template [opt] template-id
4475        ( expression-list [opt] )
4476      postfix-expression . template [opt] id-expression
4477      postfix-expression -> template [opt] id-expression
4478      postfix-expression . pseudo-destructor-name
4479      postfix-expression -> pseudo-destructor-name
4480      postfix-expression ++
4481      postfix-expression --
4482      dynamic_cast < type-id > ( expression )
4483      static_cast < type-id > ( expression )
4484      reinterpret_cast < type-id > ( expression )
4485      const_cast < type-id > ( expression )
4486      typeid ( expression )
4487      typeid ( type-id )
4488
4489    GNU Extension:
4490
4491    postfix-expression:
4492      ( type-id ) { initializer-list , [opt] }
4493
4494    This extension is a GNU version of the C99 compound-literal
4495    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4496    but they are essentially the same concept.)
4497
4498    If ADDRESS_P is true, the postfix expression is the operand of the
4499    `&' operator.  CAST_P is true if this expression is the target of a
4500    cast.
4501
4502    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4503    class member access expressions [expr.ref].
4504
4505    Returns a representation of the expression.  */
4506
4507 static tree
4508 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4509                               bool member_access_only_p,
4510                               cp_id_kind * pidk_return)
4511 {
4512   cp_token *token;
4513   enum rid keyword;
4514   cp_id_kind idk = CP_ID_KIND_NONE;
4515   tree postfix_expression = NULL_TREE;
4516   bool is_member_access = false;
4517
4518   /* Peek at the next token.  */
4519   token = cp_lexer_peek_token (parser->lexer);
4520   /* Some of the productions are determined by keywords.  */
4521   keyword = token->keyword;
4522   switch (keyword)
4523     {
4524     case RID_DYNCAST:
4525     case RID_STATCAST:
4526     case RID_REINTCAST:
4527     case RID_CONSTCAST:
4528       {
4529         tree type;
4530         tree expression;
4531         const char *saved_message;
4532
4533         /* All of these can be handled in the same way from the point
4534            of view of parsing.  Begin by consuming the token
4535            identifying the cast.  */
4536         cp_lexer_consume_token (parser->lexer);
4537
4538         /* New types cannot be defined in the cast.  */
4539         saved_message = parser->type_definition_forbidden_message;
4540         parser->type_definition_forbidden_message
4541           = G_("types may not be defined in casts");
4542
4543         /* Look for the opening `<'.  */
4544         cp_parser_require (parser, CPP_LESS, "%<<%>");
4545         /* Parse the type to which we are casting.  */
4546         type = cp_parser_type_id (parser);
4547         /* Look for the closing `>'.  */
4548         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4549         /* Restore the old message.  */
4550         parser->type_definition_forbidden_message = saved_message;
4551
4552         /* And the expression which is being cast.  */
4553         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4554         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4555         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4556
4557         /* Only type conversions to integral or enumeration types
4558            can be used in constant-expressions.  */
4559         if (!cast_valid_in_integral_constant_expression_p (type)
4560             && (cp_parser_non_integral_constant_expression
4561                 (parser,
4562                  "a cast to a type other than an integral or "
4563                  "enumeration type")))
4564           return error_mark_node;
4565
4566         switch (keyword)
4567           {
4568           case RID_DYNCAST:
4569             postfix_expression
4570               = build_dynamic_cast (type, expression, tf_warning_or_error);
4571             break;
4572           case RID_STATCAST:
4573             postfix_expression
4574               = build_static_cast (type, expression, tf_warning_or_error);
4575             break;
4576           case RID_REINTCAST:
4577             postfix_expression
4578               = build_reinterpret_cast (type, expression, 
4579                                         tf_warning_or_error);
4580             break;
4581           case RID_CONSTCAST:
4582             postfix_expression
4583               = build_const_cast (type, expression, tf_warning_or_error);
4584             break;
4585           default:
4586             gcc_unreachable ();
4587           }
4588       }
4589       break;
4590
4591     case RID_TYPEID:
4592       {
4593         tree type;
4594         const char *saved_message;
4595         bool saved_in_type_id_in_expr_p;
4596
4597         /* Consume the `typeid' token.  */
4598         cp_lexer_consume_token (parser->lexer);
4599         /* Look for the `(' token.  */
4600         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4601         /* Types cannot be defined in a `typeid' expression.  */
4602         saved_message = parser->type_definition_forbidden_message;
4603         parser->type_definition_forbidden_message
4604           = G_("types may not be defined in a %<typeid%> expression");
4605         /* We can't be sure yet whether we're looking at a type-id or an
4606            expression.  */
4607         cp_parser_parse_tentatively (parser);
4608         /* Try a type-id first.  */
4609         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4610         parser->in_type_id_in_expr_p = true;
4611         type = cp_parser_type_id (parser);
4612         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4613         /* Look for the `)' token.  Otherwise, we can't be sure that
4614            we're not looking at an expression: consider `typeid (int
4615            (3))', for example.  */
4616         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4617         /* If all went well, simply lookup the type-id.  */
4618         if (cp_parser_parse_definitely (parser))
4619           postfix_expression = get_typeid (type);
4620         /* Otherwise, fall back to the expression variant.  */
4621         else
4622           {
4623             tree expression;
4624
4625             /* Look for an expression.  */
4626             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4627             /* Compute its typeid.  */
4628             postfix_expression = build_typeid (expression);
4629             /* Look for the `)' token.  */
4630             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4631           }
4632         /* Restore the saved message.  */
4633         parser->type_definition_forbidden_message = saved_message;
4634         /* `typeid' may not appear in an integral constant expression.  */
4635         if (cp_parser_non_integral_constant_expression(parser,
4636                                                        "%<typeid%> operator"))
4637           return error_mark_node;
4638       }
4639       break;
4640
4641     case RID_TYPENAME:
4642       {
4643         tree type;
4644         /* The syntax permitted here is the same permitted for an
4645            elaborated-type-specifier.  */
4646         type = cp_parser_elaborated_type_specifier (parser,
4647                                                     /*is_friend=*/false,
4648                                                     /*is_declaration=*/false);
4649         postfix_expression = cp_parser_functional_cast (parser, type);
4650       }
4651       break;
4652
4653     default:
4654       {
4655         tree type;
4656
4657         /* If the next thing is a simple-type-specifier, we may be
4658            looking at a functional cast.  We could also be looking at
4659            an id-expression.  So, we try the functional cast, and if
4660            that doesn't work we fall back to the primary-expression.  */
4661         cp_parser_parse_tentatively (parser);
4662         /* Look for the simple-type-specifier.  */
4663         type = cp_parser_simple_type_specifier (parser,
4664                                                 /*decl_specs=*/NULL,
4665                                                 CP_PARSER_FLAGS_NONE);
4666         /* Parse the cast itself.  */
4667         if (!cp_parser_error_occurred (parser))
4668           postfix_expression
4669             = cp_parser_functional_cast (parser, type);
4670         /* If that worked, we're done.  */
4671         if (cp_parser_parse_definitely (parser))
4672           break;
4673
4674         /* If the functional-cast didn't work out, try a
4675            compound-literal.  */
4676         if (cp_parser_allow_gnu_extensions_p (parser)
4677             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4678           {
4679             VEC(constructor_elt,gc) *initializer_list = NULL;
4680             bool saved_in_type_id_in_expr_p;
4681
4682             cp_parser_parse_tentatively (parser);
4683             /* Consume the `('.  */
4684             cp_lexer_consume_token (parser->lexer);
4685             /* Parse the type.  */
4686             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4687             parser->in_type_id_in_expr_p = true;
4688             type = cp_parser_type_id (parser);
4689             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4690             /* Look for the `)'.  */
4691             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4692             /* Look for the `{'.  */
4693             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4694             /* If things aren't going well, there's no need to
4695                keep going.  */
4696             if (!cp_parser_error_occurred (parser))
4697               {
4698                 bool non_constant_p;
4699                 /* Parse the initializer-list.  */
4700                 initializer_list
4701                   = cp_parser_initializer_list (parser, &non_constant_p);
4702                 /* Allow a trailing `,'.  */
4703                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4704                   cp_lexer_consume_token (parser->lexer);
4705                 /* Look for the final `}'.  */
4706                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4707               }
4708             /* If that worked, we're definitely looking at a
4709                compound-literal expression.  */
4710             if (cp_parser_parse_definitely (parser))
4711               {
4712                 /* Warn the user that a compound literal is not
4713                    allowed in standard C++.  */
4714                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4715                 /* For simplicity, we disallow compound literals in
4716                    constant-expressions.  We could
4717                    allow compound literals of integer type, whose
4718                    initializer was a constant, in constant
4719                    expressions.  Permitting that usage, as a further
4720                    extension, would not change the meaning of any
4721                    currently accepted programs.  (Of course, as
4722                    compound literals are not part of ISO C++, the
4723                    standard has nothing to say.)  */
4724                 if (cp_parser_non_integral_constant_expression 
4725                     (parser, "non-constant compound literals"))
4726                   {
4727                     postfix_expression = error_mark_node;
4728                     break;
4729                   }
4730                 /* Form the representation of the compound-literal.  */
4731                 postfix_expression
4732                   = (finish_compound_literal
4733                      (type, build_constructor (init_list_type_node,
4734                                                initializer_list)));
4735                 break;
4736               }
4737           }
4738
4739         /* It must be a primary-expression.  */
4740         postfix_expression
4741           = cp_parser_primary_expression (parser, address_p, cast_p,
4742                                           /*template_arg_p=*/false,
4743                                           &idk);
4744       }
4745       break;
4746     }
4747
4748   /* Keep looping until the postfix-expression is complete.  */
4749   while (true)
4750     {
4751       if (idk == CP_ID_KIND_UNQUALIFIED
4752           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4753           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4754         /* It is not a Koenig lookup function call.  */
4755         postfix_expression
4756           = unqualified_name_lookup_error (postfix_expression);
4757
4758       /* Peek at the next token.  */
4759       token = cp_lexer_peek_token (parser->lexer);
4760
4761       switch (token->type)
4762         {
4763         case CPP_OPEN_SQUARE:
4764           postfix_expression
4765             = cp_parser_postfix_open_square_expression (parser,
4766                                                         postfix_expression,
4767                                                         false);
4768           idk = CP_ID_KIND_NONE;
4769           is_member_access = false;
4770           break;
4771
4772         case CPP_OPEN_PAREN:
4773           /* postfix-expression ( expression-list [opt] ) */
4774           {
4775             bool koenig_p;
4776             bool is_builtin_constant_p;
4777             bool saved_integral_constant_expression_p = false;
4778             bool saved_non_integral_constant_expression_p = false;
4779             VEC(tree,gc) *args;
4780
4781             is_member_access = false;
4782
4783             is_builtin_constant_p
4784               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4785             if (is_builtin_constant_p)
4786               {
4787                 /* The whole point of __builtin_constant_p is to allow
4788                    non-constant expressions to appear as arguments.  */
4789                 saved_integral_constant_expression_p
4790                   = parser->integral_constant_expression_p;
4791                 saved_non_integral_constant_expression_p
4792                   = parser->non_integral_constant_expression_p;
4793                 parser->integral_constant_expression_p = false;
4794               }
4795             args = (cp_parser_parenthesized_expression_list
4796                     (parser, /*is_attribute_list=*/false,
4797                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4798                      /*non_constant_p=*/NULL));
4799             if (is_builtin_constant_p)
4800               {
4801                 parser->integral_constant_expression_p
4802                   = saved_integral_constant_expression_p;
4803                 parser->non_integral_constant_expression_p
4804                   = saved_non_integral_constant_expression_p;
4805               }
4806
4807             if (args == NULL)
4808               {
4809                 postfix_expression = error_mark_node;
4810                 break;
4811               }
4812
4813             /* Function calls are not permitted in
4814                constant-expressions.  */
4815             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4816                 && cp_parser_non_integral_constant_expression (parser,
4817                                                                "a function call"))
4818               {
4819                 postfix_expression = error_mark_node;
4820                 release_tree_vector (args);
4821                 break;
4822               }
4823
4824             koenig_p = false;
4825             if (idk == CP_ID_KIND_UNQUALIFIED
4826                 || idk == CP_ID_KIND_TEMPLATE_ID)
4827               {
4828                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4829                   {
4830                     if (!VEC_empty (tree, args))
4831                       {
4832                         koenig_p = true;
4833                         if (!any_type_dependent_arguments_p (args))
4834                           postfix_expression
4835                             = perform_koenig_lookup (postfix_expression, args);
4836                       }
4837                     else
4838                       postfix_expression
4839                         = unqualified_fn_lookup_error (postfix_expression);
4840                   }
4841                 /* We do not perform argument-dependent lookup if
4842                    normal lookup finds a non-function, in accordance
4843                    with the expected resolution of DR 218.  */
4844                 else if (!VEC_empty (tree, args)
4845                          && is_overloaded_fn (postfix_expression))
4846                   {
4847                     tree fn = get_first_fn (postfix_expression);
4848                     fn = STRIP_TEMPLATE (fn);
4849
4850                     /* Do not do argument dependent lookup if regular
4851                        lookup finds a member function or a block-scope
4852                        function declaration.  [basic.lookup.argdep]/3  */
4853                     if (!DECL_FUNCTION_MEMBER_P (fn)
4854                         && !DECL_LOCAL_FUNCTION_P (fn))
4855                       {
4856                         koenig_p = true;
4857                         if (!any_type_dependent_arguments_p (args))
4858                           postfix_expression
4859                             = perform_koenig_lookup (postfix_expression, args);
4860                       }
4861                   }
4862               }
4863
4864             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4865               {
4866                 tree instance = TREE_OPERAND (postfix_expression, 0);
4867                 tree fn = TREE_OPERAND (postfix_expression, 1);
4868
4869                 if (processing_template_decl
4870                     && (type_dependent_expression_p (instance)
4871                         || (!BASELINK_P (fn)
4872                             && TREE_CODE (fn) != FIELD_DECL)
4873                         || type_dependent_expression_p (fn)
4874                         || any_type_dependent_arguments_p (args)))
4875                   {
4876                     postfix_expression
4877                       = build_nt_call_vec (postfix_expression, args);
4878                     release_tree_vector (args);
4879                     break;
4880                   }
4881
4882                 if (BASELINK_P (fn))
4883                   {
4884                   postfix_expression
4885                     = (build_new_method_call
4886                        (instance, fn, &args, NULL_TREE,
4887                         (idk == CP_ID_KIND_QUALIFIED
4888                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4889                         /*fn_p=*/NULL,
4890                         tf_warning_or_error));
4891                   }
4892                 else
4893                   postfix_expression
4894                     = finish_call_expr (postfix_expression, &args,
4895                                         /*disallow_virtual=*/false,
4896                                         /*koenig_p=*/false,
4897                                         tf_warning_or_error);
4898               }
4899             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4900                      || TREE_CODE (postfix_expression) == MEMBER_REF
4901                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4902               postfix_expression = (build_offset_ref_call_from_tree
4903                                     (postfix_expression, &args));
4904             else if (idk == CP_ID_KIND_QUALIFIED)
4905               /* A call to a static class member, or a namespace-scope
4906                  function.  */
4907               postfix_expression
4908                 = finish_call_expr (postfix_expression, &args,
4909                                     /*disallow_virtual=*/true,
4910                                     koenig_p,
4911                                     tf_warning_or_error);
4912             else
4913               /* All other function calls.  */
4914               postfix_expression
4915                 = finish_call_expr (postfix_expression, &args,
4916                                     /*disallow_virtual=*/false,
4917                                     koenig_p,
4918                                     tf_warning_or_error);
4919
4920             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4921             idk = CP_ID_KIND_NONE;
4922
4923             release_tree_vector (args);
4924           }
4925           break;
4926
4927         case CPP_DOT:
4928         case CPP_DEREF:
4929           /* postfix-expression . template [opt] id-expression
4930              postfix-expression . pseudo-destructor-name
4931              postfix-expression -> template [opt] id-expression
4932              postfix-expression -> pseudo-destructor-name */
4933
4934           /* Consume the `.' or `->' operator.  */
4935           cp_lexer_consume_token (parser->lexer);
4936
4937           postfix_expression
4938             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4939                                                       postfix_expression,
4940                                                       false, &idk,
4941                                                       token->location);
4942
4943           is_member_access = true;
4944           break;
4945
4946         case CPP_PLUS_PLUS:
4947           /* postfix-expression ++  */
4948           /* Consume the `++' token.  */
4949           cp_lexer_consume_token (parser->lexer);
4950           /* Generate a representation for the complete expression.  */
4951           postfix_expression
4952             = finish_increment_expr (postfix_expression,
4953                                      POSTINCREMENT_EXPR);
4954           /* Increments may not appear in constant-expressions.  */
4955           if (cp_parser_non_integral_constant_expression (parser,
4956                                                           "an increment"))
4957             postfix_expression = error_mark_node;
4958           idk = CP_ID_KIND_NONE;
4959           is_member_access = false;
4960           break;
4961
4962         case CPP_MINUS_MINUS:
4963           /* postfix-expression -- */
4964           /* Consume the `--' token.  */
4965           cp_lexer_consume_token (parser->lexer);
4966           /* Generate a representation for the complete expression.  */
4967           postfix_expression
4968             = finish_increment_expr (postfix_expression,
4969                                      POSTDECREMENT_EXPR);
4970           /* Decrements may not appear in constant-expressions.  */
4971           if (cp_parser_non_integral_constant_expression (parser,
4972                                                           "a decrement"))
4973             postfix_expression = error_mark_node;
4974           idk = CP_ID_KIND_NONE;
4975           is_member_access = false;
4976           break;
4977
4978         default:
4979           if (pidk_return != NULL)
4980             * pidk_return = idk;
4981           if (member_access_only_p)
4982             return is_member_access? postfix_expression : error_mark_node;
4983           else
4984             return postfix_expression;
4985         }
4986     }
4987
4988   /* We should never get here.  */
4989   gcc_unreachable ();
4990   return error_mark_node;
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 [ expression ]
4997
4998    FOR_OFFSETOF is set if we're being called in that context, which
4999    changes how we deal with integer constant expressions.  */
5000
5001 static tree
5002 cp_parser_postfix_open_square_expression (cp_parser *parser,
5003                                           tree postfix_expression,
5004                                           bool for_offsetof)
5005 {
5006   tree index;
5007
5008   /* Consume the `[' token.  */
5009   cp_lexer_consume_token (parser->lexer);
5010
5011   /* Parse the index expression.  */
5012   /* ??? For offsetof, there is a question of what to allow here.  If
5013      offsetof is not being used in an integral constant expression context,
5014      then we *could* get the right answer by computing the value at runtime.
5015      If we are in an integral constant expression context, then we might
5016      could accept any constant expression; hard to say without analysis.
5017      Rather than open the barn door too wide right away, allow only integer
5018      constant expressions here.  */
5019   if (for_offsetof)
5020     index = cp_parser_constant_expression (parser, false, NULL);
5021   else
5022     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5023
5024   /* Look for the closing `]'.  */
5025   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5026
5027   /* Build the ARRAY_REF.  */
5028   postfix_expression = grok_array_decl (postfix_expression, index);
5029
5030   /* When not doing offsetof, array references are not permitted in
5031      constant-expressions.  */
5032   if (!for_offsetof
5033       && (cp_parser_non_integral_constant_expression
5034           (parser, "an array reference")))
5035     postfix_expression = error_mark_node;
5036
5037   return postfix_expression;
5038 }
5039
5040 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5041    by cp_parser_builtin_offsetof.  We're looking for
5042
5043      postfix-expression . template [opt] id-expression
5044      postfix-expression . pseudo-destructor-name
5045      postfix-expression -> template [opt] id-expression
5046      postfix-expression -> pseudo-destructor-name
5047
5048    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5049    limits what of the above we'll actually accept, but nevermind.
5050    TOKEN_TYPE is the "." or "->" token, which will already have been
5051    removed from the stream.  */
5052
5053 static tree
5054 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5055                                         enum cpp_ttype token_type,
5056                                         tree postfix_expression,
5057                                         bool for_offsetof, cp_id_kind *idk,
5058                                         location_t location)
5059 {
5060   tree name;
5061   bool dependent_p;
5062   bool pseudo_destructor_p;
5063   tree scope = NULL_TREE;
5064
5065   /* If this is a `->' operator, dereference the pointer.  */
5066   if (token_type == CPP_DEREF)
5067     postfix_expression = build_x_arrow (postfix_expression);
5068   /* Check to see whether or not the expression is type-dependent.  */
5069   dependent_p = type_dependent_expression_p (postfix_expression);
5070   /* The identifier following the `->' or `.' is not qualified.  */
5071   parser->scope = NULL_TREE;
5072   parser->qualifying_scope = NULL_TREE;
5073   parser->object_scope = NULL_TREE;
5074   *idk = CP_ID_KIND_NONE;
5075
5076   /* Enter the scope corresponding to the type of the object
5077      given by the POSTFIX_EXPRESSION.  */
5078   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5079     {
5080       scope = TREE_TYPE (postfix_expression);
5081       /* According to the standard, no expression should ever have
5082          reference type.  Unfortunately, we do not currently match
5083          the standard in this respect in that our internal representation
5084          of an expression may have reference type even when the standard
5085          says it does not.  Therefore, we have to manually obtain the
5086          underlying type here.  */
5087       scope = non_reference (scope);
5088       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5089       if (scope == unknown_type_node)
5090         {
5091           error_at (location, "%qE does not have class type",
5092                     postfix_expression);
5093           scope = NULL_TREE;
5094         }
5095       else
5096         scope = complete_type_or_else (scope, NULL_TREE);
5097       /* Let the name lookup machinery know that we are processing a
5098          class member access expression.  */
5099       parser->context->object_type = scope;
5100       /* If something went wrong, we want to be able to discern that case,
5101          as opposed to the case where there was no SCOPE due to the type
5102          of expression being dependent.  */
5103       if (!scope)
5104         scope = error_mark_node;
5105       /* If the SCOPE was erroneous, make the various semantic analysis
5106          functions exit quickly -- and without issuing additional error
5107          messages.  */
5108       if (scope == error_mark_node)
5109         postfix_expression = error_mark_node;
5110     }
5111
5112   /* Assume this expression is not a pseudo-destructor access.  */
5113   pseudo_destructor_p = false;
5114
5115   /* If the SCOPE is a scalar type, then, if this is a valid program,
5116      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5117      is type dependent, it can be pseudo-destructor-name or something else.
5118      Try to parse it as pseudo-destructor-name first.  */
5119   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5120     {
5121       tree s;
5122       tree type;
5123
5124       cp_parser_parse_tentatively (parser);
5125       /* Parse the pseudo-destructor-name.  */
5126       s = NULL_TREE;
5127       cp_parser_pseudo_destructor_name (parser, &s, &type);
5128       if (dependent_p
5129           && (cp_parser_error_occurred (parser)
5130               || TREE_CODE (type) != TYPE_DECL
5131               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5132         cp_parser_abort_tentative_parse (parser);
5133       else if (cp_parser_parse_definitely (parser))
5134         {
5135           pseudo_destructor_p = true;
5136           postfix_expression
5137             = finish_pseudo_destructor_expr (postfix_expression,
5138                                              s, TREE_TYPE (type));
5139         }
5140     }
5141
5142   if (!pseudo_destructor_p)
5143     {
5144       /* If the SCOPE is not a scalar type, we are looking at an
5145          ordinary class member access expression, rather than a
5146          pseudo-destructor-name.  */
5147       bool template_p;
5148       cp_token *token = cp_lexer_peek_token (parser->lexer);
5149       /* Parse the id-expression.  */
5150       name = (cp_parser_id_expression
5151               (parser,
5152                cp_parser_optional_template_keyword (parser),
5153                /*check_dependency_p=*/true,
5154                &template_p,
5155                /*declarator_p=*/false,
5156                /*optional_p=*/false));
5157       /* In general, build a SCOPE_REF if the member name is qualified.
5158          However, if the name was not dependent and has already been
5159          resolved; there is no need to build the SCOPE_REF.  For example;
5160
5161              struct X { void f(); };
5162              template <typename T> void f(T* t) { t->X::f(); }
5163
5164          Even though "t" is dependent, "X::f" is not and has been resolved
5165          to a BASELINK; there is no need to include scope information.  */
5166
5167       /* But we do need to remember that there was an explicit scope for
5168          virtual function calls.  */
5169       if (parser->scope)
5170         *idk = CP_ID_KIND_QUALIFIED;
5171
5172       /* If the name is a template-id that names a type, we will get a
5173          TYPE_DECL here.  That is invalid code.  */
5174       if (TREE_CODE (name) == TYPE_DECL)
5175         {
5176           error_at (token->location, "invalid use of %qD", name);
5177           postfix_expression = error_mark_node;
5178         }
5179       else
5180         {
5181           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5182             {
5183               name = build_qualified_name (/*type=*/NULL_TREE,
5184                                            parser->scope,
5185                                            name,
5186                                            template_p);
5187               parser->scope = NULL_TREE;
5188               parser->qualifying_scope = NULL_TREE;
5189               parser->object_scope = NULL_TREE;
5190             }
5191           if (scope && name && BASELINK_P (name))
5192             adjust_result_of_qualified_name_lookup
5193               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5194           postfix_expression
5195             = finish_class_member_access_expr (postfix_expression, name,
5196                                                template_p, 
5197                                                tf_warning_or_error);
5198         }
5199     }
5200
5201   /* We no longer need to look up names in the scope of the object on
5202      the left-hand side of the `.' or `->' operator.  */
5203   parser->context->object_type = NULL_TREE;
5204
5205   /* Outside of offsetof, these operators may not appear in
5206      constant-expressions.  */
5207   if (!for_offsetof
5208       && (cp_parser_non_integral_constant_expression
5209           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5210     postfix_expression = error_mark_node;
5211
5212   return postfix_expression;
5213 }
5214
5215 /* Parse a parenthesized expression-list.
5216
5217    expression-list:
5218      assignment-expression
5219      expression-list, assignment-expression
5220
5221    attribute-list:
5222      expression-list
5223      identifier
5224      identifier, expression-list
5225
5226    CAST_P is true if this expression is the target of a cast.
5227
5228    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5229    argument pack.
5230
5231    Returns a vector of trees.  Each element is a representation of an
5232    assignment-expression.  NULL is returned if the ( and or ) are
5233    missing.  An empty, but allocated, vector is returned on no
5234    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is true
5235    if this is really an attribute list being parsed.  If
5236    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5237    not all of the expressions in the list were constant.  */
5238
5239 static VEC(tree,gc) *
5240 cp_parser_parenthesized_expression_list (cp_parser* parser,
5241                                          bool is_attribute_list,
5242                                          bool cast_p,
5243                                          bool allow_expansion_p,
5244                                          bool *non_constant_p)
5245 {
5246   VEC(tree,gc) *expression_list;
5247   bool fold_expr_p = is_attribute_list;
5248   tree identifier = NULL_TREE;
5249   bool saved_greater_than_is_operator_p;
5250
5251   /* Assume all the expressions will be constant.  */
5252   if (non_constant_p)
5253     *non_constant_p = false;
5254
5255   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5256     return NULL;
5257
5258   expression_list = make_tree_vector ();
5259
5260   /* Within a parenthesized expression, a `>' token is always
5261      the greater-than operator.  */
5262   saved_greater_than_is_operator_p
5263     = parser->greater_than_is_operator_p;
5264   parser->greater_than_is_operator_p = true;
5265
5266   /* Consume expressions until there are no more.  */
5267   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5268     while (true)
5269       {
5270         tree expr;
5271
5272         /* At the beginning of attribute lists, check to see if the
5273            next token is an identifier.  */
5274         if (is_attribute_list
5275             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5276           {
5277             cp_token *token;
5278
5279             /* Consume the identifier.  */
5280             token = cp_lexer_consume_token (parser->lexer);
5281             /* Save the identifier.  */
5282             identifier = token->u.value;
5283           }
5284         else
5285           {
5286             bool expr_non_constant_p;
5287
5288             /* Parse the next assignment-expression.  */
5289             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5290               {
5291                 /* A braced-init-list.  */
5292                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5293                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5294                 if (non_constant_p && expr_non_constant_p)
5295                   *non_constant_p = true;
5296               }
5297             else if (non_constant_p)
5298               {
5299                 expr = (cp_parser_constant_expression
5300                         (parser, /*allow_non_constant_p=*/true,
5301                          &expr_non_constant_p));
5302                 if (expr_non_constant_p)
5303                   *non_constant_p = true;
5304               }
5305             else
5306               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5307
5308             if (fold_expr_p)
5309               expr = fold_non_dependent_expr (expr);
5310
5311             /* If we have an ellipsis, then this is an expression
5312                expansion.  */
5313             if (allow_expansion_p
5314                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5315               {
5316                 /* Consume the `...'.  */
5317                 cp_lexer_consume_token (parser->lexer);
5318
5319                 /* Build the argument pack.  */
5320                 expr = make_pack_expansion (expr);
5321               }
5322
5323              /* Add it to the list.  We add error_mark_node
5324                 expressions to the list, so that we can still tell if
5325                 the correct form for a parenthesized expression-list
5326                 is found. That gives better errors.  */
5327             VEC_safe_push (tree, gc, expression_list, expr);
5328
5329             if (expr == error_mark_node)
5330               goto skip_comma;
5331           }
5332
5333         /* After the first item, attribute lists look the same as
5334            expression lists.  */
5335         is_attribute_list = false;
5336
5337       get_comma:;
5338         /* If the next token isn't a `,', then we are done.  */
5339         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5340           break;
5341
5342         /* Otherwise, consume the `,' and keep going.  */
5343         cp_lexer_consume_token (parser->lexer);
5344       }
5345
5346   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5347     {
5348       int ending;
5349
5350     skip_comma:;
5351       /* We try and resync to an unnested comma, as that will give the
5352          user better diagnostics.  */
5353       ending = cp_parser_skip_to_closing_parenthesis (parser,
5354                                                       /*recovering=*/true,
5355                                                       /*or_comma=*/true,
5356                                                       /*consume_paren=*/true);
5357       if (ending < 0)
5358         goto get_comma;
5359       if (!ending)
5360         {
5361           parser->greater_than_is_operator_p
5362             = saved_greater_than_is_operator_p;
5363           return NULL;
5364         }
5365     }
5366
5367   parser->greater_than_is_operator_p
5368     = saved_greater_than_is_operator_p;
5369
5370   if (identifier)
5371     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5372
5373   return expression_list;
5374 }
5375
5376 /* Parse a pseudo-destructor-name.
5377
5378    pseudo-destructor-name:
5379      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5380      :: [opt] nested-name-specifier template template-id :: ~ type-name
5381      :: [opt] nested-name-specifier [opt] ~ type-name
5382
5383    If either of the first two productions is used, sets *SCOPE to the
5384    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5385    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5386    or ERROR_MARK_NODE if the parse fails.  */
5387
5388 static void
5389 cp_parser_pseudo_destructor_name (cp_parser* parser,
5390                                   tree* scope,
5391                                   tree* type)
5392 {
5393   bool nested_name_specifier_p;
5394
5395   /* Assume that things will not work out.  */
5396   *type = error_mark_node;
5397
5398   /* Look for the optional `::' operator.  */
5399   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5400   /* Look for the optional nested-name-specifier.  */
5401   nested_name_specifier_p
5402     = (cp_parser_nested_name_specifier_opt (parser,
5403                                             /*typename_keyword_p=*/false,
5404                                             /*check_dependency_p=*/true,
5405                                             /*type_p=*/false,
5406                                             /*is_declaration=*/false)
5407        != NULL_TREE);
5408   /* Now, if we saw a nested-name-specifier, we might be doing the
5409      second production.  */
5410   if (nested_name_specifier_p
5411       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5412     {
5413       /* Consume the `template' keyword.  */
5414       cp_lexer_consume_token (parser->lexer);
5415       /* Parse the template-id.  */
5416       cp_parser_template_id (parser,
5417                              /*template_keyword_p=*/true,
5418                              /*check_dependency_p=*/false,
5419                              /*is_declaration=*/true);
5420       /* Look for the `::' token.  */
5421       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5422     }
5423   /* If the next token is not a `~', then there might be some
5424      additional qualification.  */
5425   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5426     {
5427       /* At this point, we're looking for "type-name :: ~".  The type-name
5428          must not be a class-name, since this is a pseudo-destructor.  So,
5429          it must be either an enum-name, or a typedef-name -- both of which
5430          are just identifiers.  So, we peek ahead to check that the "::"
5431          and "~" tokens are present; if they are not, then we can avoid
5432          calling type_name.  */
5433       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5434           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5435           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5436         {
5437           cp_parser_error (parser, "non-scalar type");
5438           return;
5439         }
5440
5441       /* Look for the type-name.  */
5442       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5443       if (*scope == error_mark_node)
5444         return;
5445
5446       /* Look for the `::' token.  */
5447       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5448     }
5449   else
5450     *scope = NULL_TREE;
5451
5452   /* Look for the `~'.  */
5453   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5454   /* Look for the type-name again.  We are not responsible for
5455      checking that it matches the first type-name.  */
5456   *type = cp_parser_nonclass_name (parser);
5457 }
5458
5459 /* Parse a unary-expression.
5460
5461    unary-expression:
5462      postfix-expression
5463      ++ cast-expression
5464      -- cast-expression
5465      unary-operator cast-expression
5466      sizeof unary-expression
5467      sizeof ( type-id )
5468      new-expression
5469      delete-expression
5470
5471    GNU Extensions:
5472
5473    unary-expression:
5474      __extension__ cast-expression
5475      __alignof__ unary-expression
5476      __alignof__ ( type-id )
5477      __real__ cast-expression
5478      __imag__ cast-expression
5479      && identifier
5480
5481    ADDRESS_P is true iff the unary-expression is appearing as the
5482    operand of the `&' operator.   CAST_P is true if this expression is
5483    the target of a cast.
5484
5485    Returns a representation of the expression.  */
5486
5487 static tree
5488 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5489                             cp_id_kind * pidk)
5490 {
5491   cp_token *token;
5492   enum tree_code unary_operator;
5493
5494   /* Peek at the next token.  */
5495   token = cp_lexer_peek_token (parser->lexer);
5496   /* Some keywords give away the kind of expression.  */
5497   if (token->type == CPP_KEYWORD)
5498     {
5499       enum rid keyword = token->keyword;
5500
5501       switch (keyword)
5502         {
5503         case RID_ALIGNOF:
5504         case RID_SIZEOF:
5505           {
5506             tree operand;
5507             enum tree_code op;
5508
5509             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5510             /* Consume the token.  */
5511             cp_lexer_consume_token (parser->lexer);
5512             /* Parse the operand.  */
5513             operand = cp_parser_sizeof_operand (parser, keyword);
5514
5515             if (TYPE_P (operand))
5516               return cxx_sizeof_or_alignof_type (operand, op, true);
5517             else
5518               return cxx_sizeof_or_alignof_expr (operand, op, true);
5519           }
5520
5521         case RID_NEW:
5522           return cp_parser_new_expression (parser);
5523
5524         case RID_DELETE:
5525           return cp_parser_delete_expression (parser);
5526
5527         case RID_EXTENSION:
5528           {
5529             /* The saved value of the PEDANTIC flag.  */
5530             int saved_pedantic;
5531             tree expr;
5532
5533             /* Save away the PEDANTIC flag.  */
5534             cp_parser_extension_opt (parser, &saved_pedantic);
5535             /* Parse the cast-expression.  */
5536             expr = cp_parser_simple_cast_expression (parser);
5537             /* Restore the PEDANTIC flag.  */
5538             pedantic = saved_pedantic;
5539
5540             return expr;
5541           }
5542
5543         case RID_REALPART:
5544         case RID_IMAGPART:
5545           {
5546             tree expression;
5547
5548             /* Consume the `__real__' or `__imag__' token.  */
5549             cp_lexer_consume_token (parser->lexer);
5550             /* Parse the cast-expression.  */
5551             expression = cp_parser_simple_cast_expression (parser);
5552             /* Create the complete representation.  */
5553             return build_x_unary_op ((keyword == RID_REALPART
5554                                       ? REALPART_EXPR : IMAGPART_EXPR),
5555                                      expression,
5556                                      tf_warning_or_error);
5557           }
5558           break;
5559
5560         default:
5561           break;
5562         }
5563     }
5564
5565   /* Look for the `:: new' and `:: delete', which also signal the
5566      beginning of a new-expression, or delete-expression,
5567      respectively.  If the next token is `::', then it might be one of
5568      these.  */
5569   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5570     {
5571       enum rid keyword;
5572
5573       /* See if the token after the `::' is one of the keywords in
5574          which we're interested.  */
5575       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5576       /* If it's `new', we have a new-expression.  */
5577       if (keyword == RID_NEW)
5578         return cp_parser_new_expression (parser);
5579       /* Similarly, for `delete'.  */
5580       else if (keyword == RID_DELETE)
5581         return cp_parser_delete_expression (parser);
5582     }
5583
5584   /* Look for a unary operator.  */
5585   unary_operator = cp_parser_unary_operator (token);
5586   /* The `++' and `--' operators can be handled similarly, even though
5587      they are not technically unary-operators in the grammar.  */
5588   if (unary_operator == ERROR_MARK)
5589     {
5590       if (token->type == CPP_PLUS_PLUS)
5591         unary_operator = PREINCREMENT_EXPR;
5592       else if (token->type == CPP_MINUS_MINUS)
5593         unary_operator = PREDECREMENT_EXPR;
5594       /* Handle the GNU address-of-label extension.  */
5595       else if (cp_parser_allow_gnu_extensions_p (parser)
5596                && token->type == CPP_AND_AND)
5597         {
5598           tree identifier;
5599           tree expression;
5600           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5601
5602           /* Consume the '&&' token.  */
5603           cp_lexer_consume_token (parser->lexer);
5604           /* Look for the identifier.  */
5605           identifier = cp_parser_identifier (parser);
5606           /* Create an expression representing the address.  */
5607           expression = finish_label_address_expr (identifier, loc);
5608           if (cp_parser_non_integral_constant_expression (parser,
5609                                                 "the address of a label"))
5610             expression = error_mark_node;
5611           return expression;
5612         }
5613     }
5614   if (unary_operator != ERROR_MARK)
5615     {
5616       tree cast_expression;
5617       tree expression = error_mark_node;
5618       const char *non_constant_p = NULL;
5619
5620       /* Consume the operator token.  */
5621       token = cp_lexer_consume_token (parser->lexer);
5622       /* Parse the cast-expression.  */
5623       cast_expression
5624         = cp_parser_cast_expression (parser,
5625                                      unary_operator == ADDR_EXPR,
5626                                      /*cast_p=*/false, pidk);
5627       /* Now, build an appropriate representation.  */
5628       switch (unary_operator)
5629         {
5630         case INDIRECT_REF:
5631           non_constant_p = "%<*%>";
5632           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5633                                              tf_warning_or_error);
5634           break;
5635
5636         case ADDR_EXPR:
5637           non_constant_p = "%<&%>";
5638           /* Fall through.  */
5639         case BIT_NOT_EXPR:
5640           expression = build_x_unary_op (unary_operator, cast_expression,
5641                                          tf_warning_or_error);
5642           break;
5643
5644         case PREINCREMENT_EXPR:
5645         case PREDECREMENT_EXPR:
5646           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5647                             ? "%<++%>" : "%<--%>");
5648           /* Fall through.  */
5649         case UNARY_PLUS_EXPR:
5650         case NEGATE_EXPR:
5651         case TRUTH_NOT_EXPR:
5652           expression = finish_unary_op_expr (unary_operator, cast_expression);
5653           break;
5654
5655         default:
5656           gcc_unreachable ();
5657         }
5658
5659       if (non_constant_p
5660           && cp_parser_non_integral_constant_expression (parser,
5661                                                          non_constant_p))
5662         expression = error_mark_node;
5663
5664       return expression;
5665     }
5666
5667   return cp_parser_postfix_expression (parser, address_p, cast_p,
5668                                        /*member_access_only_p=*/false,
5669                                        pidk);
5670 }
5671
5672 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5673    unary-operator, the corresponding tree code is returned.  */
5674
5675 static enum tree_code
5676 cp_parser_unary_operator (cp_token* token)
5677 {
5678   switch (token->type)
5679     {
5680     case CPP_MULT:
5681       return INDIRECT_REF;
5682
5683     case CPP_AND:
5684       return ADDR_EXPR;
5685
5686     case CPP_PLUS:
5687       return UNARY_PLUS_EXPR;
5688
5689     case CPP_MINUS:
5690       return NEGATE_EXPR;
5691
5692     case CPP_NOT:
5693       return TRUTH_NOT_EXPR;
5694
5695     case CPP_COMPL:
5696       return BIT_NOT_EXPR;
5697
5698     default:
5699       return ERROR_MARK;
5700     }
5701 }
5702
5703 /* Parse a new-expression.
5704
5705    new-expression:
5706      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5707      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5708
5709    Returns a representation of the expression.  */
5710
5711 static tree
5712 cp_parser_new_expression (cp_parser* parser)
5713 {
5714   bool global_scope_p;
5715   VEC(tree,gc) *placement;
5716   tree type;
5717   VEC(tree,gc) *initializer;
5718   tree nelts;
5719   tree ret;
5720
5721   /* Look for the optional `::' operator.  */
5722   global_scope_p
5723     = (cp_parser_global_scope_opt (parser,
5724                                    /*current_scope_valid_p=*/false)
5725        != NULL_TREE);
5726   /* Look for the `new' operator.  */
5727   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5728   /* There's no easy way to tell a new-placement from the
5729      `( type-id )' construct.  */
5730   cp_parser_parse_tentatively (parser);
5731   /* Look for a new-placement.  */
5732   placement = cp_parser_new_placement (parser);
5733   /* If that didn't work out, there's no new-placement.  */
5734   if (!cp_parser_parse_definitely (parser))
5735     {
5736       if (placement != NULL)
5737         release_tree_vector (placement);
5738       placement = NULL;
5739     }
5740
5741   /* If the next token is a `(', then we have a parenthesized
5742      type-id.  */
5743   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5744     {
5745       cp_token *token;
5746       /* Consume the `('.  */
5747       cp_lexer_consume_token (parser->lexer);
5748       /* Parse the type-id.  */
5749       type = cp_parser_type_id (parser);
5750       /* Look for the closing `)'.  */
5751       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5752       token = cp_lexer_peek_token (parser->lexer);
5753       /* There should not be a direct-new-declarator in this production,
5754          but GCC used to allowed this, so we check and emit a sensible error
5755          message for this case.  */
5756       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5757         {
5758           error_at (token->location,
5759                     "array bound forbidden after parenthesized type-id");
5760           inform (token->location, 
5761                   "try removing the parentheses around the type-id");
5762           cp_parser_direct_new_declarator (parser);
5763         }
5764       nelts = NULL_TREE;
5765     }
5766   /* Otherwise, there must be a new-type-id.  */
5767   else
5768     type = cp_parser_new_type_id (parser, &nelts);
5769
5770   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5771   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5772       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5773     initializer = cp_parser_new_initializer (parser);
5774   else
5775     initializer = NULL;
5776
5777   /* A new-expression may not appear in an integral constant
5778      expression.  */
5779   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5780     ret = error_mark_node;
5781   else
5782     {
5783       /* Create a representation of the new-expression.  */
5784       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5785                        tf_warning_or_error);
5786     }
5787
5788   if (placement != NULL)
5789     release_tree_vector (placement);
5790   if (initializer != NULL)
5791     release_tree_vector (initializer);
5792
5793   return ret;
5794 }
5795
5796 /* Parse a new-placement.
5797
5798    new-placement:
5799      ( expression-list )
5800
5801    Returns the same representation as for an expression-list.  */
5802
5803 static VEC(tree,gc) *
5804 cp_parser_new_placement (cp_parser* parser)
5805 {
5806   VEC(tree,gc) *expression_list;
5807
5808   /* Parse the expression-list.  */
5809   expression_list = (cp_parser_parenthesized_expression_list
5810                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5811                       /*non_constant_p=*/NULL));
5812
5813   return expression_list;
5814 }
5815
5816 /* Parse a new-type-id.
5817
5818    new-type-id:
5819      type-specifier-seq new-declarator [opt]
5820
5821    Returns the TYPE allocated.  If the new-type-id indicates an array
5822    type, *NELTS is set to the number of elements in the last array
5823    bound; the TYPE will not include the last array bound.  */
5824
5825 static tree
5826 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5827 {
5828   cp_decl_specifier_seq type_specifier_seq;
5829   cp_declarator *new_declarator;
5830   cp_declarator *declarator;
5831   cp_declarator *outer_declarator;
5832   const char *saved_message;
5833   tree type;
5834
5835   /* The type-specifier sequence must not contain type definitions.
5836      (It cannot contain declarations of new types either, but if they
5837      are not definitions we will catch that because they are not
5838      complete.)  */
5839   saved_message = parser->type_definition_forbidden_message;
5840   parser->type_definition_forbidden_message
5841     = G_("types may not be defined in a new-type-id");
5842   /* Parse the type-specifier-seq.  */
5843   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5844                                 /*is_trailing_return=*/false,
5845                                 &type_specifier_seq);
5846   /* Restore the old message.  */
5847   parser->type_definition_forbidden_message = saved_message;
5848   /* Parse the new-declarator.  */
5849   new_declarator = cp_parser_new_declarator_opt (parser);
5850
5851   /* Determine the number of elements in the last array dimension, if
5852      any.  */
5853   *nelts = NULL_TREE;
5854   /* Skip down to the last array dimension.  */
5855   declarator = new_declarator;
5856   outer_declarator = NULL;
5857   while (declarator && (declarator->kind == cdk_pointer
5858                         || declarator->kind == cdk_ptrmem))
5859     {
5860       outer_declarator = declarator;
5861       declarator = declarator->declarator;
5862     }
5863   while (declarator
5864          && declarator->kind == cdk_array
5865          && declarator->declarator
5866          && declarator->declarator->kind == cdk_array)
5867     {
5868       outer_declarator = declarator;
5869       declarator = declarator->declarator;
5870     }
5871
5872   if (declarator && declarator->kind == cdk_array)
5873     {
5874       *nelts = declarator->u.array.bounds;
5875       if (*nelts == error_mark_node)
5876         *nelts = integer_one_node;
5877
5878       if (outer_declarator)
5879         outer_declarator->declarator = declarator->declarator;
5880       else
5881         new_declarator = NULL;
5882     }
5883
5884   type = groktypename (&type_specifier_seq, new_declarator, false);
5885   return type;
5886 }
5887
5888 /* Parse an (optional) new-declarator.
5889
5890    new-declarator:
5891      ptr-operator new-declarator [opt]
5892      direct-new-declarator
5893
5894    Returns the declarator.  */
5895
5896 static cp_declarator *
5897 cp_parser_new_declarator_opt (cp_parser* parser)
5898 {
5899   enum tree_code code;
5900   tree type;
5901   cp_cv_quals cv_quals;
5902
5903   /* We don't know if there's a ptr-operator next, or not.  */
5904   cp_parser_parse_tentatively (parser);
5905   /* Look for a ptr-operator.  */
5906   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5907   /* If that worked, look for more new-declarators.  */
5908   if (cp_parser_parse_definitely (parser))
5909     {
5910       cp_declarator *declarator;
5911
5912       /* Parse another optional declarator.  */
5913       declarator = cp_parser_new_declarator_opt (parser);
5914
5915       return cp_parser_make_indirect_declarator
5916         (code, type, cv_quals, declarator);
5917     }
5918
5919   /* If the next token is a `[', there is a direct-new-declarator.  */
5920   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5921     return cp_parser_direct_new_declarator (parser);
5922
5923   return NULL;
5924 }
5925
5926 /* Parse a direct-new-declarator.
5927
5928    direct-new-declarator:
5929      [ expression ]
5930      direct-new-declarator [constant-expression]
5931
5932    */
5933
5934 static cp_declarator *
5935 cp_parser_direct_new_declarator (cp_parser* parser)
5936 {
5937   cp_declarator *declarator = NULL;
5938
5939   while (true)
5940     {
5941       tree expression;
5942
5943       /* Look for the opening `['.  */
5944       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5945       /* The first expression is not required to be constant.  */
5946       if (!declarator)
5947         {
5948           cp_token *token = cp_lexer_peek_token (parser->lexer);
5949           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5950           /* The standard requires that the expression have integral
5951              type.  DR 74 adds enumeration types.  We believe that the
5952              real intent is that these expressions be handled like the
5953              expression in a `switch' condition, which also allows
5954              classes with a single conversion to integral or
5955              enumeration type.  */
5956           if (!processing_template_decl)
5957             {
5958               expression
5959                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5960                                               expression,
5961                                               /*complain=*/true);
5962               if (!expression)
5963                 {
5964                   error_at (token->location,
5965                             "expression in new-declarator must have integral "
5966                             "or enumeration type");
5967                   expression = error_mark_node;
5968                 }
5969             }
5970         }
5971       /* But all the other expressions must be.  */
5972       else
5973         expression
5974           = cp_parser_constant_expression (parser,
5975                                            /*allow_non_constant=*/false,
5976                                            NULL);
5977       /* Look for the closing `]'.  */
5978       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5979
5980       /* Add this bound to the declarator.  */
5981       declarator = make_array_declarator (declarator, expression);
5982
5983       /* If the next token is not a `[', then there are no more
5984          bounds.  */
5985       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5986         break;
5987     }
5988
5989   return declarator;
5990 }
5991
5992 /* Parse a new-initializer.
5993
5994    new-initializer:
5995      ( expression-list [opt] )
5996      braced-init-list
5997
5998    Returns a representation of the expression-list.  */
5999
6000 static VEC(tree,gc) *
6001 cp_parser_new_initializer (cp_parser* parser)
6002 {
6003   VEC(tree,gc) *expression_list;
6004
6005   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6006     {
6007       tree t;
6008       bool expr_non_constant_p;
6009       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6010       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6011       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6012       expression_list = make_tree_vector_single (t);
6013     }
6014   else
6015     expression_list = (cp_parser_parenthesized_expression_list
6016                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
6017                         /*non_constant_p=*/NULL));
6018
6019   return expression_list;
6020 }
6021
6022 /* Parse a delete-expression.
6023
6024    delete-expression:
6025      :: [opt] delete cast-expression
6026      :: [opt] delete [ ] cast-expression
6027
6028    Returns a representation of the expression.  */
6029
6030 static tree
6031 cp_parser_delete_expression (cp_parser* parser)
6032 {
6033   bool global_scope_p;
6034   bool array_p;
6035   tree expression;
6036
6037   /* Look for the optional `::' operator.  */
6038   global_scope_p
6039     = (cp_parser_global_scope_opt (parser,
6040                                    /*current_scope_valid_p=*/false)
6041        != NULL_TREE);
6042   /* Look for the `delete' keyword.  */
6043   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6044   /* See if the array syntax is in use.  */
6045   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6046     {
6047       /* Consume the `[' token.  */
6048       cp_lexer_consume_token (parser->lexer);
6049       /* Look for the `]' token.  */
6050       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6051       /* Remember that this is the `[]' construct.  */
6052       array_p = true;
6053     }
6054   else
6055     array_p = false;
6056
6057   /* Parse the cast-expression.  */
6058   expression = cp_parser_simple_cast_expression (parser);
6059
6060   /* A delete-expression may not appear in an integral constant
6061      expression.  */
6062   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6063     return error_mark_node;
6064
6065   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6066 }
6067
6068 /* Returns true if TOKEN may start a cast-expression and false
6069    otherwise.  */
6070
6071 static bool
6072 cp_parser_token_starts_cast_expression (cp_token *token)
6073 {
6074   switch (token->type)
6075     {
6076     case CPP_COMMA:
6077     case CPP_SEMICOLON:
6078     case CPP_QUERY:
6079     case CPP_COLON:
6080     case CPP_CLOSE_SQUARE:
6081     case CPP_CLOSE_PAREN:
6082     case CPP_CLOSE_BRACE:
6083     case CPP_DOT:
6084     case CPP_DOT_STAR:
6085     case CPP_DEREF:
6086     case CPP_DEREF_STAR:
6087     case CPP_DIV:
6088     case CPP_MOD:
6089     case CPP_LSHIFT:
6090     case CPP_RSHIFT:
6091     case CPP_LESS:
6092     case CPP_GREATER:
6093     case CPP_LESS_EQ:
6094     case CPP_GREATER_EQ:
6095     case CPP_EQ_EQ:
6096     case CPP_NOT_EQ:
6097     case CPP_EQ:
6098     case CPP_MULT_EQ:
6099     case CPP_DIV_EQ:
6100     case CPP_MOD_EQ:
6101     case CPP_PLUS_EQ:
6102     case CPP_MINUS_EQ:
6103     case CPP_RSHIFT_EQ:
6104     case CPP_LSHIFT_EQ:
6105     case CPP_AND_EQ:
6106     case CPP_XOR_EQ:
6107     case CPP_OR_EQ:
6108     case CPP_XOR:
6109     case CPP_OR:
6110     case CPP_OR_OR:
6111     case CPP_EOF:
6112       return false;
6113
6114       /* '[' may start a primary-expression in obj-c++.  */
6115     case CPP_OPEN_SQUARE:
6116       return c_dialect_objc ();
6117
6118     default:
6119       return true;
6120     }
6121 }
6122
6123 /* Parse a cast-expression.
6124
6125    cast-expression:
6126      unary-expression
6127      ( type-id ) cast-expression
6128
6129    ADDRESS_P is true iff the unary-expression is appearing as the
6130    operand of the `&' operator.   CAST_P is true if this expression is
6131    the target of a cast.
6132
6133    Returns a representation of the expression.  */
6134
6135 static tree
6136 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6137                            cp_id_kind * pidk)
6138 {
6139   /* If it's a `(', then we might be looking at a cast.  */
6140   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6141     {
6142       tree type = NULL_TREE;
6143       tree expr = NULL_TREE;
6144       bool compound_literal_p;
6145       const char *saved_message;
6146
6147       /* There's no way to know yet whether or not this is a cast.
6148          For example, `(int (3))' is a unary-expression, while `(int)
6149          3' is a cast.  So, we resort to parsing tentatively.  */
6150       cp_parser_parse_tentatively (parser);
6151       /* Types may not be defined in a cast.  */
6152       saved_message = parser->type_definition_forbidden_message;
6153       parser->type_definition_forbidden_message
6154         = G_("types may not be defined in casts");
6155       /* Consume the `('.  */
6156       cp_lexer_consume_token (parser->lexer);
6157       /* A very tricky bit is that `(struct S) { 3 }' is a
6158          compound-literal (which we permit in C++ as an extension).
6159          But, that construct is not a cast-expression -- it is a
6160          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6161          is legal; if the compound-literal were a cast-expression,
6162          you'd need an extra set of parentheses.)  But, if we parse
6163          the type-id, and it happens to be a class-specifier, then we
6164          will commit to the parse at that point, because we cannot
6165          undo the action that is done when creating a new class.  So,
6166          then we cannot back up and do a postfix-expression.
6167
6168          Therefore, we scan ahead to the closing `)', and check to see
6169          if the token after the `)' is a `{'.  If so, we are not
6170          looking at a cast-expression.
6171
6172          Save tokens so that we can put them back.  */
6173       cp_lexer_save_tokens (parser->lexer);
6174       /* Skip tokens until the next token is a closing parenthesis.
6175          If we find the closing `)', and the next token is a `{', then
6176          we are looking at a compound-literal.  */
6177       compound_literal_p
6178         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6179                                                   /*consume_paren=*/true)
6180            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6181       /* Roll back the tokens we skipped.  */
6182       cp_lexer_rollback_tokens (parser->lexer);
6183       /* If we were looking at a compound-literal, simulate an error
6184          so that the call to cp_parser_parse_definitely below will
6185          fail.  */
6186       if (compound_literal_p)
6187         cp_parser_simulate_error (parser);
6188       else
6189         {
6190           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6191           parser->in_type_id_in_expr_p = true;
6192           /* Look for the type-id.  */
6193           type = cp_parser_type_id (parser);
6194           /* Look for the closing `)'.  */
6195           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6196           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6197         }
6198
6199       /* Restore the saved message.  */
6200       parser->type_definition_forbidden_message = saved_message;
6201
6202       /* At this point this can only be either a cast or a
6203          parenthesized ctor such as `(T ())' that looks like a cast to
6204          function returning T.  */
6205       if (!cp_parser_error_occurred (parser)
6206           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6207                                                      (parser->lexer)))
6208         {
6209           cp_parser_parse_definitely (parser);
6210           expr = cp_parser_cast_expression (parser,
6211                                             /*address_p=*/false,
6212                                             /*cast_p=*/true, pidk);
6213
6214           /* Warn about old-style casts, if so requested.  */
6215           if (warn_old_style_cast
6216               && !in_system_header
6217               && !VOID_TYPE_P (type)
6218               && current_lang_name != lang_name_c)
6219             warning (OPT_Wold_style_cast, "use of old-style cast");
6220
6221           /* Only type conversions to integral or enumeration types
6222              can be used in constant-expressions.  */
6223           if (!cast_valid_in_integral_constant_expression_p (type)
6224               && (cp_parser_non_integral_constant_expression
6225                   (parser,
6226                    "a cast to a type other than an integral or "
6227                    "enumeration type")))
6228             return error_mark_node;
6229
6230           /* Perform the cast.  */
6231           expr = build_c_cast (input_location, type, expr);
6232           return expr;
6233         }
6234       else 
6235         cp_parser_abort_tentative_parse (parser);
6236     }
6237
6238   /* If we get here, then it's not a cast, so it must be a
6239      unary-expression.  */
6240   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6241 }
6242
6243 /* Parse a binary expression of the general form:
6244
6245    pm-expression:
6246      cast-expression
6247      pm-expression .* cast-expression
6248      pm-expression ->* cast-expression
6249
6250    multiplicative-expression:
6251      pm-expression
6252      multiplicative-expression * pm-expression
6253      multiplicative-expression / pm-expression
6254      multiplicative-expression % pm-expression
6255
6256    additive-expression:
6257      multiplicative-expression
6258      additive-expression + multiplicative-expression
6259      additive-expression - multiplicative-expression
6260
6261    shift-expression:
6262      additive-expression
6263      shift-expression << additive-expression
6264      shift-expression >> additive-expression
6265
6266    relational-expression:
6267      shift-expression
6268      relational-expression < shift-expression
6269      relational-expression > shift-expression
6270      relational-expression <= shift-expression
6271      relational-expression >= shift-expression
6272
6273   GNU Extension:
6274
6275    relational-expression:
6276      relational-expression <? shift-expression
6277      relational-expression >? shift-expression
6278
6279    equality-expression:
6280      relational-expression
6281      equality-expression == relational-expression
6282      equality-expression != relational-expression
6283
6284    and-expression:
6285      equality-expression
6286      and-expression & equality-expression
6287
6288    exclusive-or-expression:
6289      and-expression
6290      exclusive-or-expression ^ and-expression
6291
6292    inclusive-or-expression:
6293      exclusive-or-expression
6294      inclusive-or-expression | exclusive-or-expression
6295
6296    logical-and-expression:
6297      inclusive-or-expression
6298      logical-and-expression && inclusive-or-expression
6299
6300    logical-or-expression:
6301      logical-and-expression
6302      logical-or-expression || logical-and-expression
6303
6304    All these are implemented with a single function like:
6305
6306    binary-expression:
6307      simple-cast-expression
6308      binary-expression <token> binary-expression
6309
6310    CAST_P is true if this expression is the target of a cast.
6311
6312    The binops_by_token map is used to get the tree codes for each <token> type.
6313    binary-expressions are associated according to a precedence table.  */
6314
6315 #define TOKEN_PRECEDENCE(token)                              \
6316 (((token->type == CPP_GREATER                                \
6317    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6318   && !parser->greater_than_is_operator_p)                    \
6319  ? PREC_NOT_OPERATOR                                         \
6320  : binops_by_token[token->type].prec)
6321
6322 static tree
6323 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6324                              bool no_toplevel_fold_p,
6325                              enum cp_parser_prec prec,
6326                              cp_id_kind * pidk)
6327 {
6328   cp_parser_expression_stack stack;
6329   cp_parser_expression_stack_entry *sp = &stack[0];
6330   tree lhs, rhs;
6331   cp_token *token;
6332   enum tree_code tree_type, lhs_type, rhs_type;
6333   enum cp_parser_prec new_prec, lookahead_prec;
6334   bool overloaded_p;
6335
6336   /* Parse the first expression.  */
6337   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6338   lhs_type = ERROR_MARK;
6339
6340   for (;;)
6341     {
6342       /* Get an operator token.  */
6343       token = cp_lexer_peek_token (parser->lexer);
6344
6345       if (warn_cxx0x_compat
6346           && token->type == CPP_RSHIFT
6347           && !parser->greater_than_is_operator_p)
6348         {
6349           if (warning_at (token->location, OPT_Wc__0x_compat, 
6350                           "%<>>%> operator will be treated as"
6351                           " two right angle brackets in C++0x"))
6352             inform (token->location,
6353                     "suggest parentheses around %<>>%> expression");
6354         }
6355
6356       new_prec = TOKEN_PRECEDENCE (token);
6357
6358       /* Popping an entry off the stack means we completed a subexpression:
6359          - either we found a token which is not an operator (`>' where it is not
6360            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6361            will happen repeatedly;
6362          - or, we found an operator which has lower priority.  This is the case
6363            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6364            parsing `3 * 4'.  */
6365       if (new_prec <= prec)
6366         {
6367           if (sp == stack)
6368             break;
6369           else
6370             goto pop;
6371         }
6372
6373      get_rhs:
6374       tree_type = binops_by_token[token->type].tree_type;
6375
6376       /* We used the operator token.  */
6377       cp_lexer_consume_token (parser->lexer);
6378
6379       /* For "false && x" or "true || x", x will never be executed;
6380          disable warnings while evaluating it.  */
6381       if (tree_type == TRUTH_ANDIF_EXPR)
6382         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6383       else if (tree_type == TRUTH_ORIF_EXPR)
6384         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6385
6386       /* Extract another operand.  It may be the RHS of this expression
6387          or the LHS of a new, higher priority expression.  */
6388       rhs = cp_parser_simple_cast_expression (parser);
6389       rhs_type = ERROR_MARK;
6390
6391       /* Get another operator token.  Look up its precedence to avoid
6392          building a useless (immediately popped) stack entry for common
6393          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6394       token = cp_lexer_peek_token (parser->lexer);
6395       lookahead_prec = TOKEN_PRECEDENCE (token);
6396       if (lookahead_prec > new_prec)
6397         {
6398           /* ... and prepare to parse the RHS of the new, higher priority
6399              expression.  Since precedence levels on the stack are
6400              monotonically increasing, we do not have to care about
6401              stack overflows.  */
6402           sp->prec = prec;
6403           sp->tree_type = tree_type;
6404           sp->lhs = lhs;
6405           sp->lhs_type = lhs_type;
6406           sp++;
6407           lhs = rhs;
6408           lhs_type = rhs_type;
6409           prec = new_prec;
6410           new_prec = lookahead_prec;
6411           goto get_rhs;
6412
6413          pop:
6414           lookahead_prec = new_prec;
6415           /* If the stack is not empty, we have parsed into LHS the right side
6416              (`4' in the example above) of an expression we had suspended.
6417              We can use the information on the stack to recover the LHS (`3')
6418              from the stack together with the tree code (`MULT_EXPR'), and
6419              the precedence of the higher level subexpression
6420              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6421              which will be used to actually build the additive expression.  */
6422           --sp;
6423           prec = sp->prec;
6424           tree_type = sp->tree_type;
6425           rhs = lhs;
6426           rhs_type = lhs_type;
6427           lhs = sp->lhs;
6428           lhs_type = sp->lhs_type;
6429         }
6430
6431       /* Undo the disabling of warnings done above.  */
6432       if (tree_type == TRUTH_ANDIF_EXPR)
6433         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6434       else if (tree_type == TRUTH_ORIF_EXPR)
6435         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6436
6437       overloaded_p = false;
6438       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6439          ERROR_MARK for everything that is not a binary expression.
6440          This makes warn_about_parentheses miss some warnings that
6441          involve unary operators.  For unary expressions we should
6442          pass the correct tree_code unless the unary expression was
6443          surrounded by parentheses.
6444       */
6445       if (no_toplevel_fold_p
6446           && lookahead_prec <= prec
6447           && sp == stack
6448           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6449         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6450       else
6451         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6452                                  &overloaded_p, tf_warning_or_error);
6453       lhs_type = tree_type;
6454
6455       /* If the binary operator required the use of an overloaded operator,
6456          then this expression cannot be an integral constant-expression.
6457          An overloaded operator can be used even if both operands are
6458          otherwise permissible in an integral constant-expression if at
6459          least one of the operands is of enumeration type.  */
6460
6461       if (overloaded_p
6462           && (cp_parser_non_integral_constant_expression
6463               (parser, "calls to overloaded operators")))
6464         return error_mark_node;
6465     }
6466
6467   return lhs;
6468 }
6469
6470
6471 /* Parse the `? expression : assignment-expression' part of a
6472    conditional-expression.  The LOGICAL_OR_EXPR is the
6473    logical-or-expression that started the conditional-expression.
6474    Returns a representation of the entire conditional-expression.
6475
6476    This routine is used by cp_parser_assignment_expression.
6477
6478      ? expression : assignment-expression
6479
6480    GNU Extensions:
6481
6482      ? : assignment-expression */
6483
6484 static tree
6485 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6486 {
6487   tree expr;
6488   tree assignment_expr;
6489
6490   /* Consume the `?' token.  */
6491   cp_lexer_consume_token (parser->lexer);
6492   if (cp_parser_allow_gnu_extensions_p (parser)
6493       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6494     {
6495       /* Implicit true clause.  */
6496       expr = NULL_TREE;
6497       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6498     }
6499   else
6500     {
6501       /* Parse the expression.  */
6502       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6503       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6504       c_inhibit_evaluation_warnings +=
6505         ((logical_or_expr == truthvalue_true_node)
6506          - (logical_or_expr == truthvalue_false_node));
6507     }
6508
6509   /* The next token should be a `:'.  */
6510   cp_parser_require (parser, CPP_COLON, "%<:%>");
6511   /* Parse the assignment-expression.  */
6512   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6513   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6514
6515   /* Build the conditional-expression.  */
6516   return build_x_conditional_expr (logical_or_expr,
6517                                    expr,
6518                                    assignment_expr,
6519                                    tf_warning_or_error);
6520 }
6521
6522 /* Parse an assignment-expression.
6523
6524    assignment-expression:
6525      conditional-expression
6526      logical-or-expression assignment-operator assignment_expression
6527      throw-expression
6528
6529    CAST_P is true if this expression is the target of a cast.
6530
6531    Returns a representation for the expression.  */
6532
6533 static tree
6534 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6535                                  cp_id_kind * pidk)
6536 {
6537   tree expr;
6538
6539   /* If the next token is the `throw' keyword, then we're looking at
6540      a throw-expression.  */
6541   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6542     expr = cp_parser_throw_expression (parser);
6543   /* Otherwise, it must be that we are looking at a
6544      logical-or-expression.  */
6545   else
6546     {
6547       /* Parse the binary expressions (logical-or-expression).  */
6548       expr = cp_parser_binary_expression (parser, cast_p, false,
6549                                           PREC_NOT_OPERATOR, pidk);
6550       /* If the next token is a `?' then we're actually looking at a
6551          conditional-expression.  */
6552       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6553         return cp_parser_question_colon_clause (parser, expr);
6554       else
6555         {
6556           enum tree_code assignment_operator;
6557
6558           /* If it's an assignment-operator, we're using the second
6559              production.  */
6560           assignment_operator
6561             = cp_parser_assignment_operator_opt (parser);
6562           if (assignment_operator != ERROR_MARK)
6563             {
6564               bool non_constant_p;
6565
6566               /* Parse the right-hand side of the assignment.  */
6567               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6568
6569               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6570                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6571
6572               /* An assignment may not appear in a
6573                  constant-expression.  */
6574               if (cp_parser_non_integral_constant_expression (parser,
6575                                                               "an assignment"))
6576                 return error_mark_node;
6577               /* Build the assignment expression.  */
6578               expr = build_x_modify_expr (expr,
6579                                           assignment_operator,
6580                                           rhs,
6581                                           tf_warning_or_error);
6582             }
6583         }
6584     }
6585
6586   return expr;
6587 }
6588
6589 /* Parse an (optional) assignment-operator.
6590
6591    assignment-operator: one of
6592      = *= /= %= += -= >>= <<= &= ^= |=
6593
6594    GNU Extension:
6595
6596    assignment-operator: one of
6597      <?= >?=
6598
6599    If the next token is an assignment operator, the corresponding tree
6600    code is returned, and the token is consumed.  For example, for
6601    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6602    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6603    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6604    operator, ERROR_MARK is returned.  */
6605
6606 static enum tree_code
6607 cp_parser_assignment_operator_opt (cp_parser* parser)
6608 {
6609   enum tree_code op;
6610   cp_token *token;
6611
6612   /* Peek at the next token.  */
6613   token = cp_lexer_peek_token (parser->lexer);
6614
6615   switch (token->type)
6616     {
6617     case CPP_EQ:
6618       op = NOP_EXPR;
6619       break;
6620
6621     case CPP_MULT_EQ:
6622       op = MULT_EXPR;
6623       break;
6624
6625     case CPP_DIV_EQ:
6626       op = TRUNC_DIV_EXPR;
6627       break;
6628
6629     case CPP_MOD_EQ:
6630       op = TRUNC_MOD_EXPR;
6631       break;
6632
6633     case CPP_PLUS_EQ:
6634       op = PLUS_EXPR;
6635       break;
6636
6637     case CPP_MINUS_EQ:
6638       op = MINUS_EXPR;
6639       break;
6640
6641     case CPP_RSHIFT_EQ:
6642       op = RSHIFT_EXPR;
6643       break;
6644
6645     case CPP_LSHIFT_EQ:
6646       op = LSHIFT_EXPR;
6647       break;
6648
6649     case CPP_AND_EQ:
6650       op = BIT_AND_EXPR;
6651       break;
6652
6653     case CPP_XOR_EQ:
6654       op = BIT_XOR_EXPR;
6655       break;
6656
6657     case CPP_OR_EQ:
6658       op = BIT_IOR_EXPR;
6659       break;
6660
6661     default:
6662       /* Nothing else is an assignment operator.  */
6663       op = ERROR_MARK;
6664     }
6665
6666   /* If it was an assignment operator, consume it.  */
6667   if (op != ERROR_MARK)
6668     cp_lexer_consume_token (parser->lexer);
6669
6670   return op;
6671 }
6672
6673 /* Parse an expression.
6674
6675    expression:
6676      assignment-expression
6677      expression , assignment-expression
6678
6679    CAST_P is true if this expression is the target of a cast.
6680
6681    Returns a representation of the expression.  */
6682
6683 static tree
6684 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6685 {
6686   tree expression = NULL_TREE;
6687
6688   while (true)
6689     {
6690       tree assignment_expression;
6691
6692       /* Parse the next assignment-expression.  */
6693       assignment_expression
6694         = cp_parser_assignment_expression (parser, cast_p, pidk);
6695       /* If this is the first assignment-expression, we can just
6696          save it away.  */
6697       if (!expression)
6698         expression = assignment_expression;
6699       else
6700         expression = build_x_compound_expr (expression,
6701                                             assignment_expression,
6702                                             tf_warning_or_error);
6703       /* If the next token is not a comma, then we are done with the
6704          expression.  */
6705       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6706         break;
6707       /* Consume the `,'.  */
6708       cp_lexer_consume_token (parser->lexer);
6709       /* A comma operator cannot appear in a constant-expression.  */
6710       if (cp_parser_non_integral_constant_expression (parser,
6711                                                       "a comma operator"))
6712         expression = error_mark_node;
6713     }
6714
6715   return expression;
6716 }
6717
6718 /* Parse a constant-expression.
6719
6720    constant-expression:
6721      conditional-expression
6722
6723   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6724   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6725   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6726   is false, NON_CONSTANT_P should be NULL.  */
6727
6728 static tree
6729 cp_parser_constant_expression (cp_parser* parser,
6730                                bool allow_non_constant_p,
6731                                bool *non_constant_p)
6732 {
6733   bool saved_integral_constant_expression_p;
6734   bool saved_allow_non_integral_constant_expression_p;
6735   bool saved_non_integral_constant_expression_p;
6736   tree expression;
6737
6738   /* It might seem that we could simply parse the
6739      conditional-expression, and then check to see if it were
6740      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6741      one that the compiler can figure out is constant, possibly after
6742      doing some simplifications or optimizations.  The standard has a
6743      precise definition of constant-expression, and we must honor
6744      that, even though it is somewhat more restrictive.
6745
6746      For example:
6747
6748        int i[(2, 3)];
6749
6750      is not a legal declaration, because `(2, 3)' is not a
6751      constant-expression.  The `,' operator is forbidden in a
6752      constant-expression.  However, GCC's constant-folding machinery
6753      will fold this operation to an INTEGER_CST for `3'.  */
6754
6755   /* Save the old settings.  */
6756   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6757   saved_allow_non_integral_constant_expression_p
6758     = parser->allow_non_integral_constant_expression_p;
6759   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6760   /* We are now parsing a constant-expression.  */
6761   parser->integral_constant_expression_p = true;
6762   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6763   parser->non_integral_constant_expression_p = false;
6764   /* Although the grammar says "conditional-expression", we parse an
6765      "assignment-expression", which also permits "throw-expression"
6766      and the use of assignment operators.  In the case that
6767      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6768      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6769      actually essential that we look for an assignment-expression.
6770      For example, cp_parser_initializer_clauses uses this function to
6771      determine whether a particular assignment-expression is in fact
6772      constant.  */
6773   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6774   /* Restore the old settings.  */
6775   parser->integral_constant_expression_p
6776     = saved_integral_constant_expression_p;
6777   parser->allow_non_integral_constant_expression_p
6778     = saved_allow_non_integral_constant_expression_p;
6779   if (allow_non_constant_p)
6780     *non_constant_p = parser->non_integral_constant_expression_p;
6781   else if (parser->non_integral_constant_expression_p)
6782     expression = error_mark_node;
6783   parser->non_integral_constant_expression_p
6784     = saved_non_integral_constant_expression_p;
6785
6786   return expression;
6787 }
6788
6789 /* Parse __builtin_offsetof.
6790
6791    offsetof-expression:
6792      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6793
6794    offsetof-member-designator:
6795      id-expression
6796      | offsetof-member-designator "." id-expression
6797      | offsetof-member-designator "[" expression "]"
6798      | offsetof-member-designator "->" id-expression  */
6799
6800 static tree
6801 cp_parser_builtin_offsetof (cp_parser *parser)
6802 {
6803   int save_ice_p, save_non_ice_p;
6804   tree type, expr;
6805   cp_id_kind dummy;
6806   cp_token *token;
6807
6808   /* We're about to accept non-integral-constant things, but will
6809      definitely yield an integral constant expression.  Save and
6810      restore these values around our local parsing.  */
6811   save_ice_p = parser->integral_constant_expression_p;
6812   save_non_ice_p = parser->non_integral_constant_expression_p;
6813
6814   /* Consume the "__builtin_offsetof" token.  */
6815   cp_lexer_consume_token (parser->lexer);
6816   /* Consume the opening `('.  */
6817   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6818   /* Parse the type-id.  */
6819   type = cp_parser_type_id (parser);
6820   /* Look for the `,'.  */
6821   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6822   token = cp_lexer_peek_token (parser->lexer);
6823
6824   /* Build the (type *)null that begins the traditional offsetof macro.  */
6825   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6826                             tf_warning_or_error);
6827
6828   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6829   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6830                                                  true, &dummy, token->location);
6831   while (true)
6832     {
6833       token = cp_lexer_peek_token (parser->lexer);
6834       switch (token->type)
6835         {
6836         case CPP_OPEN_SQUARE:
6837           /* offsetof-member-designator "[" expression "]" */
6838           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6839           break;
6840
6841         case CPP_DEREF:
6842           /* offsetof-member-designator "->" identifier */
6843           expr = grok_array_decl (expr, integer_zero_node);
6844           /* FALLTHRU */
6845
6846         case CPP_DOT:
6847           /* offsetof-member-designator "." identifier */
6848           cp_lexer_consume_token (parser->lexer);
6849           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6850                                                          expr, true, &dummy,
6851                                                          token->location);
6852           break;
6853
6854         case CPP_CLOSE_PAREN:
6855           /* Consume the ")" token.  */
6856           cp_lexer_consume_token (parser->lexer);
6857           goto success;
6858
6859         default:
6860           /* Error.  We know the following require will fail, but
6861              that gives the proper error message.  */
6862           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6863           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6864           expr = error_mark_node;
6865           goto failure;
6866         }
6867     }
6868
6869  success:
6870   /* If we're processing a template, we can't finish the semantics yet.
6871      Otherwise we can fold the entire expression now.  */
6872   if (processing_template_decl)
6873     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6874   else
6875     expr = finish_offsetof (expr);
6876
6877  failure:
6878   parser->integral_constant_expression_p = save_ice_p;
6879   parser->non_integral_constant_expression_p = save_non_ice_p;
6880
6881   return expr;
6882 }
6883
6884 /* Parse a trait expression.  */
6885
6886 static tree
6887 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6888 {
6889   cp_trait_kind kind;
6890   tree type1, type2 = NULL_TREE;
6891   bool binary = false;
6892   cp_decl_specifier_seq decl_specs;
6893
6894   switch (keyword)
6895     {
6896     case RID_HAS_NOTHROW_ASSIGN:
6897       kind = CPTK_HAS_NOTHROW_ASSIGN;
6898       break;
6899     case RID_HAS_NOTHROW_CONSTRUCTOR:
6900       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6901       break;
6902     case RID_HAS_NOTHROW_COPY:
6903       kind = CPTK_HAS_NOTHROW_COPY;
6904       break;
6905     case RID_HAS_TRIVIAL_ASSIGN:
6906       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6907       break;
6908     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6909       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6910       break;
6911     case RID_HAS_TRIVIAL_COPY:
6912       kind = CPTK_HAS_TRIVIAL_COPY;
6913       break;
6914     case RID_HAS_TRIVIAL_DESTRUCTOR:
6915       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6916       break;
6917     case RID_HAS_VIRTUAL_DESTRUCTOR:
6918       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6919       break;
6920     case RID_IS_ABSTRACT:
6921       kind = CPTK_IS_ABSTRACT;
6922       break;
6923     case RID_IS_BASE_OF:
6924       kind = CPTK_IS_BASE_OF;
6925       binary = true;
6926       break;
6927     case RID_IS_CLASS:
6928       kind = CPTK_IS_CLASS;
6929       break;
6930     case RID_IS_CONVERTIBLE_TO:
6931       kind = CPTK_IS_CONVERTIBLE_TO;
6932       binary = true;
6933       break;
6934     case RID_IS_EMPTY:
6935       kind = CPTK_IS_EMPTY;
6936       break;
6937     case RID_IS_ENUM:
6938       kind = CPTK_IS_ENUM;
6939       break;
6940     case RID_IS_POD:
6941       kind = CPTK_IS_POD;
6942       break;
6943     case RID_IS_POLYMORPHIC:
6944       kind = CPTK_IS_POLYMORPHIC;
6945       break;
6946     case RID_IS_STD_LAYOUT:
6947       kind = CPTK_IS_STD_LAYOUT;
6948       break;
6949     case RID_IS_TRIVIAL:
6950       kind = CPTK_IS_TRIVIAL;
6951       break;
6952     case RID_IS_UNION:
6953       kind = CPTK_IS_UNION;
6954       break;
6955     default:
6956       gcc_unreachable ();
6957     }
6958
6959   /* Consume the token.  */
6960   cp_lexer_consume_token (parser->lexer);
6961
6962   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6963
6964   type1 = cp_parser_type_id (parser);
6965
6966   if (type1 == error_mark_node)
6967     return error_mark_node;
6968
6969   /* Build a trivial decl-specifier-seq.  */
6970   clear_decl_specs (&decl_specs);
6971   decl_specs.type = type1;
6972
6973   /* Call grokdeclarator to figure out what type this is.  */
6974   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6975                           /*initialized=*/0, /*attrlist=*/NULL);
6976
6977   if (binary)
6978     {
6979       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6980  
6981       type2 = cp_parser_type_id (parser);
6982
6983       if (type2 == error_mark_node)
6984         return error_mark_node;
6985
6986       /* Build a trivial decl-specifier-seq.  */
6987       clear_decl_specs (&decl_specs);
6988       decl_specs.type = type2;
6989
6990       /* Call grokdeclarator to figure out what type this is.  */
6991       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6992                               /*initialized=*/0, /*attrlist=*/NULL);
6993     }
6994
6995   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6996
6997   /* Complete the trait expression, which may mean either processing
6998      the trait expr now or saving it for template instantiation.  */
6999   return finish_trait_expr (kind, type1, type2);
7000 }
7001
7002 /* Lambdas that appear in variable initializer or default argument scope
7003    get that in their mangling, so we need to record it.  We might as well
7004    use the count for function and namespace scopes as well.  */
7005 static GTY(()) tree lambda_scope;
7006 static GTY(()) int lambda_count;
7007 typedef struct GTY(()) tree_int
7008 {
7009   tree t;
7010   int i;
7011 } tree_int;
7012 DEF_VEC_O(tree_int);
7013 DEF_VEC_ALLOC_O(tree_int,gc);
7014 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7015
7016 static void
7017 start_lambda_scope (tree decl)
7018 {
7019   tree_int ti;
7020   gcc_assert (decl);
7021   /* Once we're inside a function, we ignore other scopes and just push
7022      the function again so that popping works properly.  */
7023   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7024     decl = current_function_decl;
7025   ti.t = lambda_scope;
7026   ti.i = lambda_count;
7027   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7028   if (lambda_scope != decl)
7029     {
7030       /* Don't reset the count if we're still in the same function.  */
7031       lambda_scope = decl;
7032       lambda_count = 0;
7033     }
7034 }
7035
7036 static void
7037 record_lambda_scope (tree lambda)
7038 {
7039   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7040   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7041 }
7042
7043 static void
7044 finish_lambda_scope (void)
7045 {
7046   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7047   if (lambda_scope != p->t)
7048     {
7049       lambda_scope = p->t;
7050       lambda_count = p->i;
7051     }
7052   VEC_pop (tree_int, lambda_scope_stack);
7053 }
7054
7055 /* Parse a lambda expression.
7056
7057    lambda-expression:
7058      lambda-introducer lambda-declarator [opt] compound-statement
7059
7060    Returns a representation of the expression.  */
7061
7062 static tree
7063 cp_parser_lambda_expression (cp_parser* parser)
7064 {
7065   tree lambda_expr = build_lambda_expr ();
7066   tree type;
7067
7068   LAMBDA_EXPR_LOCATION (lambda_expr)
7069     = cp_lexer_peek_token (parser->lexer)->location;
7070
7071   /* We may be in the middle of deferred access check.  Disable
7072      it now.  */
7073   push_deferring_access_checks (dk_no_deferred);
7074
7075   type = begin_lambda_type (lambda_expr);
7076
7077   record_lambda_scope (lambda_expr);
7078
7079   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7080   determine_visibility (TYPE_NAME (type));
7081
7082   {
7083     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7084     unsigned int saved_num_template_parameter_lists
7085         = parser->num_template_parameter_lists;
7086
7087     parser->num_template_parameter_lists = 0;
7088
7089     cp_parser_lambda_introducer (parser, lambda_expr);
7090
7091     /* By virtue of defining a local class, a lambda expression has access to
7092        the private variables of enclosing classes.  */
7093
7094     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7095
7096     cp_parser_lambda_body (parser, lambda_expr);
7097
7098     /* The capture list was built up in reverse order; fix that now.  */
7099     {
7100       tree newlist = NULL_TREE;
7101       tree elt, next;
7102
7103       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7104            elt; elt = next)
7105         {
7106           tree field = TREE_PURPOSE (elt);
7107           char *buf;
7108
7109           next = TREE_CHAIN (elt);
7110           TREE_CHAIN (elt) = newlist;
7111           newlist = elt;
7112
7113           /* Also add __ to the beginning of the field name so that code
7114              outside the lambda body can't see the captured name.  We could
7115              just remove the name entirely, but this is more useful for
7116              debugging.  */
7117           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7118             /* The 'this' capture already starts with __.  */
7119             continue;
7120
7121           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7122           buf[1] = buf[0] = '_';
7123           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7124                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7125           DECL_NAME (field) = get_identifier (buf);
7126         }
7127       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7128     }
7129
7130     maybe_add_lambda_conv_op (type);
7131
7132     type = finish_struct (type, /*attributes=*/NULL_TREE);
7133
7134     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7135   }
7136
7137   pop_deferring_access_checks ();
7138
7139   return build_lambda_object (lambda_expr);
7140 }
7141
7142 /* Parse the beginning of a lambda expression.
7143
7144    lambda-introducer:
7145      [ lambda-capture [opt] ]
7146
7147    LAMBDA_EXPR is the current representation of the lambda expression.  */
7148
7149 static void
7150 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7151 {
7152   /* Need commas after the first capture.  */
7153   bool first = true;
7154
7155   /* Eat the leading `['.  */
7156   cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7157
7158   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7159   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7160       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7161     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7162   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7163     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7164
7165   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7166     {
7167       cp_lexer_consume_token (parser->lexer);
7168       first = false;
7169     }
7170
7171   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7172     {
7173       cp_token* capture_token;
7174       tree capture_id;
7175       tree capture_init_expr;
7176       cp_id_kind idk = CP_ID_KIND_NONE;
7177       bool explicit_init_p = false;
7178
7179       enum capture_kind_type
7180       {
7181         BY_COPY,
7182         BY_REFERENCE
7183       };
7184       enum capture_kind_type capture_kind = BY_COPY;
7185
7186       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7187         {
7188           error ("expected end of capture-list");
7189           return;
7190         }
7191
7192       if (first)
7193         first = false;
7194       else
7195         cp_parser_require (parser, CPP_COMMA, "%<,%>");
7196
7197       /* Possibly capture `this'.  */
7198       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7199         {
7200           cp_lexer_consume_token (parser->lexer);
7201           add_capture (lambda_expr,
7202                        /*id=*/get_identifier ("__this"),
7203                        /*initializer=*/finish_this_expr(),
7204                        /*by_reference_p=*/false,
7205                        explicit_init_p);
7206           continue;
7207         }
7208
7209       /* Remember whether we want to capture as a reference or not.  */
7210       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7211         {
7212           capture_kind = BY_REFERENCE;
7213           cp_lexer_consume_token (parser->lexer);
7214         }
7215
7216       /* Get the identifier.  */
7217       capture_token = cp_lexer_peek_token (parser->lexer);
7218       capture_id = cp_parser_identifier (parser);
7219
7220       if (capture_id == error_mark_node)
7221         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7222            delimiters, but I modified this to stop on unnested ']' as well.  It
7223            was already changed to stop on unnested '}', so the
7224            "closing_parenthesis" name is no more misleading with my change.  */
7225         {
7226           cp_parser_skip_to_closing_parenthesis (parser,
7227                                                  /*recovering=*/true,
7228                                                  /*or_comma=*/true,
7229                                                  /*consume_paren=*/true);
7230           break;
7231         }
7232
7233       /* Find the initializer for this capture.  */
7234       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7235         {
7236           /* An explicit expression exists.  */
7237           cp_lexer_consume_token (parser->lexer);
7238           pedwarn (input_location, OPT_pedantic,
7239                    "ISO C++ does not allow initializers "
7240                    "in lambda expression capture lists");
7241           capture_init_expr = cp_parser_assignment_expression (parser,
7242                                                                /*cast_p=*/true,
7243                                                                &idk);
7244           explicit_init_p = true;
7245         }
7246       else
7247         {
7248           const char* error_msg;
7249
7250           /* Turn the identifier into an id-expression.  */
7251           capture_init_expr
7252             = cp_parser_lookup_name
7253                 (parser,
7254                  capture_id,
7255                  none_type,
7256                  /*is_template=*/false,
7257                  /*is_namespace=*/false,
7258                  /*check_dependency=*/true,
7259                  /*ambiguous_decls=*/NULL,
7260                  capture_token->location);
7261
7262           capture_init_expr
7263             = finish_id_expression
7264                 (capture_id,
7265                  capture_init_expr,
7266                  parser->scope,
7267                  &idk,
7268                  /*integral_constant_expression_p=*/false,
7269                  /*allow_non_integral_constant_expression_p=*/false,
7270                  /*non_integral_constant_expression_p=*/NULL,
7271                  /*template_p=*/false,
7272                  /*done=*/true,
7273                  /*address_p=*/false,
7274                  /*template_arg_p=*/false,
7275                  &error_msg,
7276                  capture_token->location);
7277         }
7278
7279       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7280         capture_init_expr
7281           = unqualified_name_lookup_error (capture_init_expr);
7282
7283       add_capture (lambda_expr,
7284                    capture_id,
7285                    capture_init_expr,
7286                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7287                    explicit_init_p);
7288     }
7289
7290   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7291 }
7292
7293 /* Parse the (optional) middle of a lambda expression.
7294
7295    lambda-declarator:
7296      ( parameter-declaration-clause [opt] )
7297        attribute-specifier [opt]
7298        mutable [opt]
7299        exception-specification [opt]
7300        lambda-return-type-clause [opt]
7301
7302    LAMBDA_EXPR is the current representation of the lambda expression.  */
7303
7304 static void
7305 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7306 {
7307   /* 5.1.1.4 of the standard says:
7308        If a lambda-expression does not include a lambda-declarator, it is as if
7309        the lambda-declarator were ().
7310      This means an empty parameter list, no attributes, and no exception
7311      specification.  */
7312   tree param_list = void_list_node;
7313   tree attributes = NULL_TREE;
7314   tree exception_spec = NULL_TREE;
7315   tree t;
7316
7317   /* The lambda-declarator is optional, but must begin with an opening
7318      parenthesis if present.  */
7319   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7320     {
7321       cp_lexer_consume_token (parser->lexer);
7322
7323       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7324
7325       /* Parse parameters.  */
7326       param_list = cp_parser_parameter_declaration_clause (parser);
7327
7328       /* Default arguments shall not be specified in the
7329          parameter-declaration-clause of a lambda-declarator.  */
7330       for (t = param_list; t; t = TREE_CHAIN (t))
7331         if (TREE_PURPOSE (t))
7332           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7333                    "default argument specified for lambda parameter");
7334
7335       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7336
7337       attributes = cp_parser_attributes_opt (parser);
7338
7339       /* Parse optional `mutable' keyword.  */
7340       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7341         {
7342           cp_lexer_consume_token (parser->lexer);
7343           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7344         }
7345
7346       /* Parse optional exception specification.  */
7347       exception_spec = cp_parser_exception_specification_opt (parser);
7348
7349       /* Parse optional trailing return type.  */
7350       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7351         {
7352           cp_lexer_consume_token (parser->lexer);
7353           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7354         }
7355
7356       /* The function parameters must be in scope all the way until after the
7357          trailing-return-type in case of decltype.  */
7358       for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7359         pop_binding (DECL_NAME (t), t);
7360
7361       leave_scope ();
7362     }
7363
7364   /* Create the function call operator.
7365
7366      Messing with declarators like this is no uglier than building up the
7367      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7368      other code.  */
7369   {
7370     cp_decl_specifier_seq return_type_specs;
7371     cp_declarator* declarator;
7372     tree fco;
7373     int quals;
7374     void *p;
7375
7376     clear_decl_specs (&return_type_specs);
7377     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7378       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7379     else
7380       /* Maybe we will deduce the return type later, but we can use void
7381          as a placeholder return type anyways.  */
7382       return_type_specs.type = void_type_node;
7383
7384     p = obstack_alloc (&declarator_obstack, 0);
7385
7386     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7387                                      sfk_none);
7388
7389     quals = TYPE_UNQUALIFIED;
7390     if (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) == NULL_TREE
7391         && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
7392       {
7393         /* A lambda with no captures has a static op() and a conversion op
7394            to function type.  */
7395         if (LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7396           error ("lambda expression with no captures declared mutable");
7397         return_type_specs.storage_class = sc_static;
7398       }
7399     else if (!LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7400       quals = TYPE_QUAL_CONST;
7401     declarator = make_call_declarator (declarator, param_list, quals,
7402                                        exception_spec,
7403                                        /*late_return_type=*/NULL_TREE);
7404
7405     fco = grokmethod (&return_type_specs,
7406                       declarator,
7407                       attributes);
7408     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7409     DECL_ARTIFICIAL (fco) = 1;
7410
7411     finish_member_declaration (fco);
7412
7413     obstack_free (&declarator_obstack, p);
7414   }
7415 }
7416
7417 /* Parse the body of a lambda expression, which is simply
7418
7419    compound-statement
7420
7421    but which requires special handling.
7422    LAMBDA_EXPR is the current representation of the lambda expression.  */
7423
7424 static void
7425 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7426 {
7427   bool nested = (current_function_decl != NULL_TREE);
7428   if (nested)
7429     push_function_context ();
7430
7431   /* Finish the function call operator
7432      - class_specifier
7433      + late_parsing_for_member
7434      + function_definition_after_declarator
7435      + ctor_initializer_opt_and_function_body  */
7436   {
7437     tree fco = lambda_function (lambda_expr);
7438     tree body;
7439     bool done = false;
7440
7441     /* Let the front end know that we are going to be defining this
7442        function.  */
7443     start_preparsed_function (fco,
7444                               NULL_TREE,
7445                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7446
7447     start_lambda_scope (fco);
7448     body = begin_function_body ();
7449
7450     /* 5.1.1.4 of the standard says:
7451          If a lambda-expression does not include a trailing-return-type, it
7452          is as if the trailing-return-type denotes the following type:
7453           * if the compound-statement is of the form
7454                { return attribute-specifier [opt] expression ; }
7455              the type of the returned expression after lvalue-to-rvalue
7456              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7457              (_conv.array_ 4.2), and function-to-pointer conversion
7458              (_conv.func_ 4.3);
7459           * otherwise, void.  */
7460
7461     /* In a lambda that has neither a lambda-return-type-clause
7462        nor a deducible form, errors should be reported for return statements
7463        in the body.  Since we used void as the placeholder return type, parsing
7464        the body as usual will give such desired behavior.  */
7465     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7466         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7467         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7468         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7469       {
7470         tree compound_stmt;
7471         tree expr = NULL_TREE;
7472         cp_id_kind idk = CP_ID_KIND_NONE;
7473
7474         /* Parse tentatively in case there's more after the initial return
7475            statement.  */
7476         cp_parser_parse_tentatively (parser);
7477
7478         cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7479         cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7480
7481         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7482
7483         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7484         cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7485
7486         if (cp_parser_parse_definitely (parser))
7487           {
7488             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7489
7490             compound_stmt = begin_compound_stmt (0);
7491             /* Will get error here if type not deduced yet.  */
7492             finish_return_stmt (expr);
7493             finish_compound_stmt (compound_stmt);
7494
7495             done = true;
7496           }
7497       }
7498
7499     if (!done)
7500       {
7501         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7502           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7503         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7504            cp_parser_compound_stmt does not pass it.  */
7505         cp_parser_function_body (parser);
7506         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7507       }
7508
7509     finish_function_body (body);
7510     finish_lambda_scope ();
7511
7512     /* Finish the function and generate code for it if necessary.  */
7513     expand_or_defer_fn (finish_function (/*inline*/2));
7514   }
7515
7516   if (nested)
7517     pop_function_context();
7518 }
7519
7520 /* Statements [gram.stmt.stmt]  */
7521
7522 /* Parse a statement.
7523
7524    statement:
7525      labeled-statement
7526      expression-statement
7527      compound-statement
7528      selection-statement
7529      iteration-statement
7530      jump-statement
7531      declaration-statement
7532      try-block
7533
7534   IN_COMPOUND is true when the statement is nested inside a
7535   cp_parser_compound_statement; this matters for certain pragmas.
7536
7537   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7538   is a (possibly labeled) if statement which is not enclosed in braces
7539   and has an else clause.  This is used to implement -Wparentheses.  */
7540
7541 static void
7542 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7543                      bool in_compound, bool *if_p)
7544 {
7545   tree statement;
7546   cp_token *token;
7547   location_t statement_location;
7548
7549  restart:
7550   if (if_p != NULL)
7551     *if_p = false;
7552   /* There is no statement yet.  */
7553   statement = NULL_TREE;
7554   /* Peek at the next token.  */
7555   token = cp_lexer_peek_token (parser->lexer);
7556   /* Remember the location of the first token in the statement.  */
7557   statement_location = token->location;
7558   /* If this is a keyword, then that will often determine what kind of
7559      statement we have.  */
7560   if (token->type == CPP_KEYWORD)
7561     {
7562       enum rid keyword = token->keyword;
7563
7564       switch (keyword)
7565         {
7566         case RID_CASE:
7567         case RID_DEFAULT:
7568           /* Looks like a labeled-statement with a case label.
7569              Parse the label, and then use tail recursion to parse
7570              the statement.  */
7571           cp_parser_label_for_labeled_statement (parser);
7572           goto restart;
7573
7574         case RID_IF:
7575         case RID_SWITCH:
7576           statement = cp_parser_selection_statement (parser, if_p);
7577           break;
7578
7579         case RID_WHILE:
7580         case RID_DO:
7581         case RID_FOR:
7582           statement = cp_parser_iteration_statement (parser);
7583           break;
7584
7585         case RID_BREAK:
7586         case RID_CONTINUE:
7587         case RID_RETURN:
7588         case RID_GOTO:
7589           statement = cp_parser_jump_statement (parser);
7590           break;
7591
7592           /* Objective-C++ exception-handling constructs.  */
7593         case RID_AT_TRY:
7594         case RID_AT_CATCH:
7595         case RID_AT_FINALLY:
7596         case RID_AT_SYNCHRONIZED:
7597         case RID_AT_THROW:
7598           statement = cp_parser_objc_statement (parser);
7599           break;
7600
7601         case RID_TRY:
7602           statement = cp_parser_try_block (parser);
7603           break;
7604
7605         case RID_NAMESPACE:
7606           /* This must be a namespace alias definition.  */
7607           cp_parser_declaration_statement (parser);
7608           return;
7609           
7610         default:
7611           /* It might be a keyword like `int' that can start a
7612              declaration-statement.  */
7613           break;
7614         }
7615     }
7616   else if (token->type == CPP_NAME)
7617     {
7618       /* If the next token is a `:', then we are looking at a
7619          labeled-statement.  */
7620       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7621       if (token->type == CPP_COLON)
7622         {
7623           /* Looks like a labeled-statement with an ordinary label.
7624              Parse the label, and then use tail recursion to parse
7625              the statement.  */
7626           cp_parser_label_for_labeled_statement (parser);
7627           goto restart;
7628         }
7629     }
7630   /* Anything that starts with a `{' must be a compound-statement.  */
7631   else if (token->type == CPP_OPEN_BRACE)
7632     statement = cp_parser_compound_statement (parser, NULL, false);
7633   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7634      a statement all its own.  */
7635   else if (token->type == CPP_PRAGMA)
7636     {
7637       /* Only certain OpenMP pragmas are attached to statements, and thus
7638          are considered statements themselves.  All others are not.  In
7639          the context of a compound, accept the pragma as a "statement" and
7640          return so that we can check for a close brace.  Otherwise we
7641          require a real statement and must go back and read one.  */
7642       if (in_compound)
7643         cp_parser_pragma (parser, pragma_compound);
7644       else if (!cp_parser_pragma (parser, pragma_stmt))
7645         goto restart;
7646       return;
7647     }
7648   else if (token->type == CPP_EOF)
7649     {
7650       cp_parser_error (parser, "expected statement");
7651       return;
7652     }
7653
7654   /* Everything else must be a declaration-statement or an
7655      expression-statement.  Try for the declaration-statement
7656      first, unless we are looking at a `;', in which case we know that
7657      we have an expression-statement.  */
7658   if (!statement)
7659     {
7660       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7661         {
7662           cp_parser_parse_tentatively (parser);
7663           /* Try to parse the declaration-statement.  */
7664           cp_parser_declaration_statement (parser);
7665           /* If that worked, we're done.  */
7666           if (cp_parser_parse_definitely (parser))
7667             return;
7668         }
7669       /* Look for an expression-statement instead.  */
7670       statement = cp_parser_expression_statement (parser, in_statement_expr);
7671     }
7672
7673   /* Set the line number for the statement.  */
7674   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7675     SET_EXPR_LOCATION (statement, statement_location);
7676 }
7677
7678 /* Parse the label for a labeled-statement, i.e.
7679
7680    identifier :
7681    case constant-expression :
7682    default :
7683
7684    GNU Extension:
7685    case constant-expression ... constant-expression : statement
7686
7687    When a label is parsed without errors, the label is added to the
7688    parse tree by the finish_* functions, so this function doesn't
7689    have to return the label.  */
7690
7691 static void
7692 cp_parser_label_for_labeled_statement (cp_parser* parser)
7693 {
7694   cp_token *token;
7695   tree label = NULL_TREE;
7696
7697   /* The next token should be an identifier.  */
7698   token = cp_lexer_peek_token (parser->lexer);
7699   if (token->type != CPP_NAME
7700       && token->type != CPP_KEYWORD)
7701     {
7702       cp_parser_error (parser, "expected labeled-statement");
7703       return;
7704     }
7705
7706   switch (token->keyword)
7707     {
7708     case RID_CASE:
7709       {
7710         tree expr, expr_hi;
7711         cp_token *ellipsis;
7712
7713         /* Consume the `case' token.  */
7714         cp_lexer_consume_token (parser->lexer);
7715         /* Parse the constant-expression.  */
7716         expr = cp_parser_constant_expression (parser,
7717                                               /*allow_non_constant_p=*/false,
7718                                               NULL);
7719
7720         ellipsis = cp_lexer_peek_token (parser->lexer);
7721         if (ellipsis->type == CPP_ELLIPSIS)
7722           {
7723             /* Consume the `...' token.  */
7724             cp_lexer_consume_token (parser->lexer);
7725             expr_hi =
7726               cp_parser_constant_expression (parser,
7727                                              /*allow_non_constant_p=*/false,
7728                                              NULL);
7729             /* We don't need to emit warnings here, as the common code
7730                will do this for us.  */
7731           }
7732         else
7733           expr_hi = NULL_TREE;
7734
7735         if (parser->in_switch_statement_p)
7736           finish_case_label (token->location, expr, expr_hi);
7737         else
7738           error_at (token->location,
7739                     "case label %qE not within a switch statement",
7740                     expr);
7741       }
7742       break;
7743
7744     case RID_DEFAULT:
7745       /* Consume the `default' token.  */
7746       cp_lexer_consume_token (parser->lexer);
7747
7748       if (parser->in_switch_statement_p)
7749         finish_case_label (token->location, NULL_TREE, NULL_TREE);
7750       else
7751         error_at (token->location, "case label not within a switch statement");
7752       break;
7753
7754     default:
7755       /* Anything else must be an ordinary label.  */
7756       label = finish_label_stmt (cp_parser_identifier (parser));
7757       break;
7758     }
7759
7760   /* Require the `:' token.  */
7761   cp_parser_require (parser, CPP_COLON, "%<:%>");
7762
7763   /* An ordinary label may optionally be followed by attributes.
7764      However, this is only permitted if the attributes are then
7765      followed by a semicolon.  This is because, for backward
7766      compatibility, when parsing
7767        lab: __attribute__ ((unused)) int i;
7768      we want the attribute to attach to "i", not "lab".  */
7769   if (label != NULL_TREE
7770       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7771     {
7772       tree attrs;
7773
7774       cp_parser_parse_tentatively (parser);
7775       attrs = cp_parser_attributes_opt (parser);
7776       if (attrs == NULL_TREE
7777           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7778         cp_parser_abort_tentative_parse (parser);
7779       else if (!cp_parser_parse_definitely (parser))
7780         ;
7781       else
7782         cplus_decl_attributes (&label, attrs, 0);
7783     }
7784 }
7785
7786 /* Parse an expression-statement.
7787
7788    expression-statement:
7789      expression [opt] ;
7790
7791    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7792    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7793    indicates whether this expression-statement is part of an
7794    expression statement.  */
7795
7796 static tree
7797 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7798 {
7799   tree statement = NULL_TREE;
7800   cp_token *token = cp_lexer_peek_token (parser->lexer);
7801
7802   /* If the next token is a ';', then there is no expression
7803      statement.  */
7804   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7805     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7806
7807   /* Give a helpful message for "A<T>::type t;" and the like.  */
7808   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7809       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
7810     {
7811       if (TREE_CODE (statement) == SCOPE_REF)
7812         error_at (token->location, "need %<typename%> before %qE because "
7813                   "%qT is a dependent scope",
7814                   statement, TREE_OPERAND (statement, 0));
7815       else if (is_overloaded_fn (statement)
7816                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
7817         {
7818           /* A::A a; */
7819           tree fn = get_first_fn (statement);
7820           error_at (token->location,
7821                     "%<%T::%D%> names the constructor, not the type",
7822                     DECL_CONTEXT (fn), DECL_NAME (fn));
7823         }
7824     }
7825
7826   /* Consume the final `;'.  */
7827   cp_parser_consume_semicolon_at_end_of_statement (parser);
7828
7829   if (in_statement_expr
7830       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7831     /* This is the final expression statement of a statement
7832        expression.  */
7833     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7834   else if (statement)
7835     statement = finish_expr_stmt (statement);
7836   else
7837     finish_stmt ();
7838
7839   return statement;
7840 }
7841
7842 /* Parse a compound-statement.
7843
7844    compound-statement:
7845      { statement-seq [opt] }
7846
7847    GNU extension:
7848
7849    compound-statement:
7850      { label-declaration-seq [opt] statement-seq [opt] }
7851
7852    label-declaration-seq:
7853      label-declaration
7854      label-declaration-seq label-declaration
7855
7856    Returns a tree representing the statement.  */
7857
7858 static tree
7859 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7860                               bool in_try)
7861 {
7862   tree compound_stmt;
7863
7864   /* Consume the `{'.  */
7865   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7866     return error_mark_node;
7867   /* Begin the compound-statement.  */
7868   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7869   /* If the next keyword is `__label__' we have a label declaration.  */
7870   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7871     cp_parser_label_declaration (parser);
7872   /* Parse an (optional) statement-seq.  */
7873   cp_parser_statement_seq_opt (parser, in_statement_expr);
7874   /* Finish the compound-statement.  */
7875   finish_compound_stmt (compound_stmt);
7876   /* Consume the `}'.  */
7877   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7878
7879   return compound_stmt;
7880 }
7881
7882 /* Parse an (optional) statement-seq.
7883
7884    statement-seq:
7885      statement
7886      statement-seq [opt] statement  */
7887
7888 static void
7889 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7890 {
7891   /* Scan statements until there aren't any more.  */
7892   while (true)
7893     {
7894       cp_token *token = cp_lexer_peek_token (parser->lexer);
7895
7896       /* If we're looking at a `}', then we've run out of statements.  */
7897       if (token->type == CPP_CLOSE_BRACE
7898           || token->type == CPP_EOF
7899           || token->type == CPP_PRAGMA_EOL)
7900         break;
7901       
7902       /* If we are in a compound statement and find 'else' then
7903          something went wrong.  */
7904       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7905         {
7906           if (parser->in_statement & IN_IF_STMT) 
7907             break;
7908           else
7909             {
7910               token = cp_lexer_consume_token (parser->lexer);
7911               error_at (token->location, "%<else%> without a previous %<if%>");
7912             }
7913         }
7914
7915       /* Parse the statement.  */
7916       cp_parser_statement (parser, in_statement_expr, true, NULL);
7917     }
7918 }
7919
7920 /* Parse a selection-statement.
7921
7922    selection-statement:
7923      if ( condition ) statement
7924      if ( condition ) statement else statement
7925      switch ( condition ) statement
7926
7927    Returns the new IF_STMT or SWITCH_STMT.
7928
7929    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7930    is a (possibly labeled) if statement which is not enclosed in
7931    braces and has an else clause.  This is used to implement
7932    -Wparentheses.  */
7933
7934 static tree
7935 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7936 {
7937   cp_token *token;
7938   enum rid keyword;
7939
7940   if (if_p != NULL)
7941     *if_p = false;
7942
7943   /* Peek at the next token.  */
7944   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7945
7946   /* See what kind of keyword it is.  */
7947   keyword = token->keyword;
7948   switch (keyword)
7949     {
7950     case RID_IF:
7951     case RID_SWITCH:
7952       {
7953         tree statement;
7954         tree condition;
7955
7956         /* Look for the `('.  */
7957         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7958           {
7959             cp_parser_skip_to_end_of_statement (parser);
7960             return error_mark_node;
7961           }
7962
7963         /* Begin the selection-statement.  */
7964         if (keyword == RID_IF)
7965           statement = begin_if_stmt ();
7966         else
7967           statement = begin_switch_stmt ();
7968
7969         /* Parse the condition.  */
7970         condition = cp_parser_condition (parser);
7971         /* Look for the `)'.  */
7972         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7973           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7974                                                  /*consume_paren=*/true);
7975
7976         if (keyword == RID_IF)
7977           {
7978             bool nested_if;
7979             unsigned char in_statement;
7980
7981             /* Add the condition.  */
7982             finish_if_stmt_cond (condition, statement);
7983
7984             /* Parse the then-clause.  */
7985             in_statement = parser->in_statement;
7986             parser->in_statement |= IN_IF_STMT;
7987             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7988               {
7989                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7990                 add_stmt (build_empty_stmt (loc));
7991                 cp_lexer_consume_token (parser->lexer);
7992                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7993                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7994                               "empty body in an %<if%> statement");
7995                 nested_if = false;
7996               }
7997             else
7998               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7999             parser->in_statement = in_statement;
8000
8001             finish_then_clause (statement);
8002
8003             /* If the next token is `else', parse the else-clause.  */
8004             if (cp_lexer_next_token_is_keyword (parser->lexer,
8005                                                 RID_ELSE))
8006               {
8007                 /* Consume the `else' keyword.  */
8008                 cp_lexer_consume_token (parser->lexer);
8009                 begin_else_clause (statement);
8010                 /* Parse the else-clause.  */
8011                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8012                   {
8013                     location_t loc;
8014                     loc = cp_lexer_peek_token (parser->lexer)->location;
8015                     warning_at (loc,
8016                                 OPT_Wempty_body, "suggest braces around "
8017                                 "empty body in an %<else%> statement");
8018                     add_stmt (build_empty_stmt (loc));
8019                     cp_lexer_consume_token (parser->lexer);
8020                   }
8021                 else
8022                   cp_parser_implicitly_scoped_statement (parser, NULL);
8023
8024                 finish_else_clause (statement);
8025
8026                 /* If we are currently parsing a then-clause, then
8027                    IF_P will not be NULL.  We set it to true to
8028                    indicate that this if statement has an else clause.
8029                    This may trigger the Wparentheses warning below
8030                    when we get back up to the parent if statement.  */
8031                 if (if_p != NULL)
8032                   *if_p = true;
8033               }
8034             else
8035               {
8036                 /* This if statement does not have an else clause.  If
8037                    NESTED_IF is true, then the then-clause is an if
8038                    statement which does have an else clause.  We warn
8039                    about the potential ambiguity.  */
8040                 if (nested_if)
8041                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8042                               "suggest explicit braces to avoid ambiguous"
8043                               " %<else%>");
8044               }
8045
8046             /* Now we're all done with the if-statement.  */
8047             finish_if_stmt (statement);
8048           }
8049         else
8050           {
8051             bool in_switch_statement_p;
8052             unsigned char in_statement;
8053
8054             /* Add the condition.  */
8055             finish_switch_cond (condition, statement);
8056
8057             /* Parse the body of the switch-statement.  */
8058             in_switch_statement_p = parser->in_switch_statement_p;
8059             in_statement = parser->in_statement;
8060             parser->in_switch_statement_p = true;
8061             parser->in_statement |= IN_SWITCH_STMT;
8062             cp_parser_implicitly_scoped_statement (parser, NULL);
8063             parser->in_switch_statement_p = in_switch_statement_p;
8064             parser->in_statement = in_statement;
8065
8066             /* Now we're all done with the switch-statement.  */
8067             finish_switch_stmt (statement);
8068           }
8069
8070         return statement;
8071       }
8072       break;
8073
8074     default:
8075       cp_parser_error (parser, "expected selection-statement");
8076       return error_mark_node;
8077     }
8078 }
8079
8080 /* Parse a condition.
8081
8082    condition:
8083      expression
8084      type-specifier-seq declarator = initializer-clause
8085      type-specifier-seq declarator braced-init-list
8086
8087    GNU Extension:
8088
8089    condition:
8090      type-specifier-seq declarator asm-specification [opt]
8091        attributes [opt] = assignment-expression
8092
8093    Returns the expression that should be tested.  */
8094
8095 static tree
8096 cp_parser_condition (cp_parser* parser)
8097 {
8098   cp_decl_specifier_seq type_specifiers;
8099   const char *saved_message;
8100
8101   /* Try the declaration first.  */
8102   cp_parser_parse_tentatively (parser);
8103   /* New types are not allowed in the type-specifier-seq for a
8104      condition.  */
8105   saved_message = parser->type_definition_forbidden_message;
8106   parser->type_definition_forbidden_message
8107     = G_("types may not be defined in conditions");
8108   /* Parse the type-specifier-seq.  */
8109   cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8110                                 /*is_trailing_return=*/false,
8111                                 &type_specifiers);
8112   /* Restore the saved message.  */
8113   parser->type_definition_forbidden_message = saved_message;
8114   /* If all is well, we might be looking at a declaration.  */
8115   if (!cp_parser_error_occurred (parser))
8116     {
8117       tree decl;
8118       tree asm_specification;
8119       tree attributes;
8120       cp_declarator *declarator;
8121       tree initializer = NULL_TREE;
8122
8123       /* Parse the declarator.  */
8124       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8125                                          /*ctor_dtor_or_conv_p=*/NULL,
8126                                          /*parenthesized_p=*/NULL,
8127                                          /*member_p=*/false);
8128       /* Parse the attributes.  */
8129       attributes = cp_parser_attributes_opt (parser);
8130       /* Parse the asm-specification.  */
8131       asm_specification = cp_parser_asm_specification_opt (parser);
8132       /* If the next token is not an `=' or '{', then we might still be
8133          looking at an expression.  For example:
8134
8135            if (A(a).x)
8136
8137          looks like a decl-specifier-seq and a declarator -- but then
8138          there is no `=', so this is an expression.  */
8139       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8140           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8141         cp_parser_simulate_error (parser);
8142         
8143       /* If we did see an `=' or '{', then we are looking at a declaration
8144          for sure.  */
8145       if (cp_parser_parse_definitely (parser))
8146         {
8147           tree pushed_scope;
8148           bool non_constant_p;
8149           bool flags = LOOKUP_ONLYCONVERTING;
8150
8151           /* Create the declaration.  */
8152           decl = start_decl (declarator, &type_specifiers,
8153                              /*initialized_p=*/true,
8154                              attributes, /*prefix_attributes=*/NULL_TREE,
8155                              &pushed_scope);
8156
8157           /* Parse the initializer.  */
8158           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8159             {
8160               initializer = cp_parser_braced_list (parser, &non_constant_p);
8161               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8162               flags = 0;
8163             }
8164           else
8165             {
8166               /* Consume the `='.  */
8167               cp_parser_require (parser, CPP_EQ, "%<=%>");
8168               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8169             }
8170           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8171             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8172
8173           if (!non_constant_p)
8174             initializer = fold_non_dependent_expr (initializer);
8175
8176           /* Process the initializer.  */
8177           cp_finish_decl (decl,
8178                           initializer, !non_constant_p,
8179                           asm_specification,
8180                           flags);
8181
8182           if (pushed_scope)
8183             pop_scope (pushed_scope);
8184
8185           return convert_from_reference (decl);
8186         }
8187     }
8188   /* If we didn't even get past the declarator successfully, we are
8189      definitely not looking at a declaration.  */
8190   else
8191     cp_parser_abort_tentative_parse (parser);
8192
8193   /* Otherwise, we are looking at an expression.  */
8194   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8195 }
8196
8197 /* Parse an iteration-statement.
8198
8199    iteration-statement:
8200      while ( condition ) statement
8201      do statement while ( expression ) ;
8202      for ( for-init-statement condition [opt] ; expression [opt] )
8203        statement
8204
8205    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
8206
8207 static tree
8208 cp_parser_iteration_statement (cp_parser* parser)
8209 {
8210   cp_token *token;
8211   enum rid keyword;
8212   tree statement;
8213   unsigned char in_statement;
8214
8215   /* Peek at the next token.  */
8216   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8217   if (!token)
8218     return error_mark_node;
8219
8220   /* Remember whether or not we are already within an iteration
8221      statement.  */
8222   in_statement = parser->in_statement;
8223
8224   /* See what kind of keyword it is.  */
8225   keyword = token->keyword;
8226   switch (keyword)
8227     {
8228     case RID_WHILE:
8229       {
8230         tree condition;
8231
8232         /* Begin the while-statement.  */
8233         statement = begin_while_stmt ();
8234         /* Look for the `('.  */
8235         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8236         /* Parse the condition.  */
8237         condition = cp_parser_condition (parser);
8238         finish_while_stmt_cond (condition, statement);
8239         /* Look for the `)'.  */
8240         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8241         /* Parse the dependent statement.  */
8242         parser->in_statement = IN_ITERATION_STMT;
8243         cp_parser_already_scoped_statement (parser);
8244         parser->in_statement = in_statement;
8245         /* We're done with the while-statement.  */
8246         finish_while_stmt (statement);
8247       }
8248       break;
8249
8250     case RID_DO:
8251       {
8252         tree expression;
8253
8254         /* Begin the do-statement.  */
8255         statement = begin_do_stmt ();
8256         /* Parse the body of the do-statement.  */
8257         parser->in_statement = IN_ITERATION_STMT;
8258         cp_parser_implicitly_scoped_statement (parser, NULL);
8259         parser->in_statement = in_statement;
8260         finish_do_body (statement);
8261         /* Look for the `while' keyword.  */
8262         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8263         /* Look for the `('.  */
8264         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8265         /* Parse the expression.  */
8266         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8267         /* We're done with the do-statement.  */
8268         finish_do_stmt (expression, statement);
8269         /* Look for the `)'.  */
8270         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8271         /* Look for the `;'.  */
8272         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8273       }
8274       break;
8275
8276     case RID_FOR:
8277       {
8278         tree condition = NULL_TREE;
8279         tree expression = NULL_TREE;
8280
8281         /* Begin the for-statement.  */
8282         statement = begin_for_stmt ();
8283         /* Look for the `('.  */
8284         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8285         /* Parse the initialization.  */
8286         cp_parser_for_init_statement (parser);
8287         finish_for_init_stmt (statement);
8288
8289         /* If there's a condition, process it.  */
8290         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8291           condition = cp_parser_condition (parser);
8292         finish_for_cond (condition, statement);
8293         /* Look for the `;'.  */
8294         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8295
8296         /* If there's an expression, process it.  */
8297         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8298           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8299         finish_for_expr (expression, statement);
8300         /* Look for the `)'.  */
8301         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8302
8303         /* Parse the body of the for-statement.  */
8304         parser->in_statement = IN_ITERATION_STMT;
8305         cp_parser_already_scoped_statement (parser);
8306         parser->in_statement = in_statement;
8307
8308         /* We're done with the for-statement.  */
8309         finish_for_stmt (statement);
8310       }
8311       break;
8312
8313     default:
8314       cp_parser_error (parser, "expected iteration-statement");
8315       statement = error_mark_node;
8316       break;
8317     }
8318
8319   return statement;
8320 }
8321
8322 /* Parse a for-init-statement.
8323
8324    for-init-statement:
8325      expression-statement
8326      simple-declaration  */
8327
8328 static void
8329 cp_parser_for_init_statement (cp_parser* parser)
8330 {
8331   /* If the next token is a `;', then we have an empty
8332      expression-statement.  Grammatically, this is also a
8333      simple-declaration, but an invalid one, because it does not
8334      declare anything.  Therefore, if we did not handle this case
8335      specially, we would issue an error message about an invalid
8336      declaration.  */
8337   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8338     {
8339       /* We're going to speculatively look for a declaration, falling back
8340          to an expression, if necessary.  */
8341       cp_parser_parse_tentatively (parser);
8342       /* Parse the declaration.  */
8343       cp_parser_simple_declaration (parser,
8344                                     /*function_definition_allowed_p=*/false);
8345       /* If the tentative parse failed, then we shall need to look for an
8346          expression-statement.  */
8347       if (cp_parser_parse_definitely (parser))
8348         return;
8349     }
8350
8351   cp_parser_expression_statement (parser, false);
8352 }
8353
8354 /* Parse a jump-statement.
8355
8356    jump-statement:
8357      break ;
8358      continue ;
8359      return expression [opt] ;
8360      return braced-init-list ;
8361      goto identifier ;
8362
8363    GNU extension:
8364
8365    jump-statement:
8366      goto * expression ;
8367
8368    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
8369
8370 static tree
8371 cp_parser_jump_statement (cp_parser* parser)
8372 {
8373   tree statement = error_mark_node;
8374   cp_token *token;
8375   enum rid keyword;
8376   unsigned char in_statement;
8377
8378   /* Peek at the next token.  */
8379   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8380   if (!token)
8381     return error_mark_node;
8382
8383   /* See what kind of keyword it is.  */
8384   keyword = token->keyword;
8385   switch (keyword)
8386     {
8387     case RID_BREAK:
8388       in_statement = parser->in_statement & ~IN_IF_STMT;      
8389       switch (in_statement)
8390         {
8391         case 0:
8392           error_at (token->location, "break statement not within loop or switch");
8393           break;
8394         default:
8395           gcc_assert ((in_statement & IN_SWITCH_STMT)
8396                       || in_statement == IN_ITERATION_STMT);
8397           statement = finish_break_stmt ();
8398           break;
8399         case IN_OMP_BLOCK:
8400           error_at (token->location, "invalid exit from OpenMP structured block");
8401           break;
8402         case IN_OMP_FOR:
8403           error_at (token->location, "break statement used with OpenMP for loop");
8404           break;
8405         }
8406       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8407       break;
8408
8409     case RID_CONTINUE:
8410       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8411         {
8412         case 0:
8413           error_at (token->location, "continue statement not within a loop");
8414           break;
8415         case IN_ITERATION_STMT:
8416         case IN_OMP_FOR:
8417           statement = finish_continue_stmt ();
8418           break;
8419         case IN_OMP_BLOCK:
8420           error_at (token->location, "invalid exit from OpenMP structured block");
8421           break;
8422         default:
8423           gcc_unreachable ();
8424         }
8425       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8426       break;
8427
8428     case RID_RETURN:
8429       {
8430         tree expr;
8431         bool expr_non_constant_p;
8432
8433         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8434           {
8435             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8436             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8437           }
8438         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8439           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8440         else
8441           /* If the next token is a `;', then there is no
8442              expression.  */
8443           expr = NULL_TREE;
8444         /* Build the return-statement.  */
8445         statement = finish_return_stmt (expr);
8446         /* Look for the final `;'.  */
8447         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8448       }
8449       break;
8450
8451     case RID_GOTO:
8452       /* Create the goto-statement.  */
8453       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8454         {
8455           /* Issue a warning about this use of a GNU extension.  */
8456           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8457           /* Consume the '*' token.  */
8458           cp_lexer_consume_token (parser->lexer);
8459           /* Parse the dependent expression.  */
8460           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8461         }
8462       else
8463         finish_goto_stmt (cp_parser_identifier (parser));
8464       /* Look for the final `;'.  */
8465       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8466       break;
8467
8468     default:
8469       cp_parser_error (parser, "expected jump-statement");
8470       break;
8471     }
8472
8473   return statement;
8474 }
8475
8476 /* Parse a declaration-statement.
8477
8478    declaration-statement:
8479      block-declaration  */
8480
8481 static void
8482 cp_parser_declaration_statement (cp_parser* parser)
8483 {
8484   void *p;
8485
8486   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8487   p = obstack_alloc (&declarator_obstack, 0);
8488
8489  /* Parse the block-declaration.  */
8490   cp_parser_block_declaration (parser, /*statement_p=*/true);
8491
8492   /* Free any declarators allocated.  */
8493   obstack_free (&declarator_obstack, p);
8494
8495   /* Finish off the statement.  */
8496   finish_stmt ();
8497 }
8498
8499 /* Some dependent statements (like `if (cond) statement'), are
8500    implicitly in their own scope.  In other words, if the statement is
8501    a single statement (as opposed to a compound-statement), it is
8502    none-the-less treated as if it were enclosed in braces.  Any
8503    declarations appearing in the dependent statement are out of scope
8504    after control passes that point.  This function parses a statement,
8505    but ensures that is in its own scope, even if it is not a
8506    compound-statement.
8507
8508    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8509    is a (possibly labeled) if statement which is not enclosed in
8510    braces and has an else clause.  This is used to implement
8511    -Wparentheses.
8512
8513    Returns the new statement.  */
8514
8515 static tree
8516 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8517 {
8518   tree statement;
8519
8520   if (if_p != NULL)
8521     *if_p = false;
8522
8523   /* Mark if () ; with a special NOP_EXPR.  */
8524   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8525     {
8526       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8527       cp_lexer_consume_token (parser->lexer);
8528       statement = add_stmt (build_empty_stmt (loc));
8529     }
8530   /* if a compound is opened, we simply parse the statement directly.  */
8531   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8532     statement = cp_parser_compound_statement (parser, NULL, false);
8533   /* If the token is not a `{', then we must take special action.  */
8534   else
8535     {
8536       /* Create a compound-statement.  */
8537       statement = begin_compound_stmt (0);
8538       /* Parse the dependent-statement.  */
8539       cp_parser_statement (parser, NULL_TREE, false, if_p);
8540       /* Finish the dummy compound-statement.  */
8541       finish_compound_stmt (statement);
8542     }
8543
8544   /* Return the statement.  */
8545   return statement;
8546 }
8547
8548 /* For some dependent statements (like `while (cond) statement'), we
8549    have already created a scope.  Therefore, even if the dependent
8550    statement is a compound-statement, we do not want to create another
8551    scope.  */
8552
8553 static void
8554 cp_parser_already_scoped_statement (cp_parser* parser)
8555 {
8556   /* If the token is a `{', then we must take special action.  */
8557   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8558     cp_parser_statement (parser, NULL_TREE, false, NULL);
8559   else
8560     {
8561       /* Avoid calling cp_parser_compound_statement, so that we
8562          don't create a new scope.  Do everything else by hand.  */
8563       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8564       /* If the next keyword is `__label__' we have a label declaration.  */
8565       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8566         cp_parser_label_declaration (parser);
8567       /* Parse an (optional) statement-seq.  */
8568       cp_parser_statement_seq_opt (parser, NULL_TREE);
8569       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8570     }
8571 }
8572
8573 /* Declarations [gram.dcl.dcl] */
8574
8575 /* Parse an optional declaration-sequence.
8576
8577    declaration-seq:
8578      declaration
8579      declaration-seq declaration  */
8580
8581 static void
8582 cp_parser_declaration_seq_opt (cp_parser* parser)
8583 {
8584   while (true)
8585     {
8586       cp_token *token;
8587
8588       token = cp_lexer_peek_token (parser->lexer);
8589
8590       if (token->type == CPP_CLOSE_BRACE
8591           || token->type == CPP_EOF
8592           || token->type == CPP_PRAGMA_EOL)
8593         break;
8594
8595       if (token->type == CPP_SEMICOLON)
8596         {
8597           /* A declaration consisting of a single semicolon is
8598              invalid.  Allow it unless we're being pedantic.  */
8599           cp_lexer_consume_token (parser->lexer);
8600           if (!in_system_header)
8601             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8602           continue;
8603         }
8604
8605       /* If we're entering or exiting a region that's implicitly
8606          extern "C", modify the lang context appropriately.  */
8607       if (!parser->implicit_extern_c && token->implicit_extern_c)
8608         {
8609           push_lang_context (lang_name_c);
8610           parser->implicit_extern_c = true;
8611         }
8612       else if (parser->implicit_extern_c && !token->implicit_extern_c)
8613         {
8614           pop_lang_context ();
8615           parser->implicit_extern_c = false;
8616         }
8617
8618       if (token->type == CPP_PRAGMA)
8619         {
8620           /* A top-level declaration can consist solely of a #pragma.
8621              A nested declaration cannot, so this is done here and not
8622              in cp_parser_declaration.  (A #pragma at block scope is
8623              handled in cp_parser_statement.)  */
8624           cp_parser_pragma (parser, pragma_external);
8625           continue;
8626         }
8627
8628       /* Parse the declaration itself.  */
8629       cp_parser_declaration (parser);
8630     }
8631 }
8632
8633 /* Parse a declaration.
8634
8635    declaration:
8636      block-declaration
8637      function-definition
8638      template-declaration
8639      explicit-instantiation
8640      explicit-specialization
8641      linkage-specification
8642      namespace-definition
8643
8644    GNU extension:
8645
8646    declaration:
8647       __extension__ declaration */
8648
8649 static void
8650 cp_parser_declaration (cp_parser* parser)
8651 {
8652   cp_token token1;
8653   cp_token token2;
8654   int saved_pedantic;
8655   void *p;
8656
8657   /* Check for the `__extension__' keyword.  */
8658   if (cp_parser_extension_opt (parser, &saved_pedantic))
8659     {
8660       /* Parse the qualified declaration.  */
8661       cp_parser_declaration (parser);
8662       /* Restore the PEDANTIC flag.  */
8663       pedantic = saved_pedantic;
8664
8665       return;
8666     }
8667
8668   /* Try to figure out what kind of declaration is present.  */
8669   token1 = *cp_lexer_peek_token (parser->lexer);
8670
8671   if (token1.type != CPP_EOF)
8672     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8673   else
8674     {
8675       token2.type = CPP_EOF;
8676       token2.keyword = RID_MAX;
8677     }
8678
8679   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8680   p = obstack_alloc (&declarator_obstack, 0);
8681
8682   /* If the next token is `extern' and the following token is a string
8683      literal, then we have a linkage specification.  */
8684   if (token1.keyword == RID_EXTERN
8685       && cp_parser_is_string_literal (&token2))
8686     cp_parser_linkage_specification (parser);
8687   /* If the next token is `template', then we have either a template
8688      declaration, an explicit instantiation, or an explicit
8689      specialization.  */
8690   else if (token1.keyword == RID_TEMPLATE)
8691     {
8692       /* `template <>' indicates a template specialization.  */
8693       if (token2.type == CPP_LESS
8694           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8695         cp_parser_explicit_specialization (parser);
8696       /* `template <' indicates a template declaration.  */
8697       else if (token2.type == CPP_LESS)
8698         cp_parser_template_declaration (parser, /*member_p=*/false);
8699       /* Anything else must be an explicit instantiation.  */
8700       else
8701         cp_parser_explicit_instantiation (parser);
8702     }
8703   /* If the next token is `export', then we have a template
8704      declaration.  */
8705   else if (token1.keyword == RID_EXPORT)
8706     cp_parser_template_declaration (parser, /*member_p=*/false);
8707   /* If the next token is `extern', 'static' or 'inline' and the one
8708      after that is `template', we have a GNU extended explicit
8709      instantiation directive.  */
8710   else if (cp_parser_allow_gnu_extensions_p (parser)
8711            && (token1.keyword == RID_EXTERN
8712                || token1.keyword == RID_STATIC
8713                || token1.keyword == RID_INLINE)
8714            && token2.keyword == RID_TEMPLATE)
8715     cp_parser_explicit_instantiation (parser);
8716   /* If the next token is `namespace', check for a named or unnamed
8717      namespace definition.  */
8718   else if (token1.keyword == RID_NAMESPACE
8719            && (/* A named namespace definition.  */
8720                (token2.type == CPP_NAME
8721                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8722                     != CPP_EQ))
8723                /* An unnamed namespace definition.  */
8724                || token2.type == CPP_OPEN_BRACE
8725                || token2.keyword == RID_ATTRIBUTE))
8726     cp_parser_namespace_definition (parser);
8727   /* An inline (associated) namespace definition.  */
8728   else if (token1.keyword == RID_INLINE
8729            && token2.keyword == RID_NAMESPACE)
8730     cp_parser_namespace_definition (parser);
8731   /* Objective-C++ declaration/definition.  */
8732   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8733     cp_parser_objc_declaration (parser);
8734   /* We must have either a block declaration or a function
8735      definition.  */
8736   else
8737     /* Try to parse a block-declaration, or a function-definition.  */
8738     cp_parser_block_declaration (parser, /*statement_p=*/false);
8739
8740   /* Free any declarators allocated.  */
8741   obstack_free (&declarator_obstack, p);
8742 }
8743
8744 /* Parse a block-declaration.
8745
8746    block-declaration:
8747      simple-declaration
8748      asm-definition
8749      namespace-alias-definition
8750      using-declaration
8751      using-directive
8752
8753    GNU Extension:
8754
8755    block-declaration:
8756      __extension__ block-declaration
8757
8758    C++0x Extension:
8759
8760    block-declaration:
8761      static_assert-declaration
8762
8763    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8764    part of a declaration-statement.  */
8765
8766 static void
8767 cp_parser_block_declaration (cp_parser *parser,
8768                              bool      statement_p)
8769 {
8770   cp_token *token1;
8771   int saved_pedantic;
8772
8773   /* Check for the `__extension__' keyword.  */
8774   if (cp_parser_extension_opt (parser, &saved_pedantic))
8775     {
8776       /* Parse the qualified declaration.  */
8777       cp_parser_block_declaration (parser, statement_p);
8778       /* Restore the PEDANTIC flag.  */
8779       pedantic = saved_pedantic;
8780
8781       return;
8782     }
8783
8784   /* Peek at the next token to figure out which kind of declaration is
8785      present.  */
8786   token1 = cp_lexer_peek_token (parser->lexer);
8787
8788   /* If the next keyword is `asm', we have an asm-definition.  */
8789   if (token1->keyword == RID_ASM)
8790     {
8791       if (statement_p)
8792         cp_parser_commit_to_tentative_parse (parser);
8793       cp_parser_asm_definition (parser);
8794     }
8795   /* If the next keyword is `namespace', we have a
8796      namespace-alias-definition.  */
8797   else if (token1->keyword == RID_NAMESPACE)
8798     cp_parser_namespace_alias_definition (parser);
8799   /* If the next keyword is `using', we have either a
8800      using-declaration or a using-directive.  */
8801   else if (token1->keyword == RID_USING)
8802     {
8803       cp_token *token2;
8804
8805       if (statement_p)
8806         cp_parser_commit_to_tentative_parse (parser);
8807       /* If the token after `using' is `namespace', then we have a
8808          using-directive.  */
8809       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8810       if (token2->keyword == RID_NAMESPACE)
8811         cp_parser_using_directive (parser);
8812       /* Otherwise, it's a using-declaration.  */
8813       else
8814         cp_parser_using_declaration (parser,
8815                                      /*access_declaration_p=*/false);
8816     }
8817   /* If the next keyword is `__label__' we have a misplaced label
8818      declaration.  */
8819   else if (token1->keyword == RID_LABEL)
8820     {
8821       cp_lexer_consume_token (parser->lexer);
8822       error_at (token1->location, "%<__label__%> not at the beginning of a block");
8823       cp_parser_skip_to_end_of_statement (parser);
8824       /* If the next token is now a `;', consume it.  */
8825       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8826         cp_lexer_consume_token (parser->lexer);
8827     }
8828   /* If the next token is `static_assert' we have a static assertion.  */
8829   else if (token1->keyword == RID_STATIC_ASSERT)
8830     cp_parser_static_assert (parser, /*member_p=*/false);
8831   /* Anything else must be a simple-declaration.  */
8832   else
8833     cp_parser_simple_declaration (parser, !statement_p);
8834 }
8835
8836 /* Parse a simple-declaration.
8837
8838    simple-declaration:
8839      decl-specifier-seq [opt] init-declarator-list [opt] ;
8840
8841    init-declarator-list:
8842      init-declarator
8843      init-declarator-list , init-declarator
8844
8845    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8846    function-definition as a simple-declaration.  */
8847
8848 static void
8849 cp_parser_simple_declaration (cp_parser* parser,
8850                               bool function_definition_allowed_p)
8851 {
8852   cp_decl_specifier_seq decl_specifiers;
8853   int declares_class_or_enum;
8854   bool saw_declarator;
8855
8856   /* Defer access checks until we know what is being declared; the
8857      checks for names appearing in the decl-specifier-seq should be
8858      done as if we were in the scope of the thing being declared.  */
8859   push_deferring_access_checks (dk_deferred);
8860
8861   /* Parse the decl-specifier-seq.  We have to keep track of whether
8862      or not the decl-specifier-seq declares a named class or
8863      enumeration type, since that is the only case in which the
8864      init-declarator-list is allowed to be empty.
8865
8866      [dcl.dcl]
8867
8868      In a simple-declaration, the optional init-declarator-list can be
8869      omitted only when declaring a class or enumeration, that is when
8870      the decl-specifier-seq contains either a class-specifier, an
8871      elaborated-type-specifier, or an enum-specifier.  */
8872   cp_parser_decl_specifier_seq (parser,
8873                                 CP_PARSER_FLAGS_OPTIONAL,
8874                                 &decl_specifiers,
8875                                 &declares_class_or_enum);
8876   /* We no longer need to defer access checks.  */
8877   stop_deferring_access_checks ();
8878
8879   /* In a block scope, a valid declaration must always have a
8880      decl-specifier-seq.  By not trying to parse declarators, we can
8881      resolve the declaration/expression ambiguity more quickly.  */
8882   if (!function_definition_allowed_p
8883       && !decl_specifiers.any_specifiers_p)
8884     {
8885       cp_parser_error (parser, "expected declaration");
8886       goto done;
8887     }
8888
8889   /* If the next two tokens are both identifiers, the code is
8890      erroneous. The usual cause of this situation is code like:
8891
8892        T t;
8893
8894      where "T" should name a type -- but does not.  */
8895   if (!decl_specifiers.any_type_specifiers_p
8896       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8897     {
8898       /* If parsing tentatively, we should commit; we really are
8899          looking at a declaration.  */
8900       cp_parser_commit_to_tentative_parse (parser);
8901       /* Give up.  */
8902       goto done;
8903     }
8904
8905   /* If we have seen at least one decl-specifier, and the next token
8906      is not a parenthesis, then we must be looking at a declaration.
8907      (After "int (" we might be looking at a functional cast.)  */
8908   if (decl_specifiers.any_specifiers_p
8909       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8910       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8911       && !cp_parser_error_occurred (parser))
8912     cp_parser_commit_to_tentative_parse (parser);
8913
8914   /* Keep going until we hit the `;' at the end of the simple
8915      declaration.  */
8916   saw_declarator = false;
8917   while (cp_lexer_next_token_is_not (parser->lexer,
8918                                      CPP_SEMICOLON))
8919     {
8920       cp_token *token;
8921       bool function_definition_p;
8922       tree decl;
8923
8924       if (saw_declarator)
8925         {
8926           /* If we are processing next declarator, coma is expected */
8927           token = cp_lexer_peek_token (parser->lexer);
8928           gcc_assert (token->type == CPP_COMMA);
8929           cp_lexer_consume_token (parser->lexer);
8930         }
8931       else
8932         saw_declarator = true;
8933
8934       /* Parse the init-declarator.  */
8935       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8936                                         /*checks=*/NULL,
8937                                         function_definition_allowed_p,
8938                                         /*member_p=*/false,
8939                                         declares_class_or_enum,
8940                                         &function_definition_p);
8941       /* If an error occurred while parsing tentatively, exit quickly.
8942          (That usually happens when in the body of a function; each
8943          statement is treated as a declaration-statement until proven
8944          otherwise.)  */
8945       if (cp_parser_error_occurred (parser))
8946         goto done;
8947       /* Handle function definitions specially.  */
8948       if (function_definition_p)
8949         {
8950           /* If the next token is a `,', then we are probably
8951              processing something like:
8952
8953                void f() {}, *p;
8954
8955              which is erroneous.  */
8956           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8957             {
8958               cp_token *token = cp_lexer_peek_token (parser->lexer);
8959               error_at (token->location,
8960                         "mixing"
8961                         " declarations and function-definitions is forbidden");
8962             }
8963           /* Otherwise, we're done with the list of declarators.  */
8964           else
8965             {
8966               pop_deferring_access_checks ();
8967               return;
8968             }
8969         }
8970       /* The next token should be either a `,' or a `;'.  */
8971       token = cp_lexer_peek_token (parser->lexer);
8972       /* If it's a `,', there are more declarators to come.  */
8973       if (token->type == CPP_COMMA)
8974         /* will be consumed next time around */;
8975       /* If it's a `;', we are done.  */
8976       else if (token->type == CPP_SEMICOLON)
8977         break;
8978       /* Anything else is an error.  */
8979       else
8980         {
8981           /* If we have already issued an error message we don't need
8982              to issue another one.  */
8983           if (decl != error_mark_node
8984               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8985             cp_parser_error (parser, "expected %<,%> or %<;%>");
8986           /* Skip tokens until we reach the end of the statement.  */
8987           cp_parser_skip_to_end_of_statement (parser);
8988           /* If the next token is now a `;', consume it.  */
8989           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8990             cp_lexer_consume_token (parser->lexer);
8991           goto done;
8992         }
8993       /* After the first time around, a function-definition is not
8994          allowed -- even if it was OK at first.  For example:
8995
8996            int i, f() {}
8997
8998          is not valid.  */
8999       function_definition_allowed_p = false;
9000     }
9001
9002   /* Issue an error message if no declarators are present, and the
9003      decl-specifier-seq does not itself declare a class or
9004      enumeration.  */
9005   if (!saw_declarator)
9006     {
9007       if (cp_parser_declares_only_class_p (parser))
9008         shadow_tag (&decl_specifiers);
9009       /* Perform any deferred access checks.  */
9010       perform_deferred_access_checks ();
9011     }
9012
9013   /* Consume the `;'.  */
9014   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9015
9016  done:
9017   pop_deferring_access_checks ();
9018 }
9019
9020 /* Parse a decl-specifier-seq.
9021
9022    decl-specifier-seq:
9023      decl-specifier-seq [opt] decl-specifier
9024
9025    decl-specifier:
9026      storage-class-specifier
9027      type-specifier
9028      function-specifier
9029      friend
9030      typedef
9031
9032    GNU Extension:
9033
9034    decl-specifier:
9035      attributes
9036
9037    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9038
9039    The parser flags FLAGS is used to control type-specifier parsing.
9040
9041    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9042    flags:
9043
9044      1: one of the decl-specifiers is an elaborated-type-specifier
9045         (i.e., a type declaration)
9046      2: one of the decl-specifiers is an enum-specifier or a
9047         class-specifier (i.e., a type definition)
9048
9049    */
9050
9051 static void
9052 cp_parser_decl_specifier_seq (cp_parser* parser,
9053                               cp_parser_flags flags,
9054                               cp_decl_specifier_seq *decl_specs,
9055                               int* declares_class_or_enum)
9056 {
9057   bool constructor_possible_p = !parser->in_declarator_p;
9058   cp_token *start_token = NULL;
9059
9060   /* Clear DECL_SPECS.  */
9061   clear_decl_specs (decl_specs);
9062
9063   /* Assume no class or enumeration type is declared.  */
9064   *declares_class_or_enum = 0;
9065
9066   /* Keep reading specifiers until there are no more to read.  */
9067   while (true)
9068     {
9069       bool constructor_p;
9070       bool found_decl_spec;
9071       cp_token *token;
9072
9073       /* Peek at the next token.  */
9074       token = cp_lexer_peek_token (parser->lexer);
9075
9076       /* Save the first token of the decl spec list for error
9077          reporting.  */
9078       if (!start_token)
9079         start_token = token;
9080       /* Handle attributes.  */
9081       if (token->keyword == RID_ATTRIBUTE)
9082         {
9083           /* Parse the attributes.  */
9084           decl_specs->attributes
9085             = chainon (decl_specs->attributes,
9086                        cp_parser_attributes_opt (parser));
9087           continue;
9088         }
9089       /* Assume we will find a decl-specifier keyword.  */
9090       found_decl_spec = true;
9091       /* If the next token is an appropriate keyword, we can simply
9092          add it to the list.  */
9093       switch (token->keyword)
9094         {
9095           /* decl-specifier:
9096                friend
9097                constexpr */
9098         case RID_FRIEND:
9099           if (!at_class_scope_p ())
9100             {
9101               error_at (token->location, "%<friend%> used outside of class");
9102               cp_lexer_purge_token (parser->lexer);
9103             }
9104           else
9105             {
9106               ++decl_specs->specs[(int) ds_friend];
9107               /* Consume the token.  */
9108               cp_lexer_consume_token (parser->lexer);
9109             }
9110           break;
9111
9112         case RID_CONSTEXPR:
9113           ++decl_specs->specs[(int) ds_constexpr];
9114           cp_lexer_consume_token (parser->lexer);
9115           break;
9116
9117           /* function-specifier:
9118                inline
9119                virtual
9120                explicit  */
9121         case RID_INLINE:
9122         case RID_VIRTUAL:
9123         case RID_EXPLICIT:
9124           cp_parser_function_specifier_opt (parser, decl_specs);
9125           break;
9126
9127           /* decl-specifier:
9128                typedef  */
9129         case RID_TYPEDEF:
9130           ++decl_specs->specs[(int) ds_typedef];
9131           /* Consume the token.  */
9132           cp_lexer_consume_token (parser->lexer);
9133           /* A constructor declarator cannot appear in a typedef.  */
9134           constructor_possible_p = false;
9135           /* The "typedef" keyword can only occur in a declaration; we
9136              may as well commit at this point.  */
9137           cp_parser_commit_to_tentative_parse (parser);
9138
9139           if (decl_specs->storage_class != sc_none)
9140             decl_specs->conflicting_specifiers_p = true;
9141           break;
9142
9143           /* storage-class-specifier:
9144                auto
9145                register
9146                static
9147                extern
9148                mutable
9149
9150              GNU Extension:
9151                thread  */
9152         case RID_AUTO:
9153           if (cxx_dialect == cxx98) 
9154             {
9155               /* Consume the token.  */
9156               cp_lexer_consume_token (parser->lexer);
9157
9158               /* Complain about `auto' as a storage specifier, if
9159                  we're complaining about C++0x compatibility.  */
9160               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9161                           " will change meaning in C++0x; please remove it");
9162
9163               /* Set the storage class anyway.  */
9164               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9165                                            token->location);
9166             }
9167           else
9168             /* C++0x auto type-specifier.  */
9169             found_decl_spec = false;
9170           break;
9171
9172         case RID_REGISTER:
9173         case RID_STATIC:
9174         case RID_EXTERN:
9175         case RID_MUTABLE:
9176           /* Consume the token.  */
9177           cp_lexer_consume_token (parser->lexer);
9178           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9179                                        token->location);
9180           break;
9181         case RID_THREAD:
9182           /* Consume the token.  */
9183           cp_lexer_consume_token (parser->lexer);
9184           ++decl_specs->specs[(int) ds_thread];
9185           break;
9186
9187         default:
9188           /* We did not yet find a decl-specifier yet.  */
9189           found_decl_spec = false;
9190           break;
9191         }
9192
9193       /* Constructors are a special case.  The `S' in `S()' is not a
9194          decl-specifier; it is the beginning of the declarator.  */
9195       constructor_p
9196         = (!found_decl_spec
9197            && constructor_possible_p
9198            && (cp_parser_constructor_declarator_p
9199                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9200
9201       /* If we don't have a DECL_SPEC yet, then we must be looking at
9202          a type-specifier.  */
9203       if (!found_decl_spec && !constructor_p)
9204         {
9205           int decl_spec_declares_class_or_enum;
9206           bool is_cv_qualifier;
9207           tree type_spec;
9208
9209           type_spec
9210             = cp_parser_type_specifier (parser, flags,
9211                                         decl_specs,
9212                                         /*is_declaration=*/true,
9213                                         &decl_spec_declares_class_or_enum,
9214                                         &is_cv_qualifier);
9215           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9216
9217           /* If this type-specifier referenced a user-defined type
9218              (a typedef, class-name, etc.), then we can't allow any
9219              more such type-specifiers henceforth.
9220
9221              [dcl.spec]
9222
9223              The longest sequence of decl-specifiers that could
9224              possibly be a type name is taken as the
9225              decl-specifier-seq of a declaration.  The sequence shall
9226              be self-consistent as described below.
9227
9228              [dcl.type]
9229
9230              As a general rule, at most one type-specifier is allowed
9231              in the complete decl-specifier-seq of a declaration.  The
9232              only exceptions are the following:
9233
9234              -- const or volatile can be combined with any other
9235                 type-specifier.
9236
9237              -- signed or unsigned can be combined with char, long,
9238                 short, or int.
9239
9240              -- ..
9241
9242              Example:
9243
9244                typedef char* Pc;
9245                void g (const int Pc);
9246
9247              Here, Pc is *not* part of the decl-specifier seq; it's
9248              the declarator.  Therefore, once we see a type-specifier
9249              (other than a cv-qualifier), we forbid any additional
9250              user-defined types.  We *do* still allow things like `int
9251              int' to be considered a decl-specifier-seq, and issue the
9252              error message later.  */
9253           if (type_spec && !is_cv_qualifier)
9254             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9255           /* A constructor declarator cannot follow a type-specifier.  */
9256           if (type_spec)
9257             {
9258               constructor_possible_p = false;
9259               found_decl_spec = true;
9260               if (!is_cv_qualifier)
9261                 decl_specs->any_type_specifiers_p = true;
9262             }
9263         }
9264
9265       /* If we still do not have a DECL_SPEC, then there are no more
9266          decl-specifiers.  */
9267       if (!found_decl_spec)
9268         break;
9269
9270       decl_specs->any_specifiers_p = true;
9271       /* After we see one decl-specifier, further decl-specifiers are
9272          always optional.  */
9273       flags |= CP_PARSER_FLAGS_OPTIONAL;
9274     }
9275
9276   cp_parser_check_decl_spec (decl_specs, start_token->location);
9277
9278   /* Don't allow a friend specifier with a class definition.  */
9279   if (decl_specs->specs[(int) ds_friend] != 0
9280       && (*declares_class_or_enum & 2))
9281     error_at (start_token->location,
9282               "class definition may not be declared a friend");
9283 }
9284
9285 /* Parse an (optional) storage-class-specifier.
9286
9287    storage-class-specifier:
9288      auto
9289      register
9290      static
9291      extern
9292      mutable
9293
9294    GNU Extension:
9295
9296    storage-class-specifier:
9297      thread
9298
9299    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9300
9301 static tree
9302 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9303 {
9304   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9305     {
9306     case RID_AUTO:
9307       if (cxx_dialect != cxx98)
9308         return NULL_TREE;
9309       /* Fall through for C++98.  */
9310
9311     case RID_REGISTER:
9312     case RID_STATIC:
9313     case RID_EXTERN:
9314     case RID_MUTABLE:
9315     case RID_THREAD:
9316       /* Consume the token.  */
9317       return cp_lexer_consume_token (parser->lexer)->u.value;
9318
9319     default:
9320       return NULL_TREE;
9321     }
9322 }
9323
9324 /* Parse an (optional) function-specifier.
9325
9326    function-specifier:
9327      inline
9328      virtual
9329      explicit
9330
9331    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9332    Updates DECL_SPECS, if it is non-NULL.  */
9333
9334 static tree
9335 cp_parser_function_specifier_opt (cp_parser* parser,
9336                                   cp_decl_specifier_seq *decl_specs)
9337 {
9338   cp_token *token = cp_lexer_peek_token (parser->lexer);
9339   switch (token->keyword)
9340     {
9341     case RID_INLINE:
9342       if (decl_specs)
9343         ++decl_specs->specs[(int) ds_inline];
9344       break;
9345
9346     case RID_VIRTUAL:
9347       /* 14.5.2.3 [temp.mem]
9348
9349          A member function template shall not be virtual.  */
9350       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9351         error_at (token->location, "templates may not be %<virtual%>");
9352       else if (decl_specs)
9353         ++decl_specs->specs[(int) ds_virtual];
9354       break;
9355
9356     case RID_EXPLICIT:
9357       if (decl_specs)
9358         ++decl_specs->specs[(int) ds_explicit];
9359       break;
9360
9361     default:
9362       return NULL_TREE;
9363     }
9364
9365   /* Consume the token.  */
9366   return cp_lexer_consume_token (parser->lexer)->u.value;
9367 }
9368
9369 /* Parse a linkage-specification.
9370
9371    linkage-specification:
9372      extern string-literal { declaration-seq [opt] }
9373      extern string-literal declaration  */
9374
9375 static void
9376 cp_parser_linkage_specification (cp_parser* parser)
9377 {
9378   tree linkage;
9379
9380   /* Look for the `extern' keyword.  */
9381   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9382
9383   /* Look for the string-literal.  */
9384   linkage = cp_parser_string_literal (parser, false, false);
9385
9386   /* Transform the literal into an identifier.  If the literal is a
9387      wide-character string, or contains embedded NULs, then we can't
9388      handle it as the user wants.  */
9389   if (strlen (TREE_STRING_POINTER (linkage))
9390       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9391     {
9392       cp_parser_error (parser, "invalid linkage-specification");
9393       /* Assume C++ linkage.  */
9394       linkage = lang_name_cplusplus;
9395     }
9396   else
9397     linkage = get_identifier (TREE_STRING_POINTER (linkage));
9398
9399   /* We're now using the new linkage.  */
9400   push_lang_context (linkage);
9401
9402   /* If the next token is a `{', then we're using the first
9403      production.  */
9404   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9405     {
9406       /* Consume the `{' token.  */
9407       cp_lexer_consume_token (parser->lexer);
9408       /* Parse the declarations.  */
9409       cp_parser_declaration_seq_opt (parser);
9410       /* Look for the closing `}'.  */
9411       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9412     }
9413   /* Otherwise, there's just one declaration.  */
9414   else
9415     {
9416       bool saved_in_unbraced_linkage_specification_p;
9417
9418       saved_in_unbraced_linkage_specification_p
9419         = parser->in_unbraced_linkage_specification_p;
9420       parser->in_unbraced_linkage_specification_p = true;
9421       cp_parser_declaration (parser);
9422       parser->in_unbraced_linkage_specification_p
9423         = saved_in_unbraced_linkage_specification_p;
9424     }
9425
9426   /* We're done with the linkage-specification.  */
9427   pop_lang_context ();
9428 }
9429
9430 /* Parse a static_assert-declaration.
9431
9432    static_assert-declaration:
9433      static_assert ( constant-expression , string-literal ) ; 
9434
9435    If MEMBER_P, this static_assert is a class member.  */
9436
9437 static void 
9438 cp_parser_static_assert(cp_parser *parser, bool member_p)
9439 {
9440   tree condition;
9441   tree message;
9442   cp_token *token;
9443   location_t saved_loc;
9444
9445   /* Peek at the `static_assert' token so we can keep track of exactly
9446      where the static assertion started.  */
9447   token = cp_lexer_peek_token (parser->lexer);
9448   saved_loc = token->location;
9449
9450   /* Look for the `static_assert' keyword.  */
9451   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
9452                                   "%<static_assert%>"))
9453     return;
9454
9455   /*  We know we are in a static assertion; commit to any tentative
9456       parse.  */
9457   if (cp_parser_parsing_tentatively (parser))
9458     cp_parser_commit_to_tentative_parse (parser);
9459
9460   /* Parse the `(' starting the static assertion condition.  */
9461   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9462
9463   /* Parse the constant-expression.  */
9464   condition = 
9465     cp_parser_constant_expression (parser,
9466                                    /*allow_non_constant_p=*/false,
9467                                    /*non_constant_p=*/NULL);
9468
9469   /* Parse the separating `,'.  */
9470   cp_parser_require (parser, CPP_COMMA, "%<,%>");
9471
9472   /* Parse the string-literal message.  */
9473   message = cp_parser_string_literal (parser, 
9474                                       /*translate=*/false,
9475                                       /*wide_ok=*/true);
9476
9477   /* A `)' completes the static assertion.  */
9478   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9479     cp_parser_skip_to_closing_parenthesis (parser, 
9480                                            /*recovering=*/true, 
9481                                            /*or_comma=*/false,
9482                                            /*consume_paren=*/true);
9483
9484   /* A semicolon terminates the declaration.  */
9485   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9486
9487   /* Complete the static assertion, which may mean either processing 
9488      the static assert now or saving it for template instantiation.  */
9489   finish_static_assert (condition, message, saved_loc, member_p);
9490 }
9491
9492 /* Parse a `decltype' type. Returns the type. 
9493
9494    simple-type-specifier:
9495      decltype ( expression )  */
9496
9497 static tree
9498 cp_parser_decltype (cp_parser *parser)
9499 {
9500   tree expr;
9501   bool id_expression_or_member_access_p = false;
9502   const char *saved_message;
9503   bool saved_integral_constant_expression_p;
9504   bool saved_non_integral_constant_expression_p;
9505   cp_token *id_expr_start_token;
9506
9507   /* Look for the `decltype' token.  */
9508   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9509     return error_mark_node;
9510
9511   /* Types cannot be defined in a `decltype' expression.  Save away the
9512      old message.  */
9513   saved_message = parser->type_definition_forbidden_message;
9514
9515   /* And create the new one.  */
9516   parser->type_definition_forbidden_message
9517     = G_("types may not be defined in %<decltype%> expressions");
9518
9519   /* The restrictions on constant-expressions do not apply inside
9520      decltype expressions.  */
9521   saved_integral_constant_expression_p
9522     = parser->integral_constant_expression_p;
9523   saved_non_integral_constant_expression_p
9524     = parser->non_integral_constant_expression_p;
9525   parser->integral_constant_expression_p = false;
9526
9527   /* Do not actually evaluate the expression.  */
9528   ++cp_unevaluated_operand;
9529
9530   /* Do not warn about problems with the expression.  */
9531   ++c_inhibit_evaluation_warnings;
9532
9533   /* Parse the opening `('.  */
9534   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9535     return error_mark_node;
9536   
9537   /* First, try parsing an id-expression.  */
9538   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9539   cp_parser_parse_tentatively (parser);
9540   expr = cp_parser_id_expression (parser,
9541                                   /*template_keyword_p=*/false,
9542                                   /*check_dependency_p=*/true,
9543                                   /*template_p=*/NULL,
9544                                   /*declarator_p=*/false,
9545                                   /*optional_p=*/false);
9546
9547   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9548     {
9549       bool non_integral_constant_expression_p = false;
9550       tree id_expression = expr;
9551       cp_id_kind idk;
9552       const char *error_msg;
9553
9554       if (TREE_CODE (expr) == IDENTIFIER_NODE)
9555         /* Lookup the name we got back from the id-expression.  */
9556         expr = cp_parser_lookup_name (parser, expr,
9557                                       none_type,
9558                                       /*is_template=*/false,
9559                                       /*is_namespace=*/false,
9560                                       /*check_dependency=*/true,
9561                                       /*ambiguous_decls=*/NULL,
9562                                       id_expr_start_token->location);
9563
9564       if (expr
9565           && expr != error_mark_node
9566           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9567           && TREE_CODE (expr) != TYPE_DECL
9568           && (TREE_CODE (expr) != BIT_NOT_EXPR
9569               || !TYPE_P (TREE_OPERAND (expr, 0)))
9570           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9571         {
9572           /* Complete lookup of the id-expression.  */
9573           expr = (finish_id_expression
9574                   (id_expression, expr, parser->scope, &idk,
9575                    /*integral_constant_expression_p=*/false,
9576                    /*allow_non_integral_constant_expression_p=*/true,
9577                    &non_integral_constant_expression_p,
9578                    /*template_p=*/false,
9579                    /*done=*/true,
9580                    /*address_p=*/false,
9581                    /*template_arg_p=*/false,
9582                    &error_msg,
9583                    id_expr_start_token->location));
9584
9585           if (expr == error_mark_node)
9586             /* We found an id-expression, but it was something that we
9587                should not have found. This is an error, not something
9588                we can recover from, so note that we found an
9589                id-expression and we'll recover as gracefully as
9590                possible.  */
9591             id_expression_or_member_access_p = true;
9592         }
9593
9594       if (expr 
9595           && expr != error_mark_node
9596           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9597         /* We have an id-expression.  */
9598         id_expression_or_member_access_p = true;
9599     }
9600
9601   if (!id_expression_or_member_access_p)
9602     {
9603       /* Abort the id-expression parse.  */
9604       cp_parser_abort_tentative_parse (parser);
9605
9606       /* Parsing tentatively, again.  */
9607       cp_parser_parse_tentatively (parser);
9608
9609       /* Parse a class member access.  */
9610       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9611                                            /*cast_p=*/false,
9612                                            /*member_access_only_p=*/true, NULL);
9613
9614       if (expr 
9615           && expr != error_mark_node
9616           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9617         /* We have an id-expression.  */
9618         id_expression_or_member_access_p = true;
9619     }
9620
9621   if (id_expression_or_member_access_p)
9622     /* We have parsed the complete id-expression or member access.  */
9623     cp_parser_parse_definitely (parser);
9624   else
9625     {
9626       bool saved_greater_than_is_operator_p;
9627
9628       /* Abort our attempt to parse an id-expression or member access
9629          expression.  */
9630       cp_parser_abort_tentative_parse (parser);
9631
9632       /* Within a parenthesized expression, a `>' token is always
9633          the greater-than operator.  */
9634       saved_greater_than_is_operator_p
9635         = parser->greater_than_is_operator_p;
9636       parser->greater_than_is_operator_p = true;
9637
9638       /* Parse a full expression.  */
9639       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9640
9641       /* The `>' token might be the end of a template-id or
9642          template-parameter-list now.  */
9643       parser->greater_than_is_operator_p
9644         = saved_greater_than_is_operator_p;
9645     }
9646
9647   /* Go back to evaluating expressions.  */
9648   --cp_unevaluated_operand;
9649   --c_inhibit_evaluation_warnings;
9650
9651   /* Restore the old message and the integral constant expression
9652      flags.  */
9653   parser->type_definition_forbidden_message = saved_message;
9654   parser->integral_constant_expression_p
9655     = saved_integral_constant_expression_p;
9656   parser->non_integral_constant_expression_p
9657     = saved_non_integral_constant_expression_p;
9658
9659   if (expr == error_mark_node)
9660     {
9661       /* Skip everything up to the closing `)'.  */
9662       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9663                                              /*consume_paren=*/true);
9664       return error_mark_node;
9665     }
9666   
9667   /* Parse to the closing `)'.  */
9668   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9669     {
9670       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9671                                              /*consume_paren=*/true);
9672       return error_mark_node;
9673     }
9674
9675   return finish_decltype_type (expr, id_expression_or_member_access_p);
9676 }
9677
9678 /* Special member functions [gram.special] */
9679
9680 /* Parse a conversion-function-id.
9681
9682    conversion-function-id:
9683      operator conversion-type-id
9684
9685    Returns an IDENTIFIER_NODE representing the operator.  */
9686
9687 static tree
9688 cp_parser_conversion_function_id (cp_parser* parser)
9689 {
9690   tree type;
9691   tree saved_scope;
9692   tree saved_qualifying_scope;
9693   tree saved_object_scope;
9694   tree pushed_scope = NULL_TREE;
9695
9696   /* Look for the `operator' token.  */
9697   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9698     return error_mark_node;
9699   /* When we parse the conversion-type-id, the current scope will be
9700      reset.  However, we need that information in able to look up the
9701      conversion function later, so we save it here.  */
9702   saved_scope = parser->scope;
9703   saved_qualifying_scope = parser->qualifying_scope;
9704   saved_object_scope = parser->object_scope;
9705   /* We must enter the scope of the class so that the names of
9706      entities declared within the class are available in the
9707      conversion-type-id.  For example, consider:
9708
9709        struct S {
9710          typedef int I;
9711          operator I();
9712        };
9713
9714        S::operator I() { ... }
9715
9716      In order to see that `I' is a type-name in the definition, we
9717      must be in the scope of `S'.  */
9718   if (saved_scope)
9719     pushed_scope = push_scope (saved_scope);
9720   /* Parse the conversion-type-id.  */
9721   type = cp_parser_conversion_type_id (parser);
9722   /* Leave the scope of the class, if any.  */
9723   if (pushed_scope)
9724     pop_scope (pushed_scope);
9725   /* Restore the saved scope.  */
9726   parser->scope = saved_scope;
9727   parser->qualifying_scope = saved_qualifying_scope;
9728   parser->object_scope = saved_object_scope;
9729   /* If the TYPE is invalid, indicate failure.  */
9730   if (type == error_mark_node)
9731     return error_mark_node;
9732   return mangle_conv_op_name_for_type (type);
9733 }
9734
9735 /* Parse a conversion-type-id:
9736
9737    conversion-type-id:
9738      type-specifier-seq conversion-declarator [opt]
9739
9740    Returns the TYPE specified.  */
9741
9742 static tree
9743 cp_parser_conversion_type_id (cp_parser* parser)
9744 {
9745   tree attributes;
9746   cp_decl_specifier_seq type_specifiers;
9747   cp_declarator *declarator;
9748   tree type_specified;
9749
9750   /* Parse the attributes.  */
9751   attributes = cp_parser_attributes_opt (parser);
9752   /* Parse the type-specifiers.  */
9753   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9754                                 /*is_trailing_return=*/false,
9755                                 &type_specifiers);
9756   /* If that didn't work, stop.  */
9757   if (type_specifiers.type == error_mark_node)
9758     return error_mark_node;
9759   /* Parse the conversion-declarator.  */
9760   declarator = cp_parser_conversion_declarator_opt (parser);
9761
9762   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9763                                     /*initialized=*/0, &attributes);
9764   if (attributes)
9765     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9766
9767   /* Don't give this error when parsing tentatively.  This happens to
9768      work because we always parse this definitively once.  */
9769   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9770       && type_uses_auto (type_specified))
9771     {
9772       error ("invalid use of %<auto%> in conversion operator");
9773       return error_mark_node;
9774     }
9775
9776   return type_specified;
9777 }
9778
9779 /* Parse an (optional) conversion-declarator.
9780
9781    conversion-declarator:
9782      ptr-operator conversion-declarator [opt]
9783
9784    */
9785
9786 static cp_declarator *
9787 cp_parser_conversion_declarator_opt (cp_parser* parser)
9788 {
9789   enum tree_code code;
9790   tree class_type;
9791   cp_cv_quals cv_quals;
9792
9793   /* We don't know if there's a ptr-operator next, or not.  */
9794   cp_parser_parse_tentatively (parser);
9795   /* Try the ptr-operator.  */
9796   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9797   /* If it worked, look for more conversion-declarators.  */
9798   if (cp_parser_parse_definitely (parser))
9799     {
9800       cp_declarator *declarator;
9801
9802       /* Parse another optional declarator.  */
9803       declarator = cp_parser_conversion_declarator_opt (parser);
9804
9805       return cp_parser_make_indirect_declarator
9806         (code, class_type, cv_quals, declarator);
9807    }
9808
9809   return NULL;
9810 }
9811
9812 /* Parse an (optional) ctor-initializer.
9813
9814    ctor-initializer:
9815      : mem-initializer-list
9816
9817    Returns TRUE iff the ctor-initializer was actually present.  */
9818
9819 static bool
9820 cp_parser_ctor_initializer_opt (cp_parser* parser)
9821 {
9822   /* If the next token is not a `:', then there is no
9823      ctor-initializer.  */
9824   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9825     {
9826       /* Do default initialization of any bases and members.  */
9827       if (DECL_CONSTRUCTOR_P (current_function_decl))
9828         finish_mem_initializers (NULL_TREE);
9829
9830       return false;
9831     }
9832
9833   /* Consume the `:' token.  */
9834   cp_lexer_consume_token (parser->lexer);
9835   /* And the mem-initializer-list.  */
9836   cp_parser_mem_initializer_list (parser);
9837
9838   return true;
9839 }
9840
9841 /* Parse a mem-initializer-list.
9842
9843    mem-initializer-list:
9844      mem-initializer ... [opt]
9845      mem-initializer ... [opt] , mem-initializer-list  */
9846
9847 static void
9848 cp_parser_mem_initializer_list (cp_parser* parser)
9849 {
9850   tree mem_initializer_list = NULL_TREE;
9851   cp_token *token = cp_lexer_peek_token (parser->lexer);
9852
9853   /* Let the semantic analysis code know that we are starting the
9854      mem-initializer-list.  */
9855   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9856     error_at (token->location,
9857               "only constructors take base initializers");
9858
9859   /* Loop through the list.  */
9860   while (true)
9861     {
9862       tree mem_initializer;
9863
9864       token = cp_lexer_peek_token (parser->lexer);
9865       /* Parse the mem-initializer.  */
9866       mem_initializer = cp_parser_mem_initializer (parser);
9867       /* If the next token is a `...', we're expanding member initializers. */
9868       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9869         {
9870           /* Consume the `...'. */
9871           cp_lexer_consume_token (parser->lexer);
9872
9873           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9874              can be expanded but members cannot. */
9875           if (mem_initializer != error_mark_node
9876               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9877             {
9878               error_at (token->location,
9879                         "cannot expand initializer for member %<%D%>",
9880                         TREE_PURPOSE (mem_initializer));
9881               mem_initializer = error_mark_node;
9882             }
9883
9884           /* Construct the pack expansion type. */
9885           if (mem_initializer != error_mark_node)
9886             mem_initializer = make_pack_expansion (mem_initializer);
9887         }
9888       /* Add it to the list, unless it was erroneous.  */
9889       if (mem_initializer != error_mark_node)
9890         {
9891           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9892           mem_initializer_list = mem_initializer;
9893         }
9894       /* If the next token is not a `,', we're done.  */
9895       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9896         break;
9897       /* Consume the `,' token.  */
9898       cp_lexer_consume_token (parser->lexer);
9899     }
9900
9901   /* Perform semantic analysis.  */
9902   if (DECL_CONSTRUCTOR_P (current_function_decl))
9903     finish_mem_initializers (mem_initializer_list);
9904 }
9905
9906 /* Parse a mem-initializer.
9907
9908    mem-initializer:
9909      mem-initializer-id ( expression-list [opt] )
9910      mem-initializer-id braced-init-list
9911
9912    GNU extension:
9913
9914    mem-initializer:
9915      ( expression-list [opt] )
9916
9917    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9918    class) or FIELD_DECL (for a non-static data member) to initialize;
9919    the TREE_VALUE is the expression-list.  An empty initialization
9920    list is represented by void_list_node.  */
9921
9922 static tree
9923 cp_parser_mem_initializer (cp_parser* parser)
9924 {
9925   tree mem_initializer_id;
9926   tree expression_list;
9927   tree member;
9928   cp_token *token = cp_lexer_peek_token (parser->lexer);
9929
9930   /* Find out what is being initialized.  */
9931   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9932     {
9933       permerror (token->location,
9934                  "anachronistic old-style base class initializer");
9935       mem_initializer_id = NULL_TREE;
9936     }
9937   else
9938     {
9939       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9940       if (mem_initializer_id == error_mark_node)
9941         return mem_initializer_id;
9942     }
9943   member = expand_member_init (mem_initializer_id);
9944   if (member && !DECL_P (member))
9945     in_base_initializer = 1;
9946
9947   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9948     {
9949       bool expr_non_constant_p;
9950       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9951       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9952       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9953       expression_list = build_tree_list (NULL_TREE, expression_list);
9954     }
9955   else
9956     {
9957       VEC(tree,gc)* vec;
9958       vec = cp_parser_parenthesized_expression_list (parser, false,
9959                                                      /*cast_p=*/false,
9960                                                      /*allow_expansion_p=*/true,
9961                                                      /*non_constant_p=*/NULL);
9962       if (vec == NULL)
9963         return error_mark_node;
9964       expression_list = build_tree_list_vec (vec);
9965       release_tree_vector (vec);
9966     }
9967
9968   if (expression_list == error_mark_node)
9969     return error_mark_node;
9970   if (!expression_list)
9971     expression_list = void_type_node;
9972
9973   in_base_initializer = 0;
9974
9975   return member ? build_tree_list (member, expression_list) : error_mark_node;
9976 }
9977
9978 /* Parse a mem-initializer-id.
9979
9980    mem-initializer-id:
9981      :: [opt] nested-name-specifier [opt] class-name
9982      identifier
9983
9984    Returns a TYPE indicating the class to be initializer for the first
9985    production.  Returns an IDENTIFIER_NODE indicating the data member
9986    to be initialized for the second production.  */
9987
9988 static tree
9989 cp_parser_mem_initializer_id (cp_parser* parser)
9990 {
9991   bool global_scope_p;
9992   bool nested_name_specifier_p;
9993   bool template_p = false;
9994   tree id;
9995
9996   cp_token *token = cp_lexer_peek_token (parser->lexer);
9997
9998   /* `typename' is not allowed in this context ([temp.res]).  */
9999   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10000     {
10001       error_at (token->location, 
10002                 "keyword %<typename%> not allowed in this context (a qualified "
10003                 "member initializer is implicitly a type)");
10004       cp_lexer_consume_token (parser->lexer);
10005     }
10006   /* Look for the optional `::' operator.  */
10007   global_scope_p
10008     = (cp_parser_global_scope_opt (parser,
10009                                    /*current_scope_valid_p=*/false)
10010        != NULL_TREE);
10011   /* Look for the optional nested-name-specifier.  The simplest way to
10012      implement:
10013
10014        [temp.res]
10015
10016        The keyword `typename' is not permitted in a base-specifier or
10017        mem-initializer; in these contexts a qualified name that
10018        depends on a template-parameter is implicitly assumed to be a
10019        type name.
10020
10021      is to assume that we have seen the `typename' keyword at this
10022      point.  */
10023   nested_name_specifier_p
10024     = (cp_parser_nested_name_specifier_opt (parser,
10025                                             /*typename_keyword_p=*/true,
10026                                             /*check_dependency_p=*/true,
10027                                             /*type_p=*/true,
10028                                             /*is_declaration=*/true)
10029        != NULL_TREE);
10030   if (nested_name_specifier_p)
10031     template_p = cp_parser_optional_template_keyword (parser);
10032   /* If there is a `::' operator or a nested-name-specifier, then we
10033      are definitely looking for a class-name.  */
10034   if (global_scope_p || nested_name_specifier_p)
10035     return cp_parser_class_name (parser,
10036                                  /*typename_keyword_p=*/true,
10037                                  /*template_keyword_p=*/template_p,
10038                                  typename_type,
10039                                  /*check_dependency_p=*/true,
10040                                  /*class_head_p=*/false,
10041                                  /*is_declaration=*/true);
10042   /* Otherwise, we could also be looking for an ordinary identifier.  */
10043   cp_parser_parse_tentatively (parser);
10044   /* Try a class-name.  */
10045   id = cp_parser_class_name (parser,
10046                              /*typename_keyword_p=*/true,
10047                              /*template_keyword_p=*/false,
10048                              none_type,
10049                              /*check_dependency_p=*/true,
10050                              /*class_head_p=*/false,
10051                              /*is_declaration=*/true);
10052   /* If we found one, we're done.  */
10053   if (cp_parser_parse_definitely (parser))
10054     return id;
10055   /* Otherwise, look for an ordinary identifier.  */
10056   return cp_parser_identifier (parser);
10057 }
10058
10059 /* Overloading [gram.over] */
10060
10061 /* Parse an operator-function-id.
10062
10063    operator-function-id:
10064      operator operator
10065
10066    Returns an IDENTIFIER_NODE for the operator which is a
10067    human-readable spelling of the identifier, e.g., `operator +'.  */
10068
10069 static tree
10070 cp_parser_operator_function_id (cp_parser* parser)
10071 {
10072   /* Look for the `operator' keyword.  */
10073   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10074     return error_mark_node;
10075   /* And then the name of the operator itself.  */
10076   return cp_parser_operator (parser);
10077 }
10078
10079 /* Parse an operator.
10080
10081    operator:
10082      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10083      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10084      || ++ -- , ->* -> () []
10085
10086    GNU Extensions:
10087
10088    operator:
10089      <? >? <?= >?=
10090
10091    Returns an IDENTIFIER_NODE for the operator which is a
10092    human-readable spelling of the identifier, e.g., `operator +'.  */
10093
10094 static tree
10095 cp_parser_operator (cp_parser* parser)
10096 {
10097   tree id = NULL_TREE;
10098   cp_token *token;
10099
10100   /* Peek at the next token.  */
10101   token = cp_lexer_peek_token (parser->lexer);
10102   /* Figure out which operator we have.  */
10103   switch (token->type)
10104     {
10105     case CPP_KEYWORD:
10106       {
10107         enum tree_code op;
10108
10109         /* The keyword should be either `new' or `delete'.  */
10110         if (token->keyword == RID_NEW)
10111           op = NEW_EXPR;
10112         else if (token->keyword == RID_DELETE)
10113           op = DELETE_EXPR;
10114         else
10115           break;
10116
10117         /* Consume the `new' or `delete' token.  */
10118         cp_lexer_consume_token (parser->lexer);
10119
10120         /* Peek at the next token.  */
10121         token = cp_lexer_peek_token (parser->lexer);
10122         /* If it's a `[' token then this is the array variant of the
10123            operator.  */
10124         if (token->type == CPP_OPEN_SQUARE)
10125           {
10126             /* Consume the `[' token.  */
10127             cp_lexer_consume_token (parser->lexer);
10128             /* Look for the `]' token.  */
10129             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10130             id = ansi_opname (op == NEW_EXPR
10131                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10132           }
10133         /* Otherwise, we have the non-array variant.  */
10134         else
10135           id = ansi_opname (op);
10136
10137         return id;
10138       }
10139
10140     case CPP_PLUS:
10141       id = ansi_opname (PLUS_EXPR);
10142       break;
10143
10144     case CPP_MINUS:
10145       id = ansi_opname (MINUS_EXPR);
10146       break;
10147
10148     case CPP_MULT:
10149       id = ansi_opname (MULT_EXPR);
10150       break;
10151
10152     case CPP_DIV:
10153       id = ansi_opname (TRUNC_DIV_EXPR);
10154       break;
10155
10156     case CPP_MOD:
10157       id = ansi_opname (TRUNC_MOD_EXPR);
10158       break;
10159
10160     case CPP_XOR:
10161       id = ansi_opname (BIT_XOR_EXPR);
10162       break;
10163
10164     case CPP_AND:
10165       id = ansi_opname (BIT_AND_EXPR);
10166       break;
10167
10168     case CPP_OR:
10169       id = ansi_opname (BIT_IOR_EXPR);
10170       break;
10171
10172     case CPP_COMPL:
10173       id = ansi_opname (BIT_NOT_EXPR);
10174       break;
10175
10176     case CPP_NOT:
10177       id = ansi_opname (TRUTH_NOT_EXPR);
10178       break;
10179
10180     case CPP_EQ:
10181       id = ansi_assopname (NOP_EXPR);
10182       break;
10183
10184     case CPP_LESS:
10185       id = ansi_opname (LT_EXPR);
10186       break;
10187
10188     case CPP_GREATER:
10189       id = ansi_opname (GT_EXPR);
10190       break;
10191
10192     case CPP_PLUS_EQ:
10193       id = ansi_assopname (PLUS_EXPR);
10194       break;
10195
10196     case CPP_MINUS_EQ:
10197       id = ansi_assopname (MINUS_EXPR);
10198       break;
10199
10200     case CPP_MULT_EQ:
10201       id = ansi_assopname (MULT_EXPR);
10202       break;
10203
10204     case CPP_DIV_EQ:
10205       id = ansi_assopname (TRUNC_DIV_EXPR);
10206       break;
10207
10208     case CPP_MOD_EQ:
10209       id = ansi_assopname (TRUNC_MOD_EXPR);
10210       break;
10211
10212     case CPP_XOR_EQ:
10213       id = ansi_assopname (BIT_XOR_EXPR);
10214       break;
10215
10216     case CPP_AND_EQ:
10217       id = ansi_assopname (BIT_AND_EXPR);
10218       break;
10219
10220     case CPP_OR_EQ:
10221       id = ansi_assopname (BIT_IOR_EXPR);
10222       break;
10223
10224     case CPP_LSHIFT:
10225       id = ansi_opname (LSHIFT_EXPR);
10226       break;
10227
10228     case CPP_RSHIFT:
10229       id = ansi_opname (RSHIFT_EXPR);
10230       break;
10231
10232     case CPP_LSHIFT_EQ:
10233       id = ansi_assopname (LSHIFT_EXPR);
10234       break;
10235
10236     case CPP_RSHIFT_EQ:
10237       id = ansi_assopname (RSHIFT_EXPR);
10238       break;
10239
10240     case CPP_EQ_EQ:
10241       id = ansi_opname (EQ_EXPR);
10242       break;
10243
10244     case CPP_NOT_EQ:
10245       id = ansi_opname (NE_EXPR);
10246       break;
10247
10248     case CPP_LESS_EQ:
10249       id = ansi_opname (LE_EXPR);
10250       break;
10251
10252     case CPP_GREATER_EQ:
10253       id = ansi_opname (GE_EXPR);
10254       break;
10255
10256     case CPP_AND_AND:
10257       id = ansi_opname (TRUTH_ANDIF_EXPR);
10258       break;
10259
10260     case CPP_OR_OR:
10261       id = ansi_opname (TRUTH_ORIF_EXPR);
10262       break;
10263
10264     case CPP_PLUS_PLUS:
10265       id = ansi_opname (POSTINCREMENT_EXPR);
10266       break;
10267
10268     case CPP_MINUS_MINUS:
10269       id = ansi_opname (PREDECREMENT_EXPR);
10270       break;
10271
10272     case CPP_COMMA:
10273       id = ansi_opname (COMPOUND_EXPR);
10274       break;
10275
10276     case CPP_DEREF_STAR:
10277       id = ansi_opname (MEMBER_REF);
10278       break;
10279
10280     case CPP_DEREF:
10281       id = ansi_opname (COMPONENT_REF);
10282       break;
10283
10284     case CPP_OPEN_PAREN:
10285       /* Consume the `('.  */
10286       cp_lexer_consume_token (parser->lexer);
10287       /* Look for the matching `)'.  */
10288       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10289       return ansi_opname (CALL_EXPR);
10290
10291     case CPP_OPEN_SQUARE:
10292       /* Consume the `['.  */
10293       cp_lexer_consume_token (parser->lexer);
10294       /* Look for the matching `]'.  */
10295       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10296       return ansi_opname (ARRAY_REF);
10297
10298     default:
10299       /* Anything else is an error.  */
10300       break;
10301     }
10302
10303   /* If we have selected an identifier, we need to consume the
10304      operator token.  */
10305   if (id)
10306     cp_lexer_consume_token (parser->lexer);
10307   /* Otherwise, no valid operator name was present.  */
10308   else
10309     {
10310       cp_parser_error (parser, "expected operator");
10311       id = error_mark_node;
10312     }
10313
10314   return id;
10315 }
10316
10317 /* Parse a template-declaration.
10318
10319    template-declaration:
10320      export [opt] template < template-parameter-list > declaration
10321
10322    If MEMBER_P is TRUE, this template-declaration occurs within a
10323    class-specifier.
10324
10325    The grammar rule given by the standard isn't correct.  What
10326    is really meant is:
10327
10328    template-declaration:
10329      export [opt] template-parameter-list-seq
10330        decl-specifier-seq [opt] init-declarator [opt] ;
10331      export [opt] template-parameter-list-seq
10332        function-definition
10333
10334    template-parameter-list-seq:
10335      template-parameter-list-seq [opt]
10336      template < template-parameter-list >  */
10337
10338 static void
10339 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10340 {
10341   /* Check for `export'.  */
10342   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10343     {
10344       /* Consume the `export' token.  */
10345       cp_lexer_consume_token (parser->lexer);
10346       /* Warn that we do not support `export'.  */
10347       warning (0, "keyword %<export%> not implemented, and will be ignored");
10348     }
10349
10350   cp_parser_template_declaration_after_export (parser, member_p);
10351 }
10352
10353 /* Parse a template-parameter-list.
10354
10355    template-parameter-list:
10356      template-parameter
10357      template-parameter-list , template-parameter
10358
10359    Returns a TREE_LIST.  Each node represents a template parameter.
10360    The nodes are connected via their TREE_CHAINs.  */
10361
10362 static tree
10363 cp_parser_template_parameter_list (cp_parser* parser)
10364 {
10365   tree parameter_list = NULL_TREE;
10366
10367   begin_template_parm_list ();
10368   while (true)
10369     {
10370       tree parameter;
10371       bool is_non_type;
10372       bool is_parameter_pack;
10373       location_t parm_loc;
10374
10375       /* Parse the template-parameter.  */
10376       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10377       parameter = cp_parser_template_parameter (parser, 
10378                                                 &is_non_type,
10379                                                 &is_parameter_pack);
10380       /* Add it to the list.  */
10381       if (parameter != error_mark_node)
10382         parameter_list = process_template_parm (parameter_list,
10383                                                 parm_loc,
10384                                                 parameter,
10385                                                 is_non_type,
10386                                                 is_parameter_pack);
10387       else
10388        {
10389          tree err_parm = build_tree_list (parameter, parameter);
10390          TREE_VALUE (err_parm) = error_mark_node;
10391          parameter_list = chainon (parameter_list, err_parm);
10392        }
10393
10394       /* If the next token is not a `,', we're done.  */
10395       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10396         break;
10397       /* Otherwise, consume the `,' token.  */
10398       cp_lexer_consume_token (parser->lexer);
10399     }
10400
10401   return end_template_parm_list (parameter_list);
10402 }
10403
10404 /* Parse a template-parameter.
10405
10406    template-parameter:
10407      type-parameter
10408      parameter-declaration
10409
10410    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
10411    the parameter.  The TREE_PURPOSE is the default value, if any.
10412    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
10413    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
10414    set to true iff this parameter is a parameter pack. */
10415
10416 static tree
10417 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10418                               bool *is_parameter_pack)
10419 {
10420   cp_token *token;
10421   cp_parameter_declarator *parameter_declarator;
10422   cp_declarator *id_declarator;
10423   tree parm;
10424
10425   /* Assume it is a type parameter or a template parameter.  */
10426   *is_non_type = false;
10427   /* Assume it not a parameter pack. */
10428   *is_parameter_pack = false;
10429   /* Peek at the next token.  */
10430   token = cp_lexer_peek_token (parser->lexer);
10431   /* If it is `class' or `template', we have a type-parameter.  */
10432   if (token->keyword == RID_TEMPLATE)
10433     return cp_parser_type_parameter (parser, is_parameter_pack);
10434   /* If it is `class' or `typename' we do not know yet whether it is a
10435      type parameter or a non-type parameter.  Consider:
10436
10437        template <typename T, typename T::X X> ...
10438
10439      or:
10440
10441        template <class C, class D*> ...
10442
10443      Here, the first parameter is a type parameter, and the second is
10444      a non-type parameter.  We can tell by looking at the token after
10445      the identifier -- if it is a `,', `=', or `>' then we have a type
10446      parameter.  */
10447   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10448     {
10449       /* Peek at the token after `class' or `typename'.  */
10450       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10451       /* If it's an ellipsis, we have a template type parameter
10452          pack. */
10453       if (token->type == CPP_ELLIPSIS)
10454         return cp_parser_type_parameter (parser, is_parameter_pack);
10455       /* If it's an identifier, skip it.  */
10456       if (token->type == CPP_NAME)
10457         token = cp_lexer_peek_nth_token (parser->lexer, 3);
10458       /* Now, see if the token looks like the end of a template
10459          parameter.  */
10460       if (token->type == CPP_COMMA
10461           || token->type == CPP_EQ
10462           || token->type == CPP_GREATER)
10463         return cp_parser_type_parameter (parser, is_parameter_pack);
10464     }
10465
10466   /* Otherwise, it is a non-type parameter.
10467
10468      [temp.param]
10469
10470      When parsing a default template-argument for a non-type
10471      template-parameter, the first non-nested `>' is taken as the end
10472      of the template parameter-list rather than a greater-than
10473      operator.  */
10474   *is_non_type = true;
10475   parameter_declarator
10476      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10477                                         /*parenthesized_p=*/NULL);
10478
10479   /* If the parameter declaration is marked as a parameter pack, set
10480      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10481      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10482      grokdeclarator. */
10483   if (parameter_declarator
10484       && parameter_declarator->declarator
10485       && parameter_declarator->declarator->parameter_pack_p)
10486     {
10487       *is_parameter_pack = true;
10488       parameter_declarator->declarator->parameter_pack_p = false;
10489     }
10490
10491   /* If the next token is an ellipsis, and we don't already have it
10492      marked as a parameter pack, then we have a parameter pack (that
10493      has no declarator).  */
10494   if (!*is_parameter_pack
10495       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10496       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10497     {
10498       /* Consume the `...'.  */
10499       cp_lexer_consume_token (parser->lexer);
10500       maybe_warn_variadic_templates ();
10501       
10502       *is_parameter_pack = true;
10503     }
10504   /* We might end up with a pack expansion as the type of the non-type
10505      template parameter, in which case this is a non-type template
10506      parameter pack.  */
10507   else if (parameter_declarator
10508            && parameter_declarator->decl_specifiers.type
10509            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10510     {
10511       *is_parameter_pack = true;
10512       parameter_declarator->decl_specifiers.type = 
10513         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10514     }
10515
10516   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10517     {
10518       /* Parameter packs cannot have default arguments.  However, a
10519          user may try to do so, so we'll parse them and give an
10520          appropriate diagnostic here.  */
10521
10522       /* Consume the `='.  */
10523       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10524       cp_lexer_consume_token (parser->lexer);
10525       
10526       /* Find the name of the parameter pack.  */     
10527       id_declarator = parameter_declarator->declarator;
10528       while (id_declarator && id_declarator->kind != cdk_id)
10529         id_declarator = id_declarator->declarator;
10530       
10531       if (id_declarator && id_declarator->kind == cdk_id)
10532         error_at (start_token->location,
10533                   "template parameter pack %qD cannot have a default argument",
10534                   id_declarator->u.id.unqualified_name);
10535       else
10536         error_at (start_token->location,
10537                   "template parameter pack cannot have a default argument");
10538       
10539       /* Parse the default argument, but throw away the result.  */
10540       cp_parser_default_argument (parser, /*template_parm_p=*/true);
10541     }
10542
10543   parm = grokdeclarator (parameter_declarator->declarator,
10544                          &parameter_declarator->decl_specifiers,
10545                          TPARM, /*initialized=*/0,
10546                          /*attrlist=*/NULL);
10547   if (parm == error_mark_node)
10548     return error_mark_node;
10549
10550   return build_tree_list (parameter_declarator->default_argument, parm);
10551 }
10552
10553 /* Parse a type-parameter.
10554
10555    type-parameter:
10556      class identifier [opt]
10557      class identifier [opt] = type-id
10558      typename identifier [opt]
10559      typename identifier [opt] = type-id
10560      template < template-parameter-list > class identifier [opt]
10561      template < template-parameter-list > class identifier [opt]
10562        = id-expression
10563
10564    GNU Extension (variadic templates):
10565
10566    type-parameter:
10567      class ... identifier [opt]
10568      typename ... identifier [opt]
10569
10570    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
10571    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
10572    the declaration of the parameter.
10573
10574    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10575
10576 static tree
10577 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10578 {
10579   cp_token *token;
10580   tree parameter;
10581
10582   /* Look for a keyword to tell us what kind of parameter this is.  */
10583   token = cp_parser_require (parser, CPP_KEYWORD,
10584                              "%<class%>, %<typename%>, or %<template%>");
10585   if (!token)
10586     return error_mark_node;
10587
10588   switch (token->keyword)
10589     {
10590     case RID_CLASS:
10591     case RID_TYPENAME:
10592       {
10593         tree identifier;
10594         tree default_argument;
10595
10596         /* If the next token is an ellipsis, we have a template
10597            argument pack. */
10598         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10599           {
10600             /* Consume the `...' token. */
10601             cp_lexer_consume_token (parser->lexer);
10602             maybe_warn_variadic_templates ();
10603
10604             *is_parameter_pack = true;
10605           }
10606
10607         /* If the next token is an identifier, then it names the
10608            parameter.  */
10609         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10610           identifier = cp_parser_identifier (parser);
10611         else
10612           identifier = NULL_TREE;
10613
10614         /* Create the parameter.  */
10615         parameter = finish_template_type_parm (class_type_node, identifier);
10616
10617         /* If the next token is an `=', we have a default argument.  */
10618         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10619           {
10620             /* Consume the `=' token.  */
10621             cp_lexer_consume_token (parser->lexer);
10622             /* Parse the default-argument.  */
10623             push_deferring_access_checks (dk_no_deferred);
10624             default_argument = cp_parser_type_id (parser);
10625
10626             /* Template parameter packs cannot have default
10627                arguments. */
10628             if (*is_parameter_pack)
10629               {
10630                 if (identifier)
10631                   error_at (token->location,
10632                             "template parameter pack %qD cannot have a "
10633                             "default argument", identifier);
10634                 else
10635                   error_at (token->location,
10636                             "template parameter packs cannot have "
10637                             "default arguments");
10638                 default_argument = NULL_TREE;
10639               }
10640             pop_deferring_access_checks ();
10641           }
10642         else
10643           default_argument = NULL_TREE;
10644
10645         /* Create the combined representation of the parameter and the
10646            default argument.  */
10647         parameter = build_tree_list (default_argument, parameter);
10648       }
10649       break;
10650
10651     case RID_TEMPLATE:
10652       {
10653         tree identifier;
10654         tree default_argument;
10655
10656         /* Look for the `<'.  */
10657         cp_parser_require (parser, CPP_LESS, "%<<%>");
10658         /* Parse the template-parameter-list.  */
10659         cp_parser_template_parameter_list (parser);
10660         /* Look for the `>'.  */
10661         cp_parser_require (parser, CPP_GREATER, "%<>%>");
10662         /* Look for the `class' keyword.  */
10663         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10664         /* If the next token is an ellipsis, we have a template
10665            argument pack. */
10666         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10667           {
10668             /* Consume the `...' token. */
10669             cp_lexer_consume_token (parser->lexer);
10670             maybe_warn_variadic_templates ();
10671
10672             *is_parameter_pack = true;
10673           }
10674         /* If the next token is an `=', then there is a
10675            default-argument.  If the next token is a `>', we are at
10676            the end of the parameter-list.  If the next token is a `,',
10677            then we are at the end of this parameter.  */
10678         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10679             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10680             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10681           {
10682             identifier = cp_parser_identifier (parser);
10683             /* Treat invalid names as if the parameter were nameless.  */
10684             if (identifier == error_mark_node)
10685               identifier = NULL_TREE;
10686           }
10687         else
10688           identifier = NULL_TREE;
10689
10690         /* Create the template parameter.  */
10691         parameter = finish_template_template_parm (class_type_node,
10692                                                    identifier);
10693
10694         /* If the next token is an `=', then there is a
10695            default-argument.  */
10696         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10697           {
10698             bool is_template;
10699
10700             /* Consume the `='.  */
10701             cp_lexer_consume_token (parser->lexer);
10702             /* Parse the id-expression.  */
10703             push_deferring_access_checks (dk_no_deferred);
10704             /* save token before parsing the id-expression, for error
10705                reporting */
10706             token = cp_lexer_peek_token (parser->lexer);
10707             default_argument
10708               = cp_parser_id_expression (parser,
10709                                          /*template_keyword_p=*/false,
10710                                          /*check_dependency_p=*/true,
10711                                          /*template_p=*/&is_template,
10712                                          /*declarator_p=*/false,
10713                                          /*optional_p=*/false);
10714             if (TREE_CODE (default_argument) == TYPE_DECL)
10715               /* If the id-expression was a template-id that refers to
10716                  a template-class, we already have the declaration here,
10717                  so no further lookup is needed.  */
10718                  ;
10719             else
10720               /* Look up the name.  */
10721               default_argument
10722                 = cp_parser_lookup_name (parser, default_argument,
10723                                          none_type,
10724                                          /*is_template=*/is_template,
10725                                          /*is_namespace=*/false,
10726                                          /*check_dependency=*/true,
10727                                          /*ambiguous_decls=*/NULL,
10728                                          token->location);
10729             /* See if the default argument is valid.  */
10730             default_argument
10731               = check_template_template_default_arg (default_argument);
10732
10733             /* Template parameter packs cannot have default
10734                arguments. */
10735             if (*is_parameter_pack)
10736               {
10737                 if (identifier)
10738                   error_at (token->location,
10739                             "template parameter pack %qD cannot "
10740                             "have a default argument",
10741                             identifier);
10742                 else
10743                   error_at (token->location, "template parameter packs cannot "
10744                             "have default arguments");
10745                 default_argument = NULL_TREE;
10746               }
10747             pop_deferring_access_checks ();
10748           }
10749         else
10750           default_argument = NULL_TREE;
10751
10752         /* Create the combined representation of the parameter and the
10753            default argument.  */
10754         parameter = build_tree_list (default_argument, parameter);
10755       }
10756       break;
10757
10758     default:
10759       gcc_unreachable ();
10760       break;
10761     }
10762
10763   return parameter;
10764 }
10765
10766 /* Parse a template-id.
10767
10768    template-id:
10769      template-name < template-argument-list [opt] >
10770
10771    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10772    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10773    returned.  Otherwise, if the template-name names a function, or set
10774    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10775    names a class, returns a TYPE_DECL for the specialization.
10776
10777    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10778    uninstantiated templates.  */
10779
10780 static tree
10781 cp_parser_template_id (cp_parser *parser,
10782                        bool template_keyword_p,
10783                        bool check_dependency_p,
10784                        bool is_declaration)
10785 {
10786   int i;
10787   tree templ;
10788   tree arguments;
10789   tree template_id;
10790   cp_token_position start_of_id = 0;
10791   deferred_access_check *chk;
10792   VEC (deferred_access_check,gc) *access_check;
10793   cp_token *next_token = NULL, *next_token_2 = NULL;
10794   bool is_identifier;
10795
10796   /* If the next token corresponds to a template-id, there is no need
10797      to reparse it.  */
10798   next_token = cp_lexer_peek_token (parser->lexer);
10799   if (next_token->type == CPP_TEMPLATE_ID)
10800     {
10801       struct tree_check *check_value;
10802
10803       /* Get the stored value.  */
10804       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10805       /* Perform any access checks that were deferred.  */
10806       access_check = check_value->checks;
10807       if (access_check)
10808         {
10809           for (i = 0 ;
10810                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10811                ++i)
10812             {
10813               perform_or_defer_access_check (chk->binfo,
10814                                              chk->decl,
10815                                              chk->diag_decl);
10816             }
10817         }
10818       /* Return the stored value.  */
10819       return check_value->value;
10820     }
10821
10822   /* Avoid performing name lookup if there is no possibility of
10823      finding a template-id.  */
10824   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10825       || (next_token->type == CPP_NAME
10826           && !cp_parser_nth_token_starts_template_argument_list_p
10827                (parser, 2)))
10828     {
10829       cp_parser_error (parser, "expected template-id");
10830       return error_mark_node;
10831     }
10832
10833   /* Remember where the template-id starts.  */
10834   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10835     start_of_id = cp_lexer_token_position (parser->lexer, false);
10836
10837   push_deferring_access_checks (dk_deferred);
10838
10839   /* Parse the template-name.  */
10840   is_identifier = false;
10841   templ = cp_parser_template_name (parser, template_keyword_p,
10842                                    check_dependency_p,
10843                                    is_declaration,
10844                                    &is_identifier);
10845   if (templ == error_mark_node || is_identifier)
10846     {
10847       pop_deferring_access_checks ();
10848       return templ;
10849     }
10850
10851   /* If we find the sequence `[:' after a template-name, it's probably
10852      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10853      parse correctly the argument list.  */
10854   next_token = cp_lexer_peek_token (parser->lexer);
10855   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10856   if (next_token->type == CPP_OPEN_SQUARE
10857       && next_token->flags & DIGRAPH
10858       && next_token_2->type == CPP_COLON
10859       && !(next_token_2->flags & PREV_WHITE))
10860     {
10861       cp_parser_parse_tentatively (parser);
10862       /* Change `:' into `::'.  */
10863       next_token_2->type = CPP_SCOPE;
10864       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10865          CPP_LESS.  */
10866       cp_lexer_consume_token (parser->lexer);
10867
10868       /* Parse the arguments.  */
10869       arguments = cp_parser_enclosed_template_argument_list (parser);
10870       if (!cp_parser_parse_definitely (parser))
10871         {
10872           /* If we couldn't parse an argument list, then we revert our changes
10873              and return simply an error. Maybe this is not a template-id
10874              after all.  */
10875           next_token_2->type = CPP_COLON;
10876           cp_parser_error (parser, "expected %<<%>");
10877           pop_deferring_access_checks ();
10878           return error_mark_node;
10879         }
10880       /* Otherwise, emit an error about the invalid digraph, but continue
10881          parsing because we got our argument list.  */
10882       if (permerror (next_token->location,
10883                      "%<<::%> cannot begin a template-argument list"))
10884         {
10885           static bool hint = false;
10886           inform (next_token->location,
10887                   "%<<:%> is an alternate spelling for %<[%>."
10888                   " Insert whitespace between %<<%> and %<::%>");
10889           if (!hint && !flag_permissive)
10890             {
10891               inform (next_token->location, "(if you use %<-fpermissive%>"
10892                       " G++ will accept your code)");
10893               hint = true;
10894             }
10895         }
10896     }
10897   else
10898     {
10899       /* Look for the `<' that starts the template-argument-list.  */
10900       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10901         {
10902           pop_deferring_access_checks ();
10903           return error_mark_node;
10904         }
10905       /* Parse the arguments.  */
10906       arguments = cp_parser_enclosed_template_argument_list (parser);
10907     }
10908
10909   /* Build a representation of the specialization.  */
10910   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10911     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10912   else if (DECL_CLASS_TEMPLATE_P (templ)
10913            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10914     {
10915       bool entering_scope;
10916       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10917          template (rather than some instantiation thereof) only if
10918          is not nested within some other construct.  For example, in
10919          "template <typename T> void f(T) { A<T>::", A<T> is just an
10920          instantiation of A.  */
10921       entering_scope = (template_parm_scope_p ()
10922                         && cp_lexer_next_token_is (parser->lexer,
10923                                                    CPP_SCOPE));
10924       template_id
10925         = finish_template_type (templ, arguments, entering_scope);
10926     }
10927   else
10928     {
10929       /* If it's not a class-template or a template-template, it should be
10930          a function-template.  */
10931       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10932                    || TREE_CODE (templ) == OVERLOAD
10933                    || BASELINK_P (templ)));
10934
10935       template_id = lookup_template_function (templ, arguments);
10936     }
10937
10938   /* If parsing tentatively, replace the sequence of tokens that makes
10939      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10940      should we re-parse the token stream, we will not have to repeat
10941      the effort required to do the parse, nor will we issue duplicate
10942      error messages about problems during instantiation of the
10943      template.  */
10944   if (start_of_id)
10945     {
10946       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10947
10948       /* Reset the contents of the START_OF_ID token.  */
10949       token->type = CPP_TEMPLATE_ID;
10950       /* Retrieve any deferred checks.  Do not pop this access checks yet
10951          so the memory will not be reclaimed during token replacing below.  */
10952       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10953       token->u.tree_check_value->value = template_id;
10954       token->u.tree_check_value->checks = get_deferred_access_checks ();
10955       token->keyword = RID_MAX;
10956
10957       /* Purge all subsequent tokens.  */
10958       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10959
10960       /* ??? Can we actually assume that, if template_id ==
10961          error_mark_node, we will have issued a diagnostic to the
10962          user, as opposed to simply marking the tentative parse as
10963          failed?  */
10964       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10965         error_at (token->location, "parse error in template argument list");
10966     }
10967
10968   pop_deferring_access_checks ();
10969   return template_id;
10970 }
10971
10972 /* Parse a template-name.
10973
10974    template-name:
10975      identifier
10976
10977    The standard should actually say:
10978
10979    template-name:
10980      identifier
10981      operator-function-id
10982
10983    A defect report has been filed about this issue.
10984
10985    A conversion-function-id cannot be a template name because they cannot
10986    be part of a template-id. In fact, looking at this code:
10987
10988    a.operator K<int>()
10989
10990    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10991    It is impossible to call a templated conversion-function-id with an
10992    explicit argument list, since the only allowed template parameter is
10993    the type to which it is converting.
10994
10995    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10996    `template' keyword, in a construction like:
10997
10998      T::template f<3>()
10999
11000    In that case `f' is taken to be a template-name, even though there
11001    is no way of knowing for sure.
11002
11003    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11004    name refers to a set of overloaded functions, at least one of which
11005    is a template, or an IDENTIFIER_NODE with the name of the template,
11006    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11007    names are looked up inside uninstantiated templates.  */
11008
11009 static tree
11010 cp_parser_template_name (cp_parser* parser,
11011                          bool template_keyword_p,
11012                          bool check_dependency_p,
11013                          bool is_declaration,
11014                          bool *is_identifier)
11015 {
11016   tree identifier;
11017   tree decl;
11018   tree fns;
11019   cp_token *token = cp_lexer_peek_token (parser->lexer);
11020
11021   /* If the next token is `operator', then we have either an
11022      operator-function-id or a conversion-function-id.  */
11023   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11024     {
11025       /* We don't know whether we're looking at an
11026          operator-function-id or a conversion-function-id.  */
11027       cp_parser_parse_tentatively (parser);
11028       /* Try an operator-function-id.  */
11029       identifier = cp_parser_operator_function_id (parser);
11030       /* If that didn't work, try a conversion-function-id.  */
11031       if (!cp_parser_parse_definitely (parser))
11032         {
11033           cp_parser_error (parser, "expected template-name");
11034           return error_mark_node;
11035         }
11036     }
11037   /* Look for the identifier.  */
11038   else
11039     identifier = cp_parser_identifier (parser);
11040
11041   /* If we didn't find an identifier, we don't have a template-id.  */
11042   if (identifier == error_mark_node)
11043     return error_mark_node;
11044
11045   /* If the name immediately followed the `template' keyword, then it
11046      is a template-name.  However, if the next token is not `<', then
11047      we do not treat it as a template-name, since it is not being used
11048      as part of a template-id.  This enables us to handle constructs
11049      like:
11050
11051        template <typename T> struct S { S(); };
11052        template <typename T> S<T>::S();
11053
11054      correctly.  We would treat `S' as a template -- if it were `S<T>'
11055      -- but we do not if there is no `<'.  */
11056
11057   if (processing_template_decl
11058       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11059     {
11060       /* In a declaration, in a dependent context, we pretend that the
11061          "template" keyword was present in order to improve error
11062          recovery.  For example, given:
11063
11064            template <typename T> void f(T::X<int>);
11065
11066          we want to treat "X<int>" as a template-id.  */
11067       if (is_declaration
11068           && !template_keyword_p
11069           && parser->scope && TYPE_P (parser->scope)
11070           && check_dependency_p
11071           && dependent_scope_p (parser->scope)
11072           /* Do not do this for dtors (or ctors), since they never
11073              need the template keyword before their name.  */
11074           && !constructor_name_p (identifier, parser->scope))
11075         {
11076           cp_token_position start = 0;
11077
11078           /* Explain what went wrong.  */
11079           error_at (token->location, "non-template %qD used as template",
11080                     identifier);
11081           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11082                   parser->scope, identifier);
11083           /* If parsing tentatively, find the location of the "<" token.  */
11084           if (cp_parser_simulate_error (parser))
11085             start = cp_lexer_token_position (parser->lexer, true);
11086           /* Parse the template arguments so that we can issue error
11087              messages about them.  */
11088           cp_lexer_consume_token (parser->lexer);
11089           cp_parser_enclosed_template_argument_list (parser);
11090           /* Skip tokens until we find a good place from which to
11091              continue parsing.  */
11092           cp_parser_skip_to_closing_parenthesis (parser,
11093                                                  /*recovering=*/true,
11094                                                  /*or_comma=*/true,
11095                                                  /*consume_paren=*/false);
11096           /* If parsing tentatively, permanently remove the
11097              template argument list.  That will prevent duplicate
11098              error messages from being issued about the missing
11099              "template" keyword.  */
11100           if (start)
11101             cp_lexer_purge_tokens_after (parser->lexer, start);
11102           if (is_identifier)
11103             *is_identifier = true;
11104           return identifier;
11105         }
11106
11107       /* If the "template" keyword is present, then there is generally
11108          no point in doing name-lookup, so we just return IDENTIFIER.
11109          But, if the qualifying scope is non-dependent then we can
11110          (and must) do name-lookup normally.  */
11111       if (template_keyword_p
11112           && (!parser->scope
11113               || (TYPE_P (parser->scope)
11114                   && dependent_type_p (parser->scope))))
11115         return identifier;
11116     }
11117
11118   /* Look up the name.  */
11119   decl = cp_parser_lookup_name (parser, identifier,
11120                                 none_type,
11121                                 /*is_template=*/true,
11122                                 /*is_namespace=*/false,
11123                                 check_dependency_p,
11124                                 /*ambiguous_decls=*/NULL,
11125                                 token->location);
11126
11127   /* If DECL is a template, then the name was a template-name.  */
11128   if (TREE_CODE (decl) == TEMPLATE_DECL)
11129     ;
11130   else
11131     {
11132       tree fn = NULL_TREE;
11133
11134       /* The standard does not explicitly indicate whether a name that
11135          names a set of overloaded declarations, some of which are
11136          templates, is a template-name.  However, such a name should
11137          be a template-name; otherwise, there is no way to form a
11138          template-id for the overloaded templates.  */
11139       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11140       if (TREE_CODE (fns) == OVERLOAD)
11141         for (fn = fns; fn; fn = OVL_NEXT (fn))
11142           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11143             break;
11144
11145       if (!fn)
11146         {
11147           /* The name does not name a template.  */
11148           cp_parser_error (parser, "expected template-name");
11149           return error_mark_node;
11150         }
11151     }
11152
11153   /* If DECL is dependent, and refers to a function, then just return
11154      its name; we will look it up again during template instantiation.  */
11155   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11156     {
11157       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11158       if (TYPE_P (scope) && dependent_type_p (scope))
11159         return identifier;
11160     }
11161
11162   return decl;
11163 }
11164
11165 /* Parse a template-argument-list.
11166
11167    template-argument-list:
11168      template-argument ... [opt]
11169      template-argument-list , template-argument ... [opt]
11170
11171    Returns a TREE_VEC containing the arguments.  */
11172
11173 static tree
11174 cp_parser_template_argument_list (cp_parser* parser)
11175 {
11176   tree fixed_args[10];
11177   unsigned n_args = 0;
11178   unsigned alloced = 10;
11179   tree *arg_ary = fixed_args;
11180   tree vec;
11181   bool saved_in_template_argument_list_p;
11182   bool saved_ice_p;
11183   bool saved_non_ice_p;
11184
11185   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11186   parser->in_template_argument_list_p = true;
11187   /* Even if the template-id appears in an integral
11188      constant-expression, the contents of the argument list do
11189      not.  */
11190   saved_ice_p = parser->integral_constant_expression_p;
11191   parser->integral_constant_expression_p = false;
11192   saved_non_ice_p = parser->non_integral_constant_expression_p;
11193   parser->non_integral_constant_expression_p = false;
11194   /* Parse the arguments.  */
11195   do
11196     {
11197       tree argument;
11198
11199       if (n_args)
11200         /* Consume the comma.  */
11201         cp_lexer_consume_token (parser->lexer);
11202
11203       /* Parse the template-argument.  */
11204       argument = cp_parser_template_argument (parser);
11205
11206       /* If the next token is an ellipsis, we're expanding a template
11207          argument pack. */
11208       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11209         {
11210           if (argument == error_mark_node)
11211             {
11212               cp_token *token = cp_lexer_peek_token (parser->lexer);
11213               error_at (token->location,
11214                         "expected parameter pack before %<...%>");
11215             }
11216           /* Consume the `...' token. */
11217           cp_lexer_consume_token (parser->lexer);
11218
11219           /* Make the argument into a TYPE_PACK_EXPANSION or
11220              EXPR_PACK_EXPANSION. */
11221           argument = make_pack_expansion (argument);
11222         }
11223
11224       if (n_args == alloced)
11225         {
11226           alloced *= 2;
11227
11228           if (arg_ary == fixed_args)
11229             {
11230               arg_ary = XNEWVEC (tree, alloced);
11231               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11232             }
11233           else
11234             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11235         }
11236       arg_ary[n_args++] = argument;
11237     }
11238   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11239
11240   vec = make_tree_vec (n_args);
11241
11242   while (n_args--)
11243     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11244
11245   if (arg_ary != fixed_args)
11246     free (arg_ary);
11247   parser->non_integral_constant_expression_p = saved_non_ice_p;
11248   parser->integral_constant_expression_p = saved_ice_p;
11249   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11250 #ifdef ENABLE_CHECKING
11251   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11252 #endif
11253   return vec;
11254 }
11255
11256 /* Parse a template-argument.
11257
11258    template-argument:
11259      assignment-expression
11260      type-id
11261      id-expression
11262
11263    The representation is that of an assignment-expression, type-id, or
11264    id-expression -- except that the qualified id-expression is
11265    evaluated, so that the value returned is either a DECL or an
11266    OVERLOAD.
11267
11268    Although the standard says "assignment-expression", it forbids
11269    throw-expressions or assignments in the template argument.
11270    Therefore, we use "conditional-expression" instead.  */
11271
11272 static tree
11273 cp_parser_template_argument (cp_parser* parser)
11274 {
11275   tree argument;
11276   bool template_p;
11277   bool address_p;
11278   bool maybe_type_id = false;
11279   cp_token *token = NULL, *argument_start_token = NULL;
11280   cp_id_kind idk;
11281
11282   /* There's really no way to know what we're looking at, so we just
11283      try each alternative in order.
11284
11285        [temp.arg]
11286
11287        In a template-argument, an ambiguity between a type-id and an
11288        expression is resolved to a type-id, regardless of the form of
11289        the corresponding template-parameter.
11290
11291      Therefore, we try a type-id first.  */
11292   cp_parser_parse_tentatively (parser);
11293   argument = cp_parser_template_type_arg (parser);
11294   /* If there was no error parsing the type-id but the next token is a
11295      '>>', our behavior depends on which dialect of C++ we're
11296      parsing. In C++98, we probably found a typo for '> >'. But there
11297      are type-id which are also valid expressions. For instance:
11298
11299      struct X { int operator >> (int); };
11300      template <int V> struct Foo {};
11301      Foo<X () >> 5> r;
11302
11303      Here 'X()' is a valid type-id of a function type, but the user just
11304      wanted to write the expression "X() >> 5". Thus, we remember that we
11305      found a valid type-id, but we still try to parse the argument as an
11306      expression to see what happens. 
11307
11308      In C++0x, the '>>' will be considered two separate '>'
11309      tokens.  */
11310   if (!cp_parser_error_occurred (parser)
11311       && cxx_dialect == cxx98
11312       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11313     {
11314       maybe_type_id = true;
11315       cp_parser_abort_tentative_parse (parser);
11316     }
11317   else
11318     {
11319       /* If the next token isn't a `,' or a `>', then this argument wasn't
11320       really finished. This means that the argument is not a valid
11321       type-id.  */
11322       if (!cp_parser_next_token_ends_template_argument_p (parser))
11323         cp_parser_error (parser, "expected template-argument");
11324       /* If that worked, we're done.  */
11325       if (cp_parser_parse_definitely (parser))
11326         return argument;
11327     }
11328   /* We're still not sure what the argument will be.  */
11329   cp_parser_parse_tentatively (parser);
11330   /* Try a template.  */
11331   argument_start_token = cp_lexer_peek_token (parser->lexer);
11332   argument = cp_parser_id_expression (parser,
11333                                       /*template_keyword_p=*/false,
11334                                       /*check_dependency_p=*/true,
11335                                       &template_p,
11336                                       /*declarator_p=*/false,
11337                                       /*optional_p=*/false);
11338   /* If the next token isn't a `,' or a `>', then this argument wasn't
11339      really finished.  */
11340   if (!cp_parser_next_token_ends_template_argument_p (parser))
11341     cp_parser_error (parser, "expected template-argument");
11342   if (!cp_parser_error_occurred (parser))
11343     {
11344       /* Figure out what is being referred to.  If the id-expression
11345          was for a class template specialization, then we will have a
11346          TYPE_DECL at this point.  There is no need to do name lookup
11347          at this point in that case.  */
11348       if (TREE_CODE (argument) != TYPE_DECL)
11349         argument = cp_parser_lookup_name (parser, argument,
11350                                           none_type,
11351                                           /*is_template=*/template_p,
11352                                           /*is_namespace=*/false,
11353                                           /*check_dependency=*/true,
11354                                           /*ambiguous_decls=*/NULL,
11355                                           argument_start_token->location);
11356       if (TREE_CODE (argument) != TEMPLATE_DECL
11357           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11358         cp_parser_error (parser, "expected template-name");
11359     }
11360   if (cp_parser_parse_definitely (parser))
11361     return argument;
11362   /* It must be a non-type argument.  There permitted cases are given
11363      in [temp.arg.nontype]:
11364
11365      -- an integral constant-expression of integral or enumeration
11366         type; or
11367
11368      -- the name of a non-type template-parameter; or
11369
11370      -- the name of an object or function with external linkage...
11371
11372      -- the address of an object or function with external linkage...
11373
11374      -- a pointer to member...  */
11375   /* Look for a non-type template parameter.  */
11376   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11377     {
11378       cp_parser_parse_tentatively (parser);
11379       argument = cp_parser_primary_expression (parser,
11380                                                /*address_p=*/false,
11381                                                /*cast_p=*/false,
11382                                                /*template_arg_p=*/true,
11383                                                &idk);
11384       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11385           || !cp_parser_next_token_ends_template_argument_p (parser))
11386         cp_parser_simulate_error (parser);
11387       if (cp_parser_parse_definitely (parser))
11388         return argument;
11389     }
11390
11391   /* If the next token is "&", the argument must be the address of an
11392      object or function with external linkage.  */
11393   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11394   if (address_p)
11395     cp_lexer_consume_token (parser->lexer);
11396   /* See if we might have an id-expression.  */
11397   token = cp_lexer_peek_token (parser->lexer);
11398   if (token->type == CPP_NAME
11399       || token->keyword == RID_OPERATOR
11400       || token->type == CPP_SCOPE
11401       || token->type == CPP_TEMPLATE_ID
11402       || token->type == CPP_NESTED_NAME_SPECIFIER)
11403     {
11404       cp_parser_parse_tentatively (parser);
11405       argument = cp_parser_primary_expression (parser,
11406                                                address_p,
11407                                                /*cast_p=*/false,
11408                                                /*template_arg_p=*/true,
11409                                                &idk);
11410       if (cp_parser_error_occurred (parser)
11411           || !cp_parser_next_token_ends_template_argument_p (parser))
11412         cp_parser_abort_tentative_parse (parser);
11413       else
11414         {
11415           tree probe;
11416
11417           if (TREE_CODE (argument) == INDIRECT_REF)
11418             {
11419               gcc_assert (REFERENCE_REF_P (argument));
11420               argument = TREE_OPERAND (argument, 0);
11421             }
11422
11423           /* If we're in a template, we represent a qualified-id referring
11424              to a static data member as a SCOPE_REF even if the scope isn't
11425              dependent so that we can check access control later.  */
11426           probe = argument;
11427           if (TREE_CODE (probe) == SCOPE_REF)
11428             probe = TREE_OPERAND (probe, 1);
11429           if (TREE_CODE (probe) == VAR_DECL)
11430             {
11431               /* A variable without external linkage might still be a
11432                  valid constant-expression, so no error is issued here
11433                  if the external-linkage check fails.  */
11434               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11435                 cp_parser_simulate_error (parser);
11436             }
11437           else if (is_overloaded_fn (argument))
11438             /* All overloaded functions are allowed; if the external
11439                linkage test does not pass, an error will be issued
11440                later.  */
11441             ;
11442           else if (address_p
11443                    && (TREE_CODE (argument) == OFFSET_REF
11444                        || TREE_CODE (argument) == SCOPE_REF))
11445             /* A pointer-to-member.  */
11446             ;
11447           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11448             ;
11449           else
11450             cp_parser_simulate_error (parser);
11451
11452           if (cp_parser_parse_definitely (parser))
11453             {
11454               if (address_p)
11455                 argument = build_x_unary_op (ADDR_EXPR, argument,
11456                                              tf_warning_or_error);
11457               return argument;
11458             }
11459         }
11460     }
11461   /* If the argument started with "&", there are no other valid
11462      alternatives at this point.  */
11463   if (address_p)
11464     {
11465       cp_parser_error (parser, "invalid non-type template argument");
11466       return error_mark_node;
11467     }
11468
11469   /* If the argument wasn't successfully parsed as a type-id followed
11470      by '>>', the argument can only be a constant expression now.
11471      Otherwise, we try parsing the constant-expression tentatively,
11472      because the argument could really be a type-id.  */
11473   if (maybe_type_id)
11474     cp_parser_parse_tentatively (parser);
11475   argument = cp_parser_constant_expression (parser,
11476                                             /*allow_non_constant_p=*/false,
11477                                             /*non_constant_p=*/NULL);
11478   argument = fold_non_dependent_expr (argument);
11479   if (!maybe_type_id)
11480     return argument;
11481   if (!cp_parser_next_token_ends_template_argument_p (parser))
11482     cp_parser_error (parser, "expected template-argument");
11483   if (cp_parser_parse_definitely (parser))
11484     return argument;
11485   /* We did our best to parse the argument as a non type-id, but that
11486      was the only alternative that matched (albeit with a '>' after
11487      it). We can assume it's just a typo from the user, and a
11488      diagnostic will then be issued.  */
11489   return cp_parser_template_type_arg (parser);
11490 }
11491
11492 /* Parse an explicit-instantiation.
11493
11494    explicit-instantiation:
11495      template declaration
11496
11497    Although the standard says `declaration', what it really means is:
11498
11499    explicit-instantiation:
11500      template decl-specifier-seq [opt] declarator [opt] ;
11501
11502    Things like `template int S<int>::i = 5, int S<double>::j;' are not
11503    supposed to be allowed.  A defect report has been filed about this
11504    issue.
11505
11506    GNU Extension:
11507
11508    explicit-instantiation:
11509      storage-class-specifier template
11510        decl-specifier-seq [opt] declarator [opt] ;
11511      function-specifier template
11512        decl-specifier-seq [opt] declarator [opt] ;  */
11513
11514 static void
11515 cp_parser_explicit_instantiation (cp_parser* parser)
11516 {
11517   int declares_class_or_enum;
11518   cp_decl_specifier_seq decl_specifiers;
11519   tree extension_specifier = NULL_TREE;
11520
11521   /* Look for an (optional) storage-class-specifier or
11522      function-specifier.  */
11523   if (cp_parser_allow_gnu_extensions_p (parser))
11524     {
11525       extension_specifier
11526         = cp_parser_storage_class_specifier_opt (parser);
11527       if (!extension_specifier)
11528         extension_specifier
11529           = cp_parser_function_specifier_opt (parser,
11530                                               /*decl_specs=*/NULL);
11531     }
11532
11533   /* Look for the `template' keyword.  */
11534   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11535   /* Let the front end know that we are processing an explicit
11536      instantiation.  */
11537   begin_explicit_instantiation ();
11538   /* [temp.explicit] says that we are supposed to ignore access
11539      control while processing explicit instantiation directives.  */
11540   push_deferring_access_checks (dk_no_check);
11541   /* Parse a decl-specifier-seq.  */
11542   cp_parser_decl_specifier_seq (parser,
11543                                 CP_PARSER_FLAGS_OPTIONAL,
11544                                 &decl_specifiers,
11545                                 &declares_class_or_enum);
11546   /* If there was exactly one decl-specifier, and it declared a class,
11547      and there's no declarator, then we have an explicit type
11548      instantiation.  */
11549   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11550     {
11551       tree type;
11552
11553       type = check_tag_decl (&decl_specifiers);
11554       /* Turn access control back on for names used during
11555          template instantiation.  */
11556       pop_deferring_access_checks ();
11557       if (type)
11558         do_type_instantiation (type, extension_specifier,
11559                                /*complain=*/tf_error);
11560     }
11561   else
11562     {
11563       cp_declarator *declarator;
11564       tree decl;
11565
11566       /* Parse the declarator.  */
11567       declarator
11568         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11569                                 /*ctor_dtor_or_conv_p=*/NULL,
11570                                 /*parenthesized_p=*/NULL,
11571                                 /*member_p=*/false);
11572       if (declares_class_or_enum & 2)
11573         cp_parser_check_for_definition_in_return_type (declarator,
11574                                                        decl_specifiers.type,
11575                                                        decl_specifiers.type_location);
11576       if (declarator != cp_error_declarator)
11577         {
11578           decl = grokdeclarator (declarator, &decl_specifiers,
11579                                  NORMAL, 0, &decl_specifiers.attributes);
11580           /* Turn access control back on for names used during
11581              template instantiation.  */
11582           pop_deferring_access_checks ();
11583           /* Do the explicit instantiation.  */
11584           do_decl_instantiation (decl, extension_specifier);
11585         }
11586       else
11587         {
11588           pop_deferring_access_checks ();
11589           /* Skip the body of the explicit instantiation.  */
11590           cp_parser_skip_to_end_of_statement (parser);
11591         }
11592     }
11593   /* We're done with the instantiation.  */
11594   end_explicit_instantiation ();
11595
11596   cp_parser_consume_semicolon_at_end_of_statement (parser);
11597 }
11598
11599 /* Parse an explicit-specialization.
11600
11601    explicit-specialization:
11602      template < > declaration
11603
11604    Although the standard says `declaration', what it really means is:
11605
11606    explicit-specialization:
11607      template <> decl-specifier [opt] init-declarator [opt] ;
11608      template <> function-definition
11609      template <> explicit-specialization
11610      template <> template-declaration  */
11611
11612 static void
11613 cp_parser_explicit_specialization (cp_parser* parser)
11614 {
11615   bool need_lang_pop;
11616   cp_token *token = cp_lexer_peek_token (parser->lexer);
11617
11618   /* Look for the `template' keyword.  */
11619   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11620   /* Look for the `<'.  */
11621   cp_parser_require (parser, CPP_LESS, "%<<%>");
11622   /* Look for the `>'.  */
11623   cp_parser_require (parser, CPP_GREATER, "%<>%>");
11624   /* We have processed another parameter list.  */
11625   ++parser->num_template_parameter_lists;
11626   /* [temp]
11627
11628      A template ... explicit specialization ... shall not have C
11629      linkage.  */
11630   if (current_lang_name == lang_name_c)
11631     {
11632       error_at (token->location, "template specialization with C linkage");
11633       /* Give it C++ linkage to avoid confusing other parts of the
11634          front end.  */
11635       push_lang_context (lang_name_cplusplus);
11636       need_lang_pop = true;
11637     }
11638   else
11639     need_lang_pop = false;
11640   /* Let the front end know that we are beginning a specialization.  */
11641   if (!begin_specialization ())
11642     {
11643       end_specialization ();
11644       return;
11645     }
11646
11647   /* If the next keyword is `template', we need to figure out whether
11648      or not we're looking a template-declaration.  */
11649   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11650     {
11651       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11652           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11653         cp_parser_template_declaration_after_export (parser,
11654                                                      /*member_p=*/false);
11655       else
11656         cp_parser_explicit_specialization (parser);
11657     }
11658   else
11659     /* Parse the dependent declaration.  */
11660     cp_parser_single_declaration (parser,
11661                                   /*checks=*/NULL,
11662                                   /*member_p=*/false,
11663                                   /*explicit_specialization_p=*/true,
11664                                   /*friend_p=*/NULL);
11665   /* We're done with the specialization.  */
11666   end_specialization ();
11667   /* For the erroneous case of a template with C linkage, we pushed an
11668      implicit C++ linkage scope; exit that scope now.  */
11669   if (need_lang_pop)
11670     pop_lang_context ();
11671   /* We're done with this parameter list.  */
11672   --parser->num_template_parameter_lists;
11673 }
11674
11675 /* Parse a type-specifier.
11676
11677    type-specifier:
11678      simple-type-specifier
11679      class-specifier
11680      enum-specifier
11681      elaborated-type-specifier
11682      cv-qualifier
11683
11684    GNU Extension:
11685
11686    type-specifier:
11687      __complex__
11688
11689    Returns a representation of the type-specifier.  For a
11690    class-specifier, enum-specifier, or elaborated-type-specifier, a
11691    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11692
11693    The parser flags FLAGS is used to control type-specifier parsing.
11694
11695    If IS_DECLARATION is TRUE, then this type-specifier is appearing
11696    in a decl-specifier-seq.
11697
11698    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11699    class-specifier, enum-specifier, or elaborated-type-specifier, then
11700    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
11701    if a type is declared; 2 if it is defined.  Otherwise, it is set to
11702    zero.
11703
11704    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11705    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11706    is set to FALSE.  */
11707
11708 static tree
11709 cp_parser_type_specifier (cp_parser* parser,
11710                           cp_parser_flags flags,
11711                           cp_decl_specifier_seq *decl_specs,
11712                           bool is_declaration,
11713                           int* declares_class_or_enum,
11714                           bool* is_cv_qualifier)
11715 {
11716   tree type_spec = NULL_TREE;
11717   cp_token *token;
11718   enum rid keyword;
11719   cp_decl_spec ds = ds_last;
11720
11721   /* Assume this type-specifier does not declare a new type.  */
11722   if (declares_class_or_enum)
11723     *declares_class_or_enum = 0;
11724   /* And that it does not specify a cv-qualifier.  */
11725   if (is_cv_qualifier)
11726     *is_cv_qualifier = false;
11727   /* Peek at the next token.  */
11728   token = cp_lexer_peek_token (parser->lexer);
11729
11730   /* If we're looking at a keyword, we can use that to guide the
11731      production we choose.  */
11732   keyword = token->keyword;
11733   switch (keyword)
11734     {
11735     case RID_ENUM:
11736       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11737         goto elaborated_type_specifier;
11738
11739       /* Look for the enum-specifier.  */
11740       type_spec = cp_parser_enum_specifier (parser);
11741       /* If that worked, we're done.  */
11742       if (type_spec)
11743         {
11744           if (declares_class_or_enum)
11745             *declares_class_or_enum = 2;
11746           if (decl_specs)
11747             cp_parser_set_decl_spec_type (decl_specs,
11748                                           type_spec,
11749                                           token->location,
11750                                           /*user_defined_p=*/true);
11751           return type_spec;
11752         }
11753       else
11754         goto elaborated_type_specifier;
11755
11756       /* Any of these indicate either a class-specifier, or an
11757          elaborated-type-specifier.  */
11758     case RID_CLASS:
11759     case RID_STRUCT:
11760     case RID_UNION:
11761       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11762         goto elaborated_type_specifier;
11763
11764       /* Parse tentatively so that we can back up if we don't find a
11765          class-specifier.  */
11766       cp_parser_parse_tentatively (parser);
11767       /* Look for the class-specifier.  */
11768       type_spec = cp_parser_class_specifier (parser);
11769       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11770       /* If that worked, we're done.  */
11771       if (cp_parser_parse_definitely (parser))
11772         {
11773           if (declares_class_or_enum)
11774             *declares_class_or_enum = 2;
11775           if (decl_specs)
11776             cp_parser_set_decl_spec_type (decl_specs,
11777                                           type_spec,
11778                                           token->location,
11779                                           /*user_defined_p=*/true);
11780           return type_spec;
11781         }
11782
11783       /* Fall through.  */
11784     elaborated_type_specifier:
11785       /* We're declaring (not defining) a class or enum.  */
11786       if (declares_class_or_enum)
11787         *declares_class_or_enum = 1;
11788
11789       /* Fall through.  */
11790     case RID_TYPENAME:
11791       /* Look for an elaborated-type-specifier.  */
11792       type_spec
11793         = (cp_parser_elaborated_type_specifier
11794            (parser,
11795             decl_specs && decl_specs->specs[(int) ds_friend],
11796             is_declaration));
11797       if (decl_specs)
11798         cp_parser_set_decl_spec_type (decl_specs,
11799                                       type_spec,
11800                                       token->location,
11801                                       /*user_defined_p=*/true);
11802       return type_spec;
11803
11804     case RID_CONST:
11805       ds = ds_const;
11806       if (is_cv_qualifier)
11807         *is_cv_qualifier = true;
11808       break;
11809
11810     case RID_VOLATILE:
11811       ds = ds_volatile;
11812       if (is_cv_qualifier)
11813         *is_cv_qualifier = true;
11814       break;
11815
11816     case RID_RESTRICT:
11817       ds = ds_restrict;
11818       if (is_cv_qualifier)
11819         *is_cv_qualifier = true;
11820       break;
11821
11822     case RID_COMPLEX:
11823       /* The `__complex__' keyword is a GNU extension.  */
11824       ds = ds_complex;
11825       break;
11826
11827     default:
11828       break;
11829     }
11830
11831   /* Handle simple keywords.  */
11832   if (ds != ds_last)
11833     {
11834       if (decl_specs)
11835         {
11836           ++decl_specs->specs[(int)ds];
11837           decl_specs->any_specifiers_p = true;
11838         }
11839       return cp_lexer_consume_token (parser->lexer)->u.value;
11840     }
11841
11842   /* If we do not already have a type-specifier, assume we are looking
11843      at a simple-type-specifier.  */
11844   type_spec = cp_parser_simple_type_specifier (parser,
11845                                                decl_specs,
11846                                                flags);
11847
11848   /* If we didn't find a type-specifier, and a type-specifier was not
11849      optional in this context, issue an error message.  */
11850   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11851     {
11852       cp_parser_error (parser, "expected type specifier");
11853       return error_mark_node;
11854     }
11855
11856   return type_spec;
11857 }
11858
11859 /* Parse a simple-type-specifier.
11860
11861    simple-type-specifier:
11862      :: [opt] nested-name-specifier [opt] type-name
11863      :: [opt] nested-name-specifier template template-id
11864      char
11865      wchar_t
11866      bool
11867      short
11868      int
11869      long
11870      signed
11871      unsigned
11872      float
11873      double
11874      void
11875
11876    C++0x Extension:
11877
11878    simple-type-specifier:
11879      auto
11880      decltype ( expression )   
11881      char16_t
11882      char32_t
11883
11884    GNU Extension:
11885
11886    simple-type-specifier:
11887      __typeof__ unary-expression
11888      __typeof__ ( type-id )
11889
11890    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11891    appropriately updated.  */
11892
11893 static tree
11894 cp_parser_simple_type_specifier (cp_parser* parser,
11895                                  cp_decl_specifier_seq *decl_specs,
11896                                  cp_parser_flags flags)
11897 {
11898   tree type = NULL_TREE;
11899   cp_token *token;
11900
11901   /* Peek at the next token.  */
11902   token = cp_lexer_peek_token (parser->lexer);
11903
11904   /* If we're looking at a keyword, things are easy.  */
11905   switch (token->keyword)
11906     {
11907     case RID_CHAR:
11908       if (decl_specs)
11909         decl_specs->explicit_char_p = true;
11910       type = char_type_node;
11911       break;
11912     case RID_CHAR16:
11913       type = char16_type_node;
11914       break;
11915     case RID_CHAR32:
11916       type = char32_type_node;
11917       break;
11918     case RID_WCHAR:
11919       type = wchar_type_node;
11920       break;
11921     case RID_BOOL:
11922       type = boolean_type_node;
11923       break;
11924     case RID_SHORT:
11925       if (decl_specs)
11926         ++decl_specs->specs[(int) ds_short];
11927       type = short_integer_type_node;
11928       break;
11929     case RID_INT:
11930       if (decl_specs)
11931         decl_specs->explicit_int_p = true;
11932       type = integer_type_node;
11933       break;
11934     case RID_LONG:
11935       if (decl_specs)
11936         ++decl_specs->specs[(int) ds_long];
11937       type = long_integer_type_node;
11938       break;
11939     case RID_SIGNED:
11940       if (decl_specs)
11941         ++decl_specs->specs[(int) ds_signed];
11942       type = integer_type_node;
11943       break;
11944     case RID_UNSIGNED:
11945       if (decl_specs)
11946         ++decl_specs->specs[(int) ds_unsigned];
11947       type = unsigned_type_node;
11948       break;
11949     case RID_FLOAT:
11950       type = float_type_node;
11951       break;
11952     case RID_DOUBLE:
11953       type = double_type_node;
11954       break;
11955     case RID_VOID:
11956       type = void_type_node;
11957       break;
11958       
11959     case RID_AUTO:
11960       maybe_warn_cpp0x (CPP0X_AUTO);
11961       type = make_auto ();
11962       break;
11963
11964     case RID_DECLTYPE:
11965       /* Parse the `decltype' type.  */
11966       type = cp_parser_decltype (parser);
11967
11968       if (decl_specs)
11969         cp_parser_set_decl_spec_type (decl_specs, type,
11970                                       token->location,
11971                                       /*user_defined_p=*/true);
11972
11973       return type;
11974
11975     case RID_TYPEOF:
11976       /* Consume the `typeof' token.  */
11977       cp_lexer_consume_token (parser->lexer);
11978       /* Parse the operand to `typeof'.  */
11979       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11980       /* If it is not already a TYPE, take its type.  */
11981       if (!TYPE_P (type))
11982         type = finish_typeof (type);
11983
11984       if (decl_specs)
11985         cp_parser_set_decl_spec_type (decl_specs, type,
11986                                       token->location,
11987                                       /*user_defined_p=*/true);
11988
11989       return type;
11990
11991     default:
11992       break;
11993     }
11994
11995   /* If the type-specifier was for a built-in type, we're done.  */
11996   if (type)
11997     {
11998       /* Record the type.  */
11999       if (decl_specs
12000           && (token->keyword != RID_SIGNED
12001               && token->keyword != RID_UNSIGNED
12002               && token->keyword != RID_SHORT
12003               && token->keyword != RID_LONG))
12004         cp_parser_set_decl_spec_type (decl_specs,
12005                                       type,
12006                                       token->location,
12007                                       /*user_defined=*/false);
12008       if (decl_specs)
12009         decl_specs->any_specifiers_p = true;
12010
12011       /* Consume the token.  */
12012       cp_lexer_consume_token (parser->lexer);
12013
12014       /* There is no valid C++ program where a non-template type is
12015          followed by a "<".  That usually indicates that the user thought
12016          that the type was a template.  */
12017       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12018
12019       return TYPE_NAME (type);
12020     }
12021
12022   /* The type-specifier must be a user-defined type.  */
12023   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12024     {
12025       bool qualified_p;
12026       bool global_p;
12027
12028       /* Don't gobble tokens or issue error messages if this is an
12029          optional type-specifier.  */
12030       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12031         cp_parser_parse_tentatively (parser);
12032
12033       /* Look for the optional `::' operator.  */
12034       global_p
12035         = (cp_parser_global_scope_opt (parser,
12036                                        /*current_scope_valid_p=*/false)
12037            != NULL_TREE);
12038       /* Look for the nested-name specifier.  */
12039       qualified_p
12040         = (cp_parser_nested_name_specifier_opt (parser,
12041                                                 /*typename_keyword_p=*/false,
12042                                                 /*check_dependency_p=*/true,
12043                                                 /*type_p=*/false,
12044                                                 /*is_declaration=*/false)
12045            != NULL_TREE);
12046       token = cp_lexer_peek_token (parser->lexer);
12047       /* If we have seen a nested-name-specifier, and the next token
12048          is `template', then we are using the template-id production.  */
12049       if (parser->scope
12050           && cp_parser_optional_template_keyword (parser))
12051         {
12052           /* Look for the template-id.  */
12053           type = cp_parser_template_id (parser,
12054                                         /*template_keyword_p=*/true,
12055                                         /*check_dependency_p=*/true,
12056                                         /*is_declaration=*/false);
12057           /* If the template-id did not name a type, we are out of
12058              luck.  */
12059           if (TREE_CODE (type) != TYPE_DECL)
12060             {
12061               cp_parser_error (parser, "expected template-id for type");
12062               type = NULL_TREE;
12063             }
12064         }
12065       /* Otherwise, look for a type-name.  */
12066       else
12067         type = cp_parser_type_name (parser);
12068       /* Keep track of all name-lookups performed in class scopes.  */
12069       if (type
12070           && !global_p
12071           && !qualified_p
12072           && TREE_CODE (type) == TYPE_DECL
12073           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12074         maybe_note_name_used_in_class (DECL_NAME (type), type);
12075       /* If it didn't work out, we don't have a TYPE.  */
12076       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12077           && !cp_parser_parse_definitely (parser))
12078         type = NULL_TREE;
12079       if (type && decl_specs)
12080         cp_parser_set_decl_spec_type (decl_specs, type,
12081                                       token->location,
12082                                       /*user_defined=*/true);
12083     }
12084
12085   /* If we didn't get a type-name, issue an error message.  */
12086   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12087     {
12088       cp_parser_error (parser, "expected type-name");
12089       return error_mark_node;
12090     }
12091
12092   /* There is no valid C++ program where a non-template type is
12093      followed by a "<".  That usually indicates that the user thought
12094      that the type was a template.  */
12095   if (type && type != error_mark_node)
12096     {
12097       /* As a last-ditch effort, see if TYPE is an Objective-C type.
12098          If it is, then the '<'...'>' enclose protocol names rather than
12099          template arguments, and so everything is fine.  */
12100       if (c_dialect_objc ()
12101           && (objc_is_id (type) || objc_is_class_name (type)))
12102         {
12103           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12104           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12105
12106           /* Clobber the "unqualified" type previously entered into
12107              DECL_SPECS with the new, improved protocol-qualified version.  */
12108           if (decl_specs)
12109             decl_specs->type = qual_type;
12110
12111           return qual_type;
12112         }
12113
12114       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12115                                                token->location);
12116     }
12117
12118   return type;
12119 }
12120
12121 /* Parse a type-name.
12122
12123    type-name:
12124      class-name
12125      enum-name
12126      typedef-name
12127
12128    enum-name:
12129      identifier
12130
12131    typedef-name:
12132      identifier
12133
12134    Returns a TYPE_DECL for the type.  */
12135
12136 static tree
12137 cp_parser_type_name (cp_parser* parser)
12138 {
12139   tree type_decl;
12140
12141   /* We can't know yet whether it is a class-name or not.  */
12142   cp_parser_parse_tentatively (parser);
12143   /* Try a class-name.  */
12144   type_decl = cp_parser_class_name (parser,
12145                                     /*typename_keyword_p=*/false,
12146                                     /*template_keyword_p=*/false,
12147                                     none_type,
12148                                     /*check_dependency_p=*/true,
12149                                     /*class_head_p=*/false,
12150                                     /*is_declaration=*/false);
12151   /* If it's not a class-name, keep looking.  */
12152   if (!cp_parser_parse_definitely (parser))
12153     {
12154       /* It must be a typedef-name or an enum-name.  */
12155       return cp_parser_nonclass_name (parser);
12156     }
12157
12158   return type_decl;
12159 }
12160
12161 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12162
12163    enum-name:
12164      identifier
12165
12166    typedef-name:
12167      identifier
12168
12169    Returns a TYPE_DECL for the type.  */
12170
12171 static tree
12172 cp_parser_nonclass_name (cp_parser* parser)
12173 {
12174   tree type_decl;
12175   tree identifier;
12176
12177   cp_token *token = cp_lexer_peek_token (parser->lexer);
12178   identifier = cp_parser_identifier (parser);
12179   if (identifier == error_mark_node)
12180     return error_mark_node;
12181
12182   /* Look up the type-name.  */
12183   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12184
12185   if (TREE_CODE (type_decl) != TYPE_DECL
12186       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12187     {
12188       /* See if this is an Objective-C type.  */
12189       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12190       tree type = objc_get_protocol_qualified_type (identifier, protos);
12191       if (type)
12192         type_decl = TYPE_NAME (type);
12193     }
12194   
12195   /* Issue an error if we did not find a type-name.  */
12196   if (TREE_CODE (type_decl) != TYPE_DECL)
12197     {
12198       if (!cp_parser_simulate_error (parser))
12199         cp_parser_name_lookup_error (parser, identifier, type_decl,
12200                                      "is not a type", token->location);
12201       return error_mark_node;
12202     }
12203   /* Remember that the name was used in the definition of the
12204      current class so that we can check later to see if the
12205      meaning would have been different after the class was
12206      entirely defined.  */
12207   else if (type_decl != error_mark_node
12208            && !parser->scope)
12209     maybe_note_name_used_in_class (identifier, type_decl);
12210   
12211   return type_decl;
12212 }
12213
12214 /* Parse an elaborated-type-specifier.  Note that the grammar given
12215    here incorporates the resolution to DR68.
12216
12217    elaborated-type-specifier:
12218      class-key :: [opt] nested-name-specifier [opt] identifier
12219      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12220      enum-key :: [opt] nested-name-specifier [opt] identifier
12221      typename :: [opt] nested-name-specifier identifier
12222      typename :: [opt] nested-name-specifier template [opt]
12223        template-id
12224
12225    GNU extension:
12226
12227    elaborated-type-specifier:
12228      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12229      class-key attributes :: [opt] nested-name-specifier [opt]
12230                template [opt] template-id
12231      enum attributes :: [opt] nested-name-specifier [opt] identifier
12232
12233    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12234    declared `friend'.  If IS_DECLARATION is TRUE, then this
12235    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12236    something is being declared.
12237
12238    Returns the TYPE specified.  */
12239
12240 static tree
12241 cp_parser_elaborated_type_specifier (cp_parser* parser,
12242                                      bool is_friend,
12243                                      bool is_declaration)
12244 {
12245   enum tag_types tag_type;
12246   tree identifier;
12247   tree type = NULL_TREE;
12248   tree attributes = NULL_TREE;
12249   tree globalscope;
12250   cp_token *token = NULL;
12251
12252   /* See if we're looking at the `enum' keyword.  */
12253   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12254     {
12255       /* Consume the `enum' token.  */
12256       cp_lexer_consume_token (parser->lexer);
12257       /* Remember that it's an enumeration type.  */
12258       tag_type = enum_type;
12259       /* Parse the optional `struct' or `class' key (for C++0x scoped
12260          enums).  */
12261       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12262           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12263         {
12264           if (cxx_dialect == cxx98)
12265             maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12266
12267           /* Consume the `struct' or `class'.  */
12268           cp_lexer_consume_token (parser->lexer);
12269         }
12270       /* Parse the attributes.  */
12271       attributes = cp_parser_attributes_opt (parser);
12272     }
12273   /* Or, it might be `typename'.  */
12274   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12275                                            RID_TYPENAME))
12276     {
12277       /* Consume the `typename' token.  */
12278       cp_lexer_consume_token (parser->lexer);
12279       /* Remember that it's a `typename' type.  */
12280       tag_type = typename_type;
12281     }
12282   /* Otherwise it must be a class-key.  */
12283   else
12284     {
12285       tag_type = cp_parser_class_key (parser);
12286       if (tag_type == none_type)
12287         return error_mark_node;
12288       /* Parse the attributes.  */
12289       attributes = cp_parser_attributes_opt (parser);
12290     }
12291
12292   /* Look for the `::' operator.  */
12293   globalscope =  cp_parser_global_scope_opt (parser,
12294                                              /*current_scope_valid_p=*/false);
12295   /* Look for the nested-name-specifier.  */
12296   if (tag_type == typename_type && !globalscope)
12297     {
12298       if (!cp_parser_nested_name_specifier (parser,
12299                                            /*typename_keyword_p=*/true,
12300                                            /*check_dependency_p=*/true,
12301                                            /*type_p=*/true,
12302                                             is_declaration))
12303         return error_mark_node;
12304     }
12305   else
12306     /* Even though `typename' is not present, the proposed resolution
12307        to Core Issue 180 says that in `class A<T>::B', `B' should be
12308        considered a type-name, even if `A<T>' is dependent.  */
12309     cp_parser_nested_name_specifier_opt (parser,
12310                                          /*typename_keyword_p=*/true,
12311                                          /*check_dependency_p=*/true,
12312                                          /*type_p=*/true,
12313                                          is_declaration);
12314  /* For everything but enumeration types, consider a template-id.
12315     For an enumeration type, consider only a plain identifier.  */
12316   if (tag_type != enum_type)
12317     {
12318       bool template_p = false;
12319       tree decl;
12320
12321       /* Allow the `template' keyword.  */
12322       template_p = cp_parser_optional_template_keyword (parser);
12323       /* If we didn't see `template', we don't know if there's a
12324          template-id or not.  */
12325       if (!template_p)
12326         cp_parser_parse_tentatively (parser);
12327       /* Parse the template-id.  */
12328       token = cp_lexer_peek_token (parser->lexer);
12329       decl = cp_parser_template_id (parser, template_p,
12330                                     /*check_dependency_p=*/true,
12331                                     is_declaration);
12332       /* If we didn't find a template-id, look for an ordinary
12333          identifier.  */
12334       if (!template_p && !cp_parser_parse_definitely (parser))
12335         ;
12336       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12337          in effect, then we must assume that, upon instantiation, the
12338          template will correspond to a class.  */
12339       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12340                && tag_type == typename_type)
12341         type = make_typename_type (parser->scope, decl,
12342                                    typename_type,
12343                                    /*complain=*/tf_error);
12344       /* If the `typename' keyword is in effect and DECL is not a type
12345          decl. Then type is non existant.   */
12346       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12347         type = NULL_TREE; 
12348       else 
12349         type = TREE_TYPE (decl);
12350     }
12351
12352   if (!type)
12353     {
12354       token = cp_lexer_peek_token (parser->lexer);
12355       identifier = cp_parser_identifier (parser);
12356
12357       if (identifier == error_mark_node)
12358         {
12359           parser->scope = NULL_TREE;
12360           return error_mark_node;
12361         }
12362
12363       /* For a `typename', we needn't call xref_tag.  */
12364       if (tag_type == typename_type
12365           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12366         return cp_parser_make_typename_type (parser, parser->scope,
12367                                              identifier,
12368                                              token->location);
12369       /* Look up a qualified name in the usual way.  */
12370       if (parser->scope)
12371         {
12372           tree decl;
12373           tree ambiguous_decls;
12374
12375           decl = cp_parser_lookup_name (parser, identifier,
12376                                         tag_type,
12377                                         /*is_template=*/false,
12378                                         /*is_namespace=*/false,
12379                                         /*check_dependency=*/true,
12380                                         &ambiguous_decls,
12381                                         token->location);
12382
12383           /* If the lookup was ambiguous, an error will already have been
12384              issued.  */
12385           if (ambiguous_decls)
12386             return error_mark_node;
12387
12388           /* If we are parsing friend declaration, DECL may be a
12389              TEMPLATE_DECL tree node here.  However, we need to check
12390              whether this TEMPLATE_DECL results in valid code.  Consider
12391              the following example:
12392
12393                namespace N {
12394                  template <class T> class C {};
12395                }
12396                class X {
12397                  template <class T> friend class N::C; // #1, valid code
12398                };
12399                template <class T> class Y {
12400                  friend class N::C;                    // #2, invalid code
12401                };
12402
12403              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12404              name lookup of `N::C'.  We see that friend declaration must
12405              be template for the code to be valid.  Note that
12406              processing_template_decl does not work here since it is
12407              always 1 for the above two cases.  */
12408
12409           decl = (cp_parser_maybe_treat_template_as_class
12410                   (decl, /*tag_name_p=*/is_friend
12411                          && parser->num_template_parameter_lists));
12412
12413           if (TREE_CODE (decl) != TYPE_DECL)
12414             {
12415               cp_parser_diagnose_invalid_type_name (parser,
12416                                                     parser->scope,
12417                                                     identifier,
12418                                                     token->location);
12419               return error_mark_node;
12420             }
12421
12422           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12423             {
12424               bool allow_template = (parser->num_template_parameter_lists
12425                                       || DECL_SELF_REFERENCE_P (decl));
12426               type = check_elaborated_type_specifier (tag_type, decl, 
12427                                                       allow_template);
12428
12429               if (type == error_mark_node)
12430                 return error_mark_node;
12431             }
12432
12433           /* Forward declarations of nested types, such as
12434
12435                class C1::C2;
12436                class C1::C2::C3;
12437
12438              are invalid unless all components preceding the final '::'
12439              are complete.  If all enclosing types are complete, these
12440              declarations become merely pointless.
12441
12442              Invalid forward declarations of nested types are errors
12443              caught elsewhere in parsing.  Those that are pointless arrive
12444              here.  */
12445
12446           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12447               && !is_friend && !processing_explicit_instantiation)
12448             warning (0, "declaration %qD does not declare anything", decl);
12449
12450           type = TREE_TYPE (decl);
12451         }
12452       else
12453         {
12454           /* An elaborated-type-specifier sometimes introduces a new type and
12455              sometimes names an existing type.  Normally, the rule is that it
12456              introduces a new type only if there is not an existing type of
12457              the same name already in scope.  For example, given:
12458
12459                struct S {};
12460                void f() { struct S s; }
12461
12462              the `struct S' in the body of `f' is the same `struct S' as in
12463              the global scope; the existing definition is used.  However, if
12464              there were no global declaration, this would introduce a new
12465              local class named `S'.
12466
12467              An exception to this rule applies to the following code:
12468
12469                namespace N { struct S; }
12470
12471              Here, the elaborated-type-specifier names a new type
12472              unconditionally; even if there is already an `S' in the
12473              containing scope this declaration names a new type.
12474              This exception only applies if the elaborated-type-specifier
12475              forms the complete declaration:
12476
12477                [class.name]
12478
12479                A declaration consisting solely of `class-key identifier ;' is
12480                either a redeclaration of the name in the current scope or a
12481                forward declaration of the identifier as a class name.  It
12482                introduces the name into the current scope.
12483
12484              We are in this situation precisely when the next token is a `;'.
12485
12486              An exception to the exception is that a `friend' declaration does
12487              *not* name a new type; i.e., given:
12488
12489                struct S { friend struct T; };
12490
12491              `T' is not a new type in the scope of `S'.
12492
12493              Also, `new struct S' or `sizeof (struct S)' never results in the
12494              definition of a new type; a new type can only be declared in a
12495              declaration context.  */
12496
12497           tag_scope ts;
12498           bool template_p;
12499
12500           if (is_friend)
12501             /* Friends have special name lookup rules.  */
12502             ts = ts_within_enclosing_non_class;
12503           else if (is_declaration
12504                    && cp_lexer_next_token_is (parser->lexer,
12505                                               CPP_SEMICOLON))
12506             /* This is a `class-key identifier ;' */
12507             ts = ts_current;
12508           else
12509             ts = ts_global;
12510
12511           template_p =
12512             (parser->num_template_parameter_lists
12513              && (cp_parser_next_token_starts_class_definition_p (parser)
12514                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12515           /* An unqualified name was used to reference this type, so
12516              there were no qualifying templates.  */
12517           if (!cp_parser_check_template_parameters (parser,
12518                                                     /*num_templates=*/0,
12519                                                     token->location,
12520                                                     /*declarator=*/NULL))
12521             return error_mark_node;
12522           type = xref_tag (tag_type, identifier, ts, template_p);
12523         }
12524     }
12525
12526   if (type == error_mark_node)
12527     return error_mark_node;
12528
12529   /* Allow attributes on forward declarations of classes.  */
12530   if (attributes)
12531     {
12532       if (TREE_CODE (type) == TYPENAME_TYPE)
12533         warning (OPT_Wattributes,
12534                  "attributes ignored on uninstantiated type");
12535       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12536                && ! processing_explicit_instantiation)
12537         warning (OPT_Wattributes,
12538                  "attributes ignored on template instantiation");
12539       else if (is_declaration && cp_parser_declares_only_class_p (parser))
12540         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12541       else
12542         warning (OPT_Wattributes,
12543                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12544     }
12545
12546   if (tag_type != enum_type)
12547     cp_parser_check_class_key (tag_type, type);
12548
12549   /* A "<" cannot follow an elaborated type specifier.  If that
12550      happens, the user was probably trying to form a template-id.  */
12551   cp_parser_check_for_invalid_template_id (parser, type, token->location);
12552
12553   return type;
12554 }
12555
12556 /* Parse an enum-specifier.
12557
12558    enum-specifier:
12559      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12560
12561    enum-key:
12562      enum
12563      enum class   [C++0x]
12564      enum struct  [C++0x]
12565
12566    enum-base:   [C++0x]
12567      : type-specifier-seq
12568
12569    GNU Extensions:
12570      enum-key attributes[opt] identifier [opt] enum-base [opt] 
12571        { enumerator-list [opt] }attributes[opt]
12572
12573    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12574    if the token stream isn't an enum-specifier after all.  */
12575
12576 static tree
12577 cp_parser_enum_specifier (cp_parser* parser)
12578 {
12579   tree identifier;
12580   tree type;
12581   tree attributes;
12582   bool scoped_enum_p = false;
12583   bool has_underlying_type = false;
12584   tree underlying_type = NULL_TREE;
12585
12586   /* Parse tentatively so that we can back up if we don't find a
12587      enum-specifier.  */
12588   cp_parser_parse_tentatively (parser);
12589
12590   /* Caller guarantees that the current token is 'enum', an identifier
12591      possibly follows, and the token after that is an opening brace.
12592      If we don't have an identifier, fabricate an anonymous name for
12593      the enumeration being defined.  */
12594   cp_lexer_consume_token (parser->lexer);
12595
12596   /* Parse the "class" or "struct", which indicates a scoped
12597      enumeration type in C++0x.  */
12598   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12599       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12600     {
12601       if (cxx_dialect == cxx98)
12602         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12603
12604       /* Consume the `struct' or `class' token.  */
12605       cp_lexer_consume_token (parser->lexer);
12606
12607       scoped_enum_p = true;
12608     }
12609
12610   attributes = cp_parser_attributes_opt (parser);
12611
12612   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12613     identifier = cp_parser_identifier (parser);
12614   else
12615     identifier = make_anon_name ();
12616
12617   /* Check for the `:' that denotes a specified underlying type in C++0x.
12618      Note that a ':' could also indicate a bitfield width, however.  */
12619   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12620     {
12621       cp_decl_specifier_seq type_specifiers;
12622
12623       /* Consume the `:'.  */
12624       cp_lexer_consume_token (parser->lexer);
12625
12626       /* Parse the type-specifier-seq.  */
12627       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12628                                     /*is_trailing_return=*/false,
12629                                     &type_specifiers);
12630
12631       /* At this point this is surely not elaborated type specifier.  */
12632       if (!cp_parser_parse_definitely (parser))
12633         return NULL_TREE;
12634
12635       if (cxx_dialect == cxx98)
12636         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12637
12638       has_underlying_type = true;
12639
12640       /* If that didn't work, stop.  */
12641       if (type_specifiers.type != error_mark_node)
12642         {
12643           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12644                                             /*initialized=*/0, NULL);
12645           if (underlying_type == error_mark_node)
12646             underlying_type = NULL_TREE;
12647         }
12648     }
12649
12650   /* Look for the `{' but don't consume it yet.  */
12651   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12652     {
12653       cp_parser_error (parser, "expected %<{%>");
12654       if (has_underlying_type)
12655         return NULL_TREE;
12656     }
12657
12658   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12659     return NULL_TREE;
12660
12661   /* Issue an error message if type-definitions are forbidden here.  */
12662   if (!cp_parser_check_type_definition (parser))
12663     type = error_mark_node;
12664   else
12665     /* Create the new type.  We do this before consuming the opening
12666        brace so the enum will be recorded as being on the line of its
12667        tag (or the 'enum' keyword, if there is no tag).  */
12668     type = start_enum (identifier, underlying_type, scoped_enum_p);
12669   
12670   /* Consume the opening brace.  */
12671   cp_lexer_consume_token (parser->lexer);
12672
12673   if (type == error_mark_node)
12674     {
12675       cp_parser_skip_to_end_of_block_or_statement (parser);
12676       return error_mark_node;
12677     }
12678
12679   /* If the next token is not '}', then there are some enumerators.  */
12680   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12681     cp_parser_enumerator_list (parser, type);
12682
12683   /* Consume the final '}'.  */
12684   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12685
12686   /* Look for trailing attributes to apply to this enumeration, and
12687      apply them if appropriate.  */
12688   if (cp_parser_allow_gnu_extensions_p (parser))
12689     {
12690       tree trailing_attr = cp_parser_attributes_opt (parser);
12691       trailing_attr = chainon (trailing_attr, attributes);
12692       cplus_decl_attributes (&type,
12693                              trailing_attr,
12694                              (int) ATTR_FLAG_TYPE_IN_PLACE);
12695     }
12696
12697   /* Finish up the enumeration.  */
12698   finish_enum (type);
12699
12700   return type;
12701 }
12702
12703 /* Parse an enumerator-list.  The enumerators all have the indicated
12704    TYPE.
12705
12706    enumerator-list:
12707      enumerator-definition
12708      enumerator-list , enumerator-definition  */
12709
12710 static void
12711 cp_parser_enumerator_list (cp_parser* parser, tree type)
12712 {
12713   while (true)
12714     {
12715       /* Parse an enumerator-definition.  */
12716       cp_parser_enumerator_definition (parser, type);
12717
12718       /* If the next token is not a ',', we've reached the end of
12719          the list.  */
12720       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12721         break;
12722       /* Otherwise, consume the `,' and keep going.  */
12723       cp_lexer_consume_token (parser->lexer);
12724       /* If the next token is a `}', there is a trailing comma.  */
12725       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12726         {
12727           if (!in_system_header)
12728             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12729           break;
12730         }
12731     }
12732 }
12733
12734 /* Parse an enumerator-definition.  The enumerator has the indicated
12735    TYPE.
12736
12737    enumerator-definition:
12738      enumerator
12739      enumerator = constant-expression
12740
12741    enumerator:
12742      identifier  */
12743
12744 static void
12745 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12746 {
12747   tree identifier;
12748   tree value;
12749
12750   /* Look for the identifier.  */
12751   identifier = cp_parser_identifier (parser);
12752   if (identifier == error_mark_node)
12753     return;
12754
12755   /* If the next token is an '=', then there is an explicit value.  */
12756   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12757     {
12758       /* Consume the `=' token.  */
12759       cp_lexer_consume_token (parser->lexer);
12760       /* Parse the value.  */
12761       value = cp_parser_constant_expression (parser,
12762                                              /*allow_non_constant_p=*/false,
12763                                              NULL);
12764     }
12765   else
12766     value = NULL_TREE;
12767
12768   /* If we are processing a template, make sure the initializer of the
12769      enumerator doesn't contain any bare template parameter pack.  */
12770   if (check_for_bare_parameter_packs (value))
12771     value = error_mark_node;
12772
12773   /* Create the enumerator.  */
12774   build_enumerator (identifier, value, type);
12775 }
12776
12777 /* Parse a namespace-name.
12778
12779    namespace-name:
12780      original-namespace-name
12781      namespace-alias
12782
12783    Returns the NAMESPACE_DECL for the namespace.  */
12784
12785 static tree
12786 cp_parser_namespace_name (cp_parser* parser)
12787 {
12788   tree identifier;
12789   tree namespace_decl;
12790
12791   cp_token *token = cp_lexer_peek_token (parser->lexer);
12792
12793   /* Get the name of the namespace.  */
12794   identifier = cp_parser_identifier (parser);
12795   if (identifier == error_mark_node)
12796     return error_mark_node;
12797
12798   /* Look up the identifier in the currently active scope.  Look only
12799      for namespaces, due to:
12800
12801        [basic.lookup.udir]
12802
12803        When looking up a namespace-name in a using-directive or alias
12804        definition, only namespace names are considered.
12805
12806      And:
12807
12808        [basic.lookup.qual]
12809
12810        During the lookup of a name preceding the :: scope resolution
12811        operator, object, function, and enumerator names are ignored.
12812
12813      (Note that cp_parser_qualifying_entity only calls this
12814      function if the token after the name is the scope resolution
12815      operator.)  */
12816   namespace_decl = cp_parser_lookup_name (parser, identifier,
12817                                           none_type,
12818                                           /*is_template=*/false,
12819                                           /*is_namespace=*/true,
12820                                           /*check_dependency=*/true,
12821                                           /*ambiguous_decls=*/NULL,
12822                                           token->location);
12823   /* If it's not a namespace, issue an error.  */
12824   if (namespace_decl == error_mark_node
12825       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12826     {
12827       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12828         error_at (token->location, "%qD is not a namespace-name", identifier);
12829       cp_parser_error (parser, "expected namespace-name");
12830       namespace_decl = error_mark_node;
12831     }
12832
12833   return namespace_decl;
12834 }
12835
12836 /* Parse a namespace-definition.
12837
12838    namespace-definition:
12839      named-namespace-definition
12840      unnamed-namespace-definition
12841
12842    named-namespace-definition:
12843      original-namespace-definition
12844      extension-namespace-definition
12845
12846    original-namespace-definition:
12847      namespace identifier { namespace-body }
12848
12849    extension-namespace-definition:
12850      namespace original-namespace-name { namespace-body }
12851
12852    unnamed-namespace-definition:
12853      namespace { namespace-body } */
12854
12855 static void
12856 cp_parser_namespace_definition (cp_parser* parser)
12857 {
12858   tree identifier, attribs;
12859   bool has_visibility;
12860   bool is_inline;
12861
12862   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12863     {
12864       is_inline = true;
12865       cp_lexer_consume_token (parser->lexer);
12866     }
12867   else
12868     is_inline = false;
12869
12870   /* Look for the `namespace' keyword.  */
12871   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12872
12873   /* Get the name of the namespace.  We do not attempt to distinguish
12874      between an original-namespace-definition and an
12875      extension-namespace-definition at this point.  The semantic
12876      analysis routines are responsible for that.  */
12877   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12878     identifier = cp_parser_identifier (parser);
12879   else
12880     identifier = NULL_TREE;
12881
12882   /* Parse any specified attributes.  */
12883   attribs = cp_parser_attributes_opt (parser);
12884
12885   /* Look for the `{' to start the namespace.  */
12886   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12887   /* Start the namespace.  */
12888   push_namespace (identifier);
12889
12890   /* "inline namespace" is equivalent to a stub namespace definition
12891      followed by a strong using directive.  */
12892   if (is_inline)
12893     {
12894       tree name_space = current_namespace;
12895       /* Set up namespace association.  */
12896       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12897         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12898                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12899       /* Import the contents of the inline namespace.  */
12900       pop_namespace ();
12901       do_using_directive (name_space);
12902       push_namespace (identifier);
12903     }
12904
12905   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12906
12907   /* Parse the body of the namespace.  */
12908   cp_parser_namespace_body (parser);
12909
12910 #ifdef HANDLE_PRAGMA_VISIBILITY
12911   if (has_visibility)
12912     pop_visibility (1);
12913 #endif
12914
12915   /* Finish the namespace.  */
12916   pop_namespace ();
12917   /* Look for the final `}'.  */
12918   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12919 }
12920
12921 /* Parse a namespace-body.
12922
12923    namespace-body:
12924      declaration-seq [opt]  */
12925
12926 static void
12927 cp_parser_namespace_body (cp_parser* parser)
12928 {
12929   cp_parser_declaration_seq_opt (parser);
12930 }
12931
12932 /* Parse a namespace-alias-definition.
12933
12934    namespace-alias-definition:
12935      namespace identifier = qualified-namespace-specifier ;  */
12936
12937 static void
12938 cp_parser_namespace_alias_definition (cp_parser* parser)
12939 {
12940   tree identifier;
12941   tree namespace_specifier;
12942
12943   cp_token *token = cp_lexer_peek_token (parser->lexer);
12944
12945   /* Look for the `namespace' keyword.  */
12946   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12947   /* Look for the identifier.  */
12948   identifier = cp_parser_identifier (parser);
12949   if (identifier == error_mark_node)
12950     return;
12951   /* Look for the `=' token.  */
12952   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12953       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12954     {
12955       error_at (token->location, "%<namespace%> definition is not allowed here");
12956       /* Skip the definition.  */
12957       cp_lexer_consume_token (parser->lexer);
12958       if (cp_parser_skip_to_closing_brace (parser))
12959         cp_lexer_consume_token (parser->lexer);
12960       return;
12961     }
12962   cp_parser_require (parser, CPP_EQ, "%<=%>");
12963   /* Look for the qualified-namespace-specifier.  */
12964   namespace_specifier
12965     = cp_parser_qualified_namespace_specifier (parser);
12966   /* Look for the `;' token.  */
12967   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12968
12969   /* Register the alias in the symbol table.  */
12970   do_namespace_alias (identifier, namespace_specifier);
12971 }
12972
12973 /* Parse a qualified-namespace-specifier.
12974
12975    qualified-namespace-specifier:
12976      :: [opt] nested-name-specifier [opt] namespace-name
12977
12978    Returns a NAMESPACE_DECL corresponding to the specified
12979    namespace.  */
12980
12981 static tree
12982 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12983 {
12984   /* Look for the optional `::'.  */
12985   cp_parser_global_scope_opt (parser,
12986                               /*current_scope_valid_p=*/false);
12987
12988   /* Look for the optional nested-name-specifier.  */
12989   cp_parser_nested_name_specifier_opt (parser,
12990                                        /*typename_keyword_p=*/false,
12991                                        /*check_dependency_p=*/true,
12992                                        /*type_p=*/false,
12993                                        /*is_declaration=*/true);
12994
12995   return cp_parser_namespace_name (parser);
12996 }
12997
12998 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12999    access declaration.
13000
13001    using-declaration:
13002      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13003      using :: unqualified-id ;  
13004
13005    access-declaration:
13006      qualified-id ;  
13007
13008    */
13009
13010 static bool
13011 cp_parser_using_declaration (cp_parser* parser, 
13012                              bool access_declaration_p)
13013 {
13014   cp_token *token;
13015   bool typename_p = false;
13016   bool global_scope_p;
13017   tree decl;
13018   tree identifier;
13019   tree qscope;
13020
13021   if (access_declaration_p)
13022     cp_parser_parse_tentatively (parser);
13023   else
13024     {
13025       /* Look for the `using' keyword.  */
13026       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13027       
13028       /* Peek at the next token.  */
13029       token = cp_lexer_peek_token (parser->lexer);
13030       /* See if it's `typename'.  */
13031       if (token->keyword == RID_TYPENAME)
13032         {
13033           /* Remember that we've seen it.  */
13034           typename_p = true;
13035           /* Consume the `typename' token.  */
13036           cp_lexer_consume_token (parser->lexer);
13037         }
13038     }
13039
13040   /* Look for the optional global scope qualification.  */
13041   global_scope_p
13042     = (cp_parser_global_scope_opt (parser,
13043                                    /*current_scope_valid_p=*/false)
13044        != NULL_TREE);
13045
13046   /* If we saw `typename', or didn't see `::', then there must be a
13047      nested-name-specifier present.  */
13048   if (typename_p || !global_scope_p)
13049     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13050                                               /*check_dependency_p=*/true,
13051                                               /*type_p=*/false,
13052                                               /*is_declaration=*/true);
13053   /* Otherwise, we could be in either of the two productions.  In that
13054      case, treat the nested-name-specifier as optional.  */
13055   else
13056     qscope = cp_parser_nested_name_specifier_opt (parser,
13057                                                   /*typename_keyword_p=*/false,
13058                                                   /*check_dependency_p=*/true,
13059                                                   /*type_p=*/false,
13060                                                   /*is_declaration=*/true);
13061   if (!qscope)
13062     qscope = global_namespace;
13063
13064   if (access_declaration_p && cp_parser_error_occurred (parser))
13065     /* Something has already gone wrong; there's no need to parse
13066        further.  Since an error has occurred, the return value of
13067        cp_parser_parse_definitely will be false, as required.  */
13068     return cp_parser_parse_definitely (parser);
13069
13070   token = cp_lexer_peek_token (parser->lexer);
13071   /* Parse the unqualified-id.  */
13072   identifier = cp_parser_unqualified_id (parser,
13073                                          /*template_keyword_p=*/false,
13074                                          /*check_dependency_p=*/true,
13075                                          /*declarator_p=*/true,
13076                                          /*optional_p=*/false);
13077
13078   if (access_declaration_p)
13079     {
13080       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13081         cp_parser_simulate_error (parser);
13082       if (!cp_parser_parse_definitely (parser))
13083         return false;
13084     }
13085
13086   /* The function we call to handle a using-declaration is different
13087      depending on what scope we are in.  */
13088   if (qscope == error_mark_node || identifier == error_mark_node)
13089     ;
13090   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13091            && TREE_CODE (identifier) != BIT_NOT_EXPR)
13092     /* [namespace.udecl]
13093
13094        A using declaration shall not name a template-id.  */
13095     error_at (token->location,
13096               "a template-id may not appear in a using-declaration");
13097   else
13098     {
13099       if (at_class_scope_p ())
13100         {
13101           /* Create the USING_DECL.  */
13102           decl = do_class_using_decl (parser->scope, identifier);
13103
13104           if (check_for_bare_parameter_packs (decl))
13105             return false;
13106           else
13107             /* Add it to the list of members in this class.  */
13108             finish_member_declaration (decl);
13109         }
13110       else
13111         {
13112           decl = cp_parser_lookup_name_simple (parser,
13113                                                identifier,
13114                                                token->location);
13115           if (decl == error_mark_node)
13116             cp_parser_name_lookup_error (parser, identifier,
13117                                          decl, NULL,
13118                                          token->location);
13119           else if (check_for_bare_parameter_packs (decl))
13120             return false;
13121           else if (!at_namespace_scope_p ())
13122             do_local_using_decl (decl, qscope, identifier);
13123           else
13124             do_toplevel_using_decl (decl, qscope, identifier);
13125         }
13126     }
13127
13128   /* Look for the final `;'.  */
13129   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13130   
13131   return true;
13132 }
13133
13134 /* Parse a using-directive.
13135
13136    using-directive:
13137      using namespace :: [opt] nested-name-specifier [opt]
13138        namespace-name ;  */
13139
13140 static void
13141 cp_parser_using_directive (cp_parser* parser)
13142 {
13143   tree namespace_decl;
13144   tree attribs;
13145
13146   /* Look for the `using' keyword.  */
13147   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13148   /* And the `namespace' keyword.  */
13149   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13150   /* Look for the optional `::' operator.  */
13151   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13152   /* And the optional nested-name-specifier.  */
13153   cp_parser_nested_name_specifier_opt (parser,
13154                                        /*typename_keyword_p=*/false,
13155                                        /*check_dependency_p=*/true,
13156                                        /*type_p=*/false,
13157                                        /*is_declaration=*/true);
13158   /* Get the namespace being used.  */
13159   namespace_decl = cp_parser_namespace_name (parser);
13160   /* And any specified attributes.  */
13161   attribs = cp_parser_attributes_opt (parser);
13162   /* Update the symbol table.  */
13163   parse_using_directive (namespace_decl, attribs);
13164   /* Look for the final `;'.  */
13165   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13166 }
13167
13168 /* Parse an asm-definition.
13169
13170    asm-definition:
13171      asm ( string-literal ) ;
13172
13173    GNU Extension:
13174
13175    asm-definition:
13176      asm volatile [opt] ( string-literal ) ;
13177      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13178      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13179                           : asm-operand-list [opt] ) ;
13180      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13181                           : asm-operand-list [opt]
13182                           : asm-clobber-list [opt] ) ;
13183      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13184                                : asm-clobber-list [opt]
13185                                : asm-goto-list ) ;  */
13186
13187 static void
13188 cp_parser_asm_definition (cp_parser* parser)
13189 {
13190   tree string;
13191   tree outputs = NULL_TREE;
13192   tree inputs = NULL_TREE;
13193   tree clobbers = NULL_TREE;
13194   tree labels = NULL_TREE;
13195   tree asm_stmt;
13196   bool volatile_p = false;
13197   bool extended_p = false;
13198   bool invalid_inputs_p = false;
13199   bool invalid_outputs_p = false;
13200   bool goto_p = false;
13201   const char *missing = NULL;
13202
13203   /* Look for the `asm' keyword.  */
13204   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13205   /* See if the next token is `volatile'.  */
13206   if (cp_parser_allow_gnu_extensions_p (parser)
13207       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13208     {
13209       /* Remember that we saw the `volatile' keyword.  */
13210       volatile_p = true;
13211       /* Consume the token.  */
13212       cp_lexer_consume_token (parser->lexer);
13213     }
13214   if (cp_parser_allow_gnu_extensions_p (parser)
13215       && parser->in_function_body
13216       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13217     {
13218       /* Remember that we saw the `goto' keyword.  */
13219       goto_p = true;
13220       /* Consume the token.  */
13221       cp_lexer_consume_token (parser->lexer);
13222     }
13223   /* Look for the opening `('.  */
13224   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13225     return;
13226   /* Look for the string.  */
13227   string = cp_parser_string_literal (parser, false, false);
13228   if (string == error_mark_node)
13229     {
13230       cp_parser_skip_to_closing_parenthesis (parser, true, false,
13231                                              /*consume_paren=*/true);
13232       return;
13233     }
13234
13235   /* If we're allowing GNU extensions, check for the extended assembly
13236      syntax.  Unfortunately, the `:' tokens need not be separated by
13237      a space in C, and so, for compatibility, we tolerate that here
13238      too.  Doing that means that we have to treat the `::' operator as
13239      two `:' tokens.  */
13240   if (cp_parser_allow_gnu_extensions_p (parser)
13241       && parser->in_function_body
13242       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13243           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13244     {
13245       bool inputs_p = false;
13246       bool clobbers_p = false;
13247       bool labels_p = false;
13248
13249       /* The extended syntax was used.  */
13250       extended_p = true;
13251
13252       /* Look for outputs.  */
13253       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13254         {
13255           /* Consume the `:'.  */
13256           cp_lexer_consume_token (parser->lexer);
13257           /* Parse the output-operands.  */
13258           if (cp_lexer_next_token_is_not (parser->lexer,
13259                                           CPP_COLON)
13260               && cp_lexer_next_token_is_not (parser->lexer,
13261                                              CPP_SCOPE)
13262               && cp_lexer_next_token_is_not (parser->lexer,
13263                                              CPP_CLOSE_PAREN)
13264               && !goto_p)
13265             outputs = cp_parser_asm_operand_list (parser);
13266
13267             if (outputs == error_mark_node)
13268               invalid_outputs_p = true;
13269         }
13270       /* If the next token is `::', there are no outputs, and the
13271          next token is the beginning of the inputs.  */
13272       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13273         /* The inputs are coming next.  */
13274         inputs_p = true;
13275
13276       /* Look for inputs.  */
13277       if (inputs_p
13278           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13279         {
13280           /* Consume the `:' or `::'.  */
13281           cp_lexer_consume_token (parser->lexer);
13282           /* Parse the output-operands.  */
13283           if (cp_lexer_next_token_is_not (parser->lexer,
13284                                           CPP_COLON)
13285               && cp_lexer_next_token_is_not (parser->lexer,
13286                                              CPP_SCOPE)
13287               && cp_lexer_next_token_is_not (parser->lexer,
13288                                              CPP_CLOSE_PAREN))
13289             inputs = cp_parser_asm_operand_list (parser);
13290
13291             if (inputs == error_mark_node)
13292               invalid_inputs_p = true;
13293         }
13294       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13295         /* The clobbers are coming next.  */
13296         clobbers_p = true;
13297
13298       /* Look for clobbers.  */
13299       if (clobbers_p
13300           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13301         {
13302           clobbers_p = true;
13303           /* Consume the `:' or `::'.  */
13304           cp_lexer_consume_token (parser->lexer);
13305           /* Parse the clobbers.  */
13306           if (cp_lexer_next_token_is_not (parser->lexer,
13307                                           CPP_COLON)
13308               && cp_lexer_next_token_is_not (parser->lexer,
13309                                              CPP_CLOSE_PAREN))
13310             clobbers = cp_parser_asm_clobber_list (parser);
13311         }
13312       else if (goto_p
13313                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13314         /* The labels are coming next.  */
13315         labels_p = true;
13316
13317       /* Look for labels.  */
13318       if (labels_p
13319           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13320         {
13321           labels_p = true;
13322           /* Consume the `:' or `::'.  */
13323           cp_lexer_consume_token (parser->lexer);
13324           /* Parse the labels.  */
13325           labels = cp_parser_asm_label_list (parser);
13326         }
13327
13328       if (goto_p && !labels_p)
13329         missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13330     }
13331   else if (goto_p)
13332     missing = "%<:%> or %<::%>";
13333
13334   /* Look for the closing `)'.  */
13335   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13336                           missing ? missing : "%<)%>"))
13337     cp_parser_skip_to_closing_parenthesis (parser, true, false,
13338                                            /*consume_paren=*/true);
13339   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13340
13341   if (!invalid_inputs_p && !invalid_outputs_p)
13342     {
13343       /* Create the ASM_EXPR.  */
13344       if (parser->in_function_body)
13345         {
13346           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13347                                       inputs, clobbers, labels);
13348           /* If the extended syntax was not used, mark the ASM_EXPR.  */
13349           if (!extended_p)
13350             {
13351               tree temp = asm_stmt;
13352               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13353                 temp = TREE_OPERAND (temp, 0);
13354
13355               ASM_INPUT_P (temp) = 1;
13356             }
13357         }
13358       else
13359         cgraph_add_asm_node (string);
13360     }
13361 }
13362
13363 /* Declarators [gram.dcl.decl] */
13364
13365 /* Parse an init-declarator.
13366
13367    init-declarator:
13368      declarator initializer [opt]
13369
13370    GNU Extension:
13371
13372    init-declarator:
13373      declarator asm-specification [opt] attributes [opt] initializer [opt]
13374
13375    function-definition:
13376      decl-specifier-seq [opt] declarator ctor-initializer [opt]
13377        function-body
13378      decl-specifier-seq [opt] declarator function-try-block
13379
13380    GNU Extension:
13381
13382    function-definition:
13383      __extension__ function-definition
13384
13385    The DECL_SPECIFIERS apply to this declarator.  Returns a
13386    representation of the entity declared.  If MEMBER_P is TRUE, then
13387    this declarator appears in a class scope.  The new DECL created by
13388    this declarator is returned.
13389
13390    The CHECKS are access checks that should be performed once we know
13391    what entity is being declared (and, therefore, what classes have
13392    befriended it).
13393
13394    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13395    for a function-definition here as well.  If the declarator is a
13396    declarator for a function-definition, *FUNCTION_DEFINITION_P will
13397    be TRUE upon return.  By that point, the function-definition will
13398    have been completely parsed.
13399
13400    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13401    is FALSE.  */
13402
13403 static tree
13404 cp_parser_init_declarator (cp_parser* parser,
13405                            cp_decl_specifier_seq *decl_specifiers,
13406                            VEC (deferred_access_check,gc)* checks,
13407                            bool function_definition_allowed_p,
13408                            bool member_p,
13409                            int declares_class_or_enum,
13410                            bool* function_definition_p)
13411 {
13412   cp_token *token = NULL, *asm_spec_start_token = NULL,
13413            *attributes_start_token = NULL;
13414   cp_declarator *declarator;
13415   tree prefix_attributes;
13416   tree attributes;
13417   tree asm_specification;
13418   tree initializer;
13419   tree decl = NULL_TREE;
13420   tree scope;
13421   int is_initialized;
13422   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
13423      initialized with "= ..", CPP_OPEN_PAREN if initialized with
13424      "(...)".  */
13425   enum cpp_ttype initialization_kind;
13426   bool is_direct_init = false;
13427   bool is_non_constant_init;
13428   int ctor_dtor_or_conv_p;
13429   bool friend_p;
13430   tree pushed_scope = NULL;
13431
13432   /* Gather the attributes that were provided with the
13433      decl-specifiers.  */
13434   prefix_attributes = decl_specifiers->attributes;
13435
13436   /* Assume that this is not the declarator for a function
13437      definition.  */
13438   if (function_definition_p)
13439     *function_definition_p = false;
13440
13441   /* Defer access checks while parsing the declarator; we cannot know
13442      what names are accessible until we know what is being
13443      declared.  */
13444   resume_deferring_access_checks ();
13445
13446   /* Parse the declarator.  */
13447   token = cp_lexer_peek_token (parser->lexer);
13448   declarator
13449     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13450                             &ctor_dtor_or_conv_p,
13451                             /*parenthesized_p=*/NULL,
13452                             /*member_p=*/false);
13453   /* Gather up the deferred checks.  */
13454   stop_deferring_access_checks ();
13455
13456   /* If the DECLARATOR was erroneous, there's no need to go
13457      further.  */
13458   if (declarator == cp_error_declarator)
13459     return error_mark_node;
13460
13461   /* Check that the number of template-parameter-lists is OK.  */
13462   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13463                                                        token->location))
13464     return error_mark_node;
13465
13466   if (declares_class_or_enum & 2)
13467     cp_parser_check_for_definition_in_return_type (declarator,
13468                                                    decl_specifiers->type,
13469                                                    decl_specifiers->type_location);
13470
13471   /* Figure out what scope the entity declared by the DECLARATOR is
13472      located in.  `grokdeclarator' sometimes changes the scope, so
13473      we compute it now.  */
13474   scope = get_scope_of_declarator (declarator);
13475
13476   /* If we're allowing GNU extensions, look for an asm-specification
13477      and attributes.  */
13478   if (cp_parser_allow_gnu_extensions_p (parser))
13479     {
13480       /* Look for an asm-specification.  */
13481       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13482       asm_specification = cp_parser_asm_specification_opt (parser);
13483       /* And attributes.  */
13484       attributes_start_token = cp_lexer_peek_token (parser->lexer);
13485       attributes = cp_parser_attributes_opt (parser);
13486     }
13487   else
13488     {
13489       asm_specification = NULL_TREE;
13490       attributes = NULL_TREE;
13491     }
13492
13493   /* Peek at the next token.  */
13494   token = cp_lexer_peek_token (parser->lexer);
13495   /* Check to see if the token indicates the start of a
13496      function-definition.  */
13497   if (function_declarator_p (declarator)
13498       && cp_parser_token_starts_function_definition_p (token))
13499     {
13500       if (!function_definition_allowed_p)
13501         {
13502           /* If a function-definition should not appear here, issue an
13503              error message.  */
13504           cp_parser_error (parser,
13505                            "a function-definition is not allowed here");
13506           return error_mark_node;
13507         }
13508       else
13509         {
13510           location_t func_brace_location
13511             = cp_lexer_peek_token (parser->lexer)->location;
13512
13513           /* Neither attributes nor an asm-specification are allowed
13514              on a function-definition.  */
13515           if (asm_specification)
13516             error_at (asm_spec_start_token->location,
13517                       "an asm-specification is not allowed "
13518                       "on a function-definition");
13519           if (attributes)
13520             error_at (attributes_start_token->location,
13521                       "attributes are not allowed on a function-definition");
13522           /* This is a function-definition.  */
13523           *function_definition_p = true;
13524
13525           /* Parse the function definition.  */
13526           if (member_p)
13527             decl = cp_parser_save_member_function_body (parser,
13528                                                         decl_specifiers,
13529                                                         declarator,
13530                                                         prefix_attributes);
13531           else
13532             decl
13533               = (cp_parser_function_definition_from_specifiers_and_declarator
13534                  (parser, decl_specifiers, prefix_attributes, declarator));
13535
13536           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13537             {
13538               /* This is where the prologue starts...  */
13539               DECL_STRUCT_FUNCTION (decl)->function_start_locus
13540                 = func_brace_location;
13541             }
13542
13543           return decl;
13544         }
13545     }
13546
13547   /* [dcl.dcl]
13548
13549      Only in function declarations for constructors, destructors, and
13550      type conversions can the decl-specifier-seq be omitted.
13551
13552      We explicitly postpone this check past the point where we handle
13553      function-definitions because we tolerate function-definitions
13554      that are missing their return types in some modes.  */
13555   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13556     {
13557       cp_parser_error (parser,
13558                        "expected constructor, destructor, or type conversion");
13559       return error_mark_node;
13560     }
13561
13562   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
13563   if (token->type == CPP_EQ
13564       || token->type == CPP_OPEN_PAREN
13565       || token->type == CPP_OPEN_BRACE)
13566     {
13567       is_initialized = SD_INITIALIZED;
13568       initialization_kind = token->type;
13569
13570       if (token->type == CPP_EQ
13571           && function_declarator_p (declarator))
13572         {
13573           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13574           if (t2->keyword == RID_DEFAULT)
13575             is_initialized = SD_DEFAULTED;
13576           else if (t2->keyword == RID_DELETE)
13577             is_initialized = SD_DELETED;
13578         }
13579     }
13580   else
13581     {
13582       /* If the init-declarator isn't initialized and isn't followed by a
13583          `,' or `;', it's not a valid init-declarator.  */
13584       if (token->type != CPP_COMMA
13585           && token->type != CPP_SEMICOLON)
13586         {
13587           cp_parser_error (parser, "expected initializer");
13588           return error_mark_node;
13589         }
13590       is_initialized = SD_UNINITIALIZED;
13591       initialization_kind = CPP_EOF;
13592     }
13593
13594   /* Because start_decl has side-effects, we should only call it if we
13595      know we're going ahead.  By this point, we know that we cannot
13596      possibly be looking at any other construct.  */
13597   cp_parser_commit_to_tentative_parse (parser);
13598
13599   /* If the decl specifiers were bad, issue an error now that we're
13600      sure this was intended to be a declarator.  Then continue
13601      declaring the variable(s), as int, to try to cut down on further
13602      errors.  */
13603   if (decl_specifiers->any_specifiers_p
13604       && decl_specifiers->type == error_mark_node)
13605     {
13606       cp_parser_error (parser, "invalid type in declaration");
13607       decl_specifiers->type = integer_type_node;
13608     }
13609
13610   /* Check to see whether or not this declaration is a friend.  */
13611   friend_p = cp_parser_friend_p (decl_specifiers);
13612
13613   /* Enter the newly declared entry in the symbol table.  If we're
13614      processing a declaration in a class-specifier, we wait until
13615      after processing the initializer.  */
13616   if (!member_p)
13617     {
13618       if (parser->in_unbraced_linkage_specification_p)
13619         decl_specifiers->storage_class = sc_extern;
13620       decl = start_decl (declarator, decl_specifiers,
13621                          is_initialized, attributes, prefix_attributes,
13622                          &pushed_scope);
13623     }
13624   else if (scope)
13625     /* Enter the SCOPE.  That way unqualified names appearing in the
13626        initializer will be looked up in SCOPE.  */
13627     pushed_scope = push_scope (scope);
13628
13629   /* Perform deferred access control checks, now that we know in which
13630      SCOPE the declared entity resides.  */
13631   if (!member_p && decl)
13632     {
13633       tree saved_current_function_decl = NULL_TREE;
13634
13635       /* If the entity being declared is a function, pretend that we
13636          are in its scope.  If it is a `friend', it may have access to
13637          things that would not otherwise be accessible.  */
13638       if (TREE_CODE (decl) == FUNCTION_DECL)
13639         {
13640           saved_current_function_decl = current_function_decl;
13641           current_function_decl = decl;
13642         }
13643
13644       /* Perform access checks for template parameters.  */
13645       cp_parser_perform_template_parameter_access_checks (checks);
13646
13647       /* Perform the access control checks for the declarator and the
13648          decl-specifiers.  */
13649       perform_deferred_access_checks ();
13650
13651       /* Restore the saved value.  */
13652       if (TREE_CODE (decl) == FUNCTION_DECL)
13653         current_function_decl = saved_current_function_decl;
13654     }
13655
13656   /* Parse the initializer.  */
13657   initializer = NULL_TREE;
13658   is_direct_init = false;
13659   is_non_constant_init = true;
13660   if (is_initialized)
13661     {
13662       if (function_declarator_p (declarator))
13663         {
13664           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13665            if (initialization_kind == CPP_EQ)
13666              initializer = cp_parser_pure_specifier (parser);
13667            else
13668              {
13669                /* If the declaration was erroneous, we don't really
13670                   know what the user intended, so just silently
13671                   consume the initializer.  */
13672                if (decl != error_mark_node)
13673                  error_at (initializer_start_token->location,
13674                            "initializer provided for function");
13675                cp_parser_skip_to_closing_parenthesis (parser,
13676                                                       /*recovering=*/true,
13677                                                       /*or_comma=*/false,
13678                                                       /*consume_paren=*/true);
13679              }
13680         }
13681       else
13682         {
13683           /* We want to record the extra mangling scope for in-class
13684              initializers of class members and initializers of static data
13685              member templates.  The former is a C++0x feature which isn't
13686              implemented yet, and I expect it will involve deferring
13687              parsing of the initializer until end of class as with default
13688              arguments.  So right here we only handle the latter.  */
13689           if (!member_p && processing_template_decl)
13690             start_lambda_scope (decl);
13691           initializer = cp_parser_initializer (parser,
13692                                                &is_direct_init,
13693                                                &is_non_constant_init);
13694           if (!member_p && processing_template_decl)
13695             finish_lambda_scope ();
13696         }
13697     }
13698
13699   /* The old parser allows attributes to appear after a parenthesized
13700      initializer.  Mark Mitchell proposed removing this functionality
13701      on the GCC mailing lists on 2002-08-13.  This parser accepts the
13702      attributes -- but ignores them.  */
13703   if (cp_parser_allow_gnu_extensions_p (parser)
13704       && initialization_kind == CPP_OPEN_PAREN)
13705     if (cp_parser_attributes_opt (parser))
13706       warning (OPT_Wattributes,
13707                "attributes after parenthesized initializer ignored");
13708
13709   /* For an in-class declaration, use `grokfield' to create the
13710      declaration.  */
13711   if (member_p)
13712     {
13713       if (pushed_scope)
13714         {
13715           pop_scope (pushed_scope);
13716           pushed_scope = false;
13717         }
13718       decl = grokfield (declarator, decl_specifiers,
13719                         initializer, !is_non_constant_init,
13720                         /*asmspec=*/NULL_TREE,
13721                         prefix_attributes);
13722       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13723         cp_parser_save_default_args (parser, decl);
13724     }
13725
13726   /* Finish processing the declaration.  But, skip friend
13727      declarations.  */
13728   if (!friend_p && decl && decl != error_mark_node)
13729     {
13730       cp_finish_decl (decl,
13731                       initializer, !is_non_constant_init,
13732                       asm_specification,
13733                       /* If the initializer is in parentheses, then this is
13734                          a direct-initialization, which means that an
13735                          `explicit' constructor is OK.  Otherwise, an
13736                          `explicit' constructor cannot be used.  */
13737                       ((is_direct_init || !is_initialized)
13738                        ? 0 : LOOKUP_ONLYCONVERTING));
13739     }
13740   else if ((cxx_dialect != cxx98) && friend_p
13741            && decl && TREE_CODE (decl) == FUNCTION_DECL)
13742     /* Core issue #226 (C++0x only): A default template-argument
13743        shall not be specified in a friend class template
13744        declaration. */
13745     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
13746                              /*is_partial=*/0, /*is_friend_decl=*/1);
13747
13748   if (!friend_p && pushed_scope)
13749     pop_scope (pushed_scope);
13750
13751   return decl;
13752 }
13753
13754 /* Parse a declarator.
13755
13756    declarator:
13757      direct-declarator
13758      ptr-operator declarator
13759
13760    abstract-declarator:
13761      ptr-operator abstract-declarator [opt]
13762      direct-abstract-declarator
13763
13764    GNU Extensions:
13765
13766    declarator:
13767      attributes [opt] direct-declarator
13768      attributes [opt] ptr-operator declarator
13769
13770    abstract-declarator:
13771      attributes [opt] ptr-operator abstract-declarator [opt]
13772      attributes [opt] direct-abstract-declarator
13773
13774    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13775    detect constructor, destructor or conversion operators. It is set
13776    to -1 if the declarator is a name, and +1 if it is a
13777    function. Otherwise it is set to zero. Usually you just want to
13778    test for >0, but internally the negative value is used.
13779
13780    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13781    a decl-specifier-seq unless it declares a constructor, destructor,
13782    or conversion.  It might seem that we could check this condition in
13783    semantic analysis, rather than parsing, but that makes it difficult
13784    to handle something like `f()'.  We want to notice that there are
13785    no decl-specifiers, and therefore realize that this is an
13786    expression, not a declaration.)
13787
13788    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13789    the declarator is a direct-declarator of the form "(...)".
13790
13791    MEMBER_P is true iff this declarator is a member-declarator.  */
13792
13793 static cp_declarator *
13794 cp_parser_declarator (cp_parser* parser,
13795                       cp_parser_declarator_kind dcl_kind,
13796                       int* ctor_dtor_or_conv_p,
13797                       bool* parenthesized_p,
13798                       bool member_p)
13799 {
13800   cp_declarator *declarator;
13801   enum tree_code code;
13802   cp_cv_quals cv_quals;
13803   tree class_type;
13804   tree attributes = NULL_TREE;
13805
13806   /* Assume this is not a constructor, destructor, or type-conversion
13807      operator.  */
13808   if (ctor_dtor_or_conv_p)
13809     *ctor_dtor_or_conv_p = 0;
13810
13811   if (cp_parser_allow_gnu_extensions_p (parser))
13812     attributes = cp_parser_attributes_opt (parser);
13813
13814   /* Check for the ptr-operator production.  */
13815   cp_parser_parse_tentatively (parser);
13816   /* Parse the ptr-operator.  */
13817   code = cp_parser_ptr_operator (parser,
13818                                  &class_type,
13819                                  &cv_quals);
13820   /* If that worked, then we have a ptr-operator.  */
13821   if (cp_parser_parse_definitely (parser))
13822     {
13823       /* If a ptr-operator was found, then this declarator was not
13824          parenthesized.  */
13825       if (parenthesized_p)
13826         *parenthesized_p = true;
13827       /* The dependent declarator is optional if we are parsing an
13828          abstract-declarator.  */
13829       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13830         cp_parser_parse_tentatively (parser);
13831
13832       /* Parse the dependent declarator.  */
13833       declarator = cp_parser_declarator (parser, dcl_kind,
13834                                          /*ctor_dtor_or_conv_p=*/NULL,
13835                                          /*parenthesized_p=*/NULL,
13836                                          /*member_p=*/false);
13837
13838       /* If we are parsing an abstract-declarator, we must handle the
13839          case where the dependent declarator is absent.  */
13840       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13841           && !cp_parser_parse_definitely (parser))
13842         declarator = NULL;
13843
13844       declarator = cp_parser_make_indirect_declarator
13845         (code, class_type, cv_quals, declarator);
13846     }
13847   /* Everything else is a direct-declarator.  */
13848   else
13849     {
13850       if (parenthesized_p)
13851         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13852                                                    CPP_OPEN_PAREN);
13853       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13854                                                 ctor_dtor_or_conv_p,
13855                                                 member_p);
13856     }
13857
13858   if (attributes && declarator && declarator != cp_error_declarator)
13859     declarator->attributes = attributes;
13860
13861   return declarator;
13862 }
13863
13864 /* Parse a direct-declarator or direct-abstract-declarator.
13865
13866    direct-declarator:
13867      declarator-id
13868      direct-declarator ( parameter-declaration-clause )
13869        cv-qualifier-seq [opt]
13870        exception-specification [opt]
13871      direct-declarator [ constant-expression [opt] ]
13872      ( declarator )
13873
13874    direct-abstract-declarator:
13875      direct-abstract-declarator [opt]
13876        ( parameter-declaration-clause )
13877        cv-qualifier-seq [opt]
13878        exception-specification [opt]
13879      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13880      ( abstract-declarator )
13881
13882    Returns a representation of the declarator.  DCL_KIND is
13883    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13884    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13885    we are parsing a direct-declarator.  It is
13886    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13887    of ambiguity we prefer an abstract declarator, as per
13888    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13889    cp_parser_declarator.  */
13890
13891 static cp_declarator *
13892 cp_parser_direct_declarator (cp_parser* parser,
13893                              cp_parser_declarator_kind dcl_kind,
13894                              int* ctor_dtor_or_conv_p,
13895                              bool member_p)
13896 {
13897   cp_token *token;
13898   cp_declarator *declarator = NULL;
13899   tree scope = NULL_TREE;
13900   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13901   bool saved_in_declarator_p = parser->in_declarator_p;
13902   bool first = true;
13903   tree pushed_scope = NULL_TREE;
13904
13905   while (true)
13906     {
13907       /* Peek at the next token.  */
13908       token = cp_lexer_peek_token (parser->lexer);
13909       if (token->type == CPP_OPEN_PAREN)
13910         {
13911           /* This is either a parameter-declaration-clause, or a
13912              parenthesized declarator. When we know we are parsing a
13913              named declarator, it must be a parenthesized declarator
13914              if FIRST is true. For instance, `(int)' is a
13915              parameter-declaration-clause, with an omitted
13916              direct-abstract-declarator. But `((*))', is a
13917              parenthesized abstract declarator. Finally, when T is a
13918              template parameter `(T)' is a
13919              parameter-declaration-clause, and not a parenthesized
13920              named declarator.
13921
13922              We first try and parse a parameter-declaration-clause,
13923              and then try a nested declarator (if FIRST is true).
13924
13925              It is not an error for it not to be a
13926              parameter-declaration-clause, even when FIRST is
13927              false. Consider,
13928
13929                int i (int);
13930                int i (3);
13931
13932              The first is the declaration of a function while the
13933              second is the definition of a variable, including its
13934              initializer.
13935
13936              Having seen only the parenthesis, we cannot know which of
13937              these two alternatives should be selected.  Even more
13938              complex are examples like:
13939
13940                int i (int (a));
13941                int i (int (3));
13942
13943              The former is a function-declaration; the latter is a
13944              variable initialization.
13945
13946              Thus again, we try a parameter-declaration-clause, and if
13947              that fails, we back out and return.  */
13948
13949           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13950             {
13951               tree params;
13952               unsigned saved_num_template_parameter_lists;
13953               bool is_declarator = false;
13954               tree t;
13955
13956               /* In a member-declarator, the only valid interpretation
13957                  of a parenthesis is the start of a
13958                  parameter-declaration-clause.  (It is invalid to
13959                  initialize a static data member with a parenthesized
13960                  initializer; only the "=" form of initialization is
13961                  permitted.)  */
13962               if (!member_p)
13963                 cp_parser_parse_tentatively (parser);
13964
13965               /* Consume the `('.  */
13966               cp_lexer_consume_token (parser->lexer);
13967               if (first)
13968                 {
13969                   /* If this is going to be an abstract declarator, we're
13970                      in a declarator and we can't have default args.  */
13971                   parser->default_arg_ok_p = false;
13972                   parser->in_declarator_p = true;
13973                 }
13974
13975               /* Inside the function parameter list, surrounding
13976                  template-parameter-lists do not apply.  */
13977               saved_num_template_parameter_lists
13978                 = parser->num_template_parameter_lists;
13979               parser->num_template_parameter_lists = 0;
13980
13981               begin_scope (sk_function_parms, NULL_TREE);
13982
13983               /* Parse the parameter-declaration-clause.  */
13984               params = cp_parser_parameter_declaration_clause (parser);
13985
13986               parser->num_template_parameter_lists
13987                 = saved_num_template_parameter_lists;
13988
13989               /* If all went well, parse the cv-qualifier-seq and the
13990                  exception-specification.  */
13991               if (member_p || cp_parser_parse_definitely (parser))
13992                 {
13993                   cp_cv_quals cv_quals;
13994                   tree exception_specification;
13995                   tree late_return;
13996
13997                   is_declarator = true;
13998
13999                   if (ctor_dtor_or_conv_p)
14000                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14001                   first = false;
14002                   /* Consume the `)'.  */
14003                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
14004
14005                   /* Parse the cv-qualifier-seq.  */
14006                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14007                   /* And the exception-specification.  */
14008                   exception_specification
14009                     = cp_parser_exception_specification_opt (parser);
14010
14011                   late_return
14012                     = cp_parser_late_return_type_opt (parser);
14013
14014                   /* Create the function-declarator.  */
14015                   declarator = make_call_declarator (declarator,
14016                                                      params,
14017                                                      cv_quals,
14018                                                      exception_specification,
14019                                                      late_return);
14020                   /* Any subsequent parameter lists are to do with
14021                      return type, so are not those of the declared
14022                      function.  */
14023                   parser->default_arg_ok_p = false;
14024                 }
14025
14026               /* Remove the function parms from scope.  */
14027               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14028                 pop_binding (DECL_NAME (t), t);
14029               leave_scope();
14030
14031               if (is_declarator)
14032                 /* Repeat the main loop.  */
14033                 continue;
14034             }
14035
14036           /* If this is the first, we can try a parenthesized
14037              declarator.  */
14038           if (first)
14039             {
14040               bool saved_in_type_id_in_expr_p;
14041
14042               parser->default_arg_ok_p = saved_default_arg_ok_p;
14043               parser->in_declarator_p = saved_in_declarator_p;
14044
14045               /* Consume the `('.  */
14046               cp_lexer_consume_token (parser->lexer);
14047               /* Parse the nested declarator.  */
14048               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14049               parser->in_type_id_in_expr_p = true;
14050               declarator
14051                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14052                                         /*parenthesized_p=*/NULL,
14053                                         member_p);
14054               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14055               first = false;
14056               /* Expect a `)'.  */
14057               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14058                 declarator = cp_error_declarator;
14059               if (declarator == cp_error_declarator)
14060                 break;
14061
14062               goto handle_declarator;
14063             }
14064           /* Otherwise, we must be done.  */
14065           else
14066             break;
14067         }
14068       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14069                && token->type == CPP_OPEN_SQUARE)
14070         {
14071           /* Parse an array-declarator.  */
14072           tree bounds;
14073
14074           if (ctor_dtor_or_conv_p)
14075             *ctor_dtor_or_conv_p = 0;
14076
14077           first = false;
14078           parser->default_arg_ok_p = false;
14079           parser->in_declarator_p = true;
14080           /* Consume the `['.  */
14081           cp_lexer_consume_token (parser->lexer);
14082           /* Peek at the next token.  */
14083           token = cp_lexer_peek_token (parser->lexer);
14084           /* If the next token is `]', then there is no
14085              constant-expression.  */
14086           if (token->type != CPP_CLOSE_SQUARE)
14087             {
14088               bool non_constant_p;
14089
14090               bounds
14091                 = cp_parser_constant_expression (parser,
14092                                                  /*allow_non_constant=*/true,
14093                                                  &non_constant_p);
14094               if (!non_constant_p)
14095                 bounds = fold_non_dependent_expr (bounds);
14096               /* Normally, the array bound must be an integral constant
14097                  expression.  However, as an extension, we allow VLAs
14098                  in function scopes.  */
14099               else if (!parser->in_function_body)
14100                 {
14101                   error_at (token->location,
14102                             "array bound is not an integer constant");
14103                   bounds = error_mark_node;
14104                 }
14105               else if (processing_template_decl && !error_operand_p (bounds))
14106                 {
14107                   /* Remember this wasn't a constant-expression.  */
14108                   bounds = build_nop (TREE_TYPE (bounds), bounds);
14109                   TREE_SIDE_EFFECTS (bounds) = 1;
14110                 }
14111             }
14112           else
14113             bounds = NULL_TREE;
14114           /* Look for the closing `]'.  */
14115           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14116             {
14117               declarator = cp_error_declarator;
14118               break;
14119             }
14120
14121           declarator = make_array_declarator (declarator, bounds);
14122         }
14123       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14124         {
14125           {
14126             tree qualifying_scope;
14127             tree unqualified_name;
14128             special_function_kind sfk;
14129             bool abstract_ok;
14130             bool pack_expansion_p = false;
14131             cp_token *declarator_id_start_token;
14132
14133             /* Parse a declarator-id */
14134             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14135             if (abstract_ok)
14136               {
14137                 cp_parser_parse_tentatively (parser);
14138
14139                 /* If we see an ellipsis, we should be looking at a
14140                    parameter pack. */
14141                 if (token->type == CPP_ELLIPSIS)
14142                   {
14143                     /* Consume the `...' */
14144                     cp_lexer_consume_token (parser->lexer);
14145
14146                     pack_expansion_p = true;
14147                   }
14148               }
14149
14150             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14151             unqualified_name
14152               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14153             qualifying_scope = parser->scope;
14154             if (abstract_ok)
14155               {
14156                 bool okay = false;
14157
14158                 if (!unqualified_name && pack_expansion_p)
14159                   {
14160                     /* Check whether an error occurred. */
14161                     okay = !cp_parser_error_occurred (parser);
14162
14163                     /* We already consumed the ellipsis to mark a
14164                        parameter pack, but we have no way to report it,
14165                        so abort the tentative parse. We will be exiting
14166                        immediately anyway. */
14167                     cp_parser_abort_tentative_parse (parser);
14168                   }
14169                 else
14170                   okay = cp_parser_parse_definitely (parser);
14171
14172                 if (!okay)
14173                   unqualified_name = error_mark_node;
14174                 else if (unqualified_name
14175                          && (qualifying_scope
14176                              || (TREE_CODE (unqualified_name)
14177                                  != IDENTIFIER_NODE)))
14178                   {
14179                     cp_parser_error (parser, "expected unqualified-id");
14180                     unqualified_name = error_mark_node;
14181                   }
14182               }
14183
14184             if (!unqualified_name)
14185               return NULL;
14186             if (unqualified_name == error_mark_node)
14187               {
14188                 declarator = cp_error_declarator;
14189                 pack_expansion_p = false;
14190                 declarator->parameter_pack_p = false;
14191                 break;
14192               }
14193
14194             if (qualifying_scope && at_namespace_scope_p ()
14195                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14196               {
14197                 /* In the declaration of a member of a template class
14198                    outside of the class itself, the SCOPE will sometimes
14199                    be a TYPENAME_TYPE.  For example, given:
14200
14201                    template <typename T>
14202                    int S<T>::R::i = 3;
14203
14204                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
14205                    this context, we must resolve S<T>::R to an ordinary
14206                    type, rather than a typename type.
14207
14208                    The reason we normally avoid resolving TYPENAME_TYPEs
14209                    is that a specialization of `S' might render
14210                    `S<T>::R' not a type.  However, if `S' is
14211                    specialized, then this `i' will not be used, so there
14212                    is no harm in resolving the types here.  */
14213                 tree type;
14214
14215                 /* Resolve the TYPENAME_TYPE.  */
14216                 type = resolve_typename_type (qualifying_scope,
14217                                               /*only_current_p=*/false);
14218                 /* If that failed, the declarator is invalid.  */
14219                 if (TREE_CODE (type) == TYPENAME_TYPE)
14220                   {
14221                     if (typedef_variant_p (type))
14222                       error_at (declarator_id_start_token->location,
14223                                 "cannot define member of dependent typedef "
14224                                 "%qT", type);
14225                     else
14226                       error_at (declarator_id_start_token->location,
14227                                 "%<%T::%E%> is not a type",
14228                                 TYPE_CONTEXT (qualifying_scope),
14229                                 TYPE_IDENTIFIER (qualifying_scope));
14230                   }
14231                 qualifying_scope = type;
14232               }
14233
14234             sfk = sfk_none;
14235
14236             if (unqualified_name)
14237               {
14238                 tree class_type;
14239
14240                 if (qualifying_scope
14241                     && CLASS_TYPE_P (qualifying_scope))
14242                   class_type = qualifying_scope;
14243                 else
14244                   class_type = current_class_type;
14245
14246                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14247                   {
14248                     tree name_type = TREE_TYPE (unqualified_name);
14249                     if (class_type && same_type_p (name_type, class_type))
14250                       {
14251                         if (qualifying_scope
14252                             && CLASSTYPE_USE_TEMPLATE (name_type))
14253                           {
14254                             error_at (declarator_id_start_token->location,
14255                                       "invalid use of constructor as a template");
14256                             inform (declarator_id_start_token->location,
14257                                     "use %<%T::%D%> instead of %<%T::%D%> to "
14258                                     "name the constructor in a qualified name",
14259                                     class_type,
14260                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14261                                     class_type, name_type);
14262                             declarator = cp_error_declarator;
14263                             break;
14264                           }
14265                         else
14266                           unqualified_name = constructor_name (class_type);
14267                       }
14268                     else
14269                       {
14270                         /* We do not attempt to print the declarator
14271                            here because we do not have enough
14272                            information about its original syntactic
14273                            form.  */
14274                         cp_parser_error (parser, "invalid declarator");
14275                         declarator = cp_error_declarator;
14276                         break;
14277                       }
14278                   }
14279
14280                 if (class_type)
14281                   {
14282                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14283                       sfk = sfk_destructor;
14284                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14285                       sfk = sfk_conversion;
14286                     else if (/* There's no way to declare a constructor
14287                                 for an anonymous type, even if the type
14288                                 got a name for linkage purposes.  */
14289                              !TYPE_WAS_ANONYMOUS (class_type)
14290                              && constructor_name_p (unqualified_name,
14291                                                     class_type))
14292                       {
14293                         unqualified_name = constructor_name (class_type);
14294                         sfk = sfk_constructor;
14295                       }
14296                     else if (is_overloaded_fn (unqualified_name)
14297                              && DECL_CONSTRUCTOR_P (get_first_fn
14298                                                     (unqualified_name)))
14299                       sfk = sfk_constructor;
14300
14301                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
14302                       *ctor_dtor_or_conv_p = -1;
14303                   }
14304               }
14305             declarator = make_id_declarator (qualifying_scope,
14306                                              unqualified_name,
14307                                              sfk);
14308             declarator->id_loc = token->location;
14309             declarator->parameter_pack_p = pack_expansion_p;
14310
14311             if (pack_expansion_p)
14312               maybe_warn_variadic_templates ();
14313           }
14314
14315         handle_declarator:;
14316           scope = get_scope_of_declarator (declarator);
14317           if (scope)
14318             /* Any names that appear after the declarator-id for a
14319                member are looked up in the containing scope.  */
14320             pushed_scope = push_scope (scope);
14321           parser->in_declarator_p = true;
14322           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14323               || (declarator && declarator->kind == cdk_id))
14324             /* Default args are only allowed on function
14325                declarations.  */
14326             parser->default_arg_ok_p = saved_default_arg_ok_p;
14327           else
14328             parser->default_arg_ok_p = false;
14329
14330           first = false;
14331         }
14332       /* We're done.  */
14333       else
14334         break;
14335     }
14336
14337   /* For an abstract declarator, we might wind up with nothing at this
14338      point.  That's an error; the declarator is not optional.  */
14339   if (!declarator)
14340     cp_parser_error (parser, "expected declarator");
14341
14342   /* If we entered a scope, we must exit it now.  */
14343   if (pushed_scope)
14344     pop_scope (pushed_scope);
14345
14346   parser->default_arg_ok_p = saved_default_arg_ok_p;
14347   parser->in_declarator_p = saved_in_declarator_p;
14348
14349   return declarator;
14350 }
14351
14352 /* Parse a ptr-operator.
14353
14354    ptr-operator:
14355      * cv-qualifier-seq [opt]
14356      &
14357      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14358
14359    GNU Extension:
14360
14361    ptr-operator:
14362      & cv-qualifier-seq [opt]
14363
14364    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14365    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14366    an rvalue reference. In the case of a pointer-to-member, *TYPE is
14367    filled in with the TYPE containing the member.  *CV_QUALS is
14368    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14369    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
14370    Note that the tree codes returned by this function have nothing
14371    to do with the types of trees that will be eventually be created
14372    to represent the pointer or reference type being parsed. They are
14373    just constants with suggestive names. */
14374 static enum tree_code
14375 cp_parser_ptr_operator (cp_parser* parser,
14376                         tree* type,
14377                         cp_cv_quals *cv_quals)
14378 {
14379   enum tree_code code = ERROR_MARK;
14380   cp_token *token;
14381
14382   /* Assume that it's not a pointer-to-member.  */
14383   *type = NULL_TREE;
14384   /* And that there are no cv-qualifiers.  */
14385   *cv_quals = TYPE_UNQUALIFIED;
14386
14387   /* Peek at the next token.  */
14388   token = cp_lexer_peek_token (parser->lexer);
14389
14390   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
14391   if (token->type == CPP_MULT)
14392     code = INDIRECT_REF;
14393   else if (token->type == CPP_AND)
14394     code = ADDR_EXPR;
14395   else if ((cxx_dialect != cxx98) &&
14396            token->type == CPP_AND_AND) /* C++0x only */
14397     code = NON_LVALUE_EXPR;
14398
14399   if (code != ERROR_MARK)
14400     {
14401       /* Consume the `*', `&' or `&&'.  */
14402       cp_lexer_consume_token (parser->lexer);
14403
14404       /* A `*' can be followed by a cv-qualifier-seq, and so can a
14405          `&', if we are allowing GNU extensions.  (The only qualifier
14406          that can legally appear after `&' is `restrict', but that is
14407          enforced during semantic analysis.  */
14408       if (code == INDIRECT_REF
14409           || cp_parser_allow_gnu_extensions_p (parser))
14410         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14411     }
14412   else
14413     {
14414       /* Try the pointer-to-member case.  */
14415       cp_parser_parse_tentatively (parser);
14416       /* Look for the optional `::' operator.  */
14417       cp_parser_global_scope_opt (parser,
14418                                   /*current_scope_valid_p=*/false);
14419       /* Look for the nested-name specifier.  */
14420       token = cp_lexer_peek_token (parser->lexer);
14421       cp_parser_nested_name_specifier (parser,
14422                                        /*typename_keyword_p=*/false,
14423                                        /*check_dependency_p=*/true,
14424                                        /*type_p=*/false,
14425                                        /*is_declaration=*/false);
14426       /* If we found it, and the next token is a `*', then we are
14427          indeed looking at a pointer-to-member operator.  */
14428       if (!cp_parser_error_occurred (parser)
14429           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14430         {
14431           /* Indicate that the `*' operator was used.  */
14432           code = INDIRECT_REF;
14433
14434           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14435             error_at (token->location, "%qD is a namespace", parser->scope);
14436           else
14437             {
14438               /* The type of which the member is a member is given by the
14439                  current SCOPE.  */
14440               *type = parser->scope;
14441               /* The next name will not be qualified.  */
14442               parser->scope = NULL_TREE;
14443               parser->qualifying_scope = NULL_TREE;
14444               parser->object_scope = NULL_TREE;
14445               /* Look for the optional cv-qualifier-seq.  */
14446               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14447             }
14448         }
14449       /* If that didn't work we don't have a ptr-operator.  */
14450       if (!cp_parser_parse_definitely (parser))
14451         cp_parser_error (parser, "expected ptr-operator");
14452     }
14453
14454   return code;
14455 }
14456
14457 /* Parse an (optional) cv-qualifier-seq.
14458
14459    cv-qualifier-seq:
14460      cv-qualifier cv-qualifier-seq [opt]
14461
14462    cv-qualifier:
14463      const
14464      volatile
14465
14466    GNU Extension:
14467
14468    cv-qualifier:
14469      __restrict__
14470
14471    Returns a bitmask representing the cv-qualifiers.  */
14472
14473 static cp_cv_quals
14474 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14475 {
14476   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14477
14478   while (true)
14479     {
14480       cp_token *token;
14481       cp_cv_quals cv_qualifier;
14482
14483       /* Peek at the next token.  */
14484       token = cp_lexer_peek_token (parser->lexer);
14485       /* See if it's a cv-qualifier.  */
14486       switch (token->keyword)
14487         {
14488         case RID_CONST:
14489           cv_qualifier = TYPE_QUAL_CONST;
14490           break;
14491
14492         case RID_VOLATILE:
14493           cv_qualifier = TYPE_QUAL_VOLATILE;
14494           break;
14495
14496         case RID_RESTRICT:
14497           cv_qualifier = TYPE_QUAL_RESTRICT;
14498           break;
14499
14500         default:
14501           cv_qualifier = TYPE_UNQUALIFIED;
14502           break;
14503         }
14504
14505       if (!cv_qualifier)
14506         break;
14507
14508       if (cv_quals & cv_qualifier)
14509         {
14510           error_at (token->location, "duplicate cv-qualifier");
14511           cp_lexer_purge_token (parser->lexer);
14512         }
14513       else
14514         {
14515           cp_lexer_consume_token (parser->lexer);
14516           cv_quals |= cv_qualifier;
14517         }
14518     }
14519
14520   return cv_quals;
14521 }
14522
14523 /* Parse a late-specified return type, if any.  This is not a separate
14524    non-terminal, but part of a function declarator, which looks like
14525
14526    -> trailing-type-specifier-seq abstract-declarator(opt)
14527
14528    Returns the type indicated by the type-id.  */
14529
14530 static tree
14531 cp_parser_late_return_type_opt (cp_parser* parser)
14532 {
14533   cp_token *token;
14534
14535   /* Peek at the next token.  */
14536   token = cp_lexer_peek_token (parser->lexer);
14537   /* A late-specified return type is indicated by an initial '->'. */
14538   if (token->type != CPP_DEREF)
14539     return NULL_TREE;
14540
14541   /* Consume the ->.  */
14542   cp_lexer_consume_token (parser->lexer);
14543
14544   return cp_parser_trailing_type_id (parser);
14545 }
14546
14547 /* Parse a declarator-id.
14548
14549    declarator-id:
14550      id-expression
14551      :: [opt] nested-name-specifier [opt] type-name
14552
14553    In the `id-expression' case, the value returned is as for
14554    cp_parser_id_expression if the id-expression was an unqualified-id.
14555    If the id-expression was a qualified-id, then a SCOPE_REF is
14556    returned.  The first operand is the scope (either a NAMESPACE_DECL
14557    or TREE_TYPE), but the second is still just a representation of an
14558    unqualified-id.  */
14559
14560 static tree
14561 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14562 {
14563   tree id;
14564   /* The expression must be an id-expression.  Assume that qualified
14565      names are the names of types so that:
14566
14567        template <class T>
14568        int S<T>::R::i = 3;
14569
14570      will work; we must treat `S<T>::R' as the name of a type.
14571      Similarly, assume that qualified names are templates, where
14572      required, so that:
14573
14574        template <class T>
14575        int S<T>::R<T>::i = 3;
14576
14577      will work, too.  */
14578   id = cp_parser_id_expression (parser,
14579                                 /*template_keyword_p=*/false,
14580                                 /*check_dependency_p=*/false,
14581                                 /*template_p=*/NULL,
14582                                 /*declarator_p=*/true,
14583                                 optional_p);
14584   if (id && BASELINK_P (id))
14585     id = BASELINK_FUNCTIONS (id);
14586   return id;
14587 }
14588
14589 /* Parse a type-id.
14590
14591    type-id:
14592      type-specifier-seq abstract-declarator [opt]
14593
14594    Returns the TYPE specified.  */
14595
14596 static tree
14597 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14598                      bool is_trailing_return)
14599 {
14600   cp_decl_specifier_seq type_specifier_seq;
14601   cp_declarator *abstract_declarator;
14602
14603   /* Parse the type-specifier-seq.  */
14604   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14605                                 is_trailing_return,
14606                                 &type_specifier_seq);
14607   if (type_specifier_seq.type == error_mark_node)
14608     return error_mark_node;
14609
14610   /* There might or might not be an abstract declarator.  */
14611   cp_parser_parse_tentatively (parser);
14612   /* Look for the declarator.  */
14613   abstract_declarator
14614     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14615                             /*parenthesized_p=*/NULL,
14616                             /*member_p=*/false);
14617   /* Check to see if there really was a declarator.  */
14618   if (!cp_parser_parse_definitely (parser))
14619     abstract_declarator = NULL;
14620
14621   if (type_specifier_seq.type
14622       && type_uses_auto (type_specifier_seq.type))
14623     {
14624       /* A type-id with type 'auto' is only ok if the abstract declarator
14625          is a function declarator with a late-specified return type.  */
14626       if (abstract_declarator
14627           && abstract_declarator->kind == cdk_function
14628           && abstract_declarator->u.function.late_return_type)
14629         /* OK */;
14630       else
14631         {
14632           error ("invalid use of %<auto%>");
14633           return error_mark_node;
14634         }
14635     }
14636   
14637   return groktypename (&type_specifier_seq, abstract_declarator,
14638                        is_template_arg);
14639 }
14640
14641 static tree cp_parser_type_id (cp_parser *parser)
14642 {
14643   return cp_parser_type_id_1 (parser, false, false);
14644 }
14645
14646 static tree cp_parser_template_type_arg (cp_parser *parser)
14647 {
14648   return cp_parser_type_id_1 (parser, true, false);
14649 }
14650
14651 static tree cp_parser_trailing_type_id (cp_parser *parser)
14652 {
14653   return cp_parser_type_id_1 (parser, false, true);
14654 }
14655
14656 /* Parse a type-specifier-seq.
14657
14658    type-specifier-seq:
14659      type-specifier type-specifier-seq [opt]
14660
14661    GNU extension:
14662
14663    type-specifier-seq:
14664      attributes type-specifier-seq [opt]
14665
14666    If IS_DECLARATION is true, we are at the start of a "condition" or
14667    exception-declaration, so we might be followed by a declarator-id.
14668
14669    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14670    i.e. we've just seen "->".
14671
14672    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
14673
14674 static void
14675 cp_parser_type_specifier_seq (cp_parser* parser,
14676                               bool is_declaration,
14677                               bool is_trailing_return,
14678                               cp_decl_specifier_seq *type_specifier_seq)
14679 {
14680   bool seen_type_specifier = false;
14681   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14682   cp_token *start_token = NULL;
14683
14684   /* Clear the TYPE_SPECIFIER_SEQ.  */
14685   clear_decl_specs (type_specifier_seq);
14686
14687   /* In the context of a trailing return type, enum E { } is an
14688      elaborated-type-specifier followed by a function-body, not an
14689      enum-specifier.  */
14690   if (is_trailing_return)
14691     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14692
14693   /* Parse the type-specifiers and attributes.  */
14694   while (true)
14695     {
14696       tree type_specifier;
14697       bool is_cv_qualifier;
14698
14699       /* Check for attributes first.  */
14700       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14701         {
14702           type_specifier_seq->attributes =
14703             chainon (type_specifier_seq->attributes,
14704                      cp_parser_attributes_opt (parser));
14705           continue;
14706         }
14707
14708       /* record the token of the beginning of the type specifier seq,
14709          for error reporting purposes*/
14710      if (!start_token)
14711        start_token = cp_lexer_peek_token (parser->lexer);
14712
14713       /* Look for the type-specifier.  */
14714       type_specifier = cp_parser_type_specifier (parser,
14715                                                  flags,
14716                                                  type_specifier_seq,
14717                                                  /*is_declaration=*/false,
14718                                                  NULL,
14719                                                  &is_cv_qualifier);
14720       if (!type_specifier)
14721         {
14722           /* If the first type-specifier could not be found, this is not a
14723              type-specifier-seq at all.  */
14724           if (!seen_type_specifier)
14725             {
14726               cp_parser_error (parser, "expected type-specifier");
14727               type_specifier_seq->type = error_mark_node;
14728               return;
14729             }
14730           /* If subsequent type-specifiers could not be found, the
14731              type-specifier-seq is complete.  */
14732           break;
14733         }
14734
14735       seen_type_specifier = true;
14736       /* The standard says that a condition can be:
14737
14738             type-specifier-seq declarator = assignment-expression
14739
14740          However, given:
14741
14742            struct S {};
14743            if (int S = ...)
14744
14745          we should treat the "S" as a declarator, not as a
14746          type-specifier.  The standard doesn't say that explicitly for
14747          type-specifier-seq, but it does say that for
14748          decl-specifier-seq in an ordinary declaration.  Perhaps it
14749          would be clearer just to allow a decl-specifier-seq here, and
14750          then add a semantic restriction that if any decl-specifiers
14751          that are not type-specifiers appear, the program is invalid.  */
14752       if (is_declaration && !is_cv_qualifier)
14753         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14754     }
14755
14756   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14757 }
14758
14759 /* Parse a parameter-declaration-clause.
14760
14761    parameter-declaration-clause:
14762      parameter-declaration-list [opt] ... [opt]
14763      parameter-declaration-list , ...
14764
14765    Returns a representation for the parameter declarations.  A return
14766    value of NULL indicates a parameter-declaration-clause consisting
14767    only of an ellipsis.  */
14768
14769 static tree
14770 cp_parser_parameter_declaration_clause (cp_parser* parser)
14771 {
14772   tree parameters;
14773   cp_token *token;
14774   bool ellipsis_p;
14775   bool is_error;
14776
14777   /* Peek at the next token.  */
14778   token = cp_lexer_peek_token (parser->lexer);
14779   /* Check for trivial parameter-declaration-clauses.  */
14780   if (token->type == CPP_ELLIPSIS)
14781     {
14782       /* Consume the `...' token.  */
14783       cp_lexer_consume_token (parser->lexer);
14784       return NULL_TREE;
14785     }
14786   else if (token->type == CPP_CLOSE_PAREN)
14787     /* There are no parameters.  */
14788     {
14789 #ifndef NO_IMPLICIT_EXTERN_C
14790       if (in_system_header && current_class_type == NULL
14791           && current_lang_name == lang_name_c)
14792         return NULL_TREE;
14793       else
14794 #endif
14795         return void_list_node;
14796     }
14797   /* Check for `(void)', too, which is a special case.  */
14798   else if (token->keyword == RID_VOID
14799            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14800                == CPP_CLOSE_PAREN))
14801     {
14802       /* Consume the `void' token.  */
14803       cp_lexer_consume_token (parser->lexer);
14804       /* There are no parameters.  */
14805       return void_list_node;
14806     }
14807
14808   /* Parse the parameter-declaration-list.  */
14809   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14810   /* If a parse error occurred while parsing the
14811      parameter-declaration-list, then the entire
14812      parameter-declaration-clause is erroneous.  */
14813   if (is_error)
14814     return NULL;
14815
14816   /* Peek at the next token.  */
14817   token = cp_lexer_peek_token (parser->lexer);
14818   /* If it's a `,', the clause should terminate with an ellipsis.  */
14819   if (token->type == CPP_COMMA)
14820     {
14821       /* Consume the `,'.  */
14822       cp_lexer_consume_token (parser->lexer);
14823       /* Expect an ellipsis.  */
14824       ellipsis_p
14825         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14826     }
14827   /* It might also be `...' if the optional trailing `,' was
14828      omitted.  */
14829   else if (token->type == CPP_ELLIPSIS)
14830     {
14831       /* Consume the `...' token.  */
14832       cp_lexer_consume_token (parser->lexer);
14833       /* And remember that we saw it.  */
14834       ellipsis_p = true;
14835     }
14836   else
14837     ellipsis_p = false;
14838
14839   /* Finish the parameter list.  */
14840   if (!ellipsis_p)
14841     parameters = chainon (parameters, void_list_node);
14842
14843   return parameters;
14844 }
14845
14846 /* Parse a parameter-declaration-list.
14847
14848    parameter-declaration-list:
14849      parameter-declaration
14850      parameter-declaration-list , parameter-declaration
14851
14852    Returns a representation of the parameter-declaration-list, as for
14853    cp_parser_parameter_declaration_clause.  However, the
14854    `void_list_node' is never appended to the list.  Upon return,
14855    *IS_ERROR will be true iff an error occurred.  */
14856
14857 static tree
14858 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14859 {
14860   tree parameters = NULL_TREE;
14861   tree *tail = &parameters; 
14862   bool saved_in_unbraced_linkage_specification_p;
14863   int index = 0;
14864
14865   /* Assume all will go well.  */
14866   *is_error = false;
14867   /* The special considerations that apply to a function within an
14868      unbraced linkage specifications do not apply to the parameters
14869      to the function.  */
14870   saved_in_unbraced_linkage_specification_p 
14871     = parser->in_unbraced_linkage_specification_p;
14872   parser->in_unbraced_linkage_specification_p = false;
14873
14874   /* Look for more parameters.  */
14875   while (true)
14876     {
14877       cp_parameter_declarator *parameter;
14878       tree decl = error_mark_node;
14879       bool parenthesized_p;
14880       /* Parse the parameter.  */
14881       parameter
14882         = cp_parser_parameter_declaration (parser,
14883                                            /*template_parm_p=*/false,
14884                                            &parenthesized_p);
14885
14886       /* We don't know yet if the enclosing context is deprecated, so wait
14887          and warn in grokparms if appropriate.  */
14888       deprecated_state = DEPRECATED_SUPPRESS;
14889
14890       if (parameter)
14891         decl = grokdeclarator (parameter->declarator,
14892                                &parameter->decl_specifiers,
14893                                PARM,
14894                                parameter->default_argument != NULL_TREE,
14895                                &parameter->decl_specifiers.attributes);
14896
14897       deprecated_state = DEPRECATED_NORMAL;
14898
14899       /* If a parse error occurred parsing the parameter declaration,
14900          then the entire parameter-declaration-list is erroneous.  */
14901       if (decl == error_mark_node)
14902         {
14903           *is_error = true;
14904           parameters = error_mark_node;
14905           break;
14906         }
14907
14908       if (parameter->decl_specifiers.attributes)
14909         cplus_decl_attributes (&decl,
14910                                parameter->decl_specifiers.attributes,
14911                                0);
14912       if (DECL_NAME (decl))
14913         decl = pushdecl (decl);
14914
14915       if (decl != error_mark_node)
14916         {
14917           retrofit_lang_decl (decl);
14918           DECL_PARM_INDEX (decl) = ++index;
14919         }
14920
14921       /* Add the new parameter to the list.  */
14922       *tail = build_tree_list (parameter->default_argument, decl);
14923       tail = &TREE_CHAIN (*tail);
14924
14925       /* Peek at the next token.  */
14926       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14927           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14928           /* These are for Objective-C++ */
14929           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14930           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14931         /* The parameter-declaration-list is complete.  */
14932         break;
14933       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14934         {
14935           cp_token *token;
14936
14937           /* Peek at the next token.  */
14938           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14939           /* If it's an ellipsis, then the list is complete.  */
14940           if (token->type == CPP_ELLIPSIS)
14941             break;
14942           /* Otherwise, there must be more parameters.  Consume the
14943              `,'.  */
14944           cp_lexer_consume_token (parser->lexer);
14945           /* When parsing something like:
14946
14947                 int i(float f, double d)
14948
14949              we can tell after seeing the declaration for "f" that we
14950              are not looking at an initialization of a variable "i",
14951              but rather at the declaration of a function "i".
14952
14953              Due to the fact that the parsing of template arguments
14954              (as specified to a template-id) requires backtracking we
14955              cannot use this technique when inside a template argument
14956              list.  */
14957           if (!parser->in_template_argument_list_p
14958               && !parser->in_type_id_in_expr_p
14959               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14960               /* However, a parameter-declaration of the form
14961                  "foat(f)" (which is a valid declaration of a
14962                  parameter "f") can also be interpreted as an
14963                  expression (the conversion of "f" to "float").  */
14964               && !parenthesized_p)
14965             cp_parser_commit_to_tentative_parse (parser);
14966         }
14967       else
14968         {
14969           cp_parser_error (parser, "expected %<,%> or %<...%>");
14970           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14971             cp_parser_skip_to_closing_parenthesis (parser,
14972                                                    /*recovering=*/true,
14973                                                    /*or_comma=*/false,
14974                                                    /*consume_paren=*/false);
14975           break;
14976         }
14977     }
14978
14979   parser->in_unbraced_linkage_specification_p
14980     = saved_in_unbraced_linkage_specification_p;
14981
14982   return parameters;
14983 }
14984
14985 /* Parse a parameter declaration.
14986
14987    parameter-declaration:
14988      decl-specifier-seq ... [opt] declarator
14989      decl-specifier-seq declarator = assignment-expression
14990      decl-specifier-seq ... [opt] abstract-declarator [opt]
14991      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14992
14993    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14994    declares a template parameter.  (In that case, a non-nested `>'
14995    token encountered during the parsing of the assignment-expression
14996    is not interpreted as a greater-than operator.)
14997
14998    Returns a representation of the parameter, or NULL if an error
14999    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15000    true iff the declarator is of the form "(p)".  */
15001
15002 static cp_parameter_declarator *
15003 cp_parser_parameter_declaration (cp_parser *parser,
15004                                  bool template_parm_p,
15005                                  bool *parenthesized_p)
15006 {
15007   int declares_class_or_enum;
15008   cp_decl_specifier_seq decl_specifiers;
15009   cp_declarator *declarator;
15010   tree default_argument;
15011   cp_token *token = NULL, *declarator_token_start = NULL;
15012   const char *saved_message;
15013
15014   /* In a template parameter, `>' is not an operator.
15015
15016      [temp.param]
15017
15018      When parsing a default template-argument for a non-type
15019      template-parameter, the first non-nested `>' is taken as the end
15020      of the template parameter-list rather than a greater-than
15021      operator.  */
15022
15023   /* Type definitions may not appear in parameter types.  */
15024   saved_message = parser->type_definition_forbidden_message;
15025   parser->type_definition_forbidden_message
15026     = G_("types may not be defined in parameter types");
15027
15028   /* Parse the declaration-specifiers.  */
15029   cp_parser_decl_specifier_seq (parser,
15030                                 CP_PARSER_FLAGS_NONE,
15031                                 &decl_specifiers,
15032                                 &declares_class_or_enum);
15033
15034   /* Complain about missing 'typename' or other invalid type names.  */
15035   if (!decl_specifiers.any_type_specifiers_p)
15036     cp_parser_parse_and_diagnose_invalid_type_name (parser);
15037
15038   /* If an error occurred, there's no reason to attempt to parse the
15039      rest of the declaration.  */
15040   if (cp_parser_error_occurred (parser))
15041     {
15042       parser->type_definition_forbidden_message = saved_message;
15043       return NULL;
15044     }
15045
15046   /* Peek at the next token.  */
15047   token = cp_lexer_peek_token (parser->lexer);
15048
15049   /* If the next token is a `)', `,', `=', `>', or `...', then there
15050      is no declarator. However, when variadic templates are enabled,
15051      there may be a declarator following `...'.  */
15052   if (token->type == CPP_CLOSE_PAREN
15053       || token->type == CPP_COMMA
15054       || token->type == CPP_EQ
15055       || token->type == CPP_GREATER)
15056     {
15057       declarator = NULL;
15058       if (parenthesized_p)
15059         *parenthesized_p = false;
15060     }
15061   /* Otherwise, there should be a declarator.  */
15062   else
15063     {
15064       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15065       parser->default_arg_ok_p = false;
15066
15067       /* After seeing a decl-specifier-seq, if the next token is not a
15068          "(", there is no possibility that the code is a valid
15069          expression.  Therefore, if parsing tentatively, we commit at
15070          this point.  */
15071       if (!parser->in_template_argument_list_p
15072           /* In an expression context, having seen:
15073
15074                (int((char ...
15075
15076              we cannot be sure whether we are looking at a
15077              function-type (taking a "char" as a parameter) or a cast
15078              of some object of type "char" to "int".  */
15079           && !parser->in_type_id_in_expr_p
15080           && cp_parser_uncommitted_to_tentative_parse_p (parser)
15081           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15082         cp_parser_commit_to_tentative_parse (parser);
15083       /* Parse the declarator.  */
15084       declarator_token_start = token;
15085       declarator = cp_parser_declarator (parser,
15086                                          CP_PARSER_DECLARATOR_EITHER,
15087                                          /*ctor_dtor_or_conv_p=*/NULL,
15088                                          parenthesized_p,
15089                                          /*member_p=*/false);
15090       parser->default_arg_ok_p = saved_default_arg_ok_p;
15091       /* After the declarator, allow more attributes.  */
15092       decl_specifiers.attributes
15093         = chainon (decl_specifiers.attributes,
15094                    cp_parser_attributes_opt (parser));
15095     }
15096
15097   /* If the next token is an ellipsis, and we have not seen a
15098      declarator name, and the type of the declarator contains parameter
15099      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15100      a parameter pack expansion expression. Otherwise, leave the
15101      ellipsis for a C-style variadic function. */
15102   token = cp_lexer_peek_token (parser->lexer);
15103   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15104     {
15105       tree type = decl_specifiers.type;
15106
15107       if (type && DECL_P (type))
15108         type = TREE_TYPE (type);
15109
15110       if (type
15111           && TREE_CODE (type) != TYPE_PACK_EXPANSION
15112           && declarator_can_be_parameter_pack (declarator)
15113           && (!declarator || !declarator->parameter_pack_p)
15114           && uses_parameter_packs (type))
15115         {
15116           /* Consume the `...'. */
15117           cp_lexer_consume_token (parser->lexer);
15118           maybe_warn_variadic_templates ();
15119           
15120           /* Build a pack expansion type */
15121           if (declarator)
15122             declarator->parameter_pack_p = true;
15123           else
15124             decl_specifiers.type = make_pack_expansion (type);
15125         }
15126     }
15127
15128   /* The restriction on defining new types applies only to the type
15129      of the parameter, not to the default argument.  */
15130   parser->type_definition_forbidden_message = saved_message;
15131
15132   /* If the next token is `=', then process a default argument.  */
15133   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15134     {
15135       /* Consume the `='.  */
15136       cp_lexer_consume_token (parser->lexer);
15137
15138       /* If we are defining a class, then the tokens that make up the
15139          default argument must be saved and processed later.  */
15140       if (!template_parm_p && at_class_scope_p ()
15141           && TYPE_BEING_DEFINED (current_class_type)
15142           && !LAMBDA_TYPE_P (current_class_type))
15143         {
15144           unsigned depth = 0;
15145           int maybe_template_id = 0;
15146           cp_token *first_token;
15147           cp_token *token;
15148
15149           /* Add tokens until we have processed the entire default
15150              argument.  We add the range [first_token, token).  */
15151           first_token = cp_lexer_peek_token (parser->lexer);
15152           while (true)
15153             {
15154               bool done = false;
15155
15156               /* Peek at the next token.  */
15157               token = cp_lexer_peek_token (parser->lexer);
15158               /* What we do depends on what token we have.  */
15159               switch (token->type)
15160                 {
15161                   /* In valid code, a default argument must be
15162                      immediately followed by a `,' `)', or `...'.  */
15163                 case CPP_COMMA:
15164                   if (depth == 0 && maybe_template_id)
15165                     {
15166                       /* If we've seen a '<', we might be in a
15167                          template-argument-list.  Until Core issue 325 is
15168                          resolved, we don't know how this situation ought
15169                          to be handled, so try to DTRT.  We check whether
15170                          what comes after the comma is a valid parameter
15171                          declaration list.  If it is, then the comma ends
15172                          the default argument; otherwise the default
15173                          argument continues.  */
15174                       bool error = false;
15175
15176                       /* Set ITALP so cp_parser_parameter_declaration_list
15177                          doesn't decide to commit to this parse.  */
15178                       bool saved_italp = parser->in_template_argument_list_p;
15179                       parser->in_template_argument_list_p = true;
15180
15181                       cp_parser_parse_tentatively (parser);
15182                       cp_lexer_consume_token (parser->lexer);
15183                       cp_parser_parameter_declaration_list (parser, &error);
15184                       if (!cp_parser_error_occurred (parser) && !error)
15185                         done = true;
15186                       cp_parser_abort_tentative_parse (parser);
15187
15188                       parser->in_template_argument_list_p = saved_italp;
15189                       break;
15190                     }
15191                 case CPP_CLOSE_PAREN:
15192                 case CPP_ELLIPSIS:
15193                   /* If we run into a non-nested `;', `}', or `]',
15194                      then the code is invalid -- but the default
15195                      argument is certainly over.  */
15196                 case CPP_SEMICOLON:
15197                 case CPP_CLOSE_BRACE:
15198                 case CPP_CLOSE_SQUARE:
15199                   if (depth == 0)
15200                     done = true;
15201                   /* Update DEPTH, if necessary.  */
15202                   else if (token->type == CPP_CLOSE_PAREN
15203                            || token->type == CPP_CLOSE_BRACE
15204                            || token->type == CPP_CLOSE_SQUARE)
15205                     --depth;
15206                   break;
15207
15208                 case CPP_OPEN_PAREN:
15209                 case CPP_OPEN_SQUARE:
15210                 case CPP_OPEN_BRACE:
15211                   ++depth;
15212                   break;
15213
15214                 case CPP_LESS:
15215                   if (depth == 0)
15216                     /* This might be the comparison operator, or it might
15217                        start a template argument list.  */
15218                     ++maybe_template_id;
15219                   break;
15220
15221                 case CPP_RSHIFT:
15222                   if (cxx_dialect == cxx98)
15223                     break;
15224                   /* Fall through for C++0x, which treats the `>>'
15225                      operator like two `>' tokens in certain
15226                      cases.  */
15227
15228                 case CPP_GREATER:
15229                   if (depth == 0)
15230                     {
15231                       /* This might be an operator, or it might close a
15232                          template argument list.  But if a previous '<'
15233                          started a template argument list, this will have
15234                          closed it, so we can't be in one anymore.  */
15235                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15236                       if (maybe_template_id < 0)
15237                         maybe_template_id = 0;
15238                     }
15239                   break;
15240
15241                   /* If we run out of tokens, issue an error message.  */
15242                 case CPP_EOF:
15243                 case CPP_PRAGMA_EOL:
15244                   error_at (token->location, "file ends in default argument");
15245                   done = true;
15246                   break;
15247
15248                 case CPP_NAME:
15249                 case CPP_SCOPE:
15250                   /* In these cases, we should look for template-ids.
15251                      For example, if the default argument is
15252                      `X<int, double>()', we need to do name lookup to
15253                      figure out whether or not `X' is a template; if
15254                      so, the `,' does not end the default argument.
15255
15256                      That is not yet done.  */
15257                   break;
15258
15259                 default:
15260                   break;
15261                 }
15262
15263               /* If we've reached the end, stop.  */
15264               if (done)
15265                 break;
15266
15267               /* Add the token to the token block.  */
15268               token = cp_lexer_consume_token (parser->lexer);
15269             }
15270
15271           /* Create a DEFAULT_ARG to represent the unparsed default
15272              argument.  */
15273           default_argument = make_node (DEFAULT_ARG);
15274           DEFARG_TOKENS (default_argument)
15275             = cp_token_cache_new (first_token, token);
15276           DEFARG_INSTANTIATIONS (default_argument) = NULL;
15277         }
15278       /* Outside of a class definition, we can just parse the
15279          assignment-expression.  */
15280       else
15281         {
15282           token = cp_lexer_peek_token (parser->lexer);
15283           default_argument 
15284             = cp_parser_default_argument (parser, template_parm_p);
15285         }
15286
15287       if (!parser->default_arg_ok_p)
15288         {
15289           if (flag_permissive)
15290             warning (0, "deprecated use of default argument for parameter of non-function");
15291           else
15292             {
15293               error_at (token->location,
15294                         "default arguments are only "
15295                         "permitted for function parameters");
15296               default_argument = NULL_TREE;
15297             }
15298         }
15299       else if ((declarator && declarator->parameter_pack_p)
15300                || (decl_specifiers.type
15301                    && PACK_EXPANSION_P (decl_specifiers.type)))
15302         {
15303           /* Find the name of the parameter pack.  */     
15304           cp_declarator *id_declarator = declarator;
15305           while (id_declarator && id_declarator->kind != cdk_id)
15306             id_declarator = id_declarator->declarator;
15307           
15308           if (id_declarator && id_declarator->kind == cdk_id)
15309             error_at (declarator_token_start->location,
15310                       template_parm_p 
15311                       ? "template parameter pack %qD"
15312                       " cannot have a default argument"
15313                       : "parameter pack %qD cannot have a default argument",
15314                       id_declarator->u.id.unqualified_name);
15315           else
15316             error_at (declarator_token_start->location,
15317                       template_parm_p 
15318                       ? "template parameter pack cannot have a default argument"
15319                       : "parameter pack cannot have a default argument");
15320           
15321           default_argument = NULL_TREE;
15322         }
15323     }
15324   else
15325     default_argument = NULL_TREE;
15326
15327   return make_parameter_declarator (&decl_specifiers,
15328                                     declarator,
15329                                     default_argument);
15330 }
15331
15332 /* Parse a default argument and return it.
15333
15334    TEMPLATE_PARM_P is true if this is a default argument for a
15335    non-type template parameter.  */
15336 static tree
15337 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15338 {
15339   tree default_argument = NULL_TREE;
15340   bool saved_greater_than_is_operator_p;
15341   bool saved_local_variables_forbidden_p;
15342
15343   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15344      set correctly.  */
15345   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15346   parser->greater_than_is_operator_p = !template_parm_p;
15347   /* Local variable names (and the `this' keyword) may not
15348      appear in a default argument.  */
15349   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15350   parser->local_variables_forbidden_p = true;
15351   /* Parse the assignment-expression.  */
15352   if (template_parm_p)
15353     push_deferring_access_checks (dk_no_deferred);
15354   default_argument
15355     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15356   if (template_parm_p)
15357     pop_deferring_access_checks ();
15358   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15359   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15360
15361   return default_argument;
15362 }
15363
15364 /* Parse a function-body.
15365
15366    function-body:
15367      compound_statement  */
15368
15369 static void
15370 cp_parser_function_body (cp_parser *parser)
15371 {
15372   cp_parser_compound_statement (parser, NULL, false);
15373 }
15374
15375 /* Parse a ctor-initializer-opt followed by a function-body.  Return
15376    true if a ctor-initializer was present.  */
15377
15378 static bool
15379 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15380 {
15381   tree body;
15382   bool ctor_initializer_p;
15383
15384   /* Begin the function body.  */
15385   body = begin_function_body ();
15386   /* Parse the optional ctor-initializer.  */
15387   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15388   /* Parse the function-body.  */
15389   cp_parser_function_body (parser);
15390   /* Finish the function body.  */
15391   finish_function_body (body);
15392
15393   return ctor_initializer_p;
15394 }
15395
15396 /* Parse an initializer.
15397
15398    initializer:
15399      = initializer-clause
15400      ( expression-list )
15401
15402    Returns an expression representing the initializer.  If no
15403    initializer is present, NULL_TREE is returned.
15404
15405    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15406    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
15407    set to TRUE if there is no initializer present.  If there is an
15408    initializer, and it is not a constant-expression, *NON_CONSTANT_P
15409    is set to true; otherwise it is set to false.  */
15410
15411 static tree
15412 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15413                        bool* non_constant_p)
15414 {
15415   cp_token *token;
15416   tree init;
15417
15418   /* Peek at the next token.  */
15419   token = cp_lexer_peek_token (parser->lexer);
15420
15421   /* Let our caller know whether or not this initializer was
15422      parenthesized.  */
15423   *is_direct_init = (token->type != CPP_EQ);
15424   /* Assume that the initializer is constant.  */
15425   *non_constant_p = false;
15426
15427   if (token->type == CPP_EQ)
15428     {
15429       /* Consume the `='.  */
15430       cp_lexer_consume_token (parser->lexer);
15431       /* Parse the initializer-clause.  */
15432       init = cp_parser_initializer_clause (parser, non_constant_p);
15433     }
15434   else if (token->type == CPP_OPEN_PAREN)
15435     {
15436       VEC(tree,gc) *vec;
15437       vec = cp_parser_parenthesized_expression_list (parser, false,
15438                                                      /*cast_p=*/false,
15439                                                      /*allow_expansion_p=*/true,
15440                                                      non_constant_p);
15441       if (vec == NULL)
15442         return error_mark_node;
15443       init = build_tree_list_vec (vec);
15444       release_tree_vector (vec);
15445     }
15446   else if (token->type == CPP_OPEN_BRACE)
15447     {
15448       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15449       init = cp_parser_braced_list (parser, non_constant_p);
15450       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15451     }
15452   else
15453     {
15454       /* Anything else is an error.  */
15455       cp_parser_error (parser, "expected initializer");
15456       init = error_mark_node;
15457     }
15458
15459   return init;
15460 }
15461
15462 /* Parse an initializer-clause.
15463
15464    initializer-clause:
15465      assignment-expression
15466      braced-init-list
15467
15468    Returns an expression representing the initializer.
15469
15470    If the `assignment-expression' production is used the value
15471    returned is simply a representation for the expression.
15472
15473    Otherwise, calls cp_parser_braced_list.  */
15474
15475 static tree
15476 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15477 {
15478   tree initializer;
15479
15480   /* Assume the expression is constant.  */
15481   *non_constant_p = false;
15482
15483   /* If it is not a `{', then we are looking at an
15484      assignment-expression.  */
15485   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15486     {
15487       initializer
15488         = cp_parser_constant_expression (parser,
15489                                         /*allow_non_constant_p=*/true,
15490                                         non_constant_p);
15491       if (!*non_constant_p)
15492         initializer = fold_non_dependent_expr (initializer);
15493     }
15494   else
15495     initializer = cp_parser_braced_list (parser, non_constant_p);
15496
15497   return initializer;
15498 }
15499
15500 /* Parse a brace-enclosed initializer list.
15501
15502    braced-init-list:
15503      { initializer-list , [opt] }
15504      { }
15505
15506    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
15507    the elements of the initializer-list (or NULL, if the last
15508    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
15509    NULL_TREE.  There is no way to detect whether or not the optional
15510    trailing `,' was provided.  NON_CONSTANT_P is as for
15511    cp_parser_initializer.  */     
15512
15513 static tree
15514 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15515 {
15516   tree initializer;
15517
15518   /* Consume the `{' token.  */
15519   cp_lexer_consume_token (parser->lexer);
15520   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
15521   initializer = make_node (CONSTRUCTOR);
15522   /* If it's not a `}', then there is a non-trivial initializer.  */
15523   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15524     {
15525       /* Parse the initializer list.  */
15526       CONSTRUCTOR_ELTS (initializer)
15527         = cp_parser_initializer_list (parser, non_constant_p);
15528       /* A trailing `,' token is allowed.  */
15529       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15530         cp_lexer_consume_token (parser->lexer);
15531     }
15532   /* Now, there should be a trailing `}'.  */
15533   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15534   TREE_TYPE (initializer) = init_list_type_node;
15535   return initializer;
15536 }
15537
15538 /* Parse an initializer-list.
15539
15540    initializer-list:
15541      initializer-clause ... [opt]
15542      initializer-list , initializer-clause ... [opt]
15543
15544    GNU Extension:
15545
15546    initializer-list:
15547      identifier : initializer-clause
15548      initializer-list, identifier : initializer-clause
15549
15550    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
15551    for the initializer.  If the INDEX of the elt is non-NULL, it is the
15552    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
15553    as for cp_parser_initializer.  */
15554
15555 static VEC(constructor_elt,gc) *
15556 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15557 {
15558   VEC(constructor_elt,gc) *v = NULL;
15559
15560   /* Assume all of the expressions are constant.  */
15561   *non_constant_p = false;
15562
15563   /* Parse the rest of the list.  */
15564   while (true)
15565     {
15566       cp_token *token;
15567       tree identifier;
15568       tree initializer;
15569       bool clause_non_constant_p;
15570
15571       /* If the next token is an identifier and the following one is a
15572          colon, we are looking at the GNU designated-initializer
15573          syntax.  */
15574       if (cp_parser_allow_gnu_extensions_p (parser)
15575           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15576           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15577         {
15578           /* Warn the user that they are using an extension.  */
15579           pedwarn (input_location, OPT_pedantic, 
15580                    "ISO C++ does not allow designated initializers");
15581           /* Consume the identifier.  */
15582           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15583           /* Consume the `:'.  */
15584           cp_lexer_consume_token (parser->lexer);
15585         }
15586       else
15587         identifier = NULL_TREE;
15588
15589       /* Parse the initializer.  */
15590       initializer = cp_parser_initializer_clause (parser,
15591                                                   &clause_non_constant_p);
15592       /* If any clause is non-constant, so is the entire initializer.  */
15593       if (clause_non_constant_p)
15594         *non_constant_p = true;
15595
15596       /* If we have an ellipsis, this is an initializer pack
15597          expansion.  */
15598       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15599         {
15600           /* Consume the `...'.  */
15601           cp_lexer_consume_token (parser->lexer);
15602
15603           /* Turn the initializer into an initializer expansion.  */
15604           initializer = make_pack_expansion (initializer);
15605         }
15606
15607       /* Add it to the vector.  */
15608       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15609
15610       /* If the next token is not a comma, we have reached the end of
15611          the list.  */
15612       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15613         break;
15614
15615       /* Peek at the next token.  */
15616       token = cp_lexer_peek_nth_token (parser->lexer, 2);
15617       /* If the next token is a `}', then we're still done.  An
15618          initializer-clause can have a trailing `,' after the
15619          initializer-list and before the closing `}'.  */
15620       if (token->type == CPP_CLOSE_BRACE)
15621         break;
15622
15623       /* Consume the `,' token.  */
15624       cp_lexer_consume_token (parser->lexer);
15625     }
15626
15627   return v;
15628 }
15629
15630 /* Classes [gram.class] */
15631
15632 /* Parse a class-name.
15633
15634    class-name:
15635      identifier
15636      template-id
15637
15638    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15639    to indicate that names looked up in dependent types should be
15640    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
15641    keyword has been used to indicate that the name that appears next
15642    is a template.  TAG_TYPE indicates the explicit tag given before
15643    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
15644    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
15645    is the class being defined in a class-head.
15646
15647    Returns the TYPE_DECL representing the class.  */
15648
15649 static tree
15650 cp_parser_class_name (cp_parser *parser,
15651                       bool typename_keyword_p,
15652                       bool template_keyword_p,
15653                       enum tag_types tag_type,
15654                       bool check_dependency_p,
15655                       bool class_head_p,
15656                       bool is_declaration)
15657 {
15658   tree decl;
15659   tree scope;
15660   bool typename_p;
15661   cp_token *token;
15662   tree identifier = NULL_TREE;
15663
15664   /* All class-names start with an identifier.  */
15665   token = cp_lexer_peek_token (parser->lexer);
15666   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15667     {
15668       cp_parser_error (parser, "expected class-name");
15669       return error_mark_node;
15670     }
15671
15672   /* PARSER->SCOPE can be cleared when parsing the template-arguments
15673      to a template-id, so we save it here.  */
15674   scope = parser->scope;
15675   if (scope == error_mark_node)
15676     return error_mark_node;
15677
15678   /* Any name names a type if we're following the `typename' keyword
15679      in a qualified name where the enclosing scope is type-dependent.  */
15680   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15681                 && dependent_type_p (scope));
15682   /* Handle the common case (an identifier, but not a template-id)
15683      efficiently.  */
15684   if (token->type == CPP_NAME
15685       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15686     {
15687       cp_token *identifier_token;
15688       bool ambiguous_p;
15689
15690       /* Look for the identifier.  */
15691       identifier_token = cp_lexer_peek_token (parser->lexer);
15692       ambiguous_p = identifier_token->ambiguous_p;
15693       identifier = cp_parser_identifier (parser);
15694       /* If the next token isn't an identifier, we are certainly not
15695          looking at a class-name.  */
15696       if (identifier == error_mark_node)
15697         decl = error_mark_node;
15698       /* If we know this is a type-name, there's no need to look it
15699          up.  */
15700       else if (typename_p)
15701         decl = identifier;
15702       else
15703         {
15704           tree ambiguous_decls;
15705           /* If we already know that this lookup is ambiguous, then
15706              we've already issued an error message; there's no reason
15707              to check again.  */
15708           if (ambiguous_p)
15709             {
15710               cp_parser_simulate_error (parser);
15711               return error_mark_node;
15712             }
15713           /* If the next token is a `::', then the name must be a type
15714              name.
15715
15716              [basic.lookup.qual]
15717
15718              During the lookup for a name preceding the :: scope
15719              resolution operator, object, function, and enumerator
15720              names are ignored.  */
15721           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15722             tag_type = typename_type;
15723           /* Look up the name.  */
15724           decl = cp_parser_lookup_name (parser, identifier,
15725                                         tag_type,
15726                                         /*is_template=*/false,
15727                                         /*is_namespace=*/false,
15728                                         check_dependency_p,
15729                                         &ambiguous_decls,
15730                                         identifier_token->location);
15731           if (ambiguous_decls)
15732             {
15733               if (cp_parser_parsing_tentatively (parser))
15734                 cp_parser_simulate_error (parser);
15735               return error_mark_node;
15736             }
15737         }
15738     }
15739   else
15740     {
15741       /* Try a template-id.  */
15742       decl = cp_parser_template_id (parser, template_keyword_p,
15743                                     check_dependency_p,
15744                                     is_declaration);
15745       if (decl == error_mark_node)
15746         return error_mark_node;
15747     }
15748
15749   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15750
15751   /* If this is a typename, create a TYPENAME_TYPE.  */
15752   if (typename_p && decl != error_mark_node)
15753     {
15754       decl = make_typename_type (scope, decl, typename_type,
15755                                  /*complain=*/tf_error);
15756       if (decl != error_mark_node)
15757         decl = TYPE_NAME (decl);
15758     }
15759
15760   /* Check to see that it is really the name of a class.  */
15761   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15762       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15763       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15764     /* Situations like this:
15765
15766          template <typename T> struct A {
15767            typename T::template X<int>::I i;
15768          };
15769
15770        are problematic.  Is `T::template X<int>' a class-name?  The
15771        standard does not seem to be definitive, but there is no other
15772        valid interpretation of the following `::'.  Therefore, those
15773        names are considered class-names.  */
15774     {
15775       decl = make_typename_type (scope, decl, tag_type, tf_error);
15776       if (decl != error_mark_node)
15777         decl = TYPE_NAME (decl);
15778     }
15779   else if (TREE_CODE (decl) != TYPE_DECL
15780            || TREE_TYPE (decl) == error_mark_node
15781            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15782     decl = error_mark_node;
15783
15784   if (decl == error_mark_node)
15785     cp_parser_error (parser, "expected class-name");
15786   else if (identifier && !parser->scope)
15787     maybe_note_name_used_in_class (identifier, decl);
15788
15789   return decl;
15790 }
15791
15792 /* Parse a class-specifier.
15793
15794    class-specifier:
15795      class-head { member-specification [opt] }
15796
15797    Returns the TREE_TYPE representing the class.  */
15798
15799 static tree
15800 cp_parser_class_specifier (cp_parser* parser)
15801 {
15802   tree type;
15803   tree attributes = NULL_TREE;
15804   bool nested_name_specifier_p;
15805   unsigned saved_num_template_parameter_lists;
15806   bool saved_in_function_body;
15807   bool saved_in_unbraced_linkage_specification_p;
15808   tree old_scope = NULL_TREE;
15809   tree scope = NULL_TREE;
15810   tree bases;
15811
15812   push_deferring_access_checks (dk_no_deferred);
15813
15814   /* Parse the class-head.  */
15815   type = cp_parser_class_head (parser,
15816                                &nested_name_specifier_p,
15817                                &attributes,
15818                                &bases);
15819   /* If the class-head was a semantic disaster, skip the entire body
15820      of the class.  */
15821   if (!type)
15822     {
15823       cp_parser_skip_to_end_of_block_or_statement (parser);
15824       pop_deferring_access_checks ();
15825       return error_mark_node;
15826     }
15827
15828   /* Look for the `{'.  */
15829   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15830     {
15831       pop_deferring_access_checks ();
15832       return error_mark_node;
15833     }
15834
15835   /* Process the base classes. If they're invalid, skip the 
15836      entire class body.  */
15837   if (!xref_basetypes (type, bases))
15838     {
15839       /* Consuming the closing brace yields better error messages
15840          later on.  */
15841       if (cp_parser_skip_to_closing_brace (parser))
15842         cp_lexer_consume_token (parser->lexer);
15843       pop_deferring_access_checks ();
15844       return error_mark_node;
15845     }
15846
15847   /* Issue an error message if type-definitions are forbidden here.  */
15848   cp_parser_check_type_definition (parser);
15849   /* Remember that we are defining one more class.  */
15850   ++parser->num_classes_being_defined;
15851   /* Inside the class, surrounding template-parameter-lists do not
15852      apply.  */
15853   saved_num_template_parameter_lists
15854     = parser->num_template_parameter_lists;
15855   parser->num_template_parameter_lists = 0;
15856   /* We are not in a function body.  */
15857   saved_in_function_body = parser->in_function_body;
15858   parser->in_function_body = false;
15859   /* We are not immediately inside an extern "lang" block.  */
15860   saved_in_unbraced_linkage_specification_p
15861     = parser->in_unbraced_linkage_specification_p;
15862   parser->in_unbraced_linkage_specification_p = false;
15863
15864   /* Start the class.  */
15865   if (nested_name_specifier_p)
15866     {
15867       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15868       old_scope = push_inner_scope (scope);
15869     }
15870   type = begin_class_definition (type, attributes);
15871
15872   if (type == error_mark_node)
15873     /* If the type is erroneous, skip the entire body of the class.  */
15874     cp_parser_skip_to_closing_brace (parser);
15875   else
15876     /* Parse the member-specification.  */
15877     cp_parser_member_specification_opt (parser);
15878
15879   /* Look for the trailing `}'.  */
15880   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15881   /* Look for trailing attributes to apply to this class.  */
15882   if (cp_parser_allow_gnu_extensions_p (parser))
15883     attributes = cp_parser_attributes_opt (parser);
15884   if (type != error_mark_node)
15885     type = finish_struct (type, attributes);
15886   if (nested_name_specifier_p)
15887     pop_inner_scope (old_scope, scope);
15888   /* If this class is not itself within the scope of another class,
15889      then we need to parse the bodies of all of the queued function
15890      definitions.  Note that the queued functions defined in a class
15891      are not always processed immediately following the
15892      class-specifier for that class.  Consider:
15893
15894        struct A {
15895          struct B { void f() { sizeof (A); } };
15896        };
15897
15898      If `f' were processed before the processing of `A' were
15899      completed, there would be no way to compute the size of `A'.
15900      Note that the nesting we are interested in here is lexical --
15901      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15902      for:
15903
15904        struct A { struct B; };
15905        struct A::B { void f() { } };
15906
15907      there is no need to delay the parsing of `A::B::f'.  */
15908   if (--parser->num_classes_being_defined == 0)
15909     {
15910       tree queue_entry;
15911       tree fn;
15912       tree class_type = NULL_TREE;
15913       tree pushed_scope = NULL_TREE;
15914
15915       /* In a first pass, parse default arguments to the functions.
15916          Then, in a second pass, parse the bodies of the functions.
15917          This two-phased approach handles cases like:
15918
15919             struct S {
15920               void f() { g(); }
15921               void g(int i = 3);
15922             };
15923
15924          */
15925       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15926              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15927            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15928            TREE_PURPOSE (parser->unparsed_functions_queues)
15929              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15930         {
15931           fn = TREE_VALUE (queue_entry);
15932           /* If there are default arguments that have not yet been processed,
15933              take care of them now.  */
15934           if (class_type != TREE_PURPOSE (queue_entry))
15935             {
15936               if (pushed_scope)
15937                 pop_scope (pushed_scope);
15938               class_type = TREE_PURPOSE (queue_entry);
15939               pushed_scope = push_scope (class_type);
15940             }
15941           /* Make sure that any template parameters are in scope.  */
15942           maybe_begin_member_template_processing (fn);
15943           /* Parse the default argument expressions.  */
15944           cp_parser_late_parsing_default_args (parser, fn);
15945           /* Remove any template parameters from the symbol table.  */
15946           maybe_end_member_template_processing ();
15947         }
15948       if (pushed_scope)
15949         pop_scope (pushed_scope);
15950       /* Now parse the body of the functions.  */
15951       for (TREE_VALUE (parser->unparsed_functions_queues)
15952              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15953            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15954            TREE_VALUE (parser->unparsed_functions_queues)
15955              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15956         {
15957           /* Figure out which function we need to process.  */
15958           fn = TREE_VALUE (queue_entry);
15959           /* Parse the function.  */
15960           cp_parser_late_parsing_for_member (parser, fn);
15961         }
15962     }
15963
15964   /* Put back any saved access checks.  */
15965   pop_deferring_access_checks ();
15966
15967   /* Restore saved state.  */
15968   parser->in_function_body = saved_in_function_body;
15969   parser->num_template_parameter_lists
15970     = saved_num_template_parameter_lists;
15971   parser->in_unbraced_linkage_specification_p
15972     = saved_in_unbraced_linkage_specification_p;
15973
15974   return type;
15975 }
15976
15977 /* Parse a class-head.
15978
15979    class-head:
15980      class-key identifier [opt] base-clause [opt]
15981      class-key nested-name-specifier identifier base-clause [opt]
15982      class-key nested-name-specifier [opt] template-id
15983        base-clause [opt]
15984
15985    GNU Extensions:
15986      class-key attributes identifier [opt] base-clause [opt]
15987      class-key attributes nested-name-specifier identifier base-clause [opt]
15988      class-key attributes nested-name-specifier [opt] template-id
15989        base-clause [opt]
15990
15991    Upon return BASES is initialized to the list of base classes (or
15992    NULL, if there are none) in the same form returned by
15993    cp_parser_base_clause.
15994
15995    Returns the TYPE of the indicated class.  Sets
15996    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15997    involving a nested-name-specifier was used, and FALSE otherwise.
15998
15999    Returns error_mark_node if this is not a class-head.
16000
16001    Returns NULL_TREE if the class-head is syntactically valid, but
16002    semantically invalid in a way that means we should skip the entire
16003    body of the class.  */
16004
16005 static tree
16006 cp_parser_class_head (cp_parser* parser,
16007                       bool* nested_name_specifier_p,
16008                       tree *attributes_p,
16009                       tree *bases)
16010 {
16011   tree nested_name_specifier;
16012   enum tag_types class_key;
16013   tree id = NULL_TREE;
16014   tree type = NULL_TREE;
16015   tree attributes;
16016   bool template_id_p = false;
16017   bool qualified_p = false;
16018   bool invalid_nested_name_p = false;
16019   bool invalid_explicit_specialization_p = false;
16020   tree pushed_scope = NULL_TREE;
16021   unsigned num_templates;
16022   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16023   /* Assume no nested-name-specifier will be present.  */
16024   *nested_name_specifier_p = false;
16025   /* Assume no template parameter lists will be used in defining the
16026      type.  */
16027   num_templates = 0;
16028
16029   *bases = NULL_TREE;
16030
16031   /* Look for the class-key.  */
16032   class_key = cp_parser_class_key (parser);
16033   if (class_key == none_type)
16034     return error_mark_node;
16035
16036   /* Parse the attributes.  */
16037   attributes = cp_parser_attributes_opt (parser);
16038
16039   /* If the next token is `::', that is invalid -- but sometimes
16040      people do try to write:
16041
16042        struct ::S {};
16043
16044      Handle this gracefully by accepting the extra qualifier, and then
16045      issuing an error about it later if this really is a
16046      class-head.  If it turns out just to be an elaborated type
16047      specifier, remain silent.  */
16048   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16049     qualified_p = true;
16050
16051   push_deferring_access_checks (dk_no_check);
16052
16053   /* Determine the name of the class.  Begin by looking for an
16054      optional nested-name-specifier.  */
16055   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16056   nested_name_specifier
16057     = cp_parser_nested_name_specifier_opt (parser,
16058                                            /*typename_keyword_p=*/false,
16059                                            /*check_dependency_p=*/false,
16060                                            /*type_p=*/false,
16061                                            /*is_declaration=*/false);
16062   /* If there was a nested-name-specifier, then there *must* be an
16063      identifier.  */
16064   if (nested_name_specifier)
16065     {
16066       type_start_token = cp_lexer_peek_token (parser->lexer);
16067       /* Although the grammar says `identifier', it really means
16068          `class-name' or `template-name'.  You are only allowed to
16069          define a class that has already been declared with this
16070          syntax.
16071
16072          The proposed resolution for Core Issue 180 says that wherever
16073          you see `class T::X' you should treat `X' as a type-name.
16074
16075          It is OK to define an inaccessible class; for example:
16076
16077            class A { class B; };
16078            class A::B {};
16079
16080          We do not know if we will see a class-name, or a
16081          template-name.  We look for a class-name first, in case the
16082          class-name is a template-id; if we looked for the
16083          template-name first we would stop after the template-name.  */
16084       cp_parser_parse_tentatively (parser);
16085       type = cp_parser_class_name (parser,
16086                                    /*typename_keyword_p=*/false,
16087                                    /*template_keyword_p=*/false,
16088                                    class_type,
16089                                    /*check_dependency_p=*/false,
16090                                    /*class_head_p=*/true,
16091                                    /*is_declaration=*/false);
16092       /* If that didn't work, ignore the nested-name-specifier.  */
16093       if (!cp_parser_parse_definitely (parser))
16094         {
16095           invalid_nested_name_p = true;
16096           type_start_token = cp_lexer_peek_token (parser->lexer);
16097           id = cp_parser_identifier (parser);
16098           if (id == error_mark_node)
16099             id = NULL_TREE;
16100         }
16101       /* If we could not find a corresponding TYPE, treat this
16102          declaration like an unqualified declaration.  */
16103       if (type == error_mark_node)
16104         nested_name_specifier = NULL_TREE;
16105       /* Otherwise, count the number of templates used in TYPE and its
16106          containing scopes.  */
16107       else
16108         {
16109           tree scope;
16110
16111           for (scope = TREE_TYPE (type);
16112                scope && TREE_CODE (scope) != NAMESPACE_DECL;
16113                scope = (TYPE_P (scope)
16114                         ? TYPE_CONTEXT (scope)
16115                         : DECL_CONTEXT (scope)))
16116             if (TYPE_P (scope)
16117                 && CLASS_TYPE_P (scope)
16118                 && CLASSTYPE_TEMPLATE_INFO (scope)
16119                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16120                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16121               ++num_templates;
16122         }
16123     }
16124   /* Otherwise, the identifier is optional.  */
16125   else
16126     {
16127       /* We don't know whether what comes next is a template-id,
16128          an identifier, or nothing at all.  */
16129       cp_parser_parse_tentatively (parser);
16130       /* Check for a template-id.  */
16131       type_start_token = cp_lexer_peek_token (parser->lexer);
16132       id = cp_parser_template_id (parser,
16133                                   /*template_keyword_p=*/false,
16134                                   /*check_dependency_p=*/true,
16135                                   /*is_declaration=*/true);
16136       /* If that didn't work, it could still be an identifier.  */
16137       if (!cp_parser_parse_definitely (parser))
16138         {
16139           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16140             {
16141               type_start_token = cp_lexer_peek_token (parser->lexer);
16142               id = cp_parser_identifier (parser);
16143             }
16144           else
16145             id = NULL_TREE;
16146         }
16147       else
16148         {
16149           template_id_p = true;
16150           ++num_templates;
16151         }
16152     }
16153
16154   pop_deferring_access_checks ();
16155
16156   if (id)
16157     cp_parser_check_for_invalid_template_id (parser, id,
16158                                              type_start_token->location);
16159
16160   /* If it's not a `:' or a `{' then we can't really be looking at a
16161      class-head, since a class-head only appears as part of a
16162      class-specifier.  We have to detect this situation before calling
16163      xref_tag, since that has irreversible side-effects.  */
16164   if (!cp_parser_next_token_starts_class_definition_p (parser))
16165     {
16166       cp_parser_error (parser, "expected %<{%> or %<:%>");
16167       return error_mark_node;
16168     }
16169
16170   /* At this point, we're going ahead with the class-specifier, even
16171      if some other problem occurs.  */
16172   cp_parser_commit_to_tentative_parse (parser);
16173   /* Issue the error about the overly-qualified name now.  */
16174   if (qualified_p)
16175     {
16176       cp_parser_error (parser,
16177                        "global qualification of class name is invalid");
16178       return error_mark_node;
16179     }
16180   else if (invalid_nested_name_p)
16181     {
16182       cp_parser_error (parser,
16183                        "qualified name does not name a class");
16184       return error_mark_node;
16185     }
16186   else if (nested_name_specifier)
16187     {
16188       tree scope;
16189
16190       /* Reject typedef-names in class heads.  */
16191       if (!DECL_IMPLICIT_TYPEDEF_P (type))
16192         {
16193           error_at (type_start_token->location,
16194                     "invalid class name in declaration of %qD",
16195                     type);
16196           type = NULL_TREE;
16197           goto done;
16198         }
16199
16200       /* Figure out in what scope the declaration is being placed.  */
16201       scope = current_scope ();
16202       /* If that scope does not contain the scope in which the
16203          class was originally declared, the program is invalid.  */
16204       if (scope && !is_ancestor (scope, nested_name_specifier))
16205         {
16206           if (at_namespace_scope_p ())
16207             error_at (type_start_token->location,
16208                       "declaration of %qD in namespace %qD which does not "
16209                       "enclose %qD",
16210                       type, scope, nested_name_specifier);
16211           else
16212             error_at (type_start_token->location,
16213                       "declaration of %qD in %qD which does not enclose %qD",
16214                       type, scope, nested_name_specifier);
16215           type = NULL_TREE;
16216           goto done;
16217         }
16218       /* [dcl.meaning]
16219
16220          A declarator-id shall not be qualified except for the
16221          definition of a ... nested class outside of its class
16222          ... [or] the definition or explicit instantiation of a
16223          class member of a namespace outside of its namespace.  */
16224       if (scope == nested_name_specifier)
16225         {
16226           permerror (nested_name_specifier_token_start->location,
16227                      "extra qualification not allowed");
16228           nested_name_specifier = NULL_TREE;
16229           num_templates = 0;
16230         }
16231     }
16232   /* An explicit-specialization must be preceded by "template <>".  If
16233      it is not, try to recover gracefully.  */
16234   if (at_namespace_scope_p ()
16235       && parser->num_template_parameter_lists == 0
16236       && template_id_p)
16237     {
16238       error_at (type_start_token->location,
16239                 "an explicit specialization must be preceded by %<template <>%>");
16240       invalid_explicit_specialization_p = true;
16241       /* Take the same action that would have been taken by
16242          cp_parser_explicit_specialization.  */
16243       ++parser->num_template_parameter_lists;
16244       begin_specialization ();
16245     }
16246   /* There must be no "return" statements between this point and the
16247      end of this function; set "type "to the correct return value and
16248      use "goto done;" to return.  */
16249   /* Make sure that the right number of template parameters were
16250      present.  */
16251   if (!cp_parser_check_template_parameters (parser, num_templates,
16252                                             type_start_token->location,
16253                                             /*declarator=*/NULL))
16254     {
16255       /* If something went wrong, there is no point in even trying to
16256          process the class-definition.  */
16257       type = NULL_TREE;
16258       goto done;
16259     }
16260
16261   /* Look up the type.  */
16262   if (template_id_p)
16263     {
16264       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16265           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16266               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16267         {
16268           error_at (type_start_token->location,
16269                     "function template %qD redeclared as a class template", id);
16270           type = error_mark_node;
16271         }
16272       else
16273         {
16274           type = TREE_TYPE (id);
16275           type = maybe_process_partial_specialization (type);
16276         }
16277       if (nested_name_specifier)
16278         pushed_scope = push_scope (nested_name_specifier);
16279     }
16280   else if (nested_name_specifier)
16281     {
16282       tree class_type;
16283
16284       /* Given:
16285
16286             template <typename T> struct S { struct T };
16287             template <typename T> struct S<T>::T { };
16288
16289          we will get a TYPENAME_TYPE when processing the definition of
16290          `S::T'.  We need to resolve it to the actual type before we
16291          try to define it.  */
16292       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16293         {
16294           class_type = resolve_typename_type (TREE_TYPE (type),
16295                                               /*only_current_p=*/false);
16296           if (TREE_CODE (class_type) != TYPENAME_TYPE)
16297             type = TYPE_NAME (class_type);
16298           else
16299             {
16300               cp_parser_error (parser, "could not resolve typename type");
16301               type = error_mark_node;
16302             }
16303         }
16304
16305       if (maybe_process_partial_specialization (TREE_TYPE (type))
16306           == error_mark_node)
16307         {
16308           type = NULL_TREE;
16309           goto done;
16310         }
16311
16312       class_type = current_class_type;
16313       /* Enter the scope indicated by the nested-name-specifier.  */
16314       pushed_scope = push_scope (nested_name_specifier);
16315       /* Get the canonical version of this type.  */
16316       type = TYPE_MAIN_DECL (TREE_TYPE (type));
16317       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16318           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16319         {
16320           type = push_template_decl (type);
16321           if (type == error_mark_node)
16322             {
16323               type = NULL_TREE;
16324               goto done;
16325             }
16326         }
16327
16328       type = TREE_TYPE (type);
16329       *nested_name_specifier_p = true;
16330     }
16331   else      /* The name is not a nested name.  */
16332     {
16333       /* If the class was unnamed, create a dummy name.  */
16334       if (!id)
16335         id = make_anon_name ();
16336       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16337                        parser->num_template_parameter_lists);
16338     }
16339
16340   /* Indicate whether this class was declared as a `class' or as a
16341      `struct'.  */
16342   if (TREE_CODE (type) == RECORD_TYPE)
16343     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16344   cp_parser_check_class_key (class_key, type);
16345
16346   /* If this type was already complete, and we see another definition,
16347      that's an error.  */
16348   if (type != error_mark_node && COMPLETE_TYPE_P (type))
16349     {
16350       error_at (type_start_token->location, "redefinition of %q#T",
16351                 type);
16352       error_at (type_start_token->location, "previous definition of %q+#T",
16353                 type);
16354       type = NULL_TREE;
16355       goto done;
16356     }
16357   else if (type == error_mark_node)
16358     type = NULL_TREE;
16359
16360   /* We will have entered the scope containing the class; the names of
16361      base classes should be looked up in that context.  For example:
16362
16363        struct A { struct B {}; struct C; };
16364        struct A::C : B {};
16365
16366      is valid.  */
16367
16368   /* Get the list of base-classes, if there is one.  */
16369   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16370     *bases = cp_parser_base_clause (parser);
16371
16372  done:
16373   /* Leave the scope given by the nested-name-specifier.  We will
16374      enter the class scope itself while processing the members.  */
16375   if (pushed_scope)
16376     pop_scope (pushed_scope);
16377
16378   if (invalid_explicit_specialization_p)
16379     {
16380       end_specialization ();
16381       --parser->num_template_parameter_lists;
16382     }
16383   *attributes_p = attributes;
16384   return type;
16385 }
16386
16387 /* Parse a class-key.
16388
16389    class-key:
16390      class
16391      struct
16392      union
16393
16394    Returns the kind of class-key specified, or none_type to indicate
16395    error.  */
16396
16397 static enum tag_types
16398 cp_parser_class_key (cp_parser* parser)
16399 {
16400   cp_token *token;
16401   enum tag_types tag_type;
16402
16403   /* Look for the class-key.  */
16404   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16405   if (!token)
16406     return none_type;
16407
16408   /* Check to see if the TOKEN is a class-key.  */
16409   tag_type = cp_parser_token_is_class_key (token);
16410   if (!tag_type)
16411     cp_parser_error (parser, "expected class-key");
16412   return tag_type;
16413 }
16414
16415 /* Parse an (optional) member-specification.
16416
16417    member-specification:
16418      member-declaration member-specification [opt]
16419      access-specifier : member-specification [opt]  */
16420
16421 static void
16422 cp_parser_member_specification_opt (cp_parser* parser)
16423 {
16424   while (true)
16425     {
16426       cp_token *token;
16427       enum rid keyword;
16428
16429       /* Peek at the next token.  */
16430       token = cp_lexer_peek_token (parser->lexer);
16431       /* If it's a `}', or EOF then we've seen all the members.  */
16432       if (token->type == CPP_CLOSE_BRACE
16433           || token->type == CPP_EOF
16434           || token->type == CPP_PRAGMA_EOL)
16435         break;
16436
16437       /* See if this token is a keyword.  */
16438       keyword = token->keyword;
16439       switch (keyword)
16440         {
16441         case RID_PUBLIC:
16442         case RID_PROTECTED:
16443         case RID_PRIVATE:
16444           /* Consume the access-specifier.  */
16445           cp_lexer_consume_token (parser->lexer);
16446           /* Remember which access-specifier is active.  */
16447           current_access_specifier = token->u.value;
16448           /* Look for the `:'.  */
16449           cp_parser_require (parser, CPP_COLON, "%<:%>");
16450           break;
16451
16452         default:
16453           /* Accept #pragmas at class scope.  */
16454           if (token->type == CPP_PRAGMA)
16455             {
16456               cp_parser_pragma (parser, pragma_external);
16457               break;
16458             }
16459
16460           /* Otherwise, the next construction must be a
16461              member-declaration.  */
16462           cp_parser_member_declaration (parser);
16463         }
16464     }
16465 }
16466
16467 /* Parse a member-declaration.
16468
16469    member-declaration:
16470      decl-specifier-seq [opt] member-declarator-list [opt] ;
16471      function-definition ; [opt]
16472      :: [opt] nested-name-specifier template [opt] unqualified-id ;
16473      using-declaration
16474      template-declaration
16475
16476    member-declarator-list:
16477      member-declarator
16478      member-declarator-list , member-declarator
16479
16480    member-declarator:
16481      declarator pure-specifier [opt]
16482      declarator constant-initializer [opt]
16483      identifier [opt] : constant-expression
16484
16485    GNU Extensions:
16486
16487    member-declaration:
16488      __extension__ member-declaration
16489
16490    member-declarator:
16491      declarator attributes [opt] pure-specifier [opt]
16492      declarator attributes [opt] constant-initializer [opt]
16493      identifier [opt] attributes [opt] : constant-expression  
16494
16495    C++0x Extensions:
16496
16497    member-declaration:
16498      static_assert-declaration  */
16499
16500 static void
16501 cp_parser_member_declaration (cp_parser* parser)
16502 {
16503   cp_decl_specifier_seq decl_specifiers;
16504   tree prefix_attributes;
16505   tree decl;
16506   int declares_class_or_enum;
16507   bool friend_p;
16508   cp_token *token = NULL;
16509   cp_token *decl_spec_token_start = NULL;
16510   cp_token *initializer_token_start = NULL;
16511   int saved_pedantic;
16512
16513   /* Check for the `__extension__' keyword.  */
16514   if (cp_parser_extension_opt (parser, &saved_pedantic))
16515     {
16516       /* Recurse.  */
16517       cp_parser_member_declaration (parser);
16518       /* Restore the old value of the PEDANTIC flag.  */
16519       pedantic = saved_pedantic;
16520
16521       return;
16522     }
16523
16524   /* Check for a template-declaration.  */
16525   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16526     {
16527       /* An explicit specialization here is an error condition, and we
16528          expect the specialization handler to detect and report this.  */
16529       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16530           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16531         cp_parser_explicit_specialization (parser);
16532       else
16533         cp_parser_template_declaration (parser, /*member_p=*/true);
16534
16535       return;
16536     }
16537
16538   /* Check for a using-declaration.  */
16539   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16540     {
16541       /* Parse the using-declaration.  */
16542       cp_parser_using_declaration (parser,
16543                                    /*access_declaration_p=*/false);
16544       return;
16545     }
16546
16547   /* Check for @defs.  */
16548   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16549     {
16550       tree ivar, member;
16551       tree ivar_chains = cp_parser_objc_defs_expression (parser);
16552       ivar = ivar_chains;
16553       while (ivar)
16554         {
16555           member = ivar;
16556           ivar = TREE_CHAIN (member);
16557           TREE_CHAIN (member) = NULL_TREE;
16558           finish_member_declaration (member);
16559         }
16560       return;
16561     }
16562
16563   /* If the next token is `static_assert' we have a static assertion.  */
16564   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16565     {
16566       cp_parser_static_assert (parser, /*member_p=*/true);
16567       return;
16568     }
16569
16570   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16571     return;
16572
16573   /* Parse the decl-specifier-seq.  */
16574   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16575   cp_parser_decl_specifier_seq (parser,
16576                                 CP_PARSER_FLAGS_OPTIONAL,
16577                                 &decl_specifiers,
16578                                 &declares_class_or_enum);
16579   prefix_attributes = decl_specifiers.attributes;
16580   decl_specifiers.attributes = NULL_TREE;
16581   /* Check for an invalid type-name.  */
16582   if (!decl_specifiers.any_type_specifiers_p
16583       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16584     return;
16585   /* If there is no declarator, then the decl-specifier-seq should
16586      specify a type.  */
16587   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16588     {
16589       /* If there was no decl-specifier-seq, and the next token is a
16590          `;', then we have something like:
16591
16592            struct S { ; };
16593
16594          [class.mem]
16595
16596          Each member-declaration shall declare at least one member
16597          name of the class.  */
16598       if (!decl_specifiers.any_specifiers_p)
16599         {
16600           cp_token *token = cp_lexer_peek_token (parser->lexer);
16601           if (!in_system_header_at (token->location))
16602             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16603         }
16604       else
16605         {
16606           tree type;
16607
16608           /* See if this declaration is a friend.  */
16609           friend_p = cp_parser_friend_p (&decl_specifiers);
16610           /* If there were decl-specifiers, check to see if there was
16611              a class-declaration.  */
16612           type = check_tag_decl (&decl_specifiers);
16613           /* Nested classes have already been added to the class, but
16614              a `friend' needs to be explicitly registered.  */
16615           if (friend_p)
16616             {
16617               /* If the `friend' keyword was present, the friend must
16618                  be introduced with a class-key.  */
16619                if (!declares_class_or_enum)
16620                  error_at (decl_spec_token_start->location,
16621                            "a class-key must be used when declaring a friend");
16622                /* In this case:
16623
16624                     template <typename T> struct A {
16625                       friend struct A<T>::B;
16626                     };
16627
16628                   A<T>::B will be represented by a TYPENAME_TYPE, and
16629                   therefore not recognized by check_tag_decl.  */
16630                if (!type
16631                    && decl_specifiers.type
16632                    && TYPE_P (decl_specifiers.type))
16633                  type = decl_specifiers.type;
16634                if (!type || !TYPE_P (type))
16635                  error_at (decl_spec_token_start->location,
16636                            "friend declaration does not name a class or "
16637                            "function");
16638                else
16639                  make_friend_class (current_class_type, type,
16640                                     /*complain=*/true);
16641             }
16642           /* If there is no TYPE, an error message will already have
16643              been issued.  */
16644           else if (!type || type == error_mark_node)
16645             ;
16646           /* An anonymous aggregate has to be handled specially; such
16647              a declaration really declares a data member (with a
16648              particular type), as opposed to a nested class.  */
16649           else if (ANON_AGGR_TYPE_P (type))
16650             {
16651               /* Remove constructors and such from TYPE, now that we
16652                  know it is an anonymous aggregate.  */
16653               fixup_anonymous_aggr (type);
16654               /* And make the corresponding data member.  */
16655               decl = build_decl (decl_spec_token_start->location,
16656                                  FIELD_DECL, NULL_TREE, type);
16657               /* Add it to the class.  */
16658               finish_member_declaration (decl);
16659             }
16660           else
16661             cp_parser_check_access_in_redeclaration
16662                                               (TYPE_NAME (type),
16663                                                decl_spec_token_start->location);
16664         }
16665     }
16666   else
16667     {
16668       /* See if these declarations will be friends.  */
16669       friend_p = cp_parser_friend_p (&decl_specifiers);
16670
16671       /* Keep going until we hit the `;' at the end of the
16672          declaration.  */
16673       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16674         {
16675           tree attributes = NULL_TREE;
16676           tree first_attribute;
16677
16678           /* Peek at the next token.  */
16679           token = cp_lexer_peek_token (parser->lexer);
16680
16681           /* Check for a bitfield declaration.  */
16682           if (token->type == CPP_COLON
16683               || (token->type == CPP_NAME
16684                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16685                   == CPP_COLON))
16686             {
16687               tree identifier;
16688               tree width;
16689
16690               /* Get the name of the bitfield.  Note that we cannot just
16691                  check TOKEN here because it may have been invalidated by
16692                  the call to cp_lexer_peek_nth_token above.  */
16693               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16694                 identifier = cp_parser_identifier (parser);
16695               else
16696                 identifier = NULL_TREE;
16697
16698               /* Consume the `:' token.  */
16699               cp_lexer_consume_token (parser->lexer);
16700               /* Get the width of the bitfield.  */
16701               width
16702                 = cp_parser_constant_expression (parser,
16703                                                  /*allow_non_constant=*/false,
16704                                                  NULL);
16705
16706               /* Look for attributes that apply to the bitfield.  */
16707               attributes = cp_parser_attributes_opt (parser);
16708               /* Remember which attributes are prefix attributes and
16709                  which are not.  */
16710               first_attribute = attributes;
16711               /* Combine the attributes.  */
16712               attributes = chainon (prefix_attributes, attributes);
16713
16714               /* Create the bitfield declaration.  */
16715               decl = grokbitfield (identifier
16716                                    ? make_id_declarator (NULL_TREE,
16717                                                          identifier,
16718                                                          sfk_none)
16719                                    : NULL,
16720                                    &decl_specifiers,
16721                                    width,
16722                                    attributes);
16723             }
16724           else
16725             {
16726               cp_declarator *declarator;
16727               tree initializer;
16728               tree asm_specification;
16729               int ctor_dtor_or_conv_p;
16730
16731               /* Parse the declarator.  */
16732               declarator
16733                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16734                                         &ctor_dtor_or_conv_p,
16735                                         /*parenthesized_p=*/NULL,
16736                                         /*member_p=*/true);
16737
16738               /* If something went wrong parsing the declarator, make sure
16739                  that we at least consume some tokens.  */
16740               if (declarator == cp_error_declarator)
16741                 {
16742                   /* Skip to the end of the statement.  */
16743                   cp_parser_skip_to_end_of_statement (parser);
16744                   /* If the next token is not a semicolon, that is
16745                      probably because we just skipped over the body of
16746                      a function.  So, we consume a semicolon if
16747                      present, but do not issue an error message if it
16748                      is not present.  */
16749                   if (cp_lexer_next_token_is (parser->lexer,
16750                                               CPP_SEMICOLON))
16751                     cp_lexer_consume_token (parser->lexer);
16752                   return;
16753                 }
16754
16755               if (declares_class_or_enum & 2)
16756                 cp_parser_check_for_definition_in_return_type
16757                                             (declarator, decl_specifiers.type,
16758                                              decl_specifiers.type_location);
16759
16760               /* Look for an asm-specification.  */
16761               asm_specification = cp_parser_asm_specification_opt (parser);
16762               /* Look for attributes that apply to the declaration.  */
16763               attributes = cp_parser_attributes_opt (parser);
16764               /* Remember which attributes are prefix attributes and
16765                  which are not.  */
16766               first_attribute = attributes;
16767               /* Combine the attributes.  */
16768               attributes = chainon (prefix_attributes, attributes);
16769
16770               /* If it's an `=', then we have a constant-initializer or a
16771                  pure-specifier.  It is not correct to parse the
16772                  initializer before registering the member declaration
16773                  since the member declaration should be in scope while
16774                  its initializer is processed.  However, the rest of the
16775                  front end does not yet provide an interface that allows
16776                  us to handle this correctly.  */
16777               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16778                 {
16779                   /* In [class.mem]:
16780
16781                      A pure-specifier shall be used only in the declaration of
16782                      a virtual function.
16783
16784                      A member-declarator can contain a constant-initializer
16785                      only if it declares a static member of integral or
16786                      enumeration type.
16787
16788                      Therefore, if the DECLARATOR is for a function, we look
16789                      for a pure-specifier; otherwise, we look for a
16790                      constant-initializer.  When we call `grokfield', it will
16791                      perform more stringent semantics checks.  */
16792                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
16793                   if (function_declarator_p (declarator))
16794                     initializer = cp_parser_pure_specifier (parser);
16795                   else
16796                     /* Parse the initializer.  */
16797                     initializer = cp_parser_constant_initializer (parser);
16798                 }
16799               /* Otherwise, there is no initializer.  */
16800               else
16801                 initializer = NULL_TREE;
16802
16803               /* See if we are probably looking at a function
16804                  definition.  We are certainly not looking at a
16805                  member-declarator.  Calling `grokfield' has
16806                  side-effects, so we must not do it unless we are sure
16807                  that we are looking at a member-declarator.  */
16808               if (cp_parser_token_starts_function_definition_p
16809                   (cp_lexer_peek_token (parser->lexer)))
16810                 {
16811                   /* The grammar does not allow a pure-specifier to be
16812                      used when a member function is defined.  (It is
16813                      possible that this fact is an oversight in the
16814                      standard, since a pure function may be defined
16815                      outside of the class-specifier.  */
16816                   if (initializer)
16817                     error_at (initializer_token_start->location,
16818                               "pure-specifier on function-definition");
16819                   decl = cp_parser_save_member_function_body (parser,
16820                                                               &decl_specifiers,
16821                                                               declarator,
16822                                                               attributes);
16823                   /* If the member was not a friend, declare it here.  */
16824                   if (!friend_p)
16825                     finish_member_declaration (decl);
16826                   /* Peek at the next token.  */
16827                   token = cp_lexer_peek_token (parser->lexer);
16828                   /* If the next token is a semicolon, consume it.  */
16829                   if (token->type == CPP_SEMICOLON)
16830                     cp_lexer_consume_token (parser->lexer);
16831                   return;
16832                 }
16833               else
16834                 if (declarator->kind == cdk_function)
16835                   declarator->id_loc = token->location;
16836                 /* Create the declaration.  */
16837                 decl = grokfield (declarator, &decl_specifiers,
16838                                   initializer, /*init_const_expr_p=*/true,
16839                                   asm_specification,
16840                                   attributes);
16841             }
16842
16843           /* Reset PREFIX_ATTRIBUTES.  */
16844           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16845             attributes = TREE_CHAIN (attributes);
16846           if (attributes)
16847             TREE_CHAIN (attributes) = NULL_TREE;
16848
16849           /* If there is any qualification still in effect, clear it
16850              now; we will be starting fresh with the next declarator.  */
16851           parser->scope = NULL_TREE;
16852           parser->qualifying_scope = NULL_TREE;
16853           parser->object_scope = NULL_TREE;
16854           /* If it's a `,', then there are more declarators.  */
16855           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16856             cp_lexer_consume_token (parser->lexer);
16857           /* If the next token isn't a `;', then we have a parse error.  */
16858           else if (cp_lexer_next_token_is_not (parser->lexer,
16859                                                CPP_SEMICOLON))
16860             {
16861               cp_parser_error (parser, "expected %<;%>");
16862               /* Skip tokens until we find a `;'.  */
16863               cp_parser_skip_to_end_of_statement (parser);
16864
16865               break;
16866             }
16867
16868           if (decl)
16869             {
16870               /* Add DECL to the list of members.  */
16871               if (!friend_p)
16872                 finish_member_declaration (decl);
16873
16874               if (TREE_CODE (decl) == FUNCTION_DECL)
16875                 cp_parser_save_default_args (parser, decl);
16876             }
16877         }
16878     }
16879
16880   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16881 }
16882
16883 /* Parse a pure-specifier.
16884
16885    pure-specifier:
16886      = 0
16887
16888    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16889    Otherwise, ERROR_MARK_NODE is returned.  */
16890
16891 static tree
16892 cp_parser_pure_specifier (cp_parser* parser)
16893 {
16894   cp_token *token;
16895
16896   /* Look for the `=' token.  */
16897   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16898     return error_mark_node;
16899   /* Look for the `0' token.  */
16900   token = cp_lexer_peek_token (parser->lexer);
16901
16902   if (token->type == CPP_EOF
16903       || token->type == CPP_PRAGMA_EOL)
16904     return error_mark_node;
16905
16906   cp_lexer_consume_token (parser->lexer);
16907
16908   /* Accept = default or = delete in c++0x mode.  */
16909   if (token->keyword == RID_DEFAULT
16910       || token->keyword == RID_DELETE)
16911     {
16912       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
16913       return token->u.value;
16914     }
16915
16916   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16917   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16918     {
16919       cp_parser_error (parser,
16920                        "invalid pure specifier (only %<= 0%> is allowed)");
16921       cp_parser_skip_to_end_of_statement (parser);
16922       return error_mark_node;
16923     }
16924   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16925     {
16926       error_at (token->location, "templates may not be %<virtual%>");
16927       return error_mark_node;
16928     }
16929
16930   return integer_zero_node;
16931 }
16932
16933 /* Parse a constant-initializer.
16934
16935    constant-initializer:
16936      = constant-expression
16937
16938    Returns a representation of the constant-expression.  */
16939
16940 static tree
16941 cp_parser_constant_initializer (cp_parser* parser)
16942 {
16943   /* Look for the `=' token.  */
16944   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16945     return error_mark_node;
16946
16947   /* It is invalid to write:
16948
16949        struct S { static const int i = { 7 }; };
16950
16951      */
16952   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16953     {
16954       cp_parser_error (parser,
16955                        "a brace-enclosed initializer is not allowed here");
16956       /* Consume the opening brace.  */
16957       cp_lexer_consume_token (parser->lexer);
16958       /* Skip the initializer.  */
16959       cp_parser_skip_to_closing_brace (parser);
16960       /* Look for the trailing `}'.  */
16961       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16962
16963       return error_mark_node;
16964     }
16965
16966   return cp_parser_constant_expression (parser,
16967                                         /*allow_non_constant=*/false,
16968                                         NULL);
16969 }
16970
16971 /* Derived classes [gram.class.derived] */
16972
16973 /* Parse a base-clause.
16974
16975    base-clause:
16976      : base-specifier-list
16977
16978    base-specifier-list:
16979      base-specifier ... [opt]
16980      base-specifier-list , base-specifier ... [opt]
16981
16982    Returns a TREE_LIST representing the base-classes, in the order in
16983    which they were declared.  The representation of each node is as
16984    described by cp_parser_base_specifier.
16985
16986    In the case that no bases are specified, this function will return
16987    NULL_TREE, not ERROR_MARK_NODE.  */
16988
16989 static tree
16990 cp_parser_base_clause (cp_parser* parser)
16991 {
16992   tree bases = NULL_TREE;
16993
16994   /* Look for the `:' that begins the list.  */
16995   cp_parser_require (parser, CPP_COLON, "%<:%>");
16996
16997   /* Scan the base-specifier-list.  */
16998   while (true)
16999     {
17000       cp_token *token;
17001       tree base;
17002       bool pack_expansion_p = false;
17003
17004       /* Look for the base-specifier.  */
17005       base = cp_parser_base_specifier (parser);
17006       /* Look for the (optional) ellipsis. */
17007       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17008         {
17009           /* Consume the `...'. */
17010           cp_lexer_consume_token (parser->lexer);
17011
17012           pack_expansion_p = true;
17013         }
17014
17015       /* Add BASE to the front of the list.  */
17016       if (base != error_mark_node)
17017         {
17018           if (pack_expansion_p)
17019             /* Make this a pack expansion type. */
17020             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17021           
17022
17023           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17024             {
17025               TREE_CHAIN (base) = bases;
17026               bases = base;
17027             }
17028         }
17029       /* Peek at the next token.  */
17030       token = cp_lexer_peek_token (parser->lexer);
17031       /* If it's not a comma, then the list is complete.  */
17032       if (token->type != CPP_COMMA)
17033         break;
17034       /* Consume the `,'.  */
17035       cp_lexer_consume_token (parser->lexer);
17036     }
17037
17038   /* PARSER->SCOPE may still be non-NULL at this point, if the last
17039      base class had a qualified name.  However, the next name that
17040      appears is certainly not qualified.  */
17041   parser->scope = NULL_TREE;
17042   parser->qualifying_scope = NULL_TREE;
17043   parser->object_scope = NULL_TREE;
17044
17045   return nreverse (bases);
17046 }
17047
17048 /* Parse a base-specifier.
17049
17050    base-specifier:
17051      :: [opt] nested-name-specifier [opt] class-name
17052      virtual access-specifier [opt] :: [opt] nested-name-specifier
17053        [opt] class-name
17054      access-specifier virtual [opt] :: [opt] nested-name-specifier
17055        [opt] class-name
17056
17057    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
17058    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17059    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
17060    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
17061
17062 static tree
17063 cp_parser_base_specifier (cp_parser* parser)
17064 {
17065   cp_token *token;
17066   bool done = false;
17067   bool virtual_p = false;
17068   bool duplicate_virtual_error_issued_p = false;
17069   bool duplicate_access_error_issued_p = false;
17070   bool class_scope_p, template_p;
17071   tree access = access_default_node;
17072   tree type;
17073
17074   /* Process the optional `virtual' and `access-specifier'.  */
17075   while (!done)
17076     {
17077       /* Peek at the next token.  */
17078       token = cp_lexer_peek_token (parser->lexer);
17079       /* Process `virtual'.  */
17080       switch (token->keyword)
17081         {
17082         case RID_VIRTUAL:
17083           /* If `virtual' appears more than once, issue an error.  */
17084           if (virtual_p && !duplicate_virtual_error_issued_p)
17085             {
17086               cp_parser_error (parser,
17087                                "%<virtual%> specified more than once in base-specified");
17088               duplicate_virtual_error_issued_p = true;
17089             }
17090
17091           virtual_p = true;
17092
17093           /* Consume the `virtual' token.  */
17094           cp_lexer_consume_token (parser->lexer);
17095
17096           break;
17097
17098         case RID_PUBLIC:
17099         case RID_PROTECTED:
17100         case RID_PRIVATE:
17101           /* If more than one access specifier appears, issue an
17102              error.  */
17103           if (access != access_default_node
17104               && !duplicate_access_error_issued_p)
17105             {
17106               cp_parser_error (parser,
17107                                "more than one access specifier in base-specified");
17108               duplicate_access_error_issued_p = true;
17109             }
17110
17111           access = ridpointers[(int) token->keyword];
17112
17113           /* Consume the access-specifier.  */
17114           cp_lexer_consume_token (parser->lexer);
17115
17116           break;
17117
17118         default:
17119           done = true;
17120           break;
17121         }
17122     }
17123   /* It is not uncommon to see programs mechanically, erroneously, use
17124      the 'typename' keyword to denote (dependent) qualified types
17125      as base classes.  */
17126   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17127     {
17128       token = cp_lexer_peek_token (parser->lexer);
17129       if (!processing_template_decl)
17130         error_at (token->location,
17131                   "keyword %<typename%> not allowed outside of templates");
17132       else
17133         error_at (token->location,
17134                   "keyword %<typename%> not allowed in this context "
17135                   "(the base class is implicitly a type)");
17136       cp_lexer_consume_token (parser->lexer);
17137     }
17138
17139   /* Look for the optional `::' operator.  */
17140   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17141   /* Look for the nested-name-specifier.  The simplest way to
17142      implement:
17143
17144        [temp.res]
17145
17146        The keyword `typename' is not permitted in a base-specifier or
17147        mem-initializer; in these contexts a qualified name that
17148        depends on a template-parameter is implicitly assumed to be a
17149        type name.
17150
17151      is to pretend that we have seen the `typename' keyword at this
17152      point.  */
17153   cp_parser_nested_name_specifier_opt (parser,
17154                                        /*typename_keyword_p=*/true,
17155                                        /*check_dependency_p=*/true,
17156                                        typename_type,
17157                                        /*is_declaration=*/true);
17158   /* If the base class is given by a qualified name, assume that names
17159      we see are type names or templates, as appropriate.  */
17160   class_scope_p = (parser->scope && TYPE_P (parser->scope));
17161   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17162
17163   /* Finally, look for the class-name.  */
17164   type = cp_parser_class_name (parser,
17165                                class_scope_p,
17166                                template_p,
17167                                typename_type,
17168                                /*check_dependency_p=*/true,
17169                                /*class_head_p=*/false,
17170                                /*is_declaration=*/true);
17171
17172   if (type == error_mark_node)
17173     return error_mark_node;
17174
17175   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17176 }
17177
17178 /* Exception handling [gram.exception] */
17179
17180 /* Parse an (optional) exception-specification.
17181
17182    exception-specification:
17183      throw ( type-id-list [opt] )
17184
17185    Returns a TREE_LIST representing the exception-specification.  The
17186    TREE_VALUE of each node is a type.  */
17187
17188 static tree
17189 cp_parser_exception_specification_opt (cp_parser* parser)
17190 {
17191   cp_token *token;
17192   tree type_id_list;
17193
17194   /* Peek at the next token.  */
17195   token = cp_lexer_peek_token (parser->lexer);
17196   /* If it's not `throw', then there's no exception-specification.  */
17197   if (!cp_parser_is_keyword (token, RID_THROW))
17198     return NULL_TREE;
17199
17200   /* Consume the `throw'.  */
17201   cp_lexer_consume_token (parser->lexer);
17202
17203   /* Look for the `('.  */
17204   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17205
17206   /* Peek at the next token.  */
17207   token = cp_lexer_peek_token (parser->lexer);
17208   /* If it's not a `)', then there is a type-id-list.  */
17209   if (token->type != CPP_CLOSE_PAREN)
17210     {
17211       const char *saved_message;
17212
17213       /* Types may not be defined in an exception-specification.  */
17214       saved_message = parser->type_definition_forbidden_message;
17215       parser->type_definition_forbidden_message
17216         = G_("types may not be defined in an exception-specification");
17217       /* Parse the type-id-list.  */
17218       type_id_list = cp_parser_type_id_list (parser);
17219       /* Restore the saved message.  */
17220       parser->type_definition_forbidden_message = saved_message;
17221     }
17222   else
17223     type_id_list = empty_except_spec;
17224
17225   /* Look for the `)'.  */
17226   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17227
17228   return type_id_list;
17229 }
17230
17231 /* Parse an (optional) type-id-list.
17232
17233    type-id-list:
17234      type-id ... [opt]
17235      type-id-list , type-id ... [opt]
17236
17237    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
17238    in the order that the types were presented.  */
17239
17240 static tree
17241 cp_parser_type_id_list (cp_parser* parser)
17242 {
17243   tree types = NULL_TREE;
17244
17245   while (true)
17246     {
17247       cp_token *token;
17248       tree type;
17249
17250       /* Get the next type-id.  */
17251       type = cp_parser_type_id (parser);
17252       /* Parse the optional ellipsis. */
17253       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17254         {
17255           /* Consume the `...'. */
17256           cp_lexer_consume_token (parser->lexer);
17257
17258           /* Turn the type into a pack expansion expression. */
17259           type = make_pack_expansion (type);
17260         }
17261       /* Add it to the list.  */
17262       types = add_exception_specifier (types, type, /*complain=*/1);
17263       /* Peek at the next token.  */
17264       token = cp_lexer_peek_token (parser->lexer);
17265       /* If it is not a `,', we are done.  */
17266       if (token->type != CPP_COMMA)
17267         break;
17268       /* Consume the `,'.  */
17269       cp_lexer_consume_token (parser->lexer);
17270     }
17271
17272   return nreverse (types);
17273 }
17274
17275 /* Parse a try-block.
17276
17277    try-block:
17278      try compound-statement handler-seq  */
17279
17280 static tree
17281 cp_parser_try_block (cp_parser* parser)
17282 {
17283   tree try_block;
17284
17285   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17286   try_block = begin_try_block ();
17287   cp_parser_compound_statement (parser, NULL, true);
17288   finish_try_block (try_block);
17289   cp_parser_handler_seq (parser);
17290   finish_handler_sequence (try_block);
17291
17292   return try_block;
17293 }
17294
17295 /* Parse a function-try-block.
17296
17297    function-try-block:
17298      try ctor-initializer [opt] function-body handler-seq  */
17299
17300 static bool
17301 cp_parser_function_try_block (cp_parser* parser)
17302 {
17303   tree compound_stmt;
17304   tree try_block;
17305   bool ctor_initializer_p;
17306
17307   /* Look for the `try' keyword.  */
17308   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17309     return false;
17310   /* Let the rest of the front end know where we are.  */
17311   try_block = begin_function_try_block (&compound_stmt);
17312   /* Parse the function-body.  */
17313   ctor_initializer_p
17314     = cp_parser_ctor_initializer_opt_and_function_body (parser);
17315   /* We're done with the `try' part.  */
17316   finish_function_try_block (try_block);
17317   /* Parse the handlers.  */
17318   cp_parser_handler_seq (parser);
17319   /* We're done with the handlers.  */
17320   finish_function_handler_sequence (try_block, compound_stmt);
17321
17322   return ctor_initializer_p;
17323 }
17324
17325 /* Parse a handler-seq.
17326
17327    handler-seq:
17328      handler handler-seq [opt]  */
17329
17330 static void
17331 cp_parser_handler_seq (cp_parser* parser)
17332 {
17333   while (true)
17334     {
17335       cp_token *token;
17336
17337       /* Parse the handler.  */
17338       cp_parser_handler (parser);
17339       /* Peek at the next token.  */
17340       token = cp_lexer_peek_token (parser->lexer);
17341       /* If it's not `catch' then there are no more handlers.  */
17342       if (!cp_parser_is_keyword (token, RID_CATCH))
17343         break;
17344     }
17345 }
17346
17347 /* Parse a handler.
17348
17349    handler:
17350      catch ( exception-declaration ) compound-statement  */
17351
17352 static void
17353 cp_parser_handler (cp_parser* parser)
17354 {
17355   tree handler;
17356   tree declaration;
17357
17358   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17359   handler = begin_handler ();
17360   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17361   declaration = cp_parser_exception_declaration (parser);
17362   finish_handler_parms (declaration, handler);
17363   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17364   cp_parser_compound_statement (parser, NULL, false);
17365   finish_handler (handler);
17366 }
17367
17368 /* Parse an exception-declaration.
17369
17370    exception-declaration:
17371      type-specifier-seq declarator
17372      type-specifier-seq abstract-declarator
17373      type-specifier-seq
17374      ...
17375
17376    Returns a VAR_DECL for the declaration, or NULL_TREE if the
17377    ellipsis variant is used.  */
17378
17379 static tree
17380 cp_parser_exception_declaration (cp_parser* parser)
17381 {
17382   cp_decl_specifier_seq type_specifiers;
17383   cp_declarator *declarator;
17384   const char *saved_message;
17385
17386   /* If it's an ellipsis, it's easy to handle.  */
17387   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17388     {
17389       /* Consume the `...' token.  */
17390       cp_lexer_consume_token (parser->lexer);
17391       return NULL_TREE;
17392     }
17393
17394   /* Types may not be defined in exception-declarations.  */
17395   saved_message = parser->type_definition_forbidden_message;
17396   parser->type_definition_forbidden_message
17397     = G_("types may not be defined in exception-declarations");
17398
17399   /* Parse the type-specifier-seq.  */
17400   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17401                                 /*is_trailing_return=*/false,
17402                                 &type_specifiers);
17403   /* If it's a `)', then there is no declarator.  */
17404   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17405     declarator = NULL;
17406   else
17407     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17408                                        /*ctor_dtor_or_conv_p=*/NULL,
17409                                        /*parenthesized_p=*/NULL,
17410                                        /*member_p=*/false);
17411
17412   /* Restore the saved message.  */
17413   parser->type_definition_forbidden_message = saved_message;
17414
17415   if (!type_specifiers.any_specifiers_p)
17416     return error_mark_node;
17417
17418   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17419 }
17420
17421 /* Parse a throw-expression.
17422
17423    throw-expression:
17424      throw assignment-expression [opt]
17425
17426    Returns a THROW_EXPR representing the throw-expression.  */
17427
17428 static tree
17429 cp_parser_throw_expression (cp_parser* parser)
17430 {
17431   tree expression;
17432   cp_token* token;
17433
17434   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17435   token = cp_lexer_peek_token (parser->lexer);
17436   /* Figure out whether or not there is an assignment-expression
17437      following the "throw" keyword.  */
17438   if (token->type == CPP_COMMA
17439       || token->type == CPP_SEMICOLON
17440       || token->type == CPP_CLOSE_PAREN
17441       || token->type == CPP_CLOSE_SQUARE
17442       || token->type == CPP_CLOSE_BRACE
17443       || token->type == CPP_COLON)
17444     expression = NULL_TREE;
17445   else
17446     expression = cp_parser_assignment_expression (parser,
17447                                                   /*cast_p=*/false, NULL);
17448
17449   return build_throw (expression);
17450 }
17451
17452 /* GNU Extensions */
17453
17454 /* Parse an (optional) asm-specification.
17455
17456    asm-specification:
17457      asm ( string-literal )
17458
17459    If the asm-specification is present, returns a STRING_CST
17460    corresponding to the string-literal.  Otherwise, returns
17461    NULL_TREE.  */
17462
17463 static tree
17464 cp_parser_asm_specification_opt (cp_parser* parser)
17465 {
17466   cp_token *token;
17467   tree asm_specification;
17468
17469   /* Peek at the next token.  */
17470   token = cp_lexer_peek_token (parser->lexer);
17471   /* If the next token isn't the `asm' keyword, then there's no
17472      asm-specification.  */
17473   if (!cp_parser_is_keyword (token, RID_ASM))
17474     return NULL_TREE;
17475
17476   /* Consume the `asm' token.  */
17477   cp_lexer_consume_token (parser->lexer);
17478   /* Look for the `('.  */
17479   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17480
17481   /* Look for the string-literal.  */
17482   asm_specification = cp_parser_string_literal (parser, false, false);
17483
17484   /* Look for the `)'.  */
17485   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17486
17487   return asm_specification;
17488 }
17489
17490 /* Parse an asm-operand-list.
17491
17492    asm-operand-list:
17493      asm-operand
17494      asm-operand-list , asm-operand
17495
17496    asm-operand:
17497      string-literal ( expression )
17498      [ string-literal ] string-literal ( expression )
17499
17500    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
17501    each node is the expression.  The TREE_PURPOSE is itself a
17502    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17503    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17504    is a STRING_CST for the string literal before the parenthesis. Returns
17505    ERROR_MARK_NODE if any of the operands are invalid.  */
17506
17507 static tree
17508 cp_parser_asm_operand_list (cp_parser* parser)
17509 {
17510   tree asm_operands = NULL_TREE;
17511   bool invalid_operands = false;
17512
17513   while (true)
17514     {
17515       tree string_literal;
17516       tree expression;
17517       tree name;
17518
17519       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17520         {
17521           /* Consume the `[' token.  */
17522           cp_lexer_consume_token (parser->lexer);
17523           /* Read the operand name.  */
17524           name = cp_parser_identifier (parser);
17525           if (name != error_mark_node)
17526             name = build_string (IDENTIFIER_LENGTH (name),
17527                                  IDENTIFIER_POINTER (name));
17528           /* Look for the closing `]'.  */
17529           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17530         }
17531       else
17532         name = NULL_TREE;
17533       /* Look for the string-literal.  */
17534       string_literal = cp_parser_string_literal (parser, false, false);
17535
17536       /* Look for the `('.  */
17537       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17538       /* Parse the expression.  */
17539       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17540       /* Look for the `)'.  */
17541       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17542
17543       if (name == error_mark_node 
17544           || string_literal == error_mark_node 
17545           || expression == error_mark_node)
17546         invalid_operands = true;
17547
17548       /* Add this operand to the list.  */
17549       asm_operands = tree_cons (build_tree_list (name, string_literal),
17550                                 expression,
17551                                 asm_operands);
17552       /* If the next token is not a `,', there are no more
17553          operands.  */
17554       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17555         break;
17556       /* Consume the `,'.  */
17557       cp_lexer_consume_token (parser->lexer);
17558     }
17559
17560   return invalid_operands ? error_mark_node : nreverse (asm_operands);
17561 }
17562
17563 /* Parse an asm-clobber-list.
17564
17565    asm-clobber-list:
17566      string-literal
17567      asm-clobber-list , string-literal
17568
17569    Returns a TREE_LIST, indicating the clobbers in the order that they
17570    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
17571
17572 static tree
17573 cp_parser_asm_clobber_list (cp_parser* parser)
17574 {
17575   tree clobbers = NULL_TREE;
17576
17577   while (true)
17578     {
17579       tree string_literal;
17580
17581       /* Look for the string literal.  */
17582       string_literal = cp_parser_string_literal (parser, false, false);
17583       /* Add it to the list.  */
17584       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17585       /* If the next token is not a `,', then the list is
17586          complete.  */
17587       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17588         break;
17589       /* Consume the `,' token.  */
17590       cp_lexer_consume_token (parser->lexer);
17591     }
17592
17593   return clobbers;
17594 }
17595
17596 /* Parse an asm-label-list.
17597
17598    asm-label-list:
17599      identifier
17600      asm-label-list , identifier
17601
17602    Returns a TREE_LIST, indicating the labels in the order that they
17603    appeared.  The TREE_VALUE of each node is a label.  */
17604
17605 static tree
17606 cp_parser_asm_label_list (cp_parser* parser)
17607 {
17608   tree labels = NULL_TREE;
17609
17610   while (true)
17611     {
17612       tree identifier, label, name;
17613
17614       /* Look for the identifier.  */
17615       identifier = cp_parser_identifier (parser);
17616       if (!error_operand_p (identifier))
17617         {
17618           label = lookup_label (identifier);
17619           if (TREE_CODE (label) == LABEL_DECL)
17620             {
17621               TREE_USED (label) = 1;
17622               check_goto (label);
17623               name = build_string (IDENTIFIER_LENGTH (identifier),
17624                                    IDENTIFIER_POINTER (identifier));
17625               labels = tree_cons (name, label, labels);
17626             }
17627         }
17628       /* If the next token is not a `,', then the list is
17629          complete.  */
17630       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17631         break;
17632       /* Consume the `,' token.  */
17633       cp_lexer_consume_token (parser->lexer);
17634     }
17635
17636   return nreverse (labels);
17637 }
17638
17639 /* Parse an (optional) series of attributes.
17640
17641    attributes:
17642      attributes attribute
17643
17644    attribute:
17645      __attribute__ (( attribute-list [opt] ))
17646
17647    The return value is as for cp_parser_attribute_list.  */
17648
17649 static tree
17650 cp_parser_attributes_opt (cp_parser* parser)
17651 {
17652   tree attributes = NULL_TREE;
17653
17654   while (true)
17655     {
17656       cp_token *token;
17657       tree attribute_list;
17658
17659       /* Peek at the next token.  */
17660       token = cp_lexer_peek_token (parser->lexer);
17661       /* If it's not `__attribute__', then we're done.  */
17662       if (token->keyword != RID_ATTRIBUTE)
17663         break;
17664
17665       /* Consume the `__attribute__' keyword.  */
17666       cp_lexer_consume_token (parser->lexer);
17667       /* Look for the two `(' tokens.  */
17668       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17669       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17670
17671       /* Peek at the next token.  */
17672       token = cp_lexer_peek_token (parser->lexer);
17673       if (token->type != CPP_CLOSE_PAREN)
17674         /* Parse the attribute-list.  */
17675         attribute_list = cp_parser_attribute_list (parser);
17676       else
17677         /* If the next token is a `)', then there is no attribute
17678            list.  */
17679         attribute_list = NULL;
17680
17681       /* Look for the two `)' tokens.  */
17682       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17683       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17684
17685       /* Add these new attributes to the list.  */
17686       attributes = chainon (attributes, attribute_list);
17687     }
17688
17689   return attributes;
17690 }
17691
17692 /* Parse an attribute-list.
17693
17694    attribute-list:
17695      attribute
17696      attribute-list , attribute
17697
17698    attribute:
17699      identifier
17700      identifier ( identifier )
17701      identifier ( identifier , expression-list )
17702      identifier ( expression-list )
17703
17704    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
17705    to an attribute.  The TREE_PURPOSE of each node is the identifier
17706    indicating which attribute is in use.  The TREE_VALUE represents
17707    the arguments, if any.  */
17708
17709 static tree
17710 cp_parser_attribute_list (cp_parser* parser)
17711 {
17712   tree attribute_list = NULL_TREE;
17713   bool save_translate_strings_p = parser->translate_strings_p;
17714
17715   parser->translate_strings_p = false;
17716   while (true)
17717     {
17718       cp_token *token;
17719       tree identifier;
17720       tree attribute;
17721
17722       /* Look for the identifier.  We also allow keywords here; for
17723          example `__attribute__ ((const))' is legal.  */
17724       token = cp_lexer_peek_token (parser->lexer);
17725       if (token->type == CPP_NAME
17726           || token->type == CPP_KEYWORD)
17727         {
17728           tree arguments = NULL_TREE;
17729
17730           /* Consume the token.  */
17731           token = cp_lexer_consume_token (parser->lexer);
17732
17733           /* Save away the identifier that indicates which attribute
17734              this is.  */
17735           identifier = (token->type == CPP_KEYWORD) 
17736             /* For keywords, use the canonical spelling, not the
17737                parsed identifier.  */
17738             ? ridpointers[(int) token->keyword]
17739             : token->u.value;
17740           
17741           attribute = build_tree_list (identifier, NULL_TREE);
17742
17743           /* Peek at the next token.  */
17744           token = cp_lexer_peek_token (parser->lexer);
17745           /* If it's an `(', then parse the attribute arguments.  */
17746           if (token->type == CPP_OPEN_PAREN)
17747             {
17748               VEC(tree,gc) *vec;
17749               vec = cp_parser_parenthesized_expression_list
17750                     (parser, true, /*cast_p=*/false,
17751                      /*allow_expansion_p=*/false,
17752                      /*non_constant_p=*/NULL);
17753               if (vec == NULL)
17754                 arguments = error_mark_node;
17755               else
17756                 {
17757                   arguments = build_tree_list_vec (vec);
17758                   release_tree_vector (vec);
17759                 }
17760               /* Save the arguments away.  */
17761               TREE_VALUE (attribute) = arguments;
17762             }
17763
17764           if (arguments != error_mark_node)
17765             {
17766               /* Add this attribute to the list.  */
17767               TREE_CHAIN (attribute) = attribute_list;
17768               attribute_list = attribute;
17769             }
17770
17771           token = cp_lexer_peek_token (parser->lexer);
17772         }
17773       /* Now, look for more attributes.  If the next token isn't a
17774          `,', we're done.  */
17775       if (token->type != CPP_COMMA)
17776         break;
17777
17778       /* Consume the comma and keep going.  */
17779       cp_lexer_consume_token (parser->lexer);
17780     }
17781   parser->translate_strings_p = save_translate_strings_p;
17782
17783   /* We built up the list in reverse order.  */
17784   return nreverse (attribute_list);
17785 }
17786
17787 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
17788    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
17789    current value of the PEDANTIC flag, regardless of whether or not
17790    the `__extension__' keyword is present.  The caller is responsible
17791    for restoring the value of the PEDANTIC flag.  */
17792
17793 static bool
17794 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17795 {
17796   /* Save the old value of the PEDANTIC flag.  */
17797   *saved_pedantic = pedantic;
17798
17799   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17800     {
17801       /* Consume the `__extension__' token.  */
17802       cp_lexer_consume_token (parser->lexer);
17803       /* We're not being pedantic while the `__extension__' keyword is
17804          in effect.  */
17805       pedantic = 0;
17806
17807       return true;
17808     }
17809
17810   return false;
17811 }
17812
17813 /* Parse a label declaration.
17814
17815    label-declaration:
17816      __label__ label-declarator-seq ;
17817
17818    label-declarator-seq:
17819      identifier , label-declarator-seq
17820      identifier  */
17821
17822 static void
17823 cp_parser_label_declaration (cp_parser* parser)
17824 {
17825   /* Look for the `__label__' keyword.  */
17826   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17827
17828   while (true)
17829     {
17830       tree identifier;
17831
17832       /* Look for an identifier.  */
17833       identifier = cp_parser_identifier (parser);
17834       /* If we failed, stop.  */
17835       if (identifier == error_mark_node)
17836         break;
17837       /* Declare it as a label.  */
17838       finish_label_decl (identifier);
17839       /* If the next token is a `;', stop.  */
17840       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17841         break;
17842       /* Look for the `,' separating the label declarations.  */
17843       cp_parser_require (parser, CPP_COMMA, "%<,%>");
17844     }
17845
17846   /* Look for the final `;'.  */
17847   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17848 }
17849
17850 /* Support Functions */
17851
17852 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17853    NAME should have one of the representations used for an
17854    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17855    is returned.  If PARSER->SCOPE is a dependent type, then a
17856    SCOPE_REF is returned.
17857
17858    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17859    returned; the name was already resolved when the TEMPLATE_ID_EXPR
17860    was formed.  Abstractly, such entities should not be passed to this
17861    function, because they do not need to be looked up, but it is
17862    simpler to check for this special case here, rather than at the
17863    call-sites.
17864
17865    In cases not explicitly covered above, this function returns a
17866    DECL, OVERLOAD, or baselink representing the result of the lookup.
17867    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17868    is returned.
17869
17870    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17871    (e.g., "struct") that was used.  In that case bindings that do not
17872    refer to types are ignored.
17873
17874    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17875    ignored.
17876
17877    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17878    are ignored.
17879
17880    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17881    types.
17882
17883    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17884    TREE_LIST of candidates if name-lookup results in an ambiguity, and
17885    NULL_TREE otherwise.  */
17886
17887 static tree
17888 cp_parser_lookup_name (cp_parser *parser, tree name,
17889                        enum tag_types tag_type,
17890                        bool is_template,
17891                        bool is_namespace,
17892                        bool check_dependency,
17893                        tree *ambiguous_decls,
17894                        location_t name_location)
17895 {
17896   int flags = 0;
17897   tree decl;
17898   tree object_type = parser->context->object_type;
17899
17900   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17901     flags |= LOOKUP_COMPLAIN;
17902
17903   /* Assume that the lookup will be unambiguous.  */
17904   if (ambiguous_decls)
17905     *ambiguous_decls = NULL_TREE;
17906
17907   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17908      no longer valid.  Note that if we are parsing tentatively, and
17909      the parse fails, OBJECT_TYPE will be automatically restored.  */
17910   parser->context->object_type = NULL_TREE;
17911
17912   if (name == error_mark_node)
17913     return error_mark_node;
17914
17915   /* A template-id has already been resolved; there is no lookup to
17916      do.  */
17917   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17918     return name;
17919   if (BASELINK_P (name))
17920     {
17921       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17922                   == TEMPLATE_ID_EXPR);
17923       return name;
17924     }
17925
17926   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17927      it should already have been checked to make sure that the name
17928      used matches the type being destroyed.  */
17929   if (TREE_CODE (name) == BIT_NOT_EXPR)
17930     {
17931       tree type;
17932
17933       /* Figure out to which type this destructor applies.  */
17934       if (parser->scope)
17935         type = parser->scope;
17936       else if (object_type)
17937         type = object_type;
17938       else
17939         type = current_class_type;
17940       /* If that's not a class type, there is no destructor.  */
17941       if (!type || !CLASS_TYPE_P (type))
17942         return error_mark_node;
17943       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17944         lazily_declare_fn (sfk_destructor, type);
17945       if (!CLASSTYPE_DESTRUCTORS (type))
17946           return error_mark_node;
17947       /* If it was a class type, return the destructor.  */
17948       return CLASSTYPE_DESTRUCTORS (type);
17949     }
17950
17951   /* By this point, the NAME should be an ordinary identifier.  If
17952      the id-expression was a qualified name, the qualifying scope is
17953      stored in PARSER->SCOPE at this point.  */
17954   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17955
17956   /* Perform the lookup.  */
17957   if (parser->scope)
17958     {
17959       bool dependent_p;
17960
17961       if (parser->scope == error_mark_node)
17962         return error_mark_node;
17963
17964       /* If the SCOPE is dependent, the lookup must be deferred until
17965          the template is instantiated -- unless we are explicitly
17966          looking up names in uninstantiated templates.  Even then, we
17967          cannot look up the name if the scope is not a class type; it
17968          might, for example, be a template type parameter.  */
17969       dependent_p = (TYPE_P (parser->scope)
17970                      && dependent_scope_p (parser->scope));
17971       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17972           && dependent_p)
17973         /* Defer lookup.  */
17974         decl = error_mark_node;
17975       else
17976         {
17977           tree pushed_scope = NULL_TREE;
17978
17979           /* If PARSER->SCOPE is a dependent type, then it must be a
17980              class type, and we must not be checking dependencies;
17981              otherwise, we would have processed this lookup above.  So
17982              that PARSER->SCOPE is not considered a dependent base by
17983              lookup_member, we must enter the scope here.  */
17984           if (dependent_p)
17985             pushed_scope = push_scope (parser->scope);
17986
17987           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
17988              lookup result and the nested-name-specifier nominates a class C:
17989                * if the name specified after the nested-name-specifier, when
17990                looked up in C, is the injected-class-name of C (Clause 9), or
17991                * if the name specified after the nested-name-specifier is the
17992                same as the identifier or the simple-template-id's template-
17993                name in the last component of the nested-name-specifier,
17994              the name is instead considered to name the constructor of
17995              class C. [ Note: for example, the constructor is not an
17996              acceptable lookup result in an elaborated-type-specifier so
17997              the constructor would not be used in place of the
17998              injected-class-name. --end note ] Such a constructor name
17999              shall be used only in the declarator-id of a declaration that
18000              names a constructor or in a using-declaration.  */
18001           if (tag_type == none_type
18002               && CLASS_TYPE_P (parser->scope)
18003               && constructor_name_p (name, parser->scope))
18004             name = ctor_identifier;
18005
18006           /* If the PARSER->SCOPE is a template specialization, it
18007              may be instantiated during name lookup.  In that case,
18008              errors may be issued.  Even if we rollback the current
18009              tentative parse, those errors are valid.  */
18010           decl = lookup_qualified_name (parser->scope, name,
18011                                         tag_type != none_type,
18012                                         /*complain=*/true);
18013
18014           /* If we have a single function from a using decl, pull it out.  */
18015           if (TREE_CODE (decl) == OVERLOAD
18016               && !really_overloaded_fn (decl))
18017             decl = OVL_FUNCTION (decl);
18018
18019           if (pushed_scope)
18020             pop_scope (pushed_scope);
18021         }
18022
18023       /* If the scope is a dependent type and either we deferred lookup or
18024          we did lookup but didn't find the name, rememeber the name.  */
18025       if (decl == error_mark_node && TYPE_P (parser->scope)
18026           && dependent_type_p (parser->scope))
18027         {
18028           if (tag_type)
18029             {
18030               tree type;
18031
18032               /* The resolution to Core Issue 180 says that `struct
18033                  A::B' should be considered a type-name, even if `A'
18034                  is dependent.  */
18035               type = make_typename_type (parser->scope, name, tag_type,
18036                                          /*complain=*/tf_error);
18037               decl = TYPE_NAME (type);
18038             }
18039           else if (is_template
18040                    && (cp_parser_next_token_ends_template_argument_p (parser)
18041                        || cp_lexer_next_token_is (parser->lexer,
18042                                                   CPP_CLOSE_PAREN)))
18043             decl = make_unbound_class_template (parser->scope,
18044                                                 name, NULL_TREE,
18045                                                 /*complain=*/tf_error);
18046           else
18047             decl = build_qualified_name (/*type=*/NULL_TREE,
18048                                          parser->scope, name,
18049                                          is_template);
18050         }
18051       parser->qualifying_scope = parser->scope;
18052       parser->object_scope = NULL_TREE;
18053     }
18054   else if (object_type)
18055     {
18056       tree object_decl = NULL_TREE;
18057       /* Look up the name in the scope of the OBJECT_TYPE, unless the
18058          OBJECT_TYPE is not a class.  */
18059       if (CLASS_TYPE_P (object_type))
18060         /* If the OBJECT_TYPE is a template specialization, it may
18061            be instantiated during name lookup.  In that case, errors
18062            may be issued.  Even if we rollback the current tentative
18063            parse, those errors are valid.  */
18064         object_decl = lookup_member (object_type,
18065                                      name,
18066                                      /*protect=*/0,
18067                                      tag_type != none_type);
18068       /* Look it up in the enclosing context, too.  */
18069       decl = lookup_name_real (name, tag_type != none_type,
18070                                /*nonclass=*/0,
18071                                /*block_p=*/true, is_namespace, flags);
18072       parser->object_scope = object_type;
18073       parser->qualifying_scope = NULL_TREE;
18074       if (object_decl)
18075         decl = object_decl;
18076     }
18077   else
18078     {
18079       decl = lookup_name_real (name, tag_type != none_type,
18080                                /*nonclass=*/0,
18081                                /*block_p=*/true, is_namespace, flags);
18082       parser->qualifying_scope = NULL_TREE;
18083       parser->object_scope = NULL_TREE;
18084     }
18085
18086   /* If the lookup failed, let our caller know.  */
18087   if (!decl || decl == error_mark_node)
18088     return error_mark_node;
18089
18090   /* Pull out the template from an injected-class-name (or multiple).  */
18091   if (is_template)
18092     decl = maybe_get_template_decl_from_type_decl (decl);
18093
18094   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
18095   if (TREE_CODE (decl) == TREE_LIST)
18096     {
18097       if (ambiguous_decls)
18098         *ambiguous_decls = decl;
18099       /* The error message we have to print is too complicated for
18100          cp_parser_error, so we incorporate its actions directly.  */
18101       if (!cp_parser_simulate_error (parser))
18102         {
18103           error_at (name_location, "reference to %qD is ambiguous",
18104                     name);
18105           print_candidates (decl);
18106         }
18107       return error_mark_node;
18108     }
18109
18110   gcc_assert (DECL_P (decl)
18111               || TREE_CODE (decl) == OVERLOAD
18112               || TREE_CODE (decl) == SCOPE_REF
18113               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18114               || BASELINK_P (decl));
18115
18116   /* If we have resolved the name of a member declaration, check to
18117      see if the declaration is accessible.  When the name resolves to
18118      set of overloaded functions, accessibility is checked when
18119      overload resolution is done.
18120
18121      During an explicit instantiation, access is not checked at all,
18122      as per [temp.explicit].  */
18123   if (DECL_P (decl))
18124     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18125
18126   return decl;
18127 }
18128
18129 /* Like cp_parser_lookup_name, but for use in the typical case where
18130    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18131    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
18132
18133 static tree
18134 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18135 {
18136   return cp_parser_lookup_name (parser, name,
18137                                 none_type,
18138                                 /*is_template=*/false,
18139                                 /*is_namespace=*/false,
18140                                 /*check_dependency=*/true,
18141                                 /*ambiguous_decls=*/NULL,
18142                                 location);
18143 }
18144
18145 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18146    the current context, return the TYPE_DECL.  If TAG_NAME_P is
18147    true, the DECL indicates the class being defined in a class-head,
18148    or declared in an elaborated-type-specifier.
18149
18150    Otherwise, return DECL.  */
18151
18152 static tree
18153 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18154 {
18155   /* If the TEMPLATE_DECL is being declared as part of a class-head,
18156      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18157
18158        struct A {
18159          template <typename T> struct B;
18160        };
18161
18162        template <typename T> struct A::B {};
18163
18164      Similarly, in an elaborated-type-specifier:
18165
18166        namespace N { struct X{}; }
18167
18168        struct A {
18169          template <typename T> friend struct N::X;
18170        };
18171
18172      However, if the DECL refers to a class type, and we are in
18173      the scope of the class, then the name lookup automatically
18174      finds the TYPE_DECL created by build_self_reference rather
18175      than a TEMPLATE_DECL.  For example, in:
18176
18177        template <class T> struct S {
18178          S s;
18179        };
18180
18181      there is no need to handle such case.  */
18182
18183   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18184     return DECL_TEMPLATE_RESULT (decl);
18185
18186   return decl;
18187 }
18188
18189 /* If too many, or too few, template-parameter lists apply to the
18190    declarator, issue an error message.  Returns TRUE if all went well,
18191    and FALSE otherwise.  */
18192
18193 static bool
18194 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18195                                                 cp_declarator *declarator,
18196                                                 location_t declarator_location)
18197 {
18198   unsigned num_templates;
18199
18200   /* We haven't seen any classes that involve template parameters yet.  */
18201   num_templates = 0;
18202
18203   switch (declarator->kind)
18204     {
18205     case cdk_id:
18206       if (declarator->u.id.qualifying_scope)
18207         {
18208           tree scope;
18209
18210           scope = declarator->u.id.qualifying_scope;
18211
18212           while (scope && CLASS_TYPE_P (scope))
18213             {
18214               /* You're supposed to have one `template <...>'
18215                  for every template class, but you don't need one
18216                  for a full specialization.  For example:
18217
18218                  template <class T> struct S{};
18219                  template <> struct S<int> { void f(); };
18220                  void S<int>::f () {}
18221
18222                  is correct; there shouldn't be a `template <>' for
18223                  the definition of `S<int>::f'.  */
18224               if (!CLASSTYPE_TEMPLATE_INFO (scope))
18225                 /* If SCOPE does not have template information of any
18226                    kind, then it is not a template, nor is it nested
18227                    within a template.  */
18228                 break;
18229               if (explicit_class_specialization_p (scope))
18230                 break;
18231               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18232                 ++num_templates;
18233
18234               scope = TYPE_CONTEXT (scope);
18235             }
18236         }
18237       else if (TREE_CODE (declarator->u.id.unqualified_name)
18238                == TEMPLATE_ID_EXPR)
18239         /* If the DECLARATOR has the form `X<y>' then it uses one
18240            additional level of template parameters.  */
18241         ++num_templates;
18242
18243       return cp_parser_check_template_parameters 
18244         (parser, num_templates, declarator_location, declarator);
18245
18246
18247     case cdk_function:
18248     case cdk_array:
18249     case cdk_pointer:
18250     case cdk_reference:
18251     case cdk_ptrmem:
18252       return (cp_parser_check_declarator_template_parameters
18253               (parser, declarator->declarator, declarator_location));
18254
18255     case cdk_error:
18256       return true;
18257
18258     default:
18259       gcc_unreachable ();
18260     }
18261   return false;
18262 }
18263
18264 /* NUM_TEMPLATES were used in the current declaration.  If that is
18265    invalid, return FALSE and issue an error messages.  Otherwise,
18266    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
18267    declarator and we can print more accurate diagnostics.  */
18268
18269 static bool
18270 cp_parser_check_template_parameters (cp_parser* parser,
18271                                      unsigned num_templates,
18272                                      location_t location,
18273                                      cp_declarator *declarator)
18274 {
18275   /* If there are the same number of template classes and parameter
18276      lists, that's OK.  */
18277   if (parser->num_template_parameter_lists == num_templates)
18278     return true;
18279   /* If there are more, but only one more, then we are referring to a
18280      member template.  That's OK too.  */
18281   if (parser->num_template_parameter_lists == num_templates + 1)
18282     return true;
18283   /* If there are more template classes than parameter lists, we have
18284      something like:
18285
18286        template <class T> void S<T>::R<T>::f ();  */
18287   if (parser->num_template_parameter_lists < num_templates)
18288     {
18289       if (declarator && !current_function_decl)
18290         error_at (location, "specializing member %<%T::%E%> "
18291                   "requires %<template<>%> syntax", 
18292                   declarator->u.id.qualifying_scope,
18293                   declarator->u.id.unqualified_name);
18294       else if (declarator)
18295         error_at (location, "invalid declaration of %<%T::%E%>",
18296                   declarator->u.id.qualifying_scope,
18297                   declarator->u.id.unqualified_name);
18298       else 
18299         error_at (location, "too few template-parameter-lists");
18300       return false;
18301     }
18302   /* Otherwise, there are too many template parameter lists.  We have
18303      something like:
18304
18305      template <class T> template <class U> void S::f();  */
18306   error_at (location, "too many template-parameter-lists");
18307   return false;
18308 }
18309
18310 /* Parse an optional `::' token indicating that the following name is
18311    from the global namespace.  If so, PARSER->SCOPE is set to the
18312    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18313    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18314    Returns the new value of PARSER->SCOPE, if the `::' token is
18315    present, and NULL_TREE otherwise.  */
18316
18317 static tree
18318 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18319 {
18320   cp_token *token;
18321
18322   /* Peek at the next token.  */
18323   token = cp_lexer_peek_token (parser->lexer);
18324   /* If we're looking at a `::' token then we're starting from the
18325      global namespace, not our current location.  */
18326   if (token->type == CPP_SCOPE)
18327     {
18328       /* Consume the `::' token.  */
18329       cp_lexer_consume_token (parser->lexer);
18330       /* Set the SCOPE so that we know where to start the lookup.  */
18331       parser->scope = global_namespace;
18332       parser->qualifying_scope = global_namespace;
18333       parser->object_scope = NULL_TREE;
18334
18335       return parser->scope;
18336     }
18337   else if (!current_scope_valid_p)
18338     {
18339       parser->scope = NULL_TREE;
18340       parser->qualifying_scope = NULL_TREE;
18341       parser->object_scope = NULL_TREE;
18342     }
18343
18344   return NULL_TREE;
18345 }
18346
18347 /* Returns TRUE if the upcoming token sequence is the start of a
18348    constructor declarator.  If FRIEND_P is true, the declarator is
18349    preceded by the `friend' specifier.  */
18350
18351 static bool
18352 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18353 {
18354   bool constructor_p;
18355   tree nested_name_specifier;
18356   cp_token *next_token;
18357
18358   /* The common case is that this is not a constructor declarator, so
18359      try to avoid doing lots of work if at all possible.  It's not
18360      valid declare a constructor at function scope.  */
18361   if (parser->in_function_body)
18362     return false;
18363   /* And only certain tokens can begin a constructor declarator.  */
18364   next_token = cp_lexer_peek_token (parser->lexer);
18365   if (next_token->type != CPP_NAME
18366       && next_token->type != CPP_SCOPE
18367       && next_token->type != CPP_NESTED_NAME_SPECIFIER
18368       && next_token->type != CPP_TEMPLATE_ID)
18369     return false;
18370
18371   /* Parse tentatively; we are going to roll back all of the tokens
18372      consumed here.  */
18373   cp_parser_parse_tentatively (parser);
18374   /* Assume that we are looking at a constructor declarator.  */
18375   constructor_p = true;
18376
18377   /* Look for the optional `::' operator.  */
18378   cp_parser_global_scope_opt (parser,
18379                               /*current_scope_valid_p=*/false);
18380   /* Look for the nested-name-specifier.  */
18381   nested_name_specifier
18382     = (cp_parser_nested_name_specifier_opt (parser,
18383                                             /*typename_keyword_p=*/false,
18384                                             /*check_dependency_p=*/false,
18385                                             /*type_p=*/false,
18386                                             /*is_declaration=*/false));
18387   /* Outside of a class-specifier, there must be a
18388      nested-name-specifier.  */
18389   if (!nested_name_specifier &&
18390       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18391        || friend_p))
18392     constructor_p = false;
18393   else if (nested_name_specifier == error_mark_node)
18394     constructor_p = false;
18395
18396   /* If we have a class scope, this is easy; DR 147 says that S::S always
18397      names the constructor, and no other qualified name could.  */
18398   if (constructor_p && nested_name_specifier
18399       && TYPE_P (nested_name_specifier))
18400     {
18401       tree id = cp_parser_unqualified_id (parser,
18402                                           /*template_keyword_p=*/false,
18403                                           /*check_dependency_p=*/false,
18404                                           /*declarator_p=*/true,
18405                                           /*optional_p=*/false);
18406       if (is_overloaded_fn (id))
18407         id = DECL_NAME (get_first_fn (id));
18408       if (!constructor_name_p (id, nested_name_specifier))
18409         constructor_p = false;
18410     }
18411   /* If we still think that this might be a constructor-declarator,
18412      look for a class-name.  */
18413   else if (constructor_p)
18414     {
18415       /* If we have:
18416
18417            template <typename T> struct S {
18418              S();
18419            };
18420
18421          we must recognize that the nested `S' names a class.  */
18422       tree type_decl;
18423       type_decl = cp_parser_class_name (parser,
18424                                         /*typename_keyword_p=*/false,
18425                                         /*template_keyword_p=*/false,
18426                                         none_type,
18427                                         /*check_dependency_p=*/false,
18428                                         /*class_head_p=*/false,
18429                                         /*is_declaration=*/false);
18430       /* If there was no class-name, then this is not a constructor.  */
18431       constructor_p = !cp_parser_error_occurred (parser);
18432
18433       /* If we're still considering a constructor, we have to see a `(',
18434          to begin the parameter-declaration-clause, followed by either a
18435          `)', an `...', or a decl-specifier.  We need to check for a
18436          type-specifier to avoid being fooled into thinking that:
18437
18438            S (f) (int);
18439
18440          is a constructor.  (It is actually a function named `f' that
18441          takes one parameter (of type `int') and returns a value of type
18442          `S'.  */
18443       if (constructor_p
18444           && !cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18445         constructor_p = false;
18446
18447       if (constructor_p
18448           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18449           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18450           /* A parameter declaration begins with a decl-specifier,
18451              which is either the "attribute" keyword, a storage class
18452              specifier, or (usually) a type-specifier.  */
18453           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18454         {
18455           tree type;
18456           tree pushed_scope = NULL_TREE;
18457           unsigned saved_num_template_parameter_lists;
18458
18459           /* Names appearing in the type-specifier should be looked up
18460              in the scope of the class.  */
18461           if (current_class_type)
18462             type = NULL_TREE;
18463           else
18464             {
18465               type = TREE_TYPE (type_decl);
18466               if (TREE_CODE (type) == TYPENAME_TYPE)
18467                 {
18468                   type = resolve_typename_type (type,
18469                                                 /*only_current_p=*/false);
18470                   if (TREE_CODE (type) == TYPENAME_TYPE)
18471                     {
18472                       cp_parser_abort_tentative_parse (parser);
18473                       return false;
18474                     }
18475                 }
18476               pushed_scope = push_scope (type);
18477             }
18478
18479           /* Inside the constructor parameter list, surrounding
18480              template-parameter-lists do not apply.  */
18481           saved_num_template_parameter_lists
18482             = parser->num_template_parameter_lists;
18483           parser->num_template_parameter_lists = 0;
18484
18485           /* Look for the type-specifier.  */
18486           cp_parser_type_specifier (parser,
18487                                     CP_PARSER_FLAGS_NONE,
18488                                     /*decl_specs=*/NULL,
18489                                     /*is_declarator=*/true,
18490                                     /*declares_class_or_enum=*/NULL,
18491                                     /*is_cv_qualifier=*/NULL);
18492
18493           parser->num_template_parameter_lists
18494             = saved_num_template_parameter_lists;
18495
18496           /* Leave the scope of the class.  */
18497           if (pushed_scope)
18498             pop_scope (pushed_scope);
18499
18500           constructor_p = !cp_parser_error_occurred (parser);
18501         }
18502     }
18503
18504   /* We did not really want to consume any tokens.  */
18505   cp_parser_abort_tentative_parse (parser);
18506
18507   return constructor_p;
18508 }
18509
18510 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18511    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
18512    they must be performed once we are in the scope of the function.
18513
18514    Returns the function defined.  */
18515
18516 static tree
18517 cp_parser_function_definition_from_specifiers_and_declarator
18518   (cp_parser* parser,
18519    cp_decl_specifier_seq *decl_specifiers,
18520    tree attributes,
18521    const cp_declarator *declarator)
18522 {
18523   tree fn;
18524   bool success_p;
18525
18526   /* Begin the function-definition.  */
18527   success_p = start_function (decl_specifiers, declarator, attributes);
18528
18529   /* The things we're about to see are not directly qualified by any
18530      template headers we've seen thus far.  */
18531   reset_specialization ();
18532
18533   /* If there were names looked up in the decl-specifier-seq that we
18534      did not check, check them now.  We must wait until we are in the
18535      scope of the function to perform the checks, since the function
18536      might be a friend.  */
18537   perform_deferred_access_checks ();
18538
18539   if (!success_p)
18540     {
18541       /* Skip the entire function.  */
18542       cp_parser_skip_to_end_of_block_or_statement (parser);
18543       fn = error_mark_node;
18544     }
18545   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18546     {
18547       /* Seen already, skip it.  An error message has already been output.  */
18548       cp_parser_skip_to_end_of_block_or_statement (parser);
18549       fn = current_function_decl;
18550       current_function_decl = NULL_TREE;
18551       /* If this is a function from a class, pop the nested class.  */
18552       if (current_class_name)
18553         pop_nested_class ();
18554     }
18555   else
18556     fn = cp_parser_function_definition_after_declarator (parser,
18557                                                          /*inline_p=*/false);
18558
18559   return fn;
18560 }
18561
18562 /* Parse the part of a function-definition that follows the
18563    declarator.  INLINE_P is TRUE iff this function is an inline
18564    function defined within a class-specifier.
18565
18566    Returns the function defined.  */
18567
18568 static tree
18569 cp_parser_function_definition_after_declarator (cp_parser* parser,
18570                                                 bool inline_p)
18571 {
18572   tree fn;
18573   bool ctor_initializer_p = false;
18574   bool saved_in_unbraced_linkage_specification_p;
18575   bool saved_in_function_body;
18576   unsigned saved_num_template_parameter_lists;
18577   cp_token *token;
18578
18579   saved_in_function_body = parser->in_function_body;
18580   parser->in_function_body = true;
18581   /* If the next token is `return', then the code may be trying to
18582      make use of the "named return value" extension that G++ used to
18583      support.  */
18584   token = cp_lexer_peek_token (parser->lexer);
18585   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18586     {
18587       /* Consume the `return' keyword.  */
18588       cp_lexer_consume_token (parser->lexer);
18589       /* Look for the identifier that indicates what value is to be
18590          returned.  */
18591       cp_parser_identifier (parser);
18592       /* Issue an error message.  */
18593       error_at (token->location,
18594                 "named return values are no longer supported");
18595       /* Skip tokens until we reach the start of the function body.  */
18596       while (true)
18597         {
18598           cp_token *token = cp_lexer_peek_token (parser->lexer);
18599           if (token->type == CPP_OPEN_BRACE
18600               || token->type == CPP_EOF
18601               || token->type == CPP_PRAGMA_EOL)
18602             break;
18603           cp_lexer_consume_token (parser->lexer);
18604         }
18605     }
18606   /* The `extern' in `extern "C" void f () { ... }' does not apply to
18607      anything declared inside `f'.  */
18608   saved_in_unbraced_linkage_specification_p
18609     = parser->in_unbraced_linkage_specification_p;
18610   parser->in_unbraced_linkage_specification_p = false;
18611   /* Inside the function, surrounding template-parameter-lists do not
18612      apply.  */
18613   saved_num_template_parameter_lists
18614     = parser->num_template_parameter_lists;
18615   parser->num_template_parameter_lists = 0;
18616
18617   start_lambda_scope (current_function_decl);
18618
18619   /* If the next token is `try', then we are looking at a
18620      function-try-block.  */
18621   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18622     ctor_initializer_p = cp_parser_function_try_block (parser);
18623   /* A function-try-block includes the function-body, so we only do
18624      this next part if we're not processing a function-try-block.  */
18625   else
18626     ctor_initializer_p
18627       = cp_parser_ctor_initializer_opt_and_function_body (parser);
18628
18629   finish_lambda_scope ();
18630
18631   /* Finish the function.  */
18632   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18633                         (inline_p ? 2 : 0));
18634   /* Generate code for it, if necessary.  */
18635   expand_or_defer_fn (fn);
18636   /* Restore the saved values.  */
18637   parser->in_unbraced_linkage_specification_p
18638     = saved_in_unbraced_linkage_specification_p;
18639   parser->num_template_parameter_lists
18640     = saved_num_template_parameter_lists;
18641   parser->in_function_body = saved_in_function_body;
18642
18643   return fn;
18644 }
18645
18646 /* Parse a template-declaration, assuming that the `export' (and
18647    `extern') keywords, if present, has already been scanned.  MEMBER_P
18648    is as for cp_parser_template_declaration.  */
18649
18650 static void
18651 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18652 {
18653   tree decl = NULL_TREE;
18654   VEC (deferred_access_check,gc) *checks;
18655   tree parameter_list;
18656   bool friend_p = false;
18657   bool need_lang_pop;
18658   cp_token *token;
18659
18660   /* Look for the `template' keyword.  */
18661   token = cp_lexer_peek_token (parser->lexer);
18662   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18663     return;
18664
18665   /* And the `<'.  */
18666   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18667     return;
18668   if (at_class_scope_p () && current_function_decl)
18669     {
18670       /* 14.5.2.2 [temp.mem]
18671
18672          A local class shall not have member templates.  */
18673       error_at (token->location,
18674                 "invalid declaration of member template in local class");
18675       cp_parser_skip_to_end_of_block_or_statement (parser);
18676       return;
18677     }
18678   /* [temp]
18679
18680      A template ... shall not have C linkage.  */
18681   if (current_lang_name == lang_name_c)
18682     {
18683       error_at (token->location, "template with C linkage");
18684       /* Give it C++ linkage to avoid confusing other parts of the
18685          front end.  */
18686       push_lang_context (lang_name_cplusplus);
18687       need_lang_pop = true;
18688     }
18689   else
18690     need_lang_pop = false;
18691
18692   /* We cannot perform access checks on the template parameter
18693      declarations until we know what is being declared, just as we
18694      cannot check the decl-specifier list.  */
18695   push_deferring_access_checks (dk_deferred);
18696
18697   /* If the next token is `>', then we have an invalid
18698      specialization.  Rather than complain about an invalid template
18699      parameter, issue an error message here.  */
18700   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18701     {
18702       cp_parser_error (parser, "invalid explicit specialization");
18703       begin_specialization ();
18704       parameter_list = NULL_TREE;
18705     }
18706   else
18707     /* Parse the template parameters.  */
18708     parameter_list = cp_parser_template_parameter_list (parser);
18709
18710   /* Get the deferred access checks from the parameter list.  These
18711      will be checked once we know what is being declared, as for a
18712      member template the checks must be performed in the scope of the
18713      class containing the member.  */
18714   checks = get_deferred_access_checks ();
18715
18716   /* Look for the `>'.  */
18717   cp_parser_skip_to_end_of_template_parameter_list (parser);
18718   /* We just processed one more parameter list.  */
18719   ++parser->num_template_parameter_lists;
18720   /* If the next token is `template', there are more template
18721      parameters.  */
18722   if (cp_lexer_next_token_is_keyword (parser->lexer,
18723                                       RID_TEMPLATE))
18724     cp_parser_template_declaration_after_export (parser, member_p);
18725   else
18726     {
18727       /* There are no access checks when parsing a template, as we do not
18728          know if a specialization will be a friend.  */
18729       push_deferring_access_checks (dk_no_check);
18730       token = cp_lexer_peek_token (parser->lexer);
18731       decl = cp_parser_single_declaration (parser,
18732                                            checks,
18733                                            member_p,
18734                                            /*explicit_specialization_p=*/false,
18735                                            &friend_p);
18736       pop_deferring_access_checks ();
18737
18738       /* If this is a member template declaration, let the front
18739          end know.  */
18740       if (member_p && !friend_p && decl)
18741         {
18742           if (TREE_CODE (decl) == TYPE_DECL)
18743             cp_parser_check_access_in_redeclaration (decl, token->location);
18744
18745           decl = finish_member_template_decl (decl);
18746         }
18747       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18748         make_friend_class (current_class_type, TREE_TYPE (decl),
18749                            /*complain=*/true);
18750     }
18751   /* We are done with the current parameter list.  */
18752   --parser->num_template_parameter_lists;
18753
18754   pop_deferring_access_checks ();
18755
18756   /* Finish up.  */
18757   finish_template_decl (parameter_list);
18758
18759   /* Register member declarations.  */
18760   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18761     finish_member_declaration (decl);
18762   /* For the erroneous case of a template with C linkage, we pushed an
18763      implicit C++ linkage scope; exit that scope now.  */
18764   if (need_lang_pop)
18765     pop_lang_context ();
18766   /* If DECL is a function template, we must return to parse it later.
18767      (Even though there is no definition, there might be default
18768      arguments that need handling.)  */
18769   if (member_p && decl
18770       && (TREE_CODE (decl) == FUNCTION_DECL
18771           || DECL_FUNCTION_TEMPLATE_P (decl)))
18772     TREE_VALUE (parser->unparsed_functions_queues)
18773       = tree_cons (NULL_TREE, decl,
18774                    TREE_VALUE (parser->unparsed_functions_queues));
18775 }
18776
18777 /* Perform the deferred access checks from a template-parameter-list.
18778    CHECKS is a TREE_LIST of access checks, as returned by
18779    get_deferred_access_checks.  */
18780
18781 static void
18782 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18783 {
18784   ++processing_template_parmlist;
18785   perform_access_checks (checks);
18786   --processing_template_parmlist;
18787 }
18788
18789 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18790    `function-definition' sequence.  MEMBER_P is true, this declaration
18791    appears in a class scope.
18792
18793    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
18794    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
18795
18796 static tree
18797 cp_parser_single_declaration (cp_parser* parser,
18798                               VEC (deferred_access_check,gc)* checks,
18799                               bool member_p,
18800                               bool explicit_specialization_p,
18801                               bool* friend_p)
18802 {
18803   int declares_class_or_enum;
18804   tree decl = NULL_TREE;
18805   cp_decl_specifier_seq decl_specifiers;
18806   bool function_definition_p = false;
18807   cp_token *decl_spec_token_start;
18808
18809   /* This function is only used when processing a template
18810      declaration.  */
18811   gcc_assert (innermost_scope_kind () == sk_template_parms
18812               || innermost_scope_kind () == sk_template_spec);
18813
18814   /* Defer access checks until we know what is being declared.  */
18815   push_deferring_access_checks (dk_deferred);
18816
18817   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18818      alternative.  */
18819   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18820   cp_parser_decl_specifier_seq (parser,
18821                                 CP_PARSER_FLAGS_OPTIONAL,
18822                                 &decl_specifiers,
18823                                 &declares_class_or_enum);
18824   if (friend_p)
18825     *friend_p = cp_parser_friend_p (&decl_specifiers);
18826
18827   /* There are no template typedefs.  */
18828   if (decl_specifiers.specs[(int) ds_typedef])
18829     {
18830       error_at (decl_spec_token_start->location,
18831                 "template declaration of %<typedef%>");
18832       decl = error_mark_node;
18833     }
18834
18835   /* Gather up the access checks that occurred the
18836      decl-specifier-seq.  */
18837   stop_deferring_access_checks ();
18838
18839   /* Check for the declaration of a template class.  */
18840   if (declares_class_or_enum)
18841     {
18842       if (cp_parser_declares_only_class_p (parser))
18843         {
18844           decl = shadow_tag (&decl_specifiers);
18845
18846           /* In this case:
18847
18848                struct C {
18849                  friend template <typename T> struct A<T>::B;
18850                };
18851
18852              A<T>::B will be represented by a TYPENAME_TYPE, and
18853              therefore not recognized by shadow_tag.  */
18854           if (friend_p && *friend_p
18855               && !decl
18856               && decl_specifiers.type
18857               && TYPE_P (decl_specifiers.type))
18858             decl = decl_specifiers.type;
18859
18860           if (decl && decl != error_mark_node)
18861             decl = TYPE_NAME (decl);
18862           else
18863             decl = error_mark_node;
18864
18865           /* Perform access checks for template parameters.  */
18866           cp_parser_perform_template_parameter_access_checks (checks);
18867         }
18868     }
18869
18870   /* Complain about missing 'typename' or other invalid type names.  */
18871   if (!decl_specifiers.any_type_specifiers_p)
18872     cp_parser_parse_and_diagnose_invalid_type_name (parser);
18873
18874   /* If it's not a template class, try for a template function.  If
18875      the next token is a `;', then this declaration does not declare
18876      anything.  But, if there were errors in the decl-specifiers, then
18877      the error might well have come from an attempted class-specifier.
18878      In that case, there's no need to warn about a missing declarator.  */
18879   if (!decl
18880       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18881           || decl_specifiers.type != error_mark_node))
18882     {
18883       decl = cp_parser_init_declarator (parser,
18884                                         &decl_specifiers,
18885                                         checks,
18886                                         /*function_definition_allowed_p=*/true,
18887                                         member_p,
18888                                         declares_class_or_enum,
18889                                         &function_definition_p);
18890
18891     /* 7.1.1-1 [dcl.stc]
18892
18893        A storage-class-specifier shall not be specified in an explicit
18894        specialization...  */
18895     if (decl
18896         && explicit_specialization_p
18897         && decl_specifiers.storage_class != sc_none)
18898       {
18899         error_at (decl_spec_token_start->location,
18900                   "explicit template specialization cannot have a storage class");
18901         decl = error_mark_node;
18902       }
18903     }
18904
18905   pop_deferring_access_checks ();
18906
18907   /* Clear any current qualification; whatever comes next is the start
18908      of something new.  */
18909   parser->scope = NULL_TREE;
18910   parser->qualifying_scope = NULL_TREE;
18911   parser->object_scope = NULL_TREE;
18912   /* Look for a trailing `;' after the declaration.  */
18913   if (!function_definition_p
18914       && (decl == error_mark_node
18915           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18916     cp_parser_skip_to_end_of_block_or_statement (parser);
18917
18918   return decl;
18919 }
18920
18921 /* Parse a cast-expression that is not the operand of a unary "&".  */
18922
18923 static tree
18924 cp_parser_simple_cast_expression (cp_parser *parser)
18925 {
18926   return cp_parser_cast_expression (parser, /*address_p=*/false,
18927                                     /*cast_p=*/false, NULL);
18928 }
18929
18930 /* Parse a functional cast to TYPE.  Returns an expression
18931    representing the cast.  */
18932
18933 static tree
18934 cp_parser_functional_cast (cp_parser* parser, tree type)
18935 {
18936   VEC(tree,gc) *vec;
18937   tree expression_list;
18938   tree cast;
18939   bool nonconst_p;
18940
18941   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18942     {
18943       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18944       expression_list = cp_parser_braced_list (parser, &nonconst_p);
18945       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18946       if (TREE_CODE (type) == TYPE_DECL)
18947         type = TREE_TYPE (type);
18948       return finish_compound_literal (type, expression_list);
18949     }
18950
18951
18952   vec = cp_parser_parenthesized_expression_list (parser, false,
18953                                                  /*cast_p=*/true,
18954                                                  /*allow_expansion_p=*/true,
18955                                                  /*non_constant_p=*/NULL);
18956   if (vec == NULL)
18957     expression_list = error_mark_node;
18958   else
18959     {
18960       expression_list = build_tree_list_vec (vec);
18961       release_tree_vector (vec);
18962     }
18963
18964   cast = build_functional_cast (type, expression_list,
18965                                 tf_warning_or_error);
18966   /* [expr.const]/1: In an integral constant expression "only type
18967      conversions to integral or enumeration type can be used".  */
18968   if (TREE_CODE (type) == TYPE_DECL)
18969     type = TREE_TYPE (type);
18970   if (cast != error_mark_node
18971       && !cast_valid_in_integral_constant_expression_p (type)
18972       && (cp_parser_non_integral_constant_expression
18973           (parser, "a call to a constructor")))
18974     return error_mark_node;
18975   return cast;
18976 }
18977
18978 /* Save the tokens that make up the body of a member function defined
18979    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18980    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18981    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18982    for the member function.  */
18983
18984 static tree
18985 cp_parser_save_member_function_body (cp_parser* parser,
18986                                      cp_decl_specifier_seq *decl_specifiers,
18987                                      cp_declarator *declarator,
18988                                      tree attributes)
18989 {
18990   cp_token *first;
18991   cp_token *last;
18992   tree fn;
18993
18994   /* Create the FUNCTION_DECL.  */
18995   fn = grokmethod (decl_specifiers, declarator, attributes);
18996   /* If something went badly wrong, bail out now.  */
18997   if (fn == error_mark_node)
18998     {
18999       /* If there's a function-body, skip it.  */
19000       if (cp_parser_token_starts_function_definition_p
19001           (cp_lexer_peek_token (parser->lexer)))
19002         cp_parser_skip_to_end_of_block_or_statement (parser);
19003       return error_mark_node;
19004     }
19005
19006   /* Remember it, if there default args to post process.  */
19007   cp_parser_save_default_args (parser, fn);
19008
19009   /* Save away the tokens that make up the body of the
19010      function.  */
19011   first = parser->lexer->next_token;
19012   /* We can have braced-init-list mem-initializers before the fn body.  */
19013   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19014     {
19015       cp_lexer_consume_token (parser->lexer);
19016       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19017              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19018         {
19019           /* cache_group will stop after an un-nested { } pair, too.  */
19020           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19021             break;
19022
19023           /* variadic mem-inits have ... after the ')'.  */
19024           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19025             cp_lexer_consume_token (parser->lexer);
19026         }
19027     }
19028   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19029   /* Handle function try blocks.  */
19030   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19031     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19032   last = parser->lexer->next_token;
19033
19034   /* Save away the inline definition; we will process it when the
19035      class is complete.  */
19036   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19037   DECL_PENDING_INLINE_P (fn) = 1;
19038
19039   /* We need to know that this was defined in the class, so that
19040      friend templates are handled correctly.  */
19041   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19042
19043   /* Add FN to the queue of functions to be parsed later.  */
19044   TREE_VALUE (parser->unparsed_functions_queues)
19045     = tree_cons (NULL_TREE, fn,
19046                  TREE_VALUE (parser->unparsed_functions_queues));
19047
19048   return fn;
19049 }
19050
19051 /* Parse a template-argument-list, as well as the trailing ">" (but
19052    not the opening ">").  See cp_parser_template_argument_list for the
19053    return value.  */
19054
19055 static tree
19056 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19057 {
19058   tree arguments;
19059   tree saved_scope;
19060   tree saved_qualifying_scope;
19061   tree saved_object_scope;
19062   bool saved_greater_than_is_operator_p;
19063   int saved_unevaluated_operand;
19064   int saved_inhibit_evaluation_warnings;
19065
19066   /* [temp.names]
19067
19068      When parsing a template-id, the first non-nested `>' is taken as
19069      the end of the template-argument-list rather than a greater-than
19070      operator.  */
19071   saved_greater_than_is_operator_p
19072     = parser->greater_than_is_operator_p;
19073   parser->greater_than_is_operator_p = false;
19074   /* Parsing the argument list may modify SCOPE, so we save it
19075      here.  */
19076   saved_scope = parser->scope;
19077   saved_qualifying_scope = parser->qualifying_scope;
19078   saved_object_scope = parser->object_scope;
19079   /* We need to evaluate the template arguments, even though this
19080      template-id may be nested within a "sizeof".  */
19081   saved_unevaluated_operand = cp_unevaluated_operand;
19082   cp_unevaluated_operand = 0;
19083   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19084   c_inhibit_evaluation_warnings = 0;
19085   /* Parse the template-argument-list itself.  */
19086   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19087       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19088     arguments = NULL_TREE;
19089   else
19090     arguments = cp_parser_template_argument_list (parser);
19091   /* Look for the `>' that ends the template-argument-list. If we find
19092      a '>>' instead, it's probably just a typo.  */
19093   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19094     {
19095       if (cxx_dialect != cxx98)
19096         {
19097           /* In C++0x, a `>>' in a template argument list or cast
19098              expression is considered to be two separate `>'
19099              tokens. So, change the current token to a `>', but don't
19100              consume it: it will be consumed later when the outer
19101              template argument list (or cast expression) is parsed.
19102              Note that this replacement of `>' for `>>' is necessary
19103              even if we are parsing tentatively: in the tentative
19104              case, after calling
19105              cp_parser_enclosed_template_argument_list we will always
19106              throw away all of the template arguments and the first
19107              closing `>', either because the template argument list
19108              was erroneous or because we are replacing those tokens
19109              with a CPP_TEMPLATE_ID token.  The second `>' (which will
19110              not have been thrown away) is needed either to close an
19111              outer template argument list or to complete a new-style
19112              cast.  */
19113           cp_token *token = cp_lexer_peek_token (parser->lexer);
19114           token->type = CPP_GREATER;
19115         }
19116       else if (!saved_greater_than_is_operator_p)
19117         {
19118           /* If we're in a nested template argument list, the '>>' has
19119             to be a typo for '> >'. We emit the error message, but we
19120             continue parsing and we push a '>' as next token, so that
19121             the argument list will be parsed correctly.  Note that the
19122             global source location is still on the token before the
19123             '>>', so we need to say explicitly where we want it.  */
19124           cp_token *token = cp_lexer_peek_token (parser->lexer);
19125           error_at (token->location, "%<>>%> should be %<> >%> "
19126                     "within a nested template argument list");
19127
19128           token->type = CPP_GREATER;
19129         }
19130       else
19131         {
19132           /* If this is not a nested template argument list, the '>>'
19133             is a typo for '>'. Emit an error message and continue.
19134             Same deal about the token location, but here we can get it
19135             right by consuming the '>>' before issuing the diagnostic.  */
19136           cp_token *token = cp_lexer_consume_token (parser->lexer);
19137           error_at (token->location,
19138                     "spurious %<>>%>, use %<>%> to terminate "
19139                     "a template argument list");
19140         }
19141     }
19142   else
19143     cp_parser_skip_to_end_of_template_parameter_list (parser);
19144   /* The `>' token might be a greater-than operator again now.  */
19145   parser->greater_than_is_operator_p
19146     = saved_greater_than_is_operator_p;
19147   /* Restore the SAVED_SCOPE.  */
19148   parser->scope = saved_scope;
19149   parser->qualifying_scope = saved_qualifying_scope;
19150   parser->object_scope = saved_object_scope;
19151   cp_unevaluated_operand = saved_unevaluated_operand;
19152   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19153
19154   return arguments;
19155 }
19156
19157 /* MEMBER_FUNCTION is a member function, or a friend.  If default
19158    arguments, or the body of the function have not yet been parsed,
19159    parse them now.  */
19160
19161 static void
19162 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19163 {
19164   /* If this member is a template, get the underlying
19165      FUNCTION_DECL.  */
19166   if (DECL_FUNCTION_TEMPLATE_P (member_function))
19167     member_function = DECL_TEMPLATE_RESULT (member_function);
19168
19169   /* There should not be any class definitions in progress at this
19170      point; the bodies of members are only parsed outside of all class
19171      definitions.  */
19172   gcc_assert (parser->num_classes_being_defined == 0);
19173   /* While we're parsing the member functions we might encounter more
19174      classes.  We want to handle them right away, but we don't want
19175      them getting mixed up with functions that are currently in the
19176      queue.  */
19177   parser->unparsed_functions_queues
19178     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19179
19180   /* Make sure that any template parameters are in scope.  */
19181   maybe_begin_member_template_processing (member_function);
19182
19183   /* If the body of the function has not yet been parsed, parse it
19184      now.  */
19185   if (DECL_PENDING_INLINE_P (member_function))
19186     {
19187       tree function_scope;
19188       cp_token_cache *tokens;
19189
19190       /* The function is no longer pending; we are processing it.  */
19191       tokens = DECL_PENDING_INLINE_INFO (member_function);
19192       DECL_PENDING_INLINE_INFO (member_function) = NULL;
19193       DECL_PENDING_INLINE_P (member_function) = 0;
19194
19195       /* If this is a local class, enter the scope of the containing
19196          function.  */
19197       function_scope = current_function_decl;
19198       if (function_scope)
19199         push_function_context ();
19200
19201       /* Push the body of the function onto the lexer stack.  */
19202       cp_parser_push_lexer_for_tokens (parser, tokens);
19203
19204       /* Let the front end know that we going to be defining this
19205          function.  */
19206       start_preparsed_function (member_function, NULL_TREE,
19207                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
19208
19209       /* Don't do access checking if it is a templated function.  */
19210       if (processing_template_decl)
19211         push_deferring_access_checks (dk_no_check);
19212
19213       /* Now, parse the body of the function.  */
19214       cp_parser_function_definition_after_declarator (parser,
19215                                                       /*inline_p=*/true);
19216
19217       if (processing_template_decl)
19218         pop_deferring_access_checks ();
19219
19220       /* Leave the scope of the containing function.  */
19221       if (function_scope)
19222         pop_function_context ();
19223       cp_parser_pop_lexer (parser);
19224     }
19225
19226   /* Remove any template parameters from the symbol table.  */
19227   maybe_end_member_template_processing ();
19228
19229   /* Restore the queue.  */
19230   parser->unparsed_functions_queues
19231     = TREE_CHAIN (parser->unparsed_functions_queues);
19232 }
19233
19234 /* If DECL contains any default args, remember it on the unparsed
19235    functions queue.  */
19236
19237 static void
19238 cp_parser_save_default_args (cp_parser* parser, tree decl)
19239 {
19240   tree probe;
19241
19242   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19243        probe;
19244        probe = TREE_CHAIN (probe))
19245     if (TREE_PURPOSE (probe))
19246       {
19247         TREE_PURPOSE (parser->unparsed_functions_queues)
19248           = tree_cons (current_class_type, decl,
19249                        TREE_PURPOSE (parser->unparsed_functions_queues));
19250         break;
19251       }
19252 }
19253
19254 /* FN is a FUNCTION_DECL which may contains a parameter with an
19255    unparsed DEFAULT_ARG.  Parse the default args now.  This function
19256    assumes that the current scope is the scope in which the default
19257    argument should be processed.  */
19258
19259 static void
19260 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19261 {
19262   bool saved_local_variables_forbidden_p;
19263   tree parm, parmdecl;
19264
19265   /* While we're parsing the default args, we might (due to the
19266      statement expression extension) encounter more classes.  We want
19267      to handle them right away, but we don't want them getting mixed
19268      up with default args that are currently in the queue.  */
19269   parser->unparsed_functions_queues
19270     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19271
19272   /* Local variable names (and the `this' keyword) may not appear
19273      in a default argument.  */
19274   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19275   parser->local_variables_forbidden_p = true;
19276
19277   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19278          parmdecl = DECL_ARGUMENTS (fn);
19279        parm && parm != void_list_node;
19280        parm = TREE_CHAIN (parm),
19281          parmdecl = TREE_CHAIN (parmdecl))
19282     {
19283       cp_token_cache *tokens;
19284       tree default_arg = TREE_PURPOSE (parm);
19285       tree parsed_arg;
19286       VEC(tree,gc) *insts;
19287       tree copy;
19288       unsigned ix;
19289
19290       if (!default_arg)
19291         continue;
19292
19293       if (TREE_CODE (default_arg) != DEFAULT_ARG)
19294         /* This can happen for a friend declaration for a function
19295            already declared with default arguments.  */
19296         continue;
19297
19298        /* Push the saved tokens for the default argument onto the parser's
19299           lexer stack.  */
19300       tokens = DEFARG_TOKENS (default_arg);
19301       cp_parser_push_lexer_for_tokens (parser, tokens);
19302
19303       start_lambda_scope (parmdecl);
19304
19305       /* Parse the assignment-expression.  */
19306       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19307       if (parsed_arg == error_mark_node)
19308         {
19309           cp_parser_pop_lexer (parser);
19310           continue;
19311         }
19312
19313       if (!processing_template_decl)
19314         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19315
19316       TREE_PURPOSE (parm) = parsed_arg;
19317
19318       /* Update any instantiations we've already created.  */
19319       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19320            VEC_iterate (tree, insts, ix, copy); ix++)
19321         TREE_PURPOSE (copy) = parsed_arg;
19322
19323       finish_lambda_scope ();
19324
19325       /* If the token stream has not been completely used up, then
19326          there was extra junk after the end of the default
19327          argument.  */
19328       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19329         cp_parser_error (parser, "expected %<,%>");
19330
19331       /* Revert to the main lexer.  */
19332       cp_parser_pop_lexer (parser);
19333     }
19334
19335   /* Make sure no default arg is missing.  */
19336   check_default_args (fn);
19337
19338   /* Restore the state of local_variables_forbidden_p.  */
19339   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19340
19341   /* Restore the queue.  */
19342   parser->unparsed_functions_queues
19343     = TREE_CHAIN (parser->unparsed_functions_queues);
19344 }
19345
19346 /* Parse the operand of `sizeof' (or a similar operator).  Returns
19347    either a TYPE or an expression, depending on the form of the
19348    input.  The KEYWORD indicates which kind of expression we have
19349    encountered.  */
19350
19351 static tree
19352 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19353 {
19354   tree expr = NULL_TREE;
19355   const char *saved_message;
19356   char *tmp;
19357   bool saved_integral_constant_expression_p;
19358   bool saved_non_integral_constant_expression_p;
19359   bool pack_expansion_p = false;
19360
19361   /* Types cannot be defined in a `sizeof' expression.  Save away the
19362      old message.  */
19363   saved_message = parser->type_definition_forbidden_message;
19364   /* And create the new one.  */
19365   tmp = concat ("types may not be defined in %<",
19366                 IDENTIFIER_POINTER (ridpointers[keyword]),
19367                 "%> expressions", NULL);
19368   parser->type_definition_forbidden_message = tmp;
19369
19370   /* The restrictions on constant-expressions do not apply inside
19371      sizeof expressions.  */
19372   saved_integral_constant_expression_p
19373     = parser->integral_constant_expression_p;
19374   saved_non_integral_constant_expression_p
19375     = parser->non_integral_constant_expression_p;
19376   parser->integral_constant_expression_p = false;
19377
19378   /* If it's a `...', then we are computing the length of a parameter
19379      pack.  */
19380   if (keyword == RID_SIZEOF
19381       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19382     {
19383       /* Consume the `...'.  */
19384       cp_lexer_consume_token (parser->lexer);
19385       maybe_warn_variadic_templates ();
19386
19387       /* Note that this is an expansion.  */
19388       pack_expansion_p = true;
19389     }
19390
19391   /* Do not actually evaluate the expression.  */
19392   ++cp_unevaluated_operand;
19393   ++c_inhibit_evaluation_warnings;
19394   /* If it's a `(', then we might be looking at the type-id
19395      construction.  */
19396   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19397     {
19398       tree type;
19399       bool saved_in_type_id_in_expr_p;
19400
19401       /* We can't be sure yet whether we're looking at a type-id or an
19402          expression.  */
19403       cp_parser_parse_tentatively (parser);
19404       /* Consume the `('.  */
19405       cp_lexer_consume_token (parser->lexer);
19406       /* Parse the type-id.  */
19407       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19408       parser->in_type_id_in_expr_p = true;
19409       type = cp_parser_type_id (parser);
19410       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19411       /* Now, look for the trailing `)'.  */
19412       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19413       /* If all went well, then we're done.  */
19414       if (cp_parser_parse_definitely (parser))
19415         {
19416           cp_decl_specifier_seq decl_specs;
19417
19418           /* Build a trivial decl-specifier-seq.  */
19419           clear_decl_specs (&decl_specs);
19420           decl_specs.type = type;
19421
19422           /* Call grokdeclarator to figure out what type this is.  */
19423           expr = grokdeclarator (NULL,
19424                                  &decl_specs,
19425                                  TYPENAME,
19426                                  /*initialized=*/0,
19427                                  /*attrlist=*/NULL);
19428         }
19429     }
19430
19431   /* If the type-id production did not work out, then we must be
19432      looking at the unary-expression production.  */
19433   if (!expr)
19434     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19435                                        /*cast_p=*/false, NULL);
19436
19437   if (pack_expansion_p)
19438     /* Build a pack expansion. */
19439     expr = make_pack_expansion (expr);
19440
19441   /* Go back to evaluating expressions.  */
19442   --cp_unevaluated_operand;
19443   --c_inhibit_evaluation_warnings;
19444
19445   /* Free the message we created.  */
19446   free (tmp);
19447   /* And restore the old one.  */
19448   parser->type_definition_forbidden_message = saved_message;
19449   parser->integral_constant_expression_p
19450     = saved_integral_constant_expression_p;
19451   parser->non_integral_constant_expression_p
19452     = saved_non_integral_constant_expression_p;
19453
19454   return expr;
19455 }
19456
19457 /* If the current declaration has no declarator, return true.  */
19458
19459 static bool
19460 cp_parser_declares_only_class_p (cp_parser *parser)
19461 {
19462   /* If the next token is a `;' or a `,' then there is no
19463      declarator.  */
19464   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19465           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19466 }
19467
19468 /* Update the DECL_SPECS to reflect the storage class indicated by
19469    KEYWORD.  */
19470
19471 static void
19472 cp_parser_set_storage_class (cp_parser *parser,
19473                              cp_decl_specifier_seq *decl_specs,
19474                              enum rid keyword,
19475                              location_t location)
19476 {
19477   cp_storage_class storage_class;
19478
19479   if (parser->in_unbraced_linkage_specification_p)
19480     {
19481       error_at (location, "invalid use of %qD in linkage specification",
19482                 ridpointers[keyword]);
19483       return;
19484     }
19485   else if (decl_specs->storage_class != sc_none)
19486     {
19487       decl_specs->conflicting_specifiers_p = true;
19488       return;
19489     }
19490
19491   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19492       && decl_specs->specs[(int) ds_thread])
19493     {
19494       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19495       decl_specs->specs[(int) ds_thread] = 0;
19496     }
19497
19498   switch (keyword)
19499     {
19500     case RID_AUTO:
19501       storage_class = sc_auto;
19502       break;
19503     case RID_REGISTER:
19504       storage_class = sc_register;
19505       break;
19506     case RID_STATIC:
19507       storage_class = sc_static;
19508       break;
19509     case RID_EXTERN:
19510       storage_class = sc_extern;
19511       break;
19512     case RID_MUTABLE:
19513       storage_class = sc_mutable;
19514       break;
19515     default:
19516       gcc_unreachable ();
19517     }
19518   decl_specs->storage_class = storage_class;
19519
19520   /* A storage class specifier cannot be applied alongside a typedef 
19521      specifier. If there is a typedef specifier present then set 
19522      conflicting_specifiers_p which will trigger an error later
19523      on in grokdeclarator. */
19524   if (decl_specs->specs[(int)ds_typedef])
19525     decl_specs->conflicting_specifiers_p = true;
19526 }
19527
19528 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
19529    is true, the type is a user-defined type; otherwise it is a
19530    built-in type specified by a keyword.  */
19531
19532 static void
19533 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19534                               tree type_spec,
19535                               location_t location,
19536                               bool user_defined_p)
19537 {
19538   decl_specs->any_specifiers_p = true;
19539
19540   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19541      (with, for example, in "typedef int wchar_t;") we remember that
19542      this is what happened.  In system headers, we ignore these
19543      declarations so that G++ can work with system headers that are not
19544      C++-safe.  */
19545   if (decl_specs->specs[(int) ds_typedef]
19546       && !user_defined_p
19547       && (type_spec == boolean_type_node
19548           || type_spec == char16_type_node
19549           || type_spec == char32_type_node
19550           || type_spec == wchar_type_node)
19551       && (decl_specs->type
19552           || decl_specs->specs[(int) ds_long]
19553           || decl_specs->specs[(int) ds_short]
19554           || decl_specs->specs[(int) ds_unsigned]
19555           || decl_specs->specs[(int) ds_signed]))
19556     {
19557       decl_specs->redefined_builtin_type = type_spec;
19558       if (!decl_specs->type)
19559         {
19560           decl_specs->type = type_spec;
19561           decl_specs->user_defined_type_p = false;
19562           decl_specs->type_location = location;
19563         }
19564     }
19565   else if (decl_specs->type)
19566     decl_specs->multiple_types_p = true;
19567   else
19568     {
19569       decl_specs->type = type_spec;
19570       decl_specs->user_defined_type_p = user_defined_p;
19571       decl_specs->redefined_builtin_type = NULL_TREE;
19572       decl_specs->type_location = location;
19573     }
19574 }
19575
19576 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19577    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
19578
19579 static bool
19580 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19581 {
19582   return decl_specifiers->specs[(int) ds_friend] != 0;
19583 }
19584
19585 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
19586    issue an error message indicating that TOKEN_DESC was expected.
19587
19588    Returns the token consumed, if the token had the appropriate type.
19589    Otherwise, returns NULL.  */
19590
19591 static cp_token *
19592 cp_parser_require (cp_parser* parser,
19593                    enum cpp_ttype type,
19594                    const char* token_desc)
19595 {
19596   if (cp_lexer_next_token_is (parser->lexer, type))
19597     return cp_lexer_consume_token (parser->lexer);
19598   else
19599     {
19600       /* Output the MESSAGE -- unless we're parsing tentatively.  */
19601       if (!cp_parser_simulate_error (parser))
19602         {
19603           char *message = concat ("expected ", token_desc, NULL);
19604           cp_parser_error (parser, message);
19605           free (message);
19606         }
19607       return NULL;
19608     }
19609 }
19610
19611 /* An error message is produced if the next token is not '>'.
19612    All further tokens are skipped until the desired token is
19613    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
19614
19615 static void
19616 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19617 {
19618   /* Current level of '< ... >'.  */
19619   unsigned level = 0;
19620   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
19621   unsigned nesting_depth = 0;
19622
19623   /* Are we ready, yet?  If not, issue error message.  */
19624   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19625     return;
19626
19627   /* Skip tokens until the desired token is found.  */
19628   while (true)
19629     {
19630       /* Peek at the next token.  */
19631       switch (cp_lexer_peek_token (parser->lexer)->type)
19632         {
19633         case CPP_LESS:
19634           if (!nesting_depth)
19635             ++level;
19636           break;
19637
19638         case CPP_RSHIFT:
19639           if (cxx_dialect == cxx98)
19640             /* C++0x views the `>>' operator as two `>' tokens, but
19641                C++98 does not. */
19642             break;
19643           else if (!nesting_depth && level-- == 0)
19644             {
19645               /* We've hit a `>>' where the first `>' closes the
19646                  template argument list, and the second `>' is
19647                  spurious.  Just consume the `>>' and stop; we've
19648                  already produced at least one error.  */
19649               cp_lexer_consume_token (parser->lexer);
19650               return;
19651             }
19652           /* Fall through for C++0x, so we handle the second `>' in
19653              the `>>'.  */
19654
19655         case CPP_GREATER:
19656           if (!nesting_depth && level-- == 0)
19657             {
19658               /* We've reached the token we want, consume it and stop.  */
19659               cp_lexer_consume_token (parser->lexer);
19660               return;
19661             }
19662           break;
19663
19664         case CPP_OPEN_PAREN:
19665         case CPP_OPEN_SQUARE:
19666           ++nesting_depth;
19667           break;
19668
19669         case CPP_CLOSE_PAREN:
19670         case CPP_CLOSE_SQUARE:
19671           if (nesting_depth-- == 0)
19672             return;
19673           break;
19674
19675         case CPP_EOF:
19676         case CPP_PRAGMA_EOL:
19677         case CPP_SEMICOLON:
19678         case CPP_OPEN_BRACE:
19679         case CPP_CLOSE_BRACE:
19680           /* The '>' was probably forgotten, don't look further.  */
19681           return;
19682
19683         default:
19684           break;
19685         }
19686
19687       /* Consume this token.  */
19688       cp_lexer_consume_token (parser->lexer);
19689     }
19690 }
19691
19692 /* If the next token is the indicated keyword, consume it.  Otherwise,
19693    issue an error message indicating that TOKEN_DESC was expected.
19694
19695    Returns the token consumed, if the token had the appropriate type.
19696    Otherwise, returns NULL.  */
19697
19698 static cp_token *
19699 cp_parser_require_keyword (cp_parser* parser,
19700                            enum rid keyword,
19701                            const char* token_desc)
19702 {
19703   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19704
19705   if (token && token->keyword != keyword)
19706     {
19707       dyn_string_t error_msg;
19708
19709       /* Format the error message.  */
19710       error_msg = dyn_string_new (0);
19711       dyn_string_append_cstr (error_msg, "expected ");
19712       dyn_string_append_cstr (error_msg, token_desc);
19713       cp_parser_error (parser, error_msg->s);
19714       dyn_string_delete (error_msg);
19715       return NULL;
19716     }
19717
19718   return token;
19719 }
19720
19721 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19722    function-definition.  */
19723
19724 static bool
19725 cp_parser_token_starts_function_definition_p (cp_token* token)
19726 {
19727   return (/* An ordinary function-body begins with an `{'.  */
19728           token->type == CPP_OPEN_BRACE
19729           /* A ctor-initializer begins with a `:'.  */
19730           || token->type == CPP_COLON
19731           /* A function-try-block begins with `try'.  */
19732           || token->keyword == RID_TRY
19733           /* The named return value extension begins with `return'.  */
19734           || token->keyword == RID_RETURN);
19735 }
19736
19737 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19738    definition.  */
19739
19740 static bool
19741 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19742 {
19743   cp_token *token;
19744
19745   token = cp_lexer_peek_token (parser->lexer);
19746   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19747 }
19748
19749 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19750    C++0x) ending a template-argument.  */
19751
19752 static bool
19753 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19754 {
19755   cp_token *token;
19756
19757   token = cp_lexer_peek_token (parser->lexer);
19758   return (token->type == CPP_COMMA 
19759           || token->type == CPP_GREATER
19760           || token->type == CPP_ELLIPSIS
19761           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19762 }
19763
19764 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19765    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
19766
19767 static bool
19768 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19769                                                      size_t n)
19770 {
19771   cp_token *token;
19772
19773   token = cp_lexer_peek_nth_token (parser->lexer, n);
19774   if (token->type == CPP_LESS)
19775     return true;
19776   /* Check for the sequence `<::' in the original code. It would be lexed as
19777      `[:', where `[' is a digraph, and there is no whitespace before
19778      `:'.  */
19779   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19780     {
19781       cp_token *token2;
19782       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19783       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19784         return true;
19785     }
19786   return false;
19787 }
19788
19789 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19790    or none_type otherwise.  */
19791
19792 static enum tag_types
19793 cp_parser_token_is_class_key (cp_token* token)
19794 {
19795   switch (token->keyword)
19796     {
19797     case RID_CLASS:
19798       return class_type;
19799     case RID_STRUCT:
19800       return record_type;
19801     case RID_UNION:
19802       return union_type;
19803
19804     default:
19805       return none_type;
19806     }
19807 }
19808
19809 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
19810
19811 static void
19812 cp_parser_check_class_key (enum tag_types class_key, tree type)
19813 {
19814   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19815     permerror (input_location, "%qs tag used in naming %q#T",
19816             class_key == union_type ? "union"
19817              : class_key == record_type ? "struct" : "class",
19818              type);
19819 }
19820
19821 /* Issue an error message if DECL is redeclared with different
19822    access than its original declaration [class.access.spec/3].
19823    This applies to nested classes and nested class templates.
19824    [class.mem/1].  */
19825
19826 static void
19827 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19828 {
19829   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19830     return;
19831
19832   if ((TREE_PRIVATE (decl)
19833        != (current_access_specifier == access_private_node))
19834       || (TREE_PROTECTED (decl)
19835           != (current_access_specifier == access_protected_node)))
19836     error_at (location, "%qD redeclared with different access", decl);
19837 }
19838
19839 /* Look for the `template' keyword, as a syntactic disambiguator.
19840    Return TRUE iff it is present, in which case it will be
19841    consumed.  */
19842
19843 static bool
19844 cp_parser_optional_template_keyword (cp_parser *parser)
19845 {
19846   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19847     {
19848       /* The `template' keyword can only be used within templates;
19849          outside templates the parser can always figure out what is a
19850          template and what is not.  */
19851       if (!processing_template_decl)
19852         {
19853           cp_token *token = cp_lexer_peek_token (parser->lexer);
19854           error_at (token->location,
19855                     "%<template%> (as a disambiguator) is only allowed "
19856                     "within templates");
19857           /* If this part of the token stream is rescanned, the same
19858              error message would be generated.  So, we purge the token
19859              from the stream.  */
19860           cp_lexer_purge_token (parser->lexer);
19861           return false;
19862         }
19863       else
19864         {
19865           /* Consume the `template' keyword.  */
19866           cp_lexer_consume_token (parser->lexer);
19867           return true;
19868         }
19869     }
19870
19871   return false;
19872 }
19873
19874 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
19875    set PARSER->SCOPE, and perform other related actions.  */
19876
19877 static void
19878 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19879 {
19880   int i;
19881   struct tree_check *check_value;
19882   deferred_access_check *chk;
19883   VEC (deferred_access_check,gc) *checks;
19884
19885   /* Get the stored value.  */
19886   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19887   /* Perform any access checks that were deferred.  */
19888   checks = check_value->checks;
19889   if (checks)
19890     {
19891       for (i = 0 ;
19892            VEC_iterate (deferred_access_check, checks, i, chk) ;
19893            ++i)
19894         {
19895           perform_or_defer_access_check (chk->binfo,
19896                                          chk->decl,
19897                                          chk->diag_decl);
19898         }
19899     }
19900   /* Set the scope from the stored value.  */
19901   parser->scope = check_value->value;
19902   parser->qualifying_scope = check_value->qualifying_scope;
19903   parser->object_scope = NULL_TREE;
19904 }
19905
19906 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
19907    encounter the end of a block before what we were looking for.  */
19908
19909 static bool
19910 cp_parser_cache_group (cp_parser *parser,
19911                        enum cpp_ttype end,
19912                        unsigned depth)
19913 {
19914   while (true)
19915     {
19916       cp_token *token = cp_lexer_peek_token (parser->lexer);
19917
19918       /* Abort a parenthesized expression if we encounter a semicolon.  */
19919       if ((end == CPP_CLOSE_PAREN || depth == 0)
19920           && token->type == CPP_SEMICOLON)
19921         return true;
19922       /* If we've reached the end of the file, stop.  */
19923       if (token->type == CPP_EOF
19924           || (end != CPP_PRAGMA_EOL
19925               && token->type == CPP_PRAGMA_EOL))
19926         return true;
19927       if (token->type == CPP_CLOSE_BRACE && depth == 0)
19928         /* We've hit the end of an enclosing block, so there's been some
19929            kind of syntax error.  */
19930         return true;
19931
19932       /* Consume the token.  */
19933       cp_lexer_consume_token (parser->lexer);
19934       /* See if it starts a new group.  */
19935       if (token->type == CPP_OPEN_BRACE)
19936         {
19937           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19938           /* In theory this should probably check end == '}', but
19939              cp_parser_save_member_function_body needs it to exit
19940              after either '}' or ')' when called with ')'.  */
19941           if (depth == 0)
19942             return false;
19943         }
19944       else if (token->type == CPP_OPEN_PAREN)
19945         {
19946           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19947           if (depth == 0 && end == CPP_CLOSE_PAREN)
19948             return false;
19949         }
19950       else if (token->type == CPP_PRAGMA)
19951         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19952       else if (token->type == end)
19953         return false;
19954     }
19955 }
19956
19957 /* Begin parsing tentatively.  We always save tokens while parsing
19958    tentatively so that if the tentative parsing fails we can restore the
19959    tokens.  */
19960
19961 static void
19962 cp_parser_parse_tentatively (cp_parser* parser)
19963 {
19964   /* Enter a new parsing context.  */
19965   parser->context = cp_parser_context_new (parser->context);
19966   /* Begin saving tokens.  */
19967   cp_lexer_save_tokens (parser->lexer);
19968   /* In order to avoid repetitive access control error messages,
19969      access checks are queued up until we are no longer parsing
19970      tentatively.  */
19971   push_deferring_access_checks (dk_deferred);
19972 }
19973
19974 /* Commit to the currently active tentative parse.  */
19975
19976 static void
19977 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19978 {
19979   cp_parser_context *context;
19980   cp_lexer *lexer;
19981
19982   /* Mark all of the levels as committed.  */
19983   lexer = parser->lexer;
19984   for (context = parser->context; context->next; context = context->next)
19985     {
19986       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19987         break;
19988       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19989       while (!cp_lexer_saving_tokens (lexer))
19990         lexer = lexer->next;
19991       cp_lexer_commit_tokens (lexer);
19992     }
19993 }
19994
19995 /* Abort the currently active tentative parse.  All consumed tokens
19996    will be rolled back, and no diagnostics will be issued.  */
19997
19998 static void
19999 cp_parser_abort_tentative_parse (cp_parser* parser)
20000 {
20001   cp_parser_simulate_error (parser);
20002   /* Now, pretend that we want to see if the construct was
20003      successfully parsed.  */
20004   cp_parser_parse_definitely (parser);
20005 }
20006
20007 /* Stop parsing tentatively.  If a parse error has occurred, restore the
20008    token stream.  Otherwise, commit to the tokens we have consumed.
20009    Returns true if no error occurred; false otherwise.  */
20010
20011 static bool
20012 cp_parser_parse_definitely (cp_parser* parser)
20013 {
20014   bool error_occurred;
20015   cp_parser_context *context;
20016
20017   /* Remember whether or not an error occurred, since we are about to
20018      destroy that information.  */
20019   error_occurred = cp_parser_error_occurred (parser);
20020   /* Remove the topmost context from the stack.  */
20021   context = parser->context;
20022   parser->context = context->next;
20023   /* If no parse errors occurred, commit to the tentative parse.  */
20024   if (!error_occurred)
20025     {
20026       /* Commit to the tokens read tentatively, unless that was
20027          already done.  */
20028       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20029         cp_lexer_commit_tokens (parser->lexer);
20030
20031       pop_to_parent_deferring_access_checks ();
20032     }
20033   /* Otherwise, if errors occurred, roll back our state so that things
20034      are just as they were before we began the tentative parse.  */
20035   else
20036     {
20037       cp_lexer_rollback_tokens (parser->lexer);
20038       pop_deferring_access_checks ();
20039     }
20040   /* Add the context to the front of the free list.  */
20041   context->next = cp_parser_context_free_list;
20042   cp_parser_context_free_list = context;
20043
20044   return !error_occurred;
20045 }
20046
20047 /* Returns true if we are parsing tentatively and are not committed to
20048    this tentative parse.  */
20049
20050 static bool
20051 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20052 {
20053   return (cp_parser_parsing_tentatively (parser)
20054           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20055 }
20056
20057 /* Returns nonzero iff an error has occurred during the most recent
20058    tentative parse.  */
20059
20060 static bool
20061 cp_parser_error_occurred (cp_parser* parser)
20062 {
20063   return (cp_parser_parsing_tentatively (parser)
20064           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20065 }
20066
20067 /* Returns nonzero if GNU extensions are allowed.  */
20068
20069 static bool
20070 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20071 {
20072   return parser->allow_gnu_extensions_p;
20073 }
20074 \f
20075 /* Objective-C++ Productions */
20076
20077
20078 /* Parse an Objective-C expression, which feeds into a primary-expression
20079    above.
20080
20081    objc-expression:
20082      objc-message-expression
20083      objc-string-literal
20084      objc-encode-expression
20085      objc-protocol-expression
20086      objc-selector-expression
20087
20088   Returns a tree representation of the expression.  */
20089
20090 static tree
20091 cp_parser_objc_expression (cp_parser* parser)
20092 {
20093   /* Try to figure out what kind of declaration is present.  */
20094   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20095
20096   switch (kwd->type)
20097     {
20098     case CPP_OPEN_SQUARE:
20099       return cp_parser_objc_message_expression (parser);
20100
20101     case CPP_OBJC_STRING:
20102       kwd = cp_lexer_consume_token (parser->lexer);
20103       return objc_build_string_object (kwd->u.value);
20104
20105     case CPP_KEYWORD:
20106       switch (kwd->keyword)
20107         {
20108         case RID_AT_ENCODE:
20109           return cp_parser_objc_encode_expression (parser);
20110
20111         case RID_AT_PROTOCOL:
20112           return cp_parser_objc_protocol_expression (parser);
20113
20114         case RID_AT_SELECTOR:
20115           return cp_parser_objc_selector_expression (parser);
20116
20117         default:
20118           break;
20119         }
20120     default:
20121       error_at (kwd->location,
20122                 "misplaced %<@%D%> Objective-C++ construct",
20123                 kwd->u.value);
20124       cp_parser_skip_to_end_of_block_or_statement (parser);
20125     }
20126
20127   return error_mark_node;
20128 }
20129
20130 /* Parse an Objective-C message expression.
20131
20132    objc-message-expression:
20133      [ objc-message-receiver objc-message-args ]
20134
20135    Returns a representation of an Objective-C message.  */
20136
20137 static tree
20138 cp_parser_objc_message_expression (cp_parser* parser)
20139 {
20140   tree receiver, messageargs;
20141
20142   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
20143   receiver = cp_parser_objc_message_receiver (parser);
20144   messageargs = cp_parser_objc_message_args (parser);
20145   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20146
20147   return objc_build_message_expr (build_tree_list (receiver, messageargs));
20148 }
20149
20150 /* Parse an objc-message-receiver.
20151
20152    objc-message-receiver:
20153      expression
20154      simple-type-specifier
20155
20156   Returns a representation of the type or expression.  */
20157
20158 static tree
20159 cp_parser_objc_message_receiver (cp_parser* parser)
20160 {
20161   tree rcv;
20162
20163   /* An Objective-C message receiver may be either (1) a type
20164      or (2) an expression.  */
20165   cp_parser_parse_tentatively (parser);
20166   rcv = cp_parser_expression (parser, false, NULL);
20167
20168   if (cp_parser_parse_definitely (parser))
20169     return rcv;
20170
20171   rcv = cp_parser_simple_type_specifier (parser,
20172                                          /*decl_specs=*/NULL,
20173                                          CP_PARSER_FLAGS_NONE);
20174
20175   return objc_get_class_reference (rcv);
20176 }
20177
20178 /* Parse the arguments and selectors comprising an Objective-C message.
20179
20180    objc-message-args:
20181      objc-selector
20182      objc-selector-args
20183      objc-selector-args , objc-comma-args
20184
20185    objc-selector-args:
20186      objc-selector [opt] : assignment-expression
20187      objc-selector-args objc-selector [opt] : assignment-expression
20188
20189    objc-comma-args:
20190      assignment-expression
20191      objc-comma-args , assignment-expression
20192
20193    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20194    selector arguments and TREE_VALUE containing a list of comma
20195    arguments.  */
20196
20197 static tree
20198 cp_parser_objc_message_args (cp_parser* parser)
20199 {
20200   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20201   bool maybe_unary_selector_p = true;
20202   cp_token *token = cp_lexer_peek_token (parser->lexer);
20203
20204   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20205     {
20206       tree selector = NULL_TREE, arg;
20207
20208       if (token->type != CPP_COLON)
20209         selector = cp_parser_objc_selector (parser);
20210
20211       /* Detect if we have a unary selector.  */
20212       if (maybe_unary_selector_p
20213           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20214         return build_tree_list (selector, NULL_TREE);
20215
20216       maybe_unary_selector_p = false;
20217       cp_parser_require (parser, CPP_COLON, "%<:%>");
20218       arg = cp_parser_assignment_expression (parser, false, NULL);
20219
20220       sel_args
20221         = chainon (sel_args,
20222                    build_tree_list (selector, arg));
20223
20224       token = cp_lexer_peek_token (parser->lexer);
20225     }
20226
20227   /* Handle non-selector arguments, if any. */
20228   while (token->type == CPP_COMMA)
20229     {
20230       tree arg;
20231
20232       cp_lexer_consume_token (parser->lexer);
20233       arg = cp_parser_assignment_expression (parser, false, NULL);
20234
20235       addl_args
20236         = chainon (addl_args,
20237                    build_tree_list (NULL_TREE, arg));
20238
20239       token = cp_lexer_peek_token (parser->lexer);
20240     }
20241
20242   return build_tree_list (sel_args, addl_args);
20243 }
20244
20245 /* Parse an Objective-C encode expression.
20246
20247    objc-encode-expression:
20248      @encode objc-typename
20249
20250    Returns an encoded representation of the type argument.  */
20251
20252 static tree
20253 cp_parser_objc_encode_expression (cp_parser* parser)
20254 {
20255   tree type;
20256   cp_token *token;
20257
20258   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
20259   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20260   token = cp_lexer_peek_token (parser->lexer);
20261   type = complete_type (cp_parser_type_id (parser));
20262   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20263
20264   if (!type)
20265     {
20266       error_at (token->location, 
20267                 "%<@encode%> must specify a type as an argument");
20268       return error_mark_node;
20269     }
20270
20271   return objc_build_encode_expr (type);
20272 }
20273
20274 /* Parse an Objective-C @defs expression.  */
20275
20276 static tree
20277 cp_parser_objc_defs_expression (cp_parser *parser)
20278 {
20279   tree name;
20280
20281   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
20282   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20283   name = cp_parser_identifier (parser);
20284   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20285
20286   return objc_get_class_ivars (name);
20287 }
20288
20289 /* Parse an Objective-C protocol expression.
20290
20291   objc-protocol-expression:
20292     @protocol ( identifier )
20293
20294   Returns a representation of the protocol expression.  */
20295
20296 static tree
20297 cp_parser_objc_protocol_expression (cp_parser* parser)
20298 {
20299   tree proto;
20300
20301   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20302   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20303   proto = cp_parser_identifier (parser);
20304   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20305
20306   return objc_build_protocol_expr (proto);
20307 }
20308
20309 /* Parse an Objective-C selector expression.
20310
20311    objc-selector-expression:
20312      @selector ( objc-method-signature )
20313
20314    objc-method-signature:
20315      objc-selector
20316      objc-selector-seq
20317
20318    objc-selector-seq:
20319      objc-selector :
20320      objc-selector-seq objc-selector :
20321
20322   Returns a representation of the method selector.  */
20323
20324 static tree
20325 cp_parser_objc_selector_expression (cp_parser* parser)
20326 {
20327   tree sel_seq = NULL_TREE;
20328   bool maybe_unary_selector_p = true;
20329   cp_token *token;
20330   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20331
20332   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
20333   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20334   token = cp_lexer_peek_token (parser->lexer);
20335
20336   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20337          || token->type == CPP_SCOPE)
20338     {
20339       tree selector = NULL_TREE;
20340
20341       if (token->type != CPP_COLON
20342           || token->type == CPP_SCOPE)
20343         selector = cp_parser_objc_selector (parser);
20344
20345       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20346           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20347         {
20348           /* Detect if we have a unary selector.  */
20349           if (maybe_unary_selector_p)
20350             {
20351               sel_seq = selector;
20352               goto finish_selector;
20353             }
20354           else
20355             {
20356               cp_parser_error (parser, "expected %<:%>");
20357             }
20358         }
20359       maybe_unary_selector_p = false;
20360       token = cp_lexer_consume_token (parser->lexer);
20361
20362       if (token->type == CPP_SCOPE)
20363         {
20364           sel_seq
20365             = chainon (sel_seq,
20366                        build_tree_list (selector, NULL_TREE));
20367           sel_seq
20368             = chainon (sel_seq,
20369                        build_tree_list (NULL_TREE, NULL_TREE));
20370         }
20371       else
20372         sel_seq
20373           = chainon (sel_seq,
20374                      build_tree_list (selector, NULL_TREE));
20375
20376       token = cp_lexer_peek_token (parser->lexer);
20377     }
20378
20379  finish_selector:
20380   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20381
20382   return objc_build_selector_expr (loc, sel_seq);
20383 }
20384
20385 /* Parse a list of identifiers.
20386
20387    objc-identifier-list:
20388      identifier
20389      objc-identifier-list , identifier
20390
20391    Returns a TREE_LIST of identifier nodes.  */
20392
20393 static tree
20394 cp_parser_objc_identifier_list (cp_parser* parser)
20395 {
20396   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20397   cp_token *sep = cp_lexer_peek_token (parser->lexer);
20398
20399   while (sep->type == CPP_COMMA)
20400     {
20401       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20402       list = chainon (list,
20403                       build_tree_list (NULL_TREE,
20404                                        cp_parser_identifier (parser)));
20405       sep = cp_lexer_peek_token (parser->lexer);
20406     }
20407
20408   return list;
20409 }
20410
20411 /* Parse an Objective-C alias declaration.
20412
20413    objc-alias-declaration:
20414      @compatibility_alias identifier identifier ;
20415
20416    This function registers the alias mapping with the Objective-C front end.
20417    It returns nothing.  */
20418
20419 static void
20420 cp_parser_objc_alias_declaration (cp_parser* parser)
20421 {
20422   tree alias, orig;
20423
20424   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
20425   alias = cp_parser_identifier (parser);
20426   orig = cp_parser_identifier (parser);
20427   objc_declare_alias (alias, orig);
20428   cp_parser_consume_semicolon_at_end_of_statement (parser);
20429 }
20430
20431 /* Parse an Objective-C class forward-declaration.
20432
20433    objc-class-declaration:
20434      @class objc-identifier-list ;
20435
20436    The function registers the forward declarations with the Objective-C
20437    front end.  It returns nothing.  */
20438
20439 static void
20440 cp_parser_objc_class_declaration (cp_parser* parser)
20441 {
20442   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
20443   objc_declare_class (cp_parser_objc_identifier_list (parser));
20444   cp_parser_consume_semicolon_at_end_of_statement (parser);
20445 }
20446
20447 /* Parse a list of Objective-C protocol references.
20448
20449    objc-protocol-refs-opt:
20450      objc-protocol-refs [opt]
20451
20452    objc-protocol-refs:
20453      < objc-identifier-list >
20454
20455    Returns a TREE_LIST of identifiers, if any.  */
20456
20457 static tree
20458 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20459 {
20460   tree protorefs = NULL_TREE;
20461
20462   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20463     {
20464       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
20465       protorefs = cp_parser_objc_identifier_list (parser);
20466       cp_parser_require (parser, CPP_GREATER, "%<>%>");
20467     }
20468
20469   return protorefs;
20470 }
20471
20472 /* Parse a Objective-C visibility specification.  */
20473
20474 static void
20475 cp_parser_objc_visibility_spec (cp_parser* parser)
20476 {
20477   cp_token *vis = cp_lexer_peek_token (parser->lexer);
20478
20479   switch (vis->keyword)
20480     {
20481     case RID_AT_PRIVATE:
20482       objc_set_visibility (2);
20483       break;
20484     case RID_AT_PROTECTED:
20485       objc_set_visibility (0);
20486       break;
20487     case RID_AT_PUBLIC:
20488       objc_set_visibility (1);
20489       break;
20490     default:
20491       return;
20492     }
20493
20494   /* Eat '@private'/'@protected'/'@public'.  */
20495   cp_lexer_consume_token (parser->lexer);
20496 }
20497
20498 /* Parse an Objective-C method type.  */
20499
20500 static void
20501 cp_parser_objc_method_type (cp_parser* parser)
20502 {
20503   objc_set_method_type
20504    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20505     ? PLUS_EXPR
20506     : MINUS_EXPR);
20507 }
20508
20509 /* Parse an Objective-C protocol qualifier.  */
20510
20511 static tree
20512 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20513 {
20514   tree quals = NULL_TREE, node;
20515   cp_token *token = cp_lexer_peek_token (parser->lexer);
20516
20517   node = token->u.value;
20518
20519   while (node && TREE_CODE (node) == IDENTIFIER_NODE
20520          && (node == ridpointers [(int) RID_IN]
20521              || node == ridpointers [(int) RID_OUT]
20522              || node == ridpointers [(int) RID_INOUT]
20523              || node == ridpointers [(int) RID_BYCOPY]
20524              || node == ridpointers [(int) RID_BYREF]
20525              || node == ridpointers [(int) RID_ONEWAY]))
20526     {
20527       quals = tree_cons (NULL_TREE, node, quals);
20528       cp_lexer_consume_token (parser->lexer);
20529       token = cp_lexer_peek_token (parser->lexer);
20530       node = token->u.value;
20531     }
20532
20533   return quals;
20534 }
20535
20536 /* Parse an Objective-C typename.  */
20537
20538 static tree
20539 cp_parser_objc_typename (cp_parser* parser)
20540 {
20541   tree type_name = NULL_TREE;
20542
20543   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20544     {
20545       tree proto_quals, cp_type = NULL_TREE;
20546
20547       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20548       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20549
20550       /* An ObjC type name may consist of just protocol qualifiers, in which
20551          case the type shall default to 'id'.  */
20552       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20553         cp_type = cp_parser_type_id (parser);
20554
20555       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20556       type_name = build_tree_list (proto_quals, cp_type);
20557     }
20558
20559   return type_name;
20560 }
20561
20562 /* Check to see if TYPE refers to an Objective-C selector name.  */
20563
20564 static bool
20565 cp_parser_objc_selector_p (enum cpp_ttype type)
20566 {
20567   return (type == CPP_NAME || type == CPP_KEYWORD
20568           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20569           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20570           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20571           || type == CPP_XOR || type == CPP_XOR_EQ);
20572 }
20573
20574 /* Parse an Objective-C selector.  */
20575
20576 static tree
20577 cp_parser_objc_selector (cp_parser* parser)
20578 {
20579   cp_token *token = cp_lexer_consume_token (parser->lexer);
20580
20581   if (!cp_parser_objc_selector_p (token->type))
20582     {
20583       error_at (token->location, "invalid Objective-C++ selector name");
20584       return error_mark_node;
20585     }
20586
20587   /* C++ operator names are allowed to appear in ObjC selectors.  */
20588   switch (token->type)
20589     {
20590     case CPP_AND_AND: return get_identifier ("and");
20591     case CPP_AND_EQ: return get_identifier ("and_eq");
20592     case CPP_AND: return get_identifier ("bitand");
20593     case CPP_OR: return get_identifier ("bitor");
20594     case CPP_COMPL: return get_identifier ("compl");
20595     case CPP_NOT: return get_identifier ("not");
20596     case CPP_NOT_EQ: return get_identifier ("not_eq");
20597     case CPP_OR_OR: return get_identifier ("or");
20598     case CPP_OR_EQ: return get_identifier ("or_eq");
20599     case CPP_XOR: return get_identifier ("xor");
20600     case CPP_XOR_EQ: return get_identifier ("xor_eq");
20601     default: return token->u.value;
20602     }
20603 }
20604
20605 /* Parse an Objective-C params list.  */
20606
20607 static tree
20608 cp_parser_objc_method_keyword_params (cp_parser* parser)
20609 {
20610   tree params = NULL_TREE;
20611   bool maybe_unary_selector_p = true;
20612   cp_token *token = cp_lexer_peek_token (parser->lexer);
20613
20614   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20615     {
20616       tree selector = NULL_TREE, type_name, identifier;
20617
20618       if (token->type != CPP_COLON)
20619         selector = cp_parser_objc_selector (parser);
20620
20621       /* Detect if we have a unary selector.  */
20622       if (maybe_unary_selector_p
20623           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20624         return selector;
20625
20626       maybe_unary_selector_p = false;
20627       cp_parser_require (parser, CPP_COLON, "%<:%>");
20628       type_name = cp_parser_objc_typename (parser);
20629       identifier = cp_parser_identifier (parser);
20630
20631       params
20632         = chainon (params,
20633                    objc_build_keyword_decl (selector,
20634                                             type_name,
20635                                             identifier));
20636
20637       token = cp_lexer_peek_token (parser->lexer);
20638     }
20639
20640   return params;
20641 }
20642
20643 /* Parse the non-keyword Objective-C params.  */
20644
20645 static tree
20646 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20647 {
20648   tree params = make_node (TREE_LIST);
20649   cp_token *token = cp_lexer_peek_token (parser->lexer);
20650   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
20651
20652   while (token->type == CPP_COMMA)
20653     {
20654       cp_parameter_declarator *parmdecl;
20655       tree parm;
20656
20657       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20658       token = cp_lexer_peek_token (parser->lexer);
20659
20660       if (token->type == CPP_ELLIPSIS)
20661         {
20662           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
20663           *ellipsisp = true;
20664           break;
20665         }
20666
20667       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20668       parm = grokdeclarator (parmdecl->declarator,
20669                              &parmdecl->decl_specifiers,
20670                              PARM, /*initialized=*/0,
20671                              /*attrlist=*/NULL);
20672
20673       chainon (params, build_tree_list (NULL_TREE, parm));
20674       token = cp_lexer_peek_token (parser->lexer);
20675     }
20676
20677   return params;
20678 }
20679
20680 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
20681
20682 static void
20683 cp_parser_objc_interstitial_code (cp_parser* parser)
20684 {
20685   cp_token *token = cp_lexer_peek_token (parser->lexer);
20686
20687   /* If the next token is `extern' and the following token is a string
20688      literal, then we have a linkage specification.  */
20689   if (token->keyword == RID_EXTERN
20690       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20691     cp_parser_linkage_specification (parser);
20692   /* Handle #pragma, if any.  */
20693   else if (token->type == CPP_PRAGMA)
20694     cp_parser_pragma (parser, pragma_external);
20695   /* Allow stray semicolons.  */
20696   else if (token->type == CPP_SEMICOLON)
20697     cp_lexer_consume_token (parser->lexer);
20698   /* Finally, try to parse a block-declaration, or a function-definition.  */
20699   else
20700     cp_parser_block_declaration (parser, /*statement_p=*/false);
20701 }
20702
20703 /* Parse a method signature.  */
20704
20705 static tree
20706 cp_parser_objc_method_signature (cp_parser* parser)
20707 {
20708   tree rettype, kwdparms, optparms;
20709   bool ellipsis = false;
20710
20711   cp_parser_objc_method_type (parser);
20712   rettype = cp_parser_objc_typename (parser);
20713   kwdparms = cp_parser_objc_method_keyword_params (parser);
20714   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20715
20716   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20717 }
20718
20719 /* Pars an Objective-C method prototype list.  */
20720
20721 static void
20722 cp_parser_objc_method_prototype_list (cp_parser* parser)
20723 {
20724   cp_token *token = cp_lexer_peek_token (parser->lexer);
20725
20726   while (token->keyword != RID_AT_END)
20727     {
20728       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20729         {
20730           objc_add_method_declaration
20731            (cp_parser_objc_method_signature (parser));
20732           cp_parser_consume_semicolon_at_end_of_statement (parser);
20733         }
20734       else
20735         /* Allow for interspersed non-ObjC++ code.  */
20736         cp_parser_objc_interstitial_code (parser);
20737
20738       token = cp_lexer_peek_token (parser->lexer);
20739     }
20740
20741   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20742   objc_finish_interface ();
20743 }
20744
20745 /* Parse an Objective-C method definition list.  */
20746
20747 static void
20748 cp_parser_objc_method_definition_list (cp_parser* parser)
20749 {
20750   cp_token *token = cp_lexer_peek_token (parser->lexer);
20751
20752   while (token->keyword != RID_AT_END)
20753     {
20754       tree meth;
20755
20756       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20757         {
20758           push_deferring_access_checks (dk_deferred);
20759           objc_start_method_definition
20760            (cp_parser_objc_method_signature (parser));
20761
20762           /* For historical reasons, we accept an optional semicolon.  */
20763           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20764             cp_lexer_consume_token (parser->lexer);
20765
20766           perform_deferred_access_checks ();
20767           stop_deferring_access_checks ();
20768           meth = cp_parser_function_definition_after_declarator (parser,
20769                                                                  false);
20770           pop_deferring_access_checks ();
20771           objc_finish_method_definition (meth);
20772         }
20773       else
20774         /* Allow for interspersed non-ObjC++ code.  */
20775         cp_parser_objc_interstitial_code (parser);
20776
20777       token = cp_lexer_peek_token (parser->lexer);
20778     }
20779
20780   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20781   objc_finish_implementation ();
20782 }
20783
20784 /* Parse Objective-C ivars.  */
20785
20786 static void
20787 cp_parser_objc_class_ivars (cp_parser* parser)
20788 {
20789   cp_token *token = cp_lexer_peek_token (parser->lexer);
20790
20791   if (token->type != CPP_OPEN_BRACE)
20792     return;     /* No ivars specified.  */
20793
20794   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
20795   token = cp_lexer_peek_token (parser->lexer);
20796
20797   while (token->type != CPP_CLOSE_BRACE)
20798     {
20799       cp_decl_specifier_seq declspecs;
20800       int decl_class_or_enum_p;
20801       tree prefix_attributes;
20802
20803       cp_parser_objc_visibility_spec (parser);
20804
20805       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20806         break;
20807
20808       cp_parser_decl_specifier_seq (parser,
20809                                     CP_PARSER_FLAGS_OPTIONAL,
20810                                     &declspecs,
20811                                     &decl_class_or_enum_p);
20812       prefix_attributes = declspecs.attributes;
20813       declspecs.attributes = NULL_TREE;
20814
20815       /* Keep going until we hit the `;' at the end of the
20816          declaration.  */
20817       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20818         {
20819           tree width = NULL_TREE, attributes, first_attribute, decl;
20820           cp_declarator *declarator = NULL;
20821           int ctor_dtor_or_conv_p;
20822
20823           /* Check for a (possibly unnamed) bitfield declaration.  */
20824           token = cp_lexer_peek_token (parser->lexer);
20825           if (token->type == CPP_COLON)
20826             goto eat_colon;
20827
20828           if (token->type == CPP_NAME
20829               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20830                   == CPP_COLON))
20831             {
20832               /* Get the name of the bitfield.  */
20833               declarator = make_id_declarator (NULL_TREE,
20834                                                cp_parser_identifier (parser),
20835                                                sfk_none);
20836
20837              eat_colon:
20838               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20839               /* Get the width of the bitfield.  */
20840               width
20841                 = cp_parser_constant_expression (parser,
20842                                                  /*allow_non_constant=*/false,
20843                                                  NULL);
20844             }
20845           else
20846             {
20847               /* Parse the declarator.  */
20848               declarator
20849                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20850                                         &ctor_dtor_or_conv_p,
20851                                         /*parenthesized_p=*/NULL,
20852                                         /*member_p=*/false);
20853             }
20854
20855           /* Look for attributes that apply to the ivar.  */
20856           attributes = cp_parser_attributes_opt (parser);
20857           /* Remember which attributes are prefix attributes and
20858              which are not.  */
20859           first_attribute = attributes;
20860           /* Combine the attributes.  */
20861           attributes = chainon (prefix_attributes, attributes);
20862
20863           if (width)
20864               /* Create the bitfield declaration.  */
20865               decl = grokbitfield (declarator, &declspecs,
20866                                    width,
20867                                    attributes);
20868           else
20869             decl = grokfield (declarator, &declspecs,
20870                               NULL_TREE, /*init_const_expr_p=*/false,
20871                               NULL_TREE, attributes);
20872
20873           /* Add the instance variable.  */
20874           objc_add_instance_variable (decl);
20875
20876           /* Reset PREFIX_ATTRIBUTES.  */
20877           while (attributes && TREE_CHAIN (attributes) != first_attribute)
20878             attributes = TREE_CHAIN (attributes);
20879           if (attributes)
20880             TREE_CHAIN (attributes) = NULL_TREE;
20881
20882           token = cp_lexer_peek_token (parser->lexer);
20883
20884           if (token->type == CPP_COMMA)
20885             {
20886               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20887               continue;
20888             }
20889           break;
20890         }
20891
20892       cp_parser_consume_semicolon_at_end_of_statement (parser);
20893       token = cp_lexer_peek_token (parser->lexer);
20894     }
20895
20896   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
20897   /* For historical reasons, we accept an optional semicolon.  */
20898   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20899     cp_lexer_consume_token (parser->lexer);
20900 }
20901
20902 /* Parse an Objective-C protocol declaration.  */
20903
20904 static void
20905 cp_parser_objc_protocol_declaration (cp_parser* parser)
20906 {
20907   tree proto, protorefs;
20908   cp_token *tok;
20909
20910   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20911   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20912     {
20913       tok = cp_lexer_peek_token (parser->lexer);
20914       error_at (tok->location, "identifier expected after %<@protocol%>");
20915       goto finish;
20916     }
20917
20918   /* See if we have a forward declaration or a definition.  */
20919   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20920
20921   /* Try a forward declaration first.  */
20922   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20923     {
20924       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20925      finish:
20926       cp_parser_consume_semicolon_at_end_of_statement (parser);
20927     }
20928
20929   /* Ok, we got a full-fledged definition (or at least should).  */
20930   else
20931     {
20932       proto = cp_parser_identifier (parser);
20933       protorefs = cp_parser_objc_protocol_refs_opt (parser);
20934       objc_start_protocol (proto, protorefs);
20935       cp_parser_objc_method_prototype_list (parser);
20936     }
20937 }
20938
20939 /* Parse an Objective-C superclass or category.  */
20940
20941 static void
20942 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20943                                                           tree *categ)
20944 {
20945   cp_token *next = cp_lexer_peek_token (parser->lexer);
20946
20947   *super = *categ = NULL_TREE;
20948   if (next->type == CPP_COLON)
20949     {
20950       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20951       *super = cp_parser_identifier (parser);
20952     }
20953   else if (next->type == CPP_OPEN_PAREN)
20954     {
20955       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20956       *categ = cp_parser_identifier (parser);
20957       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20958     }
20959 }
20960
20961 /* Parse an Objective-C class interface.  */
20962
20963 static void
20964 cp_parser_objc_class_interface (cp_parser* parser)
20965 {
20966   tree name, super, categ, protos;
20967
20968   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20969   name = cp_parser_identifier (parser);
20970   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20971   protos = cp_parser_objc_protocol_refs_opt (parser);
20972
20973   /* We have either a class or a category on our hands.  */
20974   if (categ)
20975     objc_start_category_interface (name, categ, protos);
20976   else
20977     {
20978       objc_start_class_interface (name, super, protos);
20979       /* Handle instance variable declarations, if any.  */
20980       cp_parser_objc_class_ivars (parser);
20981       objc_continue_interface ();
20982     }
20983
20984   cp_parser_objc_method_prototype_list (parser);
20985 }
20986
20987 /* Parse an Objective-C class implementation.  */
20988
20989 static void
20990 cp_parser_objc_class_implementation (cp_parser* parser)
20991 {
20992   tree name, super, categ;
20993
20994   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20995   name = cp_parser_identifier (parser);
20996   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20997
20998   /* We have either a class or a category on our hands.  */
20999   if (categ)
21000     objc_start_category_implementation (name, categ);
21001   else
21002     {
21003       objc_start_class_implementation (name, super);
21004       /* Handle instance variable declarations, if any.  */
21005       cp_parser_objc_class_ivars (parser);
21006       objc_continue_implementation ();
21007     }
21008
21009   cp_parser_objc_method_definition_list (parser);
21010 }
21011
21012 /* Consume the @end token and finish off the implementation.  */
21013
21014 static void
21015 cp_parser_objc_end_implementation (cp_parser* parser)
21016 {
21017   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
21018   objc_finish_implementation ();
21019 }
21020
21021 /* Parse an Objective-C declaration.  */
21022
21023 static void
21024 cp_parser_objc_declaration (cp_parser* parser)
21025 {
21026   /* Try to figure out what kind of declaration is present.  */
21027   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21028
21029   switch (kwd->keyword)
21030     {
21031     case RID_AT_ALIAS:
21032       cp_parser_objc_alias_declaration (parser);
21033       break;
21034     case RID_AT_CLASS:
21035       cp_parser_objc_class_declaration (parser);
21036       break;
21037     case RID_AT_PROTOCOL:
21038       cp_parser_objc_protocol_declaration (parser);
21039       break;
21040     case RID_AT_INTERFACE:
21041       cp_parser_objc_class_interface (parser);
21042       break;
21043     case RID_AT_IMPLEMENTATION:
21044       cp_parser_objc_class_implementation (parser);
21045       break;
21046     case RID_AT_END:
21047       cp_parser_objc_end_implementation (parser);
21048       break;
21049     default:
21050       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21051                 kwd->u.value);
21052       cp_parser_skip_to_end_of_block_or_statement (parser);
21053     }
21054 }
21055
21056 /* Parse an Objective-C try-catch-finally statement.
21057
21058    objc-try-catch-finally-stmt:
21059      @try compound-statement objc-catch-clause-seq [opt]
21060        objc-finally-clause [opt]
21061
21062    objc-catch-clause-seq:
21063      objc-catch-clause objc-catch-clause-seq [opt]
21064
21065    objc-catch-clause:
21066      @catch ( exception-declaration ) compound-statement
21067
21068    objc-finally-clause
21069      @finally compound-statement
21070
21071    Returns NULL_TREE.  */
21072
21073 static tree
21074 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21075   location_t location;
21076   tree stmt;
21077
21078   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
21079   location = cp_lexer_peek_token (parser->lexer)->location;
21080   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21081      node, lest it get absorbed into the surrounding block.  */
21082   stmt = push_stmt_list ();
21083   cp_parser_compound_statement (parser, NULL, false);
21084   objc_begin_try_stmt (location, pop_stmt_list (stmt));
21085
21086   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21087     {
21088       cp_parameter_declarator *parmdecl;
21089       tree parm;
21090
21091       cp_lexer_consume_token (parser->lexer);
21092       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21093       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21094       parm = grokdeclarator (parmdecl->declarator,
21095                              &parmdecl->decl_specifiers,
21096                              PARM, /*initialized=*/0,
21097                              /*attrlist=*/NULL);
21098       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21099       objc_begin_catch_clause (parm);
21100       cp_parser_compound_statement (parser, NULL, false);
21101       objc_finish_catch_clause ();
21102     }
21103
21104   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21105     {
21106       cp_lexer_consume_token (parser->lexer);
21107       location = cp_lexer_peek_token (parser->lexer)->location;
21108       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21109          node, lest it get absorbed into the surrounding block.  */
21110       stmt = push_stmt_list ();
21111       cp_parser_compound_statement (parser, NULL, false);
21112       objc_build_finally_clause (location, pop_stmt_list (stmt));
21113     }
21114
21115   return objc_finish_try_stmt ();
21116 }
21117
21118 /* Parse an Objective-C synchronized statement.
21119
21120    objc-synchronized-stmt:
21121      @synchronized ( expression ) compound-statement
21122
21123    Returns NULL_TREE.  */
21124
21125 static tree
21126 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21127   location_t location;
21128   tree lock, stmt;
21129
21130   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21131
21132   location = cp_lexer_peek_token (parser->lexer)->location;
21133   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21134   lock = cp_parser_expression (parser, false, NULL);
21135   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21136
21137   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21138      node, lest it get absorbed into the surrounding block.  */
21139   stmt = push_stmt_list ();
21140   cp_parser_compound_statement (parser, NULL, false);
21141
21142   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21143 }
21144
21145 /* Parse an Objective-C throw statement.
21146
21147    objc-throw-stmt:
21148      @throw assignment-expression [opt] ;
21149
21150    Returns a constructed '@throw' statement.  */
21151
21152 static tree
21153 cp_parser_objc_throw_statement (cp_parser *parser) {
21154   tree expr = NULL_TREE;
21155   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21156
21157   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21158
21159   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21160     expr = cp_parser_assignment_expression (parser, false, NULL);
21161
21162   cp_parser_consume_semicolon_at_end_of_statement (parser);
21163
21164   return objc_build_throw_stmt (loc, expr);
21165 }
21166
21167 /* Parse an Objective-C statement.  */
21168
21169 static tree
21170 cp_parser_objc_statement (cp_parser * parser) {
21171   /* Try to figure out what kind of declaration is present.  */
21172   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21173
21174   switch (kwd->keyword)
21175     {
21176     case RID_AT_TRY:
21177       return cp_parser_objc_try_catch_finally_statement (parser);
21178     case RID_AT_SYNCHRONIZED:
21179       return cp_parser_objc_synchronized_statement (parser);
21180     case RID_AT_THROW:
21181       return cp_parser_objc_throw_statement (parser);
21182     default:
21183       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21184                kwd->u.value);
21185       cp_parser_skip_to_end_of_block_or_statement (parser);
21186     }
21187
21188   return error_mark_node;
21189 }
21190 \f
21191 /* OpenMP 2.5 parsing routines.  */
21192
21193 /* Returns name of the next clause.
21194    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21195    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
21196    returned and the token is consumed.  */
21197
21198 static pragma_omp_clause
21199 cp_parser_omp_clause_name (cp_parser *parser)
21200 {
21201   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21202
21203   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21204     result = PRAGMA_OMP_CLAUSE_IF;
21205   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21206     result = PRAGMA_OMP_CLAUSE_DEFAULT;
21207   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21208     result = PRAGMA_OMP_CLAUSE_PRIVATE;
21209   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21210     {
21211       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21212       const char *p = IDENTIFIER_POINTER (id);
21213
21214       switch (p[0])
21215         {
21216         case 'c':
21217           if (!strcmp ("collapse", p))
21218             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21219           else if (!strcmp ("copyin", p))
21220             result = PRAGMA_OMP_CLAUSE_COPYIN;
21221           else if (!strcmp ("copyprivate", p))
21222             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21223           break;
21224         case 'f':
21225           if (!strcmp ("firstprivate", p))
21226             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21227           break;
21228         case 'l':
21229           if (!strcmp ("lastprivate", p))
21230             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21231           break;
21232         case 'n':
21233           if (!strcmp ("nowait", p))
21234             result = PRAGMA_OMP_CLAUSE_NOWAIT;
21235           else if (!strcmp ("num_threads", p))
21236             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21237           break;
21238         case 'o':
21239           if (!strcmp ("ordered", p))
21240             result = PRAGMA_OMP_CLAUSE_ORDERED;
21241           break;
21242         case 'r':
21243           if (!strcmp ("reduction", p))
21244             result = PRAGMA_OMP_CLAUSE_REDUCTION;
21245           break;
21246         case 's':
21247           if (!strcmp ("schedule", p))
21248             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21249           else if (!strcmp ("shared", p))
21250             result = PRAGMA_OMP_CLAUSE_SHARED;
21251           break;
21252         case 'u':
21253           if (!strcmp ("untied", p))
21254             result = PRAGMA_OMP_CLAUSE_UNTIED;
21255           break;
21256         }
21257     }
21258
21259   if (result != PRAGMA_OMP_CLAUSE_NONE)
21260     cp_lexer_consume_token (parser->lexer);
21261
21262   return result;
21263 }
21264
21265 /* Validate that a clause of the given type does not already exist.  */
21266
21267 static void
21268 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21269                            const char *name, location_t location)
21270 {
21271   tree c;
21272
21273   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21274     if (OMP_CLAUSE_CODE (c) == code)
21275       {
21276         error_at (location, "too many %qs clauses", name);
21277         break;
21278       }
21279 }
21280
21281 /* OpenMP 2.5:
21282    variable-list:
21283      identifier
21284      variable-list , identifier
21285
21286    In addition, we match a closing parenthesis.  An opening parenthesis
21287    will have been consumed by the caller.
21288
21289    If KIND is nonzero, create the appropriate node and install the decl
21290    in OMP_CLAUSE_DECL and add the node to the head of the list.
21291
21292    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21293    return the list created.  */
21294
21295 static tree
21296 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21297                                 tree list)
21298 {
21299   cp_token *token;
21300   while (1)
21301     {
21302       tree name, decl;
21303
21304       token = cp_lexer_peek_token (parser->lexer);
21305       name = cp_parser_id_expression (parser, /*template_p=*/false,
21306                                       /*check_dependency_p=*/true,
21307                                       /*template_p=*/NULL,
21308                                       /*declarator_p=*/false,
21309                                       /*optional_p=*/false);
21310       if (name == error_mark_node)
21311         goto skip_comma;
21312
21313       decl = cp_parser_lookup_name_simple (parser, name, token->location);
21314       if (decl == error_mark_node)
21315         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21316       else if (kind != 0)
21317         {
21318           tree u = build_omp_clause (token->location, kind);
21319           OMP_CLAUSE_DECL (u) = decl;
21320           OMP_CLAUSE_CHAIN (u) = list;
21321           list = u;
21322         }
21323       else
21324         list = tree_cons (decl, NULL_TREE, list);
21325
21326     get_comma:
21327       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21328         break;
21329       cp_lexer_consume_token (parser->lexer);
21330     }
21331
21332   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21333     {
21334       int ending;
21335
21336       /* Try to resync to an unnested comma.  Copied from
21337          cp_parser_parenthesized_expression_list.  */
21338     skip_comma:
21339       ending = cp_parser_skip_to_closing_parenthesis (parser,
21340                                                       /*recovering=*/true,
21341                                                       /*or_comma=*/true,
21342                                                       /*consume_paren=*/true);
21343       if (ending < 0)
21344         goto get_comma;
21345     }
21346
21347   return list;
21348 }
21349
21350 /* Similarly, but expect leading and trailing parenthesis.  This is a very
21351    common case for omp clauses.  */
21352
21353 static tree
21354 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21355 {
21356   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21357     return cp_parser_omp_var_list_no_open (parser, kind, list);
21358   return list;
21359 }
21360
21361 /* OpenMP 3.0:
21362    collapse ( constant-expression ) */
21363
21364 static tree
21365 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21366 {
21367   tree c, num;
21368   location_t loc;
21369   HOST_WIDE_INT n;
21370
21371   loc = cp_lexer_peek_token (parser->lexer)->location;
21372   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21373     return list;
21374
21375   num = cp_parser_constant_expression (parser, false, NULL);
21376
21377   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21378     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21379                                            /*or_comma=*/false,
21380                                            /*consume_paren=*/true);
21381
21382   if (num == error_mark_node)
21383     return list;
21384   num = fold_non_dependent_expr (num);
21385   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21386       || !host_integerp (num, 0)
21387       || (n = tree_low_cst (num, 0)) <= 0
21388       || (int) n != n)
21389     {
21390       error_at (loc, "collapse argument needs positive constant integer expression");
21391       return list;
21392     }
21393
21394   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21395   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21396   OMP_CLAUSE_CHAIN (c) = list;
21397   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21398
21399   return c;
21400 }
21401
21402 /* OpenMP 2.5:
21403    default ( shared | none ) */
21404
21405 static tree
21406 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21407 {
21408   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21409   tree c;
21410
21411   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21412     return list;
21413   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21414     {
21415       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21416       const char *p = IDENTIFIER_POINTER (id);
21417
21418       switch (p[0])
21419         {
21420         case 'n':
21421           if (strcmp ("none", p) != 0)
21422             goto invalid_kind;
21423           kind = OMP_CLAUSE_DEFAULT_NONE;
21424           break;
21425
21426         case 's':
21427           if (strcmp ("shared", p) != 0)
21428             goto invalid_kind;
21429           kind = OMP_CLAUSE_DEFAULT_SHARED;
21430           break;
21431
21432         default:
21433           goto invalid_kind;
21434         }
21435
21436       cp_lexer_consume_token (parser->lexer);
21437     }
21438   else
21439     {
21440     invalid_kind:
21441       cp_parser_error (parser, "expected %<none%> or %<shared%>");
21442     }
21443
21444   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21445     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21446                                            /*or_comma=*/false,
21447                                            /*consume_paren=*/true);
21448
21449   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21450     return list;
21451
21452   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21453   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21454   OMP_CLAUSE_CHAIN (c) = list;
21455   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21456
21457   return c;
21458 }
21459
21460 /* OpenMP 2.5:
21461    if ( expression ) */
21462
21463 static tree
21464 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21465 {
21466   tree t, c;
21467
21468   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21469     return list;
21470
21471   t = cp_parser_condition (parser);
21472
21473   if (t == error_mark_node
21474       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21475     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21476                                            /*or_comma=*/false,
21477                                            /*consume_paren=*/true);
21478
21479   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21480
21481   c = build_omp_clause (location, OMP_CLAUSE_IF);
21482   OMP_CLAUSE_IF_EXPR (c) = t;
21483   OMP_CLAUSE_CHAIN (c) = list;
21484
21485   return c;
21486 }
21487
21488 /* OpenMP 2.5:
21489    nowait */
21490
21491 static tree
21492 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21493                              tree list, location_t location)
21494 {
21495   tree c;
21496
21497   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21498
21499   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21500   OMP_CLAUSE_CHAIN (c) = list;
21501   return c;
21502 }
21503
21504 /* OpenMP 2.5:
21505    num_threads ( expression ) */
21506
21507 static tree
21508 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21509                                   location_t location)
21510 {
21511   tree t, c;
21512
21513   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21514     return list;
21515
21516   t = cp_parser_expression (parser, false, NULL);
21517
21518   if (t == error_mark_node
21519       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21520     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21521                                            /*or_comma=*/false,
21522                                            /*consume_paren=*/true);
21523
21524   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21525                              "num_threads", location);
21526
21527   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21528   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21529   OMP_CLAUSE_CHAIN (c) = list;
21530
21531   return c;
21532 }
21533
21534 /* OpenMP 2.5:
21535    ordered */
21536
21537 static tree
21538 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21539                               tree list, location_t location)
21540 {
21541   tree c;
21542
21543   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21544                              "ordered", location);
21545
21546   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21547   OMP_CLAUSE_CHAIN (c) = list;
21548   return c;
21549 }
21550
21551 /* OpenMP 2.5:
21552    reduction ( reduction-operator : variable-list )
21553
21554    reduction-operator:
21555      One of: + * - & ^ | && || */
21556
21557 static tree
21558 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21559 {
21560   enum tree_code code;
21561   tree nlist, c;
21562
21563   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21564     return list;
21565
21566   switch (cp_lexer_peek_token (parser->lexer)->type)
21567     {
21568     case CPP_PLUS:
21569       code = PLUS_EXPR;
21570       break;
21571     case CPP_MULT:
21572       code = MULT_EXPR;
21573       break;
21574     case CPP_MINUS:
21575       code = MINUS_EXPR;
21576       break;
21577     case CPP_AND:
21578       code = BIT_AND_EXPR;
21579       break;
21580     case CPP_XOR:
21581       code = BIT_XOR_EXPR;
21582       break;
21583     case CPP_OR:
21584       code = BIT_IOR_EXPR;
21585       break;
21586     case CPP_AND_AND:
21587       code = TRUTH_ANDIF_EXPR;
21588       break;
21589     case CPP_OR_OR:
21590       code = TRUTH_ORIF_EXPR;
21591       break;
21592     default:
21593       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21594                                "%<|%>, %<&&%>, or %<||%>");
21595     resync_fail:
21596       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21597                                              /*or_comma=*/false,
21598                                              /*consume_paren=*/true);
21599       return list;
21600     }
21601   cp_lexer_consume_token (parser->lexer);
21602
21603   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21604     goto resync_fail;
21605
21606   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21607   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21608     OMP_CLAUSE_REDUCTION_CODE (c) = code;
21609
21610   return nlist;
21611 }
21612
21613 /* OpenMP 2.5:
21614    schedule ( schedule-kind )
21615    schedule ( schedule-kind , expression )
21616
21617    schedule-kind:
21618      static | dynamic | guided | runtime | auto  */
21619
21620 static tree
21621 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21622 {
21623   tree c, t;
21624
21625   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21626     return list;
21627
21628   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21629
21630   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21631     {
21632       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21633       const char *p = IDENTIFIER_POINTER (id);
21634
21635       switch (p[0])
21636         {
21637         case 'd':
21638           if (strcmp ("dynamic", p) != 0)
21639             goto invalid_kind;
21640           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21641           break;
21642
21643         case 'g':
21644           if (strcmp ("guided", p) != 0)
21645             goto invalid_kind;
21646           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21647           break;
21648
21649         case 'r':
21650           if (strcmp ("runtime", p) != 0)
21651             goto invalid_kind;
21652           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21653           break;
21654
21655         default:
21656           goto invalid_kind;
21657         }
21658     }
21659   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21660     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21661   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21662     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21663   else
21664     goto invalid_kind;
21665   cp_lexer_consume_token (parser->lexer);
21666
21667   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21668     {
21669       cp_token *token;
21670       cp_lexer_consume_token (parser->lexer);
21671
21672       token = cp_lexer_peek_token (parser->lexer);
21673       t = cp_parser_assignment_expression (parser, false, NULL);
21674
21675       if (t == error_mark_node)
21676         goto resync_fail;
21677       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21678         error_at (token->location, "schedule %<runtime%> does not take "
21679                   "a %<chunk_size%> parameter");
21680       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21681         error_at (token->location, "schedule %<auto%> does not take "
21682                   "a %<chunk_size%> parameter");
21683       else
21684         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21685
21686       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21687         goto resync_fail;
21688     }
21689   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21690     goto resync_fail;
21691
21692   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21693   OMP_CLAUSE_CHAIN (c) = list;
21694   return c;
21695
21696  invalid_kind:
21697   cp_parser_error (parser, "invalid schedule kind");
21698  resync_fail:
21699   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21700                                          /*or_comma=*/false,
21701                                          /*consume_paren=*/true);
21702   return list;
21703 }
21704
21705 /* OpenMP 3.0:
21706    untied */
21707
21708 static tree
21709 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21710                              tree list, location_t location)
21711 {
21712   tree c;
21713
21714   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21715
21716   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21717   OMP_CLAUSE_CHAIN (c) = list;
21718   return c;
21719 }
21720
21721 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
21722    is a bitmask in MASK.  Return the list of clauses found; the result
21723    of clause default goes in *pdefault.  */
21724
21725 static tree
21726 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21727                            const char *where, cp_token *pragma_tok)
21728 {
21729   tree clauses = NULL;
21730   bool first = true;
21731   cp_token *token = NULL;
21732
21733   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21734     {
21735       pragma_omp_clause c_kind;
21736       const char *c_name;
21737       tree prev = clauses;
21738
21739       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21740         cp_lexer_consume_token (parser->lexer);
21741
21742       token = cp_lexer_peek_token (parser->lexer);
21743       c_kind = cp_parser_omp_clause_name (parser);
21744       first = false;
21745
21746       switch (c_kind)
21747         {
21748         case PRAGMA_OMP_CLAUSE_COLLAPSE:
21749           clauses = cp_parser_omp_clause_collapse (parser, clauses,
21750                                                    token->location);
21751           c_name = "collapse";
21752           break;
21753         case PRAGMA_OMP_CLAUSE_COPYIN:
21754           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21755           c_name = "copyin";
21756           break;
21757         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21758           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21759                                             clauses);
21760           c_name = "copyprivate";
21761           break;
21762         case PRAGMA_OMP_CLAUSE_DEFAULT:
21763           clauses = cp_parser_omp_clause_default (parser, clauses,
21764                                                   token->location);
21765           c_name = "default";
21766           break;
21767         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21768           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21769                                             clauses);
21770           c_name = "firstprivate";
21771           break;
21772         case PRAGMA_OMP_CLAUSE_IF:
21773           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21774           c_name = "if";
21775           break;
21776         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21777           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21778                                             clauses);
21779           c_name = "lastprivate";
21780           break;
21781         case PRAGMA_OMP_CLAUSE_NOWAIT:
21782           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21783           c_name = "nowait";
21784           break;
21785         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21786           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21787                                                       token->location);
21788           c_name = "num_threads";
21789           break;
21790         case PRAGMA_OMP_CLAUSE_ORDERED:
21791           clauses = cp_parser_omp_clause_ordered (parser, clauses,
21792                                                   token->location);
21793           c_name = "ordered";
21794           break;
21795         case PRAGMA_OMP_CLAUSE_PRIVATE:
21796           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21797                                             clauses);
21798           c_name = "private";
21799           break;
21800         case PRAGMA_OMP_CLAUSE_REDUCTION:
21801           clauses = cp_parser_omp_clause_reduction (parser, clauses);
21802           c_name = "reduction";
21803           break;
21804         case PRAGMA_OMP_CLAUSE_SCHEDULE:
21805           clauses = cp_parser_omp_clause_schedule (parser, clauses,
21806                                                    token->location);
21807           c_name = "schedule";
21808           break;
21809         case PRAGMA_OMP_CLAUSE_SHARED:
21810           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21811                                             clauses);
21812           c_name = "shared";
21813           break;
21814         case PRAGMA_OMP_CLAUSE_UNTIED:
21815           clauses = cp_parser_omp_clause_untied (parser, clauses,
21816                                                  token->location);
21817           c_name = "nowait";
21818           break;
21819         default:
21820           cp_parser_error (parser, "expected %<#pragma omp%> clause");
21821           goto saw_error;
21822         }
21823
21824       if (((mask >> c_kind) & 1) == 0)
21825         {
21826           /* Remove the invalid clause(s) from the list to avoid
21827              confusing the rest of the compiler.  */
21828           clauses = prev;
21829           error_at (token->location, "%qs is not valid for %qs", c_name, where);
21830         }
21831     }
21832  saw_error:
21833   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21834   return finish_omp_clauses (clauses);
21835 }
21836
21837 /* OpenMP 2.5:
21838    structured-block:
21839      statement
21840
21841    In practice, we're also interested in adding the statement to an
21842    outer node.  So it is convenient if we work around the fact that
21843    cp_parser_statement calls add_stmt.  */
21844
21845 static unsigned
21846 cp_parser_begin_omp_structured_block (cp_parser *parser)
21847 {
21848   unsigned save = parser->in_statement;
21849
21850   /* Only move the values to IN_OMP_BLOCK if they weren't false.
21851      This preserves the "not within loop or switch" style error messages
21852      for nonsense cases like
21853         void foo() {
21854         #pragma omp single
21855           break;
21856         }
21857   */
21858   if (parser->in_statement)
21859     parser->in_statement = IN_OMP_BLOCK;
21860
21861   return save;
21862 }
21863
21864 static void
21865 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21866 {
21867   parser->in_statement = save;
21868 }
21869
21870 static tree
21871 cp_parser_omp_structured_block (cp_parser *parser)
21872 {
21873   tree stmt = begin_omp_structured_block ();
21874   unsigned int save = cp_parser_begin_omp_structured_block (parser);
21875
21876   cp_parser_statement (parser, NULL_TREE, false, NULL);
21877
21878   cp_parser_end_omp_structured_block (parser, save);
21879   return finish_omp_structured_block (stmt);
21880 }
21881
21882 /* OpenMP 2.5:
21883    # pragma omp atomic new-line
21884      expression-stmt
21885
21886    expression-stmt:
21887      x binop= expr | x++ | ++x | x-- | --x
21888    binop:
21889      +, *, -, /, &, ^, |, <<, >>
21890
21891   where x is an lvalue expression with scalar type.  */
21892
21893 static void
21894 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21895 {
21896   tree lhs, rhs;
21897   enum tree_code code;
21898
21899   cp_parser_require_pragma_eol (parser, pragma_tok);
21900
21901   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21902                                     /*cast_p=*/false, NULL);
21903   switch (TREE_CODE (lhs))
21904     {
21905     case ERROR_MARK:
21906       goto saw_error;
21907
21908     case PREINCREMENT_EXPR:
21909     case POSTINCREMENT_EXPR:
21910       lhs = TREE_OPERAND (lhs, 0);
21911       code = PLUS_EXPR;
21912       rhs = integer_one_node;
21913       break;
21914
21915     case PREDECREMENT_EXPR:
21916     case POSTDECREMENT_EXPR:
21917       lhs = TREE_OPERAND (lhs, 0);
21918       code = MINUS_EXPR;
21919       rhs = integer_one_node;
21920       break;
21921
21922     default:
21923       switch (cp_lexer_peek_token (parser->lexer)->type)
21924         {
21925         case CPP_MULT_EQ:
21926           code = MULT_EXPR;
21927           break;
21928         case CPP_DIV_EQ:
21929           code = TRUNC_DIV_EXPR;
21930           break;
21931         case CPP_PLUS_EQ:
21932           code = PLUS_EXPR;
21933           break;
21934         case CPP_MINUS_EQ:
21935           code = MINUS_EXPR;
21936           break;
21937         case CPP_LSHIFT_EQ:
21938           code = LSHIFT_EXPR;
21939           break;
21940         case CPP_RSHIFT_EQ:
21941           code = RSHIFT_EXPR;
21942           break;
21943         case CPP_AND_EQ:
21944           code = BIT_AND_EXPR;
21945           break;
21946         case CPP_OR_EQ:
21947           code = BIT_IOR_EXPR;
21948           break;
21949         case CPP_XOR_EQ:
21950           code = BIT_XOR_EXPR;
21951           break;
21952         default:
21953           cp_parser_error (parser,
21954                            "invalid operator for %<#pragma omp atomic%>");
21955           goto saw_error;
21956         }
21957       cp_lexer_consume_token (parser->lexer);
21958
21959       rhs = cp_parser_expression (parser, false, NULL);
21960       if (rhs == error_mark_node)
21961         goto saw_error;
21962       break;
21963     }
21964   finish_omp_atomic (code, lhs, rhs);
21965   cp_parser_consume_semicolon_at_end_of_statement (parser);
21966   return;
21967
21968  saw_error:
21969   cp_parser_skip_to_end_of_block_or_statement (parser);
21970 }
21971
21972
21973 /* OpenMP 2.5:
21974    # pragma omp barrier new-line  */
21975
21976 static void
21977 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21978 {
21979   cp_parser_require_pragma_eol (parser, pragma_tok);
21980   finish_omp_barrier ();
21981 }
21982
21983 /* OpenMP 2.5:
21984    # pragma omp critical [(name)] new-line
21985      structured-block  */
21986
21987 static tree
21988 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21989 {
21990   tree stmt, name = NULL;
21991
21992   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21993     {
21994       cp_lexer_consume_token (parser->lexer);
21995
21996       name = cp_parser_identifier (parser);
21997
21998       if (name == error_mark_node
21999           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22000         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22001                                                /*or_comma=*/false,
22002                                                /*consume_paren=*/true);
22003       if (name == error_mark_node)
22004         name = NULL;
22005     }
22006   cp_parser_require_pragma_eol (parser, pragma_tok);
22007
22008   stmt = cp_parser_omp_structured_block (parser);
22009   return c_finish_omp_critical (input_location, stmt, name);
22010 }
22011
22012 /* OpenMP 2.5:
22013    # pragma omp flush flush-vars[opt] new-line
22014
22015    flush-vars:
22016      ( variable-list ) */
22017
22018 static void
22019 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22020 {
22021   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22022     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22023   cp_parser_require_pragma_eol (parser, pragma_tok);
22024
22025   finish_omp_flush ();
22026 }
22027
22028 /* Helper function, to parse omp for increment expression.  */
22029
22030 static tree
22031 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22032 {
22033   tree cond = cp_parser_binary_expression (parser, false, true,
22034                                            PREC_NOT_OPERATOR, NULL);
22035   bool overloaded_p;
22036
22037   if (cond == error_mark_node
22038       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22039     {
22040       cp_parser_skip_to_end_of_statement (parser);
22041       return error_mark_node;
22042     }
22043
22044   switch (TREE_CODE (cond))
22045     {
22046     case GT_EXPR:
22047     case GE_EXPR:
22048     case LT_EXPR:
22049     case LE_EXPR:
22050       break;
22051     default:
22052       return error_mark_node;
22053     }
22054
22055   /* If decl is an iterator, preserve LHS and RHS of the relational
22056      expr until finish_omp_for.  */
22057   if (decl
22058       && (type_dependent_expression_p (decl)
22059           || CLASS_TYPE_P (TREE_TYPE (decl))))
22060     return cond;
22061
22062   return build_x_binary_op (TREE_CODE (cond),
22063                             TREE_OPERAND (cond, 0), ERROR_MARK,
22064                             TREE_OPERAND (cond, 1), ERROR_MARK,
22065                             &overloaded_p, tf_warning_or_error);
22066 }
22067
22068 /* Helper function, to parse omp for increment expression.  */
22069
22070 static tree
22071 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22072 {
22073   cp_token *token = cp_lexer_peek_token (parser->lexer);
22074   enum tree_code op;
22075   tree lhs, rhs;
22076   cp_id_kind idk;
22077   bool decl_first;
22078
22079   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22080     {
22081       op = (token->type == CPP_PLUS_PLUS
22082             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22083       cp_lexer_consume_token (parser->lexer);
22084       lhs = cp_parser_cast_expression (parser, false, false, NULL);
22085       if (lhs != decl)
22086         return error_mark_node;
22087       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22088     }
22089
22090   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22091   if (lhs != decl)
22092     return error_mark_node;
22093
22094   token = cp_lexer_peek_token (parser->lexer);
22095   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22096     {
22097       op = (token->type == CPP_PLUS_PLUS
22098             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22099       cp_lexer_consume_token (parser->lexer);
22100       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22101     }
22102
22103   op = cp_parser_assignment_operator_opt (parser);
22104   if (op == ERROR_MARK)
22105     return error_mark_node;
22106
22107   if (op != NOP_EXPR)
22108     {
22109       rhs = cp_parser_assignment_expression (parser, false, NULL);
22110       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22111       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22112     }
22113
22114   lhs = cp_parser_binary_expression (parser, false, false,
22115                                      PREC_ADDITIVE_EXPRESSION, NULL);
22116   token = cp_lexer_peek_token (parser->lexer);
22117   decl_first = lhs == decl;
22118   if (decl_first)
22119     lhs = NULL_TREE;
22120   if (token->type != CPP_PLUS
22121       && token->type != CPP_MINUS)
22122     return error_mark_node;
22123
22124   do
22125     {
22126       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22127       cp_lexer_consume_token (parser->lexer);
22128       rhs = cp_parser_binary_expression (parser, false, false,
22129                                          PREC_ADDITIVE_EXPRESSION, NULL);
22130       token = cp_lexer_peek_token (parser->lexer);
22131       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22132         {
22133           if (lhs == NULL_TREE)
22134             {
22135               if (op == PLUS_EXPR)
22136                 lhs = rhs;
22137               else
22138                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22139             }
22140           else
22141             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22142                                      NULL, tf_warning_or_error);
22143         }
22144     }
22145   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22146
22147   if (!decl_first)
22148     {
22149       if (rhs != decl || op == MINUS_EXPR)
22150         return error_mark_node;
22151       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22152     }
22153   else
22154     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22155
22156   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22157 }
22158
22159 /* Parse the restricted form of the for statement allowed by OpenMP.  */
22160
22161 static tree
22162 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22163 {
22164   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22165   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22166   tree this_pre_body, cl;
22167   location_t loc_first;
22168   bool collapse_err = false;
22169   int i, collapse = 1, nbraces = 0;
22170
22171   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22172     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22173       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22174
22175   gcc_assert (collapse >= 1);
22176
22177   declv = make_tree_vec (collapse);
22178   initv = make_tree_vec (collapse);
22179   condv = make_tree_vec (collapse);
22180   incrv = make_tree_vec (collapse);
22181
22182   loc_first = cp_lexer_peek_token (parser->lexer)->location;
22183
22184   for (i = 0; i < collapse; i++)
22185     {
22186       int bracecount = 0;
22187       bool add_private_clause = false;
22188       location_t loc;
22189
22190       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22191         {
22192           cp_parser_error (parser, "for statement expected");
22193           return NULL;
22194         }
22195       loc = cp_lexer_consume_token (parser->lexer)->location;
22196
22197       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22198         return NULL;
22199
22200       init = decl = real_decl = NULL;
22201       this_pre_body = push_stmt_list ();
22202       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22203         {
22204           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22205
22206              init-expr:
22207                        var = lb
22208                        integer-type var = lb
22209                        random-access-iterator-type var = lb
22210                        pointer-type var = lb
22211           */
22212           cp_decl_specifier_seq type_specifiers;
22213
22214           /* First, try to parse as an initialized declaration.  See
22215              cp_parser_condition, from whence the bulk of this is copied.  */
22216
22217           cp_parser_parse_tentatively (parser);
22218           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22219                                         /*is_trailing_return=*/false,
22220                                         &type_specifiers);
22221           if (cp_parser_parse_definitely (parser))
22222             {
22223               /* If parsing a type specifier seq succeeded, then this
22224                  MUST be a initialized declaration.  */
22225               tree asm_specification, attributes;
22226               cp_declarator *declarator;
22227
22228               declarator = cp_parser_declarator (parser,
22229                                                  CP_PARSER_DECLARATOR_NAMED,
22230                                                  /*ctor_dtor_or_conv_p=*/NULL,
22231                                                  /*parenthesized_p=*/NULL,
22232                                                  /*member_p=*/false);
22233               attributes = cp_parser_attributes_opt (parser);
22234               asm_specification = cp_parser_asm_specification_opt (parser);
22235
22236               if (declarator == cp_error_declarator) 
22237                 cp_parser_skip_to_end_of_statement (parser);
22238
22239               else 
22240                 {
22241                   tree pushed_scope, auto_node;
22242
22243                   decl = start_decl (declarator, &type_specifiers,
22244                                      SD_INITIALIZED, attributes,
22245                                      /*prefix_attributes=*/NULL_TREE,
22246                                      &pushed_scope);
22247
22248                   auto_node = type_uses_auto (TREE_TYPE (decl));
22249                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22250                     {
22251                       if (cp_lexer_next_token_is (parser->lexer, 
22252                                                   CPP_OPEN_PAREN))
22253                         error ("parenthesized initialization is not allowed in "
22254                                "OpenMP %<for%> loop");
22255                       else
22256                         /* Trigger an error.  */
22257                         cp_parser_require (parser, CPP_EQ, "%<=%>");
22258
22259                       init = error_mark_node;
22260                       cp_parser_skip_to_end_of_statement (parser);
22261                     }
22262                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
22263                            || type_dependent_expression_p (decl)
22264                            || auto_node)
22265                     {
22266                       bool is_direct_init, is_non_constant_init;
22267
22268                       init = cp_parser_initializer (parser,
22269                                                     &is_direct_init,
22270                                                     &is_non_constant_init);
22271
22272                       if (auto_node && describable_type (init))
22273                         {
22274                           TREE_TYPE (decl)
22275                             = do_auto_deduction (TREE_TYPE (decl), init,
22276                                                  auto_node);
22277
22278                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
22279                               && !type_dependent_expression_p (decl))
22280                             goto non_class;
22281                         }
22282                       
22283                       cp_finish_decl (decl, init, !is_non_constant_init,
22284                                       asm_specification,
22285                                       LOOKUP_ONLYCONVERTING);
22286                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
22287                         {
22288                           for_block
22289                             = tree_cons (NULL, this_pre_body, for_block);
22290                           init = NULL_TREE;
22291                         }
22292                       else
22293                         init = pop_stmt_list (this_pre_body);
22294                       this_pre_body = NULL_TREE;
22295                     }
22296                   else
22297                     {
22298                       /* Consume '='.  */
22299                       cp_lexer_consume_token (parser->lexer);
22300                       init = cp_parser_assignment_expression (parser, false, NULL);
22301
22302                     non_class:
22303                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22304                         init = error_mark_node;
22305                       else
22306                         cp_finish_decl (decl, NULL_TREE,
22307                                         /*init_const_expr_p=*/false,
22308                                         asm_specification,
22309                                         LOOKUP_ONLYCONVERTING);
22310                     }
22311
22312                   if (pushed_scope)
22313                     pop_scope (pushed_scope);
22314                 }
22315             }
22316           else 
22317             {
22318               cp_id_kind idk;
22319               /* If parsing a type specifier sequence failed, then
22320                  this MUST be a simple expression.  */
22321               cp_parser_parse_tentatively (parser);
22322               decl = cp_parser_primary_expression (parser, false, false,
22323                                                    false, &idk);
22324               if (!cp_parser_error_occurred (parser)
22325                   && decl
22326                   && DECL_P (decl)
22327                   && CLASS_TYPE_P (TREE_TYPE (decl)))
22328                 {
22329                   tree rhs;
22330
22331                   cp_parser_parse_definitely (parser);
22332                   cp_parser_require (parser, CPP_EQ, "%<=%>");
22333                   rhs = cp_parser_assignment_expression (parser, false, NULL);
22334                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22335                                                          rhs,
22336                                                          tf_warning_or_error));
22337                   add_private_clause = true;
22338                 }
22339               else
22340                 {
22341                   decl = NULL;
22342                   cp_parser_abort_tentative_parse (parser);
22343                   init = cp_parser_expression (parser, false, NULL);
22344                   if (init)
22345                     {
22346                       if (TREE_CODE (init) == MODIFY_EXPR
22347                           || TREE_CODE (init) == MODOP_EXPR)
22348                         real_decl = TREE_OPERAND (init, 0);
22349                     }
22350                 }
22351             }
22352         }
22353       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22354       if (this_pre_body)
22355         {
22356           this_pre_body = pop_stmt_list (this_pre_body);
22357           if (pre_body)
22358             {
22359               tree t = pre_body;
22360               pre_body = push_stmt_list ();
22361               add_stmt (t);
22362               add_stmt (this_pre_body);
22363               pre_body = pop_stmt_list (pre_body);
22364             }
22365           else
22366             pre_body = this_pre_body;
22367         }
22368
22369       if (decl)
22370         real_decl = decl;
22371       if (par_clauses != NULL && real_decl != NULL_TREE)
22372         {
22373           tree *c;
22374           for (c = par_clauses; *c ; )
22375             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22376                 && OMP_CLAUSE_DECL (*c) == real_decl)
22377               {
22378                 error_at (loc, "iteration variable %qD"
22379                           " should not be firstprivate", real_decl);
22380                 *c = OMP_CLAUSE_CHAIN (*c);
22381               }
22382             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22383                      && OMP_CLAUSE_DECL (*c) == real_decl)
22384               {
22385                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22386                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
22387                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22388                 OMP_CLAUSE_DECL (l) = real_decl;
22389                 OMP_CLAUSE_CHAIN (l) = clauses;
22390                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22391                 clauses = l;
22392                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22393                 CP_OMP_CLAUSE_INFO (*c) = NULL;
22394                 add_private_clause = false;
22395               }
22396             else
22397               {
22398                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22399                     && OMP_CLAUSE_DECL (*c) == real_decl)
22400                   add_private_clause = false;
22401                 c = &OMP_CLAUSE_CHAIN (*c);
22402               }
22403         }
22404
22405       if (add_private_clause)
22406         {
22407           tree c;
22408           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22409             {
22410               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22411                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22412                   && OMP_CLAUSE_DECL (c) == decl)
22413                 break;
22414               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22415                        && OMP_CLAUSE_DECL (c) == decl)
22416                 error_at (loc, "iteration variable %qD "
22417                           "should not be firstprivate",
22418                           decl);
22419               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22420                        && OMP_CLAUSE_DECL (c) == decl)
22421                 error_at (loc, "iteration variable %qD should not be reduction",
22422                           decl);
22423             }
22424           if (c == NULL)
22425             {
22426               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22427               OMP_CLAUSE_DECL (c) = decl;
22428               c = finish_omp_clauses (c);
22429               if (c)
22430                 {
22431                   OMP_CLAUSE_CHAIN (c) = clauses;
22432                   clauses = c;
22433                 }
22434             }
22435         }
22436
22437       cond = NULL;
22438       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22439         cond = cp_parser_omp_for_cond (parser, decl);
22440       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22441
22442       incr = NULL;
22443       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22444         {
22445           /* If decl is an iterator, preserve the operator on decl
22446              until finish_omp_for.  */
22447           if (decl
22448               && (type_dependent_expression_p (decl)
22449                   || CLASS_TYPE_P (TREE_TYPE (decl))))
22450             incr = cp_parser_omp_for_incr (parser, decl);
22451           else
22452             incr = cp_parser_expression (parser, false, NULL);
22453         }
22454
22455       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22456         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22457                                                /*or_comma=*/false,
22458                                                /*consume_paren=*/true);
22459
22460       TREE_VEC_ELT (declv, i) = decl;
22461       TREE_VEC_ELT (initv, i) = init;
22462       TREE_VEC_ELT (condv, i) = cond;
22463       TREE_VEC_ELT (incrv, i) = incr;
22464
22465       if (i == collapse - 1)
22466         break;
22467
22468       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22469          in between the collapsed for loops to be still considered perfectly
22470          nested.  Hopefully the final version clarifies this.
22471          For now handle (multiple) {'s and empty statements.  */
22472       cp_parser_parse_tentatively (parser);
22473       do
22474         {
22475           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22476             break;
22477           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22478             {
22479               cp_lexer_consume_token (parser->lexer);
22480               bracecount++;
22481             }
22482           else if (bracecount
22483                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22484             cp_lexer_consume_token (parser->lexer);
22485           else
22486             {
22487               loc = cp_lexer_peek_token (parser->lexer)->location;
22488               error_at (loc, "not enough collapsed for loops");
22489               collapse_err = true;
22490               cp_parser_abort_tentative_parse (parser);
22491               declv = NULL_TREE;
22492               break;
22493             }
22494         }
22495       while (1);
22496
22497       if (declv)
22498         {
22499           cp_parser_parse_definitely (parser);
22500           nbraces += bracecount;
22501         }
22502     }
22503
22504   /* Note that we saved the original contents of this flag when we entered
22505      the structured block, and so we don't need to re-save it here.  */
22506   parser->in_statement = IN_OMP_FOR;
22507
22508   /* Note that the grammar doesn't call for a structured block here,
22509      though the loop as a whole is a structured block.  */
22510   body = push_stmt_list ();
22511   cp_parser_statement (parser, NULL_TREE, false, NULL);
22512   body = pop_stmt_list (body);
22513
22514   if (declv == NULL_TREE)
22515     ret = NULL_TREE;
22516   else
22517     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22518                           pre_body, clauses);
22519
22520   while (nbraces)
22521     {
22522       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22523         {
22524           cp_lexer_consume_token (parser->lexer);
22525           nbraces--;
22526         }
22527       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22528         cp_lexer_consume_token (parser->lexer);
22529       else
22530         {
22531           if (!collapse_err)
22532             {
22533               error_at (cp_lexer_peek_token (parser->lexer)->location,
22534                         "collapsed loops not perfectly nested");
22535             }
22536           collapse_err = true;
22537           cp_parser_statement_seq_opt (parser, NULL);
22538           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22539             break;
22540         }
22541     }
22542
22543   while (for_block)
22544     {
22545       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22546       for_block = TREE_CHAIN (for_block);
22547     }
22548
22549   return ret;
22550 }
22551
22552 /* OpenMP 2.5:
22553    #pragma omp for for-clause[optseq] new-line
22554      for-loop  */
22555
22556 #define OMP_FOR_CLAUSE_MASK                             \
22557         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22558         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22559         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22560         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22561         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
22562         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
22563         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
22564         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22565
22566 static tree
22567 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22568 {
22569   tree clauses, sb, ret;
22570   unsigned int save;
22571
22572   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22573                                        "#pragma omp for", pragma_tok);
22574
22575   sb = begin_omp_structured_block ();
22576   save = cp_parser_begin_omp_structured_block (parser);
22577
22578   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22579
22580   cp_parser_end_omp_structured_block (parser, save);
22581   add_stmt (finish_omp_structured_block (sb));
22582
22583   return ret;
22584 }
22585
22586 /* OpenMP 2.5:
22587    # pragma omp master new-line
22588      structured-block  */
22589
22590 static tree
22591 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22592 {
22593   cp_parser_require_pragma_eol (parser, pragma_tok);
22594   return c_finish_omp_master (input_location,
22595                               cp_parser_omp_structured_block (parser));
22596 }
22597
22598 /* OpenMP 2.5:
22599    # pragma omp ordered new-line
22600      structured-block  */
22601
22602 static tree
22603 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22604 {
22605   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22606   cp_parser_require_pragma_eol (parser, pragma_tok);
22607   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22608 }
22609
22610 /* OpenMP 2.5:
22611
22612    section-scope:
22613      { section-sequence }
22614
22615    section-sequence:
22616      section-directive[opt] structured-block
22617      section-sequence section-directive structured-block  */
22618
22619 static tree
22620 cp_parser_omp_sections_scope (cp_parser *parser)
22621 {
22622   tree stmt, substmt;
22623   bool error_suppress = false;
22624   cp_token *tok;
22625
22626   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22627     return NULL_TREE;
22628
22629   stmt = push_stmt_list ();
22630
22631   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22632     {
22633       unsigned save;
22634
22635       substmt = begin_omp_structured_block ();
22636       save = cp_parser_begin_omp_structured_block (parser);
22637
22638       while (1)
22639         {
22640           cp_parser_statement (parser, NULL_TREE, false, NULL);
22641
22642           tok = cp_lexer_peek_token (parser->lexer);
22643           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22644             break;
22645           if (tok->type == CPP_CLOSE_BRACE)
22646             break;
22647           if (tok->type == CPP_EOF)
22648             break;
22649         }
22650
22651       cp_parser_end_omp_structured_block (parser, save);
22652       substmt = finish_omp_structured_block (substmt);
22653       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22654       add_stmt (substmt);
22655     }
22656
22657   while (1)
22658     {
22659       tok = cp_lexer_peek_token (parser->lexer);
22660       if (tok->type == CPP_CLOSE_BRACE)
22661         break;
22662       if (tok->type == CPP_EOF)
22663         break;
22664
22665       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22666         {
22667           cp_lexer_consume_token (parser->lexer);
22668           cp_parser_require_pragma_eol (parser, tok);
22669           error_suppress = false;
22670         }
22671       else if (!error_suppress)
22672         {
22673           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22674           error_suppress = true;
22675         }
22676
22677       substmt = cp_parser_omp_structured_block (parser);
22678       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22679       add_stmt (substmt);
22680     }
22681   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22682
22683   substmt = pop_stmt_list (stmt);
22684
22685   stmt = make_node (OMP_SECTIONS);
22686   TREE_TYPE (stmt) = void_type_node;
22687   OMP_SECTIONS_BODY (stmt) = substmt;
22688
22689   add_stmt (stmt);
22690   return stmt;
22691 }
22692
22693 /* OpenMP 2.5:
22694    # pragma omp sections sections-clause[optseq] newline
22695      sections-scope  */
22696
22697 #define OMP_SECTIONS_CLAUSE_MASK                        \
22698         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22699         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22700         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22701         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22702         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22703
22704 static tree
22705 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22706 {
22707   tree clauses, ret;
22708
22709   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22710                                        "#pragma omp sections", pragma_tok);
22711
22712   ret = cp_parser_omp_sections_scope (parser);
22713   if (ret)
22714     OMP_SECTIONS_CLAUSES (ret) = clauses;
22715
22716   return ret;
22717 }
22718
22719 /* OpenMP 2.5:
22720    # pragma parallel parallel-clause new-line
22721    # pragma parallel for parallel-for-clause new-line
22722    # pragma parallel sections parallel-sections-clause new-line  */
22723
22724 #define OMP_PARALLEL_CLAUSE_MASK                        \
22725         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22726         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22727         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22728         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22729         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
22730         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
22731         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22732         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22733
22734 static tree
22735 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22736 {
22737   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22738   const char *p_name = "#pragma omp parallel";
22739   tree stmt, clauses, par_clause, ws_clause, block;
22740   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22741   unsigned int save;
22742   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22743
22744   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22745     {
22746       cp_lexer_consume_token (parser->lexer);
22747       p_kind = PRAGMA_OMP_PARALLEL_FOR;
22748       p_name = "#pragma omp parallel for";
22749       mask |= OMP_FOR_CLAUSE_MASK;
22750       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22751     }
22752   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22753     {
22754       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22755       const char *p = IDENTIFIER_POINTER (id);
22756       if (strcmp (p, "sections") == 0)
22757         {
22758           cp_lexer_consume_token (parser->lexer);
22759           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22760           p_name = "#pragma omp parallel sections";
22761           mask |= OMP_SECTIONS_CLAUSE_MASK;
22762           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22763         }
22764     }
22765
22766   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22767   block = begin_omp_parallel ();
22768   save = cp_parser_begin_omp_structured_block (parser);
22769
22770   switch (p_kind)
22771     {
22772     case PRAGMA_OMP_PARALLEL:
22773       cp_parser_statement (parser, NULL_TREE, false, NULL);
22774       par_clause = clauses;
22775       break;
22776
22777     case PRAGMA_OMP_PARALLEL_FOR:
22778       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22779       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22780       break;
22781
22782     case PRAGMA_OMP_PARALLEL_SECTIONS:
22783       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22784       stmt = cp_parser_omp_sections_scope (parser);
22785       if (stmt)
22786         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22787       break;
22788
22789     default:
22790       gcc_unreachable ();
22791     }
22792
22793   cp_parser_end_omp_structured_block (parser, save);
22794   stmt = finish_omp_parallel (par_clause, block);
22795   if (p_kind != PRAGMA_OMP_PARALLEL)
22796     OMP_PARALLEL_COMBINED (stmt) = 1;
22797   return stmt;
22798 }
22799
22800 /* OpenMP 2.5:
22801    # pragma omp single single-clause[optseq] new-line
22802      structured-block  */
22803
22804 #define OMP_SINGLE_CLAUSE_MASK                          \
22805         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22806         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22807         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
22808         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22809
22810 static tree
22811 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22812 {
22813   tree stmt = make_node (OMP_SINGLE);
22814   TREE_TYPE (stmt) = void_type_node;
22815
22816   OMP_SINGLE_CLAUSES (stmt)
22817     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22818                                  "#pragma omp single", pragma_tok);
22819   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22820
22821   return add_stmt (stmt);
22822 }
22823
22824 /* OpenMP 3.0:
22825    # pragma omp task task-clause[optseq] new-line
22826      structured-block  */
22827
22828 #define OMP_TASK_CLAUSE_MASK                            \
22829         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22830         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
22831         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22832         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22833         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22834         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22835
22836 static tree
22837 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22838 {
22839   tree clauses, block;
22840   unsigned int save;
22841
22842   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22843                                        "#pragma omp task", pragma_tok);
22844   block = begin_omp_task ();
22845   save = cp_parser_begin_omp_structured_block (parser);
22846   cp_parser_statement (parser, NULL_TREE, false, NULL);
22847   cp_parser_end_omp_structured_block (parser, save);
22848   return finish_omp_task (clauses, block);
22849 }
22850
22851 /* OpenMP 3.0:
22852    # pragma omp taskwait new-line  */
22853
22854 static void
22855 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22856 {
22857   cp_parser_require_pragma_eol (parser, pragma_tok);
22858   finish_omp_taskwait ();
22859 }
22860
22861 /* OpenMP 2.5:
22862    # pragma omp threadprivate (variable-list) */
22863
22864 static void
22865 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22866 {
22867   tree vars;
22868
22869   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22870   cp_parser_require_pragma_eol (parser, pragma_tok);
22871
22872   finish_omp_threadprivate (vars);
22873 }
22874
22875 /* Main entry point to OpenMP statement pragmas.  */
22876
22877 static void
22878 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22879 {
22880   tree stmt;
22881
22882   switch (pragma_tok->pragma_kind)
22883     {
22884     case PRAGMA_OMP_ATOMIC:
22885       cp_parser_omp_atomic (parser, pragma_tok);
22886       return;
22887     case PRAGMA_OMP_CRITICAL:
22888       stmt = cp_parser_omp_critical (parser, pragma_tok);
22889       break;
22890     case PRAGMA_OMP_FOR:
22891       stmt = cp_parser_omp_for (parser, pragma_tok);
22892       break;
22893     case PRAGMA_OMP_MASTER:
22894       stmt = cp_parser_omp_master (parser, pragma_tok);
22895       break;
22896     case PRAGMA_OMP_ORDERED:
22897       stmt = cp_parser_omp_ordered (parser, pragma_tok);
22898       break;
22899     case PRAGMA_OMP_PARALLEL:
22900       stmt = cp_parser_omp_parallel (parser, pragma_tok);
22901       break;
22902     case PRAGMA_OMP_SECTIONS:
22903       stmt = cp_parser_omp_sections (parser, pragma_tok);
22904       break;
22905     case PRAGMA_OMP_SINGLE:
22906       stmt = cp_parser_omp_single (parser, pragma_tok);
22907       break;
22908     case PRAGMA_OMP_TASK:
22909       stmt = cp_parser_omp_task (parser, pragma_tok);
22910       break;
22911     default:
22912       gcc_unreachable ();
22913     }
22914
22915   if (stmt)
22916     SET_EXPR_LOCATION (stmt, pragma_tok->location);
22917 }
22918 \f
22919 /* The parser.  */
22920
22921 static GTY (()) cp_parser *the_parser;
22922
22923 \f
22924 /* Special handling for the first token or line in the file.  The first
22925    thing in the file might be #pragma GCC pch_preprocess, which loads a
22926    PCH file, which is a GC collection point.  So we need to handle this
22927    first pragma without benefit of an existing lexer structure.
22928
22929    Always returns one token to the caller in *FIRST_TOKEN.  This is
22930    either the true first token of the file, or the first token after
22931    the initial pragma.  */
22932
22933 static void
22934 cp_parser_initial_pragma (cp_token *first_token)
22935 {
22936   tree name = NULL;
22937
22938   cp_lexer_get_preprocessor_token (NULL, first_token);
22939   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22940     return;
22941
22942   cp_lexer_get_preprocessor_token (NULL, first_token);
22943   if (first_token->type == CPP_STRING)
22944     {
22945       name = first_token->u.value;
22946
22947       cp_lexer_get_preprocessor_token (NULL, first_token);
22948       if (first_token->type != CPP_PRAGMA_EOL)
22949         error_at (first_token->location,
22950                   "junk at end of %<#pragma GCC pch_preprocess%>");
22951     }
22952   else
22953     error_at (first_token->location, "expected string literal");
22954
22955   /* Skip to the end of the pragma.  */
22956   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22957     cp_lexer_get_preprocessor_token (NULL, first_token);
22958
22959   /* Now actually load the PCH file.  */
22960   if (name)
22961     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22962
22963   /* Read one more token to return to our caller.  We have to do this
22964      after reading the PCH file in, since its pointers have to be
22965      live.  */
22966   cp_lexer_get_preprocessor_token (NULL, first_token);
22967 }
22968
22969 /* Normal parsing of a pragma token.  Here we can (and must) use the
22970    regular lexer.  */
22971
22972 static bool
22973 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22974 {
22975   cp_token *pragma_tok;
22976   unsigned int id;
22977
22978   pragma_tok = cp_lexer_consume_token (parser->lexer);
22979   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22980   parser->lexer->in_pragma = true;
22981
22982   id = pragma_tok->pragma_kind;
22983   switch (id)
22984     {
22985     case PRAGMA_GCC_PCH_PREPROCESS:
22986       error_at (pragma_tok->location,
22987                 "%<#pragma GCC pch_preprocess%> must be first");
22988       break;
22989
22990     case PRAGMA_OMP_BARRIER:
22991       switch (context)
22992         {
22993         case pragma_compound:
22994           cp_parser_omp_barrier (parser, pragma_tok);
22995           return false;
22996         case pragma_stmt:
22997           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22998                     "used in compound statements");
22999           break;
23000         default:
23001           goto bad_stmt;
23002         }
23003       break;
23004
23005     case PRAGMA_OMP_FLUSH:
23006       switch (context)
23007         {
23008         case pragma_compound:
23009           cp_parser_omp_flush (parser, pragma_tok);
23010           return false;
23011         case pragma_stmt:
23012           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23013                     "used in compound statements");
23014           break;
23015         default:
23016           goto bad_stmt;
23017         }
23018       break;
23019
23020     case PRAGMA_OMP_TASKWAIT:
23021       switch (context)
23022         {
23023         case pragma_compound:
23024           cp_parser_omp_taskwait (parser, pragma_tok);
23025           return false;
23026         case pragma_stmt:
23027           error_at (pragma_tok->location,
23028                     "%<#pragma omp taskwait%> may only be "
23029                     "used in compound statements");
23030           break;
23031         default:
23032           goto bad_stmt;
23033         }
23034       break;
23035
23036     case PRAGMA_OMP_THREADPRIVATE:
23037       cp_parser_omp_threadprivate (parser, pragma_tok);
23038       return false;
23039
23040     case PRAGMA_OMP_ATOMIC:
23041     case PRAGMA_OMP_CRITICAL:
23042     case PRAGMA_OMP_FOR:
23043     case PRAGMA_OMP_MASTER:
23044     case PRAGMA_OMP_ORDERED:
23045     case PRAGMA_OMP_PARALLEL:
23046     case PRAGMA_OMP_SECTIONS:
23047     case PRAGMA_OMP_SINGLE:
23048     case PRAGMA_OMP_TASK:
23049       if (context == pragma_external)
23050         goto bad_stmt;
23051       cp_parser_omp_construct (parser, pragma_tok);
23052       return true;
23053
23054     case PRAGMA_OMP_SECTION:
23055       error_at (pragma_tok->location, 
23056                 "%<#pragma omp section%> may only be used in "
23057                 "%<#pragma omp sections%> construct");
23058       break;
23059
23060     default:
23061       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23062       c_invoke_pragma_handler (id);
23063       break;
23064
23065     bad_stmt:
23066       cp_parser_error (parser, "expected declaration specifiers");
23067       break;
23068     }
23069
23070   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23071   return false;
23072 }
23073
23074 /* The interface the pragma parsers have to the lexer.  */
23075
23076 enum cpp_ttype
23077 pragma_lex (tree *value)
23078 {
23079   cp_token *tok;
23080   enum cpp_ttype ret;
23081
23082   tok = cp_lexer_peek_token (the_parser->lexer);
23083
23084   ret = tok->type;
23085   *value = tok->u.value;
23086
23087   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23088     ret = CPP_EOF;
23089   else if (ret == CPP_STRING)
23090     *value = cp_parser_string_literal (the_parser, false, false);
23091   else
23092     {
23093       cp_lexer_consume_token (the_parser->lexer);
23094       if (ret == CPP_KEYWORD)
23095         ret = CPP_NAME;
23096     }
23097
23098   return ret;
23099 }
23100
23101 \f
23102 /* External interface.  */
23103
23104 /* Parse one entire translation unit.  */
23105
23106 void
23107 c_parse_file (void)
23108 {
23109   static bool already_called = false;
23110
23111   if (already_called)
23112     {
23113       sorry ("inter-module optimizations not implemented for C++");
23114       return;
23115     }
23116   already_called = true;
23117
23118   the_parser = cp_parser_new ();
23119   push_deferring_access_checks (flag_access_control
23120                                 ? dk_no_deferred : dk_no_check);
23121   cp_parser_translation_unit (the_parser);
23122   the_parser = NULL;
23123 }
23124
23125 #include "gt-cp-parser.h"