OSDN Git Service

PR c++/43648
[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   declarator->id_loc = UNKNOWN_LOCATION;
894
895   return declarator;
896 }
897
898 /* Make a declarator for a generalized identifier.  If
899    QUALIFYING_SCOPE is non-NULL, the identifier is
900    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
901    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
902    is, if any.   */
903
904 static cp_declarator *
905 make_id_declarator (tree qualifying_scope, tree unqualified_name,
906                     special_function_kind sfk)
907 {
908   cp_declarator *declarator;
909
910   /* It is valid to write:
911
912        class C { void f(); };
913        typedef C D;
914        void D::f();
915
916      The standard is not clear about whether `typedef const C D' is
917      legal; as of 2002-09-15 the committee is considering that
918      question.  EDG 3.0 allows that syntax.  Therefore, we do as
919      well.  */
920   if (qualifying_scope && TYPE_P (qualifying_scope))
921     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
922
923   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
924               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
925               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
926
927   declarator = make_declarator (cdk_id);
928   declarator->u.id.qualifying_scope = qualifying_scope;
929   declarator->u.id.unqualified_name = unqualified_name;
930   declarator->u.id.sfk = sfk;
931   
932   return declarator;
933 }
934
935 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
936    of modifiers such as const or volatile to apply to the pointer
937    type, represented as identifiers.  */
938
939 cp_declarator *
940 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
941 {
942   cp_declarator *declarator;
943
944   declarator = make_declarator (cdk_pointer);
945   declarator->declarator = target;
946   declarator->u.pointer.qualifiers = cv_qualifiers;
947   declarator->u.pointer.class_type = NULL_TREE;
948   if (target)
949     {
950       declarator->parameter_pack_p = target->parameter_pack_p;
951       target->parameter_pack_p = false;
952     }
953   else
954     declarator->parameter_pack_p = false;
955
956   return declarator;
957 }
958
959 /* Like make_pointer_declarator -- but for references.  */
960
961 cp_declarator *
962 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
963                            bool rvalue_ref)
964 {
965   cp_declarator *declarator;
966
967   declarator = make_declarator (cdk_reference);
968   declarator->declarator = target;
969   declarator->u.reference.qualifiers = cv_qualifiers;
970   declarator->u.reference.rvalue_ref = rvalue_ref;
971   if (target)
972     {
973       declarator->parameter_pack_p = target->parameter_pack_p;
974       target->parameter_pack_p = false;
975     }
976   else
977     declarator->parameter_pack_p = false;
978
979   return declarator;
980 }
981
982 /* Like make_pointer_declarator -- but for a pointer to a non-static
983    member of CLASS_TYPE.  */
984
985 cp_declarator *
986 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
987                         cp_declarator *pointee)
988 {
989   cp_declarator *declarator;
990
991   declarator = make_declarator (cdk_ptrmem);
992   declarator->declarator = pointee;
993   declarator->u.pointer.qualifiers = cv_qualifiers;
994   declarator->u.pointer.class_type = class_type;
995
996   if (pointee)
997     {
998       declarator->parameter_pack_p = pointee->parameter_pack_p;
999       pointee->parameter_pack_p = false;
1000     }
1001   else
1002     declarator->parameter_pack_p = false;
1003
1004   return declarator;
1005 }
1006
1007 /* Make a declarator for the function given by TARGET, with the
1008    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1009    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1010    indicates what exceptions can be thrown.  */
1011
1012 cp_declarator *
1013 make_call_declarator (cp_declarator *target,
1014                       tree parms,
1015                       cp_cv_quals cv_qualifiers,
1016                       tree exception_specification,
1017                       tree late_return_type)
1018 {
1019   cp_declarator *declarator;
1020
1021   declarator = make_declarator (cdk_function);
1022   declarator->declarator = target;
1023   declarator->u.function.parameters = parms;
1024   declarator->u.function.qualifiers = cv_qualifiers;
1025   declarator->u.function.exception_specification = exception_specification;
1026   declarator->u.function.late_return_type = late_return_type;
1027   if (target)
1028     {
1029       declarator->parameter_pack_p = target->parameter_pack_p;
1030       target->parameter_pack_p = false;
1031     }
1032   else
1033     declarator->parameter_pack_p = false;
1034
1035   return declarator;
1036 }
1037
1038 /* Make a declarator for an array of BOUNDS elements, each of which is
1039    defined by ELEMENT.  */
1040
1041 cp_declarator *
1042 make_array_declarator (cp_declarator *element, tree bounds)
1043 {
1044   cp_declarator *declarator;
1045
1046   declarator = make_declarator (cdk_array);
1047   declarator->declarator = element;
1048   declarator->u.array.bounds = bounds;
1049   if (element)
1050     {
1051       declarator->parameter_pack_p = element->parameter_pack_p;
1052       element->parameter_pack_p = false;
1053     }
1054   else
1055     declarator->parameter_pack_p = false;
1056
1057   return declarator;
1058 }
1059
1060 /* Determine whether the declarator we've seen so far can be a
1061    parameter pack, when followed by an ellipsis.  */
1062 static bool 
1063 declarator_can_be_parameter_pack (cp_declarator *declarator)
1064 {
1065   /* Search for a declarator name, or any other declarator that goes
1066      after the point where the ellipsis could appear in a parameter
1067      pack. If we find any of these, then this declarator can not be
1068      made into a parameter pack.  */
1069   bool found = false;
1070   while (declarator && !found)
1071     {
1072       switch ((int)declarator->kind)
1073         {
1074         case cdk_id:
1075         case cdk_array:
1076           found = true;
1077           break;
1078
1079         case cdk_error:
1080           return true;
1081
1082         default:
1083           declarator = declarator->declarator;
1084           break;
1085         }
1086     }
1087
1088   return !found;
1089 }
1090
1091 cp_parameter_declarator *no_parameters;
1092
1093 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1094    DECLARATOR and DEFAULT_ARGUMENT.  */
1095
1096 cp_parameter_declarator *
1097 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1098                            cp_declarator *declarator,
1099                            tree default_argument)
1100 {
1101   cp_parameter_declarator *parameter;
1102
1103   parameter = ((cp_parameter_declarator *)
1104                alloc_declarator (sizeof (cp_parameter_declarator)));
1105   parameter->next = NULL;
1106   if (decl_specifiers)
1107     parameter->decl_specifiers = *decl_specifiers;
1108   else
1109     clear_decl_specs (&parameter->decl_specifiers);
1110   parameter->declarator = declarator;
1111   parameter->default_argument = default_argument;
1112   parameter->ellipsis_p = false;
1113
1114   return parameter;
1115 }
1116
1117 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1118
1119 static bool
1120 function_declarator_p (const cp_declarator *declarator)
1121 {
1122   while (declarator)
1123     {
1124       if (declarator->kind == cdk_function
1125           && declarator->declarator->kind == cdk_id)
1126         return true;
1127       if (declarator->kind == cdk_id
1128           || declarator->kind == cdk_error)
1129         return false;
1130       declarator = declarator->declarator;
1131     }
1132   return false;
1133 }
1134  
1135 /* The parser.  */
1136
1137 /* Overview
1138    --------
1139
1140    A cp_parser parses the token stream as specified by the C++
1141    grammar.  Its job is purely parsing, not semantic analysis.  For
1142    example, the parser breaks the token stream into declarators,
1143    expressions, statements, and other similar syntactic constructs.
1144    It does not check that the types of the expressions on either side
1145    of an assignment-statement are compatible, or that a function is
1146    not declared with a parameter of type `void'.
1147
1148    The parser invokes routines elsewhere in the compiler to perform
1149    semantic analysis and to build up the abstract syntax tree for the
1150    code processed.
1151
1152    The parser (and the template instantiation code, which is, in a
1153    way, a close relative of parsing) are the only parts of the
1154    compiler that should be calling push_scope and pop_scope, or
1155    related functions.  The parser (and template instantiation code)
1156    keeps track of what scope is presently active; everything else
1157    should simply honor that.  (The code that generates static
1158    initializers may also need to set the scope, in order to check
1159    access control correctly when emitting the initializers.)
1160
1161    Methodology
1162    -----------
1163
1164    The parser is of the standard recursive-descent variety.  Upcoming
1165    tokens in the token stream are examined in order to determine which
1166    production to use when parsing a non-terminal.  Some C++ constructs
1167    require arbitrary look ahead to disambiguate.  For example, it is
1168    impossible, in the general case, to tell whether a statement is an
1169    expression or declaration without scanning the entire statement.
1170    Therefore, the parser is capable of "parsing tentatively."  When the
1171    parser is not sure what construct comes next, it enters this mode.
1172    Then, while we attempt to parse the construct, the parser queues up
1173    error messages, rather than issuing them immediately, and saves the
1174    tokens it consumes.  If the construct is parsed successfully, the
1175    parser "commits", i.e., it issues any queued error messages and
1176    the tokens that were being preserved are permanently discarded.
1177    If, however, the construct is not parsed successfully, the parser
1178    rolls back its state completely so that it can resume parsing using
1179    a different alternative.
1180
1181    Future Improvements
1182    -------------------
1183
1184    The performance of the parser could probably be improved substantially.
1185    We could often eliminate the need to parse tentatively by looking ahead
1186    a little bit.  In some places, this approach might not entirely eliminate
1187    the need to parse tentatively, but it might still speed up the average
1188    case.  */
1189
1190 /* Flags that are passed to some parsing functions.  These values can
1191    be bitwise-ored together.  */
1192
1193 enum
1194 {
1195   /* No flags.  */
1196   CP_PARSER_FLAGS_NONE = 0x0,
1197   /* The construct is optional.  If it is not present, then no error
1198      should be issued.  */
1199   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1200   /* When parsing a type-specifier, treat user-defined type-names
1201      as non-type identifiers.  */
1202   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1203   /* When parsing a type-specifier, do not try to parse a class-specifier
1204      or enum-specifier.  */
1205   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1206 };
1207
1208 /* This type is used for parameters and variables which hold
1209    combinations of the above flags.  */
1210 typedef int cp_parser_flags;
1211
1212 /* The different kinds of declarators we want to parse.  */
1213
1214 typedef enum cp_parser_declarator_kind
1215 {
1216   /* We want an abstract declarator.  */
1217   CP_PARSER_DECLARATOR_ABSTRACT,
1218   /* We want a named declarator.  */
1219   CP_PARSER_DECLARATOR_NAMED,
1220   /* We don't mind, but the name must be an unqualified-id.  */
1221   CP_PARSER_DECLARATOR_EITHER
1222 } cp_parser_declarator_kind;
1223
1224 /* The precedence values used to parse binary expressions.  The minimum value
1225    of PREC must be 1, because zero is reserved to quickly discriminate
1226    binary operators from other tokens.  */
1227
1228 enum cp_parser_prec
1229 {
1230   PREC_NOT_OPERATOR,
1231   PREC_LOGICAL_OR_EXPRESSION,
1232   PREC_LOGICAL_AND_EXPRESSION,
1233   PREC_INCLUSIVE_OR_EXPRESSION,
1234   PREC_EXCLUSIVE_OR_EXPRESSION,
1235   PREC_AND_EXPRESSION,
1236   PREC_EQUALITY_EXPRESSION,
1237   PREC_RELATIONAL_EXPRESSION,
1238   PREC_SHIFT_EXPRESSION,
1239   PREC_ADDITIVE_EXPRESSION,
1240   PREC_MULTIPLICATIVE_EXPRESSION,
1241   PREC_PM_EXPRESSION,
1242   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1243 };
1244
1245 /* A mapping from a token type to a corresponding tree node type, with a
1246    precedence value.  */
1247
1248 typedef struct cp_parser_binary_operations_map_node
1249 {
1250   /* The token type.  */
1251   enum cpp_ttype token_type;
1252   /* The corresponding tree code.  */
1253   enum tree_code tree_type;
1254   /* The precedence of this operator.  */
1255   enum cp_parser_prec prec;
1256 } cp_parser_binary_operations_map_node;
1257
1258 /* The status of a tentative parse.  */
1259
1260 typedef enum cp_parser_status_kind
1261 {
1262   /* No errors have occurred.  */
1263   CP_PARSER_STATUS_KIND_NO_ERROR,
1264   /* An error has occurred.  */
1265   CP_PARSER_STATUS_KIND_ERROR,
1266   /* We are committed to this tentative parse, whether or not an error
1267      has occurred.  */
1268   CP_PARSER_STATUS_KIND_COMMITTED
1269 } cp_parser_status_kind;
1270
1271 typedef struct cp_parser_expression_stack_entry
1272 {
1273   /* Left hand side of the binary operation we are currently
1274      parsing.  */
1275   tree lhs;
1276   /* Original tree code for left hand side, if it was a binary
1277      expression itself (used for -Wparentheses).  */
1278   enum tree_code lhs_type;
1279   /* Tree code for the binary operation we are parsing.  */
1280   enum tree_code tree_type;
1281   /* Precedence of the binary operation we are parsing.  */
1282   enum cp_parser_prec prec;
1283 } cp_parser_expression_stack_entry;
1284
1285 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1286    entries because precedence levels on the stack are monotonically
1287    increasing.  */
1288 typedef struct cp_parser_expression_stack_entry
1289   cp_parser_expression_stack[NUM_PREC_VALUES];
1290
1291 /* Context that is saved and restored when parsing tentatively.  */
1292 typedef struct GTY (()) cp_parser_context {
1293   /* If this is a tentative parsing context, the status of the
1294      tentative parse.  */
1295   enum cp_parser_status_kind status;
1296   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1297      that are looked up in this context must be looked up both in the
1298      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1299      the context of the containing expression.  */
1300   tree object_type;
1301
1302   /* The next parsing context in the stack.  */
1303   struct cp_parser_context *next;
1304 } cp_parser_context;
1305
1306 /* Prototypes.  */
1307
1308 /* Constructors and destructors.  */
1309
1310 static cp_parser_context *cp_parser_context_new
1311   (cp_parser_context *);
1312
1313 /* Class variables.  */
1314
1315 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1316
1317 /* The operator-precedence table used by cp_parser_binary_expression.
1318    Transformed into an associative array (binops_by_token) by
1319    cp_parser_new.  */
1320
1321 static const cp_parser_binary_operations_map_node binops[] = {
1322   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1323   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1324
1325   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1326   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1327   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1328
1329   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1330   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1331
1332   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1333   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1334
1335   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1336   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1337   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1338   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1339
1340   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1341   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1342
1343   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1344
1345   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1346
1347   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1348
1349   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1350
1351   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1352 };
1353
1354 /* The same as binops, but initialized by cp_parser_new so that
1355    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1356    for speed.  */
1357 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1358
1359 /* Constructors and destructors.  */
1360
1361 /* Construct a new context.  The context below this one on the stack
1362    is given by NEXT.  */
1363
1364 static cp_parser_context *
1365 cp_parser_context_new (cp_parser_context* next)
1366 {
1367   cp_parser_context *context;
1368
1369   /* Allocate the storage.  */
1370   if (cp_parser_context_free_list != NULL)
1371     {
1372       /* Pull the first entry from the free list.  */
1373       context = cp_parser_context_free_list;
1374       cp_parser_context_free_list = context->next;
1375       memset (context, 0, sizeof (*context));
1376     }
1377   else
1378     context = GGC_CNEW (cp_parser_context);
1379
1380   /* No errors have occurred yet in this context.  */
1381   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1382   /* If this is not the bottommost context, copy information that we
1383      need from the previous context.  */
1384   if (next)
1385     {
1386       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1387          expression, then we are parsing one in this context, too.  */
1388       context->object_type = next->object_type;
1389       /* Thread the stack.  */
1390       context->next = next;
1391     }
1392
1393   return context;
1394 }
1395
1396 /* The cp_parser structure represents the C++ parser.  */
1397
1398 typedef struct GTY(()) cp_parser {
1399   /* The lexer from which we are obtaining tokens.  */
1400   cp_lexer *lexer;
1401
1402   /* The scope in which names should be looked up.  If NULL_TREE, then
1403      we look up names in the scope that is currently open in the
1404      source program.  If non-NULL, this is either a TYPE or
1405      NAMESPACE_DECL for the scope in which we should look.  It can
1406      also be ERROR_MARK, when we've parsed a bogus scope.
1407
1408      This value is not cleared automatically after a name is looked
1409      up, so we must be careful to clear it before starting a new look
1410      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1411      will look up `Z' in the scope of `X', rather than the current
1412      scope.)  Unfortunately, it is difficult to tell when name lookup
1413      is complete, because we sometimes peek at a token, look it up,
1414      and then decide not to consume it.   */
1415   tree scope;
1416
1417   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1418      last lookup took place.  OBJECT_SCOPE is used if an expression
1419      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1420      respectively.  QUALIFYING_SCOPE is used for an expression of the
1421      form "X::Y"; it refers to X.  */
1422   tree object_scope;
1423   tree qualifying_scope;
1424
1425   /* A stack of parsing contexts.  All but the bottom entry on the
1426      stack will be tentative contexts.
1427
1428      We parse tentatively in order to determine which construct is in
1429      use in some situations.  For example, in order to determine
1430      whether a statement is an expression-statement or a
1431      declaration-statement we parse it tentatively as a
1432      declaration-statement.  If that fails, we then reparse the same
1433      token stream as an expression-statement.  */
1434   cp_parser_context *context;
1435
1436   /* True if we are parsing GNU C++.  If this flag is not set, then
1437      GNU extensions are not recognized.  */
1438   bool allow_gnu_extensions_p;
1439
1440   /* TRUE if the `>' token should be interpreted as the greater-than
1441      operator.  FALSE if it is the end of a template-id or
1442      template-parameter-list. In C++0x mode, this flag also applies to
1443      `>>' tokens, which are viewed as two consecutive `>' tokens when
1444      this flag is FALSE.  */
1445   bool greater_than_is_operator_p;
1446
1447   /* TRUE if default arguments are allowed within a parameter list
1448      that starts at this point. FALSE if only a gnu extension makes
1449      them permissible.  */
1450   bool default_arg_ok_p;
1451
1452   /* TRUE if we are parsing an integral constant-expression.  See
1453      [expr.const] for a precise definition.  */
1454   bool integral_constant_expression_p;
1455
1456   /* TRUE if we are parsing an integral constant-expression -- but a
1457      non-constant expression should be permitted as well.  This flag
1458      is used when parsing an array bound so that GNU variable-length
1459      arrays are tolerated.  */
1460   bool allow_non_integral_constant_expression_p;
1461
1462   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1463      been seen that makes the expression non-constant.  */
1464   bool non_integral_constant_expression_p;
1465
1466   /* TRUE if local variable names and `this' are forbidden in the
1467      current context.  */
1468   bool local_variables_forbidden_p;
1469
1470   /* TRUE if the declaration we are parsing is part of a
1471      linkage-specification of the form `extern string-literal
1472      declaration'.  */
1473   bool in_unbraced_linkage_specification_p;
1474
1475   /* TRUE if we are presently parsing a declarator, after the
1476      direct-declarator.  */
1477   bool in_declarator_p;
1478
1479   /* TRUE if we are presently parsing a template-argument-list.  */
1480   bool in_template_argument_list_p;
1481
1482   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1483      to IN_OMP_BLOCK if parsing OpenMP structured block and
1484      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1485      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1486      iteration-statement, OpenMP block or loop within that switch.  */
1487 #define IN_SWITCH_STMT          1
1488 #define IN_ITERATION_STMT       2
1489 #define IN_OMP_BLOCK            4
1490 #define IN_OMP_FOR              8
1491 #define IN_IF_STMT             16
1492   unsigned char in_statement;
1493
1494   /* TRUE if we are presently parsing the body of a switch statement.
1495      Note that this doesn't quite overlap with in_statement above.
1496      The difference relates to giving the right sets of error messages:
1497      "case not in switch" vs "break statement used with OpenMP...".  */
1498   bool in_switch_statement_p;
1499
1500   /* TRUE if we are parsing a type-id in an expression context.  In
1501      such a situation, both "type (expr)" and "type (type)" are valid
1502      alternatives.  */
1503   bool in_type_id_in_expr_p;
1504
1505   /* TRUE if we are currently in a header file where declarations are
1506      implicitly extern "C".  */
1507   bool implicit_extern_c;
1508
1509   /* TRUE if strings in expressions should be translated to the execution
1510      character set.  */
1511   bool translate_strings_p;
1512
1513   /* TRUE if we are presently parsing the body of a function, but not
1514      a local class.  */
1515   bool in_function_body;
1516
1517   /* If non-NULL, then we are parsing a construct where new type
1518      definitions are not permitted.  The string stored here will be
1519      issued as an error message if a type is defined.  */
1520   const char *type_definition_forbidden_message;
1521
1522   /* A list of lists. The outer list is a stack, used for member
1523      functions of local classes. At each level there are two sub-list,
1524      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1525      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1526      TREE_VALUE's. The functions are chained in reverse declaration
1527      order.
1528
1529      The TREE_PURPOSE sublist contains those functions with default
1530      arguments that need post processing, and the TREE_VALUE sublist
1531      contains those functions with definitions that need post
1532      processing.
1533
1534      These lists can only be processed once the outermost class being
1535      defined is complete.  */
1536   tree unparsed_functions_queues;
1537
1538   /* The number of classes whose definitions are currently in
1539      progress.  */
1540   unsigned num_classes_being_defined;
1541
1542   /* The number of template parameter lists that apply directly to the
1543      current declaration.  */
1544   unsigned num_template_parameter_lists;
1545 } cp_parser;
1546
1547 /* Prototypes.  */
1548
1549 /* Constructors and destructors.  */
1550
1551 static cp_parser *cp_parser_new
1552   (void);
1553
1554 /* Routines to parse various constructs.
1555
1556    Those that return `tree' will return the error_mark_node (rather
1557    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1558    Sometimes, they will return an ordinary node if error-recovery was
1559    attempted, even though a parse error occurred.  So, to check
1560    whether or not a parse error occurred, you should always use
1561    cp_parser_error_occurred.  If the construct is optional (indicated
1562    either by an `_opt' in the name of the function that does the
1563    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1564    the construct is not present.  */
1565
1566 /* Lexical conventions [gram.lex]  */
1567
1568 static tree cp_parser_identifier
1569   (cp_parser *);
1570 static tree cp_parser_string_literal
1571   (cp_parser *, bool, bool);
1572
1573 /* Basic concepts [gram.basic]  */
1574
1575 static bool cp_parser_translation_unit
1576   (cp_parser *);
1577
1578 /* Expressions [gram.expr]  */
1579
1580 static tree cp_parser_primary_expression
1581   (cp_parser *, bool, bool, bool, cp_id_kind *);
1582 static tree cp_parser_id_expression
1583   (cp_parser *, bool, bool, bool *, bool, bool);
1584 static tree cp_parser_unqualified_id
1585   (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_nested_name_specifier_opt
1587   (cp_parser *, bool, bool, bool, bool);
1588 static tree cp_parser_nested_name_specifier
1589   (cp_parser *, bool, bool, bool, bool);
1590 static tree cp_parser_qualifying_entity
1591   (cp_parser *, bool, bool, bool, bool, bool);
1592 static tree cp_parser_postfix_expression
1593   (cp_parser *, bool, bool, bool, cp_id_kind *);
1594 static tree cp_parser_postfix_open_square_expression
1595   (cp_parser *, tree, bool);
1596 static tree cp_parser_postfix_dot_deref_expression
1597   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1598 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1599   (cp_parser *, bool, bool, bool, bool *);
1600 static void cp_parser_pseudo_destructor_name
1601   (cp_parser *, tree *, tree *);
1602 static tree cp_parser_unary_expression
1603   (cp_parser *, bool, bool, cp_id_kind *);
1604 static enum tree_code cp_parser_unary_operator
1605   (cp_token *);
1606 static tree cp_parser_new_expression
1607   (cp_parser *);
1608 static VEC(tree,gc) *cp_parser_new_placement
1609   (cp_parser *);
1610 static tree cp_parser_new_type_id
1611   (cp_parser *, tree *);
1612 static cp_declarator *cp_parser_new_declarator_opt
1613   (cp_parser *);
1614 static cp_declarator *cp_parser_direct_new_declarator
1615   (cp_parser *);
1616 static VEC(tree,gc) *cp_parser_new_initializer
1617   (cp_parser *);
1618 static tree cp_parser_delete_expression
1619   (cp_parser *);
1620 static tree cp_parser_cast_expression
1621   (cp_parser *, bool, bool, cp_id_kind *);
1622 static tree cp_parser_binary_expression
1623   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1624 static tree cp_parser_question_colon_clause
1625   (cp_parser *, tree);
1626 static tree cp_parser_assignment_expression
1627   (cp_parser *, bool, cp_id_kind *);
1628 static enum tree_code cp_parser_assignment_operator_opt
1629   (cp_parser *);
1630 static tree cp_parser_expression
1631   (cp_parser *, bool, cp_id_kind *);
1632 static tree cp_parser_constant_expression
1633   (cp_parser *, bool, bool *);
1634 static tree cp_parser_builtin_offsetof
1635   (cp_parser *);
1636 static tree cp_parser_lambda_expression
1637   (cp_parser *);
1638 static void cp_parser_lambda_introducer
1639   (cp_parser *, tree);
1640 static void cp_parser_lambda_declarator_opt
1641   (cp_parser *, tree);
1642 static void cp_parser_lambda_body
1643   (cp_parser *, tree);
1644
1645 /* Statements [gram.stmt.stmt]  */
1646
1647 static void cp_parser_statement
1648   (cp_parser *, tree, bool, bool *);
1649 static void cp_parser_label_for_labeled_statement
1650   (cp_parser *);
1651 static tree cp_parser_expression_statement
1652   (cp_parser *, tree);
1653 static tree cp_parser_compound_statement
1654   (cp_parser *, tree, bool);
1655 static void cp_parser_statement_seq_opt
1656   (cp_parser *, tree);
1657 static tree cp_parser_selection_statement
1658   (cp_parser *, bool *);
1659 static tree cp_parser_condition
1660   (cp_parser *);
1661 static tree cp_parser_iteration_statement
1662   (cp_parser *);
1663 static void cp_parser_for_init_statement
1664   (cp_parser *);
1665 static tree cp_parser_jump_statement
1666   (cp_parser *);
1667 static void cp_parser_declaration_statement
1668   (cp_parser *);
1669
1670 static tree cp_parser_implicitly_scoped_statement
1671   (cp_parser *, bool *);
1672 static void cp_parser_already_scoped_statement
1673   (cp_parser *);
1674
1675 /* Declarations [gram.dcl.dcl] */
1676
1677 static void cp_parser_declaration_seq_opt
1678   (cp_parser *);
1679 static void cp_parser_declaration
1680   (cp_parser *);
1681 static void cp_parser_block_declaration
1682   (cp_parser *, bool);
1683 static void cp_parser_simple_declaration
1684   (cp_parser *, bool);
1685 static void cp_parser_decl_specifier_seq
1686   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1687 static tree cp_parser_storage_class_specifier_opt
1688   (cp_parser *);
1689 static tree cp_parser_function_specifier_opt
1690   (cp_parser *, cp_decl_specifier_seq *);
1691 static tree cp_parser_type_specifier
1692   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1693    int *, bool *);
1694 static tree cp_parser_simple_type_specifier
1695   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1696 static tree cp_parser_type_name
1697   (cp_parser *);
1698 static tree cp_parser_nonclass_name 
1699   (cp_parser* parser);
1700 static tree cp_parser_elaborated_type_specifier
1701   (cp_parser *, bool, bool);
1702 static tree cp_parser_enum_specifier
1703   (cp_parser *);
1704 static void cp_parser_enumerator_list
1705   (cp_parser *, tree);
1706 static void cp_parser_enumerator_definition
1707   (cp_parser *, tree);
1708 static tree cp_parser_namespace_name
1709   (cp_parser *);
1710 static void cp_parser_namespace_definition
1711   (cp_parser *);
1712 static void cp_parser_namespace_body
1713   (cp_parser *);
1714 static tree cp_parser_qualified_namespace_specifier
1715   (cp_parser *);
1716 static void cp_parser_namespace_alias_definition
1717   (cp_parser *);
1718 static bool cp_parser_using_declaration
1719   (cp_parser *, bool);
1720 static void cp_parser_using_directive
1721   (cp_parser *);
1722 static void cp_parser_asm_definition
1723   (cp_parser *);
1724 static void cp_parser_linkage_specification
1725   (cp_parser *);
1726 static void cp_parser_static_assert
1727   (cp_parser *, bool);
1728 static tree cp_parser_decltype
1729   (cp_parser *);
1730
1731 /* Declarators [gram.dcl.decl] */
1732
1733 static tree cp_parser_init_declarator
1734   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1735 static cp_declarator *cp_parser_declarator
1736   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1737 static cp_declarator *cp_parser_direct_declarator
1738   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1739 static enum tree_code cp_parser_ptr_operator
1740   (cp_parser *, tree *, cp_cv_quals *);
1741 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1742   (cp_parser *);
1743 static tree cp_parser_late_return_type_opt
1744   (cp_parser *);
1745 static tree cp_parser_declarator_id
1746   (cp_parser *, bool);
1747 static tree cp_parser_type_id
1748   (cp_parser *);
1749 static tree cp_parser_template_type_arg
1750   (cp_parser *);
1751 static tree cp_parser_trailing_type_id (cp_parser *);
1752 static tree cp_parser_type_id_1
1753   (cp_parser *, bool, bool);
1754 static void cp_parser_type_specifier_seq
1755   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1756 static tree cp_parser_parameter_declaration_clause
1757   (cp_parser *);
1758 static tree cp_parser_parameter_declaration_list
1759   (cp_parser *, bool *);
1760 static cp_parameter_declarator *cp_parser_parameter_declaration
1761   (cp_parser *, bool, bool *);
1762 static tree cp_parser_default_argument 
1763   (cp_parser *, bool);
1764 static void cp_parser_function_body
1765   (cp_parser *);
1766 static tree cp_parser_initializer
1767   (cp_parser *, bool *, bool *);
1768 static tree cp_parser_initializer_clause
1769   (cp_parser *, bool *);
1770 static tree cp_parser_braced_list
1771   (cp_parser*, bool*);
1772 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1773   (cp_parser *, bool *);
1774
1775 static bool cp_parser_ctor_initializer_opt_and_function_body
1776   (cp_parser *);
1777
1778 /* Classes [gram.class] */
1779
1780 static tree cp_parser_class_name
1781   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1782 static tree cp_parser_class_specifier
1783   (cp_parser *);
1784 static tree cp_parser_class_head
1785   (cp_parser *, bool *, tree *, tree *);
1786 static enum tag_types cp_parser_class_key
1787   (cp_parser *);
1788 static void cp_parser_member_specification_opt
1789   (cp_parser *);
1790 static void cp_parser_member_declaration
1791   (cp_parser *);
1792 static tree cp_parser_pure_specifier
1793   (cp_parser *);
1794 static tree cp_parser_constant_initializer
1795   (cp_parser *);
1796
1797 /* Derived classes [gram.class.derived] */
1798
1799 static tree cp_parser_base_clause
1800   (cp_parser *);
1801 static tree cp_parser_base_specifier
1802   (cp_parser *);
1803
1804 /* Special member functions [gram.special] */
1805
1806 static tree cp_parser_conversion_function_id
1807   (cp_parser *);
1808 static tree cp_parser_conversion_type_id
1809   (cp_parser *);
1810 static cp_declarator *cp_parser_conversion_declarator_opt
1811   (cp_parser *);
1812 static bool cp_parser_ctor_initializer_opt
1813   (cp_parser *);
1814 static void cp_parser_mem_initializer_list
1815   (cp_parser *);
1816 static tree cp_parser_mem_initializer
1817   (cp_parser *);
1818 static tree cp_parser_mem_initializer_id
1819   (cp_parser *);
1820
1821 /* Overloading [gram.over] */
1822
1823 static tree cp_parser_operator_function_id
1824   (cp_parser *);
1825 static tree cp_parser_operator
1826   (cp_parser *);
1827
1828 /* Templates [gram.temp] */
1829
1830 static void cp_parser_template_declaration
1831   (cp_parser *, bool);
1832 static tree cp_parser_template_parameter_list
1833   (cp_parser *);
1834 static tree cp_parser_template_parameter
1835   (cp_parser *, bool *, bool *);
1836 static tree cp_parser_type_parameter
1837   (cp_parser *, bool *);
1838 static tree cp_parser_template_id
1839   (cp_parser *, bool, bool, bool);
1840 static tree cp_parser_template_name
1841   (cp_parser *, bool, bool, bool, bool *);
1842 static tree cp_parser_template_argument_list
1843   (cp_parser *);
1844 static tree cp_parser_template_argument
1845   (cp_parser *);
1846 static void cp_parser_explicit_instantiation
1847   (cp_parser *);
1848 static void cp_parser_explicit_specialization
1849   (cp_parser *);
1850
1851 /* Exception handling [gram.exception] */
1852
1853 static tree cp_parser_try_block
1854   (cp_parser *);
1855 static bool cp_parser_function_try_block
1856   (cp_parser *);
1857 static void cp_parser_handler_seq
1858   (cp_parser *);
1859 static void cp_parser_handler
1860   (cp_parser *);
1861 static tree cp_parser_exception_declaration
1862   (cp_parser *);
1863 static tree cp_parser_throw_expression
1864   (cp_parser *);
1865 static tree cp_parser_exception_specification_opt
1866   (cp_parser *);
1867 static tree cp_parser_type_id_list
1868   (cp_parser *);
1869
1870 /* GNU Extensions */
1871
1872 static tree cp_parser_asm_specification_opt
1873   (cp_parser *);
1874 static tree cp_parser_asm_operand_list
1875   (cp_parser *);
1876 static tree cp_parser_asm_clobber_list
1877   (cp_parser *);
1878 static tree cp_parser_asm_label_list
1879   (cp_parser *);
1880 static tree cp_parser_attributes_opt
1881   (cp_parser *);
1882 static tree cp_parser_attribute_list
1883   (cp_parser *);
1884 static bool cp_parser_extension_opt
1885   (cp_parser *, int *);
1886 static void cp_parser_label_declaration
1887   (cp_parser *);
1888
1889 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1890 static bool cp_parser_pragma
1891   (cp_parser *, enum pragma_context);
1892
1893 /* Objective-C++ Productions */
1894
1895 static tree cp_parser_objc_message_receiver
1896   (cp_parser *);
1897 static tree cp_parser_objc_message_args
1898   (cp_parser *);
1899 static tree cp_parser_objc_message_expression
1900   (cp_parser *);
1901 static tree cp_parser_objc_encode_expression
1902   (cp_parser *);
1903 static tree cp_parser_objc_defs_expression
1904   (cp_parser *);
1905 static tree cp_parser_objc_protocol_expression
1906   (cp_parser *);
1907 static tree cp_parser_objc_selector_expression
1908   (cp_parser *);
1909 static tree cp_parser_objc_expression
1910   (cp_parser *);
1911 static bool cp_parser_objc_selector_p
1912   (enum cpp_ttype);
1913 static tree cp_parser_objc_selector
1914   (cp_parser *);
1915 static tree cp_parser_objc_protocol_refs_opt
1916   (cp_parser *);
1917 static void cp_parser_objc_declaration
1918   (cp_parser *);
1919 static tree cp_parser_objc_statement
1920   (cp_parser *);
1921
1922 /* Utility Routines */
1923
1924 static tree cp_parser_lookup_name
1925   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1926 static tree cp_parser_lookup_name_simple
1927   (cp_parser *, tree, location_t);
1928 static tree cp_parser_maybe_treat_template_as_class
1929   (tree, bool);
1930 static bool cp_parser_check_declarator_template_parameters
1931   (cp_parser *, cp_declarator *, location_t);
1932 static bool cp_parser_check_template_parameters
1933   (cp_parser *, unsigned, location_t, cp_declarator *);
1934 static tree cp_parser_simple_cast_expression
1935   (cp_parser *);
1936 static tree cp_parser_global_scope_opt
1937   (cp_parser *, bool);
1938 static bool cp_parser_constructor_declarator_p
1939   (cp_parser *, bool);
1940 static tree cp_parser_function_definition_from_specifiers_and_declarator
1941   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1942 static tree cp_parser_function_definition_after_declarator
1943   (cp_parser *, bool);
1944 static void cp_parser_template_declaration_after_export
1945   (cp_parser *, bool);
1946 static void cp_parser_perform_template_parameter_access_checks
1947   (VEC (deferred_access_check,gc)*);
1948 static tree cp_parser_single_declaration
1949   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1950 static tree cp_parser_functional_cast
1951   (cp_parser *, tree);
1952 static tree cp_parser_save_member_function_body
1953   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1954 static tree cp_parser_enclosed_template_argument_list
1955   (cp_parser *);
1956 static void cp_parser_save_default_args
1957   (cp_parser *, tree);
1958 static void cp_parser_late_parsing_for_member
1959   (cp_parser *, tree);
1960 static void cp_parser_late_parsing_default_args
1961   (cp_parser *, tree);
1962 static tree cp_parser_sizeof_operand
1963   (cp_parser *, enum rid);
1964 static tree cp_parser_trait_expr
1965   (cp_parser *, enum rid);
1966 static bool cp_parser_declares_only_class_p
1967   (cp_parser *);
1968 static void cp_parser_set_storage_class
1969   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1970 static void cp_parser_set_decl_spec_type
1971   (cp_decl_specifier_seq *, tree, location_t, bool);
1972 static bool cp_parser_friend_p
1973   (const cp_decl_specifier_seq *);
1974 static cp_token *cp_parser_require
1975   (cp_parser *, enum cpp_ttype, const char *);
1976 static cp_token *cp_parser_require_keyword
1977   (cp_parser *, enum rid, const char *);
1978 static bool cp_parser_token_starts_function_definition_p
1979   (cp_token *);
1980 static bool cp_parser_next_token_starts_class_definition_p
1981   (cp_parser *);
1982 static bool cp_parser_next_token_ends_template_argument_p
1983   (cp_parser *);
1984 static bool cp_parser_nth_token_starts_template_argument_list_p
1985   (cp_parser *, size_t);
1986 static enum tag_types cp_parser_token_is_class_key
1987   (cp_token *);
1988 static void cp_parser_check_class_key
1989   (enum tag_types, tree type);
1990 static void cp_parser_check_access_in_redeclaration
1991   (tree type, location_t location);
1992 static bool cp_parser_optional_template_keyword
1993   (cp_parser *);
1994 static void cp_parser_pre_parsed_nested_name_specifier
1995   (cp_parser *);
1996 static bool cp_parser_cache_group
1997   (cp_parser *, enum cpp_ttype, unsigned);
1998 static void cp_parser_parse_tentatively
1999   (cp_parser *);
2000 static void cp_parser_commit_to_tentative_parse
2001   (cp_parser *);
2002 static void cp_parser_abort_tentative_parse
2003   (cp_parser *);
2004 static bool cp_parser_parse_definitely
2005   (cp_parser *);
2006 static inline bool cp_parser_parsing_tentatively
2007   (cp_parser *);
2008 static bool cp_parser_uncommitted_to_tentative_parse_p
2009   (cp_parser *);
2010 static void cp_parser_error
2011   (cp_parser *, const char *);
2012 static void cp_parser_name_lookup_error
2013   (cp_parser *, tree, tree, const char *, location_t);
2014 static bool cp_parser_simulate_error
2015   (cp_parser *);
2016 static bool cp_parser_check_type_definition
2017   (cp_parser *);
2018 static void cp_parser_check_for_definition_in_return_type
2019   (cp_declarator *, tree, location_t type_location);
2020 static void cp_parser_check_for_invalid_template_id
2021   (cp_parser *, tree, location_t location);
2022 static bool cp_parser_non_integral_constant_expression
2023   (cp_parser *, const char *);
2024 static void cp_parser_diagnose_invalid_type_name
2025   (cp_parser *, tree, tree, location_t);
2026 static bool cp_parser_parse_and_diagnose_invalid_type_name
2027   (cp_parser *);
2028 static int cp_parser_skip_to_closing_parenthesis
2029   (cp_parser *, bool, bool, bool);
2030 static void cp_parser_skip_to_end_of_statement
2031   (cp_parser *);
2032 static void cp_parser_consume_semicolon_at_end_of_statement
2033   (cp_parser *);
2034 static void cp_parser_skip_to_end_of_block_or_statement
2035   (cp_parser *);
2036 static bool cp_parser_skip_to_closing_brace
2037   (cp_parser *);
2038 static void cp_parser_skip_to_end_of_template_parameter_list
2039   (cp_parser *);
2040 static void cp_parser_skip_to_pragma_eol
2041   (cp_parser*, cp_token *);
2042 static bool cp_parser_error_occurred
2043   (cp_parser *);
2044 static bool cp_parser_allow_gnu_extensions_p
2045   (cp_parser *);
2046 static bool cp_parser_is_string_literal
2047   (cp_token *);
2048 static bool cp_parser_is_keyword
2049   (cp_token *, enum rid);
2050 static tree cp_parser_make_typename_type
2051   (cp_parser *, tree, tree, location_t location);
2052 static cp_declarator * cp_parser_make_indirect_declarator
2053   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2054
2055 /* Returns nonzero if we are parsing tentatively.  */
2056
2057 static inline bool
2058 cp_parser_parsing_tentatively (cp_parser* parser)
2059 {
2060   return parser->context->next != NULL;
2061 }
2062
2063 /* Returns nonzero if TOKEN is a string literal.  */
2064
2065 static bool
2066 cp_parser_is_string_literal (cp_token* token)
2067 {
2068   return (token->type == CPP_STRING ||
2069           token->type == CPP_STRING16 ||
2070           token->type == CPP_STRING32 ||
2071           token->type == CPP_WSTRING ||
2072           token->type == CPP_UTF8STRING);
2073 }
2074
2075 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2076
2077 static bool
2078 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2079 {
2080   return token->keyword == keyword;
2081 }
2082
2083 /* If not parsing tentatively, issue a diagnostic of the form
2084       FILE:LINE: MESSAGE before TOKEN
2085    where TOKEN is the next token in the input stream.  MESSAGE
2086    (specified by the caller) is usually of the form "expected
2087    OTHER-TOKEN".  */
2088
2089 static void
2090 cp_parser_error (cp_parser* parser, const char* message)
2091 {
2092   if (!cp_parser_simulate_error (parser))
2093     {
2094       cp_token *token = cp_lexer_peek_token (parser->lexer);
2095       /* This diagnostic makes more sense if it is tagged to the line
2096          of the token we just peeked at.  */
2097       cp_lexer_set_source_position_from_token (token);
2098
2099       if (token->type == CPP_PRAGMA)
2100         {
2101           error_at (token->location,
2102                     "%<#pragma%> is not allowed here");
2103           cp_parser_skip_to_pragma_eol (parser, token);
2104           return;
2105         }
2106
2107       c_parse_error (message,
2108                      /* Because c_parser_error does not understand
2109                         CPP_KEYWORD, keywords are treated like
2110                         identifiers.  */
2111                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2112                      token->u.value, token->flags);
2113     }
2114 }
2115
2116 /* Issue an error about name-lookup failing.  NAME is the
2117    IDENTIFIER_NODE DECL is the result of
2118    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2119    the thing that we hoped to find.  */
2120
2121 static void
2122 cp_parser_name_lookup_error (cp_parser* parser,
2123                              tree name,
2124                              tree decl,
2125                              const char* desired,
2126                              location_t location)
2127 {
2128   /* If name lookup completely failed, tell the user that NAME was not
2129      declared.  */
2130   if (decl == error_mark_node)
2131     {
2132       if (parser->scope && parser->scope != global_namespace)
2133         error_at (location, "%<%E::%E%> has not been declared",
2134                   parser->scope, name);
2135       else if (parser->scope == global_namespace)
2136         error_at (location, "%<::%E%> has not been declared", name);
2137       else if (parser->object_scope
2138                && !CLASS_TYPE_P (parser->object_scope))
2139         error_at (location, "request for member %qE in non-class type %qT",
2140                   name, parser->object_scope);
2141       else if (parser->object_scope)
2142         error_at (location, "%<%T::%E%> has not been declared",
2143                   parser->object_scope, name);
2144       else
2145         error_at (location, "%qE has not been declared", name);
2146     }
2147   else if (parser->scope && parser->scope != global_namespace)
2148     error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2149   else if (parser->scope == global_namespace)
2150     error_at (location, "%<::%E%> %s", name, desired);
2151   else
2152     error_at (location, "%qE %s", name, desired);
2153 }
2154
2155 /* If we are parsing tentatively, remember that an error has occurred
2156    during this tentative parse.  Returns true if the error was
2157    simulated; false if a message should be issued by the caller.  */
2158
2159 static bool
2160 cp_parser_simulate_error (cp_parser* parser)
2161 {
2162   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2163     {
2164       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2165       return true;
2166     }
2167   return false;
2168 }
2169
2170 /* Check for repeated decl-specifiers.  */
2171
2172 static void
2173 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2174                            location_t location)
2175 {
2176   int ds;
2177
2178   for (ds = ds_first; ds != ds_last; ++ds)
2179     {
2180       unsigned count = decl_specs->specs[ds];
2181       if (count < 2)
2182         continue;
2183       /* The "long" specifier is a special case because of "long long".  */
2184       if (ds == ds_long)
2185         {
2186           if (count > 2)
2187             error_at (location, "%<long long long%> is too long for GCC");
2188           else 
2189             pedwarn_cxx98 (location, OPT_Wlong_long, 
2190                            "ISO C++ 1998 does not support %<long long%>");
2191         }
2192       else if (count > 1)
2193         {
2194           static const char *const decl_spec_names[] = {
2195             "signed",
2196             "unsigned",
2197             "short",
2198             "long",
2199             "const",
2200             "volatile",
2201             "restrict",
2202             "inline",
2203             "virtual",
2204             "explicit",
2205             "friend",
2206             "typedef",
2207             "constexpr",
2208             "__complex",
2209             "__thread"
2210           };
2211           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2212         }
2213     }
2214 }
2215
2216 /* This function is called when a type is defined.  If type
2217    definitions are forbidden at this point, an error message is
2218    issued.  */
2219
2220 static bool
2221 cp_parser_check_type_definition (cp_parser* parser)
2222 {
2223   /* If types are forbidden here, issue a message.  */
2224   if (parser->type_definition_forbidden_message)
2225     {
2226       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2227          in the message need to be interpreted.  */
2228       error (parser->type_definition_forbidden_message);
2229       return false;
2230     }
2231   return true;
2232 }
2233
2234 /* This function is called when the DECLARATOR is processed.  The TYPE
2235    was a type defined in the decl-specifiers.  If it is invalid to
2236    define a type in the decl-specifiers for DECLARATOR, an error is
2237    issued. TYPE_LOCATION is the location of TYPE and is used
2238    for error reporting.  */
2239
2240 static void
2241 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2242                                                tree type, location_t type_location)
2243 {
2244   /* [dcl.fct] forbids type definitions in return types.
2245      Unfortunately, it's not easy to know whether or not we are
2246      processing a return type until after the fact.  */
2247   while (declarator
2248          && (declarator->kind == cdk_pointer
2249              || declarator->kind == cdk_reference
2250              || declarator->kind == cdk_ptrmem))
2251     declarator = declarator->declarator;
2252   if (declarator
2253       && declarator->kind == cdk_function)
2254     {
2255       error_at (type_location,
2256                 "new types may not be defined in a return type");
2257       inform (type_location, 
2258               "(perhaps a semicolon is missing after the definition of %qT)",
2259               type);
2260     }
2261 }
2262
2263 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2264    "<" in any valid C++ program.  If the next token is indeed "<",
2265    issue a message warning the user about what appears to be an
2266    invalid attempt to form a template-id. LOCATION is the location
2267    of the type-specifier (TYPE) */
2268
2269 static void
2270 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2271                                          tree type, location_t location)
2272 {
2273   cp_token_position start = 0;
2274
2275   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2276     {
2277       if (TYPE_P (type))
2278         error_at (location, "%qT is not a template", type);
2279       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2280         error_at (location, "%qE is not a template", type);
2281       else
2282         error_at (location, "invalid template-id");
2283       /* Remember the location of the invalid "<".  */
2284       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2285         start = cp_lexer_token_position (parser->lexer, true);
2286       /* Consume the "<".  */
2287       cp_lexer_consume_token (parser->lexer);
2288       /* Parse the template arguments.  */
2289       cp_parser_enclosed_template_argument_list (parser);
2290       /* Permanently remove the invalid template arguments so that
2291          this error message is not issued again.  */
2292       if (start)
2293         cp_lexer_purge_tokens_after (parser->lexer, start);
2294     }
2295 }
2296
2297 /* If parsing an integral constant-expression, issue an error message
2298    about the fact that THING appeared and return true.  Otherwise,
2299    return false.  In either case, set
2300    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2301
2302 static bool
2303 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2304                                             const char *thing)
2305 {
2306   parser->non_integral_constant_expression_p = true;
2307   if (parser->integral_constant_expression_p)
2308     {
2309       if (!parser->allow_non_integral_constant_expression_p)
2310         {
2311           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2312              in the message need to be interpreted.  */
2313           char *message = concat (thing,
2314                                   " cannot appear in a constant-expression",
2315                                   NULL);
2316           error (message);
2317           free (message);
2318           return true;
2319         }
2320     }
2321   return false;
2322 }
2323
2324 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2325    qualifying scope (or NULL, if none) for ID.  This function commits
2326    to the current active tentative parse, if any.  (Otherwise, the
2327    problematic construct might be encountered again later, resulting
2328    in duplicate error messages.) LOCATION is the location of ID.  */
2329
2330 static void
2331 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2332                                       tree scope, tree id,
2333                                       location_t location)
2334 {
2335   tree decl, old_scope;
2336   /* Try to lookup the identifier.  */
2337   old_scope = parser->scope;
2338   parser->scope = scope;
2339   decl = cp_parser_lookup_name_simple (parser, id, location);
2340   parser->scope = old_scope;
2341   /* If the lookup found a template-name, it means that the user forgot
2342   to specify an argument list. Emit a useful error message.  */
2343   if (TREE_CODE (decl) == TEMPLATE_DECL)
2344     error_at (location,
2345               "invalid use of template-name %qE without an argument list",
2346               decl);
2347   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2348     error_at (location, "invalid use of destructor %qD as a type", id);
2349   else if (TREE_CODE (decl) == TYPE_DECL)
2350     /* Something like 'unsigned A a;'  */
2351     error_at (location, "invalid combination of multiple type-specifiers");
2352   else if (!parser->scope)
2353     {
2354       /* Issue an error message.  */
2355       error_at (location, "%qE does not name a type", id);
2356       /* If we're in a template class, it's possible that the user was
2357          referring to a type from a base class.  For example:
2358
2359            template <typename T> struct A { typedef T X; };
2360            template <typename T> struct B : public A<T> { X x; };
2361
2362          The user should have said "typename A<T>::X".  */
2363       if (processing_template_decl && current_class_type
2364           && TYPE_BINFO (current_class_type))
2365         {
2366           tree b;
2367
2368           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2369                b;
2370                b = TREE_CHAIN (b))
2371             {
2372               tree base_type = BINFO_TYPE (b);
2373               if (CLASS_TYPE_P (base_type)
2374                   && dependent_type_p (base_type))
2375                 {
2376                   tree field;
2377                   /* Go from a particular instantiation of the
2378                      template (which will have an empty TYPE_FIELDs),
2379                      to the main version.  */
2380                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2381                   for (field = TYPE_FIELDS (base_type);
2382                        field;
2383                        field = TREE_CHAIN (field))
2384                     if (TREE_CODE (field) == TYPE_DECL
2385                         && DECL_NAME (field) == id)
2386                       {
2387                         inform (location, 
2388                                 "(perhaps %<typename %T::%E%> was intended)",
2389                                 BINFO_TYPE (b), id);
2390                         break;
2391                       }
2392                   if (field)
2393                     break;
2394                 }
2395             }
2396         }
2397     }
2398   /* Here we diagnose qualified-ids where the scope is actually correct,
2399      but the identifier does not resolve to a valid type name.  */
2400   else if (parser->scope != error_mark_node)
2401     {
2402       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2403         error_at (location, "%qE in namespace %qE does not name a type",
2404                   id, parser->scope);
2405       else if (CLASS_TYPE_P (parser->scope)
2406                && constructor_name_p (id, parser->scope))
2407         {
2408           /* A<T>::A<T>() */
2409           error_at (location, "%<%T::%E%> names the constructor, not"
2410                     " the type", parser->scope, id);
2411           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2412             error_at (location, "and %qT has no template constructors",
2413                       parser->scope);
2414         }
2415       else if (TYPE_P (parser->scope)
2416                && dependent_scope_p (parser->scope))
2417         error_at (location, "need %<typename%> before %<%T::%E%> because "
2418                   "%qT is a dependent scope",
2419                   parser->scope, id, parser->scope);
2420       else if (TYPE_P (parser->scope))
2421         error_at (location, "%qE in class %qT does not name a type",
2422                   id, parser->scope);
2423       else
2424         gcc_unreachable ();
2425     }
2426   cp_parser_commit_to_tentative_parse (parser);
2427 }
2428
2429 /* Check for a common situation where a type-name should be present,
2430    but is not, and issue a sensible error message.  Returns true if an
2431    invalid type-name was detected.
2432
2433    The situation handled by this function are variable declarations of the
2434    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2435    Usually, `ID' should name a type, but if we got here it means that it
2436    does not. We try to emit the best possible error message depending on
2437    how exactly the id-expression looks like.  */
2438
2439 static bool
2440 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2441 {
2442   tree id;
2443   cp_token *token = cp_lexer_peek_token (parser->lexer);
2444
2445   /* Avoid duplicate error about ambiguous lookup.  */
2446   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2447     {
2448       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2449       if (next->type == CPP_NAME && next->ambiguous_p)
2450         goto out;
2451     }
2452
2453   cp_parser_parse_tentatively (parser);
2454   id = cp_parser_id_expression (parser,
2455                                 /*template_keyword_p=*/false,
2456                                 /*check_dependency_p=*/true,
2457                                 /*template_p=*/NULL,
2458                                 /*declarator_p=*/true,
2459                                 /*optional_p=*/false);
2460   /* If the next token is a (, this is a function with no explicit return
2461      type, i.e. constructor, destructor or conversion op.  */
2462   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2463       || TREE_CODE (id) == TYPE_DECL)
2464     {
2465       cp_parser_abort_tentative_parse (parser);
2466       return false;
2467     }
2468   if (!cp_parser_parse_definitely (parser))
2469     return false;
2470
2471   /* Emit a diagnostic for the invalid type.  */
2472   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2473                                         id, token->location);
2474  out:
2475   /* If we aren't in the middle of a declarator (i.e. in a
2476      parameter-declaration-clause), skip to the end of the declaration;
2477      there's no point in trying to process it.  */
2478   if (!parser->in_declarator_p)
2479     cp_parser_skip_to_end_of_block_or_statement (parser);
2480   return true;
2481 }
2482
2483 /* Consume tokens up to, and including, the next non-nested closing `)'.
2484    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2485    are doing error recovery. Returns -1 if OR_COMMA is true and we
2486    found an unnested comma.  */
2487
2488 static int
2489 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2490                                        bool recovering,
2491                                        bool or_comma,
2492                                        bool consume_paren)
2493 {
2494   unsigned paren_depth = 0;
2495   unsigned brace_depth = 0;
2496   unsigned square_depth = 0;
2497
2498   if (recovering && !or_comma
2499       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2500     return 0;
2501
2502   while (true)
2503     {
2504       cp_token * token = cp_lexer_peek_token (parser->lexer);
2505
2506       switch (token->type)
2507         {
2508         case CPP_EOF:
2509         case CPP_PRAGMA_EOL:
2510           /* If we've run out of tokens, then there is no closing `)'.  */
2511           return 0;
2512
2513         /* This is good for lambda expression capture-lists.  */
2514         case CPP_OPEN_SQUARE:
2515           ++square_depth;
2516           break;
2517         case CPP_CLOSE_SQUARE:
2518           if (!square_depth--)
2519             return 0;
2520           break;
2521
2522         case CPP_SEMICOLON:
2523           /* This matches the processing in skip_to_end_of_statement.  */
2524           if (!brace_depth)
2525             return 0;
2526           break;
2527
2528         case CPP_OPEN_BRACE:
2529           ++brace_depth;
2530           break;
2531         case CPP_CLOSE_BRACE:
2532           if (!brace_depth--)
2533             return 0;
2534           break;
2535
2536         case CPP_COMMA:
2537           if (recovering && or_comma && !brace_depth && !paren_depth
2538               && !square_depth)
2539             return -1;
2540           break;
2541
2542         case CPP_OPEN_PAREN:
2543           if (!brace_depth)
2544             ++paren_depth;
2545           break;
2546
2547         case CPP_CLOSE_PAREN:
2548           if (!brace_depth && !paren_depth--)
2549             {
2550               if (consume_paren)
2551                 cp_lexer_consume_token (parser->lexer);
2552               return 1;
2553             }
2554           break;
2555
2556         default:
2557           break;
2558         }
2559
2560       /* Consume the token.  */
2561       cp_lexer_consume_token (parser->lexer);
2562     }
2563 }
2564
2565 /* Consume tokens until we reach the end of the current statement.
2566    Normally, that will be just before consuming a `;'.  However, if a
2567    non-nested `}' comes first, then we stop before consuming that.  */
2568
2569 static void
2570 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2571 {
2572   unsigned nesting_depth = 0;
2573
2574   while (true)
2575     {
2576       cp_token *token = cp_lexer_peek_token (parser->lexer);
2577
2578       switch (token->type)
2579         {
2580         case CPP_EOF:
2581         case CPP_PRAGMA_EOL:
2582           /* If we've run out of tokens, stop.  */
2583           return;
2584
2585         case CPP_SEMICOLON:
2586           /* If the next token is a `;', we have reached the end of the
2587              statement.  */
2588           if (!nesting_depth)
2589             return;
2590           break;
2591
2592         case CPP_CLOSE_BRACE:
2593           /* If this is a non-nested '}', stop before consuming it.
2594              That way, when confronted with something like:
2595
2596                { 3 + }
2597
2598              we stop before consuming the closing '}', even though we
2599              have not yet reached a `;'.  */
2600           if (nesting_depth == 0)
2601             return;
2602
2603           /* If it is the closing '}' for a block that we have
2604              scanned, stop -- but only after consuming the token.
2605              That way given:
2606
2607                 void f g () { ... }
2608                 typedef int I;
2609
2610              we will stop after the body of the erroneously declared
2611              function, but before consuming the following `typedef'
2612              declaration.  */
2613           if (--nesting_depth == 0)
2614             {
2615               cp_lexer_consume_token (parser->lexer);
2616               return;
2617             }
2618
2619         case CPP_OPEN_BRACE:
2620           ++nesting_depth;
2621           break;
2622
2623         default:
2624           break;
2625         }
2626
2627       /* Consume the token.  */
2628       cp_lexer_consume_token (parser->lexer);
2629     }
2630 }
2631
2632 /* This function is called at the end of a statement or declaration.
2633    If the next token is a semicolon, it is consumed; otherwise, error
2634    recovery is attempted.  */
2635
2636 static void
2637 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2638 {
2639   /* Look for the trailing `;'.  */
2640   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2641     {
2642       /* If there is additional (erroneous) input, skip to the end of
2643          the statement.  */
2644       cp_parser_skip_to_end_of_statement (parser);
2645       /* If the next token is now a `;', consume it.  */
2646       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2647         cp_lexer_consume_token (parser->lexer);
2648     }
2649 }
2650
2651 /* Skip tokens until we have consumed an entire block, or until we
2652    have consumed a non-nested `;'.  */
2653
2654 static void
2655 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2656 {
2657   int nesting_depth = 0;
2658
2659   while (nesting_depth >= 0)
2660     {
2661       cp_token *token = cp_lexer_peek_token (parser->lexer);
2662
2663       switch (token->type)
2664         {
2665         case CPP_EOF:
2666         case CPP_PRAGMA_EOL:
2667           /* If we've run out of tokens, stop.  */
2668           return;
2669
2670         case CPP_SEMICOLON:
2671           /* Stop if this is an unnested ';'. */
2672           if (!nesting_depth)
2673             nesting_depth = -1;
2674           break;
2675
2676         case CPP_CLOSE_BRACE:
2677           /* Stop if this is an unnested '}', or closes the outermost
2678              nesting level.  */
2679           nesting_depth--;
2680           if (nesting_depth < 0)
2681             return;
2682           if (!nesting_depth)
2683             nesting_depth = -1;
2684           break;
2685
2686         case CPP_OPEN_BRACE:
2687           /* Nest. */
2688           nesting_depth++;
2689           break;
2690
2691         default:
2692           break;
2693         }
2694
2695       /* Consume the token.  */
2696       cp_lexer_consume_token (parser->lexer);
2697     }
2698 }
2699
2700 /* Skip tokens until a non-nested closing curly brace is the next
2701    token, or there are no more tokens. Return true in the first case,
2702    false otherwise.  */
2703
2704 static bool
2705 cp_parser_skip_to_closing_brace (cp_parser *parser)
2706 {
2707   unsigned nesting_depth = 0;
2708
2709   while (true)
2710     {
2711       cp_token *token = cp_lexer_peek_token (parser->lexer);
2712
2713       switch (token->type)
2714         {
2715         case CPP_EOF:
2716         case CPP_PRAGMA_EOL:
2717           /* If we've run out of tokens, stop.  */
2718           return false;
2719
2720         case CPP_CLOSE_BRACE:
2721           /* If the next token is a non-nested `}', then we have reached
2722              the end of the current block.  */
2723           if (nesting_depth-- == 0)
2724             return true;
2725           break;
2726
2727         case CPP_OPEN_BRACE:
2728           /* If it the next token is a `{', then we are entering a new
2729              block.  Consume the entire block.  */
2730           ++nesting_depth;
2731           break;
2732
2733         default:
2734           break;
2735         }
2736
2737       /* Consume the token.  */
2738       cp_lexer_consume_token (parser->lexer);
2739     }
2740 }
2741
2742 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2743    parameter is the PRAGMA token, allowing us to purge the entire pragma
2744    sequence.  */
2745
2746 static void
2747 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2748 {
2749   cp_token *token;
2750
2751   parser->lexer->in_pragma = false;
2752
2753   do
2754     token = cp_lexer_consume_token (parser->lexer);
2755   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2756
2757   /* Ensure that the pragma is not parsed again.  */
2758   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2759 }
2760
2761 /* Require pragma end of line, resyncing with it as necessary.  The
2762    arguments are as for cp_parser_skip_to_pragma_eol.  */
2763
2764 static void
2765 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2766 {
2767   parser->lexer->in_pragma = false;
2768   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2769     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2770 }
2771
2772 /* This is a simple wrapper around make_typename_type. When the id is
2773    an unresolved identifier node, we can provide a superior diagnostic
2774    using cp_parser_diagnose_invalid_type_name.  */
2775
2776 static tree
2777 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2778                               tree id, location_t id_location)
2779 {
2780   tree result;
2781   if (TREE_CODE (id) == IDENTIFIER_NODE)
2782     {
2783       result = make_typename_type (scope, id, typename_type,
2784                                    /*complain=*/tf_none);
2785       if (result == error_mark_node)
2786         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2787       return result;
2788     }
2789   return make_typename_type (scope, id, typename_type, tf_error);
2790 }
2791
2792 /* This is a wrapper around the
2793    make_{pointer,ptrmem,reference}_declarator functions that decides
2794    which one to call based on the CODE and CLASS_TYPE arguments. The
2795    CODE argument should be one of the values returned by
2796    cp_parser_ptr_operator. */
2797 static cp_declarator *
2798 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2799                                     cp_cv_quals cv_qualifiers,
2800                                     cp_declarator *target)
2801 {
2802   if (code == ERROR_MARK)
2803     return cp_error_declarator;
2804
2805   if (code == INDIRECT_REF)
2806     if (class_type == NULL_TREE)
2807       return make_pointer_declarator (cv_qualifiers, target);
2808     else
2809       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2810   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2811     return make_reference_declarator (cv_qualifiers, target, false);
2812   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2813     return make_reference_declarator (cv_qualifiers, target, true);
2814   gcc_unreachable ();
2815 }
2816
2817 /* Create a new C++ parser.  */
2818
2819 static cp_parser *
2820 cp_parser_new (void)
2821 {
2822   cp_parser *parser;
2823   cp_lexer *lexer;
2824   unsigned i;
2825
2826   /* cp_lexer_new_main is called before calling ggc_alloc because
2827      cp_lexer_new_main might load a PCH file.  */
2828   lexer = cp_lexer_new_main ();
2829
2830   /* Initialize the binops_by_token so that we can get the tree
2831      directly from the token.  */
2832   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2833     binops_by_token[binops[i].token_type] = binops[i];
2834
2835   parser = GGC_CNEW (cp_parser);
2836   parser->lexer = lexer;
2837   parser->context = cp_parser_context_new (NULL);
2838
2839   /* For now, we always accept GNU extensions.  */
2840   parser->allow_gnu_extensions_p = 1;
2841
2842   /* The `>' token is a greater-than operator, not the end of a
2843      template-id.  */
2844   parser->greater_than_is_operator_p = true;
2845
2846   parser->default_arg_ok_p = true;
2847
2848   /* We are not parsing a constant-expression.  */
2849   parser->integral_constant_expression_p = false;
2850   parser->allow_non_integral_constant_expression_p = false;
2851   parser->non_integral_constant_expression_p = false;
2852
2853   /* Local variable names are not forbidden.  */
2854   parser->local_variables_forbidden_p = false;
2855
2856   /* We are not processing an `extern "C"' declaration.  */
2857   parser->in_unbraced_linkage_specification_p = false;
2858
2859   /* We are not processing a declarator.  */
2860   parser->in_declarator_p = false;
2861
2862   /* We are not processing a template-argument-list.  */
2863   parser->in_template_argument_list_p = false;
2864
2865   /* We are not in an iteration statement.  */
2866   parser->in_statement = 0;
2867
2868   /* We are not in a switch statement.  */
2869   parser->in_switch_statement_p = false;
2870
2871   /* We are not parsing a type-id inside an expression.  */
2872   parser->in_type_id_in_expr_p = false;
2873
2874   /* Declarations aren't implicitly extern "C".  */
2875   parser->implicit_extern_c = false;
2876
2877   /* String literals should be translated to the execution character set.  */
2878   parser->translate_strings_p = true;
2879
2880   /* We are not parsing a function body.  */
2881   parser->in_function_body = false;
2882
2883   /* The unparsed function queue is empty.  */
2884   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2885
2886   /* There are no classes being defined.  */
2887   parser->num_classes_being_defined = 0;
2888
2889   /* No template parameters apply.  */
2890   parser->num_template_parameter_lists = 0;
2891
2892   return parser;
2893 }
2894
2895 /* Create a cp_lexer structure which will emit the tokens in CACHE
2896    and push it onto the parser's lexer stack.  This is used for delayed
2897    parsing of in-class method bodies and default arguments, and should
2898    not be confused with tentative parsing.  */
2899 static void
2900 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2901 {
2902   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2903   lexer->next = parser->lexer;
2904   parser->lexer = lexer;
2905
2906   /* Move the current source position to that of the first token in the
2907      new lexer.  */
2908   cp_lexer_set_source_position_from_token (lexer->next_token);
2909 }
2910
2911 /* Pop the top lexer off the parser stack.  This is never used for the
2912    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2913 static void
2914 cp_parser_pop_lexer (cp_parser *parser)
2915 {
2916   cp_lexer *lexer = parser->lexer;
2917   parser->lexer = lexer->next;
2918   cp_lexer_destroy (lexer);
2919
2920   /* Put the current source position back where it was before this
2921      lexer was pushed.  */
2922   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2923 }
2924
2925 /* Lexical conventions [gram.lex]  */
2926
2927 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2928    identifier.  */
2929
2930 static tree
2931 cp_parser_identifier (cp_parser* parser)
2932 {
2933   cp_token *token;
2934
2935   /* Look for the identifier.  */
2936   token = cp_parser_require (parser, CPP_NAME, "identifier");
2937   /* Return the value.  */
2938   return token ? token->u.value : error_mark_node;
2939 }
2940
2941 /* Parse a sequence of adjacent string constants.  Returns a
2942    TREE_STRING representing the combined, nul-terminated string
2943    constant.  If TRANSLATE is true, translate the string to the
2944    execution character set.  If WIDE_OK is true, a wide string is
2945    invalid here.
2946
2947    C++98 [lex.string] says that if a narrow string literal token is
2948    adjacent to a wide string literal token, the behavior is undefined.
2949    However, C99 6.4.5p4 says that this results in a wide string literal.
2950    We follow C99 here, for consistency with the C front end.
2951
2952    This code is largely lifted from lex_string() in c-lex.c.
2953
2954    FUTURE: ObjC++ will need to handle @-strings here.  */
2955 static tree
2956 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2957 {
2958   tree value;
2959   size_t count;
2960   struct obstack str_ob;
2961   cpp_string str, istr, *strs;
2962   cp_token *tok;
2963   enum cpp_ttype type;
2964
2965   tok = cp_lexer_peek_token (parser->lexer);
2966   if (!cp_parser_is_string_literal (tok))
2967     {
2968       cp_parser_error (parser, "expected string-literal");
2969       return error_mark_node;
2970     }
2971
2972   type = tok->type;
2973
2974   /* Try to avoid the overhead of creating and destroying an obstack
2975      for the common case of just one string.  */
2976   if (!cp_parser_is_string_literal
2977       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2978     {
2979       cp_lexer_consume_token (parser->lexer);
2980
2981       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2982       str.len = TREE_STRING_LENGTH (tok->u.value);
2983       count = 1;
2984
2985       strs = &str;
2986     }
2987   else
2988     {
2989       gcc_obstack_init (&str_ob);
2990       count = 0;
2991
2992       do
2993         {
2994           cp_lexer_consume_token (parser->lexer);
2995           count++;
2996           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2997           str.len = TREE_STRING_LENGTH (tok->u.value);
2998
2999           if (type != tok->type)
3000             {
3001               if (type == CPP_STRING)
3002                 type = tok->type;
3003               else if (tok->type != CPP_STRING)
3004                 error_at (tok->location,
3005                           "unsupported non-standard concatenation "
3006                           "of string literals");
3007             }
3008
3009           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3010
3011           tok = cp_lexer_peek_token (parser->lexer);
3012         }
3013       while (cp_parser_is_string_literal (tok));
3014
3015       strs = (cpp_string *) obstack_finish (&str_ob);
3016     }
3017
3018   if (type != CPP_STRING && !wide_ok)
3019     {
3020       cp_parser_error (parser, "a wide string is invalid in this context");
3021       type = CPP_STRING;
3022     }
3023
3024   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3025       (parse_in, strs, count, &istr, type))
3026     {
3027       value = build_string (istr.len, (const char *)istr.text);
3028       free (CONST_CAST (unsigned char *, istr.text));
3029
3030       switch (type)
3031         {
3032         default:
3033         case CPP_STRING:
3034         case CPP_UTF8STRING:
3035           TREE_TYPE (value) = char_array_type_node;
3036           break;
3037         case CPP_STRING16:
3038           TREE_TYPE (value) = char16_array_type_node;
3039           break;
3040         case CPP_STRING32:
3041           TREE_TYPE (value) = char32_array_type_node;
3042           break;
3043         case CPP_WSTRING:
3044           TREE_TYPE (value) = wchar_array_type_node;
3045           break;
3046         }
3047
3048       value = fix_string_type (value);
3049     }
3050   else
3051     /* cpp_interpret_string has issued an error.  */
3052     value = error_mark_node;
3053
3054   if (count > 1)
3055     obstack_free (&str_ob, 0);
3056
3057   return value;
3058 }
3059
3060
3061 /* Basic concepts [gram.basic]  */
3062
3063 /* Parse a translation-unit.
3064
3065    translation-unit:
3066      declaration-seq [opt]
3067
3068    Returns TRUE if all went well.  */
3069
3070 static bool
3071 cp_parser_translation_unit (cp_parser* parser)
3072 {
3073   /* The address of the first non-permanent object on the declarator
3074      obstack.  */
3075   static void *declarator_obstack_base;
3076
3077   bool success;
3078
3079   /* Create the declarator obstack, if necessary.  */
3080   if (!cp_error_declarator)
3081     {
3082       gcc_obstack_init (&declarator_obstack);
3083       /* Create the error declarator.  */
3084       cp_error_declarator = make_declarator (cdk_error);
3085       /* Create the empty parameter list.  */
3086       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3087       /* Remember where the base of the declarator obstack lies.  */
3088       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3089     }
3090
3091   cp_parser_declaration_seq_opt (parser);
3092
3093   /* If there are no tokens left then all went well.  */
3094   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3095     {
3096       /* Get rid of the token array; we don't need it any more.  */
3097       cp_lexer_destroy (parser->lexer);
3098       parser->lexer = NULL;
3099
3100       /* This file might have been a context that's implicitly extern
3101          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3102       if (parser->implicit_extern_c)
3103         {
3104           pop_lang_context ();
3105           parser->implicit_extern_c = false;
3106         }
3107
3108       /* Finish up.  */
3109       finish_translation_unit ();
3110
3111       success = true;
3112     }
3113   else
3114     {
3115       cp_parser_error (parser, "expected declaration");
3116       success = false;
3117     }
3118
3119   /* Make sure the declarator obstack was fully cleaned up.  */
3120   gcc_assert (obstack_next_free (&declarator_obstack)
3121               == declarator_obstack_base);
3122
3123   /* All went well.  */
3124   return success;
3125 }
3126
3127 /* Expressions [gram.expr] */
3128
3129 /* Parse a primary-expression.
3130
3131    primary-expression:
3132      literal
3133      this
3134      ( expression )
3135      id-expression
3136
3137    GNU Extensions:
3138
3139    primary-expression:
3140      ( compound-statement )
3141      __builtin_va_arg ( assignment-expression , type-id )
3142      __builtin_offsetof ( type-id , offsetof-expression )
3143
3144    C++ Extensions:
3145      __has_nothrow_assign ( type-id )   
3146      __has_nothrow_constructor ( type-id )
3147      __has_nothrow_copy ( type-id )
3148      __has_trivial_assign ( type-id )   
3149      __has_trivial_constructor ( type-id )
3150      __has_trivial_copy ( type-id )
3151      __has_trivial_destructor ( type-id )
3152      __has_virtual_destructor ( type-id )     
3153      __is_abstract ( type-id )
3154      __is_base_of ( type-id , type-id )
3155      __is_class ( type-id )
3156      __is_convertible_to ( type-id , type-id )     
3157      __is_empty ( type-id )
3158      __is_enum ( type-id )
3159      __is_pod ( type-id )
3160      __is_polymorphic ( type-id )
3161      __is_union ( type-id )
3162
3163    Objective-C++ Extension:
3164
3165    primary-expression:
3166      objc-expression
3167
3168    literal:
3169      __null
3170
3171    ADDRESS_P is true iff this expression was immediately preceded by
3172    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3173    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3174    true iff this expression is a template argument.
3175
3176    Returns a representation of the expression.  Upon return, *IDK
3177    indicates what kind of id-expression (if any) was present.  */
3178
3179 static tree
3180 cp_parser_primary_expression (cp_parser *parser,
3181                               bool address_p,
3182                               bool cast_p,
3183                               bool template_arg_p,
3184                               cp_id_kind *idk)
3185 {
3186   cp_token *token = NULL;
3187
3188   /* Assume the primary expression is not an id-expression.  */
3189   *idk = CP_ID_KIND_NONE;
3190
3191   /* Peek at the next token.  */
3192   token = cp_lexer_peek_token (parser->lexer);
3193   switch (token->type)
3194     {
3195       /* literal:
3196            integer-literal
3197            character-literal
3198            floating-literal
3199            string-literal
3200            boolean-literal  */
3201     case CPP_CHAR:
3202     case CPP_CHAR16:
3203     case CPP_CHAR32:
3204     case CPP_WCHAR:
3205     case CPP_NUMBER:
3206       token = cp_lexer_consume_token (parser->lexer);
3207       if (TREE_CODE (token->u.value) == FIXED_CST)
3208         {
3209           error_at (token->location,
3210                     "fixed-point types not supported in C++");
3211           return error_mark_node;
3212         }
3213       /* Floating-point literals are only allowed in an integral
3214          constant expression if they are cast to an integral or
3215          enumeration type.  */
3216       if (TREE_CODE (token->u.value) == REAL_CST
3217           && parser->integral_constant_expression_p
3218           && pedantic)
3219         {
3220           /* CAST_P will be set even in invalid code like "int(2.7 +
3221              ...)".   Therefore, we have to check that the next token
3222              is sure to end the cast.  */
3223           if (cast_p)
3224             {
3225               cp_token *next_token;
3226
3227               next_token = cp_lexer_peek_token (parser->lexer);
3228               if (/* The comma at the end of an
3229                      enumerator-definition.  */
3230                   next_token->type != CPP_COMMA
3231                   /* The curly brace at the end of an enum-specifier.  */
3232                   && next_token->type != CPP_CLOSE_BRACE
3233                   /* The end of a statement.  */
3234                   && next_token->type != CPP_SEMICOLON
3235                   /* The end of the cast-expression.  */
3236                   && next_token->type != CPP_CLOSE_PAREN
3237                   /* The end of an array bound.  */
3238                   && next_token->type != CPP_CLOSE_SQUARE
3239                   /* The closing ">" in a template-argument-list.  */
3240                   && (next_token->type != CPP_GREATER
3241                       || parser->greater_than_is_operator_p)
3242                   /* C++0x only: A ">>" treated like two ">" tokens,
3243                      in a template-argument-list.  */
3244                   && (next_token->type != CPP_RSHIFT
3245                       || (cxx_dialect == cxx98)
3246                       || parser->greater_than_is_operator_p))
3247                 cast_p = false;
3248             }
3249
3250           /* If we are within a cast, then the constraint that the
3251              cast is to an integral or enumeration type will be
3252              checked at that point.  If we are not within a cast, then
3253              this code is invalid.  */
3254           if (!cast_p)
3255             cp_parser_non_integral_constant_expression
3256               (parser, "floating-point literal");
3257         }
3258       return token->u.value;
3259
3260     case CPP_STRING:
3261     case CPP_STRING16:
3262     case CPP_STRING32:
3263     case CPP_WSTRING:
3264     case CPP_UTF8STRING:
3265       /* ??? Should wide strings be allowed when parser->translate_strings_p
3266          is false (i.e. in attributes)?  If not, we can kill the third
3267          argument to cp_parser_string_literal.  */
3268       return cp_parser_string_literal (parser,
3269                                        parser->translate_strings_p,
3270                                        true);
3271
3272     case CPP_OPEN_PAREN:
3273       {
3274         tree expr;
3275         bool saved_greater_than_is_operator_p;
3276
3277         /* Consume the `('.  */
3278         cp_lexer_consume_token (parser->lexer);
3279         /* Within a parenthesized expression, a `>' token is always
3280            the greater-than operator.  */
3281         saved_greater_than_is_operator_p
3282           = parser->greater_than_is_operator_p;
3283         parser->greater_than_is_operator_p = true;
3284         /* If we see `( { ' then we are looking at the beginning of
3285            a GNU statement-expression.  */
3286         if (cp_parser_allow_gnu_extensions_p (parser)
3287             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3288           {
3289             /* Statement-expressions are not allowed by the standard.  */
3290             pedwarn (token->location, OPT_pedantic, 
3291                      "ISO C++ forbids braced-groups within expressions");
3292
3293             /* And they're not allowed outside of a function-body; you
3294                cannot, for example, write:
3295
3296                  int i = ({ int j = 3; j + 1; });
3297
3298                at class or namespace scope.  */
3299             if (!parser->in_function_body
3300                 || parser->in_template_argument_list_p)
3301               {
3302                 error_at (token->location,
3303                           "statement-expressions are not allowed outside "
3304                           "functions nor in template-argument lists");
3305                 cp_parser_skip_to_end_of_block_or_statement (parser);
3306                 expr = error_mark_node;
3307               }
3308             else
3309               {
3310                 /* Start the statement-expression.  */
3311                 expr = begin_stmt_expr ();
3312                 /* Parse the compound-statement.  */
3313                 cp_parser_compound_statement (parser, expr, false);
3314                 /* Finish up.  */
3315                 expr = finish_stmt_expr (expr, false);
3316               }
3317           }
3318         else
3319           {
3320             /* Parse the parenthesized expression.  */
3321             expr = cp_parser_expression (parser, cast_p, idk);
3322             /* Let the front end know that this expression was
3323                enclosed in parentheses. This matters in case, for
3324                example, the expression is of the form `A::B', since
3325                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3326                not.  */
3327             finish_parenthesized_expr (expr);
3328           }
3329         /* The `>' token might be the end of a template-id or
3330            template-parameter-list now.  */
3331         parser->greater_than_is_operator_p
3332           = saved_greater_than_is_operator_p;
3333         /* Consume the `)'.  */
3334         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3335           cp_parser_skip_to_end_of_statement (parser);
3336
3337         return expr;
3338       }
3339
3340     case CPP_OPEN_SQUARE:
3341       if (c_dialect_objc ())
3342         /* We have an Objective-C++ message. */
3343         return cp_parser_objc_expression (parser);
3344       maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3345       return cp_parser_lambda_expression (parser);
3346
3347     case CPP_OBJC_STRING:
3348       if (c_dialect_objc ())
3349         /* We have an Objective-C++ string literal. */
3350         return cp_parser_objc_expression (parser);
3351       cp_parser_error (parser, "expected primary-expression");
3352       return error_mark_node;
3353
3354     case CPP_KEYWORD:
3355       switch (token->keyword)
3356         {
3357           /* These two are the boolean literals.  */
3358         case RID_TRUE:
3359           cp_lexer_consume_token (parser->lexer);
3360           return boolean_true_node;
3361         case RID_FALSE:
3362           cp_lexer_consume_token (parser->lexer);
3363           return boolean_false_node;
3364
3365           /* The `__null' literal.  */
3366         case RID_NULL:
3367           cp_lexer_consume_token (parser->lexer);
3368           return null_node;
3369
3370           /* Recognize the `this' keyword.  */
3371         case RID_THIS:
3372           cp_lexer_consume_token (parser->lexer);
3373           if (parser->local_variables_forbidden_p)
3374             {
3375               error_at (token->location,
3376                         "%<this%> may not be used in this context");
3377               return error_mark_node;
3378             }
3379           /* Pointers cannot appear in constant-expressions.  */
3380           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3381             return error_mark_node;
3382           return finish_this_expr ();
3383
3384           /* The `operator' keyword can be the beginning of an
3385              id-expression.  */
3386         case RID_OPERATOR:
3387           goto id_expression;
3388
3389         case RID_FUNCTION_NAME:
3390         case RID_PRETTY_FUNCTION_NAME:
3391         case RID_C99_FUNCTION_NAME:
3392           {
3393             const char *name;
3394
3395             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3396                __func__ are the names of variables -- but they are
3397                treated specially.  Therefore, they are handled here,
3398                rather than relying on the generic id-expression logic
3399                below.  Grammatically, these names are id-expressions.
3400
3401                Consume the token.  */
3402             token = cp_lexer_consume_token (parser->lexer);
3403
3404             switch (token->keyword)
3405               {
3406               case RID_FUNCTION_NAME:
3407                 name = "%<__FUNCTION__%>";
3408                 break;
3409               case RID_PRETTY_FUNCTION_NAME:
3410                 name = "%<__PRETTY_FUNCTION__%>";
3411                 break;
3412               case RID_C99_FUNCTION_NAME:
3413                 name = "%<__func__%>";
3414                 break;
3415               default:
3416                 gcc_unreachable ();
3417               }
3418
3419             if (cp_parser_non_integral_constant_expression (parser, name))
3420               return error_mark_node;
3421
3422             /* Look up the name.  */
3423             return finish_fname (token->u.value);
3424           }
3425
3426         case RID_VA_ARG:
3427           {
3428             tree expression;
3429             tree type;
3430
3431             /* The `__builtin_va_arg' construct is used to handle
3432                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3433             cp_lexer_consume_token (parser->lexer);
3434             /* Look for the opening `('.  */
3435             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3436             /* Now, parse the assignment-expression.  */
3437             expression = cp_parser_assignment_expression (parser,
3438                                                           /*cast_p=*/false, NULL);
3439             /* Look for the `,'.  */
3440             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3441             /* Parse the type-id.  */
3442             type = cp_parser_type_id (parser);
3443             /* Look for the closing `)'.  */
3444             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3445             /* Using `va_arg' in a constant-expression is not
3446                allowed.  */
3447             if (cp_parser_non_integral_constant_expression (parser,
3448                                                             "%<va_arg%>"))
3449               return error_mark_node;
3450             return build_x_va_arg (expression, type);
3451           }
3452
3453         case RID_OFFSETOF:
3454           return cp_parser_builtin_offsetof (parser);
3455
3456         case RID_HAS_NOTHROW_ASSIGN:
3457         case RID_HAS_NOTHROW_CONSTRUCTOR:
3458         case RID_HAS_NOTHROW_COPY:        
3459         case RID_HAS_TRIVIAL_ASSIGN:
3460         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3461         case RID_HAS_TRIVIAL_COPY:        
3462         case RID_HAS_TRIVIAL_DESTRUCTOR:
3463         case RID_HAS_VIRTUAL_DESTRUCTOR:
3464         case RID_IS_ABSTRACT:
3465         case RID_IS_BASE_OF:
3466         case RID_IS_CLASS:
3467         case RID_IS_CONVERTIBLE_TO:
3468         case RID_IS_EMPTY:
3469         case RID_IS_ENUM:
3470         case RID_IS_POD:
3471         case RID_IS_POLYMORPHIC:
3472         case RID_IS_STD_LAYOUT:
3473         case RID_IS_TRIVIAL:
3474         case RID_IS_UNION:
3475           return cp_parser_trait_expr (parser, token->keyword);
3476
3477         /* Objective-C++ expressions.  */
3478         case RID_AT_ENCODE:
3479         case RID_AT_PROTOCOL:
3480         case RID_AT_SELECTOR:
3481           return cp_parser_objc_expression (parser);
3482
3483         default:
3484           cp_parser_error (parser, "expected primary-expression");
3485           return error_mark_node;
3486         }
3487
3488       /* An id-expression can start with either an identifier, a
3489          `::' as the beginning of a qualified-id, or the "operator"
3490          keyword.  */
3491     case CPP_NAME:
3492     case CPP_SCOPE:
3493     case CPP_TEMPLATE_ID:
3494     case CPP_NESTED_NAME_SPECIFIER:
3495       {
3496         tree id_expression;
3497         tree decl;
3498         const char *error_msg;
3499         bool template_p;
3500         bool done;
3501         cp_token *id_expr_token;
3502
3503       id_expression:
3504         /* Parse the id-expression.  */
3505         id_expression
3506           = cp_parser_id_expression (parser,
3507                                      /*template_keyword_p=*/false,
3508                                      /*check_dependency_p=*/true,
3509                                      &template_p,
3510                                      /*declarator_p=*/false,
3511                                      /*optional_p=*/false);
3512         if (id_expression == error_mark_node)
3513           return error_mark_node;
3514         id_expr_token = token;
3515         token = cp_lexer_peek_token (parser->lexer);
3516         done = (token->type != CPP_OPEN_SQUARE
3517                 && token->type != CPP_OPEN_PAREN
3518                 && token->type != CPP_DOT
3519                 && token->type != CPP_DEREF
3520                 && token->type != CPP_PLUS_PLUS
3521                 && token->type != CPP_MINUS_MINUS);
3522         /* If we have a template-id, then no further lookup is
3523            required.  If the template-id was for a template-class, we
3524            will sometimes have a TYPE_DECL at this point.  */
3525         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3526                  || TREE_CODE (id_expression) == TYPE_DECL)
3527           decl = id_expression;
3528         /* Look up the name.  */
3529         else
3530           {
3531             tree ambiguous_decls;
3532
3533             /* If we already know that this lookup is ambiguous, then
3534                we've already issued an error message; there's no reason
3535                to check again.  */
3536             if (id_expr_token->type == CPP_NAME
3537                 && id_expr_token->ambiguous_p)
3538               {
3539                 cp_parser_simulate_error (parser);
3540                 return error_mark_node;
3541               }
3542
3543             decl = cp_parser_lookup_name (parser, id_expression,
3544                                           none_type,
3545                                           template_p,
3546                                           /*is_namespace=*/false,
3547                                           /*check_dependency=*/true,
3548                                           &ambiguous_decls,
3549                                           id_expr_token->location);
3550             /* If the lookup was ambiguous, an error will already have
3551                been issued.  */
3552             if (ambiguous_decls)
3553               return error_mark_node;
3554
3555             /* In Objective-C++, an instance variable (ivar) may be preferred
3556                to whatever cp_parser_lookup_name() found.  */
3557             decl = objc_lookup_ivar (decl, id_expression);
3558
3559             /* If name lookup gives us a SCOPE_REF, then the
3560                qualifying scope was dependent.  */
3561             if (TREE_CODE (decl) == SCOPE_REF)
3562               {
3563                 /* At this point, we do not know if DECL is a valid
3564                    integral constant expression.  We assume that it is
3565                    in fact such an expression, so that code like:
3566
3567                       template <int N> struct A {
3568                         int a[B<N>::i];
3569                       };
3570                      
3571                    is accepted.  At template-instantiation time, we
3572                    will check that B<N>::i is actually a constant.  */
3573                 return decl;
3574               }
3575             /* Check to see if DECL is a local variable in a context
3576                where that is forbidden.  */
3577             if (parser->local_variables_forbidden_p
3578                 && local_variable_p (decl))
3579               {
3580                 /* It might be that we only found DECL because we are
3581                    trying to be generous with pre-ISO scoping rules.
3582                    For example, consider:
3583
3584                      int i;
3585                      void g() {
3586                        for (int i = 0; i < 10; ++i) {}
3587                        extern void f(int j = i);
3588                      }
3589
3590                    Here, name look up will originally find the out
3591                    of scope `i'.  We need to issue a warning message,
3592                    but then use the global `i'.  */
3593                 decl = check_for_out_of_scope_variable (decl);
3594                 if (local_variable_p (decl))
3595                   {
3596                     error_at (id_expr_token->location,
3597                               "local variable %qD may not appear in this context",
3598                               decl);
3599                     return error_mark_node;
3600                   }
3601               }
3602           }
3603
3604         decl = (finish_id_expression
3605                 (id_expression, decl, parser->scope,
3606                  idk,
3607                  parser->integral_constant_expression_p,
3608                  parser->allow_non_integral_constant_expression_p,
3609                  &parser->non_integral_constant_expression_p,
3610                  template_p, done, address_p,
3611                  template_arg_p,
3612                  &error_msg,
3613                  id_expr_token->location));
3614         if (error_msg)
3615           cp_parser_error (parser, error_msg);
3616         return decl;
3617       }
3618
3619       /* Anything else is an error.  */
3620     default:
3621       cp_parser_error (parser, "expected primary-expression");
3622       return error_mark_node;
3623     }
3624 }
3625
3626 /* Parse an id-expression.
3627
3628    id-expression:
3629      unqualified-id
3630      qualified-id
3631
3632    qualified-id:
3633      :: [opt] nested-name-specifier template [opt] unqualified-id
3634      :: identifier
3635      :: operator-function-id
3636      :: template-id
3637
3638    Return a representation of the unqualified portion of the
3639    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3640    a `::' or nested-name-specifier.
3641
3642    Often, if the id-expression was a qualified-id, the caller will
3643    want to make a SCOPE_REF to represent the qualified-id.  This
3644    function does not do this in order to avoid wastefully creating
3645    SCOPE_REFs when they are not required.
3646
3647    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3648    `template' keyword.
3649
3650    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3651    uninstantiated templates.
3652
3653    If *TEMPLATE_P is non-NULL, it is set to true iff the
3654    `template' keyword is used to explicitly indicate that the entity
3655    named is a template.
3656
3657    If DECLARATOR_P is true, the id-expression is appearing as part of
3658    a declarator, rather than as part of an expression.  */
3659
3660 static tree
3661 cp_parser_id_expression (cp_parser *parser,
3662                          bool template_keyword_p,
3663                          bool check_dependency_p,
3664                          bool *template_p,
3665                          bool declarator_p,
3666                          bool optional_p)
3667 {
3668   bool global_scope_p;
3669   bool nested_name_specifier_p;
3670
3671   /* Assume the `template' keyword was not used.  */
3672   if (template_p)
3673     *template_p = template_keyword_p;
3674
3675   /* Look for the optional `::' operator.  */
3676   global_scope_p
3677     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3678        != NULL_TREE);
3679   /* Look for the optional nested-name-specifier.  */
3680   nested_name_specifier_p
3681     = (cp_parser_nested_name_specifier_opt (parser,
3682                                             /*typename_keyword_p=*/false,
3683                                             check_dependency_p,
3684                                             /*type_p=*/false,
3685                                             declarator_p)
3686        != NULL_TREE);
3687   /* If there is a nested-name-specifier, then we are looking at
3688      the first qualified-id production.  */
3689   if (nested_name_specifier_p)
3690     {
3691       tree saved_scope;
3692       tree saved_object_scope;
3693       tree saved_qualifying_scope;
3694       tree unqualified_id;
3695       bool is_template;
3696
3697       /* See if the next token is the `template' keyword.  */
3698       if (!template_p)
3699         template_p = &is_template;
3700       *template_p = cp_parser_optional_template_keyword (parser);
3701       /* Name lookup we do during the processing of the
3702          unqualified-id might obliterate SCOPE.  */
3703       saved_scope = parser->scope;
3704       saved_object_scope = parser->object_scope;
3705       saved_qualifying_scope = parser->qualifying_scope;
3706       /* Process the final unqualified-id.  */
3707       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3708                                                  check_dependency_p,
3709                                                  declarator_p,
3710                                                  /*optional_p=*/false);
3711       /* Restore the SAVED_SCOPE for our caller.  */
3712       parser->scope = saved_scope;
3713       parser->object_scope = saved_object_scope;
3714       parser->qualifying_scope = saved_qualifying_scope;
3715
3716       return unqualified_id;
3717     }
3718   /* Otherwise, if we are in global scope, then we are looking at one
3719      of the other qualified-id productions.  */
3720   else if (global_scope_p)
3721     {
3722       cp_token *token;
3723       tree id;
3724
3725       /* Peek at the next token.  */
3726       token = cp_lexer_peek_token (parser->lexer);
3727
3728       /* If it's an identifier, and the next token is not a "<", then
3729          we can avoid the template-id case.  This is an optimization
3730          for this common case.  */
3731       if (token->type == CPP_NAME
3732           && !cp_parser_nth_token_starts_template_argument_list_p
3733                (parser, 2))
3734         return cp_parser_identifier (parser);
3735
3736       cp_parser_parse_tentatively (parser);
3737       /* Try a template-id.  */
3738       id = cp_parser_template_id (parser,
3739                                   /*template_keyword_p=*/false,
3740                                   /*check_dependency_p=*/true,
3741                                   declarator_p);
3742       /* If that worked, we're done.  */
3743       if (cp_parser_parse_definitely (parser))
3744         return id;
3745
3746       /* Peek at the next token.  (Changes in the token buffer may
3747          have invalidated the pointer obtained above.)  */
3748       token = cp_lexer_peek_token (parser->lexer);
3749
3750       switch (token->type)
3751         {
3752         case CPP_NAME:
3753           return cp_parser_identifier (parser);
3754
3755         case CPP_KEYWORD:
3756           if (token->keyword == RID_OPERATOR)
3757             return cp_parser_operator_function_id (parser);
3758           /* Fall through.  */
3759
3760         default:
3761           cp_parser_error (parser, "expected id-expression");
3762           return error_mark_node;
3763         }
3764     }
3765   else
3766     return cp_parser_unqualified_id (parser, template_keyword_p,
3767                                      /*check_dependency_p=*/true,
3768                                      declarator_p,
3769                                      optional_p);
3770 }
3771
3772 /* Parse an unqualified-id.
3773
3774    unqualified-id:
3775      identifier
3776      operator-function-id
3777      conversion-function-id
3778      ~ class-name
3779      template-id
3780
3781    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3782    keyword, in a construct like `A::template ...'.
3783
3784    Returns a representation of unqualified-id.  For the `identifier'
3785    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3786    production a BIT_NOT_EXPR is returned; the operand of the
3787    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3788    other productions, see the documentation accompanying the
3789    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3790    names are looked up in uninstantiated templates.  If DECLARATOR_P
3791    is true, the unqualified-id is appearing as part of a declarator,
3792    rather than as part of an expression.  */
3793
3794 static tree
3795 cp_parser_unqualified_id (cp_parser* parser,
3796                           bool template_keyword_p,
3797                           bool check_dependency_p,
3798                           bool declarator_p,
3799                           bool optional_p)
3800 {
3801   cp_token *token;
3802
3803   /* Peek at the next token.  */
3804   token = cp_lexer_peek_token (parser->lexer);
3805
3806   switch (token->type)
3807     {
3808     case CPP_NAME:
3809       {
3810         tree id;
3811
3812         /* We don't know yet whether or not this will be a
3813            template-id.  */
3814         cp_parser_parse_tentatively (parser);
3815         /* Try a template-id.  */
3816         id = cp_parser_template_id (parser, template_keyword_p,
3817                                     check_dependency_p,
3818                                     declarator_p);
3819         /* If it worked, we're done.  */
3820         if (cp_parser_parse_definitely (parser))
3821           return id;
3822         /* Otherwise, it's an ordinary identifier.  */
3823         return cp_parser_identifier (parser);
3824       }
3825
3826     case CPP_TEMPLATE_ID:
3827       return cp_parser_template_id (parser, template_keyword_p,
3828                                     check_dependency_p,
3829                                     declarator_p);
3830
3831     case CPP_COMPL:
3832       {
3833         tree type_decl;
3834         tree qualifying_scope;
3835         tree object_scope;
3836         tree scope;
3837         bool done;
3838
3839         /* Consume the `~' token.  */
3840         cp_lexer_consume_token (parser->lexer);
3841         /* Parse the class-name.  The standard, as written, seems to
3842            say that:
3843
3844              template <typename T> struct S { ~S (); };
3845              template <typename T> S<T>::~S() {}
3846
3847            is invalid, since `~' must be followed by a class-name, but
3848            `S<T>' is dependent, and so not known to be a class.
3849            That's not right; we need to look in uninstantiated
3850            templates.  A further complication arises from:
3851
3852              template <typename T> void f(T t) {
3853                t.T::~T();
3854              }
3855
3856            Here, it is not possible to look up `T' in the scope of `T'
3857            itself.  We must look in both the current scope, and the
3858            scope of the containing complete expression.
3859
3860            Yet another issue is:
3861
3862              struct S {
3863                int S;
3864                ~S();
3865              };
3866
3867              S::~S() {}
3868
3869            The standard does not seem to say that the `S' in `~S'
3870            should refer to the type `S' and not the data member
3871            `S::S'.  */
3872
3873         /* DR 244 says that we look up the name after the "~" in the
3874            same scope as we looked up the qualifying name.  That idea
3875            isn't fully worked out; it's more complicated than that.  */
3876         scope = parser->scope;
3877         object_scope = parser->object_scope;
3878         qualifying_scope = parser->qualifying_scope;
3879
3880         /* Check for invalid scopes.  */
3881         if (scope == error_mark_node)
3882           {
3883             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3884               cp_lexer_consume_token (parser->lexer);
3885             return error_mark_node;
3886           }
3887         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3888           {
3889             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3890               error_at (token->location,
3891                         "scope %qT before %<~%> is not a class-name",
3892                         scope);
3893             cp_parser_simulate_error (parser);
3894             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3895               cp_lexer_consume_token (parser->lexer);
3896             return error_mark_node;
3897           }
3898         gcc_assert (!scope || TYPE_P (scope));
3899
3900         /* If the name is of the form "X::~X" it's OK even if X is a
3901            typedef.  */
3902         token = cp_lexer_peek_token (parser->lexer);
3903         if (scope
3904             && token->type == CPP_NAME
3905             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3906                 != CPP_LESS)
3907             && (token->u.value == TYPE_IDENTIFIER (scope)
3908                 || constructor_name_p (token->u.value, scope)))
3909           {
3910             cp_lexer_consume_token (parser->lexer);
3911             return build_nt (BIT_NOT_EXPR, scope);
3912           }
3913
3914         /* If there was an explicit qualification (S::~T), first look
3915            in the scope given by the qualification (i.e., S).
3916
3917            Note: in the calls to cp_parser_class_name below we pass
3918            typename_type so that lookup finds the injected-class-name
3919            rather than the constructor.  */
3920         done = false;
3921         type_decl = NULL_TREE;
3922         if (scope)
3923           {
3924             cp_parser_parse_tentatively (parser);
3925             type_decl = cp_parser_class_name (parser,
3926                                               /*typename_keyword_p=*/false,
3927                                               /*template_keyword_p=*/false,
3928                                               typename_type,
3929                                               /*check_dependency=*/false,
3930                                               /*class_head_p=*/false,
3931                                               declarator_p);
3932             if (cp_parser_parse_definitely (parser))
3933               done = true;
3934           }
3935         /* In "N::S::~S", look in "N" as well.  */
3936         if (!done && scope && qualifying_scope)
3937           {
3938             cp_parser_parse_tentatively (parser);
3939             parser->scope = qualifying_scope;
3940             parser->object_scope = NULL_TREE;
3941             parser->qualifying_scope = NULL_TREE;
3942             type_decl
3943               = cp_parser_class_name (parser,
3944                                       /*typename_keyword_p=*/false,
3945                                       /*template_keyword_p=*/false,
3946                                       typename_type,
3947                                       /*check_dependency=*/false,
3948                                       /*class_head_p=*/false,
3949                                       declarator_p);
3950             if (cp_parser_parse_definitely (parser))
3951               done = true;
3952           }
3953         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3954         else if (!done && object_scope)
3955           {
3956             cp_parser_parse_tentatively (parser);
3957             parser->scope = object_scope;
3958             parser->object_scope = NULL_TREE;
3959             parser->qualifying_scope = NULL_TREE;
3960             type_decl
3961               = cp_parser_class_name (parser,
3962                                       /*typename_keyword_p=*/false,
3963                                       /*template_keyword_p=*/false,
3964                                       typename_type,
3965                                       /*check_dependency=*/false,
3966                                       /*class_head_p=*/false,
3967                                       declarator_p);
3968             if (cp_parser_parse_definitely (parser))
3969               done = true;
3970           }
3971         /* Look in the surrounding context.  */
3972         if (!done)
3973           {
3974             parser->scope = NULL_TREE;
3975             parser->object_scope = NULL_TREE;
3976             parser->qualifying_scope = NULL_TREE;
3977             if (processing_template_decl)
3978               cp_parser_parse_tentatively (parser);
3979             type_decl
3980               = cp_parser_class_name (parser,
3981                                       /*typename_keyword_p=*/false,
3982                                       /*template_keyword_p=*/false,
3983                                       typename_type,
3984                                       /*check_dependency=*/false,
3985                                       /*class_head_p=*/false,
3986                                       declarator_p);
3987             if (processing_template_decl
3988                 && ! cp_parser_parse_definitely (parser))
3989               {
3990                 /* We couldn't find a type with this name, so just accept
3991                    it and check for a match at instantiation time.  */
3992                 type_decl = cp_parser_identifier (parser);
3993                 if (type_decl != error_mark_node)
3994                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3995                 return type_decl;
3996               }
3997           }
3998         /* If an error occurred, assume that the name of the
3999            destructor is the same as the name of the qualifying
4000            class.  That allows us to keep parsing after running
4001            into ill-formed destructor names.  */
4002         if (type_decl == error_mark_node && scope)
4003           return build_nt (BIT_NOT_EXPR, scope);
4004         else if (type_decl == error_mark_node)
4005           return error_mark_node;
4006
4007         /* Check that destructor name and scope match.  */
4008         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4009           {
4010             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4011               error_at (token->location,
4012                         "declaration of %<~%T%> as member of %qT",
4013                         type_decl, scope);
4014             cp_parser_simulate_error (parser);
4015             return error_mark_node;
4016           }
4017
4018         /* [class.dtor]
4019
4020            A typedef-name that names a class shall not be used as the
4021            identifier in the declarator for a destructor declaration.  */
4022         if (declarator_p
4023             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4024             && !DECL_SELF_REFERENCE_P (type_decl)
4025             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4026           error_at (token->location,
4027                     "typedef-name %qD used as destructor declarator",
4028                     type_decl);
4029
4030         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4031       }
4032
4033     case CPP_KEYWORD:
4034       if (token->keyword == RID_OPERATOR)
4035         {
4036           tree id;
4037
4038           /* This could be a template-id, so we try that first.  */
4039           cp_parser_parse_tentatively (parser);
4040           /* Try a template-id.  */
4041           id = cp_parser_template_id (parser, template_keyword_p,
4042                                       /*check_dependency_p=*/true,
4043                                       declarator_p);
4044           /* If that worked, we're done.  */
4045           if (cp_parser_parse_definitely (parser))
4046             return id;
4047           /* We still don't know whether we're looking at an
4048              operator-function-id or a conversion-function-id.  */
4049           cp_parser_parse_tentatively (parser);
4050           /* Try an operator-function-id.  */
4051           id = cp_parser_operator_function_id (parser);
4052           /* If that didn't work, try a conversion-function-id.  */
4053           if (!cp_parser_parse_definitely (parser))
4054             id = cp_parser_conversion_function_id (parser);
4055
4056           return id;
4057         }
4058       /* Fall through.  */
4059
4060     default:
4061       if (optional_p)
4062         return NULL_TREE;
4063       cp_parser_error (parser, "expected unqualified-id");
4064       return error_mark_node;
4065     }
4066 }
4067
4068 /* Parse an (optional) nested-name-specifier.
4069
4070    nested-name-specifier: [C++98]
4071      class-or-namespace-name :: nested-name-specifier [opt]
4072      class-or-namespace-name :: template nested-name-specifier [opt]
4073
4074    nested-name-specifier: [C++0x]
4075      type-name ::
4076      namespace-name ::
4077      nested-name-specifier identifier ::
4078      nested-name-specifier template [opt] simple-template-id ::
4079
4080    PARSER->SCOPE should be set appropriately before this function is
4081    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4082    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4083    in name lookups.
4084
4085    Sets PARSER->SCOPE to the class (TYPE) or namespace
4086    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4087    it unchanged if there is no nested-name-specifier.  Returns the new
4088    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4089
4090    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4091    part of a declaration and/or decl-specifier.  */
4092
4093 static tree
4094 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4095                                      bool typename_keyword_p,
4096                                      bool check_dependency_p,
4097                                      bool type_p,
4098                                      bool is_declaration)
4099 {
4100   bool success = false;
4101   cp_token_position start = 0;
4102   cp_token *token;
4103
4104   /* Remember where the nested-name-specifier starts.  */
4105   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4106     {
4107       start = cp_lexer_token_position (parser->lexer, false);
4108       push_deferring_access_checks (dk_deferred);
4109     }
4110
4111   while (true)
4112     {
4113       tree new_scope;
4114       tree old_scope;
4115       tree saved_qualifying_scope;
4116       bool template_keyword_p;
4117
4118       /* Spot cases that cannot be the beginning of a
4119          nested-name-specifier.  */
4120       token = cp_lexer_peek_token (parser->lexer);
4121
4122       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4123          the already parsed nested-name-specifier.  */
4124       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4125         {
4126           /* Grab the nested-name-specifier and continue the loop.  */
4127           cp_parser_pre_parsed_nested_name_specifier (parser);
4128           /* If we originally encountered this nested-name-specifier
4129              with IS_DECLARATION set to false, we will not have
4130              resolved TYPENAME_TYPEs, so we must do so here.  */
4131           if (is_declaration
4132               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4133             {
4134               new_scope = resolve_typename_type (parser->scope,
4135                                                  /*only_current_p=*/false);
4136               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4137                 parser->scope = new_scope;
4138             }
4139           success = true;
4140           continue;
4141         }
4142
4143       /* Spot cases that cannot be the beginning of a
4144          nested-name-specifier.  On the second and subsequent times
4145          through the loop, we look for the `template' keyword.  */
4146       if (success && token->keyword == RID_TEMPLATE)
4147         ;
4148       /* A template-id can start a nested-name-specifier.  */
4149       else if (token->type == CPP_TEMPLATE_ID)
4150         ;
4151       else
4152         {
4153           /* If the next token is not an identifier, then it is
4154              definitely not a type-name or namespace-name.  */
4155           if (token->type != CPP_NAME)
4156             break;
4157           /* If the following token is neither a `<' (to begin a
4158              template-id), nor a `::', then we are not looking at a
4159              nested-name-specifier.  */
4160           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4161           if (token->type != CPP_SCOPE
4162               && !cp_parser_nth_token_starts_template_argument_list_p
4163                   (parser, 2))
4164             break;
4165         }
4166
4167       /* The nested-name-specifier is optional, so we parse
4168          tentatively.  */
4169       cp_parser_parse_tentatively (parser);
4170
4171       /* Look for the optional `template' keyword, if this isn't the
4172          first time through the loop.  */
4173       if (success)
4174         template_keyword_p = cp_parser_optional_template_keyword (parser);
4175       else
4176         template_keyword_p = false;
4177
4178       /* Save the old scope since the name lookup we are about to do
4179          might destroy it.  */
4180       old_scope = parser->scope;
4181       saved_qualifying_scope = parser->qualifying_scope;
4182       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4183          look up names in "X<T>::I" in order to determine that "Y" is
4184          a template.  So, if we have a typename at this point, we make
4185          an effort to look through it.  */
4186       if (is_declaration
4187           && !typename_keyword_p
4188           && parser->scope
4189           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4190         parser->scope = resolve_typename_type (parser->scope,
4191                                                /*only_current_p=*/false);
4192       /* Parse the qualifying entity.  */
4193       new_scope
4194         = cp_parser_qualifying_entity (parser,
4195                                        typename_keyword_p,
4196                                        template_keyword_p,
4197                                        check_dependency_p,
4198                                        type_p,
4199                                        is_declaration);
4200       /* Look for the `::' token.  */
4201       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4202
4203       /* If we found what we wanted, we keep going; otherwise, we're
4204          done.  */
4205       if (!cp_parser_parse_definitely (parser))
4206         {
4207           bool error_p = false;
4208
4209           /* Restore the OLD_SCOPE since it was valid before the
4210              failed attempt at finding the last
4211              class-or-namespace-name.  */
4212           parser->scope = old_scope;
4213           parser->qualifying_scope = saved_qualifying_scope;
4214           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4215             break;
4216           /* If the next token is an identifier, and the one after
4217              that is a `::', then any valid interpretation would have
4218              found a class-or-namespace-name.  */
4219           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4220                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4221                      == CPP_SCOPE)
4222                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4223                      != CPP_COMPL))
4224             {
4225               token = cp_lexer_consume_token (parser->lexer);
4226               if (!error_p)
4227                 {
4228                   if (!token->ambiguous_p)
4229                     {
4230                       tree decl;
4231                       tree ambiguous_decls;
4232
4233                       decl = cp_parser_lookup_name (parser, token->u.value,
4234                                                     none_type,
4235                                                     /*is_template=*/false,
4236                                                     /*is_namespace=*/false,
4237                                                     /*check_dependency=*/true,
4238                                                     &ambiguous_decls,
4239                                                     token->location);
4240                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4241                         error_at (token->location,
4242                                   "%qD used without template parameters",
4243                                   decl);
4244                       else if (ambiguous_decls)
4245                         {
4246                           error_at (token->location,
4247                                     "reference to %qD is ambiguous",
4248                                     token->u.value);
4249                           print_candidates (ambiguous_decls);
4250                           decl = error_mark_node;
4251                         }
4252                       else
4253                         {
4254                           const char* msg = "is not a class or namespace";
4255                           if (cxx_dialect != cxx98)
4256                             msg = "is not a class, namespace, or enumeration";
4257                           cp_parser_name_lookup_error
4258                             (parser, token->u.value, decl, msg,
4259                              token->location);
4260                         }
4261                     }
4262                   parser->scope = error_mark_node;
4263                   error_p = true;
4264                   /* Treat this as a successful nested-name-specifier
4265                      due to:
4266
4267                      [basic.lookup.qual]
4268
4269                      If the name found is not a class-name (clause
4270                      _class_) or namespace-name (_namespace.def_), the
4271                      program is ill-formed.  */
4272                   success = true;
4273                 }
4274               cp_lexer_consume_token (parser->lexer);
4275             }
4276           break;
4277         }
4278       /* We've found one valid nested-name-specifier.  */
4279       success = true;
4280       /* Name lookup always gives us a DECL.  */
4281       if (TREE_CODE (new_scope) == TYPE_DECL)
4282         new_scope = TREE_TYPE (new_scope);
4283       /* Uses of "template" must be followed by actual templates.  */
4284       if (template_keyword_p
4285           && !(CLASS_TYPE_P (new_scope)
4286                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4287                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4288                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4289           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4290                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4291                    == TEMPLATE_ID_EXPR)))
4292         permerror (input_location, TYPE_P (new_scope)
4293                    ? "%qT is not a template"
4294                    : "%qD is not a template",
4295                    new_scope);
4296       /* If it is a class scope, try to complete it; we are about to
4297          be looking up names inside the class.  */
4298       if (TYPE_P (new_scope)
4299           /* Since checking types for dependency can be expensive,
4300              avoid doing it if the type is already complete.  */
4301           && !COMPLETE_TYPE_P (new_scope)
4302           /* Do not try to complete dependent types.  */
4303           && !dependent_type_p (new_scope))
4304         {
4305           new_scope = complete_type (new_scope);
4306           /* If it is a typedef to current class, use the current
4307              class instead, as the typedef won't have any names inside
4308              it yet.  */
4309           if (!COMPLETE_TYPE_P (new_scope)
4310               && currently_open_class (new_scope))
4311             new_scope = TYPE_MAIN_VARIANT (new_scope);
4312         }
4313       /* Make sure we look in the right scope the next time through
4314          the loop.  */
4315       parser->scope = new_scope;
4316     }
4317
4318   /* If parsing tentatively, replace the sequence of tokens that makes
4319      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4320      token.  That way, should we re-parse the token stream, we will
4321      not have to repeat the effort required to do the parse, nor will
4322      we issue duplicate error messages.  */
4323   if (success && start)
4324     {
4325       cp_token *token;
4326
4327       token = cp_lexer_token_at (parser->lexer, start);
4328       /* Reset the contents of the START token.  */
4329       token->type = CPP_NESTED_NAME_SPECIFIER;
4330       /* Retrieve any deferred checks.  Do not pop this access checks yet
4331          so the memory will not be reclaimed during token replacing below.  */
4332       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4333       token->u.tree_check_value->value = parser->scope;
4334       token->u.tree_check_value->checks = get_deferred_access_checks ();
4335       token->u.tree_check_value->qualifying_scope =
4336         parser->qualifying_scope;
4337       token->keyword = RID_MAX;
4338
4339       /* Purge all subsequent tokens.  */
4340       cp_lexer_purge_tokens_after (parser->lexer, start);
4341     }
4342
4343   if (start)
4344     pop_to_parent_deferring_access_checks ();
4345
4346   return success ? parser->scope : NULL_TREE;
4347 }
4348
4349 /* Parse a nested-name-specifier.  See
4350    cp_parser_nested_name_specifier_opt for details.  This function
4351    behaves identically, except that it will an issue an error if no
4352    nested-name-specifier is present.  */
4353
4354 static tree
4355 cp_parser_nested_name_specifier (cp_parser *parser,
4356                                  bool typename_keyword_p,
4357                                  bool check_dependency_p,
4358                                  bool type_p,
4359                                  bool is_declaration)
4360 {
4361   tree scope;
4362
4363   /* Look for the nested-name-specifier.  */
4364   scope = cp_parser_nested_name_specifier_opt (parser,
4365                                                typename_keyword_p,
4366                                                check_dependency_p,
4367                                                type_p,
4368                                                is_declaration);
4369   /* If it was not present, issue an error message.  */
4370   if (!scope)
4371     {
4372       cp_parser_error (parser, "expected nested-name-specifier");
4373       parser->scope = NULL_TREE;
4374     }
4375
4376   return scope;
4377 }
4378
4379 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4380    this is either a class-name or a namespace-name (which corresponds
4381    to the class-or-namespace-name production in the grammar). For
4382    C++0x, it can also be a type-name that refers to an enumeration
4383    type.
4384
4385    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4386    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4387    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4388    TYPE_P is TRUE iff the next name should be taken as a class-name,
4389    even the same name is declared to be another entity in the same
4390    scope.
4391
4392    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4393    specified by the class-or-namespace-name.  If neither is found the
4394    ERROR_MARK_NODE is returned.  */
4395
4396 static tree
4397 cp_parser_qualifying_entity (cp_parser *parser,
4398                              bool typename_keyword_p,
4399                              bool template_keyword_p,
4400                              bool check_dependency_p,
4401                              bool type_p,
4402                              bool is_declaration)
4403 {
4404   tree saved_scope;
4405   tree saved_qualifying_scope;
4406   tree saved_object_scope;
4407   tree scope;
4408   bool only_class_p;
4409   bool successful_parse_p;
4410
4411   /* Before we try to parse the class-name, we must save away the
4412      current PARSER->SCOPE since cp_parser_class_name will destroy
4413      it.  */
4414   saved_scope = parser->scope;
4415   saved_qualifying_scope = parser->qualifying_scope;
4416   saved_object_scope = parser->object_scope;
4417   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4418      there is no need to look for a namespace-name.  */
4419   only_class_p = template_keyword_p 
4420     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4421   if (!only_class_p)
4422     cp_parser_parse_tentatively (parser);
4423   scope = cp_parser_class_name (parser,
4424                                 typename_keyword_p,
4425                                 template_keyword_p,
4426                                 type_p ? class_type : none_type,
4427                                 check_dependency_p,
4428                                 /*class_head_p=*/false,
4429                                 is_declaration);
4430   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4431   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4432   if (!only_class_p 
4433       && cxx_dialect != cxx98
4434       && !successful_parse_p)
4435     {
4436       /* Restore the saved scope.  */
4437       parser->scope = saved_scope;
4438       parser->qualifying_scope = saved_qualifying_scope;
4439       parser->object_scope = saved_object_scope;
4440
4441       /* Parse tentatively.  */
4442       cp_parser_parse_tentatively (parser);
4443      
4444       /* Parse a typedef-name or enum-name.  */
4445       scope = cp_parser_nonclass_name (parser);
4446
4447       /* "If the name found does not designate a namespace or a class,
4448          enumeration, or dependent type, the program is ill-formed."
4449
4450          We cover classes and dependent types above and namespaces below,
4451          so this code is only looking for enums.  */
4452       if (!scope || TREE_CODE (scope) != TYPE_DECL
4453           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4454         cp_parser_simulate_error (parser);
4455
4456       successful_parse_p = cp_parser_parse_definitely (parser);
4457     }
4458   /* If that didn't work, try for a namespace-name.  */
4459   if (!only_class_p && !successful_parse_p)
4460     {
4461       /* Restore the saved scope.  */
4462       parser->scope = saved_scope;
4463       parser->qualifying_scope = saved_qualifying_scope;
4464       parser->object_scope = saved_object_scope;
4465       /* If we are not looking at an identifier followed by the scope
4466          resolution operator, then this is not part of a
4467          nested-name-specifier.  (Note that this function is only used
4468          to parse the components of a nested-name-specifier.)  */
4469       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4470           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4471         return error_mark_node;
4472       scope = cp_parser_namespace_name (parser);
4473     }
4474
4475   return scope;
4476 }
4477
4478 /* Parse a postfix-expression.
4479
4480    postfix-expression:
4481      primary-expression
4482      postfix-expression [ expression ]
4483      postfix-expression ( expression-list [opt] )
4484      simple-type-specifier ( expression-list [opt] )
4485      typename :: [opt] nested-name-specifier identifier
4486        ( expression-list [opt] )
4487      typename :: [opt] nested-name-specifier template [opt] template-id
4488        ( expression-list [opt] )
4489      postfix-expression . template [opt] id-expression
4490      postfix-expression -> template [opt] id-expression
4491      postfix-expression . pseudo-destructor-name
4492      postfix-expression -> pseudo-destructor-name
4493      postfix-expression ++
4494      postfix-expression --
4495      dynamic_cast < type-id > ( expression )
4496      static_cast < type-id > ( expression )
4497      reinterpret_cast < type-id > ( expression )
4498      const_cast < type-id > ( expression )
4499      typeid ( expression )
4500      typeid ( type-id )
4501
4502    GNU Extension:
4503
4504    postfix-expression:
4505      ( type-id ) { initializer-list , [opt] }
4506
4507    This extension is a GNU version of the C99 compound-literal
4508    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4509    but they are essentially the same concept.)
4510
4511    If ADDRESS_P is true, the postfix expression is the operand of the
4512    `&' operator.  CAST_P is true if this expression is the target of a
4513    cast.
4514
4515    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4516    class member access expressions [expr.ref].
4517
4518    Returns a representation of the expression.  */
4519
4520 static tree
4521 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4522                               bool member_access_only_p,
4523                               cp_id_kind * pidk_return)
4524 {
4525   cp_token *token;
4526   enum rid keyword;
4527   cp_id_kind idk = CP_ID_KIND_NONE;
4528   tree postfix_expression = NULL_TREE;
4529   bool is_member_access = false;
4530
4531   /* Peek at the next token.  */
4532   token = cp_lexer_peek_token (parser->lexer);
4533   /* Some of the productions are determined by keywords.  */
4534   keyword = token->keyword;
4535   switch (keyword)
4536     {
4537     case RID_DYNCAST:
4538     case RID_STATCAST:
4539     case RID_REINTCAST:
4540     case RID_CONSTCAST:
4541       {
4542         tree type;
4543         tree expression;
4544         const char *saved_message;
4545
4546         /* All of these can be handled in the same way from the point
4547            of view of parsing.  Begin by consuming the token
4548            identifying the cast.  */
4549         cp_lexer_consume_token (parser->lexer);
4550
4551         /* New types cannot be defined in the cast.  */
4552         saved_message = parser->type_definition_forbidden_message;
4553         parser->type_definition_forbidden_message
4554           = G_("types may not be defined in casts");
4555
4556         /* Look for the opening `<'.  */
4557         cp_parser_require (parser, CPP_LESS, "%<<%>");
4558         /* Parse the type to which we are casting.  */
4559         type = cp_parser_type_id (parser);
4560         /* Look for the closing `>'.  */
4561         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4562         /* Restore the old message.  */
4563         parser->type_definition_forbidden_message = saved_message;
4564
4565         /* And the expression which is being cast.  */
4566         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4567         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4568         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4569
4570         /* Only type conversions to integral or enumeration types
4571            can be used in constant-expressions.  */
4572         if (!cast_valid_in_integral_constant_expression_p (type)
4573             && (cp_parser_non_integral_constant_expression
4574                 (parser,
4575                  "a cast to a type other than an integral or "
4576                  "enumeration type")))
4577           return error_mark_node;
4578
4579         switch (keyword)
4580           {
4581           case RID_DYNCAST:
4582             postfix_expression
4583               = build_dynamic_cast (type, expression, tf_warning_or_error);
4584             break;
4585           case RID_STATCAST:
4586             postfix_expression
4587               = build_static_cast (type, expression, tf_warning_or_error);
4588             break;
4589           case RID_REINTCAST:
4590             postfix_expression
4591               = build_reinterpret_cast (type, expression, 
4592                                         tf_warning_or_error);
4593             break;
4594           case RID_CONSTCAST:
4595             postfix_expression
4596               = build_const_cast (type, expression, tf_warning_or_error);
4597             break;
4598           default:
4599             gcc_unreachable ();
4600           }
4601       }
4602       break;
4603
4604     case RID_TYPEID:
4605       {
4606         tree type;
4607         const char *saved_message;
4608         bool saved_in_type_id_in_expr_p;
4609
4610         /* Consume the `typeid' token.  */
4611         cp_lexer_consume_token (parser->lexer);
4612         /* Look for the `(' token.  */
4613         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4614         /* Types cannot be defined in a `typeid' expression.  */
4615         saved_message = parser->type_definition_forbidden_message;
4616         parser->type_definition_forbidden_message
4617           = G_("types may not be defined in a %<typeid%> expression");
4618         /* We can't be sure yet whether we're looking at a type-id or an
4619            expression.  */
4620         cp_parser_parse_tentatively (parser);
4621         /* Try a type-id first.  */
4622         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4623         parser->in_type_id_in_expr_p = true;
4624         type = cp_parser_type_id (parser);
4625         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4626         /* Look for the `)' token.  Otherwise, we can't be sure that
4627            we're not looking at an expression: consider `typeid (int
4628            (3))', for example.  */
4629         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4630         /* If all went well, simply lookup the type-id.  */
4631         if (cp_parser_parse_definitely (parser))
4632           postfix_expression = get_typeid (type);
4633         /* Otherwise, fall back to the expression variant.  */
4634         else
4635           {
4636             tree expression;
4637
4638             /* Look for an expression.  */
4639             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4640             /* Compute its typeid.  */
4641             postfix_expression = build_typeid (expression);
4642             /* Look for the `)' token.  */
4643             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4644           }
4645         /* Restore the saved message.  */
4646         parser->type_definition_forbidden_message = saved_message;
4647         /* `typeid' may not appear in an integral constant expression.  */
4648         if (cp_parser_non_integral_constant_expression(parser,
4649                                                        "%<typeid%> operator"))
4650           return error_mark_node;
4651       }
4652       break;
4653
4654     case RID_TYPENAME:
4655       {
4656         tree type;
4657         /* The syntax permitted here is the same permitted for an
4658            elaborated-type-specifier.  */
4659         type = cp_parser_elaborated_type_specifier (parser,
4660                                                     /*is_friend=*/false,
4661                                                     /*is_declaration=*/false);
4662         postfix_expression = cp_parser_functional_cast (parser, type);
4663       }
4664       break;
4665
4666     default:
4667       {
4668         tree type;
4669
4670         /* If the next thing is a simple-type-specifier, we may be
4671            looking at a functional cast.  We could also be looking at
4672            an id-expression.  So, we try the functional cast, and if
4673            that doesn't work we fall back to the primary-expression.  */
4674         cp_parser_parse_tentatively (parser);
4675         /* Look for the simple-type-specifier.  */
4676         type = cp_parser_simple_type_specifier (parser,
4677                                                 /*decl_specs=*/NULL,
4678                                                 CP_PARSER_FLAGS_NONE);
4679         /* Parse the cast itself.  */
4680         if (!cp_parser_error_occurred (parser))
4681           postfix_expression
4682             = cp_parser_functional_cast (parser, type);
4683         /* If that worked, we're done.  */
4684         if (cp_parser_parse_definitely (parser))
4685           break;
4686
4687         /* If the functional-cast didn't work out, try a
4688            compound-literal.  */
4689         if (cp_parser_allow_gnu_extensions_p (parser)
4690             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4691           {
4692             VEC(constructor_elt,gc) *initializer_list = NULL;
4693             bool saved_in_type_id_in_expr_p;
4694
4695             cp_parser_parse_tentatively (parser);
4696             /* Consume the `('.  */
4697             cp_lexer_consume_token (parser->lexer);
4698             /* Parse the type.  */
4699             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4700             parser->in_type_id_in_expr_p = true;
4701             type = cp_parser_type_id (parser);
4702             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4703             /* Look for the `)'.  */
4704             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4705             /* Look for the `{'.  */
4706             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4707             /* If things aren't going well, there's no need to
4708                keep going.  */
4709             if (!cp_parser_error_occurred (parser))
4710               {
4711                 bool non_constant_p;
4712                 /* Parse the initializer-list.  */
4713                 initializer_list
4714                   = cp_parser_initializer_list (parser, &non_constant_p);
4715                 /* Allow a trailing `,'.  */
4716                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4717                   cp_lexer_consume_token (parser->lexer);
4718                 /* Look for the final `}'.  */
4719                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4720               }
4721             /* If that worked, we're definitely looking at a
4722                compound-literal expression.  */
4723             if (cp_parser_parse_definitely (parser))
4724               {
4725                 /* Warn the user that a compound literal is not
4726                    allowed in standard C++.  */
4727                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4728                 /* For simplicity, we disallow compound literals in
4729                    constant-expressions.  We could
4730                    allow compound literals of integer type, whose
4731                    initializer was a constant, in constant
4732                    expressions.  Permitting that usage, as a further
4733                    extension, would not change the meaning of any
4734                    currently accepted programs.  (Of course, as
4735                    compound literals are not part of ISO C++, the
4736                    standard has nothing to say.)  */
4737                 if (cp_parser_non_integral_constant_expression 
4738                     (parser, "non-constant compound literals"))
4739                   {
4740                     postfix_expression = error_mark_node;
4741                     break;
4742                   }
4743                 /* Form the representation of the compound-literal.  */
4744                 postfix_expression
4745                   = (finish_compound_literal
4746                      (type, build_constructor (init_list_type_node,
4747                                                initializer_list)));
4748                 break;
4749               }
4750           }
4751
4752         /* It must be a primary-expression.  */
4753         postfix_expression
4754           = cp_parser_primary_expression (parser, address_p, cast_p,
4755                                           /*template_arg_p=*/false,
4756                                           &idk);
4757       }
4758       break;
4759     }
4760
4761   /* Keep looping until the postfix-expression is complete.  */
4762   while (true)
4763     {
4764       if (idk == CP_ID_KIND_UNQUALIFIED
4765           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4766           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4767         /* It is not a Koenig lookup function call.  */
4768         postfix_expression
4769           = unqualified_name_lookup_error (postfix_expression);
4770
4771       /* Peek at the next token.  */
4772       token = cp_lexer_peek_token (parser->lexer);
4773
4774       switch (token->type)
4775         {
4776         case CPP_OPEN_SQUARE:
4777           postfix_expression
4778             = cp_parser_postfix_open_square_expression (parser,
4779                                                         postfix_expression,
4780                                                         false);
4781           idk = CP_ID_KIND_NONE;
4782           is_member_access = false;
4783           break;
4784
4785         case CPP_OPEN_PAREN:
4786           /* postfix-expression ( expression-list [opt] ) */
4787           {
4788             bool koenig_p;
4789             bool is_builtin_constant_p;
4790             bool saved_integral_constant_expression_p = false;
4791             bool saved_non_integral_constant_expression_p = false;
4792             VEC(tree,gc) *args;
4793
4794             is_member_access = false;
4795
4796             is_builtin_constant_p
4797               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4798             if (is_builtin_constant_p)
4799               {
4800                 /* The whole point of __builtin_constant_p is to allow
4801                    non-constant expressions to appear as arguments.  */
4802                 saved_integral_constant_expression_p
4803                   = parser->integral_constant_expression_p;
4804                 saved_non_integral_constant_expression_p
4805                   = parser->non_integral_constant_expression_p;
4806                 parser->integral_constant_expression_p = false;
4807               }
4808             args = (cp_parser_parenthesized_expression_list
4809                     (parser, /*is_attribute_list=*/false,
4810                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4811                      /*non_constant_p=*/NULL));
4812             if (is_builtin_constant_p)
4813               {
4814                 parser->integral_constant_expression_p
4815                   = saved_integral_constant_expression_p;
4816                 parser->non_integral_constant_expression_p
4817                   = saved_non_integral_constant_expression_p;
4818               }
4819
4820             if (args == NULL)
4821               {
4822                 postfix_expression = error_mark_node;
4823                 break;
4824               }
4825
4826             /* Function calls are not permitted in
4827                constant-expressions.  */
4828             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4829                 && cp_parser_non_integral_constant_expression (parser,
4830                                                                "a function call"))
4831               {
4832                 postfix_expression = error_mark_node;
4833                 release_tree_vector (args);
4834                 break;
4835               }
4836
4837             koenig_p = false;
4838             if (idk == CP_ID_KIND_UNQUALIFIED
4839                 || idk == CP_ID_KIND_TEMPLATE_ID)
4840               {
4841                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4842                   {
4843                     if (!VEC_empty (tree, args))
4844                       {
4845                         koenig_p = true;
4846                         if (!any_type_dependent_arguments_p (args))
4847                           postfix_expression
4848                             = perform_koenig_lookup (postfix_expression, args);
4849                       }
4850                     else
4851                       postfix_expression
4852                         = unqualified_fn_lookup_error (postfix_expression);
4853                   }
4854                 /* We do not perform argument-dependent lookup if
4855                    normal lookup finds a non-function, in accordance
4856                    with the expected resolution of DR 218.  */
4857                 else if (!VEC_empty (tree, args)
4858                          && is_overloaded_fn (postfix_expression))
4859                   {
4860                     tree fn = get_first_fn (postfix_expression);
4861                     fn = STRIP_TEMPLATE (fn);
4862
4863                     /* Do not do argument dependent lookup if regular
4864                        lookup finds a member function or a block-scope
4865                        function declaration.  [basic.lookup.argdep]/3  */
4866                     if (!DECL_FUNCTION_MEMBER_P (fn)
4867                         && !DECL_LOCAL_FUNCTION_P (fn))
4868                       {
4869                         koenig_p = true;
4870                         if (!any_type_dependent_arguments_p (args))
4871                           postfix_expression
4872                             = perform_koenig_lookup (postfix_expression, args);
4873                       }
4874                   }
4875               }
4876
4877             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4878               {
4879                 tree instance = TREE_OPERAND (postfix_expression, 0);
4880                 tree fn = TREE_OPERAND (postfix_expression, 1);
4881
4882                 if (processing_template_decl
4883                     && (type_dependent_expression_p (instance)
4884                         || (!BASELINK_P (fn)
4885                             && TREE_CODE (fn) != FIELD_DECL)
4886                         || type_dependent_expression_p (fn)
4887                         || any_type_dependent_arguments_p (args)))
4888                   {
4889                     postfix_expression
4890                       = build_nt_call_vec (postfix_expression, args);
4891                     release_tree_vector (args);
4892                     break;
4893                   }
4894
4895                 if (BASELINK_P (fn))
4896                   {
4897                   postfix_expression
4898                     = (build_new_method_call
4899                        (instance, fn, &args, NULL_TREE,
4900                         (idk == CP_ID_KIND_QUALIFIED
4901                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4902                         /*fn_p=*/NULL,
4903                         tf_warning_or_error));
4904                   }
4905                 else
4906                   postfix_expression
4907                     = finish_call_expr (postfix_expression, &args,
4908                                         /*disallow_virtual=*/false,
4909                                         /*koenig_p=*/false,
4910                                         tf_warning_or_error);
4911               }
4912             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4913                      || TREE_CODE (postfix_expression) == MEMBER_REF
4914                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4915               postfix_expression = (build_offset_ref_call_from_tree
4916                                     (postfix_expression, &args));
4917             else if (idk == CP_ID_KIND_QUALIFIED)
4918               /* A call to a static class member, or a namespace-scope
4919                  function.  */
4920               postfix_expression
4921                 = finish_call_expr (postfix_expression, &args,
4922                                     /*disallow_virtual=*/true,
4923                                     koenig_p,
4924                                     tf_warning_or_error);
4925             else
4926               /* All other function calls.  */
4927               postfix_expression
4928                 = finish_call_expr (postfix_expression, &args,
4929                                     /*disallow_virtual=*/false,
4930                                     koenig_p,
4931                                     tf_warning_or_error);
4932
4933             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4934             idk = CP_ID_KIND_NONE;
4935
4936             release_tree_vector (args);
4937           }
4938           break;
4939
4940         case CPP_DOT:
4941         case CPP_DEREF:
4942           /* postfix-expression . template [opt] id-expression
4943              postfix-expression . pseudo-destructor-name
4944              postfix-expression -> template [opt] id-expression
4945              postfix-expression -> pseudo-destructor-name */
4946
4947           /* Consume the `.' or `->' operator.  */
4948           cp_lexer_consume_token (parser->lexer);
4949
4950           postfix_expression
4951             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4952                                                       postfix_expression,
4953                                                       false, &idk,
4954                                                       token->location);
4955
4956           is_member_access = true;
4957           break;
4958
4959         case CPP_PLUS_PLUS:
4960           /* postfix-expression ++  */
4961           /* Consume the `++' token.  */
4962           cp_lexer_consume_token (parser->lexer);
4963           /* Generate a representation for the complete expression.  */
4964           postfix_expression
4965             = finish_increment_expr (postfix_expression,
4966                                      POSTINCREMENT_EXPR);
4967           /* Increments may not appear in constant-expressions.  */
4968           if (cp_parser_non_integral_constant_expression (parser,
4969                                                           "an increment"))
4970             postfix_expression = error_mark_node;
4971           idk = CP_ID_KIND_NONE;
4972           is_member_access = false;
4973           break;
4974
4975         case CPP_MINUS_MINUS:
4976           /* postfix-expression -- */
4977           /* Consume the `--' token.  */
4978           cp_lexer_consume_token (parser->lexer);
4979           /* Generate a representation for the complete expression.  */
4980           postfix_expression
4981             = finish_increment_expr (postfix_expression,
4982                                      POSTDECREMENT_EXPR);
4983           /* Decrements may not appear in constant-expressions.  */
4984           if (cp_parser_non_integral_constant_expression (parser,
4985                                                           "a decrement"))
4986             postfix_expression = error_mark_node;
4987           idk = CP_ID_KIND_NONE;
4988           is_member_access = false;
4989           break;
4990
4991         default:
4992           if (pidk_return != NULL)
4993             * pidk_return = idk;
4994           if (member_access_only_p)
4995             return is_member_access? postfix_expression : error_mark_node;
4996           else
4997             return postfix_expression;
4998         }
4999     }
5000
5001   /* We should never get here.  */
5002   gcc_unreachable ();
5003   return error_mark_node;
5004 }
5005
5006 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5007    by cp_parser_builtin_offsetof.  We're looking for
5008
5009      postfix-expression [ expression ]
5010
5011    FOR_OFFSETOF is set if we're being called in that context, which
5012    changes how we deal with integer constant expressions.  */
5013
5014 static tree
5015 cp_parser_postfix_open_square_expression (cp_parser *parser,
5016                                           tree postfix_expression,
5017                                           bool for_offsetof)
5018 {
5019   tree index;
5020
5021   /* Consume the `[' token.  */
5022   cp_lexer_consume_token (parser->lexer);
5023
5024   /* Parse the index expression.  */
5025   /* ??? For offsetof, there is a question of what to allow here.  If
5026      offsetof is not being used in an integral constant expression context,
5027      then we *could* get the right answer by computing the value at runtime.
5028      If we are in an integral constant expression context, then we might
5029      could accept any constant expression; hard to say without analysis.
5030      Rather than open the barn door too wide right away, allow only integer
5031      constant expressions here.  */
5032   if (for_offsetof)
5033     index = cp_parser_constant_expression (parser, false, NULL);
5034   else
5035     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5036
5037   /* Look for the closing `]'.  */
5038   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5039
5040   /* Build the ARRAY_REF.  */
5041   postfix_expression = grok_array_decl (postfix_expression, index);
5042
5043   /* When not doing offsetof, array references are not permitted in
5044      constant-expressions.  */
5045   if (!for_offsetof
5046       && (cp_parser_non_integral_constant_expression
5047           (parser, "an array reference")))
5048     postfix_expression = error_mark_node;
5049
5050   return postfix_expression;
5051 }
5052
5053 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5054    by cp_parser_builtin_offsetof.  We're looking for
5055
5056      postfix-expression . template [opt] id-expression
5057      postfix-expression . pseudo-destructor-name
5058      postfix-expression -> template [opt] id-expression
5059      postfix-expression -> pseudo-destructor-name
5060
5061    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5062    limits what of the above we'll actually accept, but nevermind.
5063    TOKEN_TYPE is the "." or "->" token, which will already have been
5064    removed from the stream.  */
5065
5066 static tree
5067 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5068                                         enum cpp_ttype token_type,
5069                                         tree postfix_expression,
5070                                         bool for_offsetof, cp_id_kind *idk,
5071                                         location_t location)
5072 {
5073   tree name;
5074   bool dependent_p;
5075   bool pseudo_destructor_p;
5076   tree scope = NULL_TREE;
5077
5078   /* If this is a `->' operator, dereference the pointer.  */
5079   if (token_type == CPP_DEREF)
5080     postfix_expression = build_x_arrow (postfix_expression);
5081   /* Check to see whether or not the expression is type-dependent.  */
5082   dependent_p = type_dependent_expression_p (postfix_expression);
5083   /* The identifier following the `->' or `.' is not qualified.  */
5084   parser->scope = NULL_TREE;
5085   parser->qualifying_scope = NULL_TREE;
5086   parser->object_scope = NULL_TREE;
5087   *idk = CP_ID_KIND_NONE;
5088
5089   /* Enter the scope corresponding to the type of the object
5090      given by the POSTFIX_EXPRESSION.  */
5091   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5092     {
5093       scope = TREE_TYPE (postfix_expression);
5094       /* According to the standard, no expression should ever have
5095          reference type.  Unfortunately, we do not currently match
5096          the standard in this respect in that our internal representation
5097          of an expression may have reference type even when the standard
5098          says it does not.  Therefore, we have to manually obtain the
5099          underlying type here.  */
5100       scope = non_reference (scope);
5101       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5102       if (scope == unknown_type_node)
5103         {
5104           error_at (location, "%qE does not have class type",
5105                     postfix_expression);
5106           scope = NULL_TREE;
5107         }
5108       else
5109         scope = complete_type_or_else (scope, NULL_TREE);
5110       /* Let the name lookup machinery know that we are processing a
5111          class member access expression.  */
5112       parser->context->object_type = scope;
5113       /* If something went wrong, we want to be able to discern that case,
5114          as opposed to the case where there was no SCOPE due to the type
5115          of expression being dependent.  */
5116       if (!scope)
5117         scope = error_mark_node;
5118       /* If the SCOPE was erroneous, make the various semantic analysis
5119          functions exit quickly -- and without issuing additional error
5120          messages.  */
5121       if (scope == error_mark_node)
5122         postfix_expression = error_mark_node;
5123     }
5124
5125   /* Assume this expression is not a pseudo-destructor access.  */
5126   pseudo_destructor_p = false;
5127
5128   /* If the SCOPE is a scalar type, then, if this is a valid program,
5129      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5130      is type dependent, it can be pseudo-destructor-name or something else.
5131      Try to parse it as pseudo-destructor-name first.  */
5132   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5133     {
5134       tree s;
5135       tree type;
5136
5137       cp_parser_parse_tentatively (parser);
5138       /* Parse the pseudo-destructor-name.  */
5139       s = NULL_TREE;
5140       cp_parser_pseudo_destructor_name (parser, &s, &type);
5141       if (dependent_p
5142           && (cp_parser_error_occurred (parser)
5143               || TREE_CODE (type) != TYPE_DECL
5144               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5145         cp_parser_abort_tentative_parse (parser);
5146       else if (cp_parser_parse_definitely (parser))
5147         {
5148           pseudo_destructor_p = true;
5149           postfix_expression
5150             = finish_pseudo_destructor_expr (postfix_expression,
5151                                              s, TREE_TYPE (type));
5152         }
5153     }
5154
5155   if (!pseudo_destructor_p)
5156     {
5157       /* If the SCOPE is not a scalar type, we are looking at an
5158          ordinary class member access expression, rather than a
5159          pseudo-destructor-name.  */
5160       bool template_p;
5161       cp_token *token = cp_lexer_peek_token (parser->lexer);
5162       /* Parse the id-expression.  */
5163       name = (cp_parser_id_expression
5164               (parser,
5165                cp_parser_optional_template_keyword (parser),
5166                /*check_dependency_p=*/true,
5167                &template_p,
5168                /*declarator_p=*/false,
5169                /*optional_p=*/false));
5170       /* In general, build a SCOPE_REF if the member name is qualified.
5171          However, if the name was not dependent and has already been
5172          resolved; there is no need to build the SCOPE_REF.  For example;
5173
5174              struct X { void f(); };
5175              template <typename T> void f(T* t) { t->X::f(); }
5176
5177          Even though "t" is dependent, "X::f" is not and has been resolved
5178          to a BASELINK; there is no need to include scope information.  */
5179
5180       /* But we do need to remember that there was an explicit scope for
5181          virtual function calls.  */
5182       if (parser->scope)
5183         *idk = CP_ID_KIND_QUALIFIED;
5184
5185       /* If the name is a template-id that names a type, we will get a
5186          TYPE_DECL here.  That is invalid code.  */
5187       if (TREE_CODE (name) == TYPE_DECL)
5188         {
5189           error_at (token->location, "invalid use of %qD", name);
5190           postfix_expression = error_mark_node;
5191         }
5192       else
5193         {
5194           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5195             {
5196               name = build_qualified_name (/*type=*/NULL_TREE,
5197                                            parser->scope,
5198                                            name,
5199                                            template_p);
5200               parser->scope = NULL_TREE;
5201               parser->qualifying_scope = NULL_TREE;
5202               parser->object_scope = NULL_TREE;
5203             }
5204           if (scope && name && BASELINK_P (name))
5205             adjust_result_of_qualified_name_lookup
5206               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5207           postfix_expression
5208             = finish_class_member_access_expr (postfix_expression, name,
5209                                                template_p, 
5210                                                tf_warning_or_error);
5211         }
5212     }
5213
5214   /* We no longer need to look up names in the scope of the object on
5215      the left-hand side of the `.' or `->' operator.  */
5216   parser->context->object_type = NULL_TREE;
5217
5218   /* Outside of offsetof, these operators may not appear in
5219      constant-expressions.  */
5220   if (!for_offsetof
5221       && (cp_parser_non_integral_constant_expression
5222           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5223     postfix_expression = error_mark_node;
5224
5225   return postfix_expression;
5226 }
5227
5228 /* Parse a parenthesized expression-list.
5229
5230    expression-list:
5231      assignment-expression
5232      expression-list, assignment-expression
5233
5234    attribute-list:
5235      expression-list
5236      identifier
5237      identifier, expression-list
5238
5239    CAST_P is true if this expression is the target of a cast.
5240
5241    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5242    argument pack.
5243
5244    Returns a vector of trees.  Each element is a representation of an
5245    assignment-expression.  NULL is returned if the ( and or ) are
5246    missing.  An empty, but allocated, vector is returned on no
5247    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is true
5248    if this is really an attribute list being parsed.  If
5249    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5250    not all of the expressions in the list were constant.  */
5251
5252 static VEC(tree,gc) *
5253 cp_parser_parenthesized_expression_list (cp_parser* parser,
5254                                          bool is_attribute_list,
5255                                          bool cast_p,
5256                                          bool allow_expansion_p,
5257                                          bool *non_constant_p)
5258 {
5259   VEC(tree,gc) *expression_list;
5260   bool fold_expr_p = is_attribute_list;
5261   tree identifier = NULL_TREE;
5262   bool saved_greater_than_is_operator_p;
5263
5264   /* Assume all the expressions will be constant.  */
5265   if (non_constant_p)
5266     *non_constant_p = false;
5267
5268   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5269     return NULL;
5270
5271   expression_list = make_tree_vector ();
5272
5273   /* Within a parenthesized expression, a `>' token is always
5274      the greater-than operator.  */
5275   saved_greater_than_is_operator_p
5276     = parser->greater_than_is_operator_p;
5277   parser->greater_than_is_operator_p = true;
5278
5279   /* Consume expressions until there are no more.  */
5280   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5281     while (true)
5282       {
5283         tree expr;
5284
5285         /* At the beginning of attribute lists, check to see if the
5286            next token is an identifier.  */
5287         if (is_attribute_list
5288             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5289           {
5290             cp_token *token;
5291
5292             /* Consume the identifier.  */
5293             token = cp_lexer_consume_token (parser->lexer);
5294             /* Save the identifier.  */
5295             identifier = token->u.value;
5296           }
5297         else
5298           {
5299             bool expr_non_constant_p;
5300
5301             /* Parse the next assignment-expression.  */
5302             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5303               {
5304                 /* A braced-init-list.  */
5305                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5306                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5307                 if (non_constant_p && expr_non_constant_p)
5308                   *non_constant_p = true;
5309               }
5310             else if (non_constant_p)
5311               {
5312                 expr = (cp_parser_constant_expression
5313                         (parser, /*allow_non_constant_p=*/true,
5314                          &expr_non_constant_p));
5315                 if (expr_non_constant_p)
5316                   *non_constant_p = true;
5317               }
5318             else
5319               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5320
5321             if (fold_expr_p)
5322               expr = fold_non_dependent_expr (expr);
5323
5324             /* If we have an ellipsis, then this is an expression
5325                expansion.  */
5326             if (allow_expansion_p
5327                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5328               {
5329                 /* Consume the `...'.  */
5330                 cp_lexer_consume_token (parser->lexer);
5331
5332                 /* Build the argument pack.  */
5333                 expr = make_pack_expansion (expr);
5334               }
5335
5336              /* Add it to the list.  We add error_mark_node
5337                 expressions to the list, so that we can still tell if
5338                 the correct form for a parenthesized expression-list
5339                 is found. That gives better errors.  */
5340             VEC_safe_push (tree, gc, expression_list, expr);
5341
5342             if (expr == error_mark_node)
5343               goto skip_comma;
5344           }
5345
5346         /* After the first item, attribute lists look the same as
5347            expression lists.  */
5348         is_attribute_list = false;
5349
5350       get_comma:;
5351         /* If the next token isn't a `,', then we are done.  */
5352         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5353           break;
5354
5355         /* Otherwise, consume the `,' and keep going.  */
5356         cp_lexer_consume_token (parser->lexer);
5357       }
5358
5359   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5360     {
5361       int ending;
5362
5363     skip_comma:;
5364       /* We try and resync to an unnested comma, as that will give the
5365          user better diagnostics.  */
5366       ending = cp_parser_skip_to_closing_parenthesis (parser,
5367                                                       /*recovering=*/true,
5368                                                       /*or_comma=*/true,
5369                                                       /*consume_paren=*/true);
5370       if (ending < 0)
5371         goto get_comma;
5372       if (!ending)
5373         {
5374           parser->greater_than_is_operator_p
5375             = saved_greater_than_is_operator_p;
5376           return NULL;
5377         }
5378     }
5379
5380   parser->greater_than_is_operator_p
5381     = saved_greater_than_is_operator_p;
5382
5383   if (identifier)
5384     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5385
5386   return expression_list;
5387 }
5388
5389 /* Parse a pseudo-destructor-name.
5390
5391    pseudo-destructor-name:
5392      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5393      :: [opt] nested-name-specifier template template-id :: ~ type-name
5394      :: [opt] nested-name-specifier [opt] ~ type-name
5395
5396    If either of the first two productions is used, sets *SCOPE to the
5397    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5398    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5399    or ERROR_MARK_NODE if the parse fails.  */
5400
5401 static void
5402 cp_parser_pseudo_destructor_name (cp_parser* parser,
5403                                   tree* scope,
5404                                   tree* type)
5405 {
5406   bool nested_name_specifier_p;
5407
5408   /* Assume that things will not work out.  */
5409   *type = error_mark_node;
5410
5411   /* Look for the optional `::' operator.  */
5412   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5413   /* Look for the optional nested-name-specifier.  */
5414   nested_name_specifier_p
5415     = (cp_parser_nested_name_specifier_opt (parser,
5416                                             /*typename_keyword_p=*/false,
5417                                             /*check_dependency_p=*/true,
5418                                             /*type_p=*/false,
5419                                             /*is_declaration=*/false)
5420        != NULL_TREE);
5421   /* Now, if we saw a nested-name-specifier, we might be doing the
5422      second production.  */
5423   if (nested_name_specifier_p
5424       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5425     {
5426       /* Consume the `template' keyword.  */
5427       cp_lexer_consume_token (parser->lexer);
5428       /* Parse the template-id.  */
5429       cp_parser_template_id (parser,
5430                              /*template_keyword_p=*/true,
5431                              /*check_dependency_p=*/false,
5432                              /*is_declaration=*/true);
5433       /* Look for the `::' token.  */
5434       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5435     }
5436   /* If the next token is not a `~', then there might be some
5437      additional qualification.  */
5438   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5439     {
5440       /* At this point, we're looking for "type-name :: ~".  The type-name
5441          must not be a class-name, since this is a pseudo-destructor.  So,
5442          it must be either an enum-name, or a typedef-name -- both of which
5443          are just identifiers.  So, we peek ahead to check that the "::"
5444          and "~" tokens are present; if they are not, then we can avoid
5445          calling type_name.  */
5446       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5447           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5448           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5449         {
5450           cp_parser_error (parser, "non-scalar type");
5451           return;
5452         }
5453
5454       /* Look for the type-name.  */
5455       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5456       if (*scope == error_mark_node)
5457         return;
5458
5459       /* Look for the `::' token.  */
5460       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5461     }
5462   else
5463     *scope = NULL_TREE;
5464
5465   /* Look for the `~'.  */
5466   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5467   /* Look for the type-name again.  We are not responsible for
5468      checking that it matches the first type-name.  */
5469   *type = cp_parser_nonclass_name (parser);
5470 }
5471
5472 /* Parse a unary-expression.
5473
5474    unary-expression:
5475      postfix-expression
5476      ++ cast-expression
5477      -- cast-expression
5478      unary-operator cast-expression
5479      sizeof unary-expression
5480      sizeof ( type-id )
5481      new-expression
5482      delete-expression
5483
5484    GNU Extensions:
5485
5486    unary-expression:
5487      __extension__ cast-expression
5488      __alignof__ unary-expression
5489      __alignof__ ( type-id )
5490      __real__ cast-expression
5491      __imag__ cast-expression
5492      && identifier
5493
5494    ADDRESS_P is true iff the unary-expression is appearing as the
5495    operand of the `&' operator.   CAST_P is true if this expression is
5496    the target of a cast.
5497
5498    Returns a representation of the expression.  */
5499
5500 static tree
5501 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5502                             cp_id_kind * pidk)
5503 {
5504   cp_token *token;
5505   enum tree_code unary_operator;
5506
5507   /* Peek at the next token.  */
5508   token = cp_lexer_peek_token (parser->lexer);
5509   /* Some keywords give away the kind of expression.  */
5510   if (token->type == CPP_KEYWORD)
5511     {
5512       enum rid keyword = token->keyword;
5513
5514       switch (keyword)
5515         {
5516         case RID_ALIGNOF:
5517         case RID_SIZEOF:
5518           {
5519             tree operand;
5520             enum tree_code op;
5521
5522             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5523             /* Consume the token.  */
5524             cp_lexer_consume_token (parser->lexer);
5525             /* Parse the operand.  */
5526             operand = cp_parser_sizeof_operand (parser, keyword);
5527
5528             if (TYPE_P (operand))
5529               return cxx_sizeof_or_alignof_type (operand, op, true);
5530             else
5531               return cxx_sizeof_or_alignof_expr (operand, op, true);
5532           }
5533
5534         case RID_NEW:
5535           return cp_parser_new_expression (parser);
5536
5537         case RID_DELETE:
5538           return cp_parser_delete_expression (parser);
5539
5540         case RID_EXTENSION:
5541           {
5542             /* The saved value of the PEDANTIC flag.  */
5543             int saved_pedantic;
5544             tree expr;
5545
5546             /* Save away the PEDANTIC flag.  */
5547             cp_parser_extension_opt (parser, &saved_pedantic);
5548             /* Parse the cast-expression.  */
5549             expr = cp_parser_simple_cast_expression (parser);
5550             /* Restore the PEDANTIC flag.  */
5551             pedantic = saved_pedantic;
5552
5553             return expr;
5554           }
5555
5556         case RID_REALPART:
5557         case RID_IMAGPART:
5558           {
5559             tree expression;
5560
5561             /* Consume the `__real__' or `__imag__' token.  */
5562             cp_lexer_consume_token (parser->lexer);
5563             /* Parse the cast-expression.  */
5564             expression = cp_parser_simple_cast_expression (parser);
5565             /* Create the complete representation.  */
5566             return build_x_unary_op ((keyword == RID_REALPART
5567                                       ? REALPART_EXPR : IMAGPART_EXPR),
5568                                      expression,
5569                                      tf_warning_or_error);
5570           }
5571           break;
5572
5573         default:
5574           break;
5575         }
5576     }
5577
5578   /* Look for the `:: new' and `:: delete', which also signal the
5579      beginning of a new-expression, or delete-expression,
5580      respectively.  If the next token is `::', then it might be one of
5581      these.  */
5582   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5583     {
5584       enum rid keyword;
5585
5586       /* See if the token after the `::' is one of the keywords in
5587          which we're interested.  */
5588       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5589       /* If it's `new', we have a new-expression.  */
5590       if (keyword == RID_NEW)
5591         return cp_parser_new_expression (parser);
5592       /* Similarly, for `delete'.  */
5593       else if (keyword == RID_DELETE)
5594         return cp_parser_delete_expression (parser);
5595     }
5596
5597   /* Look for a unary operator.  */
5598   unary_operator = cp_parser_unary_operator (token);
5599   /* The `++' and `--' operators can be handled similarly, even though
5600      they are not technically unary-operators in the grammar.  */
5601   if (unary_operator == ERROR_MARK)
5602     {
5603       if (token->type == CPP_PLUS_PLUS)
5604         unary_operator = PREINCREMENT_EXPR;
5605       else if (token->type == CPP_MINUS_MINUS)
5606         unary_operator = PREDECREMENT_EXPR;
5607       /* Handle the GNU address-of-label extension.  */
5608       else if (cp_parser_allow_gnu_extensions_p (parser)
5609                && token->type == CPP_AND_AND)
5610         {
5611           tree identifier;
5612           tree expression;
5613           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5614
5615           /* Consume the '&&' token.  */
5616           cp_lexer_consume_token (parser->lexer);
5617           /* Look for the identifier.  */
5618           identifier = cp_parser_identifier (parser);
5619           /* Create an expression representing the address.  */
5620           expression = finish_label_address_expr (identifier, loc);
5621           if (cp_parser_non_integral_constant_expression (parser,
5622                                                 "the address of a label"))
5623             expression = error_mark_node;
5624           return expression;
5625         }
5626     }
5627   if (unary_operator != ERROR_MARK)
5628     {
5629       tree cast_expression;
5630       tree expression = error_mark_node;
5631       const char *non_constant_p = NULL;
5632
5633       /* Consume the operator token.  */
5634       token = cp_lexer_consume_token (parser->lexer);
5635       /* Parse the cast-expression.  */
5636       cast_expression
5637         = cp_parser_cast_expression (parser,
5638                                      unary_operator == ADDR_EXPR,
5639                                      /*cast_p=*/false, pidk);
5640       /* Now, build an appropriate representation.  */
5641       switch (unary_operator)
5642         {
5643         case INDIRECT_REF:
5644           non_constant_p = "%<*%>";
5645           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5646                                              tf_warning_or_error);
5647           break;
5648
5649         case ADDR_EXPR:
5650           non_constant_p = "%<&%>";
5651           /* Fall through.  */
5652         case BIT_NOT_EXPR:
5653           expression = build_x_unary_op (unary_operator, cast_expression,
5654                                          tf_warning_or_error);
5655           break;
5656
5657         case PREINCREMENT_EXPR:
5658         case PREDECREMENT_EXPR:
5659           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5660                             ? "%<++%>" : "%<--%>");
5661           /* Fall through.  */
5662         case UNARY_PLUS_EXPR:
5663         case NEGATE_EXPR:
5664         case TRUTH_NOT_EXPR:
5665           expression = finish_unary_op_expr (unary_operator, cast_expression);
5666           break;
5667
5668         default:
5669           gcc_unreachable ();
5670         }
5671
5672       if (non_constant_p
5673           && cp_parser_non_integral_constant_expression (parser,
5674                                                          non_constant_p))
5675         expression = error_mark_node;
5676
5677       return expression;
5678     }
5679
5680   return cp_parser_postfix_expression (parser, address_p, cast_p,
5681                                        /*member_access_only_p=*/false,
5682                                        pidk);
5683 }
5684
5685 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5686    unary-operator, the corresponding tree code is returned.  */
5687
5688 static enum tree_code
5689 cp_parser_unary_operator (cp_token* token)
5690 {
5691   switch (token->type)
5692     {
5693     case CPP_MULT:
5694       return INDIRECT_REF;
5695
5696     case CPP_AND:
5697       return ADDR_EXPR;
5698
5699     case CPP_PLUS:
5700       return UNARY_PLUS_EXPR;
5701
5702     case CPP_MINUS:
5703       return NEGATE_EXPR;
5704
5705     case CPP_NOT:
5706       return TRUTH_NOT_EXPR;
5707
5708     case CPP_COMPL:
5709       return BIT_NOT_EXPR;
5710
5711     default:
5712       return ERROR_MARK;
5713     }
5714 }
5715
5716 /* Parse a new-expression.
5717
5718    new-expression:
5719      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5720      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5721
5722    Returns a representation of the expression.  */
5723
5724 static tree
5725 cp_parser_new_expression (cp_parser* parser)
5726 {
5727   bool global_scope_p;
5728   VEC(tree,gc) *placement;
5729   tree type;
5730   VEC(tree,gc) *initializer;
5731   tree nelts;
5732   tree ret;
5733
5734   /* Look for the optional `::' operator.  */
5735   global_scope_p
5736     = (cp_parser_global_scope_opt (parser,
5737                                    /*current_scope_valid_p=*/false)
5738        != NULL_TREE);
5739   /* Look for the `new' operator.  */
5740   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5741   /* There's no easy way to tell a new-placement from the
5742      `( type-id )' construct.  */
5743   cp_parser_parse_tentatively (parser);
5744   /* Look for a new-placement.  */
5745   placement = cp_parser_new_placement (parser);
5746   /* If that didn't work out, there's no new-placement.  */
5747   if (!cp_parser_parse_definitely (parser))
5748     {
5749       if (placement != NULL)
5750         release_tree_vector (placement);
5751       placement = NULL;
5752     }
5753
5754   /* If the next token is a `(', then we have a parenthesized
5755      type-id.  */
5756   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5757     {
5758       cp_token *token;
5759       /* Consume the `('.  */
5760       cp_lexer_consume_token (parser->lexer);
5761       /* Parse the type-id.  */
5762       type = cp_parser_type_id (parser);
5763       /* Look for the closing `)'.  */
5764       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5765       token = cp_lexer_peek_token (parser->lexer);
5766       /* There should not be a direct-new-declarator in this production,
5767          but GCC used to allowed this, so we check and emit a sensible error
5768          message for this case.  */
5769       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5770         {
5771           error_at (token->location,
5772                     "array bound forbidden after parenthesized type-id");
5773           inform (token->location, 
5774                   "try removing the parentheses around the type-id");
5775           cp_parser_direct_new_declarator (parser);
5776         }
5777       nelts = NULL_TREE;
5778     }
5779   /* Otherwise, there must be a new-type-id.  */
5780   else
5781     type = cp_parser_new_type_id (parser, &nelts);
5782
5783   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5784   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5785       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5786     initializer = cp_parser_new_initializer (parser);
5787   else
5788     initializer = NULL;
5789
5790   /* A new-expression may not appear in an integral constant
5791      expression.  */
5792   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5793     ret = error_mark_node;
5794   else
5795     {
5796       /* Create a representation of the new-expression.  */
5797       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5798                        tf_warning_or_error);
5799     }
5800
5801   if (placement != NULL)
5802     release_tree_vector (placement);
5803   if (initializer != NULL)
5804     release_tree_vector (initializer);
5805
5806   return ret;
5807 }
5808
5809 /* Parse a new-placement.
5810
5811    new-placement:
5812      ( expression-list )
5813
5814    Returns the same representation as for an expression-list.  */
5815
5816 static VEC(tree,gc) *
5817 cp_parser_new_placement (cp_parser* parser)
5818 {
5819   VEC(tree,gc) *expression_list;
5820
5821   /* Parse the expression-list.  */
5822   expression_list = (cp_parser_parenthesized_expression_list
5823                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5824                       /*non_constant_p=*/NULL));
5825
5826   return expression_list;
5827 }
5828
5829 /* Parse a new-type-id.
5830
5831    new-type-id:
5832      type-specifier-seq new-declarator [opt]
5833
5834    Returns the TYPE allocated.  If the new-type-id indicates an array
5835    type, *NELTS is set to the number of elements in the last array
5836    bound; the TYPE will not include the last array bound.  */
5837
5838 static tree
5839 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5840 {
5841   cp_decl_specifier_seq type_specifier_seq;
5842   cp_declarator *new_declarator;
5843   cp_declarator *declarator;
5844   cp_declarator *outer_declarator;
5845   const char *saved_message;
5846   tree type;
5847
5848   /* The type-specifier sequence must not contain type definitions.
5849      (It cannot contain declarations of new types either, but if they
5850      are not definitions we will catch that because they are not
5851      complete.)  */
5852   saved_message = parser->type_definition_forbidden_message;
5853   parser->type_definition_forbidden_message
5854     = G_("types may not be defined in a new-type-id");
5855   /* Parse the type-specifier-seq.  */
5856   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5857                                 /*is_trailing_return=*/false,
5858                                 &type_specifier_seq);
5859   /* Restore the old message.  */
5860   parser->type_definition_forbidden_message = saved_message;
5861   /* Parse the new-declarator.  */
5862   new_declarator = cp_parser_new_declarator_opt (parser);
5863
5864   /* Determine the number of elements in the last array dimension, if
5865      any.  */
5866   *nelts = NULL_TREE;
5867   /* Skip down to the last array dimension.  */
5868   declarator = new_declarator;
5869   outer_declarator = NULL;
5870   while (declarator && (declarator->kind == cdk_pointer
5871                         || declarator->kind == cdk_ptrmem))
5872     {
5873       outer_declarator = declarator;
5874       declarator = declarator->declarator;
5875     }
5876   while (declarator
5877          && declarator->kind == cdk_array
5878          && declarator->declarator
5879          && declarator->declarator->kind == cdk_array)
5880     {
5881       outer_declarator = declarator;
5882       declarator = declarator->declarator;
5883     }
5884
5885   if (declarator && declarator->kind == cdk_array)
5886     {
5887       *nelts = declarator->u.array.bounds;
5888       if (*nelts == error_mark_node)
5889         *nelts = integer_one_node;
5890
5891       if (outer_declarator)
5892         outer_declarator->declarator = declarator->declarator;
5893       else
5894         new_declarator = NULL;
5895     }
5896
5897   type = groktypename (&type_specifier_seq, new_declarator, false);
5898   return type;
5899 }
5900
5901 /* Parse an (optional) new-declarator.
5902
5903    new-declarator:
5904      ptr-operator new-declarator [opt]
5905      direct-new-declarator
5906
5907    Returns the declarator.  */
5908
5909 static cp_declarator *
5910 cp_parser_new_declarator_opt (cp_parser* parser)
5911 {
5912   enum tree_code code;
5913   tree type;
5914   cp_cv_quals cv_quals;
5915
5916   /* We don't know if there's a ptr-operator next, or not.  */
5917   cp_parser_parse_tentatively (parser);
5918   /* Look for a ptr-operator.  */
5919   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5920   /* If that worked, look for more new-declarators.  */
5921   if (cp_parser_parse_definitely (parser))
5922     {
5923       cp_declarator *declarator;
5924
5925       /* Parse another optional declarator.  */
5926       declarator = cp_parser_new_declarator_opt (parser);
5927
5928       return cp_parser_make_indirect_declarator
5929         (code, type, cv_quals, declarator);
5930     }
5931
5932   /* If the next token is a `[', there is a direct-new-declarator.  */
5933   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5934     return cp_parser_direct_new_declarator (parser);
5935
5936   return NULL;
5937 }
5938
5939 /* Parse a direct-new-declarator.
5940
5941    direct-new-declarator:
5942      [ expression ]
5943      direct-new-declarator [constant-expression]
5944
5945    */
5946
5947 static cp_declarator *
5948 cp_parser_direct_new_declarator (cp_parser* parser)
5949 {
5950   cp_declarator *declarator = NULL;
5951
5952   while (true)
5953     {
5954       tree expression;
5955
5956       /* Look for the opening `['.  */
5957       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5958       /* The first expression is not required to be constant.  */
5959       if (!declarator)
5960         {
5961           cp_token *token = cp_lexer_peek_token (parser->lexer);
5962           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5963           /* The standard requires that the expression have integral
5964              type.  DR 74 adds enumeration types.  We believe that the
5965              real intent is that these expressions be handled like the
5966              expression in a `switch' condition, which also allows
5967              classes with a single conversion to integral or
5968              enumeration type.  */
5969           if (!processing_template_decl)
5970             {
5971               expression
5972                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5973                                               expression,
5974                                               /*complain=*/true);
5975               if (!expression)
5976                 {
5977                   error_at (token->location,
5978                             "expression in new-declarator must have integral "
5979                             "or enumeration type");
5980                   expression = error_mark_node;
5981                 }
5982             }
5983         }
5984       /* But all the other expressions must be.  */
5985       else
5986         expression
5987           = cp_parser_constant_expression (parser,
5988                                            /*allow_non_constant=*/false,
5989                                            NULL);
5990       /* Look for the closing `]'.  */
5991       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5992
5993       /* Add this bound to the declarator.  */
5994       declarator = make_array_declarator (declarator, expression);
5995
5996       /* If the next token is not a `[', then there are no more
5997          bounds.  */
5998       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5999         break;
6000     }
6001
6002   return declarator;
6003 }
6004
6005 /* Parse a new-initializer.
6006
6007    new-initializer:
6008      ( expression-list [opt] )
6009      braced-init-list
6010
6011    Returns a representation of the expression-list.  */
6012
6013 static VEC(tree,gc) *
6014 cp_parser_new_initializer (cp_parser* parser)
6015 {
6016   VEC(tree,gc) *expression_list;
6017
6018   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6019     {
6020       tree t;
6021       bool expr_non_constant_p;
6022       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6023       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6024       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6025       expression_list = make_tree_vector_single (t);
6026     }
6027   else
6028     expression_list = (cp_parser_parenthesized_expression_list
6029                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
6030                         /*non_constant_p=*/NULL));
6031
6032   return expression_list;
6033 }
6034
6035 /* Parse a delete-expression.
6036
6037    delete-expression:
6038      :: [opt] delete cast-expression
6039      :: [opt] delete [ ] cast-expression
6040
6041    Returns a representation of the expression.  */
6042
6043 static tree
6044 cp_parser_delete_expression (cp_parser* parser)
6045 {
6046   bool global_scope_p;
6047   bool array_p;
6048   tree expression;
6049
6050   /* Look for the optional `::' operator.  */
6051   global_scope_p
6052     = (cp_parser_global_scope_opt (parser,
6053                                    /*current_scope_valid_p=*/false)
6054        != NULL_TREE);
6055   /* Look for the `delete' keyword.  */
6056   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6057   /* See if the array syntax is in use.  */
6058   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6059     {
6060       /* Consume the `[' token.  */
6061       cp_lexer_consume_token (parser->lexer);
6062       /* Look for the `]' token.  */
6063       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6064       /* Remember that this is the `[]' construct.  */
6065       array_p = true;
6066     }
6067   else
6068     array_p = false;
6069
6070   /* Parse the cast-expression.  */
6071   expression = cp_parser_simple_cast_expression (parser);
6072
6073   /* A delete-expression may not appear in an integral constant
6074      expression.  */
6075   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6076     return error_mark_node;
6077
6078   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6079 }
6080
6081 /* Returns true if TOKEN may start a cast-expression and false
6082    otherwise.  */
6083
6084 static bool
6085 cp_parser_token_starts_cast_expression (cp_token *token)
6086 {
6087   switch (token->type)
6088     {
6089     case CPP_COMMA:
6090     case CPP_SEMICOLON:
6091     case CPP_QUERY:
6092     case CPP_COLON:
6093     case CPP_CLOSE_SQUARE:
6094     case CPP_CLOSE_PAREN:
6095     case CPP_CLOSE_BRACE:
6096     case CPP_DOT:
6097     case CPP_DOT_STAR:
6098     case CPP_DEREF:
6099     case CPP_DEREF_STAR:
6100     case CPP_DIV:
6101     case CPP_MOD:
6102     case CPP_LSHIFT:
6103     case CPP_RSHIFT:
6104     case CPP_LESS:
6105     case CPP_GREATER:
6106     case CPP_LESS_EQ:
6107     case CPP_GREATER_EQ:
6108     case CPP_EQ_EQ:
6109     case CPP_NOT_EQ:
6110     case CPP_EQ:
6111     case CPP_MULT_EQ:
6112     case CPP_DIV_EQ:
6113     case CPP_MOD_EQ:
6114     case CPP_PLUS_EQ:
6115     case CPP_MINUS_EQ:
6116     case CPP_RSHIFT_EQ:
6117     case CPP_LSHIFT_EQ:
6118     case CPP_AND_EQ:
6119     case CPP_XOR_EQ:
6120     case CPP_OR_EQ:
6121     case CPP_XOR:
6122     case CPP_OR:
6123     case CPP_OR_OR:
6124     case CPP_EOF:
6125       return false;
6126
6127       /* '[' may start a primary-expression in obj-c++.  */
6128     case CPP_OPEN_SQUARE:
6129       return c_dialect_objc ();
6130
6131     default:
6132       return true;
6133     }
6134 }
6135
6136 /* Parse a cast-expression.
6137
6138    cast-expression:
6139      unary-expression
6140      ( type-id ) cast-expression
6141
6142    ADDRESS_P is true iff the unary-expression is appearing as the
6143    operand of the `&' operator.   CAST_P is true if this expression is
6144    the target of a cast.
6145
6146    Returns a representation of the expression.  */
6147
6148 static tree
6149 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6150                            cp_id_kind * pidk)
6151 {
6152   /* If it's a `(', then we might be looking at a cast.  */
6153   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6154     {
6155       tree type = NULL_TREE;
6156       tree expr = NULL_TREE;
6157       bool compound_literal_p;
6158       const char *saved_message;
6159
6160       /* There's no way to know yet whether or not this is a cast.
6161          For example, `(int (3))' is a unary-expression, while `(int)
6162          3' is a cast.  So, we resort to parsing tentatively.  */
6163       cp_parser_parse_tentatively (parser);
6164       /* Types may not be defined in a cast.  */
6165       saved_message = parser->type_definition_forbidden_message;
6166       parser->type_definition_forbidden_message
6167         = G_("types may not be defined in casts");
6168       /* Consume the `('.  */
6169       cp_lexer_consume_token (parser->lexer);
6170       /* A very tricky bit is that `(struct S) { 3 }' is a
6171          compound-literal (which we permit in C++ as an extension).
6172          But, that construct is not a cast-expression -- it is a
6173          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6174          is legal; if the compound-literal were a cast-expression,
6175          you'd need an extra set of parentheses.)  But, if we parse
6176          the type-id, and it happens to be a class-specifier, then we
6177          will commit to the parse at that point, because we cannot
6178          undo the action that is done when creating a new class.  So,
6179          then we cannot back up and do a postfix-expression.
6180
6181          Therefore, we scan ahead to the closing `)', and check to see
6182          if the token after the `)' is a `{'.  If so, we are not
6183          looking at a cast-expression.
6184
6185          Save tokens so that we can put them back.  */
6186       cp_lexer_save_tokens (parser->lexer);
6187       /* Skip tokens until the next token is a closing parenthesis.
6188          If we find the closing `)', and the next token is a `{', then
6189          we are looking at a compound-literal.  */
6190       compound_literal_p
6191         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6192                                                   /*consume_paren=*/true)
6193            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6194       /* Roll back the tokens we skipped.  */
6195       cp_lexer_rollback_tokens (parser->lexer);
6196       /* If we were looking at a compound-literal, simulate an error
6197          so that the call to cp_parser_parse_definitely below will
6198          fail.  */
6199       if (compound_literal_p)
6200         cp_parser_simulate_error (parser);
6201       else
6202         {
6203           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6204           parser->in_type_id_in_expr_p = true;
6205           /* Look for the type-id.  */
6206           type = cp_parser_type_id (parser);
6207           /* Look for the closing `)'.  */
6208           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6209           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6210         }
6211
6212       /* Restore the saved message.  */
6213       parser->type_definition_forbidden_message = saved_message;
6214
6215       /* At this point this can only be either a cast or a
6216          parenthesized ctor such as `(T ())' that looks like a cast to
6217          function returning T.  */
6218       if (!cp_parser_error_occurred (parser)
6219           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6220                                                      (parser->lexer)))
6221         {
6222           cp_parser_parse_definitely (parser);
6223           expr = cp_parser_cast_expression (parser,
6224                                             /*address_p=*/false,
6225                                             /*cast_p=*/true, pidk);
6226
6227           /* Warn about old-style casts, if so requested.  */
6228           if (warn_old_style_cast
6229               && !in_system_header
6230               && !VOID_TYPE_P (type)
6231               && current_lang_name != lang_name_c)
6232             warning (OPT_Wold_style_cast, "use of old-style cast");
6233
6234           /* Only type conversions to integral or enumeration types
6235              can be used in constant-expressions.  */
6236           if (!cast_valid_in_integral_constant_expression_p (type)
6237               && (cp_parser_non_integral_constant_expression
6238                   (parser,
6239                    "a cast to a type other than an integral or "
6240                    "enumeration type")))
6241             return error_mark_node;
6242
6243           /* Perform the cast.  */
6244           expr = build_c_cast (input_location, type, expr);
6245           return expr;
6246         }
6247       else 
6248         cp_parser_abort_tentative_parse (parser);
6249     }
6250
6251   /* If we get here, then it's not a cast, so it must be a
6252      unary-expression.  */
6253   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6254 }
6255
6256 /* Parse a binary expression of the general form:
6257
6258    pm-expression:
6259      cast-expression
6260      pm-expression .* cast-expression
6261      pm-expression ->* cast-expression
6262
6263    multiplicative-expression:
6264      pm-expression
6265      multiplicative-expression * pm-expression
6266      multiplicative-expression / pm-expression
6267      multiplicative-expression % pm-expression
6268
6269    additive-expression:
6270      multiplicative-expression
6271      additive-expression + multiplicative-expression
6272      additive-expression - multiplicative-expression
6273
6274    shift-expression:
6275      additive-expression
6276      shift-expression << additive-expression
6277      shift-expression >> additive-expression
6278
6279    relational-expression:
6280      shift-expression
6281      relational-expression < shift-expression
6282      relational-expression > shift-expression
6283      relational-expression <= shift-expression
6284      relational-expression >= shift-expression
6285
6286   GNU Extension:
6287
6288    relational-expression:
6289      relational-expression <? shift-expression
6290      relational-expression >? shift-expression
6291
6292    equality-expression:
6293      relational-expression
6294      equality-expression == relational-expression
6295      equality-expression != relational-expression
6296
6297    and-expression:
6298      equality-expression
6299      and-expression & equality-expression
6300
6301    exclusive-or-expression:
6302      and-expression
6303      exclusive-or-expression ^ and-expression
6304
6305    inclusive-or-expression:
6306      exclusive-or-expression
6307      inclusive-or-expression | exclusive-or-expression
6308
6309    logical-and-expression:
6310      inclusive-or-expression
6311      logical-and-expression && inclusive-or-expression
6312
6313    logical-or-expression:
6314      logical-and-expression
6315      logical-or-expression || logical-and-expression
6316
6317    All these are implemented with a single function like:
6318
6319    binary-expression:
6320      simple-cast-expression
6321      binary-expression <token> binary-expression
6322
6323    CAST_P is true if this expression is the target of a cast.
6324
6325    The binops_by_token map is used to get the tree codes for each <token> type.
6326    binary-expressions are associated according to a precedence table.  */
6327
6328 #define TOKEN_PRECEDENCE(token)                              \
6329 (((token->type == CPP_GREATER                                \
6330    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6331   && !parser->greater_than_is_operator_p)                    \
6332  ? PREC_NOT_OPERATOR                                         \
6333  : binops_by_token[token->type].prec)
6334
6335 static tree
6336 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6337                              bool no_toplevel_fold_p,
6338                              enum cp_parser_prec prec,
6339                              cp_id_kind * pidk)
6340 {
6341   cp_parser_expression_stack stack;
6342   cp_parser_expression_stack_entry *sp = &stack[0];
6343   tree lhs, rhs;
6344   cp_token *token;
6345   enum tree_code tree_type, lhs_type, rhs_type;
6346   enum cp_parser_prec new_prec, lookahead_prec;
6347   bool overloaded_p;
6348
6349   /* Parse the first expression.  */
6350   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6351   lhs_type = ERROR_MARK;
6352
6353   for (;;)
6354     {
6355       /* Get an operator token.  */
6356       token = cp_lexer_peek_token (parser->lexer);
6357
6358       if (warn_cxx0x_compat
6359           && token->type == CPP_RSHIFT
6360           && !parser->greater_than_is_operator_p)
6361         {
6362           if (warning_at (token->location, OPT_Wc__0x_compat, 
6363                           "%<>>%> operator will be treated as"
6364                           " two right angle brackets in C++0x"))
6365             inform (token->location,
6366                     "suggest parentheses around %<>>%> expression");
6367         }
6368
6369       new_prec = TOKEN_PRECEDENCE (token);
6370
6371       /* Popping an entry off the stack means we completed a subexpression:
6372          - either we found a token which is not an operator (`>' where it is not
6373            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6374            will happen repeatedly;
6375          - or, we found an operator which has lower priority.  This is the case
6376            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6377            parsing `3 * 4'.  */
6378       if (new_prec <= prec)
6379         {
6380           if (sp == stack)
6381             break;
6382           else
6383             goto pop;
6384         }
6385
6386      get_rhs:
6387       tree_type = binops_by_token[token->type].tree_type;
6388
6389       /* We used the operator token.  */
6390       cp_lexer_consume_token (parser->lexer);
6391
6392       /* For "false && x" or "true || x", x will never be executed;
6393          disable warnings while evaluating it.  */
6394       if (tree_type == TRUTH_ANDIF_EXPR)
6395         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6396       else if (tree_type == TRUTH_ORIF_EXPR)
6397         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6398
6399       /* Extract another operand.  It may be the RHS of this expression
6400          or the LHS of a new, higher priority expression.  */
6401       rhs = cp_parser_simple_cast_expression (parser);
6402       rhs_type = ERROR_MARK;
6403
6404       /* Get another operator token.  Look up its precedence to avoid
6405          building a useless (immediately popped) stack entry for common
6406          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6407       token = cp_lexer_peek_token (parser->lexer);
6408       lookahead_prec = TOKEN_PRECEDENCE (token);
6409       if (lookahead_prec > new_prec)
6410         {
6411           /* ... and prepare to parse the RHS of the new, higher priority
6412              expression.  Since precedence levels on the stack are
6413              monotonically increasing, we do not have to care about
6414              stack overflows.  */
6415           sp->prec = prec;
6416           sp->tree_type = tree_type;
6417           sp->lhs = lhs;
6418           sp->lhs_type = lhs_type;
6419           sp++;
6420           lhs = rhs;
6421           lhs_type = rhs_type;
6422           prec = new_prec;
6423           new_prec = lookahead_prec;
6424           goto get_rhs;
6425
6426          pop:
6427           lookahead_prec = new_prec;
6428           /* If the stack is not empty, we have parsed into LHS the right side
6429              (`4' in the example above) of an expression we had suspended.
6430              We can use the information on the stack to recover the LHS (`3')
6431              from the stack together with the tree code (`MULT_EXPR'), and
6432              the precedence of the higher level subexpression
6433              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6434              which will be used to actually build the additive expression.  */
6435           --sp;
6436           prec = sp->prec;
6437           tree_type = sp->tree_type;
6438           rhs = lhs;
6439           rhs_type = lhs_type;
6440           lhs = sp->lhs;
6441           lhs_type = sp->lhs_type;
6442         }
6443
6444       /* Undo the disabling of warnings done above.  */
6445       if (tree_type == TRUTH_ANDIF_EXPR)
6446         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6447       else if (tree_type == TRUTH_ORIF_EXPR)
6448         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6449
6450       overloaded_p = false;
6451       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6452          ERROR_MARK for everything that is not a binary expression.
6453          This makes warn_about_parentheses miss some warnings that
6454          involve unary operators.  For unary expressions we should
6455          pass the correct tree_code unless the unary expression was
6456          surrounded by parentheses.
6457       */
6458       if (no_toplevel_fold_p
6459           && lookahead_prec <= prec
6460           && sp == stack
6461           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6462         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6463       else
6464         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6465                                  &overloaded_p, tf_warning_or_error);
6466       lhs_type = tree_type;
6467
6468       /* If the binary operator required the use of an overloaded operator,
6469          then this expression cannot be an integral constant-expression.
6470          An overloaded operator can be used even if both operands are
6471          otherwise permissible in an integral constant-expression if at
6472          least one of the operands is of enumeration type.  */
6473
6474       if (overloaded_p
6475           && (cp_parser_non_integral_constant_expression
6476               (parser, "calls to overloaded operators")))
6477         return error_mark_node;
6478     }
6479
6480   return lhs;
6481 }
6482
6483
6484 /* Parse the `? expression : assignment-expression' part of a
6485    conditional-expression.  The LOGICAL_OR_EXPR is the
6486    logical-or-expression that started the conditional-expression.
6487    Returns a representation of the entire conditional-expression.
6488
6489    This routine is used by cp_parser_assignment_expression.
6490
6491      ? expression : assignment-expression
6492
6493    GNU Extensions:
6494
6495      ? : assignment-expression */
6496
6497 static tree
6498 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6499 {
6500   tree expr;
6501   tree assignment_expr;
6502
6503   /* Consume the `?' token.  */
6504   cp_lexer_consume_token (parser->lexer);
6505   if (cp_parser_allow_gnu_extensions_p (parser)
6506       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6507     {
6508       /* Implicit true clause.  */
6509       expr = NULL_TREE;
6510       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6511     }
6512   else
6513     {
6514       /* Parse the expression.  */
6515       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6516       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6517       c_inhibit_evaluation_warnings +=
6518         ((logical_or_expr == truthvalue_true_node)
6519          - (logical_or_expr == truthvalue_false_node));
6520     }
6521
6522   /* The next token should be a `:'.  */
6523   cp_parser_require (parser, CPP_COLON, "%<:%>");
6524   /* Parse the assignment-expression.  */
6525   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6526   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6527
6528   /* Build the conditional-expression.  */
6529   return build_x_conditional_expr (logical_or_expr,
6530                                    expr,
6531                                    assignment_expr,
6532                                    tf_warning_or_error);
6533 }
6534
6535 /* Parse an assignment-expression.
6536
6537    assignment-expression:
6538      conditional-expression
6539      logical-or-expression assignment-operator assignment_expression
6540      throw-expression
6541
6542    CAST_P is true if this expression is the target of a cast.
6543
6544    Returns a representation for the expression.  */
6545
6546 static tree
6547 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6548                                  cp_id_kind * pidk)
6549 {
6550   tree expr;
6551
6552   /* If the next token is the `throw' keyword, then we're looking at
6553      a throw-expression.  */
6554   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6555     expr = cp_parser_throw_expression (parser);
6556   /* Otherwise, it must be that we are looking at a
6557      logical-or-expression.  */
6558   else
6559     {
6560       /* Parse the binary expressions (logical-or-expression).  */
6561       expr = cp_parser_binary_expression (parser, cast_p, false,
6562                                           PREC_NOT_OPERATOR, pidk);
6563       /* If the next token is a `?' then we're actually looking at a
6564          conditional-expression.  */
6565       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6566         return cp_parser_question_colon_clause (parser, expr);
6567       else
6568         {
6569           enum tree_code assignment_operator;
6570
6571           /* If it's an assignment-operator, we're using the second
6572              production.  */
6573           assignment_operator
6574             = cp_parser_assignment_operator_opt (parser);
6575           if (assignment_operator != ERROR_MARK)
6576             {
6577               bool non_constant_p;
6578
6579               /* Parse the right-hand side of the assignment.  */
6580               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6581
6582               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6583                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6584
6585               /* An assignment may not appear in a
6586                  constant-expression.  */
6587               if (cp_parser_non_integral_constant_expression (parser,
6588                                                               "an assignment"))
6589                 return error_mark_node;
6590               /* Build the assignment expression.  */
6591               expr = build_x_modify_expr (expr,
6592                                           assignment_operator,
6593                                           rhs,
6594                                           tf_warning_or_error);
6595             }
6596         }
6597     }
6598
6599   return expr;
6600 }
6601
6602 /* Parse an (optional) assignment-operator.
6603
6604    assignment-operator: one of
6605      = *= /= %= += -= >>= <<= &= ^= |=
6606
6607    GNU Extension:
6608
6609    assignment-operator: one of
6610      <?= >?=
6611
6612    If the next token is an assignment operator, the corresponding tree
6613    code is returned, and the token is consumed.  For example, for
6614    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6615    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6616    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6617    operator, ERROR_MARK is returned.  */
6618
6619 static enum tree_code
6620 cp_parser_assignment_operator_opt (cp_parser* parser)
6621 {
6622   enum tree_code op;
6623   cp_token *token;
6624
6625   /* Peek at the next token.  */
6626   token = cp_lexer_peek_token (parser->lexer);
6627
6628   switch (token->type)
6629     {
6630     case CPP_EQ:
6631       op = NOP_EXPR;
6632       break;
6633
6634     case CPP_MULT_EQ:
6635       op = MULT_EXPR;
6636       break;
6637
6638     case CPP_DIV_EQ:
6639       op = TRUNC_DIV_EXPR;
6640       break;
6641
6642     case CPP_MOD_EQ:
6643       op = TRUNC_MOD_EXPR;
6644       break;
6645
6646     case CPP_PLUS_EQ:
6647       op = PLUS_EXPR;
6648       break;
6649
6650     case CPP_MINUS_EQ:
6651       op = MINUS_EXPR;
6652       break;
6653
6654     case CPP_RSHIFT_EQ:
6655       op = RSHIFT_EXPR;
6656       break;
6657
6658     case CPP_LSHIFT_EQ:
6659       op = LSHIFT_EXPR;
6660       break;
6661
6662     case CPP_AND_EQ:
6663       op = BIT_AND_EXPR;
6664       break;
6665
6666     case CPP_XOR_EQ:
6667       op = BIT_XOR_EXPR;
6668       break;
6669
6670     case CPP_OR_EQ:
6671       op = BIT_IOR_EXPR;
6672       break;
6673
6674     default:
6675       /* Nothing else is an assignment operator.  */
6676       op = ERROR_MARK;
6677     }
6678
6679   /* If it was an assignment operator, consume it.  */
6680   if (op != ERROR_MARK)
6681     cp_lexer_consume_token (parser->lexer);
6682
6683   return op;
6684 }
6685
6686 /* Parse an expression.
6687
6688    expression:
6689      assignment-expression
6690      expression , assignment-expression
6691
6692    CAST_P is true if this expression is the target of a cast.
6693
6694    Returns a representation of the expression.  */
6695
6696 static tree
6697 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6698 {
6699   tree expression = NULL_TREE;
6700
6701   while (true)
6702     {
6703       tree assignment_expression;
6704
6705       /* Parse the next assignment-expression.  */
6706       assignment_expression
6707         = cp_parser_assignment_expression (parser, cast_p, pidk);
6708       /* If this is the first assignment-expression, we can just
6709          save it away.  */
6710       if (!expression)
6711         expression = assignment_expression;
6712       else
6713         expression = build_x_compound_expr (expression,
6714                                             assignment_expression,
6715                                             tf_warning_or_error);
6716       /* If the next token is not a comma, then we are done with the
6717          expression.  */
6718       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6719         break;
6720       /* Consume the `,'.  */
6721       cp_lexer_consume_token (parser->lexer);
6722       /* A comma operator cannot appear in a constant-expression.  */
6723       if (cp_parser_non_integral_constant_expression (parser,
6724                                                       "a comma operator"))
6725         expression = error_mark_node;
6726     }
6727
6728   return expression;
6729 }
6730
6731 /* Parse a constant-expression.
6732
6733    constant-expression:
6734      conditional-expression
6735
6736   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6737   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6738   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6739   is false, NON_CONSTANT_P should be NULL.  */
6740
6741 static tree
6742 cp_parser_constant_expression (cp_parser* parser,
6743                                bool allow_non_constant_p,
6744                                bool *non_constant_p)
6745 {
6746   bool saved_integral_constant_expression_p;
6747   bool saved_allow_non_integral_constant_expression_p;
6748   bool saved_non_integral_constant_expression_p;
6749   tree expression;
6750
6751   /* It might seem that we could simply parse the
6752      conditional-expression, and then check to see if it were
6753      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6754      one that the compiler can figure out is constant, possibly after
6755      doing some simplifications or optimizations.  The standard has a
6756      precise definition of constant-expression, and we must honor
6757      that, even though it is somewhat more restrictive.
6758
6759      For example:
6760
6761        int i[(2, 3)];
6762
6763      is not a legal declaration, because `(2, 3)' is not a
6764      constant-expression.  The `,' operator is forbidden in a
6765      constant-expression.  However, GCC's constant-folding machinery
6766      will fold this operation to an INTEGER_CST for `3'.  */
6767
6768   /* Save the old settings.  */
6769   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6770   saved_allow_non_integral_constant_expression_p
6771     = parser->allow_non_integral_constant_expression_p;
6772   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6773   /* We are now parsing a constant-expression.  */
6774   parser->integral_constant_expression_p = true;
6775   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6776   parser->non_integral_constant_expression_p = false;
6777   /* Although the grammar says "conditional-expression", we parse an
6778      "assignment-expression", which also permits "throw-expression"
6779      and the use of assignment operators.  In the case that
6780      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6781      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6782      actually essential that we look for an assignment-expression.
6783      For example, cp_parser_initializer_clauses uses this function to
6784      determine whether a particular assignment-expression is in fact
6785      constant.  */
6786   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6787   /* Restore the old settings.  */
6788   parser->integral_constant_expression_p
6789     = saved_integral_constant_expression_p;
6790   parser->allow_non_integral_constant_expression_p
6791     = saved_allow_non_integral_constant_expression_p;
6792   if (allow_non_constant_p)
6793     *non_constant_p = parser->non_integral_constant_expression_p;
6794   else if (parser->non_integral_constant_expression_p)
6795     expression = error_mark_node;
6796   parser->non_integral_constant_expression_p
6797     = saved_non_integral_constant_expression_p;
6798
6799   return expression;
6800 }
6801
6802 /* Parse __builtin_offsetof.
6803
6804    offsetof-expression:
6805      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6806
6807    offsetof-member-designator:
6808      id-expression
6809      | offsetof-member-designator "." id-expression
6810      | offsetof-member-designator "[" expression "]"
6811      | offsetof-member-designator "->" id-expression  */
6812
6813 static tree
6814 cp_parser_builtin_offsetof (cp_parser *parser)
6815 {
6816   int save_ice_p, save_non_ice_p;
6817   tree type, expr;
6818   cp_id_kind dummy;
6819   cp_token *token;
6820
6821   /* We're about to accept non-integral-constant things, but will
6822      definitely yield an integral constant expression.  Save and
6823      restore these values around our local parsing.  */
6824   save_ice_p = parser->integral_constant_expression_p;
6825   save_non_ice_p = parser->non_integral_constant_expression_p;
6826
6827   /* Consume the "__builtin_offsetof" token.  */
6828   cp_lexer_consume_token (parser->lexer);
6829   /* Consume the opening `('.  */
6830   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6831   /* Parse the type-id.  */
6832   type = cp_parser_type_id (parser);
6833   /* Look for the `,'.  */
6834   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6835   token = cp_lexer_peek_token (parser->lexer);
6836
6837   /* Build the (type *)null that begins the traditional offsetof macro.  */
6838   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6839                             tf_warning_or_error);
6840
6841   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6842   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6843                                                  true, &dummy, token->location);
6844   while (true)
6845     {
6846       token = cp_lexer_peek_token (parser->lexer);
6847       switch (token->type)
6848         {
6849         case CPP_OPEN_SQUARE:
6850           /* offsetof-member-designator "[" expression "]" */
6851           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6852           break;
6853
6854         case CPP_DEREF:
6855           /* offsetof-member-designator "->" identifier */
6856           expr = grok_array_decl (expr, integer_zero_node);
6857           /* FALLTHRU */
6858
6859         case CPP_DOT:
6860           /* offsetof-member-designator "." identifier */
6861           cp_lexer_consume_token (parser->lexer);
6862           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6863                                                          expr, true, &dummy,
6864                                                          token->location);
6865           break;
6866
6867         case CPP_CLOSE_PAREN:
6868           /* Consume the ")" token.  */
6869           cp_lexer_consume_token (parser->lexer);
6870           goto success;
6871
6872         default:
6873           /* Error.  We know the following require will fail, but
6874              that gives the proper error message.  */
6875           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6876           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6877           expr = error_mark_node;
6878           goto failure;
6879         }
6880     }
6881
6882  success:
6883   /* If we're processing a template, we can't finish the semantics yet.
6884      Otherwise we can fold the entire expression now.  */
6885   if (processing_template_decl)
6886     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6887   else
6888     expr = finish_offsetof (expr);
6889
6890  failure:
6891   parser->integral_constant_expression_p = save_ice_p;
6892   parser->non_integral_constant_expression_p = save_non_ice_p;
6893
6894   return expr;
6895 }
6896
6897 /* Parse a trait expression.  */
6898
6899 static tree
6900 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6901 {
6902   cp_trait_kind kind;
6903   tree type1, type2 = NULL_TREE;
6904   bool binary = false;
6905   cp_decl_specifier_seq decl_specs;
6906
6907   switch (keyword)
6908     {
6909     case RID_HAS_NOTHROW_ASSIGN:
6910       kind = CPTK_HAS_NOTHROW_ASSIGN;
6911       break;
6912     case RID_HAS_NOTHROW_CONSTRUCTOR:
6913       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6914       break;
6915     case RID_HAS_NOTHROW_COPY:
6916       kind = CPTK_HAS_NOTHROW_COPY;
6917       break;
6918     case RID_HAS_TRIVIAL_ASSIGN:
6919       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6920       break;
6921     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6922       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6923       break;
6924     case RID_HAS_TRIVIAL_COPY:
6925       kind = CPTK_HAS_TRIVIAL_COPY;
6926       break;
6927     case RID_HAS_TRIVIAL_DESTRUCTOR:
6928       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6929       break;
6930     case RID_HAS_VIRTUAL_DESTRUCTOR:
6931       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6932       break;
6933     case RID_IS_ABSTRACT:
6934       kind = CPTK_IS_ABSTRACT;
6935       break;
6936     case RID_IS_BASE_OF:
6937       kind = CPTK_IS_BASE_OF;
6938       binary = true;
6939       break;
6940     case RID_IS_CLASS:
6941       kind = CPTK_IS_CLASS;
6942       break;
6943     case RID_IS_CONVERTIBLE_TO:
6944       kind = CPTK_IS_CONVERTIBLE_TO;
6945       binary = true;
6946       break;
6947     case RID_IS_EMPTY:
6948       kind = CPTK_IS_EMPTY;
6949       break;
6950     case RID_IS_ENUM:
6951       kind = CPTK_IS_ENUM;
6952       break;
6953     case RID_IS_POD:
6954       kind = CPTK_IS_POD;
6955       break;
6956     case RID_IS_POLYMORPHIC:
6957       kind = CPTK_IS_POLYMORPHIC;
6958       break;
6959     case RID_IS_STD_LAYOUT:
6960       kind = CPTK_IS_STD_LAYOUT;
6961       break;
6962     case RID_IS_TRIVIAL:
6963       kind = CPTK_IS_TRIVIAL;
6964       break;
6965     case RID_IS_UNION:
6966       kind = CPTK_IS_UNION;
6967       break;
6968     default:
6969       gcc_unreachable ();
6970     }
6971
6972   /* Consume the token.  */
6973   cp_lexer_consume_token (parser->lexer);
6974
6975   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6976
6977   type1 = cp_parser_type_id (parser);
6978
6979   if (type1 == error_mark_node)
6980     return error_mark_node;
6981
6982   /* Build a trivial decl-specifier-seq.  */
6983   clear_decl_specs (&decl_specs);
6984   decl_specs.type = type1;
6985
6986   /* Call grokdeclarator to figure out what type this is.  */
6987   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6988                           /*initialized=*/0, /*attrlist=*/NULL);
6989
6990   if (binary)
6991     {
6992       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6993  
6994       type2 = cp_parser_type_id (parser);
6995
6996       if (type2 == error_mark_node)
6997         return error_mark_node;
6998
6999       /* Build a trivial decl-specifier-seq.  */
7000       clear_decl_specs (&decl_specs);
7001       decl_specs.type = type2;
7002
7003       /* Call grokdeclarator to figure out what type this is.  */
7004       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7005                               /*initialized=*/0, /*attrlist=*/NULL);
7006     }
7007
7008   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7009
7010   /* Complete the trait expression, which may mean either processing
7011      the trait expr now or saving it for template instantiation.  */
7012   return finish_trait_expr (kind, type1, type2);
7013 }
7014
7015 /* Lambdas that appear in variable initializer or default argument scope
7016    get that in their mangling, so we need to record it.  We might as well
7017    use the count for function and namespace scopes as well.  */
7018 static GTY(()) tree lambda_scope;
7019 static GTY(()) int lambda_count;
7020 typedef struct GTY(()) tree_int
7021 {
7022   tree t;
7023   int i;
7024 } tree_int;
7025 DEF_VEC_O(tree_int);
7026 DEF_VEC_ALLOC_O(tree_int,gc);
7027 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7028
7029 static void
7030 start_lambda_scope (tree decl)
7031 {
7032   tree_int ti;
7033   gcc_assert (decl);
7034   /* Once we're inside a function, we ignore other scopes and just push
7035      the function again so that popping works properly.  */
7036   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7037     decl = current_function_decl;
7038   ti.t = lambda_scope;
7039   ti.i = lambda_count;
7040   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7041   if (lambda_scope != decl)
7042     {
7043       /* Don't reset the count if we're still in the same function.  */
7044       lambda_scope = decl;
7045       lambda_count = 0;
7046     }
7047 }
7048
7049 static void
7050 record_lambda_scope (tree lambda)
7051 {
7052   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7053   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7054 }
7055
7056 static void
7057 finish_lambda_scope (void)
7058 {
7059   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7060   if (lambda_scope != p->t)
7061     {
7062       lambda_scope = p->t;
7063       lambda_count = p->i;
7064     }
7065   VEC_pop (tree_int, lambda_scope_stack);
7066 }
7067
7068 /* Parse a lambda expression.
7069
7070    lambda-expression:
7071      lambda-introducer lambda-declarator [opt] compound-statement
7072
7073    Returns a representation of the expression.  */
7074
7075 static tree
7076 cp_parser_lambda_expression (cp_parser* parser)
7077 {
7078   tree lambda_expr = build_lambda_expr ();
7079   tree type;
7080
7081   LAMBDA_EXPR_LOCATION (lambda_expr)
7082     = cp_lexer_peek_token (parser->lexer)->location;
7083
7084   /* We may be in the middle of deferred access check.  Disable
7085      it now.  */
7086   push_deferring_access_checks (dk_no_deferred);
7087
7088   cp_parser_lambda_introducer (parser, lambda_expr);
7089
7090   type = begin_lambda_type (lambda_expr);
7091
7092   record_lambda_scope (lambda_expr);
7093
7094   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7095   determine_visibility (TYPE_NAME (type));
7096
7097   /* Now that we've started the type, add the capture fields for any
7098      explicit captures.  */
7099   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7100
7101   {
7102     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7103     unsigned int saved_num_template_parameter_lists
7104         = parser->num_template_parameter_lists;
7105
7106     parser->num_template_parameter_lists = 0;
7107
7108     /* By virtue of defining a local class, a lambda expression has access to
7109        the private variables of enclosing classes.  */
7110
7111     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7112
7113     cp_parser_lambda_body (parser, lambda_expr);
7114
7115     /* The capture list was built up in reverse order; fix that now.  */
7116     {
7117       tree newlist = NULL_TREE;
7118       tree elt, next;
7119
7120       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7121            elt; elt = next)
7122         {
7123           tree field = TREE_PURPOSE (elt);
7124           char *buf;
7125
7126           next = TREE_CHAIN (elt);
7127           TREE_CHAIN (elt) = newlist;
7128           newlist = elt;
7129
7130           /* Also add __ to the beginning of the field name so that code
7131              outside the lambda body can't see the captured name.  We could
7132              just remove the name entirely, but this is more useful for
7133              debugging.  */
7134           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7135             /* The 'this' capture already starts with __.  */
7136             continue;
7137
7138           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7139           buf[1] = buf[0] = '_';
7140           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7141                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7142           DECL_NAME (field) = get_identifier (buf);
7143         }
7144       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7145     }
7146
7147     maybe_add_lambda_conv_op (type);
7148
7149     type = finish_struct (type, /*attributes=*/NULL_TREE);
7150
7151     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7152   }
7153
7154   pop_deferring_access_checks ();
7155
7156   return build_lambda_object (lambda_expr);
7157 }
7158
7159 /* Parse the beginning of a lambda expression.
7160
7161    lambda-introducer:
7162      [ lambda-capture [opt] ]
7163
7164    LAMBDA_EXPR is the current representation of the lambda expression.  */
7165
7166 static void
7167 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7168 {
7169   /* Need commas after the first capture.  */
7170   bool first = true;
7171
7172   /* Eat the leading `['.  */
7173   cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7174
7175   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7176   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7177       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7178     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7179   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7180     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7181
7182   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7183     {
7184       cp_lexer_consume_token (parser->lexer);
7185       first = false;
7186     }
7187
7188   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7189     {
7190       cp_token* capture_token;
7191       tree capture_id;
7192       tree capture_init_expr;
7193       cp_id_kind idk = CP_ID_KIND_NONE;
7194       bool explicit_init_p = false;
7195
7196       enum capture_kind_type
7197       {
7198         BY_COPY,
7199         BY_REFERENCE
7200       };
7201       enum capture_kind_type capture_kind = BY_COPY;
7202
7203       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7204         {
7205           error ("expected end of capture-list");
7206           return;
7207         }
7208
7209       if (first)
7210         first = false;
7211       else
7212         cp_parser_require (parser, CPP_COMMA, "%<,%>");
7213
7214       /* Possibly capture `this'.  */
7215       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7216         {
7217           cp_lexer_consume_token (parser->lexer);
7218           add_capture (lambda_expr,
7219                        /*id=*/get_identifier ("__this"),
7220                        /*initializer=*/finish_this_expr(),
7221                        /*by_reference_p=*/false,
7222                        explicit_init_p);
7223           continue;
7224         }
7225
7226       /* Remember whether we want to capture as a reference or not.  */
7227       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7228         {
7229           capture_kind = BY_REFERENCE;
7230           cp_lexer_consume_token (parser->lexer);
7231         }
7232
7233       /* Get the identifier.  */
7234       capture_token = cp_lexer_peek_token (parser->lexer);
7235       capture_id = cp_parser_identifier (parser);
7236
7237       if (capture_id == error_mark_node)
7238         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7239            delimiters, but I modified this to stop on unnested ']' as well.  It
7240            was already changed to stop on unnested '}', so the
7241            "closing_parenthesis" name is no more misleading with my change.  */
7242         {
7243           cp_parser_skip_to_closing_parenthesis (parser,
7244                                                  /*recovering=*/true,
7245                                                  /*or_comma=*/true,
7246                                                  /*consume_paren=*/true);
7247           break;
7248         }
7249
7250       /* Find the initializer for this capture.  */
7251       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7252         {
7253           /* An explicit expression exists.  */
7254           cp_lexer_consume_token (parser->lexer);
7255           pedwarn (input_location, OPT_pedantic,
7256                    "ISO C++ does not allow initializers "
7257                    "in lambda expression capture lists");
7258           capture_init_expr = cp_parser_assignment_expression (parser,
7259                                                                /*cast_p=*/true,
7260                                                                &idk);
7261           explicit_init_p = true;
7262         }
7263       else
7264         {
7265           const char* error_msg;
7266
7267           /* Turn the identifier into an id-expression.  */
7268           capture_init_expr
7269             = cp_parser_lookup_name
7270                 (parser,
7271                  capture_id,
7272                  none_type,
7273                  /*is_template=*/false,
7274                  /*is_namespace=*/false,
7275                  /*check_dependency=*/true,
7276                  /*ambiguous_decls=*/NULL,
7277                  capture_token->location);
7278
7279           capture_init_expr
7280             = finish_id_expression
7281                 (capture_id,
7282                  capture_init_expr,
7283                  parser->scope,
7284                  &idk,
7285                  /*integral_constant_expression_p=*/false,
7286                  /*allow_non_integral_constant_expression_p=*/false,
7287                  /*non_integral_constant_expression_p=*/NULL,
7288                  /*template_p=*/false,
7289                  /*done=*/true,
7290                  /*address_p=*/false,
7291                  /*template_arg_p=*/false,
7292                  &error_msg,
7293                  capture_token->location);
7294         }
7295
7296       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7297         capture_init_expr
7298           = unqualified_name_lookup_error (capture_init_expr);
7299
7300       add_capture (lambda_expr,
7301                    capture_id,
7302                    capture_init_expr,
7303                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7304                    explicit_init_p);
7305     }
7306
7307   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7308 }
7309
7310 /* Parse the (optional) middle of a lambda expression.
7311
7312    lambda-declarator:
7313      ( parameter-declaration-clause [opt] )
7314        attribute-specifier [opt]
7315        mutable [opt]
7316        exception-specification [opt]
7317        lambda-return-type-clause [opt]
7318
7319    LAMBDA_EXPR is the current representation of the lambda expression.  */
7320
7321 static void
7322 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7323 {
7324   /* 5.1.1.4 of the standard says:
7325        If a lambda-expression does not include a lambda-declarator, it is as if
7326        the lambda-declarator were ().
7327      This means an empty parameter list, no attributes, and no exception
7328      specification.  */
7329   tree param_list = void_list_node;
7330   tree attributes = NULL_TREE;
7331   tree exception_spec = NULL_TREE;
7332   tree t;
7333
7334   /* The lambda-declarator is optional, but must begin with an opening
7335      parenthesis if present.  */
7336   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7337     {
7338       cp_lexer_consume_token (parser->lexer);
7339
7340       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7341
7342       /* Parse parameters.  */
7343       param_list = cp_parser_parameter_declaration_clause (parser);
7344
7345       /* Default arguments shall not be specified in the
7346          parameter-declaration-clause of a lambda-declarator.  */
7347       for (t = param_list; t; t = TREE_CHAIN (t))
7348         if (TREE_PURPOSE (t))
7349           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7350                    "default argument specified for lambda parameter");
7351
7352       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7353
7354       attributes = cp_parser_attributes_opt (parser);
7355
7356       /* Parse optional `mutable' keyword.  */
7357       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7358         {
7359           cp_lexer_consume_token (parser->lexer);
7360           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7361         }
7362
7363       /* Parse optional exception specification.  */
7364       exception_spec = cp_parser_exception_specification_opt (parser);
7365
7366       /* Parse optional trailing return type.  */
7367       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7368         {
7369           cp_lexer_consume_token (parser->lexer);
7370           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7371         }
7372
7373       /* The function parameters must be in scope all the way until after the
7374          trailing-return-type in case of decltype.  */
7375       for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7376         pop_binding (DECL_NAME (t), t);
7377
7378       leave_scope ();
7379     }
7380
7381   /* Create the function call operator.
7382
7383      Messing with declarators like this is no uglier than building up the
7384      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7385      other code.  */
7386   {
7387     cp_decl_specifier_seq return_type_specs;
7388     cp_declarator* declarator;
7389     tree fco;
7390     int quals;
7391     void *p;
7392
7393     clear_decl_specs (&return_type_specs);
7394     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7395       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7396     else
7397       /* Maybe we will deduce the return type later, but we can use void
7398          as a placeholder return type anyways.  */
7399       return_type_specs.type = void_type_node;
7400
7401     p = obstack_alloc (&declarator_obstack, 0);
7402
7403     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7404                                      sfk_none);
7405
7406     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7407              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7408     declarator = make_call_declarator (declarator, param_list, quals,
7409                                        exception_spec,
7410                                        /*late_return_type=*/NULL_TREE);
7411     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7412
7413     fco = grokmethod (&return_type_specs,
7414                       declarator,
7415                       attributes);
7416     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7417     DECL_ARTIFICIAL (fco) = 1;
7418
7419     finish_member_declaration (fco);
7420
7421     obstack_free (&declarator_obstack, p);
7422   }
7423 }
7424
7425 /* Parse the body of a lambda expression, which is simply
7426
7427    compound-statement
7428
7429    but which requires special handling.
7430    LAMBDA_EXPR is the current representation of the lambda expression.  */
7431
7432 static void
7433 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7434 {
7435   bool nested = (current_function_decl != NULL_TREE);
7436   if (nested)
7437     push_function_context ();
7438
7439   /* Finish the function call operator
7440      - class_specifier
7441      + late_parsing_for_member
7442      + function_definition_after_declarator
7443      + ctor_initializer_opt_and_function_body  */
7444   {
7445     tree fco = lambda_function (lambda_expr);
7446     tree body;
7447     bool done = false;
7448
7449     /* Let the front end know that we are going to be defining this
7450        function.  */
7451     start_preparsed_function (fco,
7452                               NULL_TREE,
7453                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7454
7455     start_lambda_scope (fco);
7456     body = begin_function_body ();
7457
7458     /* 5.1.1.4 of the standard says:
7459          If a lambda-expression does not include a trailing-return-type, it
7460          is as if the trailing-return-type denotes the following type:
7461           * if the compound-statement is of the form
7462                { return attribute-specifier [opt] expression ; }
7463              the type of the returned expression after lvalue-to-rvalue
7464              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7465              (_conv.array_ 4.2), and function-to-pointer conversion
7466              (_conv.func_ 4.3);
7467           * otherwise, void.  */
7468
7469     /* In a lambda that has neither a lambda-return-type-clause
7470        nor a deducible form, errors should be reported for return statements
7471        in the body.  Since we used void as the placeholder return type, parsing
7472        the body as usual will give such desired behavior.  */
7473     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7474         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7475         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7476         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7477       {
7478         tree compound_stmt;
7479         tree expr = NULL_TREE;
7480         cp_id_kind idk = CP_ID_KIND_NONE;
7481
7482         /* Parse tentatively in case there's more after the initial return
7483            statement.  */
7484         cp_parser_parse_tentatively (parser);
7485
7486         cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7487         cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7488
7489         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7490
7491         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7492         cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7493
7494         if (cp_parser_parse_definitely (parser))
7495           {
7496             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7497
7498             compound_stmt = begin_compound_stmt (0);
7499             /* Will get error here if type not deduced yet.  */
7500             finish_return_stmt (expr);
7501             finish_compound_stmt (compound_stmt);
7502
7503             done = true;
7504           }
7505       }
7506
7507     if (!done)
7508       {
7509         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7510           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7511         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7512            cp_parser_compound_stmt does not pass it.  */
7513         cp_parser_function_body (parser);
7514         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7515       }
7516
7517     finish_function_body (body);
7518     finish_lambda_scope ();
7519
7520     /* Finish the function and generate code for it if necessary.  */
7521     expand_or_defer_fn (finish_function (/*inline*/2));
7522   }
7523
7524   if (nested)
7525     pop_function_context();
7526 }
7527
7528 /* Statements [gram.stmt.stmt]  */
7529
7530 /* Parse a statement.
7531
7532    statement:
7533      labeled-statement
7534      expression-statement
7535      compound-statement
7536      selection-statement
7537      iteration-statement
7538      jump-statement
7539      declaration-statement
7540      try-block
7541
7542   IN_COMPOUND is true when the statement is nested inside a
7543   cp_parser_compound_statement; this matters for certain pragmas.
7544
7545   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7546   is a (possibly labeled) if statement which is not enclosed in braces
7547   and has an else clause.  This is used to implement -Wparentheses.  */
7548
7549 static void
7550 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7551                      bool in_compound, bool *if_p)
7552 {
7553   tree statement;
7554   cp_token *token;
7555   location_t statement_location;
7556
7557  restart:
7558   if (if_p != NULL)
7559     *if_p = false;
7560   /* There is no statement yet.  */
7561   statement = NULL_TREE;
7562   /* Peek at the next token.  */
7563   token = cp_lexer_peek_token (parser->lexer);
7564   /* Remember the location of the first token in the statement.  */
7565   statement_location = token->location;
7566   /* If this is a keyword, then that will often determine what kind of
7567      statement we have.  */
7568   if (token->type == CPP_KEYWORD)
7569     {
7570       enum rid keyword = token->keyword;
7571
7572       switch (keyword)
7573         {
7574         case RID_CASE:
7575         case RID_DEFAULT:
7576           /* Looks like a labeled-statement with a case label.
7577              Parse the label, and then use tail recursion to parse
7578              the statement.  */
7579           cp_parser_label_for_labeled_statement (parser);
7580           goto restart;
7581
7582         case RID_IF:
7583         case RID_SWITCH:
7584           statement = cp_parser_selection_statement (parser, if_p);
7585           break;
7586
7587         case RID_WHILE:
7588         case RID_DO:
7589         case RID_FOR:
7590           statement = cp_parser_iteration_statement (parser);
7591           break;
7592
7593         case RID_BREAK:
7594         case RID_CONTINUE:
7595         case RID_RETURN:
7596         case RID_GOTO:
7597           statement = cp_parser_jump_statement (parser);
7598           break;
7599
7600           /* Objective-C++ exception-handling constructs.  */
7601         case RID_AT_TRY:
7602         case RID_AT_CATCH:
7603         case RID_AT_FINALLY:
7604         case RID_AT_SYNCHRONIZED:
7605         case RID_AT_THROW:
7606           statement = cp_parser_objc_statement (parser);
7607           break;
7608
7609         case RID_TRY:
7610           statement = cp_parser_try_block (parser);
7611           break;
7612
7613         case RID_NAMESPACE:
7614           /* This must be a namespace alias definition.  */
7615           cp_parser_declaration_statement (parser);
7616           return;
7617           
7618         default:
7619           /* It might be a keyword like `int' that can start a
7620              declaration-statement.  */
7621           break;
7622         }
7623     }
7624   else if (token->type == CPP_NAME)
7625     {
7626       /* If the next token is a `:', then we are looking at a
7627          labeled-statement.  */
7628       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7629       if (token->type == CPP_COLON)
7630         {
7631           /* Looks like a labeled-statement with an ordinary label.
7632              Parse the label, and then use tail recursion to parse
7633              the statement.  */
7634           cp_parser_label_for_labeled_statement (parser);
7635           goto restart;
7636         }
7637     }
7638   /* Anything that starts with a `{' must be a compound-statement.  */
7639   else if (token->type == CPP_OPEN_BRACE)
7640     statement = cp_parser_compound_statement (parser, NULL, false);
7641   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7642      a statement all its own.  */
7643   else if (token->type == CPP_PRAGMA)
7644     {
7645       /* Only certain OpenMP pragmas are attached to statements, and thus
7646          are considered statements themselves.  All others are not.  In
7647          the context of a compound, accept the pragma as a "statement" and
7648          return so that we can check for a close brace.  Otherwise we
7649          require a real statement and must go back and read one.  */
7650       if (in_compound)
7651         cp_parser_pragma (parser, pragma_compound);
7652       else if (!cp_parser_pragma (parser, pragma_stmt))
7653         goto restart;
7654       return;
7655     }
7656   else if (token->type == CPP_EOF)
7657     {
7658       cp_parser_error (parser, "expected statement");
7659       return;
7660     }
7661
7662   /* Everything else must be a declaration-statement or an
7663      expression-statement.  Try for the declaration-statement
7664      first, unless we are looking at a `;', in which case we know that
7665      we have an expression-statement.  */
7666   if (!statement)
7667     {
7668       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7669         {
7670           cp_parser_parse_tentatively (parser);
7671           /* Try to parse the declaration-statement.  */
7672           cp_parser_declaration_statement (parser);
7673           /* If that worked, we're done.  */
7674           if (cp_parser_parse_definitely (parser))
7675             return;
7676         }
7677       /* Look for an expression-statement instead.  */
7678       statement = cp_parser_expression_statement (parser, in_statement_expr);
7679     }
7680
7681   /* Set the line number for the statement.  */
7682   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7683     SET_EXPR_LOCATION (statement, statement_location);
7684 }
7685
7686 /* Parse the label for a labeled-statement, i.e.
7687
7688    identifier :
7689    case constant-expression :
7690    default :
7691
7692    GNU Extension:
7693    case constant-expression ... constant-expression : statement
7694
7695    When a label is parsed without errors, the label is added to the
7696    parse tree by the finish_* functions, so this function doesn't
7697    have to return the label.  */
7698
7699 static void
7700 cp_parser_label_for_labeled_statement (cp_parser* parser)
7701 {
7702   cp_token *token;
7703   tree label = NULL_TREE;
7704
7705   /* The next token should be an identifier.  */
7706   token = cp_lexer_peek_token (parser->lexer);
7707   if (token->type != CPP_NAME
7708       && token->type != CPP_KEYWORD)
7709     {
7710       cp_parser_error (parser, "expected labeled-statement");
7711       return;
7712     }
7713
7714   switch (token->keyword)
7715     {
7716     case RID_CASE:
7717       {
7718         tree expr, expr_hi;
7719         cp_token *ellipsis;
7720
7721         /* Consume the `case' token.  */
7722         cp_lexer_consume_token (parser->lexer);
7723         /* Parse the constant-expression.  */
7724         expr = cp_parser_constant_expression (parser,
7725                                               /*allow_non_constant_p=*/false,
7726                                               NULL);
7727
7728         ellipsis = cp_lexer_peek_token (parser->lexer);
7729         if (ellipsis->type == CPP_ELLIPSIS)
7730           {
7731             /* Consume the `...' token.  */
7732             cp_lexer_consume_token (parser->lexer);
7733             expr_hi =
7734               cp_parser_constant_expression (parser,
7735                                              /*allow_non_constant_p=*/false,
7736                                              NULL);
7737             /* We don't need to emit warnings here, as the common code
7738                will do this for us.  */
7739           }
7740         else
7741           expr_hi = NULL_TREE;
7742
7743         if (parser->in_switch_statement_p)
7744           finish_case_label (token->location, expr, expr_hi);
7745         else
7746           error_at (token->location,
7747                     "case label %qE not within a switch statement",
7748                     expr);
7749       }
7750       break;
7751
7752     case RID_DEFAULT:
7753       /* Consume the `default' token.  */
7754       cp_lexer_consume_token (parser->lexer);
7755
7756       if (parser->in_switch_statement_p)
7757         finish_case_label (token->location, NULL_TREE, NULL_TREE);
7758       else
7759         error_at (token->location, "case label not within a switch statement");
7760       break;
7761
7762     default:
7763       /* Anything else must be an ordinary label.  */
7764       label = finish_label_stmt (cp_parser_identifier (parser));
7765       break;
7766     }
7767
7768   /* Require the `:' token.  */
7769   cp_parser_require (parser, CPP_COLON, "%<:%>");
7770
7771   /* An ordinary label may optionally be followed by attributes.
7772      However, this is only permitted if the attributes are then
7773      followed by a semicolon.  This is because, for backward
7774      compatibility, when parsing
7775        lab: __attribute__ ((unused)) int i;
7776      we want the attribute to attach to "i", not "lab".  */
7777   if (label != NULL_TREE
7778       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7779     {
7780       tree attrs;
7781
7782       cp_parser_parse_tentatively (parser);
7783       attrs = cp_parser_attributes_opt (parser);
7784       if (attrs == NULL_TREE
7785           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7786         cp_parser_abort_tentative_parse (parser);
7787       else if (!cp_parser_parse_definitely (parser))
7788         ;
7789       else
7790         cplus_decl_attributes (&label, attrs, 0);
7791     }
7792 }
7793
7794 /* Parse an expression-statement.
7795
7796    expression-statement:
7797      expression [opt] ;
7798
7799    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7800    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7801    indicates whether this expression-statement is part of an
7802    expression statement.  */
7803
7804 static tree
7805 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7806 {
7807   tree statement = NULL_TREE;
7808   cp_token *token = cp_lexer_peek_token (parser->lexer);
7809
7810   /* If the next token is a ';', then there is no expression
7811      statement.  */
7812   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7813     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7814
7815   /* Give a helpful message for "A<T>::type t;" and the like.  */
7816   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7817       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
7818     {
7819       if (TREE_CODE (statement) == SCOPE_REF)
7820         error_at (token->location, "need %<typename%> before %qE because "
7821                   "%qT is a dependent scope",
7822                   statement, TREE_OPERAND (statement, 0));
7823       else if (is_overloaded_fn (statement)
7824                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
7825         {
7826           /* A::A a; */
7827           tree fn = get_first_fn (statement);
7828           error_at (token->location,
7829                     "%<%T::%D%> names the constructor, not the type",
7830                     DECL_CONTEXT (fn), DECL_NAME (fn));
7831         }
7832     }
7833
7834   /* Consume the final `;'.  */
7835   cp_parser_consume_semicolon_at_end_of_statement (parser);
7836
7837   if (in_statement_expr
7838       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7839     /* This is the final expression statement of a statement
7840        expression.  */
7841     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7842   else if (statement)
7843     statement = finish_expr_stmt (statement);
7844   else
7845     finish_stmt ();
7846
7847   return statement;
7848 }
7849
7850 /* Parse a compound-statement.
7851
7852    compound-statement:
7853      { statement-seq [opt] }
7854
7855    GNU extension:
7856
7857    compound-statement:
7858      { label-declaration-seq [opt] statement-seq [opt] }
7859
7860    label-declaration-seq:
7861      label-declaration
7862      label-declaration-seq label-declaration
7863
7864    Returns a tree representing the statement.  */
7865
7866 static tree
7867 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7868                               bool in_try)
7869 {
7870   tree compound_stmt;
7871
7872   /* Consume the `{'.  */
7873   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7874     return error_mark_node;
7875   /* Begin the compound-statement.  */
7876   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7877   /* If the next keyword is `__label__' we have a label declaration.  */
7878   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7879     cp_parser_label_declaration (parser);
7880   /* Parse an (optional) statement-seq.  */
7881   cp_parser_statement_seq_opt (parser, in_statement_expr);
7882   /* Finish the compound-statement.  */
7883   finish_compound_stmt (compound_stmt);
7884   /* Consume the `}'.  */
7885   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7886
7887   return compound_stmt;
7888 }
7889
7890 /* Parse an (optional) statement-seq.
7891
7892    statement-seq:
7893      statement
7894      statement-seq [opt] statement  */
7895
7896 static void
7897 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7898 {
7899   /* Scan statements until there aren't any more.  */
7900   while (true)
7901     {
7902       cp_token *token = cp_lexer_peek_token (parser->lexer);
7903
7904       /* If we're looking at a `}', then we've run out of statements.  */
7905       if (token->type == CPP_CLOSE_BRACE
7906           || token->type == CPP_EOF
7907           || token->type == CPP_PRAGMA_EOL)
7908         break;
7909       
7910       /* If we are in a compound statement and find 'else' then
7911          something went wrong.  */
7912       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7913         {
7914           if (parser->in_statement & IN_IF_STMT) 
7915             break;
7916           else
7917             {
7918               token = cp_lexer_consume_token (parser->lexer);
7919               error_at (token->location, "%<else%> without a previous %<if%>");
7920             }
7921         }
7922
7923       /* Parse the statement.  */
7924       cp_parser_statement (parser, in_statement_expr, true, NULL);
7925     }
7926 }
7927
7928 /* Parse a selection-statement.
7929
7930    selection-statement:
7931      if ( condition ) statement
7932      if ( condition ) statement else statement
7933      switch ( condition ) statement
7934
7935    Returns the new IF_STMT or SWITCH_STMT.
7936
7937    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7938    is a (possibly labeled) if statement which is not enclosed in
7939    braces and has an else clause.  This is used to implement
7940    -Wparentheses.  */
7941
7942 static tree
7943 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7944 {
7945   cp_token *token;
7946   enum rid keyword;
7947
7948   if (if_p != NULL)
7949     *if_p = false;
7950
7951   /* Peek at the next token.  */
7952   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7953
7954   /* See what kind of keyword it is.  */
7955   keyword = token->keyword;
7956   switch (keyword)
7957     {
7958     case RID_IF:
7959     case RID_SWITCH:
7960       {
7961         tree statement;
7962         tree condition;
7963
7964         /* Look for the `('.  */
7965         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7966           {
7967             cp_parser_skip_to_end_of_statement (parser);
7968             return error_mark_node;
7969           }
7970
7971         /* Begin the selection-statement.  */
7972         if (keyword == RID_IF)
7973           statement = begin_if_stmt ();
7974         else
7975           statement = begin_switch_stmt ();
7976
7977         /* Parse the condition.  */
7978         condition = cp_parser_condition (parser);
7979         /* Look for the `)'.  */
7980         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7981           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7982                                                  /*consume_paren=*/true);
7983
7984         if (keyword == RID_IF)
7985           {
7986             bool nested_if;
7987             unsigned char in_statement;
7988
7989             /* Add the condition.  */
7990             finish_if_stmt_cond (condition, statement);
7991
7992             /* Parse the then-clause.  */
7993             in_statement = parser->in_statement;
7994             parser->in_statement |= IN_IF_STMT;
7995             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7996               {
7997                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7998                 add_stmt (build_empty_stmt (loc));
7999                 cp_lexer_consume_token (parser->lexer);
8000                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8001                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8002                               "empty body in an %<if%> statement");
8003                 nested_if = false;
8004               }
8005             else
8006               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8007             parser->in_statement = in_statement;
8008
8009             finish_then_clause (statement);
8010
8011             /* If the next token is `else', parse the else-clause.  */
8012             if (cp_lexer_next_token_is_keyword (parser->lexer,
8013                                                 RID_ELSE))
8014               {
8015                 /* Consume the `else' keyword.  */
8016                 cp_lexer_consume_token (parser->lexer);
8017                 begin_else_clause (statement);
8018                 /* Parse the else-clause.  */
8019                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8020                   {
8021                     location_t loc;
8022                     loc = cp_lexer_peek_token (parser->lexer)->location;
8023                     warning_at (loc,
8024                                 OPT_Wempty_body, "suggest braces around "
8025                                 "empty body in an %<else%> statement");
8026                     add_stmt (build_empty_stmt (loc));
8027                     cp_lexer_consume_token (parser->lexer);
8028                   }
8029                 else
8030                   cp_parser_implicitly_scoped_statement (parser, NULL);
8031
8032                 finish_else_clause (statement);
8033
8034                 /* If we are currently parsing a then-clause, then
8035                    IF_P will not be NULL.  We set it to true to
8036                    indicate that this if statement has an else clause.
8037                    This may trigger the Wparentheses warning below
8038                    when we get back up to the parent if statement.  */
8039                 if (if_p != NULL)
8040                   *if_p = true;
8041               }
8042             else
8043               {
8044                 /* This if statement does not have an else clause.  If
8045                    NESTED_IF is true, then the then-clause is an if
8046                    statement which does have an else clause.  We warn
8047                    about the potential ambiguity.  */
8048                 if (nested_if)
8049                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8050                               "suggest explicit braces to avoid ambiguous"
8051                               " %<else%>");
8052               }
8053
8054             /* Now we're all done with the if-statement.  */
8055             finish_if_stmt (statement);
8056           }
8057         else
8058           {
8059             bool in_switch_statement_p;
8060             unsigned char in_statement;
8061
8062             /* Add the condition.  */
8063             finish_switch_cond (condition, statement);
8064
8065             /* Parse the body of the switch-statement.  */
8066             in_switch_statement_p = parser->in_switch_statement_p;
8067             in_statement = parser->in_statement;
8068             parser->in_switch_statement_p = true;
8069             parser->in_statement |= IN_SWITCH_STMT;
8070             cp_parser_implicitly_scoped_statement (parser, NULL);
8071             parser->in_switch_statement_p = in_switch_statement_p;
8072             parser->in_statement = in_statement;
8073
8074             /* Now we're all done with the switch-statement.  */
8075             finish_switch_stmt (statement);
8076           }
8077
8078         return statement;
8079       }
8080       break;
8081
8082     default:
8083       cp_parser_error (parser, "expected selection-statement");
8084       return error_mark_node;
8085     }
8086 }
8087
8088 /* Parse a condition.
8089
8090    condition:
8091      expression
8092      type-specifier-seq declarator = initializer-clause
8093      type-specifier-seq declarator braced-init-list
8094
8095    GNU Extension:
8096
8097    condition:
8098      type-specifier-seq declarator asm-specification [opt]
8099        attributes [opt] = assignment-expression
8100
8101    Returns the expression that should be tested.  */
8102
8103 static tree
8104 cp_parser_condition (cp_parser* parser)
8105 {
8106   cp_decl_specifier_seq type_specifiers;
8107   const char *saved_message;
8108
8109   /* Try the declaration first.  */
8110   cp_parser_parse_tentatively (parser);
8111   /* New types are not allowed in the type-specifier-seq for a
8112      condition.  */
8113   saved_message = parser->type_definition_forbidden_message;
8114   parser->type_definition_forbidden_message
8115     = G_("types may not be defined in conditions");
8116   /* Parse the type-specifier-seq.  */
8117   cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8118                                 /*is_trailing_return=*/false,
8119                                 &type_specifiers);
8120   /* Restore the saved message.  */
8121   parser->type_definition_forbidden_message = saved_message;
8122   /* If all is well, we might be looking at a declaration.  */
8123   if (!cp_parser_error_occurred (parser))
8124     {
8125       tree decl;
8126       tree asm_specification;
8127       tree attributes;
8128       cp_declarator *declarator;
8129       tree initializer = NULL_TREE;
8130
8131       /* Parse the declarator.  */
8132       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8133                                          /*ctor_dtor_or_conv_p=*/NULL,
8134                                          /*parenthesized_p=*/NULL,
8135                                          /*member_p=*/false);
8136       /* Parse the attributes.  */
8137       attributes = cp_parser_attributes_opt (parser);
8138       /* Parse the asm-specification.  */
8139       asm_specification = cp_parser_asm_specification_opt (parser);
8140       /* If the next token is not an `=' or '{', then we might still be
8141          looking at an expression.  For example:
8142
8143            if (A(a).x)
8144
8145          looks like a decl-specifier-seq and a declarator -- but then
8146          there is no `=', so this is an expression.  */
8147       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8148           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8149         cp_parser_simulate_error (parser);
8150         
8151       /* If we did see an `=' or '{', then we are looking at a declaration
8152          for sure.  */
8153       if (cp_parser_parse_definitely (parser))
8154         {
8155           tree pushed_scope;
8156           bool non_constant_p;
8157           bool flags = LOOKUP_ONLYCONVERTING;
8158
8159           /* Create the declaration.  */
8160           decl = start_decl (declarator, &type_specifiers,
8161                              /*initialized_p=*/true,
8162                              attributes, /*prefix_attributes=*/NULL_TREE,
8163                              &pushed_scope);
8164
8165           /* Parse the initializer.  */
8166           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8167             {
8168               initializer = cp_parser_braced_list (parser, &non_constant_p);
8169               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8170               flags = 0;
8171             }
8172           else
8173             {
8174               /* Consume the `='.  */
8175               cp_parser_require (parser, CPP_EQ, "%<=%>");
8176               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8177             }
8178           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8179             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8180
8181           if (!non_constant_p)
8182             initializer = fold_non_dependent_expr (initializer);
8183
8184           /* Process the initializer.  */
8185           cp_finish_decl (decl,
8186                           initializer, !non_constant_p,
8187                           asm_specification,
8188                           flags);
8189
8190           if (pushed_scope)
8191             pop_scope (pushed_scope);
8192
8193           return convert_from_reference (decl);
8194         }
8195     }
8196   /* If we didn't even get past the declarator successfully, we are
8197      definitely not looking at a declaration.  */
8198   else
8199     cp_parser_abort_tentative_parse (parser);
8200
8201   /* Otherwise, we are looking at an expression.  */
8202   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8203 }
8204
8205 /* Parse an iteration-statement.
8206
8207    iteration-statement:
8208      while ( condition ) statement
8209      do statement while ( expression ) ;
8210      for ( for-init-statement condition [opt] ; expression [opt] )
8211        statement
8212
8213    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
8214
8215 static tree
8216 cp_parser_iteration_statement (cp_parser* parser)
8217 {
8218   cp_token *token;
8219   enum rid keyword;
8220   tree statement;
8221   unsigned char in_statement;
8222
8223   /* Peek at the next token.  */
8224   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8225   if (!token)
8226     return error_mark_node;
8227
8228   /* Remember whether or not we are already within an iteration
8229      statement.  */
8230   in_statement = parser->in_statement;
8231
8232   /* See what kind of keyword it is.  */
8233   keyword = token->keyword;
8234   switch (keyword)
8235     {
8236     case RID_WHILE:
8237       {
8238         tree condition;
8239
8240         /* Begin the while-statement.  */
8241         statement = begin_while_stmt ();
8242         /* Look for the `('.  */
8243         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8244         /* Parse the condition.  */
8245         condition = cp_parser_condition (parser);
8246         finish_while_stmt_cond (condition, statement);
8247         /* Look for the `)'.  */
8248         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8249         /* Parse the dependent statement.  */
8250         parser->in_statement = IN_ITERATION_STMT;
8251         cp_parser_already_scoped_statement (parser);
8252         parser->in_statement = in_statement;
8253         /* We're done with the while-statement.  */
8254         finish_while_stmt (statement);
8255       }
8256       break;
8257
8258     case RID_DO:
8259       {
8260         tree expression;
8261
8262         /* Begin the do-statement.  */
8263         statement = begin_do_stmt ();
8264         /* Parse the body of the do-statement.  */
8265         parser->in_statement = IN_ITERATION_STMT;
8266         cp_parser_implicitly_scoped_statement (parser, NULL);
8267         parser->in_statement = in_statement;
8268         finish_do_body (statement);
8269         /* Look for the `while' keyword.  */
8270         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8271         /* Look for the `('.  */
8272         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8273         /* Parse the expression.  */
8274         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8275         /* We're done with the do-statement.  */
8276         finish_do_stmt (expression, statement);
8277         /* Look for the `)'.  */
8278         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8279         /* Look for the `;'.  */
8280         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8281       }
8282       break;
8283
8284     case RID_FOR:
8285       {
8286         tree condition = NULL_TREE;
8287         tree expression = NULL_TREE;
8288
8289         /* Begin the for-statement.  */
8290         statement = begin_for_stmt ();
8291         /* Look for the `('.  */
8292         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8293         /* Parse the initialization.  */
8294         cp_parser_for_init_statement (parser);
8295         finish_for_init_stmt (statement);
8296
8297         /* If there's a condition, process it.  */
8298         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8299           condition = cp_parser_condition (parser);
8300         finish_for_cond (condition, statement);
8301         /* Look for the `;'.  */
8302         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8303
8304         /* If there's an expression, process it.  */
8305         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8306           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8307         finish_for_expr (expression, statement);
8308         /* Look for the `)'.  */
8309         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8310
8311         /* Parse the body of the for-statement.  */
8312         parser->in_statement = IN_ITERATION_STMT;
8313         cp_parser_already_scoped_statement (parser);
8314         parser->in_statement = in_statement;
8315
8316         /* We're done with the for-statement.  */
8317         finish_for_stmt (statement);
8318       }
8319       break;
8320
8321     default:
8322       cp_parser_error (parser, "expected iteration-statement");
8323       statement = error_mark_node;
8324       break;
8325     }
8326
8327   return statement;
8328 }
8329
8330 /* Parse a for-init-statement.
8331
8332    for-init-statement:
8333      expression-statement
8334      simple-declaration  */
8335
8336 static void
8337 cp_parser_for_init_statement (cp_parser* parser)
8338 {
8339   /* If the next token is a `;', then we have an empty
8340      expression-statement.  Grammatically, this is also a
8341      simple-declaration, but an invalid one, because it does not
8342      declare anything.  Therefore, if we did not handle this case
8343      specially, we would issue an error message about an invalid
8344      declaration.  */
8345   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8346     {
8347       /* We're going to speculatively look for a declaration, falling back
8348          to an expression, if necessary.  */
8349       cp_parser_parse_tentatively (parser);
8350       /* Parse the declaration.  */
8351       cp_parser_simple_declaration (parser,
8352                                     /*function_definition_allowed_p=*/false);
8353       /* If the tentative parse failed, then we shall need to look for an
8354          expression-statement.  */
8355       if (cp_parser_parse_definitely (parser))
8356         return;
8357     }
8358
8359   cp_parser_expression_statement (parser, NULL_TREE);
8360 }
8361
8362 /* Parse a jump-statement.
8363
8364    jump-statement:
8365      break ;
8366      continue ;
8367      return expression [opt] ;
8368      return braced-init-list ;
8369      goto identifier ;
8370
8371    GNU extension:
8372
8373    jump-statement:
8374      goto * expression ;
8375
8376    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
8377
8378 static tree
8379 cp_parser_jump_statement (cp_parser* parser)
8380 {
8381   tree statement = error_mark_node;
8382   cp_token *token;
8383   enum rid keyword;
8384   unsigned char in_statement;
8385
8386   /* Peek at the next token.  */
8387   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8388   if (!token)
8389     return error_mark_node;
8390
8391   /* See what kind of keyword it is.  */
8392   keyword = token->keyword;
8393   switch (keyword)
8394     {
8395     case RID_BREAK:
8396       in_statement = parser->in_statement & ~IN_IF_STMT;      
8397       switch (in_statement)
8398         {
8399         case 0:
8400           error_at (token->location, "break statement not within loop or switch");
8401           break;
8402         default:
8403           gcc_assert ((in_statement & IN_SWITCH_STMT)
8404                       || in_statement == IN_ITERATION_STMT);
8405           statement = finish_break_stmt ();
8406           break;
8407         case IN_OMP_BLOCK:
8408           error_at (token->location, "invalid exit from OpenMP structured block");
8409           break;
8410         case IN_OMP_FOR:
8411           error_at (token->location, "break statement used with OpenMP for loop");
8412           break;
8413         }
8414       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8415       break;
8416
8417     case RID_CONTINUE:
8418       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8419         {
8420         case 0:
8421           error_at (token->location, "continue statement not within a loop");
8422           break;
8423         case IN_ITERATION_STMT:
8424         case IN_OMP_FOR:
8425           statement = finish_continue_stmt ();
8426           break;
8427         case IN_OMP_BLOCK:
8428           error_at (token->location, "invalid exit from OpenMP structured block");
8429           break;
8430         default:
8431           gcc_unreachable ();
8432         }
8433       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8434       break;
8435
8436     case RID_RETURN:
8437       {
8438         tree expr;
8439         bool expr_non_constant_p;
8440
8441         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8442           {
8443             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8444             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8445           }
8446         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8447           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8448         else
8449           /* If the next token is a `;', then there is no
8450              expression.  */
8451           expr = NULL_TREE;
8452         /* Build the return-statement.  */
8453         statement = finish_return_stmt (expr);
8454         /* Look for the final `;'.  */
8455         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8456       }
8457       break;
8458
8459     case RID_GOTO:
8460       /* Create the goto-statement.  */
8461       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8462         {
8463           /* Issue a warning about this use of a GNU extension.  */
8464           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8465           /* Consume the '*' token.  */
8466           cp_lexer_consume_token (parser->lexer);
8467           /* Parse the dependent expression.  */
8468           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8469         }
8470       else
8471         finish_goto_stmt (cp_parser_identifier (parser));
8472       /* Look for the final `;'.  */
8473       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8474       break;
8475
8476     default:
8477       cp_parser_error (parser, "expected jump-statement");
8478       break;
8479     }
8480
8481   return statement;
8482 }
8483
8484 /* Parse a declaration-statement.
8485
8486    declaration-statement:
8487      block-declaration  */
8488
8489 static void
8490 cp_parser_declaration_statement (cp_parser* parser)
8491 {
8492   void *p;
8493
8494   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8495   p = obstack_alloc (&declarator_obstack, 0);
8496
8497  /* Parse the block-declaration.  */
8498   cp_parser_block_declaration (parser, /*statement_p=*/true);
8499
8500   /* Free any declarators allocated.  */
8501   obstack_free (&declarator_obstack, p);
8502
8503   /* Finish off the statement.  */
8504   finish_stmt ();
8505 }
8506
8507 /* Some dependent statements (like `if (cond) statement'), are
8508    implicitly in their own scope.  In other words, if the statement is
8509    a single statement (as opposed to a compound-statement), it is
8510    none-the-less treated as if it were enclosed in braces.  Any
8511    declarations appearing in the dependent statement are out of scope
8512    after control passes that point.  This function parses a statement,
8513    but ensures that is in its own scope, even if it is not a
8514    compound-statement.
8515
8516    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8517    is a (possibly labeled) if statement which is not enclosed in
8518    braces and has an else clause.  This is used to implement
8519    -Wparentheses.
8520
8521    Returns the new statement.  */
8522
8523 static tree
8524 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8525 {
8526   tree statement;
8527
8528   if (if_p != NULL)
8529     *if_p = false;
8530
8531   /* Mark if () ; with a special NOP_EXPR.  */
8532   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8533     {
8534       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8535       cp_lexer_consume_token (parser->lexer);
8536       statement = add_stmt (build_empty_stmt (loc));
8537     }
8538   /* if a compound is opened, we simply parse the statement directly.  */
8539   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8540     statement = cp_parser_compound_statement (parser, NULL, false);
8541   /* If the token is not a `{', then we must take special action.  */
8542   else
8543     {
8544       /* Create a compound-statement.  */
8545       statement = begin_compound_stmt (0);
8546       /* Parse the dependent-statement.  */
8547       cp_parser_statement (parser, NULL_TREE, false, if_p);
8548       /* Finish the dummy compound-statement.  */
8549       finish_compound_stmt (statement);
8550     }
8551
8552   /* Return the statement.  */
8553   return statement;
8554 }
8555
8556 /* For some dependent statements (like `while (cond) statement'), we
8557    have already created a scope.  Therefore, even if the dependent
8558    statement is a compound-statement, we do not want to create another
8559    scope.  */
8560
8561 static void
8562 cp_parser_already_scoped_statement (cp_parser* parser)
8563 {
8564   /* If the token is a `{', then we must take special action.  */
8565   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8566     cp_parser_statement (parser, NULL_TREE, false, NULL);
8567   else
8568     {
8569       /* Avoid calling cp_parser_compound_statement, so that we
8570          don't create a new scope.  Do everything else by hand.  */
8571       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8572       /* If the next keyword is `__label__' we have a label declaration.  */
8573       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8574         cp_parser_label_declaration (parser);
8575       /* Parse an (optional) statement-seq.  */
8576       cp_parser_statement_seq_opt (parser, NULL_TREE);
8577       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8578     }
8579 }
8580
8581 /* Declarations [gram.dcl.dcl] */
8582
8583 /* Parse an optional declaration-sequence.
8584
8585    declaration-seq:
8586      declaration
8587      declaration-seq declaration  */
8588
8589 static void
8590 cp_parser_declaration_seq_opt (cp_parser* parser)
8591 {
8592   while (true)
8593     {
8594       cp_token *token;
8595
8596       token = cp_lexer_peek_token (parser->lexer);
8597
8598       if (token->type == CPP_CLOSE_BRACE
8599           || token->type == CPP_EOF
8600           || token->type == CPP_PRAGMA_EOL)
8601         break;
8602
8603       if (token->type == CPP_SEMICOLON)
8604         {
8605           /* A declaration consisting of a single semicolon is
8606              invalid.  Allow it unless we're being pedantic.  */
8607           cp_lexer_consume_token (parser->lexer);
8608           if (!in_system_header)
8609             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8610           continue;
8611         }
8612
8613       /* If we're entering or exiting a region that's implicitly
8614          extern "C", modify the lang context appropriately.  */
8615       if (!parser->implicit_extern_c && token->implicit_extern_c)
8616         {
8617           push_lang_context (lang_name_c);
8618           parser->implicit_extern_c = true;
8619         }
8620       else if (parser->implicit_extern_c && !token->implicit_extern_c)
8621         {
8622           pop_lang_context ();
8623           parser->implicit_extern_c = false;
8624         }
8625
8626       if (token->type == CPP_PRAGMA)
8627         {
8628           /* A top-level declaration can consist solely of a #pragma.
8629              A nested declaration cannot, so this is done here and not
8630              in cp_parser_declaration.  (A #pragma at block scope is
8631              handled in cp_parser_statement.)  */
8632           cp_parser_pragma (parser, pragma_external);
8633           continue;
8634         }
8635
8636       /* Parse the declaration itself.  */
8637       cp_parser_declaration (parser);
8638     }
8639 }
8640
8641 /* Parse a declaration.
8642
8643    declaration:
8644      block-declaration
8645      function-definition
8646      template-declaration
8647      explicit-instantiation
8648      explicit-specialization
8649      linkage-specification
8650      namespace-definition
8651
8652    GNU extension:
8653
8654    declaration:
8655       __extension__ declaration */
8656
8657 static void
8658 cp_parser_declaration (cp_parser* parser)
8659 {
8660   cp_token token1;
8661   cp_token token2;
8662   int saved_pedantic;
8663   void *p;
8664
8665   /* Check for the `__extension__' keyword.  */
8666   if (cp_parser_extension_opt (parser, &saved_pedantic))
8667     {
8668       /* Parse the qualified declaration.  */
8669       cp_parser_declaration (parser);
8670       /* Restore the PEDANTIC flag.  */
8671       pedantic = saved_pedantic;
8672
8673       return;
8674     }
8675
8676   /* Try to figure out what kind of declaration is present.  */
8677   token1 = *cp_lexer_peek_token (parser->lexer);
8678
8679   if (token1.type != CPP_EOF)
8680     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8681   else
8682     {
8683       token2.type = CPP_EOF;
8684       token2.keyword = RID_MAX;
8685     }
8686
8687   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8688   p = obstack_alloc (&declarator_obstack, 0);
8689
8690   /* If the next token is `extern' and the following token is a string
8691      literal, then we have a linkage specification.  */
8692   if (token1.keyword == RID_EXTERN
8693       && cp_parser_is_string_literal (&token2))
8694     cp_parser_linkage_specification (parser);
8695   /* If the next token is `template', then we have either a template
8696      declaration, an explicit instantiation, or an explicit
8697      specialization.  */
8698   else if (token1.keyword == RID_TEMPLATE)
8699     {
8700       /* `template <>' indicates a template specialization.  */
8701       if (token2.type == CPP_LESS
8702           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8703         cp_parser_explicit_specialization (parser);
8704       /* `template <' indicates a template declaration.  */
8705       else if (token2.type == CPP_LESS)
8706         cp_parser_template_declaration (parser, /*member_p=*/false);
8707       /* Anything else must be an explicit instantiation.  */
8708       else
8709         cp_parser_explicit_instantiation (parser);
8710     }
8711   /* If the next token is `export', then we have a template
8712      declaration.  */
8713   else if (token1.keyword == RID_EXPORT)
8714     cp_parser_template_declaration (parser, /*member_p=*/false);
8715   /* If the next token is `extern', 'static' or 'inline' and the one
8716      after that is `template', we have a GNU extended explicit
8717      instantiation directive.  */
8718   else if (cp_parser_allow_gnu_extensions_p (parser)
8719            && (token1.keyword == RID_EXTERN
8720                || token1.keyword == RID_STATIC
8721                || token1.keyword == RID_INLINE)
8722            && token2.keyword == RID_TEMPLATE)
8723     cp_parser_explicit_instantiation (parser);
8724   /* If the next token is `namespace', check for a named or unnamed
8725      namespace definition.  */
8726   else if (token1.keyword == RID_NAMESPACE
8727            && (/* A named namespace definition.  */
8728                (token2.type == CPP_NAME
8729                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8730                     != CPP_EQ))
8731                /* An unnamed namespace definition.  */
8732                || token2.type == CPP_OPEN_BRACE
8733                || token2.keyword == RID_ATTRIBUTE))
8734     cp_parser_namespace_definition (parser);
8735   /* An inline (associated) namespace definition.  */
8736   else if (token1.keyword == RID_INLINE
8737            && token2.keyword == RID_NAMESPACE)
8738     cp_parser_namespace_definition (parser);
8739   /* Objective-C++ declaration/definition.  */
8740   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8741     cp_parser_objc_declaration (parser);
8742   /* We must have either a block declaration or a function
8743      definition.  */
8744   else
8745     /* Try to parse a block-declaration, or a function-definition.  */
8746     cp_parser_block_declaration (parser, /*statement_p=*/false);
8747
8748   /* Free any declarators allocated.  */
8749   obstack_free (&declarator_obstack, p);
8750 }
8751
8752 /* Parse a block-declaration.
8753
8754    block-declaration:
8755      simple-declaration
8756      asm-definition
8757      namespace-alias-definition
8758      using-declaration
8759      using-directive
8760
8761    GNU Extension:
8762
8763    block-declaration:
8764      __extension__ block-declaration
8765
8766    C++0x Extension:
8767
8768    block-declaration:
8769      static_assert-declaration
8770
8771    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8772    part of a declaration-statement.  */
8773
8774 static void
8775 cp_parser_block_declaration (cp_parser *parser,
8776                              bool      statement_p)
8777 {
8778   cp_token *token1;
8779   int saved_pedantic;
8780
8781   /* Check for the `__extension__' keyword.  */
8782   if (cp_parser_extension_opt (parser, &saved_pedantic))
8783     {
8784       /* Parse the qualified declaration.  */
8785       cp_parser_block_declaration (parser, statement_p);
8786       /* Restore the PEDANTIC flag.  */
8787       pedantic = saved_pedantic;
8788
8789       return;
8790     }
8791
8792   /* Peek at the next token to figure out which kind of declaration is
8793      present.  */
8794   token1 = cp_lexer_peek_token (parser->lexer);
8795
8796   /* If the next keyword is `asm', we have an asm-definition.  */
8797   if (token1->keyword == RID_ASM)
8798     {
8799       if (statement_p)
8800         cp_parser_commit_to_tentative_parse (parser);
8801       cp_parser_asm_definition (parser);
8802     }
8803   /* If the next keyword is `namespace', we have a
8804      namespace-alias-definition.  */
8805   else if (token1->keyword == RID_NAMESPACE)
8806     cp_parser_namespace_alias_definition (parser);
8807   /* If the next keyword is `using', we have either a
8808      using-declaration or a using-directive.  */
8809   else if (token1->keyword == RID_USING)
8810     {
8811       cp_token *token2;
8812
8813       if (statement_p)
8814         cp_parser_commit_to_tentative_parse (parser);
8815       /* If the token after `using' is `namespace', then we have a
8816          using-directive.  */
8817       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8818       if (token2->keyword == RID_NAMESPACE)
8819         cp_parser_using_directive (parser);
8820       /* Otherwise, it's a using-declaration.  */
8821       else
8822         cp_parser_using_declaration (parser,
8823                                      /*access_declaration_p=*/false);
8824     }
8825   /* If the next keyword is `__label__' we have a misplaced label
8826      declaration.  */
8827   else if (token1->keyword == RID_LABEL)
8828     {
8829       cp_lexer_consume_token (parser->lexer);
8830       error_at (token1->location, "%<__label__%> not at the beginning of a block");
8831       cp_parser_skip_to_end_of_statement (parser);
8832       /* If the next token is now a `;', consume it.  */
8833       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8834         cp_lexer_consume_token (parser->lexer);
8835     }
8836   /* If the next token is `static_assert' we have a static assertion.  */
8837   else if (token1->keyword == RID_STATIC_ASSERT)
8838     cp_parser_static_assert (parser, /*member_p=*/false);
8839   /* Anything else must be a simple-declaration.  */
8840   else
8841     cp_parser_simple_declaration (parser, !statement_p);
8842 }
8843
8844 /* Parse a simple-declaration.
8845
8846    simple-declaration:
8847      decl-specifier-seq [opt] init-declarator-list [opt] ;
8848
8849    init-declarator-list:
8850      init-declarator
8851      init-declarator-list , init-declarator
8852
8853    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8854    function-definition as a simple-declaration.  */
8855
8856 static void
8857 cp_parser_simple_declaration (cp_parser* parser,
8858                               bool function_definition_allowed_p)
8859 {
8860   cp_decl_specifier_seq decl_specifiers;
8861   int declares_class_or_enum;
8862   bool saw_declarator;
8863
8864   /* Defer access checks until we know what is being declared; the
8865      checks for names appearing in the decl-specifier-seq should be
8866      done as if we were in the scope of the thing being declared.  */
8867   push_deferring_access_checks (dk_deferred);
8868
8869   /* Parse the decl-specifier-seq.  We have to keep track of whether
8870      or not the decl-specifier-seq declares a named class or
8871      enumeration type, since that is the only case in which the
8872      init-declarator-list is allowed to be empty.
8873
8874      [dcl.dcl]
8875
8876      In a simple-declaration, the optional init-declarator-list can be
8877      omitted only when declaring a class or enumeration, that is when
8878      the decl-specifier-seq contains either a class-specifier, an
8879      elaborated-type-specifier, or an enum-specifier.  */
8880   cp_parser_decl_specifier_seq (parser,
8881                                 CP_PARSER_FLAGS_OPTIONAL,
8882                                 &decl_specifiers,
8883                                 &declares_class_or_enum);
8884   /* We no longer need to defer access checks.  */
8885   stop_deferring_access_checks ();
8886
8887   /* In a block scope, a valid declaration must always have a
8888      decl-specifier-seq.  By not trying to parse declarators, we can
8889      resolve the declaration/expression ambiguity more quickly.  */
8890   if (!function_definition_allowed_p
8891       && !decl_specifiers.any_specifiers_p)
8892     {
8893       cp_parser_error (parser, "expected declaration");
8894       goto done;
8895     }
8896
8897   /* If the next two tokens are both identifiers, the code is
8898      erroneous. The usual cause of this situation is code like:
8899
8900        T t;
8901
8902      where "T" should name a type -- but does not.  */
8903   if (!decl_specifiers.any_type_specifiers_p
8904       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8905     {
8906       /* If parsing tentatively, we should commit; we really are
8907          looking at a declaration.  */
8908       cp_parser_commit_to_tentative_parse (parser);
8909       /* Give up.  */
8910       goto done;
8911     }
8912
8913   /* If we have seen at least one decl-specifier, and the next token
8914      is not a parenthesis, then we must be looking at a declaration.
8915      (After "int (" we might be looking at a functional cast.)  */
8916   if (decl_specifiers.any_specifiers_p
8917       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8918       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8919       && !cp_parser_error_occurred (parser))
8920     cp_parser_commit_to_tentative_parse (parser);
8921
8922   /* Keep going until we hit the `;' at the end of the simple
8923      declaration.  */
8924   saw_declarator = false;
8925   while (cp_lexer_next_token_is_not (parser->lexer,
8926                                      CPP_SEMICOLON))
8927     {
8928       cp_token *token;
8929       bool function_definition_p;
8930       tree decl;
8931
8932       if (saw_declarator)
8933         {
8934           /* If we are processing next declarator, coma is expected */
8935           token = cp_lexer_peek_token (parser->lexer);
8936           gcc_assert (token->type == CPP_COMMA);
8937           cp_lexer_consume_token (parser->lexer);
8938         }
8939       else
8940         saw_declarator = true;
8941
8942       /* Parse the init-declarator.  */
8943       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8944                                         /*checks=*/NULL,
8945                                         function_definition_allowed_p,
8946                                         /*member_p=*/false,
8947                                         declares_class_or_enum,
8948                                         &function_definition_p);
8949       /* If an error occurred while parsing tentatively, exit quickly.
8950          (That usually happens when in the body of a function; each
8951          statement is treated as a declaration-statement until proven
8952          otherwise.)  */
8953       if (cp_parser_error_occurred (parser))
8954         goto done;
8955       /* Handle function definitions specially.  */
8956       if (function_definition_p)
8957         {
8958           /* If the next token is a `,', then we are probably
8959              processing something like:
8960
8961                void f() {}, *p;
8962
8963              which is erroneous.  */
8964           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8965             {
8966               cp_token *token = cp_lexer_peek_token (parser->lexer);
8967               error_at (token->location,
8968                         "mixing"
8969                         " declarations and function-definitions is forbidden");
8970             }
8971           /* Otherwise, we're done with the list of declarators.  */
8972           else
8973             {
8974               pop_deferring_access_checks ();
8975               return;
8976             }
8977         }
8978       /* The next token should be either a `,' or a `;'.  */
8979       token = cp_lexer_peek_token (parser->lexer);
8980       /* If it's a `,', there are more declarators to come.  */
8981       if (token->type == CPP_COMMA)
8982         /* will be consumed next time around */;
8983       /* If it's a `;', we are done.  */
8984       else if (token->type == CPP_SEMICOLON)
8985         break;
8986       /* Anything else is an error.  */
8987       else
8988         {
8989           /* If we have already issued an error message we don't need
8990              to issue another one.  */
8991           if (decl != error_mark_node
8992               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8993             cp_parser_error (parser, "expected %<,%> or %<;%>");
8994           /* Skip tokens until we reach the end of the statement.  */
8995           cp_parser_skip_to_end_of_statement (parser);
8996           /* If the next token is now a `;', consume it.  */
8997           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8998             cp_lexer_consume_token (parser->lexer);
8999           goto done;
9000         }
9001       /* After the first time around, a function-definition is not
9002          allowed -- even if it was OK at first.  For example:
9003
9004            int i, f() {}
9005
9006          is not valid.  */
9007       function_definition_allowed_p = false;
9008     }
9009
9010   /* Issue an error message if no declarators are present, and the
9011      decl-specifier-seq does not itself declare a class or
9012      enumeration.  */
9013   if (!saw_declarator)
9014     {
9015       if (cp_parser_declares_only_class_p (parser))
9016         shadow_tag (&decl_specifiers);
9017       /* Perform any deferred access checks.  */
9018       perform_deferred_access_checks ();
9019     }
9020
9021   /* Consume the `;'.  */
9022   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9023
9024  done:
9025   pop_deferring_access_checks ();
9026 }
9027
9028 /* Parse a decl-specifier-seq.
9029
9030    decl-specifier-seq:
9031      decl-specifier-seq [opt] decl-specifier
9032
9033    decl-specifier:
9034      storage-class-specifier
9035      type-specifier
9036      function-specifier
9037      friend
9038      typedef
9039
9040    GNU Extension:
9041
9042    decl-specifier:
9043      attributes
9044
9045    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9046
9047    The parser flags FLAGS is used to control type-specifier parsing.
9048
9049    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9050    flags:
9051
9052      1: one of the decl-specifiers is an elaborated-type-specifier
9053         (i.e., a type declaration)
9054      2: one of the decl-specifiers is an enum-specifier or a
9055         class-specifier (i.e., a type definition)
9056
9057    */
9058
9059 static void
9060 cp_parser_decl_specifier_seq (cp_parser* parser,
9061                               cp_parser_flags flags,
9062                               cp_decl_specifier_seq *decl_specs,
9063                               int* declares_class_or_enum)
9064 {
9065   bool constructor_possible_p = !parser->in_declarator_p;
9066   cp_token *start_token = NULL;
9067
9068   /* Clear DECL_SPECS.  */
9069   clear_decl_specs (decl_specs);
9070
9071   /* Assume no class or enumeration type is declared.  */
9072   *declares_class_or_enum = 0;
9073
9074   /* Keep reading specifiers until there are no more to read.  */
9075   while (true)
9076     {
9077       bool constructor_p;
9078       bool found_decl_spec;
9079       cp_token *token;
9080
9081       /* Peek at the next token.  */
9082       token = cp_lexer_peek_token (parser->lexer);
9083
9084       /* Save the first token of the decl spec list for error
9085          reporting.  */
9086       if (!start_token)
9087         start_token = token;
9088       /* Handle attributes.  */
9089       if (token->keyword == RID_ATTRIBUTE)
9090         {
9091           /* Parse the attributes.  */
9092           decl_specs->attributes
9093             = chainon (decl_specs->attributes,
9094                        cp_parser_attributes_opt (parser));
9095           continue;
9096         }
9097       /* Assume we will find a decl-specifier keyword.  */
9098       found_decl_spec = true;
9099       /* If the next token is an appropriate keyword, we can simply
9100          add it to the list.  */
9101       switch (token->keyword)
9102         {
9103           /* decl-specifier:
9104                friend
9105                constexpr */
9106         case RID_FRIEND:
9107           if (!at_class_scope_p ())
9108             {
9109               error_at (token->location, "%<friend%> used outside of class");
9110               cp_lexer_purge_token (parser->lexer);
9111             }
9112           else
9113             {
9114               ++decl_specs->specs[(int) ds_friend];
9115               /* Consume the token.  */
9116               cp_lexer_consume_token (parser->lexer);
9117             }
9118           break;
9119
9120         case RID_CONSTEXPR:
9121           ++decl_specs->specs[(int) ds_constexpr];
9122           cp_lexer_consume_token (parser->lexer);
9123           break;
9124
9125           /* function-specifier:
9126                inline
9127                virtual
9128                explicit  */
9129         case RID_INLINE:
9130         case RID_VIRTUAL:
9131         case RID_EXPLICIT:
9132           cp_parser_function_specifier_opt (parser, decl_specs);
9133           break;
9134
9135           /* decl-specifier:
9136                typedef  */
9137         case RID_TYPEDEF:
9138           ++decl_specs->specs[(int) ds_typedef];
9139           /* Consume the token.  */
9140           cp_lexer_consume_token (parser->lexer);
9141           /* A constructor declarator cannot appear in a typedef.  */
9142           constructor_possible_p = false;
9143           /* The "typedef" keyword can only occur in a declaration; we
9144              may as well commit at this point.  */
9145           cp_parser_commit_to_tentative_parse (parser);
9146
9147           if (decl_specs->storage_class != sc_none)
9148             decl_specs->conflicting_specifiers_p = true;
9149           break;
9150
9151           /* storage-class-specifier:
9152                auto
9153                register
9154                static
9155                extern
9156                mutable
9157
9158              GNU Extension:
9159                thread  */
9160         case RID_AUTO:
9161           if (cxx_dialect == cxx98) 
9162             {
9163               /* Consume the token.  */
9164               cp_lexer_consume_token (parser->lexer);
9165
9166               /* Complain about `auto' as a storage specifier, if
9167                  we're complaining about C++0x compatibility.  */
9168               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9169                           " will change meaning in C++0x; please remove it");
9170
9171               /* Set the storage class anyway.  */
9172               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9173                                            token->location);
9174             }
9175           else
9176             /* C++0x auto type-specifier.  */
9177             found_decl_spec = false;
9178           break;
9179
9180         case RID_REGISTER:
9181         case RID_STATIC:
9182         case RID_EXTERN:
9183         case RID_MUTABLE:
9184           /* Consume the token.  */
9185           cp_lexer_consume_token (parser->lexer);
9186           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9187                                        token->location);
9188           break;
9189         case RID_THREAD:
9190           /* Consume the token.  */
9191           cp_lexer_consume_token (parser->lexer);
9192           ++decl_specs->specs[(int) ds_thread];
9193           break;
9194
9195         default:
9196           /* We did not yet find a decl-specifier yet.  */
9197           found_decl_spec = false;
9198           break;
9199         }
9200
9201       /* Constructors are a special case.  The `S' in `S()' is not a
9202          decl-specifier; it is the beginning of the declarator.  */
9203       constructor_p
9204         = (!found_decl_spec
9205            && constructor_possible_p
9206            && (cp_parser_constructor_declarator_p
9207                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9208
9209       /* If we don't have a DECL_SPEC yet, then we must be looking at
9210          a type-specifier.  */
9211       if (!found_decl_spec && !constructor_p)
9212         {
9213           int decl_spec_declares_class_or_enum;
9214           bool is_cv_qualifier;
9215           tree type_spec;
9216
9217           type_spec
9218             = cp_parser_type_specifier (parser, flags,
9219                                         decl_specs,
9220                                         /*is_declaration=*/true,
9221                                         &decl_spec_declares_class_or_enum,
9222                                         &is_cv_qualifier);
9223           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9224
9225           /* If this type-specifier referenced a user-defined type
9226              (a typedef, class-name, etc.), then we can't allow any
9227              more such type-specifiers henceforth.
9228
9229              [dcl.spec]
9230
9231              The longest sequence of decl-specifiers that could
9232              possibly be a type name is taken as the
9233              decl-specifier-seq of a declaration.  The sequence shall
9234              be self-consistent as described below.
9235
9236              [dcl.type]
9237
9238              As a general rule, at most one type-specifier is allowed
9239              in the complete decl-specifier-seq of a declaration.  The
9240              only exceptions are the following:
9241
9242              -- const or volatile can be combined with any other
9243                 type-specifier.
9244
9245              -- signed or unsigned can be combined with char, long,
9246                 short, or int.
9247
9248              -- ..
9249
9250              Example:
9251
9252                typedef char* Pc;
9253                void g (const int Pc);
9254
9255              Here, Pc is *not* part of the decl-specifier seq; it's
9256              the declarator.  Therefore, once we see a type-specifier
9257              (other than a cv-qualifier), we forbid any additional
9258              user-defined types.  We *do* still allow things like `int
9259              int' to be considered a decl-specifier-seq, and issue the
9260              error message later.  */
9261           if (type_spec && !is_cv_qualifier)
9262             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9263           /* A constructor declarator cannot follow a type-specifier.  */
9264           if (type_spec)
9265             {
9266               constructor_possible_p = false;
9267               found_decl_spec = true;
9268               if (!is_cv_qualifier)
9269                 decl_specs->any_type_specifiers_p = true;
9270             }
9271         }
9272
9273       /* If we still do not have a DECL_SPEC, then there are no more
9274          decl-specifiers.  */
9275       if (!found_decl_spec)
9276         break;
9277
9278       decl_specs->any_specifiers_p = true;
9279       /* After we see one decl-specifier, further decl-specifiers are
9280          always optional.  */
9281       flags |= CP_PARSER_FLAGS_OPTIONAL;
9282     }
9283
9284   cp_parser_check_decl_spec (decl_specs, start_token->location);
9285
9286   /* Don't allow a friend specifier with a class definition.  */
9287   if (decl_specs->specs[(int) ds_friend] != 0
9288       && (*declares_class_or_enum & 2))
9289     error_at (start_token->location,
9290               "class definition may not be declared a friend");
9291 }
9292
9293 /* Parse an (optional) storage-class-specifier.
9294
9295    storage-class-specifier:
9296      auto
9297      register
9298      static
9299      extern
9300      mutable
9301
9302    GNU Extension:
9303
9304    storage-class-specifier:
9305      thread
9306
9307    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9308
9309 static tree
9310 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9311 {
9312   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9313     {
9314     case RID_AUTO:
9315       if (cxx_dialect != cxx98)
9316         return NULL_TREE;
9317       /* Fall through for C++98.  */
9318
9319     case RID_REGISTER:
9320     case RID_STATIC:
9321     case RID_EXTERN:
9322     case RID_MUTABLE:
9323     case RID_THREAD:
9324       /* Consume the token.  */
9325       return cp_lexer_consume_token (parser->lexer)->u.value;
9326
9327     default:
9328       return NULL_TREE;
9329     }
9330 }
9331
9332 /* Parse an (optional) function-specifier.
9333
9334    function-specifier:
9335      inline
9336      virtual
9337      explicit
9338
9339    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9340    Updates DECL_SPECS, if it is non-NULL.  */
9341
9342 static tree
9343 cp_parser_function_specifier_opt (cp_parser* parser,
9344                                   cp_decl_specifier_seq *decl_specs)
9345 {
9346   cp_token *token = cp_lexer_peek_token (parser->lexer);
9347   switch (token->keyword)
9348     {
9349     case RID_INLINE:
9350       if (decl_specs)
9351         ++decl_specs->specs[(int) ds_inline];
9352       break;
9353
9354     case RID_VIRTUAL:
9355       /* 14.5.2.3 [temp.mem]
9356
9357          A member function template shall not be virtual.  */
9358       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9359         error_at (token->location, "templates may not be %<virtual%>");
9360       else if (decl_specs)
9361         ++decl_specs->specs[(int) ds_virtual];
9362       break;
9363
9364     case RID_EXPLICIT:
9365       if (decl_specs)
9366         ++decl_specs->specs[(int) ds_explicit];
9367       break;
9368
9369     default:
9370       return NULL_TREE;
9371     }
9372
9373   /* Consume the token.  */
9374   return cp_lexer_consume_token (parser->lexer)->u.value;
9375 }
9376
9377 /* Parse a linkage-specification.
9378
9379    linkage-specification:
9380      extern string-literal { declaration-seq [opt] }
9381      extern string-literal declaration  */
9382
9383 static void
9384 cp_parser_linkage_specification (cp_parser* parser)
9385 {
9386   tree linkage;
9387
9388   /* Look for the `extern' keyword.  */
9389   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9390
9391   /* Look for the string-literal.  */
9392   linkage = cp_parser_string_literal (parser, false, false);
9393
9394   /* Transform the literal into an identifier.  If the literal is a
9395      wide-character string, or contains embedded NULs, then we can't
9396      handle it as the user wants.  */
9397   if (strlen (TREE_STRING_POINTER (linkage))
9398       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9399     {
9400       cp_parser_error (parser, "invalid linkage-specification");
9401       /* Assume C++ linkage.  */
9402       linkage = lang_name_cplusplus;
9403     }
9404   else
9405     linkage = get_identifier (TREE_STRING_POINTER (linkage));
9406
9407   /* We're now using the new linkage.  */
9408   push_lang_context (linkage);
9409
9410   /* If the next token is a `{', then we're using the first
9411      production.  */
9412   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9413     {
9414       /* Consume the `{' token.  */
9415       cp_lexer_consume_token (parser->lexer);
9416       /* Parse the declarations.  */
9417       cp_parser_declaration_seq_opt (parser);
9418       /* Look for the closing `}'.  */
9419       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9420     }
9421   /* Otherwise, there's just one declaration.  */
9422   else
9423     {
9424       bool saved_in_unbraced_linkage_specification_p;
9425
9426       saved_in_unbraced_linkage_specification_p
9427         = parser->in_unbraced_linkage_specification_p;
9428       parser->in_unbraced_linkage_specification_p = true;
9429       cp_parser_declaration (parser);
9430       parser->in_unbraced_linkage_specification_p
9431         = saved_in_unbraced_linkage_specification_p;
9432     }
9433
9434   /* We're done with the linkage-specification.  */
9435   pop_lang_context ();
9436 }
9437
9438 /* Parse a static_assert-declaration.
9439
9440    static_assert-declaration:
9441      static_assert ( constant-expression , string-literal ) ; 
9442
9443    If MEMBER_P, this static_assert is a class member.  */
9444
9445 static void 
9446 cp_parser_static_assert(cp_parser *parser, bool member_p)
9447 {
9448   tree condition;
9449   tree message;
9450   cp_token *token;
9451   location_t saved_loc;
9452
9453   /* Peek at the `static_assert' token so we can keep track of exactly
9454      where the static assertion started.  */
9455   token = cp_lexer_peek_token (parser->lexer);
9456   saved_loc = token->location;
9457
9458   /* Look for the `static_assert' keyword.  */
9459   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
9460                                   "%<static_assert%>"))
9461     return;
9462
9463   /*  We know we are in a static assertion; commit to any tentative
9464       parse.  */
9465   if (cp_parser_parsing_tentatively (parser))
9466     cp_parser_commit_to_tentative_parse (parser);
9467
9468   /* Parse the `(' starting the static assertion condition.  */
9469   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9470
9471   /* Parse the constant-expression.  */
9472   condition = 
9473     cp_parser_constant_expression (parser,
9474                                    /*allow_non_constant_p=*/false,
9475                                    /*non_constant_p=*/NULL);
9476
9477   /* Parse the separating `,'.  */
9478   cp_parser_require (parser, CPP_COMMA, "%<,%>");
9479
9480   /* Parse the string-literal message.  */
9481   message = cp_parser_string_literal (parser, 
9482                                       /*translate=*/false,
9483                                       /*wide_ok=*/true);
9484
9485   /* A `)' completes the static assertion.  */
9486   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9487     cp_parser_skip_to_closing_parenthesis (parser, 
9488                                            /*recovering=*/true, 
9489                                            /*or_comma=*/false,
9490                                            /*consume_paren=*/true);
9491
9492   /* A semicolon terminates the declaration.  */
9493   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9494
9495   /* Complete the static assertion, which may mean either processing 
9496      the static assert now or saving it for template instantiation.  */
9497   finish_static_assert (condition, message, saved_loc, member_p);
9498 }
9499
9500 /* Parse a `decltype' type. Returns the type. 
9501
9502    simple-type-specifier:
9503      decltype ( expression )  */
9504
9505 static tree
9506 cp_parser_decltype (cp_parser *parser)
9507 {
9508   tree expr;
9509   bool id_expression_or_member_access_p = false;
9510   const char *saved_message;
9511   bool saved_integral_constant_expression_p;
9512   bool saved_non_integral_constant_expression_p;
9513   cp_token *id_expr_start_token;
9514
9515   /* Look for the `decltype' token.  */
9516   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9517     return error_mark_node;
9518
9519   /* Types cannot be defined in a `decltype' expression.  Save away the
9520      old message.  */
9521   saved_message = parser->type_definition_forbidden_message;
9522
9523   /* And create the new one.  */
9524   parser->type_definition_forbidden_message
9525     = G_("types may not be defined in %<decltype%> expressions");
9526
9527   /* The restrictions on constant-expressions do not apply inside
9528      decltype expressions.  */
9529   saved_integral_constant_expression_p
9530     = parser->integral_constant_expression_p;
9531   saved_non_integral_constant_expression_p
9532     = parser->non_integral_constant_expression_p;
9533   parser->integral_constant_expression_p = false;
9534
9535   /* Do not actually evaluate the expression.  */
9536   ++cp_unevaluated_operand;
9537
9538   /* Do not warn about problems with the expression.  */
9539   ++c_inhibit_evaluation_warnings;
9540
9541   /* Parse the opening `('.  */
9542   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9543     return error_mark_node;
9544   
9545   /* First, try parsing an id-expression.  */
9546   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9547   cp_parser_parse_tentatively (parser);
9548   expr = cp_parser_id_expression (parser,
9549                                   /*template_keyword_p=*/false,
9550                                   /*check_dependency_p=*/true,
9551                                   /*template_p=*/NULL,
9552                                   /*declarator_p=*/false,
9553                                   /*optional_p=*/false);
9554
9555   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9556     {
9557       bool non_integral_constant_expression_p = false;
9558       tree id_expression = expr;
9559       cp_id_kind idk;
9560       const char *error_msg;
9561
9562       if (TREE_CODE (expr) == IDENTIFIER_NODE)
9563         /* Lookup the name we got back from the id-expression.  */
9564         expr = cp_parser_lookup_name (parser, expr,
9565                                       none_type,
9566                                       /*is_template=*/false,
9567                                       /*is_namespace=*/false,
9568                                       /*check_dependency=*/true,
9569                                       /*ambiguous_decls=*/NULL,
9570                                       id_expr_start_token->location);
9571
9572       if (expr
9573           && expr != error_mark_node
9574           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9575           && TREE_CODE (expr) != TYPE_DECL
9576           && (TREE_CODE (expr) != BIT_NOT_EXPR
9577               || !TYPE_P (TREE_OPERAND (expr, 0)))
9578           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9579         {
9580           /* Complete lookup of the id-expression.  */
9581           expr = (finish_id_expression
9582                   (id_expression, expr, parser->scope, &idk,
9583                    /*integral_constant_expression_p=*/false,
9584                    /*allow_non_integral_constant_expression_p=*/true,
9585                    &non_integral_constant_expression_p,
9586                    /*template_p=*/false,
9587                    /*done=*/true,
9588                    /*address_p=*/false,
9589                    /*template_arg_p=*/false,
9590                    &error_msg,
9591                    id_expr_start_token->location));
9592
9593           if (expr == error_mark_node)
9594             /* We found an id-expression, but it was something that we
9595                should not have found. This is an error, not something
9596                we can recover from, so note that we found an
9597                id-expression and we'll recover as gracefully as
9598                possible.  */
9599             id_expression_or_member_access_p = true;
9600         }
9601
9602       if (expr 
9603           && expr != error_mark_node
9604           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9605         /* We have an id-expression.  */
9606         id_expression_or_member_access_p = true;
9607     }
9608
9609   if (!id_expression_or_member_access_p)
9610     {
9611       /* Abort the id-expression parse.  */
9612       cp_parser_abort_tentative_parse (parser);
9613
9614       /* Parsing tentatively, again.  */
9615       cp_parser_parse_tentatively (parser);
9616
9617       /* Parse a class member access.  */
9618       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9619                                            /*cast_p=*/false,
9620                                            /*member_access_only_p=*/true, NULL);
9621
9622       if (expr 
9623           && expr != error_mark_node
9624           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9625         /* We have an id-expression.  */
9626         id_expression_or_member_access_p = true;
9627     }
9628
9629   if (id_expression_or_member_access_p)
9630     /* We have parsed the complete id-expression or member access.  */
9631     cp_parser_parse_definitely (parser);
9632   else
9633     {
9634       bool saved_greater_than_is_operator_p;
9635
9636       /* Abort our attempt to parse an id-expression or member access
9637          expression.  */
9638       cp_parser_abort_tentative_parse (parser);
9639
9640       /* Within a parenthesized expression, a `>' token is always
9641          the greater-than operator.  */
9642       saved_greater_than_is_operator_p
9643         = parser->greater_than_is_operator_p;
9644       parser->greater_than_is_operator_p = true;
9645
9646       /* Parse a full expression.  */
9647       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9648
9649       /* The `>' token might be the end of a template-id or
9650          template-parameter-list now.  */
9651       parser->greater_than_is_operator_p
9652         = saved_greater_than_is_operator_p;
9653     }
9654
9655   /* Go back to evaluating expressions.  */
9656   --cp_unevaluated_operand;
9657   --c_inhibit_evaluation_warnings;
9658
9659   /* Restore the old message and the integral constant expression
9660      flags.  */
9661   parser->type_definition_forbidden_message = saved_message;
9662   parser->integral_constant_expression_p
9663     = saved_integral_constant_expression_p;
9664   parser->non_integral_constant_expression_p
9665     = saved_non_integral_constant_expression_p;
9666
9667   if (expr == error_mark_node)
9668     {
9669       /* Skip everything up to the closing `)'.  */
9670       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9671                                              /*consume_paren=*/true);
9672       return error_mark_node;
9673     }
9674   
9675   /* Parse to the closing `)'.  */
9676   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9677     {
9678       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9679                                              /*consume_paren=*/true);
9680       return error_mark_node;
9681     }
9682
9683   return finish_decltype_type (expr, id_expression_or_member_access_p);
9684 }
9685
9686 /* Special member functions [gram.special] */
9687
9688 /* Parse a conversion-function-id.
9689
9690    conversion-function-id:
9691      operator conversion-type-id
9692
9693    Returns an IDENTIFIER_NODE representing the operator.  */
9694
9695 static tree
9696 cp_parser_conversion_function_id (cp_parser* parser)
9697 {
9698   tree type;
9699   tree saved_scope;
9700   tree saved_qualifying_scope;
9701   tree saved_object_scope;
9702   tree pushed_scope = NULL_TREE;
9703
9704   /* Look for the `operator' token.  */
9705   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9706     return error_mark_node;
9707   /* When we parse the conversion-type-id, the current scope will be
9708      reset.  However, we need that information in able to look up the
9709      conversion function later, so we save it here.  */
9710   saved_scope = parser->scope;
9711   saved_qualifying_scope = parser->qualifying_scope;
9712   saved_object_scope = parser->object_scope;
9713   /* We must enter the scope of the class so that the names of
9714      entities declared within the class are available in the
9715      conversion-type-id.  For example, consider:
9716
9717        struct S {
9718          typedef int I;
9719          operator I();
9720        };
9721
9722        S::operator I() { ... }
9723
9724      In order to see that `I' is a type-name in the definition, we
9725      must be in the scope of `S'.  */
9726   if (saved_scope)
9727     pushed_scope = push_scope (saved_scope);
9728   /* Parse the conversion-type-id.  */
9729   type = cp_parser_conversion_type_id (parser);
9730   /* Leave the scope of the class, if any.  */
9731   if (pushed_scope)
9732     pop_scope (pushed_scope);
9733   /* Restore the saved scope.  */
9734   parser->scope = saved_scope;
9735   parser->qualifying_scope = saved_qualifying_scope;
9736   parser->object_scope = saved_object_scope;
9737   /* If the TYPE is invalid, indicate failure.  */
9738   if (type == error_mark_node)
9739     return error_mark_node;
9740   return mangle_conv_op_name_for_type (type);
9741 }
9742
9743 /* Parse a conversion-type-id:
9744
9745    conversion-type-id:
9746      type-specifier-seq conversion-declarator [opt]
9747
9748    Returns the TYPE specified.  */
9749
9750 static tree
9751 cp_parser_conversion_type_id (cp_parser* parser)
9752 {
9753   tree attributes;
9754   cp_decl_specifier_seq type_specifiers;
9755   cp_declarator *declarator;
9756   tree type_specified;
9757
9758   /* Parse the attributes.  */
9759   attributes = cp_parser_attributes_opt (parser);
9760   /* Parse the type-specifiers.  */
9761   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9762                                 /*is_trailing_return=*/false,
9763                                 &type_specifiers);
9764   /* If that didn't work, stop.  */
9765   if (type_specifiers.type == error_mark_node)
9766     return error_mark_node;
9767   /* Parse the conversion-declarator.  */
9768   declarator = cp_parser_conversion_declarator_opt (parser);
9769
9770   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9771                                     /*initialized=*/0, &attributes);
9772   if (attributes)
9773     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9774
9775   /* Don't give this error when parsing tentatively.  This happens to
9776      work because we always parse this definitively once.  */
9777   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9778       && type_uses_auto (type_specified))
9779     {
9780       error ("invalid use of %<auto%> in conversion operator");
9781       return error_mark_node;
9782     }
9783
9784   return type_specified;
9785 }
9786
9787 /* Parse an (optional) conversion-declarator.
9788
9789    conversion-declarator:
9790      ptr-operator conversion-declarator [opt]
9791
9792    */
9793
9794 static cp_declarator *
9795 cp_parser_conversion_declarator_opt (cp_parser* parser)
9796 {
9797   enum tree_code code;
9798   tree class_type;
9799   cp_cv_quals cv_quals;
9800
9801   /* We don't know if there's a ptr-operator next, or not.  */
9802   cp_parser_parse_tentatively (parser);
9803   /* Try the ptr-operator.  */
9804   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9805   /* If it worked, look for more conversion-declarators.  */
9806   if (cp_parser_parse_definitely (parser))
9807     {
9808       cp_declarator *declarator;
9809
9810       /* Parse another optional declarator.  */
9811       declarator = cp_parser_conversion_declarator_opt (parser);
9812
9813       return cp_parser_make_indirect_declarator
9814         (code, class_type, cv_quals, declarator);
9815    }
9816
9817   return NULL;
9818 }
9819
9820 /* Parse an (optional) ctor-initializer.
9821
9822    ctor-initializer:
9823      : mem-initializer-list
9824
9825    Returns TRUE iff the ctor-initializer was actually present.  */
9826
9827 static bool
9828 cp_parser_ctor_initializer_opt (cp_parser* parser)
9829 {
9830   /* If the next token is not a `:', then there is no
9831      ctor-initializer.  */
9832   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9833     {
9834       /* Do default initialization of any bases and members.  */
9835       if (DECL_CONSTRUCTOR_P (current_function_decl))
9836         finish_mem_initializers (NULL_TREE);
9837
9838       return false;
9839     }
9840
9841   /* Consume the `:' token.  */
9842   cp_lexer_consume_token (parser->lexer);
9843   /* And the mem-initializer-list.  */
9844   cp_parser_mem_initializer_list (parser);
9845
9846   return true;
9847 }
9848
9849 /* Parse a mem-initializer-list.
9850
9851    mem-initializer-list:
9852      mem-initializer ... [opt]
9853      mem-initializer ... [opt] , mem-initializer-list  */
9854
9855 static void
9856 cp_parser_mem_initializer_list (cp_parser* parser)
9857 {
9858   tree mem_initializer_list = NULL_TREE;
9859   cp_token *token = cp_lexer_peek_token (parser->lexer);
9860
9861   /* Let the semantic analysis code know that we are starting the
9862      mem-initializer-list.  */
9863   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9864     error_at (token->location,
9865               "only constructors take base initializers");
9866
9867   /* Loop through the list.  */
9868   while (true)
9869     {
9870       tree mem_initializer;
9871
9872       token = cp_lexer_peek_token (parser->lexer);
9873       /* Parse the mem-initializer.  */
9874       mem_initializer = cp_parser_mem_initializer (parser);
9875       /* If the next token is a `...', we're expanding member initializers. */
9876       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9877         {
9878           /* Consume the `...'. */
9879           cp_lexer_consume_token (parser->lexer);
9880
9881           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9882              can be expanded but members cannot. */
9883           if (mem_initializer != error_mark_node
9884               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9885             {
9886               error_at (token->location,
9887                         "cannot expand initializer for member %<%D%>",
9888                         TREE_PURPOSE (mem_initializer));
9889               mem_initializer = error_mark_node;
9890             }
9891
9892           /* Construct the pack expansion type. */
9893           if (mem_initializer != error_mark_node)
9894             mem_initializer = make_pack_expansion (mem_initializer);
9895         }
9896       /* Add it to the list, unless it was erroneous.  */
9897       if (mem_initializer != error_mark_node)
9898         {
9899           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9900           mem_initializer_list = mem_initializer;
9901         }
9902       /* If the next token is not a `,', we're done.  */
9903       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9904         break;
9905       /* Consume the `,' token.  */
9906       cp_lexer_consume_token (parser->lexer);
9907     }
9908
9909   /* Perform semantic analysis.  */
9910   if (DECL_CONSTRUCTOR_P (current_function_decl))
9911     finish_mem_initializers (mem_initializer_list);
9912 }
9913
9914 /* Parse a mem-initializer.
9915
9916    mem-initializer:
9917      mem-initializer-id ( expression-list [opt] )
9918      mem-initializer-id braced-init-list
9919
9920    GNU extension:
9921
9922    mem-initializer:
9923      ( expression-list [opt] )
9924
9925    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9926    class) or FIELD_DECL (for a non-static data member) to initialize;
9927    the TREE_VALUE is the expression-list.  An empty initialization
9928    list is represented by void_list_node.  */
9929
9930 static tree
9931 cp_parser_mem_initializer (cp_parser* parser)
9932 {
9933   tree mem_initializer_id;
9934   tree expression_list;
9935   tree member;
9936   cp_token *token = cp_lexer_peek_token (parser->lexer);
9937
9938   /* Find out what is being initialized.  */
9939   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9940     {
9941       permerror (token->location,
9942                  "anachronistic old-style base class initializer");
9943       mem_initializer_id = NULL_TREE;
9944     }
9945   else
9946     {
9947       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9948       if (mem_initializer_id == error_mark_node)
9949         return mem_initializer_id;
9950     }
9951   member = expand_member_init (mem_initializer_id);
9952   if (member && !DECL_P (member))
9953     in_base_initializer = 1;
9954
9955   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9956     {
9957       bool expr_non_constant_p;
9958       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9959       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9960       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9961       expression_list = build_tree_list (NULL_TREE, expression_list);
9962     }
9963   else
9964     {
9965       VEC(tree,gc)* vec;
9966       vec = cp_parser_parenthesized_expression_list (parser, false,
9967                                                      /*cast_p=*/false,
9968                                                      /*allow_expansion_p=*/true,
9969                                                      /*non_constant_p=*/NULL);
9970       if (vec == NULL)
9971         return error_mark_node;
9972       expression_list = build_tree_list_vec (vec);
9973       release_tree_vector (vec);
9974     }
9975
9976   if (expression_list == error_mark_node)
9977     return error_mark_node;
9978   if (!expression_list)
9979     expression_list = void_type_node;
9980
9981   in_base_initializer = 0;
9982
9983   return member ? build_tree_list (member, expression_list) : error_mark_node;
9984 }
9985
9986 /* Parse a mem-initializer-id.
9987
9988    mem-initializer-id:
9989      :: [opt] nested-name-specifier [opt] class-name
9990      identifier
9991
9992    Returns a TYPE indicating the class to be initializer for the first
9993    production.  Returns an IDENTIFIER_NODE indicating the data member
9994    to be initialized for the second production.  */
9995
9996 static tree
9997 cp_parser_mem_initializer_id (cp_parser* parser)
9998 {
9999   bool global_scope_p;
10000   bool nested_name_specifier_p;
10001   bool template_p = false;
10002   tree id;
10003
10004   cp_token *token = cp_lexer_peek_token (parser->lexer);
10005
10006   /* `typename' is not allowed in this context ([temp.res]).  */
10007   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10008     {
10009       error_at (token->location, 
10010                 "keyword %<typename%> not allowed in this context (a qualified "
10011                 "member initializer is implicitly a type)");
10012       cp_lexer_consume_token (parser->lexer);
10013     }
10014   /* Look for the optional `::' operator.  */
10015   global_scope_p
10016     = (cp_parser_global_scope_opt (parser,
10017                                    /*current_scope_valid_p=*/false)
10018        != NULL_TREE);
10019   /* Look for the optional nested-name-specifier.  The simplest way to
10020      implement:
10021
10022        [temp.res]
10023
10024        The keyword `typename' is not permitted in a base-specifier or
10025        mem-initializer; in these contexts a qualified name that
10026        depends on a template-parameter is implicitly assumed to be a
10027        type name.
10028
10029      is to assume that we have seen the `typename' keyword at this
10030      point.  */
10031   nested_name_specifier_p
10032     = (cp_parser_nested_name_specifier_opt (parser,
10033                                             /*typename_keyword_p=*/true,
10034                                             /*check_dependency_p=*/true,
10035                                             /*type_p=*/true,
10036                                             /*is_declaration=*/true)
10037        != NULL_TREE);
10038   if (nested_name_specifier_p)
10039     template_p = cp_parser_optional_template_keyword (parser);
10040   /* If there is a `::' operator or a nested-name-specifier, then we
10041      are definitely looking for a class-name.  */
10042   if (global_scope_p || nested_name_specifier_p)
10043     return cp_parser_class_name (parser,
10044                                  /*typename_keyword_p=*/true,
10045                                  /*template_keyword_p=*/template_p,
10046                                  typename_type,
10047                                  /*check_dependency_p=*/true,
10048                                  /*class_head_p=*/false,
10049                                  /*is_declaration=*/true);
10050   /* Otherwise, we could also be looking for an ordinary identifier.  */
10051   cp_parser_parse_tentatively (parser);
10052   /* Try a class-name.  */
10053   id = cp_parser_class_name (parser,
10054                              /*typename_keyword_p=*/true,
10055                              /*template_keyword_p=*/false,
10056                              none_type,
10057                              /*check_dependency_p=*/true,
10058                              /*class_head_p=*/false,
10059                              /*is_declaration=*/true);
10060   /* If we found one, we're done.  */
10061   if (cp_parser_parse_definitely (parser))
10062     return id;
10063   /* Otherwise, look for an ordinary identifier.  */
10064   return cp_parser_identifier (parser);
10065 }
10066
10067 /* Overloading [gram.over] */
10068
10069 /* Parse an operator-function-id.
10070
10071    operator-function-id:
10072      operator operator
10073
10074    Returns an IDENTIFIER_NODE for the operator which is a
10075    human-readable spelling of the identifier, e.g., `operator +'.  */
10076
10077 static tree
10078 cp_parser_operator_function_id (cp_parser* parser)
10079 {
10080   /* Look for the `operator' keyword.  */
10081   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10082     return error_mark_node;
10083   /* And then the name of the operator itself.  */
10084   return cp_parser_operator (parser);
10085 }
10086
10087 /* Parse an operator.
10088
10089    operator:
10090      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10091      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10092      || ++ -- , ->* -> () []
10093
10094    GNU Extensions:
10095
10096    operator:
10097      <? >? <?= >?=
10098
10099    Returns an IDENTIFIER_NODE for the operator which is a
10100    human-readable spelling of the identifier, e.g., `operator +'.  */
10101
10102 static tree
10103 cp_parser_operator (cp_parser* parser)
10104 {
10105   tree id = NULL_TREE;
10106   cp_token *token;
10107
10108   /* Peek at the next token.  */
10109   token = cp_lexer_peek_token (parser->lexer);
10110   /* Figure out which operator we have.  */
10111   switch (token->type)
10112     {
10113     case CPP_KEYWORD:
10114       {
10115         enum tree_code op;
10116
10117         /* The keyword should be either `new' or `delete'.  */
10118         if (token->keyword == RID_NEW)
10119           op = NEW_EXPR;
10120         else if (token->keyword == RID_DELETE)
10121           op = DELETE_EXPR;
10122         else
10123           break;
10124
10125         /* Consume the `new' or `delete' token.  */
10126         cp_lexer_consume_token (parser->lexer);
10127
10128         /* Peek at the next token.  */
10129         token = cp_lexer_peek_token (parser->lexer);
10130         /* If it's a `[' token then this is the array variant of the
10131            operator.  */
10132         if (token->type == CPP_OPEN_SQUARE)
10133           {
10134             /* Consume the `[' token.  */
10135             cp_lexer_consume_token (parser->lexer);
10136             /* Look for the `]' token.  */
10137             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10138             id = ansi_opname (op == NEW_EXPR
10139                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10140           }
10141         /* Otherwise, we have the non-array variant.  */
10142         else
10143           id = ansi_opname (op);
10144
10145         return id;
10146       }
10147
10148     case CPP_PLUS:
10149       id = ansi_opname (PLUS_EXPR);
10150       break;
10151
10152     case CPP_MINUS:
10153       id = ansi_opname (MINUS_EXPR);
10154       break;
10155
10156     case CPP_MULT:
10157       id = ansi_opname (MULT_EXPR);
10158       break;
10159
10160     case CPP_DIV:
10161       id = ansi_opname (TRUNC_DIV_EXPR);
10162       break;
10163
10164     case CPP_MOD:
10165       id = ansi_opname (TRUNC_MOD_EXPR);
10166       break;
10167
10168     case CPP_XOR:
10169       id = ansi_opname (BIT_XOR_EXPR);
10170       break;
10171
10172     case CPP_AND:
10173       id = ansi_opname (BIT_AND_EXPR);
10174       break;
10175
10176     case CPP_OR:
10177       id = ansi_opname (BIT_IOR_EXPR);
10178       break;
10179
10180     case CPP_COMPL:
10181       id = ansi_opname (BIT_NOT_EXPR);
10182       break;
10183
10184     case CPP_NOT:
10185       id = ansi_opname (TRUTH_NOT_EXPR);
10186       break;
10187
10188     case CPP_EQ:
10189       id = ansi_assopname (NOP_EXPR);
10190       break;
10191
10192     case CPP_LESS:
10193       id = ansi_opname (LT_EXPR);
10194       break;
10195
10196     case CPP_GREATER:
10197       id = ansi_opname (GT_EXPR);
10198       break;
10199
10200     case CPP_PLUS_EQ:
10201       id = ansi_assopname (PLUS_EXPR);
10202       break;
10203
10204     case CPP_MINUS_EQ:
10205       id = ansi_assopname (MINUS_EXPR);
10206       break;
10207
10208     case CPP_MULT_EQ:
10209       id = ansi_assopname (MULT_EXPR);
10210       break;
10211
10212     case CPP_DIV_EQ:
10213       id = ansi_assopname (TRUNC_DIV_EXPR);
10214       break;
10215
10216     case CPP_MOD_EQ:
10217       id = ansi_assopname (TRUNC_MOD_EXPR);
10218       break;
10219
10220     case CPP_XOR_EQ:
10221       id = ansi_assopname (BIT_XOR_EXPR);
10222       break;
10223
10224     case CPP_AND_EQ:
10225       id = ansi_assopname (BIT_AND_EXPR);
10226       break;
10227
10228     case CPP_OR_EQ:
10229       id = ansi_assopname (BIT_IOR_EXPR);
10230       break;
10231
10232     case CPP_LSHIFT:
10233       id = ansi_opname (LSHIFT_EXPR);
10234       break;
10235
10236     case CPP_RSHIFT:
10237       id = ansi_opname (RSHIFT_EXPR);
10238       break;
10239
10240     case CPP_LSHIFT_EQ:
10241       id = ansi_assopname (LSHIFT_EXPR);
10242       break;
10243
10244     case CPP_RSHIFT_EQ:
10245       id = ansi_assopname (RSHIFT_EXPR);
10246       break;
10247
10248     case CPP_EQ_EQ:
10249       id = ansi_opname (EQ_EXPR);
10250       break;
10251
10252     case CPP_NOT_EQ:
10253       id = ansi_opname (NE_EXPR);
10254       break;
10255
10256     case CPP_LESS_EQ:
10257       id = ansi_opname (LE_EXPR);
10258       break;
10259
10260     case CPP_GREATER_EQ:
10261       id = ansi_opname (GE_EXPR);
10262       break;
10263
10264     case CPP_AND_AND:
10265       id = ansi_opname (TRUTH_ANDIF_EXPR);
10266       break;
10267
10268     case CPP_OR_OR:
10269       id = ansi_opname (TRUTH_ORIF_EXPR);
10270       break;
10271
10272     case CPP_PLUS_PLUS:
10273       id = ansi_opname (POSTINCREMENT_EXPR);
10274       break;
10275
10276     case CPP_MINUS_MINUS:
10277       id = ansi_opname (PREDECREMENT_EXPR);
10278       break;
10279
10280     case CPP_COMMA:
10281       id = ansi_opname (COMPOUND_EXPR);
10282       break;
10283
10284     case CPP_DEREF_STAR:
10285       id = ansi_opname (MEMBER_REF);
10286       break;
10287
10288     case CPP_DEREF:
10289       id = ansi_opname (COMPONENT_REF);
10290       break;
10291
10292     case CPP_OPEN_PAREN:
10293       /* Consume the `('.  */
10294       cp_lexer_consume_token (parser->lexer);
10295       /* Look for the matching `)'.  */
10296       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10297       return ansi_opname (CALL_EXPR);
10298
10299     case CPP_OPEN_SQUARE:
10300       /* Consume the `['.  */
10301       cp_lexer_consume_token (parser->lexer);
10302       /* Look for the matching `]'.  */
10303       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10304       return ansi_opname (ARRAY_REF);
10305
10306     default:
10307       /* Anything else is an error.  */
10308       break;
10309     }
10310
10311   /* If we have selected an identifier, we need to consume the
10312      operator token.  */
10313   if (id)
10314     cp_lexer_consume_token (parser->lexer);
10315   /* Otherwise, no valid operator name was present.  */
10316   else
10317     {
10318       cp_parser_error (parser, "expected operator");
10319       id = error_mark_node;
10320     }
10321
10322   return id;
10323 }
10324
10325 /* Parse a template-declaration.
10326
10327    template-declaration:
10328      export [opt] template < template-parameter-list > declaration
10329
10330    If MEMBER_P is TRUE, this template-declaration occurs within a
10331    class-specifier.
10332
10333    The grammar rule given by the standard isn't correct.  What
10334    is really meant is:
10335
10336    template-declaration:
10337      export [opt] template-parameter-list-seq
10338        decl-specifier-seq [opt] init-declarator [opt] ;
10339      export [opt] template-parameter-list-seq
10340        function-definition
10341
10342    template-parameter-list-seq:
10343      template-parameter-list-seq [opt]
10344      template < template-parameter-list >  */
10345
10346 static void
10347 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10348 {
10349   /* Check for `export'.  */
10350   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10351     {
10352       /* Consume the `export' token.  */
10353       cp_lexer_consume_token (parser->lexer);
10354       /* Warn that we do not support `export'.  */
10355       warning (0, "keyword %<export%> not implemented, and will be ignored");
10356     }
10357
10358   cp_parser_template_declaration_after_export (parser, member_p);
10359 }
10360
10361 /* Parse a template-parameter-list.
10362
10363    template-parameter-list:
10364      template-parameter
10365      template-parameter-list , template-parameter
10366
10367    Returns a TREE_LIST.  Each node represents a template parameter.
10368    The nodes are connected via their TREE_CHAINs.  */
10369
10370 static tree
10371 cp_parser_template_parameter_list (cp_parser* parser)
10372 {
10373   tree parameter_list = NULL_TREE;
10374
10375   begin_template_parm_list ();
10376   while (true)
10377     {
10378       tree parameter;
10379       bool is_non_type;
10380       bool is_parameter_pack;
10381       location_t parm_loc;
10382
10383       /* Parse the template-parameter.  */
10384       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10385       parameter = cp_parser_template_parameter (parser, 
10386                                                 &is_non_type,
10387                                                 &is_parameter_pack);
10388       /* Add it to the list.  */
10389       if (parameter != error_mark_node)
10390         parameter_list = process_template_parm (parameter_list,
10391                                                 parm_loc,
10392                                                 parameter,
10393                                                 is_non_type,
10394                                                 is_parameter_pack);
10395       else
10396        {
10397          tree err_parm = build_tree_list (parameter, parameter);
10398          TREE_VALUE (err_parm) = error_mark_node;
10399          parameter_list = chainon (parameter_list, err_parm);
10400        }
10401
10402       /* If the next token is not a `,', we're done.  */
10403       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10404         break;
10405       /* Otherwise, consume the `,' token.  */
10406       cp_lexer_consume_token (parser->lexer);
10407     }
10408
10409   return end_template_parm_list (parameter_list);
10410 }
10411
10412 /* Parse a template-parameter.
10413
10414    template-parameter:
10415      type-parameter
10416      parameter-declaration
10417
10418    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
10419    the parameter.  The TREE_PURPOSE is the default value, if any.
10420    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
10421    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
10422    set to true iff this parameter is a parameter pack. */
10423
10424 static tree
10425 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10426                               bool *is_parameter_pack)
10427 {
10428   cp_token *token;
10429   cp_parameter_declarator *parameter_declarator;
10430   cp_declarator *id_declarator;
10431   tree parm;
10432
10433   /* Assume it is a type parameter or a template parameter.  */
10434   *is_non_type = false;
10435   /* Assume it not a parameter pack. */
10436   *is_parameter_pack = false;
10437   /* Peek at the next token.  */
10438   token = cp_lexer_peek_token (parser->lexer);
10439   /* If it is `class' or `template', we have a type-parameter.  */
10440   if (token->keyword == RID_TEMPLATE)
10441     return cp_parser_type_parameter (parser, is_parameter_pack);
10442   /* If it is `class' or `typename' we do not know yet whether it is a
10443      type parameter or a non-type parameter.  Consider:
10444
10445        template <typename T, typename T::X X> ...
10446
10447      or:
10448
10449        template <class C, class D*> ...
10450
10451      Here, the first parameter is a type parameter, and the second is
10452      a non-type parameter.  We can tell by looking at the token after
10453      the identifier -- if it is a `,', `=', or `>' then we have a type
10454      parameter.  */
10455   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10456     {
10457       /* Peek at the token after `class' or `typename'.  */
10458       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10459       /* If it's an ellipsis, we have a template type parameter
10460          pack. */
10461       if (token->type == CPP_ELLIPSIS)
10462         return cp_parser_type_parameter (parser, is_parameter_pack);
10463       /* If it's an identifier, skip it.  */
10464       if (token->type == CPP_NAME)
10465         token = cp_lexer_peek_nth_token (parser->lexer, 3);
10466       /* Now, see if the token looks like the end of a template
10467          parameter.  */
10468       if (token->type == CPP_COMMA
10469           || token->type == CPP_EQ
10470           || token->type == CPP_GREATER)
10471         return cp_parser_type_parameter (parser, is_parameter_pack);
10472     }
10473
10474   /* Otherwise, it is a non-type parameter.
10475
10476      [temp.param]
10477
10478      When parsing a default template-argument for a non-type
10479      template-parameter, the first non-nested `>' is taken as the end
10480      of the template parameter-list rather than a greater-than
10481      operator.  */
10482   *is_non_type = true;
10483   parameter_declarator
10484      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10485                                         /*parenthesized_p=*/NULL);
10486
10487   /* If the parameter declaration is marked as a parameter pack, set
10488      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10489      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10490      grokdeclarator. */
10491   if (parameter_declarator
10492       && parameter_declarator->declarator
10493       && parameter_declarator->declarator->parameter_pack_p)
10494     {
10495       *is_parameter_pack = true;
10496       parameter_declarator->declarator->parameter_pack_p = false;
10497     }
10498
10499   /* If the next token is an ellipsis, and we don't already have it
10500      marked as a parameter pack, then we have a parameter pack (that
10501      has no declarator).  */
10502   if (!*is_parameter_pack
10503       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10504       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10505     {
10506       /* Consume the `...'.  */
10507       cp_lexer_consume_token (parser->lexer);
10508       maybe_warn_variadic_templates ();
10509       
10510       *is_parameter_pack = true;
10511     }
10512   /* We might end up with a pack expansion as the type of the non-type
10513      template parameter, in which case this is a non-type template
10514      parameter pack.  */
10515   else if (parameter_declarator
10516            && parameter_declarator->decl_specifiers.type
10517            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10518     {
10519       *is_parameter_pack = true;
10520       parameter_declarator->decl_specifiers.type = 
10521         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10522     }
10523
10524   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10525     {
10526       /* Parameter packs cannot have default arguments.  However, a
10527          user may try to do so, so we'll parse them and give an
10528          appropriate diagnostic here.  */
10529
10530       /* Consume the `='.  */
10531       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10532       cp_lexer_consume_token (parser->lexer);
10533       
10534       /* Find the name of the parameter pack.  */     
10535       id_declarator = parameter_declarator->declarator;
10536       while (id_declarator && id_declarator->kind != cdk_id)
10537         id_declarator = id_declarator->declarator;
10538       
10539       if (id_declarator && id_declarator->kind == cdk_id)
10540         error_at (start_token->location,
10541                   "template parameter pack %qD cannot have a default argument",
10542                   id_declarator->u.id.unqualified_name);
10543       else
10544         error_at (start_token->location,
10545                   "template parameter pack cannot have a default argument");
10546       
10547       /* Parse the default argument, but throw away the result.  */
10548       cp_parser_default_argument (parser, /*template_parm_p=*/true);
10549     }
10550
10551   parm = grokdeclarator (parameter_declarator->declarator,
10552                          &parameter_declarator->decl_specifiers,
10553                          TPARM, /*initialized=*/0,
10554                          /*attrlist=*/NULL);
10555   if (parm == error_mark_node)
10556     return error_mark_node;
10557
10558   return build_tree_list (parameter_declarator->default_argument, parm);
10559 }
10560
10561 /* Parse a type-parameter.
10562
10563    type-parameter:
10564      class identifier [opt]
10565      class identifier [opt] = type-id
10566      typename identifier [opt]
10567      typename identifier [opt] = type-id
10568      template < template-parameter-list > class identifier [opt]
10569      template < template-parameter-list > class identifier [opt]
10570        = id-expression
10571
10572    GNU Extension (variadic templates):
10573
10574    type-parameter:
10575      class ... identifier [opt]
10576      typename ... identifier [opt]
10577
10578    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
10579    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
10580    the declaration of the parameter.
10581
10582    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10583
10584 static tree
10585 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10586 {
10587   cp_token *token;
10588   tree parameter;
10589
10590   /* Look for a keyword to tell us what kind of parameter this is.  */
10591   token = cp_parser_require (parser, CPP_KEYWORD,
10592                              "%<class%>, %<typename%>, or %<template%>");
10593   if (!token)
10594     return error_mark_node;
10595
10596   switch (token->keyword)
10597     {
10598     case RID_CLASS:
10599     case RID_TYPENAME:
10600       {
10601         tree identifier;
10602         tree default_argument;
10603
10604         /* If the next token is an ellipsis, we have a template
10605            argument pack. */
10606         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10607           {
10608             /* Consume the `...' token. */
10609             cp_lexer_consume_token (parser->lexer);
10610             maybe_warn_variadic_templates ();
10611
10612             *is_parameter_pack = true;
10613           }
10614
10615         /* If the next token is an identifier, then it names the
10616            parameter.  */
10617         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10618           identifier = cp_parser_identifier (parser);
10619         else
10620           identifier = NULL_TREE;
10621
10622         /* Create the parameter.  */
10623         parameter = finish_template_type_parm (class_type_node, identifier);
10624
10625         /* If the next token is an `=', we have a default argument.  */
10626         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10627           {
10628             /* Consume the `=' token.  */
10629             cp_lexer_consume_token (parser->lexer);
10630             /* Parse the default-argument.  */
10631             push_deferring_access_checks (dk_no_deferred);
10632             default_argument = cp_parser_type_id (parser);
10633
10634             /* Template parameter packs cannot have default
10635                arguments. */
10636             if (*is_parameter_pack)
10637               {
10638                 if (identifier)
10639                   error_at (token->location,
10640                             "template parameter pack %qD cannot have a "
10641                             "default argument", identifier);
10642                 else
10643                   error_at (token->location,
10644                             "template parameter packs cannot have "
10645                             "default arguments");
10646                 default_argument = NULL_TREE;
10647               }
10648             pop_deferring_access_checks ();
10649           }
10650         else
10651           default_argument = NULL_TREE;
10652
10653         /* Create the combined representation of the parameter and the
10654            default argument.  */
10655         parameter = build_tree_list (default_argument, parameter);
10656       }
10657       break;
10658
10659     case RID_TEMPLATE:
10660       {
10661         tree identifier;
10662         tree default_argument;
10663
10664         /* Look for the `<'.  */
10665         cp_parser_require (parser, CPP_LESS, "%<<%>");
10666         /* Parse the template-parameter-list.  */
10667         cp_parser_template_parameter_list (parser);
10668         /* Look for the `>'.  */
10669         cp_parser_require (parser, CPP_GREATER, "%<>%>");
10670         /* Look for the `class' keyword.  */
10671         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10672         /* If the next token is an ellipsis, we have a template
10673            argument pack. */
10674         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10675           {
10676             /* Consume the `...' token. */
10677             cp_lexer_consume_token (parser->lexer);
10678             maybe_warn_variadic_templates ();
10679
10680             *is_parameter_pack = true;
10681           }
10682         /* If the next token is an `=', then there is a
10683            default-argument.  If the next token is a `>', we are at
10684            the end of the parameter-list.  If the next token is a `,',
10685            then we are at the end of this parameter.  */
10686         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10687             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10688             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10689           {
10690             identifier = cp_parser_identifier (parser);
10691             /* Treat invalid names as if the parameter were nameless.  */
10692             if (identifier == error_mark_node)
10693               identifier = NULL_TREE;
10694           }
10695         else
10696           identifier = NULL_TREE;
10697
10698         /* Create the template parameter.  */
10699         parameter = finish_template_template_parm (class_type_node,
10700                                                    identifier);
10701
10702         /* If the next token is an `=', then there is a
10703            default-argument.  */
10704         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10705           {
10706             bool is_template;
10707
10708             /* Consume the `='.  */
10709             cp_lexer_consume_token (parser->lexer);
10710             /* Parse the id-expression.  */
10711             push_deferring_access_checks (dk_no_deferred);
10712             /* save token before parsing the id-expression, for error
10713                reporting */
10714             token = cp_lexer_peek_token (parser->lexer);
10715             default_argument
10716               = cp_parser_id_expression (parser,
10717                                          /*template_keyword_p=*/false,
10718                                          /*check_dependency_p=*/true,
10719                                          /*template_p=*/&is_template,
10720                                          /*declarator_p=*/false,
10721                                          /*optional_p=*/false);
10722             if (TREE_CODE (default_argument) == TYPE_DECL)
10723               /* If the id-expression was a template-id that refers to
10724                  a template-class, we already have the declaration here,
10725                  so no further lookup is needed.  */
10726                  ;
10727             else
10728               /* Look up the name.  */
10729               default_argument
10730                 = cp_parser_lookup_name (parser, default_argument,
10731                                          none_type,
10732                                          /*is_template=*/is_template,
10733                                          /*is_namespace=*/false,
10734                                          /*check_dependency=*/true,
10735                                          /*ambiguous_decls=*/NULL,
10736                                          token->location);
10737             /* See if the default argument is valid.  */
10738             default_argument
10739               = check_template_template_default_arg (default_argument);
10740
10741             /* Template parameter packs cannot have default
10742                arguments. */
10743             if (*is_parameter_pack)
10744               {
10745                 if (identifier)
10746                   error_at (token->location,
10747                             "template parameter pack %qD cannot "
10748                             "have a default argument",
10749                             identifier);
10750                 else
10751                   error_at (token->location, "template parameter packs cannot "
10752                             "have default arguments");
10753                 default_argument = NULL_TREE;
10754               }
10755             pop_deferring_access_checks ();
10756           }
10757         else
10758           default_argument = NULL_TREE;
10759
10760         /* Create the combined representation of the parameter and the
10761            default argument.  */
10762         parameter = build_tree_list (default_argument, parameter);
10763       }
10764       break;
10765
10766     default:
10767       gcc_unreachable ();
10768       break;
10769     }
10770
10771   return parameter;
10772 }
10773
10774 /* Parse a template-id.
10775
10776    template-id:
10777      template-name < template-argument-list [opt] >
10778
10779    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10780    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10781    returned.  Otherwise, if the template-name names a function, or set
10782    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10783    names a class, returns a TYPE_DECL for the specialization.
10784
10785    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10786    uninstantiated templates.  */
10787
10788 static tree
10789 cp_parser_template_id (cp_parser *parser,
10790                        bool template_keyword_p,
10791                        bool check_dependency_p,
10792                        bool is_declaration)
10793 {
10794   int i;
10795   tree templ;
10796   tree arguments;
10797   tree template_id;
10798   cp_token_position start_of_id = 0;
10799   deferred_access_check *chk;
10800   VEC (deferred_access_check,gc) *access_check;
10801   cp_token *next_token = NULL, *next_token_2 = NULL;
10802   bool is_identifier;
10803
10804   /* If the next token corresponds to a template-id, there is no need
10805      to reparse it.  */
10806   next_token = cp_lexer_peek_token (parser->lexer);
10807   if (next_token->type == CPP_TEMPLATE_ID)
10808     {
10809       struct tree_check *check_value;
10810
10811       /* Get the stored value.  */
10812       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10813       /* Perform any access checks that were deferred.  */
10814       access_check = check_value->checks;
10815       if (access_check)
10816         {
10817           for (i = 0 ;
10818                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10819                ++i)
10820             {
10821               perform_or_defer_access_check (chk->binfo,
10822                                              chk->decl,
10823                                              chk->diag_decl);
10824             }
10825         }
10826       /* Return the stored value.  */
10827       return check_value->value;
10828     }
10829
10830   /* Avoid performing name lookup if there is no possibility of
10831      finding a template-id.  */
10832   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10833       || (next_token->type == CPP_NAME
10834           && !cp_parser_nth_token_starts_template_argument_list_p
10835                (parser, 2)))
10836     {
10837       cp_parser_error (parser, "expected template-id");
10838       return error_mark_node;
10839     }
10840
10841   /* Remember where the template-id starts.  */
10842   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10843     start_of_id = cp_lexer_token_position (parser->lexer, false);
10844
10845   push_deferring_access_checks (dk_deferred);
10846
10847   /* Parse the template-name.  */
10848   is_identifier = false;
10849   templ = cp_parser_template_name (parser, template_keyword_p,
10850                                    check_dependency_p,
10851                                    is_declaration,
10852                                    &is_identifier);
10853   if (templ == error_mark_node || is_identifier)
10854     {
10855       pop_deferring_access_checks ();
10856       return templ;
10857     }
10858
10859   /* If we find the sequence `[:' after a template-name, it's probably
10860      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10861      parse correctly the argument list.  */
10862   next_token = cp_lexer_peek_token (parser->lexer);
10863   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10864   if (next_token->type == CPP_OPEN_SQUARE
10865       && next_token->flags & DIGRAPH
10866       && next_token_2->type == CPP_COLON
10867       && !(next_token_2->flags & PREV_WHITE))
10868     {
10869       cp_parser_parse_tentatively (parser);
10870       /* Change `:' into `::'.  */
10871       next_token_2->type = CPP_SCOPE;
10872       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10873          CPP_LESS.  */
10874       cp_lexer_consume_token (parser->lexer);
10875
10876       /* Parse the arguments.  */
10877       arguments = cp_parser_enclosed_template_argument_list (parser);
10878       if (!cp_parser_parse_definitely (parser))
10879         {
10880           /* If we couldn't parse an argument list, then we revert our changes
10881              and return simply an error. Maybe this is not a template-id
10882              after all.  */
10883           next_token_2->type = CPP_COLON;
10884           cp_parser_error (parser, "expected %<<%>");
10885           pop_deferring_access_checks ();
10886           return error_mark_node;
10887         }
10888       /* Otherwise, emit an error about the invalid digraph, but continue
10889          parsing because we got our argument list.  */
10890       if (permerror (next_token->location,
10891                      "%<<::%> cannot begin a template-argument list"))
10892         {
10893           static bool hint = false;
10894           inform (next_token->location,
10895                   "%<<:%> is an alternate spelling for %<[%>."
10896                   " Insert whitespace between %<<%> and %<::%>");
10897           if (!hint && !flag_permissive)
10898             {
10899               inform (next_token->location, "(if you use %<-fpermissive%>"
10900                       " G++ will accept your code)");
10901               hint = true;
10902             }
10903         }
10904     }
10905   else
10906     {
10907       /* Look for the `<' that starts the template-argument-list.  */
10908       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10909         {
10910           pop_deferring_access_checks ();
10911           return error_mark_node;
10912         }
10913       /* Parse the arguments.  */
10914       arguments = cp_parser_enclosed_template_argument_list (parser);
10915     }
10916
10917   /* Build a representation of the specialization.  */
10918   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10919     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10920   else if (DECL_CLASS_TEMPLATE_P (templ)
10921            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10922     {
10923       bool entering_scope;
10924       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10925          template (rather than some instantiation thereof) only if
10926          is not nested within some other construct.  For example, in
10927          "template <typename T> void f(T) { A<T>::", A<T> is just an
10928          instantiation of A.  */
10929       entering_scope = (template_parm_scope_p ()
10930                         && cp_lexer_next_token_is (parser->lexer,
10931                                                    CPP_SCOPE));
10932       template_id
10933         = finish_template_type (templ, arguments, entering_scope);
10934     }
10935   else
10936     {
10937       /* If it's not a class-template or a template-template, it should be
10938          a function-template.  */
10939       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10940                    || TREE_CODE (templ) == OVERLOAD
10941                    || BASELINK_P (templ)));
10942
10943       template_id = lookup_template_function (templ, arguments);
10944     }
10945
10946   /* If parsing tentatively, replace the sequence of tokens that makes
10947      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10948      should we re-parse the token stream, we will not have to repeat
10949      the effort required to do the parse, nor will we issue duplicate
10950      error messages about problems during instantiation of the
10951      template.  */
10952   if (start_of_id)
10953     {
10954       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10955
10956       /* Reset the contents of the START_OF_ID token.  */
10957       token->type = CPP_TEMPLATE_ID;
10958       /* Retrieve any deferred checks.  Do not pop this access checks yet
10959          so the memory will not be reclaimed during token replacing below.  */
10960       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10961       token->u.tree_check_value->value = template_id;
10962       token->u.tree_check_value->checks = get_deferred_access_checks ();
10963       token->keyword = RID_MAX;
10964
10965       /* Purge all subsequent tokens.  */
10966       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10967
10968       /* ??? Can we actually assume that, if template_id ==
10969          error_mark_node, we will have issued a diagnostic to the
10970          user, as opposed to simply marking the tentative parse as
10971          failed?  */
10972       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10973         error_at (token->location, "parse error in template argument list");
10974     }
10975
10976   pop_deferring_access_checks ();
10977   return template_id;
10978 }
10979
10980 /* Parse a template-name.
10981
10982    template-name:
10983      identifier
10984
10985    The standard should actually say:
10986
10987    template-name:
10988      identifier
10989      operator-function-id
10990
10991    A defect report has been filed about this issue.
10992
10993    A conversion-function-id cannot be a template name because they cannot
10994    be part of a template-id. In fact, looking at this code:
10995
10996    a.operator K<int>()
10997
10998    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10999    It is impossible to call a templated conversion-function-id with an
11000    explicit argument list, since the only allowed template parameter is
11001    the type to which it is converting.
11002
11003    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11004    `template' keyword, in a construction like:
11005
11006      T::template f<3>()
11007
11008    In that case `f' is taken to be a template-name, even though there
11009    is no way of knowing for sure.
11010
11011    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11012    name refers to a set of overloaded functions, at least one of which
11013    is a template, or an IDENTIFIER_NODE with the name of the template,
11014    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11015    names are looked up inside uninstantiated templates.  */
11016
11017 static tree
11018 cp_parser_template_name (cp_parser* parser,
11019                          bool template_keyword_p,
11020                          bool check_dependency_p,
11021                          bool is_declaration,
11022                          bool *is_identifier)
11023 {
11024   tree identifier;
11025   tree decl;
11026   tree fns;
11027   cp_token *token = cp_lexer_peek_token (parser->lexer);
11028
11029   /* If the next token is `operator', then we have either an
11030      operator-function-id or a conversion-function-id.  */
11031   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11032     {
11033       /* We don't know whether we're looking at an
11034          operator-function-id or a conversion-function-id.  */
11035       cp_parser_parse_tentatively (parser);
11036       /* Try an operator-function-id.  */
11037       identifier = cp_parser_operator_function_id (parser);
11038       /* If that didn't work, try a conversion-function-id.  */
11039       if (!cp_parser_parse_definitely (parser))
11040         {
11041           cp_parser_error (parser, "expected template-name");
11042           return error_mark_node;
11043         }
11044     }
11045   /* Look for the identifier.  */
11046   else
11047     identifier = cp_parser_identifier (parser);
11048
11049   /* If we didn't find an identifier, we don't have a template-id.  */
11050   if (identifier == error_mark_node)
11051     return error_mark_node;
11052
11053   /* If the name immediately followed the `template' keyword, then it
11054      is a template-name.  However, if the next token is not `<', then
11055      we do not treat it as a template-name, since it is not being used
11056      as part of a template-id.  This enables us to handle constructs
11057      like:
11058
11059        template <typename T> struct S { S(); };
11060        template <typename T> S<T>::S();
11061
11062      correctly.  We would treat `S' as a template -- if it were `S<T>'
11063      -- but we do not if there is no `<'.  */
11064
11065   if (processing_template_decl
11066       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11067     {
11068       /* In a declaration, in a dependent context, we pretend that the
11069          "template" keyword was present in order to improve error
11070          recovery.  For example, given:
11071
11072            template <typename T> void f(T::X<int>);
11073
11074          we want to treat "X<int>" as a template-id.  */
11075       if (is_declaration
11076           && !template_keyword_p
11077           && parser->scope && TYPE_P (parser->scope)
11078           && check_dependency_p
11079           && dependent_scope_p (parser->scope)
11080           /* Do not do this for dtors (or ctors), since they never
11081              need the template keyword before their name.  */
11082           && !constructor_name_p (identifier, parser->scope))
11083         {
11084           cp_token_position start = 0;
11085
11086           /* Explain what went wrong.  */
11087           error_at (token->location, "non-template %qD used as template",
11088                     identifier);
11089           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11090                   parser->scope, identifier);
11091           /* If parsing tentatively, find the location of the "<" token.  */
11092           if (cp_parser_simulate_error (parser))
11093             start = cp_lexer_token_position (parser->lexer, true);
11094           /* Parse the template arguments so that we can issue error
11095              messages about them.  */
11096           cp_lexer_consume_token (parser->lexer);
11097           cp_parser_enclosed_template_argument_list (parser);
11098           /* Skip tokens until we find a good place from which to
11099              continue parsing.  */
11100           cp_parser_skip_to_closing_parenthesis (parser,
11101                                                  /*recovering=*/true,
11102                                                  /*or_comma=*/true,
11103                                                  /*consume_paren=*/false);
11104           /* If parsing tentatively, permanently remove the
11105              template argument list.  That will prevent duplicate
11106              error messages from being issued about the missing
11107              "template" keyword.  */
11108           if (start)
11109             cp_lexer_purge_tokens_after (parser->lexer, start);
11110           if (is_identifier)
11111             *is_identifier = true;
11112           return identifier;
11113         }
11114
11115       /* If the "template" keyword is present, then there is generally
11116          no point in doing name-lookup, so we just return IDENTIFIER.
11117          But, if the qualifying scope is non-dependent then we can
11118          (and must) do name-lookup normally.  */
11119       if (template_keyword_p
11120           && (!parser->scope
11121               || (TYPE_P (parser->scope)
11122                   && dependent_type_p (parser->scope))))
11123         return identifier;
11124     }
11125
11126   /* Look up the name.  */
11127   decl = cp_parser_lookup_name (parser, identifier,
11128                                 none_type,
11129                                 /*is_template=*/true,
11130                                 /*is_namespace=*/false,
11131                                 check_dependency_p,
11132                                 /*ambiguous_decls=*/NULL,
11133                                 token->location);
11134
11135   /* If DECL is a template, then the name was a template-name.  */
11136   if (TREE_CODE (decl) == TEMPLATE_DECL)
11137     ;
11138   else
11139     {
11140       tree fn = NULL_TREE;
11141
11142       /* The standard does not explicitly indicate whether a name that
11143          names a set of overloaded declarations, some of which are
11144          templates, is a template-name.  However, such a name should
11145          be a template-name; otherwise, there is no way to form a
11146          template-id for the overloaded templates.  */
11147       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11148       if (TREE_CODE (fns) == OVERLOAD)
11149         for (fn = fns; fn; fn = OVL_NEXT (fn))
11150           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11151             break;
11152
11153       if (!fn)
11154         {
11155           /* The name does not name a template.  */
11156           cp_parser_error (parser, "expected template-name");
11157           return error_mark_node;
11158         }
11159     }
11160
11161   /* If DECL is dependent, and refers to a function, then just return
11162      its name; we will look it up again during template instantiation.  */
11163   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11164     {
11165       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11166       if (TYPE_P (scope) && dependent_type_p (scope))
11167         return identifier;
11168     }
11169
11170   return decl;
11171 }
11172
11173 /* Parse a template-argument-list.
11174
11175    template-argument-list:
11176      template-argument ... [opt]
11177      template-argument-list , template-argument ... [opt]
11178
11179    Returns a TREE_VEC containing the arguments.  */
11180
11181 static tree
11182 cp_parser_template_argument_list (cp_parser* parser)
11183 {
11184   tree fixed_args[10];
11185   unsigned n_args = 0;
11186   unsigned alloced = 10;
11187   tree *arg_ary = fixed_args;
11188   tree vec;
11189   bool saved_in_template_argument_list_p;
11190   bool saved_ice_p;
11191   bool saved_non_ice_p;
11192
11193   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11194   parser->in_template_argument_list_p = true;
11195   /* Even if the template-id appears in an integral
11196      constant-expression, the contents of the argument list do
11197      not.  */
11198   saved_ice_p = parser->integral_constant_expression_p;
11199   parser->integral_constant_expression_p = false;
11200   saved_non_ice_p = parser->non_integral_constant_expression_p;
11201   parser->non_integral_constant_expression_p = false;
11202   /* Parse the arguments.  */
11203   do
11204     {
11205       tree argument;
11206
11207       if (n_args)
11208         /* Consume the comma.  */
11209         cp_lexer_consume_token (parser->lexer);
11210
11211       /* Parse the template-argument.  */
11212       argument = cp_parser_template_argument (parser);
11213
11214       /* If the next token is an ellipsis, we're expanding a template
11215          argument pack. */
11216       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11217         {
11218           if (argument == error_mark_node)
11219             {
11220               cp_token *token = cp_lexer_peek_token (parser->lexer);
11221               error_at (token->location,
11222                         "expected parameter pack before %<...%>");
11223             }
11224           /* Consume the `...' token. */
11225           cp_lexer_consume_token (parser->lexer);
11226
11227           /* Make the argument into a TYPE_PACK_EXPANSION or
11228              EXPR_PACK_EXPANSION. */
11229           argument = make_pack_expansion (argument);
11230         }
11231
11232       if (n_args == alloced)
11233         {
11234           alloced *= 2;
11235
11236           if (arg_ary == fixed_args)
11237             {
11238               arg_ary = XNEWVEC (tree, alloced);
11239               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11240             }
11241           else
11242             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11243         }
11244       arg_ary[n_args++] = argument;
11245     }
11246   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11247
11248   vec = make_tree_vec (n_args);
11249
11250   while (n_args--)
11251     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11252
11253   if (arg_ary != fixed_args)
11254     free (arg_ary);
11255   parser->non_integral_constant_expression_p = saved_non_ice_p;
11256   parser->integral_constant_expression_p = saved_ice_p;
11257   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11258 #ifdef ENABLE_CHECKING
11259   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11260 #endif
11261   return vec;
11262 }
11263
11264 /* Parse a template-argument.
11265
11266    template-argument:
11267      assignment-expression
11268      type-id
11269      id-expression
11270
11271    The representation is that of an assignment-expression, type-id, or
11272    id-expression -- except that the qualified id-expression is
11273    evaluated, so that the value returned is either a DECL or an
11274    OVERLOAD.
11275
11276    Although the standard says "assignment-expression", it forbids
11277    throw-expressions or assignments in the template argument.
11278    Therefore, we use "conditional-expression" instead.  */
11279
11280 static tree
11281 cp_parser_template_argument (cp_parser* parser)
11282 {
11283   tree argument;
11284   bool template_p;
11285   bool address_p;
11286   bool maybe_type_id = false;
11287   cp_token *token = NULL, *argument_start_token = NULL;
11288   cp_id_kind idk;
11289
11290   /* There's really no way to know what we're looking at, so we just
11291      try each alternative in order.
11292
11293        [temp.arg]
11294
11295        In a template-argument, an ambiguity between a type-id and an
11296        expression is resolved to a type-id, regardless of the form of
11297        the corresponding template-parameter.
11298
11299      Therefore, we try a type-id first.  */
11300   cp_parser_parse_tentatively (parser);
11301   argument = cp_parser_template_type_arg (parser);
11302   /* If there was no error parsing the type-id but the next token is a
11303      '>>', our behavior depends on which dialect of C++ we're
11304      parsing. In C++98, we probably found a typo for '> >'. But there
11305      are type-id which are also valid expressions. For instance:
11306
11307      struct X { int operator >> (int); };
11308      template <int V> struct Foo {};
11309      Foo<X () >> 5> r;
11310
11311      Here 'X()' is a valid type-id of a function type, but the user just
11312      wanted to write the expression "X() >> 5". Thus, we remember that we
11313      found a valid type-id, but we still try to parse the argument as an
11314      expression to see what happens. 
11315
11316      In C++0x, the '>>' will be considered two separate '>'
11317      tokens.  */
11318   if (!cp_parser_error_occurred (parser)
11319       && cxx_dialect == cxx98
11320       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11321     {
11322       maybe_type_id = true;
11323       cp_parser_abort_tentative_parse (parser);
11324     }
11325   else
11326     {
11327       /* If the next token isn't a `,' or a `>', then this argument wasn't
11328       really finished. This means that the argument is not a valid
11329       type-id.  */
11330       if (!cp_parser_next_token_ends_template_argument_p (parser))
11331         cp_parser_error (parser, "expected template-argument");
11332       /* If that worked, we're done.  */
11333       if (cp_parser_parse_definitely (parser))
11334         return argument;
11335     }
11336   /* We're still not sure what the argument will be.  */
11337   cp_parser_parse_tentatively (parser);
11338   /* Try a template.  */
11339   argument_start_token = cp_lexer_peek_token (parser->lexer);
11340   argument = cp_parser_id_expression (parser,
11341                                       /*template_keyword_p=*/false,
11342                                       /*check_dependency_p=*/true,
11343                                       &template_p,
11344                                       /*declarator_p=*/false,
11345                                       /*optional_p=*/false);
11346   /* If the next token isn't a `,' or a `>', then this argument wasn't
11347      really finished.  */
11348   if (!cp_parser_next_token_ends_template_argument_p (parser))
11349     cp_parser_error (parser, "expected template-argument");
11350   if (!cp_parser_error_occurred (parser))
11351     {
11352       /* Figure out what is being referred to.  If the id-expression
11353          was for a class template specialization, then we will have a
11354          TYPE_DECL at this point.  There is no need to do name lookup
11355          at this point in that case.  */
11356       if (TREE_CODE (argument) != TYPE_DECL)
11357         argument = cp_parser_lookup_name (parser, argument,
11358                                           none_type,
11359                                           /*is_template=*/template_p,
11360                                           /*is_namespace=*/false,
11361                                           /*check_dependency=*/true,
11362                                           /*ambiguous_decls=*/NULL,
11363                                           argument_start_token->location);
11364       if (TREE_CODE (argument) != TEMPLATE_DECL
11365           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11366         cp_parser_error (parser, "expected template-name");
11367     }
11368   if (cp_parser_parse_definitely (parser))
11369     return argument;
11370   /* It must be a non-type argument.  There permitted cases are given
11371      in [temp.arg.nontype]:
11372
11373      -- an integral constant-expression of integral or enumeration
11374         type; or
11375
11376      -- the name of a non-type template-parameter; or
11377
11378      -- the name of an object or function with external linkage...
11379
11380      -- the address of an object or function with external linkage...
11381
11382      -- a pointer to member...  */
11383   /* Look for a non-type template parameter.  */
11384   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11385     {
11386       cp_parser_parse_tentatively (parser);
11387       argument = cp_parser_primary_expression (parser,
11388                                                /*address_p=*/false,
11389                                                /*cast_p=*/false,
11390                                                /*template_arg_p=*/true,
11391                                                &idk);
11392       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11393           || !cp_parser_next_token_ends_template_argument_p (parser))
11394         cp_parser_simulate_error (parser);
11395       if (cp_parser_parse_definitely (parser))
11396         return argument;
11397     }
11398
11399   /* If the next token is "&", the argument must be the address of an
11400      object or function with external linkage.  */
11401   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11402   if (address_p)
11403     cp_lexer_consume_token (parser->lexer);
11404   /* See if we might have an id-expression.  */
11405   token = cp_lexer_peek_token (parser->lexer);
11406   if (token->type == CPP_NAME
11407       || token->keyword == RID_OPERATOR
11408       || token->type == CPP_SCOPE
11409       || token->type == CPP_TEMPLATE_ID
11410       || token->type == CPP_NESTED_NAME_SPECIFIER)
11411     {
11412       cp_parser_parse_tentatively (parser);
11413       argument = cp_parser_primary_expression (parser,
11414                                                address_p,
11415                                                /*cast_p=*/false,
11416                                                /*template_arg_p=*/true,
11417                                                &idk);
11418       if (cp_parser_error_occurred (parser)
11419           || !cp_parser_next_token_ends_template_argument_p (parser))
11420         cp_parser_abort_tentative_parse (parser);
11421       else
11422         {
11423           tree probe;
11424
11425           if (TREE_CODE (argument) == INDIRECT_REF)
11426             {
11427               gcc_assert (REFERENCE_REF_P (argument));
11428               argument = TREE_OPERAND (argument, 0);
11429             }
11430
11431           /* If we're in a template, we represent a qualified-id referring
11432              to a static data member as a SCOPE_REF even if the scope isn't
11433              dependent so that we can check access control later.  */
11434           probe = argument;
11435           if (TREE_CODE (probe) == SCOPE_REF)
11436             probe = TREE_OPERAND (probe, 1);
11437           if (TREE_CODE (probe) == VAR_DECL)
11438             {
11439               /* A variable without external linkage might still be a
11440                  valid constant-expression, so no error is issued here
11441                  if the external-linkage check fails.  */
11442               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11443                 cp_parser_simulate_error (parser);
11444             }
11445           else if (is_overloaded_fn (argument))
11446             /* All overloaded functions are allowed; if the external
11447                linkage test does not pass, an error will be issued
11448                later.  */
11449             ;
11450           else if (address_p
11451                    && (TREE_CODE (argument) == OFFSET_REF
11452                        || TREE_CODE (argument) == SCOPE_REF))
11453             /* A pointer-to-member.  */
11454             ;
11455           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11456             ;
11457           else
11458             cp_parser_simulate_error (parser);
11459
11460           if (cp_parser_parse_definitely (parser))
11461             {
11462               if (address_p)
11463                 argument = build_x_unary_op (ADDR_EXPR, argument,
11464                                              tf_warning_or_error);
11465               return argument;
11466             }
11467         }
11468     }
11469   /* If the argument started with "&", there are no other valid
11470      alternatives at this point.  */
11471   if (address_p)
11472     {
11473       cp_parser_error (parser, "invalid non-type template argument");
11474       return error_mark_node;
11475     }
11476
11477   /* If the argument wasn't successfully parsed as a type-id followed
11478      by '>>', the argument can only be a constant expression now.
11479      Otherwise, we try parsing the constant-expression tentatively,
11480      because the argument could really be a type-id.  */
11481   if (maybe_type_id)
11482     cp_parser_parse_tentatively (parser);
11483   argument = cp_parser_constant_expression (parser,
11484                                             /*allow_non_constant_p=*/false,
11485                                             /*non_constant_p=*/NULL);
11486   argument = fold_non_dependent_expr (argument);
11487   if (!maybe_type_id)
11488     return argument;
11489   if (!cp_parser_next_token_ends_template_argument_p (parser))
11490     cp_parser_error (parser, "expected template-argument");
11491   if (cp_parser_parse_definitely (parser))
11492     return argument;
11493   /* We did our best to parse the argument as a non type-id, but that
11494      was the only alternative that matched (albeit with a '>' after
11495      it). We can assume it's just a typo from the user, and a
11496      diagnostic will then be issued.  */
11497   return cp_parser_template_type_arg (parser);
11498 }
11499
11500 /* Parse an explicit-instantiation.
11501
11502    explicit-instantiation:
11503      template declaration
11504
11505    Although the standard says `declaration', what it really means is:
11506
11507    explicit-instantiation:
11508      template decl-specifier-seq [opt] declarator [opt] ;
11509
11510    Things like `template int S<int>::i = 5, int S<double>::j;' are not
11511    supposed to be allowed.  A defect report has been filed about this
11512    issue.
11513
11514    GNU Extension:
11515
11516    explicit-instantiation:
11517      storage-class-specifier template
11518        decl-specifier-seq [opt] declarator [opt] ;
11519      function-specifier template
11520        decl-specifier-seq [opt] declarator [opt] ;  */
11521
11522 static void
11523 cp_parser_explicit_instantiation (cp_parser* parser)
11524 {
11525   int declares_class_or_enum;
11526   cp_decl_specifier_seq decl_specifiers;
11527   tree extension_specifier = NULL_TREE;
11528
11529   /* Look for an (optional) storage-class-specifier or
11530      function-specifier.  */
11531   if (cp_parser_allow_gnu_extensions_p (parser))
11532     {
11533       extension_specifier
11534         = cp_parser_storage_class_specifier_opt (parser);
11535       if (!extension_specifier)
11536         extension_specifier
11537           = cp_parser_function_specifier_opt (parser,
11538                                               /*decl_specs=*/NULL);
11539     }
11540
11541   /* Look for the `template' keyword.  */
11542   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11543   /* Let the front end know that we are processing an explicit
11544      instantiation.  */
11545   begin_explicit_instantiation ();
11546   /* [temp.explicit] says that we are supposed to ignore access
11547      control while processing explicit instantiation directives.  */
11548   push_deferring_access_checks (dk_no_check);
11549   /* Parse a decl-specifier-seq.  */
11550   cp_parser_decl_specifier_seq (parser,
11551                                 CP_PARSER_FLAGS_OPTIONAL,
11552                                 &decl_specifiers,
11553                                 &declares_class_or_enum);
11554   /* If there was exactly one decl-specifier, and it declared a class,
11555      and there's no declarator, then we have an explicit type
11556      instantiation.  */
11557   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11558     {
11559       tree type;
11560
11561       type = check_tag_decl (&decl_specifiers);
11562       /* Turn access control back on for names used during
11563          template instantiation.  */
11564       pop_deferring_access_checks ();
11565       if (type)
11566         do_type_instantiation (type, extension_specifier,
11567                                /*complain=*/tf_error);
11568     }
11569   else
11570     {
11571       cp_declarator *declarator;
11572       tree decl;
11573
11574       /* Parse the declarator.  */
11575       declarator
11576         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11577                                 /*ctor_dtor_or_conv_p=*/NULL,
11578                                 /*parenthesized_p=*/NULL,
11579                                 /*member_p=*/false);
11580       if (declares_class_or_enum & 2)
11581         cp_parser_check_for_definition_in_return_type (declarator,
11582                                                        decl_specifiers.type,
11583                                                        decl_specifiers.type_location);
11584       if (declarator != cp_error_declarator)
11585         {
11586           decl = grokdeclarator (declarator, &decl_specifiers,
11587                                  NORMAL, 0, &decl_specifiers.attributes);
11588           /* Turn access control back on for names used during
11589              template instantiation.  */
11590           pop_deferring_access_checks ();
11591           /* Do the explicit instantiation.  */
11592           do_decl_instantiation (decl, extension_specifier);
11593         }
11594       else
11595         {
11596           pop_deferring_access_checks ();
11597           /* Skip the body of the explicit instantiation.  */
11598           cp_parser_skip_to_end_of_statement (parser);
11599         }
11600     }
11601   /* We're done with the instantiation.  */
11602   end_explicit_instantiation ();
11603
11604   cp_parser_consume_semicolon_at_end_of_statement (parser);
11605 }
11606
11607 /* Parse an explicit-specialization.
11608
11609    explicit-specialization:
11610      template < > declaration
11611
11612    Although the standard says `declaration', what it really means is:
11613
11614    explicit-specialization:
11615      template <> decl-specifier [opt] init-declarator [opt] ;
11616      template <> function-definition
11617      template <> explicit-specialization
11618      template <> template-declaration  */
11619
11620 static void
11621 cp_parser_explicit_specialization (cp_parser* parser)
11622 {
11623   bool need_lang_pop;
11624   cp_token *token = cp_lexer_peek_token (parser->lexer);
11625
11626   /* Look for the `template' keyword.  */
11627   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11628   /* Look for the `<'.  */
11629   cp_parser_require (parser, CPP_LESS, "%<<%>");
11630   /* Look for the `>'.  */
11631   cp_parser_require (parser, CPP_GREATER, "%<>%>");
11632   /* We have processed another parameter list.  */
11633   ++parser->num_template_parameter_lists;
11634   /* [temp]
11635
11636      A template ... explicit specialization ... shall not have C
11637      linkage.  */
11638   if (current_lang_name == lang_name_c)
11639     {
11640       error_at (token->location, "template specialization with C linkage");
11641       /* Give it C++ linkage to avoid confusing other parts of the
11642          front end.  */
11643       push_lang_context (lang_name_cplusplus);
11644       need_lang_pop = true;
11645     }
11646   else
11647     need_lang_pop = false;
11648   /* Let the front end know that we are beginning a specialization.  */
11649   if (!begin_specialization ())
11650     {
11651       end_specialization ();
11652       return;
11653     }
11654
11655   /* If the next keyword is `template', we need to figure out whether
11656      or not we're looking a template-declaration.  */
11657   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11658     {
11659       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11660           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11661         cp_parser_template_declaration_after_export (parser,
11662                                                      /*member_p=*/false);
11663       else
11664         cp_parser_explicit_specialization (parser);
11665     }
11666   else
11667     /* Parse the dependent declaration.  */
11668     cp_parser_single_declaration (parser,
11669                                   /*checks=*/NULL,
11670                                   /*member_p=*/false,
11671                                   /*explicit_specialization_p=*/true,
11672                                   /*friend_p=*/NULL);
11673   /* We're done with the specialization.  */
11674   end_specialization ();
11675   /* For the erroneous case of a template with C linkage, we pushed an
11676      implicit C++ linkage scope; exit that scope now.  */
11677   if (need_lang_pop)
11678     pop_lang_context ();
11679   /* We're done with this parameter list.  */
11680   --parser->num_template_parameter_lists;
11681 }
11682
11683 /* Parse a type-specifier.
11684
11685    type-specifier:
11686      simple-type-specifier
11687      class-specifier
11688      enum-specifier
11689      elaborated-type-specifier
11690      cv-qualifier
11691
11692    GNU Extension:
11693
11694    type-specifier:
11695      __complex__
11696
11697    Returns a representation of the type-specifier.  For a
11698    class-specifier, enum-specifier, or elaborated-type-specifier, a
11699    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11700
11701    The parser flags FLAGS is used to control type-specifier parsing.
11702
11703    If IS_DECLARATION is TRUE, then this type-specifier is appearing
11704    in a decl-specifier-seq.
11705
11706    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11707    class-specifier, enum-specifier, or elaborated-type-specifier, then
11708    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
11709    if a type is declared; 2 if it is defined.  Otherwise, it is set to
11710    zero.
11711
11712    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11713    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11714    is set to FALSE.  */
11715
11716 static tree
11717 cp_parser_type_specifier (cp_parser* parser,
11718                           cp_parser_flags flags,
11719                           cp_decl_specifier_seq *decl_specs,
11720                           bool is_declaration,
11721                           int* declares_class_or_enum,
11722                           bool* is_cv_qualifier)
11723 {
11724   tree type_spec = NULL_TREE;
11725   cp_token *token;
11726   enum rid keyword;
11727   cp_decl_spec ds = ds_last;
11728
11729   /* Assume this type-specifier does not declare a new type.  */
11730   if (declares_class_or_enum)
11731     *declares_class_or_enum = 0;
11732   /* And that it does not specify a cv-qualifier.  */
11733   if (is_cv_qualifier)
11734     *is_cv_qualifier = false;
11735   /* Peek at the next token.  */
11736   token = cp_lexer_peek_token (parser->lexer);
11737
11738   /* If we're looking at a keyword, we can use that to guide the
11739      production we choose.  */
11740   keyword = token->keyword;
11741   switch (keyword)
11742     {
11743     case RID_ENUM:
11744       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11745         goto elaborated_type_specifier;
11746
11747       /* Look for the enum-specifier.  */
11748       type_spec = cp_parser_enum_specifier (parser);
11749       /* If that worked, we're done.  */
11750       if (type_spec)
11751         {
11752           if (declares_class_or_enum)
11753             *declares_class_or_enum = 2;
11754           if (decl_specs)
11755             cp_parser_set_decl_spec_type (decl_specs,
11756                                           type_spec,
11757                                           token->location,
11758                                           /*user_defined_p=*/true);
11759           return type_spec;
11760         }
11761       else
11762         goto elaborated_type_specifier;
11763
11764       /* Any of these indicate either a class-specifier, or an
11765          elaborated-type-specifier.  */
11766     case RID_CLASS:
11767     case RID_STRUCT:
11768     case RID_UNION:
11769       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11770         goto elaborated_type_specifier;
11771
11772       /* Parse tentatively so that we can back up if we don't find a
11773          class-specifier.  */
11774       cp_parser_parse_tentatively (parser);
11775       /* Look for the class-specifier.  */
11776       type_spec = cp_parser_class_specifier (parser);
11777       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11778       /* If that worked, we're done.  */
11779       if (cp_parser_parse_definitely (parser))
11780         {
11781           if (declares_class_or_enum)
11782             *declares_class_or_enum = 2;
11783           if (decl_specs)
11784             cp_parser_set_decl_spec_type (decl_specs,
11785                                           type_spec,
11786                                           token->location,
11787                                           /*user_defined_p=*/true);
11788           return type_spec;
11789         }
11790
11791       /* Fall through.  */
11792     elaborated_type_specifier:
11793       /* We're declaring (not defining) a class or enum.  */
11794       if (declares_class_or_enum)
11795         *declares_class_or_enum = 1;
11796
11797       /* Fall through.  */
11798     case RID_TYPENAME:
11799       /* Look for an elaborated-type-specifier.  */
11800       type_spec
11801         = (cp_parser_elaborated_type_specifier
11802            (parser,
11803             decl_specs && decl_specs->specs[(int) ds_friend],
11804             is_declaration));
11805       if (decl_specs)
11806         cp_parser_set_decl_spec_type (decl_specs,
11807                                       type_spec,
11808                                       token->location,
11809                                       /*user_defined_p=*/true);
11810       return type_spec;
11811
11812     case RID_CONST:
11813       ds = ds_const;
11814       if (is_cv_qualifier)
11815         *is_cv_qualifier = true;
11816       break;
11817
11818     case RID_VOLATILE:
11819       ds = ds_volatile;
11820       if (is_cv_qualifier)
11821         *is_cv_qualifier = true;
11822       break;
11823
11824     case RID_RESTRICT:
11825       ds = ds_restrict;
11826       if (is_cv_qualifier)
11827         *is_cv_qualifier = true;
11828       break;
11829
11830     case RID_COMPLEX:
11831       /* The `__complex__' keyword is a GNU extension.  */
11832       ds = ds_complex;
11833       break;
11834
11835     default:
11836       break;
11837     }
11838
11839   /* Handle simple keywords.  */
11840   if (ds != ds_last)
11841     {
11842       if (decl_specs)
11843         {
11844           ++decl_specs->specs[(int)ds];
11845           decl_specs->any_specifiers_p = true;
11846         }
11847       return cp_lexer_consume_token (parser->lexer)->u.value;
11848     }
11849
11850   /* If we do not already have a type-specifier, assume we are looking
11851      at a simple-type-specifier.  */
11852   type_spec = cp_parser_simple_type_specifier (parser,
11853                                                decl_specs,
11854                                                flags);
11855
11856   /* If we didn't find a type-specifier, and a type-specifier was not
11857      optional in this context, issue an error message.  */
11858   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11859     {
11860       cp_parser_error (parser, "expected type specifier");
11861       return error_mark_node;
11862     }
11863
11864   return type_spec;
11865 }
11866
11867 /* Parse a simple-type-specifier.
11868
11869    simple-type-specifier:
11870      :: [opt] nested-name-specifier [opt] type-name
11871      :: [opt] nested-name-specifier template template-id
11872      char
11873      wchar_t
11874      bool
11875      short
11876      int
11877      long
11878      signed
11879      unsigned
11880      float
11881      double
11882      void
11883
11884    C++0x Extension:
11885
11886    simple-type-specifier:
11887      auto
11888      decltype ( expression )   
11889      char16_t
11890      char32_t
11891
11892    GNU Extension:
11893
11894    simple-type-specifier:
11895      __typeof__ unary-expression
11896      __typeof__ ( type-id )
11897
11898    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11899    appropriately updated.  */
11900
11901 static tree
11902 cp_parser_simple_type_specifier (cp_parser* parser,
11903                                  cp_decl_specifier_seq *decl_specs,
11904                                  cp_parser_flags flags)
11905 {
11906   tree type = NULL_TREE;
11907   cp_token *token;
11908
11909   /* Peek at the next token.  */
11910   token = cp_lexer_peek_token (parser->lexer);
11911
11912   /* If we're looking at a keyword, things are easy.  */
11913   switch (token->keyword)
11914     {
11915     case RID_CHAR:
11916       if (decl_specs)
11917         decl_specs->explicit_char_p = true;
11918       type = char_type_node;
11919       break;
11920     case RID_CHAR16:
11921       type = char16_type_node;
11922       break;
11923     case RID_CHAR32:
11924       type = char32_type_node;
11925       break;
11926     case RID_WCHAR:
11927       type = wchar_type_node;
11928       break;
11929     case RID_BOOL:
11930       type = boolean_type_node;
11931       break;
11932     case RID_SHORT:
11933       if (decl_specs)
11934         ++decl_specs->specs[(int) ds_short];
11935       type = short_integer_type_node;
11936       break;
11937     case RID_INT:
11938       if (decl_specs)
11939         decl_specs->explicit_int_p = true;
11940       type = integer_type_node;
11941       break;
11942     case RID_LONG:
11943       if (decl_specs)
11944         ++decl_specs->specs[(int) ds_long];
11945       type = long_integer_type_node;
11946       break;
11947     case RID_SIGNED:
11948       if (decl_specs)
11949         ++decl_specs->specs[(int) ds_signed];
11950       type = integer_type_node;
11951       break;
11952     case RID_UNSIGNED:
11953       if (decl_specs)
11954         ++decl_specs->specs[(int) ds_unsigned];
11955       type = unsigned_type_node;
11956       break;
11957     case RID_FLOAT:
11958       type = float_type_node;
11959       break;
11960     case RID_DOUBLE:
11961       type = double_type_node;
11962       break;
11963     case RID_VOID:
11964       type = void_type_node;
11965       break;
11966       
11967     case RID_AUTO:
11968       maybe_warn_cpp0x (CPP0X_AUTO);
11969       type = make_auto ();
11970       break;
11971
11972     case RID_DECLTYPE:
11973       /* Parse the `decltype' type.  */
11974       type = cp_parser_decltype (parser);
11975
11976       if (decl_specs)
11977         cp_parser_set_decl_spec_type (decl_specs, type,
11978                                       token->location,
11979                                       /*user_defined_p=*/true);
11980
11981       return type;
11982
11983     case RID_TYPEOF:
11984       /* Consume the `typeof' token.  */
11985       cp_lexer_consume_token (parser->lexer);
11986       /* Parse the operand to `typeof'.  */
11987       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11988       /* If it is not already a TYPE, take its type.  */
11989       if (!TYPE_P (type))
11990         type = finish_typeof (type);
11991
11992       if (decl_specs)
11993         cp_parser_set_decl_spec_type (decl_specs, type,
11994                                       token->location,
11995                                       /*user_defined_p=*/true);
11996
11997       return type;
11998
11999     default:
12000       break;
12001     }
12002
12003   /* If the type-specifier was for a built-in type, we're done.  */
12004   if (type)
12005     {
12006       /* Record the type.  */
12007       if (decl_specs
12008           && (token->keyword != RID_SIGNED
12009               && token->keyword != RID_UNSIGNED
12010               && token->keyword != RID_SHORT
12011               && token->keyword != RID_LONG))
12012         cp_parser_set_decl_spec_type (decl_specs,
12013                                       type,
12014                                       token->location,
12015                                       /*user_defined=*/false);
12016       if (decl_specs)
12017         decl_specs->any_specifiers_p = true;
12018
12019       /* Consume the token.  */
12020       cp_lexer_consume_token (parser->lexer);
12021
12022       /* There is no valid C++ program where a non-template type is
12023          followed by a "<".  That usually indicates that the user thought
12024          that the type was a template.  */
12025       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12026
12027       return TYPE_NAME (type);
12028     }
12029
12030   /* The type-specifier must be a user-defined type.  */
12031   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12032     {
12033       bool qualified_p;
12034       bool global_p;
12035
12036       /* Don't gobble tokens or issue error messages if this is an
12037          optional type-specifier.  */
12038       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12039         cp_parser_parse_tentatively (parser);
12040
12041       /* Look for the optional `::' operator.  */
12042       global_p
12043         = (cp_parser_global_scope_opt (parser,
12044                                        /*current_scope_valid_p=*/false)
12045            != NULL_TREE);
12046       /* Look for the nested-name specifier.  */
12047       qualified_p
12048         = (cp_parser_nested_name_specifier_opt (parser,
12049                                                 /*typename_keyword_p=*/false,
12050                                                 /*check_dependency_p=*/true,
12051                                                 /*type_p=*/false,
12052                                                 /*is_declaration=*/false)
12053            != NULL_TREE);
12054       token = cp_lexer_peek_token (parser->lexer);
12055       /* If we have seen a nested-name-specifier, and the next token
12056          is `template', then we are using the template-id production.  */
12057       if (parser->scope
12058           && cp_parser_optional_template_keyword (parser))
12059         {
12060           /* Look for the template-id.  */
12061           type = cp_parser_template_id (parser,
12062                                         /*template_keyword_p=*/true,
12063                                         /*check_dependency_p=*/true,
12064                                         /*is_declaration=*/false);
12065           /* If the template-id did not name a type, we are out of
12066              luck.  */
12067           if (TREE_CODE (type) != TYPE_DECL)
12068             {
12069               cp_parser_error (parser, "expected template-id for type");
12070               type = NULL_TREE;
12071             }
12072         }
12073       /* Otherwise, look for a type-name.  */
12074       else
12075         type = cp_parser_type_name (parser);
12076       /* Keep track of all name-lookups performed in class scopes.  */
12077       if (type
12078           && !global_p
12079           && !qualified_p
12080           && TREE_CODE (type) == TYPE_DECL
12081           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12082         maybe_note_name_used_in_class (DECL_NAME (type), type);
12083       /* If it didn't work out, we don't have a TYPE.  */
12084       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12085           && !cp_parser_parse_definitely (parser))
12086         type = NULL_TREE;
12087       if (type && decl_specs)
12088         cp_parser_set_decl_spec_type (decl_specs, type,
12089                                       token->location,
12090                                       /*user_defined=*/true);
12091     }
12092
12093   /* If we didn't get a type-name, issue an error message.  */
12094   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12095     {
12096       cp_parser_error (parser, "expected type-name");
12097       return error_mark_node;
12098     }
12099
12100   /* There is no valid C++ program where a non-template type is
12101      followed by a "<".  That usually indicates that the user thought
12102      that the type was a template.  */
12103   if (type && type != error_mark_node)
12104     {
12105       /* As a last-ditch effort, see if TYPE is an Objective-C type.
12106          If it is, then the '<'...'>' enclose protocol names rather than
12107          template arguments, and so everything is fine.  */
12108       if (c_dialect_objc ()
12109           && (objc_is_id (type) || objc_is_class_name (type)))
12110         {
12111           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12112           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12113
12114           /* Clobber the "unqualified" type previously entered into
12115              DECL_SPECS with the new, improved protocol-qualified version.  */
12116           if (decl_specs)
12117             decl_specs->type = qual_type;
12118
12119           return qual_type;
12120         }
12121
12122       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12123                                                token->location);
12124     }
12125
12126   return type;
12127 }
12128
12129 /* Parse a type-name.
12130
12131    type-name:
12132      class-name
12133      enum-name
12134      typedef-name
12135
12136    enum-name:
12137      identifier
12138
12139    typedef-name:
12140      identifier
12141
12142    Returns a TYPE_DECL for the type.  */
12143
12144 static tree
12145 cp_parser_type_name (cp_parser* parser)
12146 {
12147   tree type_decl;
12148
12149   /* We can't know yet whether it is a class-name or not.  */
12150   cp_parser_parse_tentatively (parser);
12151   /* Try a class-name.  */
12152   type_decl = cp_parser_class_name (parser,
12153                                     /*typename_keyword_p=*/false,
12154                                     /*template_keyword_p=*/false,
12155                                     none_type,
12156                                     /*check_dependency_p=*/true,
12157                                     /*class_head_p=*/false,
12158                                     /*is_declaration=*/false);
12159   /* If it's not a class-name, keep looking.  */
12160   if (!cp_parser_parse_definitely (parser))
12161     {
12162       /* It must be a typedef-name or an enum-name.  */
12163       return cp_parser_nonclass_name (parser);
12164     }
12165
12166   return type_decl;
12167 }
12168
12169 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12170
12171    enum-name:
12172      identifier
12173
12174    typedef-name:
12175      identifier
12176
12177    Returns a TYPE_DECL for the type.  */
12178
12179 static tree
12180 cp_parser_nonclass_name (cp_parser* parser)
12181 {
12182   tree type_decl;
12183   tree identifier;
12184
12185   cp_token *token = cp_lexer_peek_token (parser->lexer);
12186   identifier = cp_parser_identifier (parser);
12187   if (identifier == error_mark_node)
12188     return error_mark_node;
12189
12190   /* Look up the type-name.  */
12191   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12192
12193   if (TREE_CODE (type_decl) != TYPE_DECL
12194       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12195     {
12196       /* See if this is an Objective-C type.  */
12197       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12198       tree type = objc_get_protocol_qualified_type (identifier, protos);
12199       if (type)
12200         type_decl = TYPE_NAME (type);
12201     }
12202   
12203   /* Issue an error if we did not find a type-name.  */
12204   if (TREE_CODE (type_decl) != TYPE_DECL)
12205     {
12206       if (!cp_parser_simulate_error (parser))
12207         cp_parser_name_lookup_error (parser, identifier, type_decl,
12208                                      "is not a type", token->location);
12209       return error_mark_node;
12210     }
12211   /* Remember that the name was used in the definition of the
12212      current class so that we can check later to see if the
12213      meaning would have been different after the class was
12214      entirely defined.  */
12215   else if (type_decl != error_mark_node
12216            && !parser->scope)
12217     maybe_note_name_used_in_class (identifier, type_decl);
12218   
12219   return type_decl;
12220 }
12221
12222 /* Parse an elaborated-type-specifier.  Note that the grammar given
12223    here incorporates the resolution to DR68.
12224
12225    elaborated-type-specifier:
12226      class-key :: [opt] nested-name-specifier [opt] identifier
12227      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12228      enum-key :: [opt] nested-name-specifier [opt] identifier
12229      typename :: [opt] nested-name-specifier identifier
12230      typename :: [opt] nested-name-specifier template [opt]
12231        template-id
12232
12233    GNU extension:
12234
12235    elaborated-type-specifier:
12236      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12237      class-key attributes :: [opt] nested-name-specifier [opt]
12238                template [opt] template-id
12239      enum attributes :: [opt] nested-name-specifier [opt] identifier
12240
12241    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12242    declared `friend'.  If IS_DECLARATION is TRUE, then this
12243    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12244    something is being declared.
12245
12246    Returns the TYPE specified.  */
12247
12248 static tree
12249 cp_parser_elaborated_type_specifier (cp_parser* parser,
12250                                      bool is_friend,
12251                                      bool is_declaration)
12252 {
12253   enum tag_types tag_type;
12254   tree identifier;
12255   tree type = NULL_TREE;
12256   tree attributes = NULL_TREE;
12257   tree globalscope;
12258   cp_token *token = NULL;
12259
12260   /* See if we're looking at the `enum' keyword.  */
12261   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12262     {
12263       /* Consume the `enum' token.  */
12264       cp_lexer_consume_token (parser->lexer);
12265       /* Remember that it's an enumeration type.  */
12266       tag_type = enum_type;
12267       /* Parse the optional `struct' or `class' key (for C++0x scoped
12268          enums).  */
12269       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12270           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12271         {
12272           if (cxx_dialect == cxx98)
12273             maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12274
12275           /* Consume the `struct' or `class'.  */
12276           cp_lexer_consume_token (parser->lexer);
12277         }
12278       /* Parse the attributes.  */
12279       attributes = cp_parser_attributes_opt (parser);
12280     }
12281   /* Or, it might be `typename'.  */
12282   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12283                                            RID_TYPENAME))
12284     {
12285       /* Consume the `typename' token.  */
12286       cp_lexer_consume_token (parser->lexer);
12287       /* Remember that it's a `typename' type.  */
12288       tag_type = typename_type;
12289     }
12290   /* Otherwise it must be a class-key.  */
12291   else
12292     {
12293       tag_type = cp_parser_class_key (parser);
12294       if (tag_type == none_type)
12295         return error_mark_node;
12296       /* Parse the attributes.  */
12297       attributes = cp_parser_attributes_opt (parser);
12298     }
12299
12300   /* Look for the `::' operator.  */
12301   globalscope =  cp_parser_global_scope_opt (parser,
12302                                              /*current_scope_valid_p=*/false);
12303   /* Look for the nested-name-specifier.  */
12304   if (tag_type == typename_type && !globalscope)
12305     {
12306       if (!cp_parser_nested_name_specifier (parser,
12307                                            /*typename_keyword_p=*/true,
12308                                            /*check_dependency_p=*/true,
12309                                            /*type_p=*/true,
12310                                             is_declaration))
12311         return error_mark_node;
12312     }
12313   else
12314     /* Even though `typename' is not present, the proposed resolution
12315        to Core Issue 180 says that in `class A<T>::B', `B' should be
12316        considered a type-name, even if `A<T>' is dependent.  */
12317     cp_parser_nested_name_specifier_opt (parser,
12318                                          /*typename_keyword_p=*/true,
12319                                          /*check_dependency_p=*/true,
12320                                          /*type_p=*/true,
12321                                          is_declaration);
12322  /* For everything but enumeration types, consider a template-id.
12323     For an enumeration type, consider only a plain identifier.  */
12324   if (tag_type != enum_type)
12325     {
12326       bool template_p = false;
12327       tree decl;
12328
12329       /* Allow the `template' keyword.  */
12330       template_p = cp_parser_optional_template_keyword (parser);
12331       /* If we didn't see `template', we don't know if there's a
12332          template-id or not.  */
12333       if (!template_p)
12334         cp_parser_parse_tentatively (parser);
12335       /* Parse the template-id.  */
12336       token = cp_lexer_peek_token (parser->lexer);
12337       decl = cp_parser_template_id (parser, template_p,
12338                                     /*check_dependency_p=*/true,
12339                                     is_declaration);
12340       /* If we didn't find a template-id, look for an ordinary
12341          identifier.  */
12342       if (!template_p && !cp_parser_parse_definitely (parser))
12343         ;
12344       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12345          in effect, then we must assume that, upon instantiation, the
12346          template will correspond to a class.  */
12347       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12348                && tag_type == typename_type)
12349         type = make_typename_type (parser->scope, decl,
12350                                    typename_type,
12351                                    /*complain=*/tf_error);
12352       /* If the `typename' keyword is in effect and DECL is not a type
12353          decl. Then type is non existant.   */
12354       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12355         type = NULL_TREE; 
12356       else 
12357         type = TREE_TYPE (decl);
12358     }
12359
12360   if (!type)
12361     {
12362       token = cp_lexer_peek_token (parser->lexer);
12363       identifier = cp_parser_identifier (parser);
12364
12365       if (identifier == error_mark_node)
12366         {
12367           parser->scope = NULL_TREE;
12368           return error_mark_node;
12369         }
12370
12371       /* For a `typename', we needn't call xref_tag.  */
12372       if (tag_type == typename_type
12373           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12374         return cp_parser_make_typename_type (parser, parser->scope,
12375                                              identifier,
12376                                              token->location);
12377       /* Look up a qualified name in the usual way.  */
12378       if (parser->scope)
12379         {
12380           tree decl;
12381           tree ambiguous_decls;
12382
12383           decl = cp_parser_lookup_name (parser, identifier,
12384                                         tag_type,
12385                                         /*is_template=*/false,
12386                                         /*is_namespace=*/false,
12387                                         /*check_dependency=*/true,
12388                                         &ambiguous_decls,
12389                                         token->location);
12390
12391           /* If the lookup was ambiguous, an error will already have been
12392              issued.  */
12393           if (ambiguous_decls)
12394             return error_mark_node;
12395
12396           /* If we are parsing friend declaration, DECL may be a
12397              TEMPLATE_DECL tree node here.  However, we need to check
12398              whether this TEMPLATE_DECL results in valid code.  Consider
12399              the following example:
12400
12401                namespace N {
12402                  template <class T> class C {};
12403                }
12404                class X {
12405                  template <class T> friend class N::C; // #1, valid code
12406                };
12407                template <class T> class Y {
12408                  friend class N::C;                    // #2, invalid code
12409                };
12410
12411              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12412              name lookup of `N::C'.  We see that friend declaration must
12413              be template for the code to be valid.  Note that
12414              processing_template_decl does not work here since it is
12415              always 1 for the above two cases.  */
12416
12417           decl = (cp_parser_maybe_treat_template_as_class
12418                   (decl, /*tag_name_p=*/is_friend
12419                          && parser->num_template_parameter_lists));
12420
12421           if (TREE_CODE (decl) != TYPE_DECL)
12422             {
12423               cp_parser_diagnose_invalid_type_name (parser,
12424                                                     parser->scope,
12425                                                     identifier,
12426                                                     token->location);
12427               return error_mark_node;
12428             }
12429
12430           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12431             {
12432               bool allow_template = (parser->num_template_parameter_lists
12433                                       || DECL_SELF_REFERENCE_P (decl));
12434               type = check_elaborated_type_specifier (tag_type, decl, 
12435                                                       allow_template);
12436
12437               if (type == error_mark_node)
12438                 return error_mark_node;
12439             }
12440
12441           /* Forward declarations of nested types, such as
12442
12443                class C1::C2;
12444                class C1::C2::C3;
12445
12446              are invalid unless all components preceding the final '::'
12447              are complete.  If all enclosing types are complete, these
12448              declarations become merely pointless.
12449
12450              Invalid forward declarations of nested types are errors
12451              caught elsewhere in parsing.  Those that are pointless arrive
12452              here.  */
12453
12454           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12455               && !is_friend && !processing_explicit_instantiation)
12456             warning (0, "declaration %qD does not declare anything", decl);
12457
12458           type = TREE_TYPE (decl);
12459         }
12460       else
12461         {
12462           /* An elaborated-type-specifier sometimes introduces a new type and
12463              sometimes names an existing type.  Normally, the rule is that it
12464              introduces a new type only if there is not an existing type of
12465              the same name already in scope.  For example, given:
12466
12467                struct S {};
12468                void f() { struct S s; }
12469
12470              the `struct S' in the body of `f' is the same `struct S' as in
12471              the global scope; the existing definition is used.  However, if
12472              there were no global declaration, this would introduce a new
12473              local class named `S'.
12474
12475              An exception to this rule applies to the following code:
12476
12477                namespace N { struct S; }
12478
12479              Here, the elaborated-type-specifier names a new type
12480              unconditionally; even if there is already an `S' in the
12481              containing scope this declaration names a new type.
12482              This exception only applies if the elaborated-type-specifier
12483              forms the complete declaration:
12484
12485                [class.name]
12486
12487                A declaration consisting solely of `class-key identifier ;' is
12488                either a redeclaration of the name in the current scope or a
12489                forward declaration of the identifier as a class name.  It
12490                introduces the name into the current scope.
12491
12492              We are in this situation precisely when the next token is a `;'.
12493
12494              An exception to the exception is that a `friend' declaration does
12495              *not* name a new type; i.e., given:
12496
12497                struct S { friend struct T; };
12498
12499              `T' is not a new type in the scope of `S'.
12500
12501              Also, `new struct S' or `sizeof (struct S)' never results in the
12502              definition of a new type; a new type can only be declared in a
12503              declaration context.  */
12504
12505           tag_scope ts;
12506           bool template_p;
12507
12508           if (is_friend)
12509             /* Friends have special name lookup rules.  */
12510             ts = ts_within_enclosing_non_class;
12511           else if (is_declaration
12512                    && cp_lexer_next_token_is (parser->lexer,
12513                                               CPP_SEMICOLON))
12514             /* This is a `class-key identifier ;' */
12515             ts = ts_current;
12516           else
12517             ts = ts_global;
12518
12519           template_p =
12520             (parser->num_template_parameter_lists
12521              && (cp_parser_next_token_starts_class_definition_p (parser)
12522                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12523           /* An unqualified name was used to reference this type, so
12524              there were no qualifying templates.  */
12525           if (!cp_parser_check_template_parameters (parser,
12526                                                     /*num_templates=*/0,
12527                                                     token->location,
12528                                                     /*declarator=*/NULL))
12529             return error_mark_node;
12530           type = xref_tag (tag_type, identifier, ts, template_p);
12531         }
12532     }
12533
12534   if (type == error_mark_node)
12535     return error_mark_node;
12536
12537   /* Allow attributes on forward declarations of classes.  */
12538   if (attributes)
12539     {
12540       if (TREE_CODE (type) == TYPENAME_TYPE)
12541         warning (OPT_Wattributes,
12542                  "attributes ignored on uninstantiated type");
12543       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12544                && ! processing_explicit_instantiation)
12545         warning (OPT_Wattributes,
12546                  "attributes ignored on template instantiation");
12547       else if (is_declaration && cp_parser_declares_only_class_p (parser))
12548         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12549       else
12550         warning (OPT_Wattributes,
12551                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12552     }
12553
12554   if (tag_type != enum_type)
12555     cp_parser_check_class_key (tag_type, type);
12556
12557   /* A "<" cannot follow an elaborated type specifier.  If that
12558      happens, the user was probably trying to form a template-id.  */
12559   cp_parser_check_for_invalid_template_id (parser, type, token->location);
12560
12561   return type;
12562 }
12563
12564 /* Parse an enum-specifier.
12565
12566    enum-specifier:
12567      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12568
12569    enum-key:
12570      enum
12571      enum class   [C++0x]
12572      enum struct  [C++0x]
12573
12574    enum-base:   [C++0x]
12575      : type-specifier-seq
12576
12577    GNU Extensions:
12578      enum-key attributes[opt] identifier [opt] enum-base [opt] 
12579        { enumerator-list [opt] }attributes[opt]
12580
12581    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12582    if the token stream isn't an enum-specifier after all.  */
12583
12584 static tree
12585 cp_parser_enum_specifier (cp_parser* parser)
12586 {
12587   tree identifier;
12588   tree type;
12589   tree attributes;
12590   bool scoped_enum_p = false;
12591   bool has_underlying_type = false;
12592   tree underlying_type = NULL_TREE;
12593
12594   /* Parse tentatively so that we can back up if we don't find a
12595      enum-specifier.  */
12596   cp_parser_parse_tentatively (parser);
12597
12598   /* Caller guarantees that the current token is 'enum', an identifier
12599      possibly follows, and the token after that is an opening brace.
12600      If we don't have an identifier, fabricate an anonymous name for
12601      the enumeration being defined.  */
12602   cp_lexer_consume_token (parser->lexer);
12603
12604   /* Parse the "class" or "struct", which indicates a scoped
12605      enumeration type in C++0x.  */
12606   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12607       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12608     {
12609       if (cxx_dialect == cxx98)
12610         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12611
12612       /* Consume the `struct' or `class' token.  */
12613       cp_lexer_consume_token (parser->lexer);
12614
12615       scoped_enum_p = true;
12616     }
12617
12618   attributes = cp_parser_attributes_opt (parser);
12619
12620   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12621     identifier = cp_parser_identifier (parser);
12622   else
12623     identifier = make_anon_name ();
12624
12625   /* Check for the `:' that denotes a specified underlying type in C++0x.
12626      Note that a ':' could also indicate a bitfield width, however.  */
12627   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12628     {
12629       cp_decl_specifier_seq type_specifiers;
12630
12631       /* Consume the `:'.  */
12632       cp_lexer_consume_token (parser->lexer);
12633
12634       /* Parse the type-specifier-seq.  */
12635       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12636                                     /*is_trailing_return=*/false,
12637                                     &type_specifiers);
12638
12639       /* At this point this is surely not elaborated type specifier.  */
12640       if (!cp_parser_parse_definitely (parser))
12641         return NULL_TREE;
12642
12643       if (cxx_dialect == cxx98)
12644         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12645
12646       has_underlying_type = true;
12647
12648       /* If that didn't work, stop.  */
12649       if (type_specifiers.type != error_mark_node)
12650         {
12651           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12652                                             /*initialized=*/0, NULL);
12653           if (underlying_type == error_mark_node)
12654             underlying_type = NULL_TREE;
12655         }
12656     }
12657
12658   /* Look for the `{' but don't consume it yet.  */
12659   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12660     {
12661       cp_parser_error (parser, "expected %<{%>");
12662       if (has_underlying_type)
12663         return NULL_TREE;
12664     }
12665
12666   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12667     return NULL_TREE;
12668
12669   /* Issue an error message if type-definitions are forbidden here.  */
12670   if (!cp_parser_check_type_definition (parser))
12671     type = error_mark_node;
12672   else
12673     /* Create the new type.  We do this before consuming the opening
12674        brace so the enum will be recorded as being on the line of its
12675        tag (or the 'enum' keyword, if there is no tag).  */
12676     type = start_enum (identifier, underlying_type, scoped_enum_p);
12677   
12678   /* Consume the opening brace.  */
12679   cp_lexer_consume_token (parser->lexer);
12680
12681   if (type == error_mark_node)
12682     {
12683       cp_parser_skip_to_end_of_block_or_statement (parser);
12684       return error_mark_node;
12685     }
12686
12687   /* If the next token is not '}', then there are some enumerators.  */
12688   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12689     cp_parser_enumerator_list (parser, type);
12690
12691   /* Consume the final '}'.  */
12692   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12693
12694   /* Look for trailing attributes to apply to this enumeration, and
12695      apply them if appropriate.  */
12696   if (cp_parser_allow_gnu_extensions_p (parser))
12697     {
12698       tree trailing_attr = cp_parser_attributes_opt (parser);
12699       trailing_attr = chainon (trailing_attr, attributes);
12700       cplus_decl_attributes (&type,
12701                              trailing_attr,
12702                              (int) ATTR_FLAG_TYPE_IN_PLACE);
12703     }
12704
12705   /* Finish up the enumeration.  */
12706   finish_enum (type);
12707
12708   return type;
12709 }
12710
12711 /* Parse an enumerator-list.  The enumerators all have the indicated
12712    TYPE.
12713
12714    enumerator-list:
12715      enumerator-definition
12716      enumerator-list , enumerator-definition  */
12717
12718 static void
12719 cp_parser_enumerator_list (cp_parser* parser, tree type)
12720 {
12721   while (true)
12722     {
12723       /* Parse an enumerator-definition.  */
12724       cp_parser_enumerator_definition (parser, type);
12725
12726       /* If the next token is not a ',', we've reached the end of
12727          the list.  */
12728       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12729         break;
12730       /* Otherwise, consume the `,' and keep going.  */
12731       cp_lexer_consume_token (parser->lexer);
12732       /* If the next token is a `}', there is a trailing comma.  */
12733       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12734         {
12735           if (!in_system_header)
12736             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12737           break;
12738         }
12739     }
12740 }
12741
12742 /* Parse an enumerator-definition.  The enumerator has the indicated
12743    TYPE.
12744
12745    enumerator-definition:
12746      enumerator
12747      enumerator = constant-expression
12748
12749    enumerator:
12750      identifier  */
12751
12752 static void
12753 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12754 {
12755   tree identifier;
12756   tree value;
12757
12758   /* Look for the identifier.  */
12759   identifier = cp_parser_identifier (parser);
12760   if (identifier == error_mark_node)
12761     return;
12762
12763   /* If the next token is an '=', then there is an explicit value.  */
12764   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12765     {
12766       /* Consume the `=' token.  */
12767       cp_lexer_consume_token (parser->lexer);
12768       /* Parse the value.  */
12769       value = cp_parser_constant_expression (parser,
12770                                              /*allow_non_constant_p=*/false,
12771                                              NULL);
12772     }
12773   else
12774     value = NULL_TREE;
12775
12776   /* If we are processing a template, make sure the initializer of the
12777      enumerator doesn't contain any bare template parameter pack.  */
12778   if (check_for_bare_parameter_packs (value))
12779     value = error_mark_node;
12780
12781   /* Create the enumerator.  */
12782   build_enumerator (identifier, value, type);
12783 }
12784
12785 /* Parse a namespace-name.
12786
12787    namespace-name:
12788      original-namespace-name
12789      namespace-alias
12790
12791    Returns the NAMESPACE_DECL for the namespace.  */
12792
12793 static tree
12794 cp_parser_namespace_name (cp_parser* parser)
12795 {
12796   tree identifier;
12797   tree namespace_decl;
12798
12799   cp_token *token = cp_lexer_peek_token (parser->lexer);
12800
12801   /* Get the name of the namespace.  */
12802   identifier = cp_parser_identifier (parser);
12803   if (identifier == error_mark_node)
12804     return error_mark_node;
12805
12806   /* Look up the identifier in the currently active scope.  Look only
12807      for namespaces, due to:
12808
12809        [basic.lookup.udir]
12810
12811        When looking up a namespace-name in a using-directive or alias
12812        definition, only namespace names are considered.
12813
12814      And:
12815
12816        [basic.lookup.qual]
12817
12818        During the lookup of a name preceding the :: scope resolution
12819        operator, object, function, and enumerator names are ignored.
12820
12821      (Note that cp_parser_qualifying_entity only calls this
12822      function if the token after the name is the scope resolution
12823      operator.)  */
12824   namespace_decl = cp_parser_lookup_name (parser, identifier,
12825                                           none_type,
12826                                           /*is_template=*/false,
12827                                           /*is_namespace=*/true,
12828                                           /*check_dependency=*/true,
12829                                           /*ambiguous_decls=*/NULL,
12830                                           token->location);
12831   /* If it's not a namespace, issue an error.  */
12832   if (namespace_decl == error_mark_node
12833       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12834     {
12835       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12836         error_at (token->location, "%qD is not a namespace-name", identifier);
12837       cp_parser_error (parser, "expected namespace-name");
12838       namespace_decl = error_mark_node;
12839     }
12840
12841   return namespace_decl;
12842 }
12843
12844 /* Parse a namespace-definition.
12845
12846    namespace-definition:
12847      named-namespace-definition
12848      unnamed-namespace-definition
12849
12850    named-namespace-definition:
12851      original-namespace-definition
12852      extension-namespace-definition
12853
12854    original-namespace-definition:
12855      namespace identifier { namespace-body }
12856
12857    extension-namespace-definition:
12858      namespace original-namespace-name { namespace-body }
12859
12860    unnamed-namespace-definition:
12861      namespace { namespace-body } */
12862
12863 static void
12864 cp_parser_namespace_definition (cp_parser* parser)
12865 {
12866   tree identifier, attribs;
12867   bool has_visibility;
12868   bool is_inline;
12869
12870   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12871     {
12872       is_inline = true;
12873       cp_lexer_consume_token (parser->lexer);
12874     }
12875   else
12876     is_inline = false;
12877
12878   /* Look for the `namespace' keyword.  */
12879   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12880
12881   /* Get the name of the namespace.  We do not attempt to distinguish
12882      between an original-namespace-definition and an
12883      extension-namespace-definition at this point.  The semantic
12884      analysis routines are responsible for that.  */
12885   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12886     identifier = cp_parser_identifier (parser);
12887   else
12888     identifier = NULL_TREE;
12889
12890   /* Parse any specified attributes.  */
12891   attribs = cp_parser_attributes_opt (parser);
12892
12893   /* Look for the `{' to start the namespace.  */
12894   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12895   /* Start the namespace.  */
12896   push_namespace (identifier);
12897
12898   /* "inline namespace" is equivalent to a stub namespace definition
12899      followed by a strong using directive.  */
12900   if (is_inline)
12901     {
12902       tree name_space = current_namespace;
12903       /* Set up namespace association.  */
12904       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12905         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12906                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12907       /* Import the contents of the inline namespace.  */
12908       pop_namespace ();
12909       do_using_directive (name_space);
12910       push_namespace (identifier);
12911     }
12912
12913   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12914
12915   /* Parse the body of the namespace.  */
12916   cp_parser_namespace_body (parser);
12917
12918 #ifdef HANDLE_PRAGMA_VISIBILITY
12919   if (has_visibility)
12920     pop_visibility (1);
12921 #endif
12922
12923   /* Finish the namespace.  */
12924   pop_namespace ();
12925   /* Look for the final `}'.  */
12926   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12927 }
12928
12929 /* Parse a namespace-body.
12930
12931    namespace-body:
12932      declaration-seq [opt]  */
12933
12934 static void
12935 cp_parser_namespace_body (cp_parser* parser)
12936 {
12937   cp_parser_declaration_seq_opt (parser);
12938 }
12939
12940 /* Parse a namespace-alias-definition.
12941
12942    namespace-alias-definition:
12943      namespace identifier = qualified-namespace-specifier ;  */
12944
12945 static void
12946 cp_parser_namespace_alias_definition (cp_parser* parser)
12947 {
12948   tree identifier;
12949   tree namespace_specifier;
12950
12951   cp_token *token = cp_lexer_peek_token (parser->lexer);
12952
12953   /* Look for the `namespace' keyword.  */
12954   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12955   /* Look for the identifier.  */
12956   identifier = cp_parser_identifier (parser);
12957   if (identifier == error_mark_node)
12958     return;
12959   /* Look for the `=' token.  */
12960   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12961       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12962     {
12963       error_at (token->location, "%<namespace%> definition is not allowed here");
12964       /* Skip the definition.  */
12965       cp_lexer_consume_token (parser->lexer);
12966       if (cp_parser_skip_to_closing_brace (parser))
12967         cp_lexer_consume_token (parser->lexer);
12968       return;
12969     }
12970   cp_parser_require (parser, CPP_EQ, "%<=%>");
12971   /* Look for the qualified-namespace-specifier.  */
12972   namespace_specifier
12973     = cp_parser_qualified_namespace_specifier (parser);
12974   /* Look for the `;' token.  */
12975   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12976
12977   /* Register the alias in the symbol table.  */
12978   do_namespace_alias (identifier, namespace_specifier);
12979 }
12980
12981 /* Parse a qualified-namespace-specifier.
12982
12983    qualified-namespace-specifier:
12984      :: [opt] nested-name-specifier [opt] namespace-name
12985
12986    Returns a NAMESPACE_DECL corresponding to the specified
12987    namespace.  */
12988
12989 static tree
12990 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12991 {
12992   /* Look for the optional `::'.  */
12993   cp_parser_global_scope_opt (parser,
12994                               /*current_scope_valid_p=*/false);
12995
12996   /* Look for the optional nested-name-specifier.  */
12997   cp_parser_nested_name_specifier_opt (parser,
12998                                        /*typename_keyword_p=*/false,
12999                                        /*check_dependency_p=*/true,
13000                                        /*type_p=*/false,
13001                                        /*is_declaration=*/true);
13002
13003   return cp_parser_namespace_name (parser);
13004 }
13005
13006 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13007    access declaration.
13008
13009    using-declaration:
13010      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13011      using :: unqualified-id ;  
13012
13013    access-declaration:
13014      qualified-id ;  
13015
13016    */
13017
13018 static bool
13019 cp_parser_using_declaration (cp_parser* parser, 
13020                              bool access_declaration_p)
13021 {
13022   cp_token *token;
13023   bool typename_p = false;
13024   bool global_scope_p;
13025   tree decl;
13026   tree identifier;
13027   tree qscope;
13028
13029   if (access_declaration_p)
13030     cp_parser_parse_tentatively (parser);
13031   else
13032     {
13033       /* Look for the `using' keyword.  */
13034       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13035       
13036       /* Peek at the next token.  */
13037       token = cp_lexer_peek_token (parser->lexer);
13038       /* See if it's `typename'.  */
13039       if (token->keyword == RID_TYPENAME)
13040         {
13041           /* Remember that we've seen it.  */
13042           typename_p = true;
13043           /* Consume the `typename' token.  */
13044           cp_lexer_consume_token (parser->lexer);
13045         }
13046     }
13047
13048   /* Look for the optional global scope qualification.  */
13049   global_scope_p
13050     = (cp_parser_global_scope_opt (parser,
13051                                    /*current_scope_valid_p=*/false)
13052        != NULL_TREE);
13053
13054   /* If we saw `typename', or didn't see `::', then there must be a
13055      nested-name-specifier present.  */
13056   if (typename_p || !global_scope_p)
13057     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13058                                               /*check_dependency_p=*/true,
13059                                               /*type_p=*/false,
13060                                               /*is_declaration=*/true);
13061   /* Otherwise, we could be in either of the two productions.  In that
13062      case, treat the nested-name-specifier as optional.  */
13063   else
13064     qscope = cp_parser_nested_name_specifier_opt (parser,
13065                                                   /*typename_keyword_p=*/false,
13066                                                   /*check_dependency_p=*/true,
13067                                                   /*type_p=*/false,
13068                                                   /*is_declaration=*/true);
13069   if (!qscope)
13070     qscope = global_namespace;
13071
13072   if (access_declaration_p && cp_parser_error_occurred (parser))
13073     /* Something has already gone wrong; there's no need to parse
13074        further.  Since an error has occurred, the return value of
13075        cp_parser_parse_definitely will be false, as required.  */
13076     return cp_parser_parse_definitely (parser);
13077
13078   token = cp_lexer_peek_token (parser->lexer);
13079   /* Parse the unqualified-id.  */
13080   identifier = cp_parser_unqualified_id (parser,
13081                                          /*template_keyword_p=*/false,
13082                                          /*check_dependency_p=*/true,
13083                                          /*declarator_p=*/true,
13084                                          /*optional_p=*/false);
13085
13086   if (access_declaration_p)
13087     {
13088       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13089         cp_parser_simulate_error (parser);
13090       if (!cp_parser_parse_definitely (parser))
13091         return false;
13092     }
13093
13094   /* The function we call to handle a using-declaration is different
13095      depending on what scope we are in.  */
13096   if (qscope == error_mark_node || identifier == error_mark_node)
13097     ;
13098   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13099            && TREE_CODE (identifier) != BIT_NOT_EXPR)
13100     /* [namespace.udecl]
13101
13102        A using declaration shall not name a template-id.  */
13103     error_at (token->location,
13104               "a template-id may not appear in a using-declaration");
13105   else
13106     {
13107       if (at_class_scope_p ())
13108         {
13109           /* Create the USING_DECL.  */
13110           decl = do_class_using_decl (parser->scope, identifier);
13111
13112           if (check_for_bare_parameter_packs (decl))
13113             return false;
13114           else
13115             /* Add it to the list of members in this class.  */
13116             finish_member_declaration (decl);
13117         }
13118       else
13119         {
13120           decl = cp_parser_lookup_name_simple (parser,
13121                                                identifier,
13122                                                token->location);
13123           if (decl == error_mark_node)
13124             cp_parser_name_lookup_error (parser, identifier,
13125                                          decl, NULL,
13126                                          token->location);
13127           else if (check_for_bare_parameter_packs (decl))
13128             return false;
13129           else if (!at_namespace_scope_p ())
13130             do_local_using_decl (decl, qscope, identifier);
13131           else
13132             do_toplevel_using_decl (decl, qscope, identifier);
13133         }
13134     }
13135
13136   /* Look for the final `;'.  */
13137   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13138   
13139   return true;
13140 }
13141
13142 /* Parse a using-directive.
13143
13144    using-directive:
13145      using namespace :: [opt] nested-name-specifier [opt]
13146        namespace-name ;  */
13147
13148 static void
13149 cp_parser_using_directive (cp_parser* parser)
13150 {
13151   tree namespace_decl;
13152   tree attribs;
13153
13154   /* Look for the `using' keyword.  */
13155   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13156   /* And the `namespace' keyword.  */
13157   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13158   /* Look for the optional `::' operator.  */
13159   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13160   /* And the optional nested-name-specifier.  */
13161   cp_parser_nested_name_specifier_opt (parser,
13162                                        /*typename_keyword_p=*/false,
13163                                        /*check_dependency_p=*/true,
13164                                        /*type_p=*/false,
13165                                        /*is_declaration=*/true);
13166   /* Get the namespace being used.  */
13167   namespace_decl = cp_parser_namespace_name (parser);
13168   /* And any specified attributes.  */
13169   attribs = cp_parser_attributes_opt (parser);
13170   /* Update the symbol table.  */
13171   parse_using_directive (namespace_decl, attribs);
13172   /* Look for the final `;'.  */
13173   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13174 }
13175
13176 /* Parse an asm-definition.
13177
13178    asm-definition:
13179      asm ( string-literal ) ;
13180
13181    GNU Extension:
13182
13183    asm-definition:
13184      asm volatile [opt] ( string-literal ) ;
13185      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13186      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13187                           : asm-operand-list [opt] ) ;
13188      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13189                           : asm-operand-list [opt]
13190                           : asm-clobber-list [opt] ) ;
13191      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13192                                : asm-clobber-list [opt]
13193                                : asm-goto-list ) ;  */
13194
13195 static void
13196 cp_parser_asm_definition (cp_parser* parser)
13197 {
13198   tree string;
13199   tree outputs = NULL_TREE;
13200   tree inputs = NULL_TREE;
13201   tree clobbers = NULL_TREE;
13202   tree labels = NULL_TREE;
13203   tree asm_stmt;
13204   bool volatile_p = false;
13205   bool extended_p = false;
13206   bool invalid_inputs_p = false;
13207   bool invalid_outputs_p = false;
13208   bool goto_p = false;
13209   const char *missing = NULL;
13210
13211   /* Look for the `asm' keyword.  */
13212   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13213   /* See if the next token is `volatile'.  */
13214   if (cp_parser_allow_gnu_extensions_p (parser)
13215       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13216     {
13217       /* Remember that we saw the `volatile' keyword.  */
13218       volatile_p = true;
13219       /* Consume the token.  */
13220       cp_lexer_consume_token (parser->lexer);
13221     }
13222   if (cp_parser_allow_gnu_extensions_p (parser)
13223       && parser->in_function_body
13224       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13225     {
13226       /* Remember that we saw the `goto' keyword.  */
13227       goto_p = true;
13228       /* Consume the token.  */
13229       cp_lexer_consume_token (parser->lexer);
13230     }
13231   /* Look for the opening `('.  */
13232   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13233     return;
13234   /* Look for the string.  */
13235   string = cp_parser_string_literal (parser, false, false);
13236   if (string == error_mark_node)
13237     {
13238       cp_parser_skip_to_closing_parenthesis (parser, true, false,
13239                                              /*consume_paren=*/true);
13240       return;
13241     }
13242
13243   /* If we're allowing GNU extensions, check for the extended assembly
13244      syntax.  Unfortunately, the `:' tokens need not be separated by
13245      a space in C, and so, for compatibility, we tolerate that here
13246      too.  Doing that means that we have to treat the `::' operator as
13247      two `:' tokens.  */
13248   if (cp_parser_allow_gnu_extensions_p (parser)
13249       && parser->in_function_body
13250       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13251           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13252     {
13253       bool inputs_p = false;
13254       bool clobbers_p = false;
13255       bool labels_p = false;
13256
13257       /* The extended syntax was used.  */
13258       extended_p = true;
13259
13260       /* Look for outputs.  */
13261       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13262         {
13263           /* Consume the `:'.  */
13264           cp_lexer_consume_token (parser->lexer);
13265           /* Parse the output-operands.  */
13266           if (cp_lexer_next_token_is_not (parser->lexer,
13267                                           CPP_COLON)
13268               && cp_lexer_next_token_is_not (parser->lexer,
13269                                              CPP_SCOPE)
13270               && cp_lexer_next_token_is_not (parser->lexer,
13271                                              CPP_CLOSE_PAREN)
13272               && !goto_p)
13273             outputs = cp_parser_asm_operand_list (parser);
13274
13275             if (outputs == error_mark_node)
13276               invalid_outputs_p = true;
13277         }
13278       /* If the next token is `::', there are no outputs, and the
13279          next token is the beginning of the inputs.  */
13280       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13281         /* The inputs are coming next.  */
13282         inputs_p = true;
13283
13284       /* Look for inputs.  */
13285       if (inputs_p
13286           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13287         {
13288           /* Consume the `:' or `::'.  */
13289           cp_lexer_consume_token (parser->lexer);
13290           /* Parse the output-operands.  */
13291           if (cp_lexer_next_token_is_not (parser->lexer,
13292                                           CPP_COLON)
13293               && cp_lexer_next_token_is_not (parser->lexer,
13294                                              CPP_SCOPE)
13295               && cp_lexer_next_token_is_not (parser->lexer,
13296                                              CPP_CLOSE_PAREN))
13297             inputs = cp_parser_asm_operand_list (parser);
13298
13299             if (inputs == error_mark_node)
13300               invalid_inputs_p = true;
13301         }
13302       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13303         /* The clobbers are coming next.  */
13304         clobbers_p = true;
13305
13306       /* Look for clobbers.  */
13307       if (clobbers_p
13308           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13309         {
13310           clobbers_p = true;
13311           /* Consume the `:' or `::'.  */
13312           cp_lexer_consume_token (parser->lexer);
13313           /* Parse the clobbers.  */
13314           if (cp_lexer_next_token_is_not (parser->lexer,
13315                                           CPP_COLON)
13316               && cp_lexer_next_token_is_not (parser->lexer,
13317                                              CPP_CLOSE_PAREN))
13318             clobbers = cp_parser_asm_clobber_list (parser);
13319         }
13320       else if (goto_p
13321                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13322         /* The labels are coming next.  */
13323         labels_p = true;
13324
13325       /* Look for labels.  */
13326       if (labels_p
13327           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13328         {
13329           labels_p = true;
13330           /* Consume the `:' or `::'.  */
13331           cp_lexer_consume_token (parser->lexer);
13332           /* Parse the labels.  */
13333           labels = cp_parser_asm_label_list (parser);
13334         }
13335
13336       if (goto_p && !labels_p)
13337         missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13338     }
13339   else if (goto_p)
13340     missing = "%<:%> or %<::%>";
13341
13342   /* Look for the closing `)'.  */
13343   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13344                           missing ? missing : "%<)%>"))
13345     cp_parser_skip_to_closing_parenthesis (parser, true, false,
13346                                            /*consume_paren=*/true);
13347   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13348
13349   if (!invalid_inputs_p && !invalid_outputs_p)
13350     {
13351       /* Create the ASM_EXPR.  */
13352       if (parser->in_function_body)
13353         {
13354           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13355                                       inputs, clobbers, labels);
13356           /* If the extended syntax was not used, mark the ASM_EXPR.  */
13357           if (!extended_p)
13358             {
13359               tree temp = asm_stmt;
13360               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13361                 temp = TREE_OPERAND (temp, 0);
13362
13363               ASM_INPUT_P (temp) = 1;
13364             }
13365         }
13366       else
13367         cgraph_add_asm_node (string);
13368     }
13369 }
13370
13371 /* Declarators [gram.dcl.decl] */
13372
13373 /* Parse an init-declarator.
13374
13375    init-declarator:
13376      declarator initializer [opt]
13377
13378    GNU Extension:
13379
13380    init-declarator:
13381      declarator asm-specification [opt] attributes [opt] initializer [opt]
13382
13383    function-definition:
13384      decl-specifier-seq [opt] declarator ctor-initializer [opt]
13385        function-body
13386      decl-specifier-seq [opt] declarator function-try-block
13387
13388    GNU Extension:
13389
13390    function-definition:
13391      __extension__ function-definition
13392
13393    The DECL_SPECIFIERS apply to this declarator.  Returns a
13394    representation of the entity declared.  If MEMBER_P is TRUE, then
13395    this declarator appears in a class scope.  The new DECL created by
13396    this declarator is returned.
13397
13398    The CHECKS are access checks that should be performed once we know
13399    what entity is being declared (and, therefore, what classes have
13400    befriended it).
13401
13402    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13403    for a function-definition here as well.  If the declarator is a
13404    declarator for a function-definition, *FUNCTION_DEFINITION_P will
13405    be TRUE upon return.  By that point, the function-definition will
13406    have been completely parsed.
13407
13408    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13409    is FALSE.  */
13410
13411 static tree
13412 cp_parser_init_declarator (cp_parser* parser,
13413                            cp_decl_specifier_seq *decl_specifiers,
13414                            VEC (deferred_access_check,gc)* checks,
13415                            bool function_definition_allowed_p,
13416                            bool member_p,
13417                            int declares_class_or_enum,
13418                            bool* function_definition_p)
13419 {
13420   cp_token *token = NULL, *asm_spec_start_token = NULL,
13421            *attributes_start_token = NULL;
13422   cp_declarator *declarator;
13423   tree prefix_attributes;
13424   tree attributes;
13425   tree asm_specification;
13426   tree initializer;
13427   tree decl = NULL_TREE;
13428   tree scope;
13429   int is_initialized;
13430   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
13431      initialized with "= ..", CPP_OPEN_PAREN if initialized with
13432      "(...)".  */
13433   enum cpp_ttype initialization_kind;
13434   bool is_direct_init = false;
13435   bool is_non_constant_init;
13436   int ctor_dtor_or_conv_p;
13437   bool friend_p;
13438   tree pushed_scope = NULL;
13439
13440   /* Gather the attributes that were provided with the
13441      decl-specifiers.  */
13442   prefix_attributes = decl_specifiers->attributes;
13443
13444   /* Assume that this is not the declarator for a function
13445      definition.  */
13446   if (function_definition_p)
13447     *function_definition_p = false;
13448
13449   /* Defer access checks while parsing the declarator; we cannot know
13450      what names are accessible until we know what is being
13451      declared.  */
13452   resume_deferring_access_checks ();
13453
13454   /* Parse the declarator.  */
13455   token = cp_lexer_peek_token (parser->lexer);
13456   declarator
13457     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13458                             &ctor_dtor_or_conv_p,
13459                             /*parenthesized_p=*/NULL,
13460                             /*member_p=*/false);
13461   /* Gather up the deferred checks.  */
13462   stop_deferring_access_checks ();
13463
13464   /* If the DECLARATOR was erroneous, there's no need to go
13465      further.  */
13466   if (declarator == cp_error_declarator)
13467     return error_mark_node;
13468
13469   /* Check that the number of template-parameter-lists is OK.  */
13470   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13471                                                        token->location))
13472     return error_mark_node;
13473
13474   if (declares_class_or_enum & 2)
13475     cp_parser_check_for_definition_in_return_type (declarator,
13476                                                    decl_specifiers->type,
13477                                                    decl_specifiers->type_location);
13478
13479   /* Figure out what scope the entity declared by the DECLARATOR is
13480      located in.  `grokdeclarator' sometimes changes the scope, so
13481      we compute it now.  */
13482   scope = get_scope_of_declarator (declarator);
13483
13484   /* Perform any lookups in the declared type which were thought to be
13485      dependent, but are not in the scope of the declarator.  */
13486   decl_specifiers->type
13487     = maybe_update_decl_type (decl_specifiers->type, scope);
13488
13489   /* If we're allowing GNU extensions, look for an asm-specification
13490      and attributes.  */
13491   if (cp_parser_allow_gnu_extensions_p (parser))
13492     {
13493       /* Look for an asm-specification.  */
13494       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13495       asm_specification = cp_parser_asm_specification_opt (parser);
13496       /* And attributes.  */
13497       attributes_start_token = cp_lexer_peek_token (parser->lexer);
13498       attributes = cp_parser_attributes_opt (parser);
13499     }
13500   else
13501     {
13502       asm_specification = NULL_TREE;
13503       attributes = NULL_TREE;
13504     }
13505
13506   /* Peek at the next token.  */
13507   token = cp_lexer_peek_token (parser->lexer);
13508   /* Check to see if the token indicates the start of a
13509      function-definition.  */
13510   if (function_declarator_p (declarator)
13511       && cp_parser_token_starts_function_definition_p (token))
13512     {
13513       if (!function_definition_allowed_p)
13514         {
13515           /* If a function-definition should not appear here, issue an
13516              error message.  */
13517           cp_parser_error (parser,
13518                            "a function-definition is not allowed here");
13519           return error_mark_node;
13520         }
13521       else
13522         {
13523           location_t func_brace_location
13524             = cp_lexer_peek_token (parser->lexer)->location;
13525
13526           /* Neither attributes nor an asm-specification are allowed
13527              on a function-definition.  */
13528           if (asm_specification)
13529             error_at (asm_spec_start_token->location,
13530                       "an asm-specification is not allowed "
13531                       "on a function-definition");
13532           if (attributes)
13533             error_at (attributes_start_token->location,
13534                       "attributes are not allowed on a function-definition");
13535           /* This is a function-definition.  */
13536           *function_definition_p = true;
13537
13538           /* Parse the function definition.  */
13539           if (member_p)
13540             decl = cp_parser_save_member_function_body (parser,
13541                                                         decl_specifiers,
13542                                                         declarator,
13543                                                         prefix_attributes);
13544           else
13545             decl
13546               = (cp_parser_function_definition_from_specifiers_and_declarator
13547                  (parser, decl_specifiers, prefix_attributes, declarator));
13548
13549           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13550             {
13551               /* This is where the prologue starts...  */
13552               DECL_STRUCT_FUNCTION (decl)->function_start_locus
13553                 = func_brace_location;
13554             }
13555
13556           return decl;
13557         }
13558     }
13559
13560   /* [dcl.dcl]
13561
13562      Only in function declarations for constructors, destructors, and
13563      type conversions can the decl-specifier-seq be omitted.
13564
13565      We explicitly postpone this check past the point where we handle
13566      function-definitions because we tolerate function-definitions
13567      that are missing their return types in some modes.  */
13568   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13569     {
13570       cp_parser_error (parser,
13571                        "expected constructor, destructor, or type conversion");
13572       return error_mark_node;
13573     }
13574
13575   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
13576   if (token->type == CPP_EQ
13577       || token->type == CPP_OPEN_PAREN
13578       || token->type == CPP_OPEN_BRACE)
13579     {
13580       is_initialized = SD_INITIALIZED;
13581       initialization_kind = token->type;
13582
13583       if (token->type == CPP_EQ
13584           && function_declarator_p (declarator))
13585         {
13586           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13587           if (t2->keyword == RID_DEFAULT)
13588             is_initialized = SD_DEFAULTED;
13589           else if (t2->keyword == RID_DELETE)
13590             is_initialized = SD_DELETED;
13591         }
13592     }
13593   else
13594     {
13595       /* If the init-declarator isn't initialized and isn't followed by a
13596          `,' or `;', it's not a valid init-declarator.  */
13597       if (token->type != CPP_COMMA
13598           && token->type != CPP_SEMICOLON)
13599         {
13600           cp_parser_error (parser, "expected initializer");
13601           return error_mark_node;
13602         }
13603       is_initialized = SD_UNINITIALIZED;
13604       initialization_kind = CPP_EOF;
13605     }
13606
13607   /* Because start_decl has side-effects, we should only call it if we
13608      know we're going ahead.  By this point, we know that we cannot
13609      possibly be looking at any other construct.  */
13610   cp_parser_commit_to_tentative_parse (parser);
13611
13612   /* If the decl specifiers were bad, issue an error now that we're
13613      sure this was intended to be a declarator.  Then continue
13614      declaring the variable(s), as int, to try to cut down on further
13615      errors.  */
13616   if (decl_specifiers->any_specifiers_p
13617       && decl_specifiers->type == error_mark_node)
13618     {
13619       cp_parser_error (parser, "invalid type in declaration");
13620       decl_specifiers->type = integer_type_node;
13621     }
13622
13623   /* Check to see whether or not this declaration is a friend.  */
13624   friend_p = cp_parser_friend_p (decl_specifiers);
13625
13626   /* Enter the newly declared entry in the symbol table.  If we're
13627      processing a declaration in a class-specifier, we wait until
13628      after processing the initializer.  */
13629   if (!member_p)
13630     {
13631       if (parser->in_unbraced_linkage_specification_p)
13632         decl_specifiers->storage_class = sc_extern;
13633       decl = start_decl (declarator, decl_specifiers,
13634                          is_initialized, attributes, prefix_attributes,
13635                          &pushed_scope);
13636     }
13637   else if (scope)
13638     /* Enter the SCOPE.  That way unqualified names appearing in the
13639        initializer will be looked up in SCOPE.  */
13640     pushed_scope = push_scope (scope);
13641
13642   /* Perform deferred access control checks, now that we know in which
13643      SCOPE the declared entity resides.  */
13644   if (!member_p && decl)
13645     {
13646       tree saved_current_function_decl = NULL_TREE;
13647
13648       /* If the entity being declared is a function, pretend that we
13649          are in its scope.  If it is a `friend', it may have access to
13650          things that would not otherwise be accessible.  */
13651       if (TREE_CODE (decl) == FUNCTION_DECL)
13652         {
13653           saved_current_function_decl = current_function_decl;
13654           current_function_decl = decl;
13655         }
13656
13657       /* Perform access checks for template parameters.  */
13658       cp_parser_perform_template_parameter_access_checks (checks);
13659
13660       /* Perform the access control checks for the declarator and the
13661          decl-specifiers.  */
13662       perform_deferred_access_checks ();
13663
13664       /* Restore the saved value.  */
13665       if (TREE_CODE (decl) == FUNCTION_DECL)
13666         current_function_decl = saved_current_function_decl;
13667     }
13668
13669   /* Parse the initializer.  */
13670   initializer = NULL_TREE;
13671   is_direct_init = false;
13672   is_non_constant_init = true;
13673   if (is_initialized)
13674     {
13675       if (function_declarator_p (declarator))
13676         {
13677           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13678            if (initialization_kind == CPP_EQ)
13679              initializer = cp_parser_pure_specifier (parser);
13680            else
13681              {
13682                /* If the declaration was erroneous, we don't really
13683                   know what the user intended, so just silently
13684                   consume the initializer.  */
13685                if (decl != error_mark_node)
13686                  error_at (initializer_start_token->location,
13687                            "initializer provided for function");
13688                cp_parser_skip_to_closing_parenthesis (parser,
13689                                                       /*recovering=*/true,
13690                                                       /*or_comma=*/false,
13691                                                       /*consume_paren=*/true);
13692              }
13693         }
13694       else
13695         {
13696           /* We want to record the extra mangling scope for in-class
13697              initializers of class members and initializers of static data
13698              member templates.  The former is a C++0x feature which isn't
13699              implemented yet, and I expect it will involve deferring
13700              parsing of the initializer until end of class as with default
13701              arguments.  So right here we only handle the latter.  */
13702           if (!member_p && processing_template_decl)
13703             start_lambda_scope (decl);
13704           initializer = cp_parser_initializer (parser,
13705                                                &is_direct_init,
13706                                                &is_non_constant_init);
13707           if (!member_p && processing_template_decl)
13708             finish_lambda_scope ();
13709         }
13710     }
13711
13712   /* The old parser allows attributes to appear after a parenthesized
13713      initializer.  Mark Mitchell proposed removing this functionality
13714      on the GCC mailing lists on 2002-08-13.  This parser accepts the
13715      attributes -- but ignores them.  */
13716   if (cp_parser_allow_gnu_extensions_p (parser)
13717       && initialization_kind == CPP_OPEN_PAREN)
13718     if (cp_parser_attributes_opt (parser))
13719       warning (OPT_Wattributes,
13720                "attributes after parenthesized initializer ignored");
13721
13722   /* For an in-class declaration, use `grokfield' to create the
13723      declaration.  */
13724   if (member_p)
13725     {
13726       if (pushed_scope)
13727         {
13728           pop_scope (pushed_scope);
13729           pushed_scope = false;
13730         }
13731       decl = grokfield (declarator, decl_specifiers,
13732                         initializer, !is_non_constant_init,
13733                         /*asmspec=*/NULL_TREE,
13734                         prefix_attributes);
13735       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13736         cp_parser_save_default_args (parser, decl);
13737     }
13738
13739   /* Finish processing the declaration.  But, skip friend
13740      declarations.  */
13741   if (!friend_p && decl && decl != error_mark_node)
13742     {
13743       cp_finish_decl (decl,
13744                       initializer, !is_non_constant_init,
13745                       asm_specification,
13746                       /* If the initializer is in parentheses, then this is
13747                          a direct-initialization, which means that an
13748                          `explicit' constructor is OK.  Otherwise, an
13749                          `explicit' constructor cannot be used.  */
13750                       ((is_direct_init || !is_initialized)
13751                        ? 0 : LOOKUP_ONLYCONVERTING));
13752     }
13753   else if ((cxx_dialect != cxx98) && friend_p
13754            && decl && TREE_CODE (decl) == FUNCTION_DECL)
13755     /* Core issue #226 (C++0x only): A default template-argument
13756        shall not be specified in a friend class template
13757        declaration. */
13758     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
13759                              /*is_partial=*/0, /*is_friend_decl=*/1);
13760
13761   if (!friend_p && pushed_scope)
13762     pop_scope (pushed_scope);
13763
13764   return decl;
13765 }
13766
13767 /* Parse a declarator.
13768
13769    declarator:
13770      direct-declarator
13771      ptr-operator declarator
13772
13773    abstract-declarator:
13774      ptr-operator abstract-declarator [opt]
13775      direct-abstract-declarator
13776
13777    GNU Extensions:
13778
13779    declarator:
13780      attributes [opt] direct-declarator
13781      attributes [opt] ptr-operator declarator
13782
13783    abstract-declarator:
13784      attributes [opt] ptr-operator abstract-declarator [opt]
13785      attributes [opt] direct-abstract-declarator
13786
13787    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13788    detect constructor, destructor or conversion operators. It is set
13789    to -1 if the declarator is a name, and +1 if it is a
13790    function. Otherwise it is set to zero. Usually you just want to
13791    test for >0, but internally the negative value is used.
13792
13793    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13794    a decl-specifier-seq unless it declares a constructor, destructor,
13795    or conversion.  It might seem that we could check this condition in
13796    semantic analysis, rather than parsing, but that makes it difficult
13797    to handle something like `f()'.  We want to notice that there are
13798    no decl-specifiers, and therefore realize that this is an
13799    expression, not a declaration.)
13800
13801    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13802    the declarator is a direct-declarator of the form "(...)".
13803
13804    MEMBER_P is true iff this declarator is a member-declarator.  */
13805
13806 static cp_declarator *
13807 cp_parser_declarator (cp_parser* parser,
13808                       cp_parser_declarator_kind dcl_kind,
13809                       int* ctor_dtor_or_conv_p,
13810                       bool* parenthesized_p,
13811                       bool member_p)
13812 {
13813   cp_declarator *declarator;
13814   enum tree_code code;
13815   cp_cv_quals cv_quals;
13816   tree class_type;
13817   tree attributes = NULL_TREE;
13818
13819   /* Assume this is not a constructor, destructor, or type-conversion
13820      operator.  */
13821   if (ctor_dtor_or_conv_p)
13822     *ctor_dtor_or_conv_p = 0;
13823
13824   if (cp_parser_allow_gnu_extensions_p (parser))
13825     attributes = cp_parser_attributes_opt (parser);
13826
13827   /* Check for the ptr-operator production.  */
13828   cp_parser_parse_tentatively (parser);
13829   /* Parse the ptr-operator.  */
13830   code = cp_parser_ptr_operator (parser,
13831                                  &class_type,
13832                                  &cv_quals);
13833   /* If that worked, then we have a ptr-operator.  */
13834   if (cp_parser_parse_definitely (parser))
13835     {
13836       /* If a ptr-operator was found, then this declarator was not
13837          parenthesized.  */
13838       if (parenthesized_p)
13839         *parenthesized_p = true;
13840       /* The dependent declarator is optional if we are parsing an
13841          abstract-declarator.  */
13842       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13843         cp_parser_parse_tentatively (parser);
13844
13845       /* Parse the dependent declarator.  */
13846       declarator = cp_parser_declarator (parser, dcl_kind,
13847                                          /*ctor_dtor_or_conv_p=*/NULL,
13848                                          /*parenthesized_p=*/NULL,
13849                                          /*member_p=*/false);
13850
13851       /* If we are parsing an abstract-declarator, we must handle the
13852          case where the dependent declarator is absent.  */
13853       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13854           && !cp_parser_parse_definitely (parser))
13855         declarator = NULL;
13856
13857       declarator = cp_parser_make_indirect_declarator
13858         (code, class_type, cv_quals, declarator);
13859     }
13860   /* Everything else is a direct-declarator.  */
13861   else
13862     {
13863       if (parenthesized_p)
13864         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13865                                                    CPP_OPEN_PAREN);
13866       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13867                                                 ctor_dtor_or_conv_p,
13868                                                 member_p);
13869     }
13870
13871   if (attributes && declarator && declarator != cp_error_declarator)
13872     declarator->attributes = attributes;
13873
13874   return declarator;
13875 }
13876
13877 /* Parse a direct-declarator or direct-abstract-declarator.
13878
13879    direct-declarator:
13880      declarator-id
13881      direct-declarator ( parameter-declaration-clause )
13882        cv-qualifier-seq [opt]
13883        exception-specification [opt]
13884      direct-declarator [ constant-expression [opt] ]
13885      ( declarator )
13886
13887    direct-abstract-declarator:
13888      direct-abstract-declarator [opt]
13889        ( parameter-declaration-clause )
13890        cv-qualifier-seq [opt]
13891        exception-specification [opt]
13892      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13893      ( abstract-declarator )
13894
13895    Returns a representation of the declarator.  DCL_KIND is
13896    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13897    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13898    we are parsing a direct-declarator.  It is
13899    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13900    of ambiguity we prefer an abstract declarator, as per
13901    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13902    cp_parser_declarator.  */
13903
13904 static cp_declarator *
13905 cp_parser_direct_declarator (cp_parser* parser,
13906                              cp_parser_declarator_kind dcl_kind,
13907                              int* ctor_dtor_or_conv_p,
13908                              bool member_p)
13909 {
13910   cp_token *token;
13911   cp_declarator *declarator = NULL;
13912   tree scope = NULL_TREE;
13913   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13914   bool saved_in_declarator_p = parser->in_declarator_p;
13915   bool first = true;
13916   tree pushed_scope = NULL_TREE;
13917
13918   while (true)
13919     {
13920       /* Peek at the next token.  */
13921       token = cp_lexer_peek_token (parser->lexer);
13922       if (token->type == CPP_OPEN_PAREN)
13923         {
13924           /* This is either a parameter-declaration-clause, or a
13925              parenthesized declarator. When we know we are parsing a
13926              named declarator, it must be a parenthesized declarator
13927              if FIRST is true. For instance, `(int)' is a
13928              parameter-declaration-clause, with an omitted
13929              direct-abstract-declarator. But `((*))', is a
13930              parenthesized abstract declarator. Finally, when T is a
13931              template parameter `(T)' is a
13932              parameter-declaration-clause, and not a parenthesized
13933              named declarator.
13934
13935              We first try and parse a parameter-declaration-clause,
13936              and then try a nested declarator (if FIRST is true).
13937
13938              It is not an error for it not to be a
13939              parameter-declaration-clause, even when FIRST is
13940              false. Consider,
13941
13942                int i (int);
13943                int i (3);
13944
13945              The first is the declaration of a function while the
13946              second is the definition of a variable, including its
13947              initializer.
13948
13949              Having seen only the parenthesis, we cannot know which of
13950              these two alternatives should be selected.  Even more
13951              complex are examples like:
13952
13953                int i (int (a));
13954                int i (int (3));
13955
13956              The former is a function-declaration; the latter is a
13957              variable initialization.
13958
13959              Thus again, we try a parameter-declaration-clause, and if
13960              that fails, we back out and return.  */
13961
13962           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13963             {
13964               tree params;
13965               unsigned saved_num_template_parameter_lists;
13966               bool is_declarator = false;
13967               tree t;
13968
13969               /* In a member-declarator, the only valid interpretation
13970                  of a parenthesis is the start of a
13971                  parameter-declaration-clause.  (It is invalid to
13972                  initialize a static data member with a parenthesized
13973                  initializer; only the "=" form of initialization is
13974                  permitted.)  */
13975               if (!member_p)
13976                 cp_parser_parse_tentatively (parser);
13977
13978               /* Consume the `('.  */
13979               cp_lexer_consume_token (parser->lexer);
13980               if (first)
13981                 {
13982                   /* If this is going to be an abstract declarator, we're
13983                      in a declarator and we can't have default args.  */
13984                   parser->default_arg_ok_p = false;
13985                   parser->in_declarator_p = true;
13986                 }
13987
13988               /* Inside the function parameter list, surrounding
13989                  template-parameter-lists do not apply.  */
13990               saved_num_template_parameter_lists
13991                 = parser->num_template_parameter_lists;
13992               parser->num_template_parameter_lists = 0;
13993
13994               begin_scope (sk_function_parms, NULL_TREE);
13995
13996               /* Parse the parameter-declaration-clause.  */
13997               params = cp_parser_parameter_declaration_clause (parser);
13998
13999               parser->num_template_parameter_lists
14000                 = saved_num_template_parameter_lists;
14001
14002               /* If all went well, parse the cv-qualifier-seq and the
14003                  exception-specification.  */
14004               if (member_p || cp_parser_parse_definitely (parser))
14005                 {
14006                   cp_cv_quals cv_quals;
14007                   tree exception_specification;
14008                   tree late_return;
14009
14010                   is_declarator = true;
14011
14012                   if (ctor_dtor_or_conv_p)
14013                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14014                   first = false;
14015                   /* Consume the `)'.  */
14016                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
14017
14018                   /* Parse the cv-qualifier-seq.  */
14019                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14020                   /* And the exception-specification.  */
14021                   exception_specification
14022                     = cp_parser_exception_specification_opt (parser);
14023
14024                   late_return
14025                     = cp_parser_late_return_type_opt (parser);
14026
14027                   /* Create the function-declarator.  */
14028                   declarator = make_call_declarator (declarator,
14029                                                      params,
14030                                                      cv_quals,
14031                                                      exception_specification,
14032                                                      late_return);
14033                   /* Any subsequent parameter lists are to do with
14034                      return type, so are not those of the declared
14035                      function.  */
14036                   parser->default_arg_ok_p = false;
14037                 }
14038
14039               /* Remove the function parms from scope.  */
14040               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14041                 pop_binding (DECL_NAME (t), t);
14042               leave_scope();
14043
14044               if (is_declarator)
14045                 /* Repeat the main loop.  */
14046                 continue;
14047             }
14048
14049           /* If this is the first, we can try a parenthesized
14050              declarator.  */
14051           if (first)
14052             {
14053               bool saved_in_type_id_in_expr_p;
14054
14055               parser->default_arg_ok_p = saved_default_arg_ok_p;
14056               parser->in_declarator_p = saved_in_declarator_p;
14057
14058               /* Consume the `('.  */
14059               cp_lexer_consume_token (parser->lexer);
14060               /* Parse the nested declarator.  */
14061               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14062               parser->in_type_id_in_expr_p = true;
14063               declarator
14064                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14065                                         /*parenthesized_p=*/NULL,
14066                                         member_p);
14067               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14068               first = false;
14069               /* Expect a `)'.  */
14070               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14071                 declarator = cp_error_declarator;
14072               if (declarator == cp_error_declarator)
14073                 break;
14074
14075               goto handle_declarator;
14076             }
14077           /* Otherwise, we must be done.  */
14078           else
14079             break;
14080         }
14081       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14082                && token->type == CPP_OPEN_SQUARE)
14083         {
14084           /* Parse an array-declarator.  */
14085           tree bounds;
14086
14087           if (ctor_dtor_or_conv_p)
14088             *ctor_dtor_or_conv_p = 0;
14089
14090           first = false;
14091           parser->default_arg_ok_p = false;
14092           parser->in_declarator_p = true;
14093           /* Consume the `['.  */
14094           cp_lexer_consume_token (parser->lexer);
14095           /* Peek at the next token.  */
14096           token = cp_lexer_peek_token (parser->lexer);
14097           /* If the next token is `]', then there is no
14098              constant-expression.  */
14099           if (token->type != CPP_CLOSE_SQUARE)
14100             {
14101               bool non_constant_p;
14102
14103               bounds
14104                 = cp_parser_constant_expression (parser,
14105                                                  /*allow_non_constant=*/true,
14106                                                  &non_constant_p);
14107               if (!non_constant_p)
14108                 bounds = fold_non_dependent_expr (bounds);
14109               /* Normally, the array bound must be an integral constant
14110                  expression.  However, as an extension, we allow VLAs
14111                  in function scopes as long as they aren't part of a
14112                  parameter declaration.  */
14113               else if (!parser->in_function_body
14114                        || current_binding_level->kind == sk_function_parms)
14115                 {
14116                   cp_parser_error (parser,
14117                                    "array bound is not an integer constant");
14118                   bounds = error_mark_node;
14119                 }
14120               else if (processing_template_decl && !error_operand_p (bounds))
14121                 {
14122                   /* Remember this wasn't a constant-expression.  */
14123                   bounds = build_nop (TREE_TYPE (bounds), bounds);
14124                   TREE_SIDE_EFFECTS (bounds) = 1;
14125                 }
14126             }
14127           else
14128             bounds = NULL_TREE;
14129           /* Look for the closing `]'.  */
14130           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14131             {
14132               declarator = cp_error_declarator;
14133               break;
14134             }
14135
14136           declarator = make_array_declarator (declarator, bounds);
14137         }
14138       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14139         {
14140           {
14141             tree qualifying_scope;
14142             tree unqualified_name;
14143             special_function_kind sfk;
14144             bool abstract_ok;
14145             bool pack_expansion_p = false;
14146             cp_token *declarator_id_start_token;
14147
14148             /* Parse a declarator-id */
14149             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14150             if (abstract_ok)
14151               {
14152                 cp_parser_parse_tentatively (parser);
14153
14154                 /* If we see an ellipsis, we should be looking at a
14155                    parameter pack. */
14156                 if (token->type == CPP_ELLIPSIS)
14157                   {
14158                     /* Consume the `...' */
14159                     cp_lexer_consume_token (parser->lexer);
14160
14161                     pack_expansion_p = true;
14162                   }
14163               }
14164
14165             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14166             unqualified_name
14167               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14168             qualifying_scope = parser->scope;
14169             if (abstract_ok)
14170               {
14171                 bool okay = false;
14172
14173                 if (!unqualified_name && pack_expansion_p)
14174                   {
14175                     /* Check whether an error occurred. */
14176                     okay = !cp_parser_error_occurred (parser);
14177
14178                     /* We already consumed the ellipsis to mark a
14179                        parameter pack, but we have no way to report it,
14180                        so abort the tentative parse. We will be exiting
14181                        immediately anyway. */
14182                     cp_parser_abort_tentative_parse (parser);
14183                   }
14184                 else
14185                   okay = cp_parser_parse_definitely (parser);
14186
14187                 if (!okay)
14188                   unqualified_name = error_mark_node;
14189                 else if (unqualified_name
14190                          && (qualifying_scope
14191                              || (TREE_CODE (unqualified_name)
14192                                  != IDENTIFIER_NODE)))
14193                   {
14194                     cp_parser_error (parser, "expected unqualified-id");
14195                     unqualified_name = error_mark_node;
14196                   }
14197               }
14198
14199             if (!unqualified_name)
14200               return NULL;
14201             if (unqualified_name == error_mark_node)
14202               {
14203                 declarator = cp_error_declarator;
14204                 pack_expansion_p = false;
14205                 declarator->parameter_pack_p = false;
14206                 break;
14207               }
14208
14209             if (qualifying_scope && at_namespace_scope_p ()
14210                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14211               {
14212                 /* In the declaration of a member of a template class
14213                    outside of the class itself, the SCOPE will sometimes
14214                    be a TYPENAME_TYPE.  For example, given:
14215
14216                    template <typename T>
14217                    int S<T>::R::i = 3;
14218
14219                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
14220                    this context, we must resolve S<T>::R to an ordinary
14221                    type, rather than a typename type.
14222
14223                    The reason we normally avoid resolving TYPENAME_TYPEs
14224                    is that a specialization of `S' might render
14225                    `S<T>::R' not a type.  However, if `S' is
14226                    specialized, then this `i' will not be used, so there
14227                    is no harm in resolving the types here.  */
14228                 tree type;
14229
14230                 /* Resolve the TYPENAME_TYPE.  */
14231                 type = resolve_typename_type (qualifying_scope,
14232                                               /*only_current_p=*/false);
14233                 /* If that failed, the declarator is invalid.  */
14234                 if (TREE_CODE (type) == TYPENAME_TYPE)
14235                   {
14236                     if (typedef_variant_p (type))
14237                       error_at (declarator_id_start_token->location,
14238                                 "cannot define member of dependent typedef "
14239                                 "%qT", type);
14240                     else
14241                       error_at (declarator_id_start_token->location,
14242                                 "%<%T::%E%> is not a type",
14243                                 TYPE_CONTEXT (qualifying_scope),
14244                                 TYPE_IDENTIFIER (qualifying_scope));
14245                   }
14246                 qualifying_scope = type;
14247               }
14248
14249             sfk = sfk_none;
14250
14251             if (unqualified_name)
14252               {
14253                 tree class_type;
14254
14255                 if (qualifying_scope
14256                     && CLASS_TYPE_P (qualifying_scope))
14257                   class_type = qualifying_scope;
14258                 else
14259                   class_type = current_class_type;
14260
14261                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14262                   {
14263                     tree name_type = TREE_TYPE (unqualified_name);
14264                     if (class_type && same_type_p (name_type, class_type))
14265                       {
14266                         if (qualifying_scope
14267                             && CLASSTYPE_USE_TEMPLATE (name_type))
14268                           {
14269                             error_at (declarator_id_start_token->location,
14270                                       "invalid use of constructor as a template");
14271                             inform (declarator_id_start_token->location,
14272                                     "use %<%T::%D%> instead of %<%T::%D%> to "
14273                                     "name the constructor in a qualified name",
14274                                     class_type,
14275                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14276                                     class_type, name_type);
14277                             declarator = cp_error_declarator;
14278                             break;
14279                           }
14280                         else
14281                           unqualified_name = constructor_name (class_type);
14282                       }
14283                     else
14284                       {
14285                         /* We do not attempt to print the declarator
14286                            here because we do not have enough
14287                            information about its original syntactic
14288                            form.  */
14289                         cp_parser_error (parser, "invalid declarator");
14290                         declarator = cp_error_declarator;
14291                         break;
14292                       }
14293                   }
14294
14295                 if (class_type)
14296                   {
14297                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14298                       sfk = sfk_destructor;
14299                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14300                       sfk = sfk_conversion;
14301                     else if (/* There's no way to declare a constructor
14302                                 for an anonymous type, even if the type
14303                                 got a name for linkage purposes.  */
14304                              !TYPE_WAS_ANONYMOUS (class_type)
14305                              && constructor_name_p (unqualified_name,
14306                                                     class_type))
14307                       {
14308                         unqualified_name = constructor_name (class_type);
14309                         sfk = sfk_constructor;
14310                       }
14311                     else if (is_overloaded_fn (unqualified_name)
14312                              && DECL_CONSTRUCTOR_P (get_first_fn
14313                                                     (unqualified_name)))
14314                       sfk = sfk_constructor;
14315
14316                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
14317                       *ctor_dtor_or_conv_p = -1;
14318                   }
14319               }
14320             declarator = make_id_declarator (qualifying_scope,
14321                                              unqualified_name,
14322                                              sfk);
14323             declarator->id_loc = token->location;
14324             declarator->parameter_pack_p = pack_expansion_p;
14325
14326             if (pack_expansion_p)
14327               maybe_warn_variadic_templates ();
14328           }
14329
14330         handle_declarator:;
14331           scope = get_scope_of_declarator (declarator);
14332           if (scope)
14333             /* Any names that appear after the declarator-id for a
14334                member are looked up in the containing scope.  */
14335             pushed_scope = push_scope (scope);
14336           parser->in_declarator_p = true;
14337           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14338               || (declarator && declarator->kind == cdk_id))
14339             /* Default args are only allowed on function
14340                declarations.  */
14341             parser->default_arg_ok_p = saved_default_arg_ok_p;
14342           else
14343             parser->default_arg_ok_p = false;
14344
14345           first = false;
14346         }
14347       /* We're done.  */
14348       else
14349         break;
14350     }
14351
14352   /* For an abstract declarator, we might wind up with nothing at this
14353      point.  That's an error; the declarator is not optional.  */
14354   if (!declarator)
14355     cp_parser_error (parser, "expected declarator");
14356
14357   /* If we entered a scope, we must exit it now.  */
14358   if (pushed_scope)
14359     pop_scope (pushed_scope);
14360
14361   parser->default_arg_ok_p = saved_default_arg_ok_p;
14362   parser->in_declarator_p = saved_in_declarator_p;
14363
14364   return declarator;
14365 }
14366
14367 /* Parse a ptr-operator.
14368
14369    ptr-operator:
14370      * cv-qualifier-seq [opt]
14371      &
14372      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14373
14374    GNU Extension:
14375
14376    ptr-operator:
14377      & cv-qualifier-seq [opt]
14378
14379    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14380    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14381    an rvalue reference. In the case of a pointer-to-member, *TYPE is
14382    filled in with the TYPE containing the member.  *CV_QUALS is
14383    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14384    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
14385    Note that the tree codes returned by this function have nothing
14386    to do with the types of trees that will be eventually be created
14387    to represent the pointer or reference type being parsed. They are
14388    just constants with suggestive names. */
14389 static enum tree_code
14390 cp_parser_ptr_operator (cp_parser* parser,
14391                         tree* type,
14392                         cp_cv_quals *cv_quals)
14393 {
14394   enum tree_code code = ERROR_MARK;
14395   cp_token *token;
14396
14397   /* Assume that it's not a pointer-to-member.  */
14398   *type = NULL_TREE;
14399   /* And that there are no cv-qualifiers.  */
14400   *cv_quals = TYPE_UNQUALIFIED;
14401
14402   /* Peek at the next token.  */
14403   token = cp_lexer_peek_token (parser->lexer);
14404
14405   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
14406   if (token->type == CPP_MULT)
14407     code = INDIRECT_REF;
14408   else if (token->type == CPP_AND)
14409     code = ADDR_EXPR;
14410   else if ((cxx_dialect != cxx98) &&
14411            token->type == CPP_AND_AND) /* C++0x only */
14412     code = NON_LVALUE_EXPR;
14413
14414   if (code != ERROR_MARK)
14415     {
14416       /* Consume the `*', `&' or `&&'.  */
14417       cp_lexer_consume_token (parser->lexer);
14418
14419       /* A `*' can be followed by a cv-qualifier-seq, and so can a
14420          `&', if we are allowing GNU extensions.  (The only qualifier
14421          that can legally appear after `&' is `restrict', but that is
14422          enforced during semantic analysis.  */
14423       if (code == INDIRECT_REF
14424           || cp_parser_allow_gnu_extensions_p (parser))
14425         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14426     }
14427   else
14428     {
14429       /* Try the pointer-to-member case.  */
14430       cp_parser_parse_tentatively (parser);
14431       /* Look for the optional `::' operator.  */
14432       cp_parser_global_scope_opt (parser,
14433                                   /*current_scope_valid_p=*/false);
14434       /* Look for the nested-name specifier.  */
14435       token = cp_lexer_peek_token (parser->lexer);
14436       cp_parser_nested_name_specifier (parser,
14437                                        /*typename_keyword_p=*/false,
14438                                        /*check_dependency_p=*/true,
14439                                        /*type_p=*/false,
14440                                        /*is_declaration=*/false);
14441       /* If we found it, and the next token is a `*', then we are
14442          indeed looking at a pointer-to-member operator.  */
14443       if (!cp_parser_error_occurred (parser)
14444           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14445         {
14446           /* Indicate that the `*' operator was used.  */
14447           code = INDIRECT_REF;
14448
14449           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14450             error_at (token->location, "%qD is a namespace", parser->scope);
14451           else
14452             {
14453               /* The type of which the member is a member is given by the
14454                  current SCOPE.  */
14455               *type = parser->scope;
14456               /* The next name will not be qualified.  */
14457               parser->scope = NULL_TREE;
14458               parser->qualifying_scope = NULL_TREE;
14459               parser->object_scope = NULL_TREE;
14460               /* Look for the optional cv-qualifier-seq.  */
14461               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14462             }
14463         }
14464       /* If that didn't work we don't have a ptr-operator.  */
14465       if (!cp_parser_parse_definitely (parser))
14466         cp_parser_error (parser, "expected ptr-operator");
14467     }
14468
14469   return code;
14470 }
14471
14472 /* Parse an (optional) cv-qualifier-seq.
14473
14474    cv-qualifier-seq:
14475      cv-qualifier cv-qualifier-seq [opt]
14476
14477    cv-qualifier:
14478      const
14479      volatile
14480
14481    GNU Extension:
14482
14483    cv-qualifier:
14484      __restrict__
14485
14486    Returns a bitmask representing the cv-qualifiers.  */
14487
14488 static cp_cv_quals
14489 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14490 {
14491   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14492
14493   while (true)
14494     {
14495       cp_token *token;
14496       cp_cv_quals cv_qualifier;
14497
14498       /* Peek at the next token.  */
14499       token = cp_lexer_peek_token (parser->lexer);
14500       /* See if it's a cv-qualifier.  */
14501       switch (token->keyword)
14502         {
14503         case RID_CONST:
14504           cv_qualifier = TYPE_QUAL_CONST;
14505           break;
14506
14507         case RID_VOLATILE:
14508           cv_qualifier = TYPE_QUAL_VOLATILE;
14509           break;
14510
14511         case RID_RESTRICT:
14512           cv_qualifier = TYPE_QUAL_RESTRICT;
14513           break;
14514
14515         default:
14516           cv_qualifier = TYPE_UNQUALIFIED;
14517           break;
14518         }
14519
14520       if (!cv_qualifier)
14521         break;
14522
14523       if (cv_quals & cv_qualifier)
14524         {
14525           error_at (token->location, "duplicate cv-qualifier");
14526           cp_lexer_purge_token (parser->lexer);
14527         }
14528       else
14529         {
14530           cp_lexer_consume_token (parser->lexer);
14531           cv_quals |= cv_qualifier;
14532         }
14533     }
14534
14535   return cv_quals;
14536 }
14537
14538 /* Parse a late-specified return type, if any.  This is not a separate
14539    non-terminal, but part of a function declarator, which looks like
14540
14541    -> trailing-type-specifier-seq abstract-declarator(opt)
14542
14543    Returns the type indicated by the type-id.  */
14544
14545 static tree
14546 cp_parser_late_return_type_opt (cp_parser* parser)
14547 {
14548   cp_token *token;
14549
14550   /* Peek at the next token.  */
14551   token = cp_lexer_peek_token (parser->lexer);
14552   /* A late-specified return type is indicated by an initial '->'. */
14553   if (token->type != CPP_DEREF)
14554     return NULL_TREE;
14555
14556   /* Consume the ->.  */
14557   cp_lexer_consume_token (parser->lexer);
14558
14559   return cp_parser_trailing_type_id (parser);
14560 }
14561
14562 /* Parse a declarator-id.
14563
14564    declarator-id:
14565      id-expression
14566      :: [opt] nested-name-specifier [opt] type-name
14567
14568    In the `id-expression' case, the value returned is as for
14569    cp_parser_id_expression if the id-expression was an unqualified-id.
14570    If the id-expression was a qualified-id, then a SCOPE_REF is
14571    returned.  The first operand is the scope (either a NAMESPACE_DECL
14572    or TREE_TYPE), but the second is still just a representation of an
14573    unqualified-id.  */
14574
14575 static tree
14576 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14577 {
14578   tree id;
14579   /* The expression must be an id-expression.  Assume that qualified
14580      names are the names of types so that:
14581
14582        template <class T>
14583        int S<T>::R::i = 3;
14584
14585      will work; we must treat `S<T>::R' as the name of a type.
14586      Similarly, assume that qualified names are templates, where
14587      required, so that:
14588
14589        template <class T>
14590        int S<T>::R<T>::i = 3;
14591
14592      will work, too.  */
14593   id = cp_parser_id_expression (parser,
14594                                 /*template_keyword_p=*/false,
14595                                 /*check_dependency_p=*/false,
14596                                 /*template_p=*/NULL,
14597                                 /*declarator_p=*/true,
14598                                 optional_p);
14599   if (id && BASELINK_P (id))
14600     id = BASELINK_FUNCTIONS (id);
14601   return id;
14602 }
14603
14604 /* Parse a type-id.
14605
14606    type-id:
14607      type-specifier-seq abstract-declarator [opt]
14608
14609    Returns the TYPE specified.  */
14610
14611 static tree
14612 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14613                      bool is_trailing_return)
14614 {
14615   cp_decl_specifier_seq type_specifier_seq;
14616   cp_declarator *abstract_declarator;
14617
14618   /* Parse the type-specifier-seq.  */
14619   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14620                                 is_trailing_return,
14621                                 &type_specifier_seq);
14622   if (type_specifier_seq.type == error_mark_node)
14623     return error_mark_node;
14624
14625   /* There might or might not be an abstract declarator.  */
14626   cp_parser_parse_tentatively (parser);
14627   /* Look for the declarator.  */
14628   abstract_declarator
14629     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14630                             /*parenthesized_p=*/NULL,
14631                             /*member_p=*/false);
14632   /* Check to see if there really was a declarator.  */
14633   if (!cp_parser_parse_definitely (parser))
14634     abstract_declarator = NULL;
14635
14636   if (type_specifier_seq.type
14637       && type_uses_auto (type_specifier_seq.type))
14638     {
14639       /* A type-id with type 'auto' is only ok if the abstract declarator
14640          is a function declarator with a late-specified return type.  */
14641       if (abstract_declarator
14642           && abstract_declarator->kind == cdk_function
14643           && abstract_declarator->u.function.late_return_type)
14644         /* OK */;
14645       else
14646         {
14647           error ("invalid use of %<auto%>");
14648           return error_mark_node;
14649         }
14650     }
14651   
14652   return groktypename (&type_specifier_seq, abstract_declarator,
14653                        is_template_arg);
14654 }
14655
14656 static tree cp_parser_type_id (cp_parser *parser)
14657 {
14658   return cp_parser_type_id_1 (parser, false, false);
14659 }
14660
14661 static tree cp_parser_template_type_arg (cp_parser *parser)
14662 {
14663   return cp_parser_type_id_1 (parser, true, false);
14664 }
14665
14666 static tree cp_parser_trailing_type_id (cp_parser *parser)
14667 {
14668   return cp_parser_type_id_1 (parser, false, true);
14669 }
14670
14671 /* Parse a type-specifier-seq.
14672
14673    type-specifier-seq:
14674      type-specifier type-specifier-seq [opt]
14675
14676    GNU extension:
14677
14678    type-specifier-seq:
14679      attributes type-specifier-seq [opt]
14680
14681    If IS_DECLARATION is true, we are at the start of a "condition" or
14682    exception-declaration, so we might be followed by a declarator-id.
14683
14684    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14685    i.e. we've just seen "->".
14686
14687    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
14688
14689 static void
14690 cp_parser_type_specifier_seq (cp_parser* parser,
14691                               bool is_declaration,
14692                               bool is_trailing_return,
14693                               cp_decl_specifier_seq *type_specifier_seq)
14694 {
14695   bool seen_type_specifier = false;
14696   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14697   cp_token *start_token = NULL;
14698
14699   /* Clear the TYPE_SPECIFIER_SEQ.  */
14700   clear_decl_specs (type_specifier_seq);
14701
14702   /* In the context of a trailing return type, enum E { } is an
14703      elaborated-type-specifier followed by a function-body, not an
14704      enum-specifier.  */
14705   if (is_trailing_return)
14706     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14707
14708   /* Parse the type-specifiers and attributes.  */
14709   while (true)
14710     {
14711       tree type_specifier;
14712       bool is_cv_qualifier;
14713
14714       /* Check for attributes first.  */
14715       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14716         {
14717           type_specifier_seq->attributes =
14718             chainon (type_specifier_seq->attributes,
14719                      cp_parser_attributes_opt (parser));
14720           continue;
14721         }
14722
14723       /* record the token of the beginning of the type specifier seq,
14724          for error reporting purposes*/
14725      if (!start_token)
14726        start_token = cp_lexer_peek_token (parser->lexer);
14727
14728       /* Look for the type-specifier.  */
14729       type_specifier = cp_parser_type_specifier (parser,
14730                                                  flags,
14731                                                  type_specifier_seq,
14732                                                  /*is_declaration=*/false,
14733                                                  NULL,
14734                                                  &is_cv_qualifier);
14735       if (!type_specifier)
14736         {
14737           /* If the first type-specifier could not be found, this is not a
14738              type-specifier-seq at all.  */
14739           if (!seen_type_specifier)
14740             {
14741               cp_parser_error (parser, "expected type-specifier");
14742               type_specifier_seq->type = error_mark_node;
14743               return;
14744             }
14745           /* If subsequent type-specifiers could not be found, the
14746              type-specifier-seq is complete.  */
14747           break;
14748         }
14749
14750       seen_type_specifier = true;
14751       /* The standard says that a condition can be:
14752
14753             type-specifier-seq declarator = assignment-expression
14754
14755          However, given:
14756
14757            struct S {};
14758            if (int S = ...)
14759
14760          we should treat the "S" as a declarator, not as a
14761          type-specifier.  The standard doesn't say that explicitly for
14762          type-specifier-seq, but it does say that for
14763          decl-specifier-seq in an ordinary declaration.  Perhaps it
14764          would be clearer just to allow a decl-specifier-seq here, and
14765          then add a semantic restriction that if any decl-specifiers
14766          that are not type-specifiers appear, the program is invalid.  */
14767       if (is_declaration && !is_cv_qualifier)
14768         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14769     }
14770
14771   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14772 }
14773
14774 /* Parse a parameter-declaration-clause.
14775
14776    parameter-declaration-clause:
14777      parameter-declaration-list [opt] ... [opt]
14778      parameter-declaration-list , ...
14779
14780    Returns a representation for the parameter declarations.  A return
14781    value of NULL indicates a parameter-declaration-clause consisting
14782    only of an ellipsis.  */
14783
14784 static tree
14785 cp_parser_parameter_declaration_clause (cp_parser* parser)
14786 {
14787   tree parameters;
14788   cp_token *token;
14789   bool ellipsis_p;
14790   bool is_error;
14791
14792   /* Peek at the next token.  */
14793   token = cp_lexer_peek_token (parser->lexer);
14794   /* Check for trivial parameter-declaration-clauses.  */
14795   if (token->type == CPP_ELLIPSIS)
14796     {
14797       /* Consume the `...' token.  */
14798       cp_lexer_consume_token (parser->lexer);
14799       return NULL_TREE;
14800     }
14801   else if (token->type == CPP_CLOSE_PAREN)
14802     /* There are no parameters.  */
14803     {
14804 #ifndef NO_IMPLICIT_EXTERN_C
14805       if (in_system_header && current_class_type == NULL
14806           && current_lang_name == lang_name_c)
14807         return NULL_TREE;
14808       else
14809 #endif
14810         return void_list_node;
14811     }
14812   /* Check for `(void)', too, which is a special case.  */
14813   else if (token->keyword == RID_VOID
14814            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14815                == CPP_CLOSE_PAREN))
14816     {
14817       /* Consume the `void' token.  */
14818       cp_lexer_consume_token (parser->lexer);
14819       /* There are no parameters.  */
14820       return void_list_node;
14821     }
14822
14823   /* Parse the parameter-declaration-list.  */
14824   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14825   /* If a parse error occurred while parsing the
14826      parameter-declaration-list, then the entire
14827      parameter-declaration-clause is erroneous.  */
14828   if (is_error)
14829     return NULL;
14830
14831   /* Peek at the next token.  */
14832   token = cp_lexer_peek_token (parser->lexer);
14833   /* If it's a `,', the clause should terminate with an ellipsis.  */
14834   if (token->type == CPP_COMMA)
14835     {
14836       /* Consume the `,'.  */
14837       cp_lexer_consume_token (parser->lexer);
14838       /* Expect an ellipsis.  */
14839       ellipsis_p
14840         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14841     }
14842   /* It might also be `...' if the optional trailing `,' was
14843      omitted.  */
14844   else if (token->type == CPP_ELLIPSIS)
14845     {
14846       /* Consume the `...' token.  */
14847       cp_lexer_consume_token (parser->lexer);
14848       /* And remember that we saw it.  */
14849       ellipsis_p = true;
14850     }
14851   else
14852     ellipsis_p = false;
14853
14854   /* Finish the parameter list.  */
14855   if (!ellipsis_p)
14856     parameters = chainon (parameters, void_list_node);
14857
14858   return parameters;
14859 }
14860
14861 /* Parse a parameter-declaration-list.
14862
14863    parameter-declaration-list:
14864      parameter-declaration
14865      parameter-declaration-list , parameter-declaration
14866
14867    Returns a representation of the parameter-declaration-list, as for
14868    cp_parser_parameter_declaration_clause.  However, the
14869    `void_list_node' is never appended to the list.  Upon return,
14870    *IS_ERROR will be true iff an error occurred.  */
14871
14872 static tree
14873 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14874 {
14875   tree parameters = NULL_TREE;
14876   tree *tail = &parameters; 
14877   bool saved_in_unbraced_linkage_specification_p;
14878   int index = 0;
14879
14880   /* Assume all will go well.  */
14881   *is_error = false;
14882   /* The special considerations that apply to a function within an
14883      unbraced linkage specifications do not apply to the parameters
14884      to the function.  */
14885   saved_in_unbraced_linkage_specification_p 
14886     = parser->in_unbraced_linkage_specification_p;
14887   parser->in_unbraced_linkage_specification_p = false;
14888
14889   /* Look for more parameters.  */
14890   while (true)
14891     {
14892       cp_parameter_declarator *parameter;
14893       tree decl = error_mark_node;
14894       bool parenthesized_p;
14895       /* Parse the parameter.  */
14896       parameter
14897         = cp_parser_parameter_declaration (parser,
14898                                            /*template_parm_p=*/false,
14899                                            &parenthesized_p);
14900
14901       /* We don't know yet if the enclosing context is deprecated, so wait
14902          and warn in grokparms if appropriate.  */
14903       deprecated_state = DEPRECATED_SUPPRESS;
14904
14905       if (parameter)
14906         decl = grokdeclarator (parameter->declarator,
14907                                &parameter->decl_specifiers,
14908                                PARM,
14909                                parameter->default_argument != NULL_TREE,
14910                                &parameter->decl_specifiers.attributes);
14911
14912       deprecated_state = DEPRECATED_NORMAL;
14913
14914       /* If a parse error occurred parsing the parameter declaration,
14915          then the entire parameter-declaration-list is erroneous.  */
14916       if (decl == error_mark_node)
14917         {
14918           *is_error = true;
14919           parameters = error_mark_node;
14920           break;
14921         }
14922
14923       if (parameter->decl_specifiers.attributes)
14924         cplus_decl_attributes (&decl,
14925                                parameter->decl_specifiers.attributes,
14926                                0);
14927       if (DECL_NAME (decl))
14928         decl = pushdecl (decl);
14929
14930       if (decl != error_mark_node)
14931         {
14932           retrofit_lang_decl (decl);
14933           DECL_PARM_INDEX (decl) = ++index;
14934         }
14935
14936       /* Add the new parameter to the list.  */
14937       *tail = build_tree_list (parameter->default_argument, decl);
14938       tail = &TREE_CHAIN (*tail);
14939
14940       /* Peek at the next token.  */
14941       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14942           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14943           /* These are for Objective-C++ */
14944           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14945           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14946         /* The parameter-declaration-list is complete.  */
14947         break;
14948       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14949         {
14950           cp_token *token;
14951
14952           /* Peek at the next token.  */
14953           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14954           /* If it's an ellipsis, then the list is complete.  */
14955           if (token->type == CPP_ELLIPSIS)
14956             break;
14957           /* Otherwise, there must be more parameters.  Consume the
14958              `,'.  */
14959           cp_lexer_consume_token (parser->lexer);
14960           /* When parsing something like:
14961
14962                 int i(float f, double d)
14963
14964              we can tell after seeing the declaration for "f" that we
14965              are not looking at an initialization of a variable "i",
14966              but rather at the declaration of a function "i".
14967
14968              Due to the fact that the parsing of template arguments
14969              (as specified to a template-id) requires backtracking we
14970              cannot use this technique when inside a template argument
14971              list.  */
14972           if (!parser->in_template_argument_list_p
14973               && !parser->in_type_id_in_expr_p
14974               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14975               /* However, a parameter-declaration of the form
14976                  "foat(f)" (which is a valid declaration of a
14977                  parameter "f") can also be interpreted as an
14978                  expression (the conversion of "f" to "float").  */
14979               && !parenthesized_p)
14980             cp_parser_commit_to_tentative_parse (parser);
14981         }
14982       else
14983         {
14984           cp_parser_error (parser, "expected %<,%> or %<...%>");
14985           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14986             cp_parser_skip_to_closing_parenthesis (parser,
14987                                                    /*recovering=*/true,
14988                                                    /*or_comma=*/false,
14989                                                    /*consume_paren=*/false);
14990           break;
14991         }
14992     }
14993
14994   parser->in_unbraced_linkage_specification_p
14995     = saved_in_unbraced_linkage_specification_p;
14996
14997   return parameters;
14998 }
14999
15000 /* Parse a parameter declaration.
15001
15002    parameter-declaration:
15003      decl-specifier-seq ... [opt] declarator
15004      decl-specifier-seq declarator = assignment-expression
15005      decl-specifier-seq ... [opt] abstract-declarator [opt]
15006      decl-specifier-seq abstract-declarator [opt] = assignment-expression
15007
15008    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15009    declares a template parameter.  (In that case, a non-nested `>'
15010    token encountered during the parsing of the assignment-expression
15011    is not interpreted as a greater-than operator.)
15012
15013    Returns a representation of the parameter, or NULL if an error
15014    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15015    true iff the declarator is of the form "(p)".  */
15016
15017 static cp_parameter_declarator *
15018 cp_parser_parameter_declaration (cp_parser *parser,
15019                                  bool template_parm_p,
15020                                  bool *parenthesized_p)
15021 {
15022   int declares_class_or_enum;
15023   cp_decl_specifier_seq decl_specifiers;
15024   cp_declarator *declarator;
15025   tree default_argument;
15026   cp_token *token = NULL, *declarator_token_start = NULL;
15027   const char *saved_message;
15028
15029   /* In a template parameter, `>' is not an operator.
15030
15031      [temp.param]
15032
15033      When parsing a default template-argument for a non-type
15034      template-parameter, the first non-nested `>' is taken as the end
15035      of the template parameter-list rather than a greater-than
15036      operator.  */
15037
15038   /* Type definitions may not appear in parameter types.  */
15039   saved_message = parser->type_definition_forbidden_message;
15040   parser->type_definition_forbidden_message
15041     = G_("types may not be defined in parameter types");
15042
15043   /* Parse the declaration-specifiers.  */
15044   cp_parser_decl_specifier_seq (parser,
15045                                 CP_PARSER_FLAGS_NONE,
15046                                 &decl_specifiers,
15047                                 &declares_class_or_enum);
15048
15049   /* Complain about missing 'typename' or other invalid type names.  */
15050   if (!decl_specifiers.any_type_specifiers_p)
15051     cp_parser_parse_and_diagnose_invalid_type_name (parser);
15052
15053   /* If an error occurred, there's no reason to attempt to parse the
15054      rest of the declaration.  */
15055   if (cp_parser_error_occurred (parser))
15056     {
15057       parser->type_definition_forbidden_message = saved_message;
15058       return NULL;
15059     }
15060
15061   /* Peek at the next token.  */
15062   token = cp_lexer_peek_token (parser->lexer);
15063
15064   /* If the next token is a `)', `,', `=', `>', or `...', then there
15065      is no declarator. However, when variadic templates are enabled,
15066      there may be a declarator following `...'.  */
15067   if (token->type == CPP_CLOSE_PAREN
15068       || token->type == CPP_COMMA
15069       || token->type == CPP_EQ
15070       || token->type == CPP_GREATER)
15071     {
15072       declarator = NULL;
15073       if (parenthesized_p)
15074         *parenthesized_p = false;
15075     }
15076   /* Otherwise, there should be a declarator.  */
15077   else
15078     {
15079       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15080       parser->default_arg_ok_p = false;
15081
15082       /* After seeing a decl-specifier-seq, if the next token is not a
15083          "(", there is no possibility that the code is a valid
15084          expression.  Therefore, if parsing tentatively, we commit at
15085          this point.  */
15086       if (!parser->in_template_argument_list_p
15087           /* In an expression context, having seen:
15088
15089                (int((char ...
15090
15091              we cannot be sure whether we are looking at a
15092              function-type (taking a "char" as a parameter) or a cast
15093              of some object of type "char" to "int".  */
15094           && !parser->in_type_id_in_expr_p
15095           && cp_parser_uncommitted_to_tentative_parse_p (parser)
15096           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15097         cp_parser_commit_to_tentative_parse (parser);
15098       /* Parse the declarator.  */
15099       declarator_token_start = token;
15100       declarator = cp_parser_declarator (parser,
15101                                          CP_PARSER_DECLARATOR_EITHER,
15102                                          /*ctor_dtor_or_conv_p=*/NULL,
15103                                          parenthesized_p,
15104                                          /*member_p=*/false);
15105       parser->default_arg_ok_p = saved_default_arg_ok_p;
15106       /* After the declarator, allow more attributes.  */
15107       decl_specifiers.attributes
15108         = chainon (decl_specifiers.attributes,
15109                    cp_parser_attributes_opt (parser));
15110     }
15111
15112   /* If the next token is an ellipsis, and we have not seen a
15113      declarator name, and the type of the declarator contains parameter
15114      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15115      a parameter pack expansion expression. Otherwise, leave the
15116      ellipsis for a C-style variadic function. */
15117   token = cp_lexer_peek_token (parser->lexer);
15118   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15119     {
15120       tree type = decl_specifiers.type;
15121
15122       if (type && DECL_P (type))
15123         type = TREE_TYPE (type);
15124
15125       if (type
15126           && TREE_CODE (type) != TYPE_PACK_EXPANSION
15127           && declarator_can_be_parameter_pack (declarator)
15128           && (!declarator || !declarator->parameter_pack_p)
15129           && uses_parameter_packs (type))
15130         {
15131           /* Consume the `...'. */
15132           cp_lexer_consume_token (parser->lexer);
15133           maybe_warn_variadic_templates ();
15134           
15135           /* Build a pack expansion type */
15136           if (declarator)
15137             declarator->parameter_pack_p = true;
15138           else
15139             decl_specifiers.type = make_pack_expansion (type);
15140         }
15141     }
15142
15143   /* The restriction on defining new types applies only to the type
15144      of the parameter, not to the default argument.  */
15145   parser->type_definition_forbidden_message = saved_message;
15146
15147   /* If the next token is `=', then process a default argument.  */
15148   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15149     {
15150       /* Consume the `='.  */
15151       cp_lexer_consume_token (parser->lexer);
15152
15153       /* If we are defining a class, then the tokens that make up the
15154          default argument must be saved and processed later.  */
15155       if (!template_parm_p && at_class_scope_p ()
15156           && TYPE_BEING_DEFINED (current_class_type)
15157           && !LAMBDA_TYPE_P (current_class_type))
15158         {
15159           unsigned depth = 0;
15160           int maybe_template_id = 0;
15161           cp_token *first_token;
15162           cp_token *token;
15163
15164           /* Add tokens until we have processed the entire default
15165              argument.  We add the range [first_token, token).  */
15166           first_token = cp_lexer_peek_token (parser->lexer);
15167           while (true)
15168             {
15169               bool done = false;
15170
15171               /* Peek at the next token.  */
15172               token = cp_lexer_peek_token (parser->lexer);
15173               /* What we do depends on what token we have.  */
15174               switch (token->type)
15175                 {
15176                   /* In valid code, a default argument must be
15177                      immediately followed by a `,' `)', or `...'.  */
15178                 case CPP_COMMA:
15179                   if (depth == 0 && maybe_template_id)
15180                     {
15181                       /* If we've seen a '<', we might be in a
15182                          template-argument-list.  Until Core issue 325 is
15183                          resolved, we don't know how this situation ought
15184                          to be handled, so try to DTRT.  We check whether
15185                          what comes after the comma is a valid parameter
15186                          declaration list.  If it is, then the comma ends
15187                          the default argument; otherwise the default
15188                          argument continues.  */
15189                       bool error = false;
15190
15191                       /* Set ITALP so cp_parser_parameter_declaration_list
15192                          doesn't decide to commit to this parse.  */
15193                       bool saved_italp = parser->in_template_argument_list_p;
15194                       parser->in_template_argument_list_p = true;
15195
15196                       cp_parser_parse_tentatively (parser);
15197                       cp_lexer_consume_token (parser->lexer);
15198                       cp_parser_parameter_declaration_list (parser, &error);
15199                       if (!cp_parser_error_occurred (parser) && !error)
15200                         done = true;
15201                       cp_parser_abort_tentative_parse (parser);
15202
15203                       parser->in_template_argument_list_p = saved_italp;
15204                       break;
15205                     }
15206                 case CPP_CLOSE_PAREN:
15207                 case CPP_ELLIPSIS:
15208                   /* If we run into a non-nested `;', `}', or `]',
15209                      then the code is invalid -- but the default
15210                      argument is certainly over.  */
15211                 case CPP_SEMICOLON:
15212                 case CPP_CLOSE_BRACE:
15213                 case CPP_CLOSE_SQUARE:
15214                   if (depth == 0)
15215                     done = true;
15216                   /* Update DEPTH, if necessary.  */
15217                   else if (token->type == CPP_CLOSE_PAREN
15218                            || token->type == CPP_CLOSE_BRACE
15219                            || token->type == CPP_CLOSE_SQUARE)
15220                     --depth;
15221                   break;
15222
15223                 case CPP_OPEN_PAREN:
15224                 case CPP_OPEN_SQUARE:
15225                 case CPP_OPEN_BRACE:
15226                   ++depth;
15227                   break;
15228
15229                 case CPP_LESS:
15230                   if (depth == 0)
15231                     /* This might be the comparison operator, or it might
15232                        start a template argument list.  */
15233                     ++maybe_template_id;
15234                   break;
15235
15236                 case CPP_RSHIFT:
15237                   if (cxx_dialect == cxx98)
15238                     break;
15239                   /* Fall through for C++0x, which treats the `>>'
15240                      operator like two `>' tokens in certain
15241                      cases.  */
15242
15243                 case CPP_GREATER:
15244                   if (depth == 0)
15245                     {
15246                       /* This might be an operator, or it might close a
15247                          template argument list.  But if a previous '<'
15248                          started a template argument list, this will have
15249                          closed it, so we can't be in one anymore.  */
15250                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15251                       if (maybe_template_id < 0)
15252                         maybe_template_id = 0;
15253                     }
15254                   break;
15255
15256                   /* If we run out of tokens, issue an error message.  */
15257                 case CPP_EOF:
15258                 case CPP_PRAGMA_EOL:
15259                   error_at (token->location, "file ends in default argument");
15260                   done = true;
15261                   break;
15262
15263                 case CPP_NAME:
15264                 case CPP_SCOPE:
15265                   /* In these cases, we should look for template-ids.
15266                      For example, if the default argument is
15267                      `X<int, double>()', we need to do name lookup to
15268                      figure out whether or not `X' is a template; if
15269                      so, the `,' does not end the default argument.
15270
15271                      That is not yet done.  */
15272                   break;
15273
15274                 default:
15275                   break;
15276                 }
15277
15278               /* If we've reached the end, stop.  */
15279               if (done)
15280                 break;
15281
15282               /* Add the token to the token block.  */
15283               token = cp_lexer_consume_token (parser->lexer);
15284             }
15285
15286           /* Create a DEFAULT_ARG to represent the unparsed default
15287              argument.  */
15288           default_argument = make_node (DEFAULT_ARG);
15289           DEFARG_TOKENS (default_argument)
15290             = cp_token_cache_new (first_token, token);
15291           DEFARG_INSTANTIATIONS (default_argument) = NULL;
15292         }
15293       /* Outside of a class definition, we can just parse the
15294          assignment-expression.  */
15295       else
15296         {
15297           token = cp_lexer_peek_token (parser->lexer);
15298           default_argument 
15299             = cp_parser_default_argument (parser, template_parm_p);
15300         }
15301
15302       if (!parser->default_arg_ok_p)
15303         {
15304           if (flag_permissive)
15305             warning (0, "deprecated use of default argument for parameter of non-function");
15306           else
15307             {
15308               error_at (token->location,
15309                         "default arguments are only "
15310                         "permitted for function parameters");
15311               default_argument = NULL_TREE;
15312             }
15313         }
15314       else if ((declarator && declarator->parameter_pack_p)
15315                || (decl_specifiers.type
15316                    && PACK_EXPANSION_P (decl_specifiers.type)))
15317         {
15318           /* Find the name of the parameter pack.  */     
15319           cp_declarator *id_declarator = declarator;
15320           while (id_declarator && id_declarator->kind != cdk_id)
15321             id_declarator = id_declarator->declarator;
15322           
15323           if (id_declarator && id_declarator->kind == cdk_id)
15324             error_at (declarator_token_start->location,
15325                       template_parm_p 
15326                       ? "template parameter pack %qD"
15327                       " cannot have a default argument"
15328                       : "parameter pack %qD cannot have a default argument",
15329                       id_declarator->u.id.unqualified_name);
15330           else
15331             error_at (declarator_token_start->location,
15332                       template_parm_p 
15333                       ? "template parameter pack cannot have a default argument"
15334                       : "parameter pack cannot have a default argument");
15335           
15336           default_argument = NULL_TREE;
15337         }
15338     }
15339   else
15340     default_argument = NULL_TREE;
15341
15342   return make_parameter_declarator (&decl_specifiers,
15343                                     declarator,
15344                                     default_argument);
15345 }
15346
15347 /* Parse a default argument and return it.
15348
15349    TEMPLATE_PARM_P is true if this is a default argument for a
15350    non-type template parameter.  */
15351 static tree
15352 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15353 {
15354   tree default_argument = NULL_TREE;
15355   bool saved_greater_than_is_operator_p;
15356   bool saved_local_variables_forbidden_p;
15357
15358   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15359      set correctly.  */
15360   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15361   parser->greater_than_is_operator_p = !template_parm_p;
15362   /* Local variable names (and the `this' keyword) may not
15363      appear in a default argument.  */
15364   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15365   parser->local_variables_forbidden_p = true;
15366   /* Parse the assignment-expression.  */
15367   if (template_parm_p)
15368     push_deferring_access_checks (dk_no_deferred);
15369   default_argument
15370     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15371   if (template_parm_p)
15372     pop_deferring_access_checks ();
15373   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15374   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15375
15376   return default_argument;
15377 }
15378
15379 /* Parse a function-body.
15380
15381    function-body:
15382      compound_statement  */
15383
15384 static void
15385 cp_parser_function_body (cp_parser *parser)
15386 {
15387   cp_parser_compound_statement (parser, NULL, false);
15388 }
15389
15390 /* Parse a ctor-initializer-opt followed by a function-body.  Return
15391    true if a ctor-initializer was present.  */
15392
15393 static bool
15394 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15395 {
15396   tree body;
15397   bool ctor_initializer_p;
15398
15399   /* Begin the function body.  */
15400   body = begin_function_body ();
15401   /* Parse the optional ctor-initializer.  */
15402   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15403   /* Parse the function-body.  */
15404   cp_parser_function_body (parser);
15405   /* Finish the function body.  */
15406   finish_function_body (body);
15407
15408   return ctor_initializer_p;
15409 }
15410
15411 /* Parse an initializer.
15412
15413    initializer:
15414      = initializer-clause
15415      ( expression-list )
15416
15417    Returns an expression representing the initializer.  If no
15418    initializer is present, NULL_TREE is returned.
15419
15420    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15421    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
15422    set to TRUE if there is no initializer present.  If there is an
15423    initializer, and it is not a constant-expression, *NON_CONSTANT_P
15424    is set to true; otherwise it is set to false.  */
15425
15426 static tree
15427 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15428                        bool* non_constant_p)
15429 {
15430   cp_token *token;
15431   tree init;
15432
15433   /* Peek at the next token.  */
15434   token = cp_lexer_peek_token (parser->lexer);
15435
15436   /* Let our caller know whether or not this initializer was
15437      parenthesized.  */
15438   *is_direct_init = (token->type != CPP_EQ);
15439   /* Assume that the initializer is constant.  */
15440   *non_constant_p = false;
15441
15442   if (token->type == CPP_EQ)
15443     {
15444       /* Consume the `='.  */
15445       cp_lexer_consume_token (parser->lexer);
15446       /* Parse the initializer-clause.  */
15447       init = cp_parser_initializer_clause (parser, non_constant_p);
15448     }
15449   else if (token->type == CPP_OPEN_PAREN)
15450     {
15451       VEC(tree,gc) *vec;
15452       vec = cp_parser_parenthesized_expression_list (parser, false,
15453                                                      /*cast_p=*/false,
15454                                                      /*allow_expansion_p=*/true,
15455                                                      non_constant_p);
15456       if (vec == NULL)
15457         return error_mark_node;
15458       init = build_tree_list_vec (vec);
15459       release_tree_vector (vec);
15460     }
15461   else if (token->type == CPP_OPEN_BRACE)
15462     {
15463       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15464       init = cp_parser_braced_list (parser, non_constant_p);
15465       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15466     }
15467   else
15468     {
15469       /* Anything else is an error.  */
15470       cp_parser_error (parser, "expected initializer");
15471       init = error_mark_node;
15472     }
15473
15474   return init;
15475 }
15476
15477 /* Parse an initializer-clause.
15478
15479    initializer-clause:
15480      assignment-expression
15481      braced-init-list
15482
15483    Returns an expression representing the initializer.
15484
15485    If the `assignment-expression' production is used the value
15486    returned is simply a representation for the expression.
15487
15488    Otherwise, calls cp_parser_braced_list.  */
15489
15490 static tree
15491 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15492 {
15493   tree initializer;
15494
15495   /* Assume the expression is constant.  */
15496   *non_constant_p = false;
15497
15498   /* If it is not a `{', then we are looking at an
15499      assignment-expression.  */
15500   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15501     {
15502       initializer
15503         = cp_parser_constant_expression (parser,
15504                                         /*allow_non_constant_p=*/true,
15505                                         non_constant_p);
15506       if (!*non_constant_p)
15507         initializer = fold_non_dependent_expr (initializer);
15508     }
15509   else
15510     initializer = cp_parser_braced_list (parser, non_constant_p);
15511
15512   return initializer;
15513 }
15514
15515 /* Parse a brace-enclosed initializer list.
15516
15517    braced-init-list:
15518      { initializer-list , [opt] }
15519      { }
15520
15521    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
15522    the elements of the initializer-list (or NULL, if the last
15523    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
15524    NULL_TREE.  There is no way to detect whether or not the optional
15525    trailing `,' was provided.  NON_CONSTANT_P is as for
15526    cp_parser_initializer.  */     
15527
15528 static tree
15529 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15530 {
15531   tree initializer;
15532
15533   /* Consume the `{' token.  */
15534   cp_lexer_consume_token (parser->lexer);
15535   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
15536   initializer = make_node (CONSTRUCTOR);
15537   /* If it's not a `}', then there is a non-trivial initializer.  */
15538   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15539     {
15540       /* Parse the initializer list.  */
15541       CONSTRUCTOR_ELTS (initializer)
15542         = cp_parser_initializer_list (parser, non_constant_p);
15543       /* A trailing `,' token is allowed.  */
15544       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15545         cp_lexer_consume_token (parser->lexer);
15546     }
15547   /* Now, there should be a trailing `}'.  */
15548   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15549   TREE_TYPE (initializer) = init_list_type_node;
15550   return initializer;
15551 }
15552
15553 /* Parse an initializer-list.
15554
15555    initializer-list:
15556      initializer-clause ... [opt]
15557      initializer-list , initializer-clause ... [opt]
15558
15559    GNU Extension:
15560
15561    initializer-list:
15562      identifier : initializer-clause
15563      initializer-list, identifier : initializer-clause
15564
15565    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
15566    for the initializer.  If the INDEX of the elt is non-NULL, it is the
15567    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
15568    as for cp_parser_initializer.  */
15569
15570 static VEC(constructor_elt,gc) *
15571 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15572 {
15573   VEC(constructor_elt,gc) *v = NULL;
15574
15575   /* Assume all of the expressions are constant.  */
15576   *non_constant_p = false;
15577
15578   /* Parse the rest of the list.  */
15579   while (true)
15580     {
15581       cp_token *token;
15582       tree identifier;
15583       tree initializer;
15584       bool clause_non_constant_p;
15585
15586       /* If the next token is an identifier and the following one is a
15587          colon, we are looking at the GNU designated-initializer
15588          syntax.  */
15589       if (cp_parser_allow_gnu_extensions_p (parser)
15590           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15591           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15592         {
15593           /* Warn the user that they are using an extension.  */
15594           pedwarn (input_location, OPT_pedantic, 
15595                    "ISO C++ does not allow designated initializers");
15596           /* Consume the identifier.  */
15597           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15598           /* Consume the `:'.  */
15599           cp_lexer_consume_token (parser->lexer);
15600         }
15601       else
15602         identifier = NULL_TREE;
15603
15604       /* Parse the initializer.  */
15605       initializer = cp_parser_initializer_clause (parser,
15606                                                   &clause_non_constant_p);
15607       /* If any clause is non-constant, so is the entire initializer.  */
15608       if (clause_non_constant_p)
15609         *non_constant_p = true;
15610
15611       /* If we have an ellipsis, this is an initializer pack
15612          expansion.  */
15613       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15614         {
15615           /* Consume the `...'.  */
15616           cp_lexer_consume_token (parser->lexer);
15617
15618           /* Turn the initializer into an initializer expansion.  */
15619           initializer = make_pack_expansion (initializer);
15620         }
15621
15622       /* Add it to the vector.  */
15623       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15624
15625       /* If the next token is not a comma, we have reached the end of
15626          the list.  */
15627       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15628         break;
15629
15630       /* Peek at the next token.  */
15631       token = cp_lexer_peek_nth_token (parser->lexer, 2);
15632       /* If the next token is a `}', then we're still done.  An
15633          initializer-clause can have a trailing `,' after the
15634          initializer-list and before the closing `}'.  */
15635       if (token->type == CPP_CLOSE_BRACE)
15636         break;
15637
15638       /* Consume the `,' token.  */
15639       cp_lexer_consume_token (parser->lexer);
15640     }
15641
15642   return v;
15643 }
15644
15645 /* Classes [gram.class] */
15646
15647 /* Parse a class-name.
15648
15649    class-name:
15650      identifier
15651      template-id
15652
15653    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15654    to indicate that names looked up in dependent types should be
15655    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
15656    keyword has been used to indicate that the name that appears next
15657    is a template.  TAG_TYPE indicates the explicit tag given before
15658    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
15659    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
15660    is the class being defined in a class-head.
15661
15662    Returns the TYPE_DECL representing the class.  */
15663
15664 static tree
15665 cp_parser_class_name (cp_parser *parser,
15666                       bool typename_keyword_p,
15667                       bool template_keyword_p,
15668                       enum tag_types tag_type,
15669                       bool check_dependency_p,
15670                       bool class_head_p,
15671                       bool is_declaration)
15672 {
15673   tree decl;
15674   tree scope;
15675   bool typename_p;
15676   cp_token *token;
15677   tree identifier = NULL_TREE;
15678
15679   /* All class-names start with an identifier.  */
15680   token = cp_lexer_peek_token (parser->lexer);
15681   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15682     {
15683       cp_parser_error (parser, "expected class-name");
15684       return error_mark_node;
15685     }
15686
15687   /* PARSER->SCOPE can be cleared when parsing the template-arguments
15688      to a template-id, so we save it here.  */
15689   scope = parser->scope;
15690   if (scope == error_mark_node)
15691     return error_mark_node;
15692
15693   /* Any name names a type if we're following the `typename' keyword
15694      in a qualified name where the enclosing scope is type-dependent.  */
15695   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15696                 && dependent_type_p (scope));
15697   /* Handle the common case (an identifier, but not a template-id)
15698      efficiently.  */
15699   if (token->type == CPP_NAME
15700       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15701     {
15702       cp_token *identifier_token;
15703       bool ambiguous_p;
15704
15705       /* Look for the identifier.  */
15706       identifier_token = cp_lexer_peek_token (parser->lexer);
15707       ambiguous_p = identifier_token->ambiguous_p;
15708       identifier = cp_parser_identifier (parser);
15709       /* If the next token isn't an identifier, we are certainly not
15710          looking at a class-name.  */
15711       if (identifier == error_mark_node)
15712         decl = error_mark_node;
15713       /* If we know this is a type-name, there's no need to look it
15714          up.  */
15715       else if (typename_p)
15716         decl = identifier;
15717       else
15718         {
15719           tree ambiguous_decls;
15720           /* If we already know that this lookup is ambiguous, then
15721              we've already issued an error message; there's no reason
15722              to check again.  */
15723           if (ambiguous_p)
15724             {
15725               cp_parser_simulate_error (parser);
15726               return error_mark_node;
15727             }
15728           /* If the next token is a `::', then the name must be a type
15729              name.
15730
15731              [basic.lookup.qual]
15732
15733              During the lookup for a name preceding the :: scope
15734              resolution operator, object, function, and enumerator
15735              names are ignored.  */
15736           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15737             tag_type = typename_type;
15738           /* Look up the name.  */
15739           decl = cp_parser_lookup_name (parser, identifier,
15740                                         tag_type,
15741                                         /*is_template=*/false,
15742                                         /*is_namespace=*/false,
15743                                         check_dependency_p,
15744                                         &ambiguous_decls,
15745                                         identifier_token->location);
15746           if (ambiguous_decls)
15747             {
15748               if (cp_parser_parsing_tentatively (parser))
15749                 cp_parser_simulate_error (parser);
15750               return error_mark_node;
15751             }
15752         }
15753     }
15754   else
15755     {
15756       /* Try a template-id.  */
15757       decl = cp_parser_template_id (parser, template_keyword_p,
15758                                     check_dependency_p,
15759                                     is_declaration);
15760       if (decl == error_mark_node)
15761         return error_mark_node;
15762     }
15763
15764   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15765
15766   /* If this is a typename, create a TYPENAME_TYPE.  */
15767   if (typename_p && decl != error_mark_node)
15768     {
15769       decl = make_typename_type (scope, decl, typename_type,
15770                                  /*complain=*/tf_error);
15771       if (decl != error_mark_node)
15772         decl = TYPE_NAME (decl);
15773     }
15774
15775   /* Check to see that it is really the name of a class.  */
15776   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15777       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15778       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15779     /* Situations like this:
15780
15781          template <typename T> struct A {
15782            typename T::template X<int>::I i;
15783          };
15784
15785        are problematic.  Is `T::template X<int>' a class-name?  The
15786        standard does not seem to be definitive, but there is no other
15787        valid interpretation of the following `::'.  Therefore, those
15788        names are considered class-names.  */
15789     {
15790       decl = make_typename_type (scope, decl, tag_type, tf_error);
15791       if (decl != error_mark_node)
15792         decl = TYPE_NAME (decl);
15793     }
15794   else if (TREE_CODE (decl) != TYPE_DECL
15795            || TREE_TYPE (decl) == error_mark_node
15796            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15797     decl = error_mark_node;
15798
15799   if (decl == error_mark_node)
15800     cp_parser_error (parser, "expected class-name");
15801   else if (identifier && !parser->scope)
15802     maybe_note_name_used_in_class (identifier, decl);
15803
15804   return decl;
15805 }
15806
15807 /* Parse a class-specifier.
15808
15809    class-specifier:
15810      class-head { member-specification [opt] }
15811
15812    Returns the TREE_TYPE representing the class.  */
15813
15814 static tree
15815 cp_parser_class_specifier (cp_parser* parser)
15816 {
15817   tree type;
15818   tree attributes = NULL_TREE;
15819   bool nested_name_specifier_p;
15820   unsigned saved_num_template_parameter_lists;
15821   bool saved_in_function_body;
15822   bool saved_in_unbraced_linkage_specification_p;
15823   tree old_scope = NULL_TREE;
15824   tree scope = NULL_TREE;
15825   tree bases;
15826
15827   push_deferring_access_checks (dk_no_deferred);
15828
15829   /* Parse the class-head.  */
15830   type = cp_parser_class_head (parser,
15831                                &nested_name_specifier_p,
15832                                &attributes,
15833                                &bases);
15834   /* If the class-head was a semantic disaster, skip the entire body
15835      of the class.  */
15836   if (!type)
15837     {
15838       cp_parser_skip_to_end_of_block_or_statement (parser);
15839       pop_deferring_access_checks ();
15840       return error_mark_node;
15841     }
15842
15843   /* Look for the `{'.  */
15844   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15845     {
15846       pop_deferring_access_checks ();
15847       return error_mark_node;
15848     }
15849
15850   /* Process the base classes. If they're invalid, skip the 
15851      entire class body.  */
15852   if (!xref_basetypes (type, bases))
15853     {
15854       /* Consuming the closing brace yields better error messages
15855          later on.  */
15856       if (cp_parser_skip_to_closing_brace (parser))
15857         cp_lexer_consume_token (parser->lexer);
15858       pop_deferring_access_checks ();
15859       return error_mark_node;
15860     }
15861
15862   /* Issue an error message if type-definitions are forbidden here.  */
15863   cp_parser_check_type_definition (parser);
15864   /* Remember that we are defining one more class.  */
15865   ++parser->num_classes_being_defined;
15866   /* Inside the class, surrounding template-parameter-lists do not
15867      apply.  */
15868   saved_num_template_parameter_lists
15869     = parser->num_template_parameter_lists;
15870   parser->num_template_parameter_lists = 0;
15871   /* We are not in a function body.  */
15872   saved_in_function_body = parser->in_function_body;
15873   parser->in_function_body = false;
15874   /* We are not immediately inside an extern "lang" block.  */
15875   saved_in_unbraced_linkage_specification_p
15876     = parser->in_unbraced_linkage_specification_p;
15877   parser->in_unbraced_linkage_specification_p = false;
15878
15879   /* Start the class.  */
15880   if (nested_name_specifier_p)
15881     {
15882       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15883       old_scope = push_inner_scope (scope);
15884     }
15885   type = begin_class_definition (type, attributes);
15886
15887   if (type == error_mark_node)
15888     /* If the type is erroneous, skip the entire body of the class.  */
15889     cp_parser_skip_to_closing_brace (parser);
15890   else
15891     /* Parse the member-specification.  */
15892     cp_parser_member_specification_opt (parser);
15893
15894   /* Look for the trailing `}'.  */
15895   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15896   /* Look for trailing attributes to apply to this class.  */
15897   if (cp_parser_allow_gnu_extensions_p (parser))
15898     attributes = cp_parser_attributes_opt (parser);
15899   if (type != error_mark_node)
15900     type = finish_struct (type, attributes);
15901   if (nested_name_specifier_p)
15902     pop_inner_scope (old_scope, scope);
15903   /* If this class is not itself within the scope of another class,
15904      then we need to parse the bodies of all of the queued function
15905      definitions.  Note that the queued functions defined in a class
15906      are not always processed immediately following the
15907      class-specifier for that class.  Consider:
15908
15909        struct A {
15910          struct B { void f() { sizeof (A); } };
15911        };
15912
15913      If `f' were processed before the processing of `A' were
15914      completed, there would be no way to compute the size of `A'.
15915      Note that the nesting we are interested in here is lexical --
15916      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15917      for:
15918
15919        struct A { struct B; };
15920        struct A::B { void f() { } };
15921
15922      there is no need to delay the parsing of `A::B::f'.  */
15923   if (--parser->num_classes_being_defined == 0)
15924     {
15925       tree queue_entry;
15926       tree fn;
15927       tree class_type = NULL_TREE;
15928       tree pushed_scope = NULL_TREE;
15929
15930       /* In a first pass, parse default arguments to the functions.
15931          Then, in a second pass, parse the bodies of the functions.
15932          This two-phased approach handles cases like:
15933
15934             struct S {
15935               void f() { g(); }
15936               void g(int i = 3);
15937             };
15938
15939          */
15940       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15941              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15942            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15943            TREE_PURPOSE (parser->unparsed_functions_queues)
15944              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15945         {
15946           fn = TREE_VALUE (queue_entry);
15947           /* If there are default arguments that have not yet been processed,
15948              take care of them now.  */
15949           if (class_type != TREE_PURPOSE (queue_entry))
15950             {
15951               if (pushed_scope)
15952                 pop_scope (pushed_scope);
15953               class_type = TREE_PURPOSE (queue_entry);
15954               pushed_scope = push_scope (class_type);
15955             }
15956           /* Make sure that any template parameters are in scope.  */
15957           maybe_begin_member_template_processing (fn);
15958           /* Parse the default argument expressions.  */
15959           cp_parser_late_parsing_default_args (parser, fn);
15960           /* Remove any template parameters from the symbol table.  */
15961           maybe_end_member_template_processing ();
15962         }
15963       if (pushed_scope)
15964         pop_scope (pushed_scope);
15965       /* Now parse the body of the functions.  */
15966       for (TREE_VALUE (parser->unparsed_functions_queues)
15967              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15968            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15969            TREE_VALUE (parser->unparsed_functions_queues)
15970              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15971         {
15972           /* Figure out which function we need to process.  */
15973           fn = TREE_VALUE (queue_entry);
15974           /* Parse the function.  */
15975           cp_parser_late_parsing_for_member (parser, fn);
15976         }
15977     }
15978
15979   /* Put back any saved access checks.  */
15980   pop_deferring_access_checks ();
15981
15982   /* Restore saved state.  */
15983   parser->in_function_body = saved_in_function_body;
15984   parser->num_template_parameter_lists
15985     = saved_num_template_parameter_lists;
15986   parser->in_unbraced_linkage_specification_p
15987     = saved_in_unbraced_linkage_specification_p;
15988
15989   return type;
15990 }
15991
15992 /* Parse a class-head.
15993
15994    class-head:
15995      class-key identifier [opt] base-clause [opt]
15996      class-key nested-name-specifier identifier base-clause [opt]
15997      class-key nested-name-specifier [opt] template-id
15998        base-clause [opt]
15999
16000    GNU Extensions:
16001      class-key attributes identifier [opt] base-clause [opt]
16002      class-key attributes nested-name-specifier identifier base-clause [opt]
16003      class-key attributes nested-name-specifier [opt] template-id
16004        base-clause [opt]
16005
16006    Upon return BASES is initialized to the list of base classes (or
16007    NULL, if there are none) in the same form returned by
16008    cp_parser_base_clause.
16009
16010    Returns the TYPE of the indicated class.  Sets
16011    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16012    involving a nested-name-specifier was used, and FALSE otherwise.
16013
16014    Returns error_mark_node if this is not a class-head.
16015
16016    Returns NULL_TREE if the class-head is syntactically valid, but
16017    semantically invalid in a way that means we should skip the entire
16018    body of the class.  */
16019
16020 static tree
16021 cp_parser_class_head (cp_parser* parser,
16022                       bool* nested_name_specifier_p,
16023                       tree *attributes_p,
16024                       tree *bases)
16025 {
16026   tree nested_name_specifier;
16027   enum tag_types class_key;
16028   tree id = NULL_TREE;
16029   tree type = NULL_TREE;
16030   tree attributes;
16031   bool template_id_p = false;
16032   bool qualified_p = false;
16033   bool invalid_nested_name_p = false;
16034   bool invalid_explicit_specialization_p = false;
16035   tree pushed_scope = NULL_TREE;
16036   unsigned num_templates;
16037   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16038   /* Assume no nested-name-specifier will be present.  */
16039   *nested_name_specifier_p = false;
16040   /* Assume no template parameter lists will be used in defining the
16041      type.  */
16042   num_templates = 0;
16043
16044   *bases = NULL_TREE;
16045
16046   /* Look for the class-key.  */
16047   class_key = cp_parser_class_key (parser);
16048   if (class_key == none_type)
16049     return error_mark_node;
16050
16051   /* Parse the attributes.  */
16052   attributes = cp_parser_attributes_opt (parser);
16053
16054   /* If the next token is `::', that is invalid -- but sometimes
16055      people do try to write:
16056
16057        struct ::S {};
16058
16059      Handle this gracefully by accepting the extra qualifier, and then
16060      issuing an error about it later if this really is a
16061      class-head.  If it turns out just to be an elaborated type
16062      specifier, remain silent.  */
16063   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16064     qualified_p = true;
16065
16066   push_deferring_access_checks (dk_no_check);
16067
16068   /* Determine the name of the class.  Begin by looking for an
16069      optional nested-name-specifier.  */
16070   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16071   nested_name_specifier
16072     = cp_parser_nested_name_specifier_opt (parser,
16073                                            /*typename_keyword_p=*/false,
16074                                            /*check_dependency_p=*/false,
16075                                            /*type_p=*/false,
16076                                            /*is_declaration=*/false);
16077   /* If there was a nested-name-specifier, then there *must* be an
16078      identifier.  */
16079   if (nested_name_specifier)
16080     {
16081       type_start_token = cp_lexer_peek_token (parser->lexer);
16082       /* Although the grammar says `identifier', it really means
16083          `class-name' or `template-name'.  You are only allowed to
16084          define a class that has already been declared with this
16085          syntax.
16086
16087          The proposed resolution for Core Issue 180 says that wherever
16088          you see `class T::X' you should treat `X' as a type-name.
16089
16090          It is OK to define an inaccessible class; for example:
16091
16092            class A { class B; };
16093            class A::B {};
16094
16095          We do not know if we will see a class-name, or a
16096          template-name.  We look for a class-name first, in case the
16097          class-name is a template-id; if we looked for the
16098          template-name first we would stop after the template-name.  */
16099       cp_parser_parse_tentatively (parser);
16100       type = cp_parser_class_name (parser,
16101                                    /*typename_keyword_p=*/false,
16102                                    /*template_keyword_p=*/false,
16103                                    class_type,
16104                                    /*check_dependency_p=*/false,
16105                                    /*class_head_p=*/true,
16106                                    /*is_declaration=*/false);
16107       /* If that didn't work, ignore the nested-name-specifier.  */
16108       if (!cp_parser_parse_definitely (parser))
16109         {
16110           invalid_nested_name_p = true;
16111           type_start_token = cp_lexer_peek_token (parser->lexer);
16112           id = cp_parser_identifier (parser);
16113           if (id == error_mark_node)
16114             id = NULL_TREE;
16115         }
16116       /* If we could not find a corresponding TYPE, treat this
16117          declaration like an unqualified declaration.  */
16118       if (type == error_mark_node)
16119         nested_name_specifier = NULL_TREE;
16120       /* Otherwise, count the number of templates used in TYPE and its
16121          containing scopes.  */
16122       else
16123         {
16124           tree scope;
16125
16126           for (scope = TREE_TYPE (type);
16127                scope && TREE_CODE (scope) != NAMESPACE_DECL;
16128                scope = (TYPE_P (scope)
16129                         ? TYPE_CONTEXT (scope)
16130                         : DECL_CONTEXT (scope)))
16131             if (TYPE_P (scope)
16132                 && CLASS_TYPE_P (scope)
16133                 && CLASSTYPE_TEMPLATE_INFO (scope)
16134                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16135                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16136               ++num_templates;
16137         }
16138     }
16139   /* Otherwise, the identifier is optional.  */
16140   else
16141     {
16142       /* We don't know whether what comes next is a template-id,
16143          an identifier, or nothing at all.  */
16144       cp_parser_parse_tentatively (parser);
16145       /* Check for a template-id.  */
16146       type_start_token = cp_lexer_peek_token (parser->lexer);
16147       id = cp_parser_template_id (parser,
16148                                   /*template_keyword_p=*/false,
16149                                   /*check_dependency_p=*/true,
16150                                   /*is_declaration=*/true);
16151       /* If that didn't work, it could still be an identifier.  */
16152       if (!cp_parser_parse_definitely (parser))
16153         {
16154           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16155             {
16156               type_start_token = cp_lexer_peek_token (parser->lexer);
16157               id = cp_parser_identifier (parser);
16158             }
16159           else
16160             id = NULL_TREE;
16161         }
16162       else
16163         {
16164           template_id_p = true;
16165           ++num_templates;
16166         }
16167     }
16168
16169   pop_deferring_access_checks ();
16170
16171   if (id)
16172     cp_parser_check_for_invalid_template_id (parser, id,
16173                                              type_start_token->location);
16174
16175   /* If it's not a `:' or a `{' then we can't really be looking at a
16176      class-head, since a class-head only appears as part of a
16177      class-specifier.  We have to detect this situation before calling
16178      xref_tag, since that has irreversible side-effects.  */
16179   if (!cp_parser_next_token_starts_class_definition_p (parser))
16180     {
16181       cp_parser_error (parser, "expected %<{%> or %<:%>");
16182       return error_mark_node;
16183     }
16184
16185   /* At this point, we're going ahead with the class-specifier, even
16186      if some other problem occurs.  */
16187   cp_parser_commit_to_tentative_parse (parser);
16188   /* Issue the error about the overly-qualified name now.  */
16189   if (qualified_p)
16190     {
16191       cp_parser_error (parser,
16192                        "global qualification of class name is invalid");
16193       return error_mark_node;
16194     }
16195   else if (invalid_nested_name_p)
16196     {
16197       cp_parser_error (parser,
16198                        "qualified name does not name a class");
16199       return error_mark_node;
16200     }
16201   else if (nested_name_specifier)
16202     {
16203       tree scope;
16204
16205       /* Reject typedef-names in class heads.  */
16206       if (!DECL_IMPLICIT_TYPEDEF_P (type))
16207         {
16208           error_at (type_start_token->location,
16209                     "invalid class name in declaration of %qD",
16210                     type);
16211           type = NULL_TREE;
16212           goto done;
16213         }
16214
16215       /* Figure out in what scope the declaration is being placed.  */
16216       scope = current_scope ();
16217       /* If that scope does not contain the scope in which the
16218          class was originally declared, the program is invalid.  */
16219       if (scope && !is_ancestor (scope, nested_name_specifier))
16220         {
16221           if (at_namespace_scope_p ())
16222             error_at (type_start_token->location,
16223                       "declaration of %qD in namespace %qD which does not "
16224                       "enclose %qD",
16225                       type, scope, nested_name_specifier);
16226           else
16227             error_at (type_start_token->location,
16228                       "declaration of %qD in %qD which does not enclose %qD",
16229                       type, scope, nested_name_specifier);
16230           type = NULL_TREE;
16231           goto done;
16232         }
16233       /* [dcl.meaning]
16234
16235          A declarator-id shall not be qualified except for the
16236          definition of a ... nested class outside of its class
16237          ... [or] the definition or explicit instantiation of a
16238          class member of a namespace outside of its namespace.  */
16239       if (scope == nested_name_specifier)
16240         {
16241           permerror (nested_name_specifier_token_start->location,
16242                      "extra qualification not allowed");
16243           nested_name_specifier = NULL_TREE;
16244           num_templates = 0;
16245         }
16246     }
16247   /* An explicit-specialization must be preceded by "template <>".  If
16248      it is not, try to recover gracefully.  */
16249   if (at_namespace_scope_p ()
16250       && parser->num_template_parameter_lists == 0
16251       && template_id_p)
16252     {
16253       error_at (type_start_token->location,
16254                 "an explicit specialization must be preceded by %<template <>%>");
16255       invalid_explicit_specialization_p = true;
16256       /* Take the same action that would have been taken by
16257          cp_parser_explicit_specialization.  */
16258       ++parser->num_template_parameter_lists;
16259       begin_specialization ();
16260     }
16261   /* There must be no "return" statements between this point and the
16262      end of this function; set "type "to the correct return value and
16263      use "goto done;" to return.  */
16264   /* Make sure that the right number of template parameters were
16265      present.  */
16266   if (!cp_parser_check_template_parameters (parser, num_templates,
16267                                             type_start_token->location,
16268                                             /*declarator=*/NULL))
16269     {
16270       /* If something went wrong, there is no point in even trying to
16271          process the class-definition.  */
16272       type = NULL_TREE;
16273       goto done;
16274     }
16275
16276   /* Look up the type.  */
16277   if (template_id_p)
16278     {
16279       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16280           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16281               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16282         {
16283           error_at (type_start_token->location,
16284                     "function template %qD redeclared as a class template", id);
16285           type = error_mark_node;
16286         }
16287       else
16288         {
16289           type = TREE_TYPE (id);
16290           type = maybe_process_partial_specialization (type);
16291         }
16292       if (nested_name_specifier)
16293         pushed_scope = push_scope (nested_name_specifier);
16294     }
16295   else if (nested_name_specifier)
16296     {
16297       tree class_type;
16298
16299       /* Given:
16300
16301             template <typename T> struct S { struct T };
16302             template <typename T> struct S<T>::T { };
16303
16304          we will get a TYPENAME_TYPE when processing the definition of
16305          `S::T'.  We need to resolve it to the actual type before we
16306          try to define it.  */
16307       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16308         {
16309           class_type = resolve_typename_type (TREE_TYPE (type),
16310                                               /*only_current_p=*/false);
16311           if (TREE_CODE (class_type) != TYPENAME_TYPE)
16312             type = TYPE_NAME (class_type);
16313           else
16314             {
16315               cp_parser_error (parser, "could not resolve typename type");
16316               type = error_mark_node;
16317             }
16318         }
16319
16320       if (maybe_process_partial_specialization (TREE_TYPE (type))
16321           == error_mark_node)
16322         {
16323           type = NULL_TREE;
16324           goto done;
16325         }
16326
16327       class_type = current_class_type;
16328       /* Enter the scope indicated by the nested-name-specifier.  */
16329       pushed_scope = push_scope (nested_name_specifier);
16330       /* Get the canonical version of this type.  */
16331       type = TYPE_MAIN_DECL (TREE_TYPE (type));
16332       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16333           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16334         {
16335           type = push_template_decl (type);
16336           if (type == error_mark_node)
16337             {
16338               type = NULL_TREE;
16339               goto done;
16340             }
16341         }
16342
16343       type = TREE_TYPE (type);
16344       *nested_name_specifier_p = true;
16345     }
16346   else      /* The name is not a nested name.  */
16347     {
16348       /* If the class was unnamed, create a dummy name.  */
16349       if (!id)
16350         id = make_anon_name ();
16351       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16352                        parser->num_template_parameter_lists);
16353     }
16354
16355   /* Indicate whether this class was declared as a `class' or as a
16356      `struct'.  */
16357   if (TREE_CODE (type) == RECORD_TYPE)
16358     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16359   cp_parser_check_class_key (class_key, type);
16360
16361   /* If this type was already complete, and we see another definition,
16362      that's an error.  */
16363   if (type != error_mark_node && COMPLETE_TYPE_P (type))
16364     {
16365       error_at (type_start_token->location, "redefinition of %q#T",
16366                 type);
16367       error_at (type_start_token->location, "previous definition of %q+#T",
16368                 type);
16369       type = NULL_TREE;
16370       goto done;
16371     }
16372   else if (type == error_mark_node)
16373     type = NULL_TREE;
16374
16375   /* We will have entered the scope containing the class; the names of
16376      base classes should be looked up in that context.  For example:
16377
16378        struct A { struct B {}; struct C; };
16379        struct A::C : B {};
16380
16381      is valid.  */
16382
16383   /* Get the list of base-classes, if there is one.  */
16384   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16385     *bases = cp_parser_base_clause (parser);
16386
16387  done:
16388   /* Leave the scope given by the nested-name-specifier.  We will
16389      enter the class scope itself while processing the members.  */
16390   if (pushed_scope)
16391     pop_scope (pushed_scope);
16392
16393   if (invalid_explicit_specialization_p)
16394     {
16395       end_specialization ();
16396       --parser->num_template_parameter_lists;
16397     }
16398   *attributes_p = attributes;
16399   return type;
16400 }
16401
16402 /* Parse a class-key.
16403
16404    class-key:
16405      class
16406      struct
16407      union
16408
16409    Returns the kind of class-key specified, or none_type to indicate
16410    error.  */
16411
16412 static enum tag_types
16413 cp_parser_class_key (cp_parser* parser)
16414 {
16415   cp_token *token;
16416   enum tag_types tag_type;
16417
16418   /* Look for the class-key.  */
16419   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16420   if (!token)
16421     return none_type;
16422
16423   /* Check to see if the TOKEN is a class-key.  */
16424   tag_type = cp_parser_token_is_class_key (token);
16425   if (!tag_type)
16426     cp_parser_error (parser, "expected class-key");
16427   return tag_type;
16428 }
16429
16430 /* Parse an (optional) member-specification.
16431
16432    member-specification:
16433      member-declaration member-specification [opt]
16434      access-specifier : member-specification [opt]  */
16435
16436 static void
16437 cp_parser_member_specification_opt (cp_parser* parser)
16438 {
16439   while (true)
16440     {
16441       cp_token *token;
16442       enum rid keyword;
16443
16444       /* Peek at the next token.  */
16445       token = cp_lexer_peek_token (parser->lexer);
16446       /* If it's a `}', or EOF then we've seen all the members.  */
16447       if (token->type == CPP_CLOSE_BRACE
16448           || token->type == CPP_EOF
16449           || token->type == CPP_PRAGMA_EOL)
16450         break;
16451
16452       /* See if this token is a keyword.  */
16453       keyword = token->keyword;
16454       switch (keyword)
16455         {
16456         case RID_PUBLIC:
16457         case RID_PROTECTED:
16458         case RID_PRIVATE:
16459           /* Consume the access-specifier.  */
16460           cp_lexer_consume_token (parser->lexer);
16461           /* Remember which access-specifier is active.  */
16462           current_access_specifier = token->u.value;
16463           /* Look for the `:'.  */
16464           cp_parser_require (parser, CPP_COLON, "%<:%>");
16465           break;
16466
16467         default:
16468           /* Accept #pragmas at class scope.  */
16469           if (token->type == CPP_PRAGMA)
16470             {
16471               cp_parser_pragma (parser, pragma_external);
16472               break;
16473             }
16474
16475           /* Otherwise, the next construction must be a
16476              member-declaration.  */
16477           cp_parser_member_declaration (parser);
16478         }
16479     }
16480 }
16481
16482 /* Parse a member-declaration.
16483
16484    member-declaration:
16485      decl-specifier-seq [opt] member-declarator-list [opt] ;
16486      function-definition ; [opt]
16487      :: [opt] nested-name-specifier template [opt] unqualified-id ;
16488      using-declaration
16489      template-declaration
16490
16491    member-declarator-list:
16492      member-declarator
16493      member-declarator-list , member-declarator
16494
16495    member-declarator:
16496      declarator pure-specifier [opt]
16497      declarator constant-initializer [opt]
16498      identifier [opt] : constant-expression
16499
16500    GNU Extensions:
16501
16502    member-declaration:
16503      __extension__ member-declaration
16504
16505    member-declarator:
16506      declarator attributes [opt] pure-specifier [opt]
16507      declarator attributes [opt] constant-initializer [opt]
16508      identifier [opt] attributes [opt] : constant-expression  
16509
16510    C++0x Extensions:
16511
16512    member-declaration:
16513      static_assert-declaration  */
16514
16515 static void
16516 cp_parser_member_declaration (cp_parser* parser)
16517 {
16518   cp_decl_specifier_seq decl_specifiers;
16519   tree prefix_attributes;
16520   tree decl;
16521   int declares_class_or_enum;
16522   bool friend_p;
16523   cp_token *token = NULL;
16524   cp_token *decl_spec_token_start = NULL;
16525   cp_token *initializer_token_start = NULL;
16526   int saved_pedantic;
16527
16528   /* Check for the `__extension__' keyword.  */
16529   if (cp_parser_extension_opt (parser, &saved_pedantic))
16530     {
16531       /* Recurse.  */
16532       cp_parser_member_declaration (parser);
16533       /* Restore the old value of the PEDANTIC flag.  */
16534       pedantic = saved_pedantic;
16535
16536       return;
16537     }
16538
16539   /* Check for a template-declaration.  */
16540   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16541     {
16542       /* An explicit specialization here is an error condition, and we
16543          expect the specialization handler to detect and report this.  */
16544       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16545           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16546         cp_parser_explicit_specialization (parser);
16547       else
16548         cp_parser_template_declaration (parser, /*member_p=*/true);
16549
16550       return;
16551     }
16552
16553   /* Check for a using-declaration.  */
16554   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16555     {
16556       /* Parse the using-declaration.  */
16557       cp_parser_using_declaration (parser,
16558                                    /*access_declaration_p=*/false);
16559       return;
16560     }
16561
16562   /* Check for @defs.  */
16563   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16564     {
16565       tree ivar, member;
16566       tree ivar_chains = cp_parser_objc_defs_expression (parser);
16567       ivar = ivar_chains;
16568       while (ivar)
16569         {
16570           member = ivar;
16571           ivar = TREE_CHAIN (member);
16572           TREE_CHAIN (member) = NULL_TREE;
16573           finish_member_declaration (member);
16574         }
16575       return;
16576     }
16577
16578   /* If the next token is `static_assert' we have a static assertion.  */
16579   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16580     {
16581       cp_parser_static_assert (parser, /*member_p=*/true);
16582       return;
16583     }
16584
16585   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16586     return;
16587
16588   /* Parse the decl-specifier-seq.  */
16589   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16590   cp_parser_decl_specifier_seq (parser,
16591                                 CP_PARSER_FLAGS_OPTIONAL,
16592                                 &decl_specifiers,
16593                                 &declares_class_or_enum);
16594   prefix_attributes = decl_specifiers.attributes;
16595   decl_specifiers.attributes = NULL_TREE;
16596   /* Check for an invalid type-name.  */
16597   if (!decl_specifiers.any_type_specifiers_p
16598       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16599     return;
16600   /* If there is no declarator, then the decl-specifier-seq should
16601      specify a type.  */
16602   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16603     {
16604       /* If there was no decl-specifier-seq, and the next token is a
16605          `;', then we have something like:
16606
16607            struct S { ; };
16608
16609          [class.mem]
16610
16611          Each member-declaration shall declare at least one member
16612          name of the class.  */
16613       if (!decl_specifiers.any_specifiers_p)
16614         {
16615           cp_token *token = cp_lexer_peek_token (parser->lexer);
16616           if (!in_system_header_at (token->location))
16617             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16618         }
16619       else
16620         {
16621           tree type;
16622
16623           /* See if this declaration is a friend.  */
16624           friend_p = cp_parser_friend_p (&decl_specifiers);
16625           /* If there were decl-specifiers, check to see if there was
16626              a class-declaration.  */
16627           type = check_tag_decl (&decl_specifiers);
16628           /* Nested classes have already been added to the class, but
16629              a `friend' needs to be explicitly registered.  */
16630           if (friend_p)
16631             {
16632               /* If the `friend' keyword was present, the friend must
16633                  be introduced with a class-key.  */
16634                if (!declares_class_or_enum)
16635                  error_at (decl_spec_token_start->location,
16636                            "a class-key must be used when declaring a friend");
16637                /* In this case:
16638
16639                     template <typename T> struct A {
16640                       friend struct A<T>::B;
16641                     };
16642
16643                   A<T>::B will be represented by a TYPENAME_TYPE, and
16644                   therefore not recognized by check_tag_decl.  */
16645                if (!type
16646                    && decl_specifiers.type
16647                    && TYPE_P (decl_specifiers.type))
16648                  type = decl_specifiers.type;
16649                if (!type || !TYPE_P (type))
16650                  error_at (decl_spec_token_start->location,
16651                            "friend declaration does not name a class or "
16652                            "function");
16653                else
16654                  make_friend_class (current_class_type, type,
16655                                     /*complain=*/true);
16656             }
16657           /* If there is no TYPE, an error message will already have
16658              been issued.  */
16659           else if (!type || type == error_mark_node)
16660             ;
16661           /* An anonymous aggregate has to be handled specially; such
16662              a declaration really declares a data member (with a
16663              particular type), as opposed to a nested class.  */
16664           else if (ANON_AGGR_TYPE_P (type))
16665             {
16666               /* Remove constructors and such from TYPE, now that we
16667                  know it is an anonymous aggregate.  */
16668               fixup_anonymous_aggr (type);
16669               /* And make the corresponding data member.  */
16670               decl = build_decl (decl_spec_token_start->location,
16671                                  FIELD_DECL, NULL_TREE, type);
16672               /* Add it to the class.  */
16673               finish_member_declaration (decl);
16674             }
16675           else
16676             cp_parser_check_access_in_redeclaration
16677                                               (TYPE_NAME (type),
16678                                                decl_spec_token_start->location);
16679         }
16680     }
16681   else
16682     {
16683       /* See if these declarations will be friends.  */
16684       friend_p = cp_parser_friend_p (&decl_specifiers);
16685
16686       /* Keep going until we hit the `;' at the end of the
16687          declaration.  */
16688       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16689         {
16690           tree attributes = NULL_TREE;
16691           tree first_attribute;
16692
16693           /* Peek at the next token.  */
16694           token = cp_lexer_peek_token (parser->lexer);
16695
16696           /* Check for a bitfield declaration.  */
16697           if (token->type == CPP_COLON
16698               || (token->type == CPP_NAME
16699                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16700                   == CPP_COLON))
16701             {
16702               tree identifier;
16703               tree width;
16704
16705               /* Get the name of the bitfield.  Note that we cannot just
16706                  check TOKEN here because it may have been invalidated by
16707                  the call to cp_lexer_peek_nth_token above.  */
16708               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16709                 identifier = cp_parser_identifier (parser);
16710               else
16711                 identifier = NULL_TREE;
16712
16713               /* Consume the `:' token.  */
16714               cp_lexer_consume_token (parser->lexer);
16715               /* Get the width of the bitfield.  */
16716               width
16717                 = cp_parser_constant_expression (parser,
16718                                                  /*allow_non_constant=*/false,
16719                                                  NULL);
16720
16721               /* Look for attributes that apply to the bitfield.  */
16722               attributes = cp_parser_attributes_opt (parser);
16723               /* Remember which attributes are prefix attributes and
16724                  which are not.  */
16725               first_attribute = attributes;
16726               /* Combine the attributes.  */
16727               attributes = chainon (prefix_attributes, attributes);
16728
16729               /* Create the bitfield declaration.  */
16730               decl = grokbitfield (identifier
16731                                    ? make_id_declarator (NULL_TREE,
16732                                                          identifier,
16733                                                          sfk_none)
16734                                    : NULL,
16735                                    &decl_specifiers,
16736                                    width,
16737                                    attributes);
16738             }
16739           else
16740             {
16741               cp_declarator *declarator;
16742               tree initializer;
16743               tree asm_specification;
16744               int ctor_dtor_or_conv_p;
16745
16746               /* Parse the declarator.  */
16747               declarator
16748                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16749                                         &ctor_dtor_or_conv_p,
16750                                         /*parenthesized_p=*/NULL,
16751                                         /*member_p=*/true);
16752
16753               /* If something went wrong parsing the declarator, make sure
16754                  that we at least consume some tokens.  */
16755               if (declarator == cp_error_declarator)
16756                 {
16757                   /* Skip to the end of the statement.  */
16758                   cp_parser_skip_to_end_of_statement (parser);
16759                   /* If the next token is not a semicolon, that is
16760                      probably because we just skipped over the body of
16761                      a function.  So, we consume a semicolon if
16762                      present, but do not issue an error message if it
16763                      is not present.  */
16764                   if (cp_lexer_next_token_is (parser->lexer,
16765                                               CPP_SEMICOLON))
16766                     cp_lexer_consume_token (parser->lexer);
16767                   return;
16768                 }
16769
16770               if (declares_class_or_enum & 2)
16771                 cp_parser_check_for_definition_in_return_type
16772                                             (declarator, decl_specifiers.type,
16773                                              decl_specifiers.type_location);
16774
16775               /* Look for an asm-specification.  */
16776               asm_specification = cp_parser_asm_specification_opt (parser);
16777               /* Look for attributes that apply to the declaration.  */
16778               attributes = cp_parser_attributes_opt (parser);
16779               /* Remember which attributes are prefix attributes and
16780                  which are not.  */
16781               first_attribute = attributes;
16782               /* Combine the attributes.  */
16783               attributes = chainon (prefix_attributes, attributes);
16784
16785               /* If it's an `=', then we have a constant-initializer or a
16786                  pure-specifier.  It is not correct to parse the
16787                  initializer before registering the member declaration
16788                  since the member declaration should be in scope while
16789                  its initializer is processed.  However, the rest of the
16790                  front end does not yet provide an interface that allows
16791                  us to handle this correctly.  */
16792               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16793                 {
16794                   /* In [class.mem]:
16795
16796                      A pure-specifier shall be used only in the declaration of
16797                      a virtual function.
16798
16799                      A member-declarator can contain a constant-initializer
16800                      only if it declares a static member of integral or
16801                      enumeration type.
16802
16803                      Therefore, if the DECLARATOR is for a function, we look
16804                      for a pure-specifier; otherwise, we look for a
16805                      constant-initializer.  When we call `grokfield', it will
16806                      perform more stringent semantics checks.  */
16807                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
16808                   if (function_declarator_p (declarator))
16809                     initializer = cp_parser_pure_specifier (parser);
16810                   else
16811                     /* Parse the initializer.  */
16812                     initializer = cp_parser_constant_initializer (parser);
16813                 }
16814               /* Otherwise, there is no initializer.  */
16815               else
16816                 initializer = NULL_TREE;
16817
16818               /* See if we are probably looking at a function
16819                  definition.  We are certainly not looking at a
16820                  member-declarator.  Calling `grokfield' has
16821                  side-effects, so we must not do it unless we are sure
16822                  that we are looking at a member-declarator.  */
16823               if (cp_parser_token_starts_function_definition_p
16824                   (cp_lexer_peek_token (parser->lexer)))
16825                 {
16826                   /* The grammar does not allow a pure-specifier to be
16827                      used when a member function is defined.  (It is
16828                      possible that this fact is an oversight in the
16829                      standard, since a pure function may be defined
16830                      outside of the class-specifier.  */
16831                   if (initializer)
16832                     error_at (initializer_token_start->location,
16833                               "pure-specifier on function-definition");
16834                   decl = cp_parser_save_member_function_body (parser,
16835                                                               &decl_specifiers,
16836                                                               declarator,
16837                                                               attributes);
16838                   /* If the member was not a friend, declare it here.  */
16839                   if (!friend_p)
16840                     finish_member_declaration (decl);
16841                   /* Peek at the next token.  */
16842                   token = cp_lexer_peek_token (parser->lexer);
16843                   /* If the next token is a semicolon, consume it.  */
16844                   if (token->type == CPP_SEMICOLON)
16845                     cp_lexer_consume_token (parser->lexer);
16846                   return;
16847                 }
16848               else
16849                 if (declarator->kind == cdk_function)
16850                   declarator->id_loc = token->location;
16851                 /* Create the declaration.  */
16852                 decl = grokfield (declarator, &decl_specifiers,
16853                                   initializer, /*init_const_expr_p=*/true,
16854                                   asm_specification,
16855                                   attributes);
16856             }
16857
16858           /* Reset PREFIX_ATTRIBUTES.  */
16859           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16860             attributes = TREE_CHAIN (attributes);
16861           if (attributes)
16862             TREE_CHAIN (attributes) = NULL_TREE;
16863
16864           /* If there is any qualification still in effect, clear it
16865              now; we will be starting fresh with the next declarator.  */
16866           parser->scope = NULL_TREE;
16867           parser->qualifying_scope = NULL_TREE;
16868           parser->object_scope = NULL_TREE;
16869           /* If it's a `,', then there are more declarators.  */
16870           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16871             cp_lexer_consume_token (parser->lexer);
16872           /* If the next token isn't a `;', then we have a parse error.  */
16873           else if (cp_lexer_next_token_is_not (parser->lexer,
16874                                                CPP_SEMICOLON))
16875             {
16876               cp_parser_error (parser, "expected %<;%>");
16877               /* Skip tokens until we find a `;'.  */
16878               cp_parser_skip_to_end_of_statement (parser);
16879
16880               break;
16881             }
16882
16883           if (decl)
16884             {
16885               /* Add DECL to the list of members.  */
16886               if (!friend_p)
16887                 finish_member_declaration (decl);
16888
16889               if (TREE_CODE (decl) == FUNCTION_DECL)
16890                 cp_parser_save_default_args (parser, decl);
16891             }
16892         }
16893     }
16894
16895   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16896 }
16897
16898 /* Parse a pure-specifier.
16899
16900    pure-specifier:
16901      = 0
16902
16903    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16904    Otherwise, ERROR_MARK_NODE is returned.  */
16905
16906 static tree
16907 cp_parser_pure_specifier (cp_parser* parser)
16908 {
16909   cp_token *token;
16910
16911   /* Look for the `=' token.  */
16912   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16913     return error_mark_node;
16914   /* Look for the `0' token.  */
16915   token = cp_lexer_peek_token (parser->lexer);
16916
16917   if (token->type == CPP_EOF
16918       || token->type == CPP_PRAGMA_EOL)
16919     return error_mark_node;
16920
16921   cp_lexer_consume_token (parser->lexer);
16922
16923   /* Accept = default or = delete in c++0x mode.  */
16924   if (token->keyword == RID_DEFAULT
16925       || token->keyword == RID_DELETE)
16926     {
16927       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
16928       return token->u.value;
16929     }
16930
16931   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16932   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16933     {
16934       cp_parser_error (parser,
16935                        "invalid pure specifier (only %<= 0%> is allowed)");
16936       cp_parser_skip_to_end_of_statement (parser);
16937       return error_mark_node;
16938     }
16939   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16940     {
16941       error_at (token->location, "templates may not be %<virtual%>");
16942       return error_mark_node;
16943     }
16944
16945   return integer_zero_node;
16946 }
16947
16948 /* Parse a constant-initializer.
16949
16950    constant-initializer:
16951      = constant-expression
16952
16953    Returns a representation of the constant-expression.  */
16954
16955 static tree
16956 cp_parser_constant_initializer (cp_parser* parser)
16957 {
16958   /* Look for the `=' token.  */
16959   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16960     return error_mark_node;
16961
16962   /* It is invalid to write:
16963
16964        struct S { static const int i = { 7 }; };
16965
16966      */
16967   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16968     {
16969       cp_parser_error (parser,
16970                        "a brace-enclosed initializer is not allowed here");
16971       /* Consume the opening brace.  */
16972       cp_lexer_consume_token (parser->lexer);
16973       /* Skip the initializer.  */
16974       cp_parser_skip_to_closing_brace (parser);
16975       /* Look for the trailing `}'.  */
16976       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16977
16978       return error_mark_node;
16979     }
16980
16981   return cp_parser_constant_expression (parser,
16982                                         /*allow_non_constant=*/false,
16983                                         NULL);
16984 }
16985
16986 /* Derived classes [gram.class.derived] */
16987
16988 /* Parse a base-clause.
16989
16990    base-clause:
16991      : base-specifier-list
16992
16993    base-specifier-list:
16994      base-specifier ... [opt]
16995      base-specifier-list , base-specifier ... [opt]
16996
16997    Returns a TREE_LIST representing the base-classes, in the order in
16998    which they were declared.  The representation of each node is as
16999    described by cp_parser_base_specifier.
17000
17001    In the case that no bases are specified, this function will return
17002    NULL_TREE, not ERROR_MARK_NODE.  */
17003
17004 static tree
17005 cp_parser_base_clause (cp_parser* parser)
17006 {
17007   tree bases = NULL_TREE;
17008
17009   /* Look for the `:' that begins the list.  */
17010   cp_parser_require (parser, CPP_COLON, "%<:%>");
17011
17012   /* Scan the base-specifier-list.  */
17013   while (true)
17014     {
17015       cp_token *token;
17016       tree base;
17017       bool pack_expansion_p = false;
17018
17019       /* Look for the base-specifier.  */
17020       base = cp_parser_base_specifier (parser);
17021       /* Look for the (optional) ellipsis. */
17022       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17023         {
17024           /* Consume the `...'. */
17025           cp_lexer_consume_token (parser->lexer);
17026
17027           pack_expansion_p = true;
17028         }
17029
17030       /* Add BASE to the front of the list.  */
17031       if (base != error_mark_node)
17032         {
17033           if (pack_expansion_p)
17034             /* Make this a pack expansion type. */
17035             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17036           
17037
17038           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17039             {
17040               TREE_CHAIN (base) = bases;
17041               bases = base;
17042             }
17043         }
17044       /* Peek at the next token.  */
17045       token = cp_lexer_peek_token (parser->lexer);
17046       /* If it's not a comma, then the list is complete.  */
17047       if (token->type != CPP_COMMA)
17048         break;
17049       /* Consume the `,'.  */
17050       cp_lexer_consume_token (parser->lexer);
17051     }
17052
17053   /* PARSER->SCOPE may still be non-NULL at this point, if the last
17054      base class had a qualified name.  However, the next name that
17055      appears is certainly not qualified.  */
17056   parser->scope = NULL_TREE;
17057   parser->qualifying_scope = NULL_TREE;
17058   parser->object_scope = NULL_TREE;
17059
17060   return nreverse (bases);
17061 }
17062
17063 /* Parse a base-specifier.
17064
17065    base-specifier:
17066      :: [opt] nested-name-specifier [opt] class-name
17067      virtual access-specifier [opt] :: [opt] nested-name-specifier
17068        [opt] class-name
17069      access-specifier virtual [opt] :: [opt] nested-name-specifier
17070        [opt] class-name
17071
17072    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
17073    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17074    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
17075    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
17076
17077 static tree
17078 cp_parser_base_specifier (cp_parser* parser)
17079 {
17080   cp_token *token;
17081   bool done = false;
17082   bool virtual_p = false;
17083   bool duplicate_virtual_error_issued_p = false;
17084   bool duplicate_access_error_issued_p = false;
17085   bool class_scope_p, template_p;
17086   tree access = access_default_node;
17087   tree type;
17088
17089   /* Process the optional `virtual' and `access-specifier'.  */
17090   while (!done)
17091     {
17092       /* Peek at the next token.  */
17093       token = cp_lexer_peek_token (parser->lexer);
17094       /* Process `virtual'.  */
17095       switch (token->keyword)
17096         {
17097         case RID_VIRTUAL:
17098           /* If `virtual' appears more than once, issue an error.  */
17099           if (virtual_p && !duplicate_virtual_error_issued_p)
17100             {
17101               cp_parser_error (parser,
17102                                "%<virtual%> specified more than once in base-specified");
17103               duplicate_virtual_error_issued_p = true;
17104             }
17105
17106           virtual_p = true;
17107
17108           /* Consume the `virtual' token.  */
17109           cp_lexer_consume_token (parser->lexer);
17110
17111           break;
17112
17113         case RID_PUBLIC:
17114         case RID_PROTECTED:
17115         case RID_PRIVATE:
17116           /* If more than one access specifier appears, issue an
17117              error.  */
17118           if (access != access_default_node
17119               && !duplicate_access_error_issued_p)
17120             {
17121               cp_parser_error (parser,
17122                                "more than one access specifier in base-specified");
17123               duplicate_access_error_issued_p = true;
17124             }
17125
17126           access = ridpointers[(int) token->keyword];
17127
17128           /* Consume the access-specifier.  */
17129           cp_lexer_consume_token (parser->lexer);
17130
17131           break;
17132
17133         default:
17134           done = true;
17135           break;
17136         }
17137     }
17138   /* It is not uncommon to see programs mechanically, erroneously, use
17139      the 'typename' keyword to denote (dependent) qualified types
17140      as base classes.  */
17141   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17142     {
17143       token = cp_lexer_peek_token (parser->lexer);
17144       if (!processing_template_decl)
17145         error_at (token->location,
17146                   "keyword %<typename%> not allowed outside of templates");
17147       else
17148         error_at (token->location,
17149                   "keyword %<typename%> not allowed in this context "
17150                   "(the base class is implicitly a type)");
17151       cp_lexer_consume_token (parser->lexer);
17152     }
17153
17154   /* Look for the optional `::' operator.  */
17155   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17156   /* Look for the nested-name-specifier.  The simplest way to
17157      implement:
17158
17159        [temp.res]
17160
17161        The keyword `typename' is not permitted in a base-specifier or
17162        mem-initializer; in these contexts a qualified name that
17163        depends on a template-parameter is implicitly assumed to be a
17164        type name.
17165
17166      is to pretend that we have seen the `typename' keyword at this
17167      point.  */
17168   cp_parser_nested_name_specifier_opt (parser,
17169                                        /*typename_keyword_p=*/true,
17170                                        /*check_dependency_p=*/true,
17171                                        typename_type,
17172                                        /*is_declaration=*/true);
17173   /* If the base class is given by a qualified name, assume that names
17174      we see are type names or templates, as appropriate.  */
17175   class_scope_p = (parser->scope && TYPE_P (parser->scope));
17176   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17177
17178   /* Finally, look for the class-name.  */
17179   type = cp_parser_class_name (parser,
17180                                class_scope_p,
17181                                template_p,
17182                                typename_type,
17183                                /*check_dependency_p=*/true,
17184                                /*class_head_p=*/false,
17185                                /*is_declaration=*/true);
17186
17187   if (type == error_mark_node)
17188     return error_mark_node;
17189
17190   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17191 }
17192
17193 /* Exception handling [gram.exception] */
17194
17195 /* Parse an (optional) exception-specification.
17196
17197    exception-specification:
17198      throw ( type-id-list [opt] )
17199
17200    Returns a TREE_LIST representing the exception-specification.  The
17201    TREE_VALUE of each node is a type.  */
17202
17203 static tree
17204 cp_parser_exception_specification_opt (cp_parser* parser)
17205 {
17206   cp_token *token;
17207   tree type_id_list;
17208
17209   /* Peek at the next token.  */
17210   token = cp_lexer_peek_token (parser->lexer);
17211   /* If it's not `throw', then there's no exception-specification.  */
17212   if (!cp_parser_is_keyword (token, RID_THROW))
17213     return NULL_TREE;
17214
17215   /* Consume the `throw'.  */
17216   cp_lexer_consume_token (parser->lexer);
17217
17218   /* Look for the `('.  */
17219   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17220
17221   /* Peek at the next token.  */
17222   token = cp_lexer_peek_token (parser->lexer);
17223   /* If it's not a `)', then there is a type-id-list.  */
17224   if (token->type != CPP_CLOSE_PAREN)
17225     {
17226       const char *saved_message;
17227
17228       /* Types may not be defined in an exception-specification.  */
17229       saved_message = parser->type_definition_forbidden_message;
17230       parser->type_definition_forbidden_message
17231         = G_("types may not be defined in an exception-specification");
17232       /* Parse the type-id-list.  */
17233       type_id_list = cp_parser_type_id_list (parser);
17234       /* Restore the saved message.  */
17235       parser->type_definition_forbidden_message = saved_message;
17236     }
17237   else
17238     type_id_list = empty_except_spec;
17239
17240   /* Look for the `)'.  */
17241   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17242
17243   return type_id_list;
17244 }
17245
17246 /* Parse an (optional) type-id-list.
17247
17248    type-id-list:
17249      type-id ... [opt]
17250      type-id-list , type-id ... [opt]
17251
17252    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
17253    in the order that the types were presented.  */
17254
17255 static tree
17256 cp_parser_type_id_list (cp_parser* parser)
17257 {
17258   tree types = NULL_TREE;
17259
17260   while (true)
17261     {
17262       cp_token *token;
17263       tree type;
17264
17265       /* Get the next type-id.  */
17266       type = cp_parser_type_id (parser);
17267       /* Parse the optional ellipsis. */
17268       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17269         {
17270           /* Consume the `...'. */
17271           cp_lexer_consume_token (parser->lexer);
17272
17273           /* Turn the type into a pack expansion expression. */
17274           type = make_pack_expansion (type);
17275         }
17276       /* Add it to the list.  */
17277       types = add_exception_specifier (types, type, /*complain=*/1);
17278       /* Peek at the next token.  */
17279       token = cp_lexer_peek_token (parser->lexer);
17280       /* If it is not a `,', we are done.  */
17281       if (token->type != CPP_COMMA)
17282         break;
17283       /* Consume the `,'.  */
17284       cp_lexer_consume_token (parser->lexer);
17285     }
17286
17287   return nreverse (types);
17288 }
17289
17290 /* Parse a try-block.
17291
17292    try-block:
17293      try compound-statement handler-seq  */
17294
17295 static tree
17296 cp_parser_try_block (cp_parser* parser)
17297 {
17298   tree try_block;
17299
17300   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17301   try_block = begin_try_block ();
17302   cp_parser_compound_statement (parser, NULL, true);
17303   finish_try_block (try_block);
17304   cp_parser_handler_seq (parser);
17305   finish_handler_sequence (try_block);
17306
17307   return try_block;
17308 }
17309
17310 /* Parse a function-try-block.
17311
17312    function-try-block:
17313      try ctor-initializer [opt] function-body handler-seq  */
17314
17315 static bool
17316 cp_parser_function_try_block (cp_parser* parser)
17317 {
17318   tree compound_stmt;
17319   tree try_block;
17320   bool ctor_initializer_p;
17321
17322   /* Look for the `try' keyword.  */
17323   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17324     return false;
17325   /* Let the rest of the front end know where we are.  */
17326   try_block = begin_function_try_block (&compound_stmt);
17327   /* Parse the function-body.  */
17328   ctor_initializer_p
17329     = cp_parser_ctor_initializer_opt_and_function_body (parser);
17330   /* We're done with the `try' part.  */
17331   finish_function_try_block (try_block);
17332   /* Parse the handlers.  */
17333   cp_parser_handler_seq (parser);
17334   /* We're done with the handlers.  */
17335   finish_function_handler_sequence (try_block, compound_stmt);
17336
17337   return ctor_initializer_p;
17338 }
17339
17340 /* Parse a handler-seq.
17341
17342    handler-seq:
17343      handler handler-seq [opt]  */
17344
17345 static void
17346 cp_parser_handler_seq (cp_parser* parser)
17347 {
17348   while (true)
17349     {
17350       cp_token *token;
17351
17352       /* Parse the handler.  */
17353       cp_parser_handler (parser);
17354       /* Peek at the next token.  */
17355       token = cp_lexer_peek_token (parser->lexer);
17356       /* If it's not `catch' then there are no more handlers.  */
17357       if (!cp_parser_is_keyword (token, RID_CATCH))
17358         break;
17359     }
17360 }
17361
17362 /* Parse a handler.
17363
17364    handler:
17365      catch ( exception-declaration ) compound-statement  */
17366
17367 static void
17368 cp_parser_handler (cp_parser* parser)
17369 {
17370   tree handler;
17371   tree declaration;
17372
17373   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17374   handler = begin_handler ();
17375   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17376   declaration = cp_parser_exception_declaration (parser);
17377   finish_handler_parms (declaration, handler);
17378   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17379   cp_parser_compound_statement (parser, NULL, false);
17380   finish_handler (handler);
17381 }
17382
17383 /* Parse an exception-declaration.
17384
17385    exception-declaration:
17386      type-specifier-seq declarator
17387      type-specifier-seq abstract-declarator
17388      type-specifier-seq
17389      ...
17390
17391    Returns a VAR_DECL for the declaration, or NULL_TREE if the
17392    ellipsis variant is used.  */
17393
17394 static tree
17395 cp_parser_exception_declaration (cp_parser* parser)
17396 {
17397   cp_decl_specifier_seq type_specifiers;
17398   cp_declarator *declarator;
17399   const char *saved_message;
17400
17401   /* If it's an ellipsis, it's easy to handle.  */
17402   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17403     {
17404       /* Consume the `...' token.  */
17405       cp_lexer_consume_token (parser->lexer);
17406       return NULL_TREE;
17407     }
17408
17409   /* Types may not be defined in exception-declarations.  */
17410   saved_message = parser->type_definition_forbidden_message;
17411   parser->type_definition_forbidden_message
17412     = G_("types may not be defined in exception-declarations");
17413
17414   /* Parse the type-specifier-seq.  */
17415   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17416                                 /*is_trailing_return=*/false,
17417                                 &type_specifiers);
17418   /* If it's a `)', then there is no declarator.  */
17419   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17420     declarator = NULL;
17421   else
17422     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17423                                        /*ctor_dtor_or_conv_p=*/NULL,
17424                                        /*parenthesized_p=*/NULL,
17425                                        /*member_p=*/false);
17426
17427   /* Restore the saved message.  */
17428   parser->type_definition_forbidden_message = saved_message;
17429
17430   if (!type_specifiers.any_specifiers_p)
17431     return error_mark_node;
17432
17433   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17434 }
17435
17436 /* Parse a throw-expression.
17437
17438    throw-expression:
17439      throw assignment-expression [opt]
17440
17441    Returns a THROW_EXPR representing the throw-expression.  */
17442
17443 static tree
17444 cp_parser_throw_expression (cp_parser* parser)
17445 {
17446   tree expression;
17447   cp_token* token;
17448
17449   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17450   token = cp_lexer_peek_token (parser->lexer);
17451   /* Figure out whether or not there is an assignment-expression
17452      following the "throw" keyword.  */
17453   if (token->type == CPP_COMMA
17454       || token->type == CPP_SEMICOLON
17455       || token->type == CPP_CLOSE_PAREN
17456       || token->type == CPP_CLOSE_SQUARE
17457       || token->type == CPP_CLOSE_BRACE
17458       || token->type == CPP_COLON)
17459     expression = NULL_TREE;
17460   else
17461     expression = cp_parser_assignment_expression (parser,
17462                                                   /*cast_p=*/false, NULL);
17463
17464   return build_throw (expression);
17465 }
17466
17467 /* GNU Extensions */
17468
17469 /* Parse an (optional) asm-specification.
17470
17471    asm-specification:
17472      asm ( string-literal )
17473
17474    If the asm-specification is present, returns a STRING_CST
17475    corresponding to the string-literal.  Otherwise, returns
17476    NULL_TREE.  */
17477
17478 static tree
17479 cp_parser_asm_specification_opt (cp_parser* parser)
17480 {
17481   cp_token *token;
17482   tree asm_specification;
17483
17484   /* Peek at the next token.  */
17485   token = cp_lexer_peek_token (parser->lexer);
17486   /* If the next token isn't the `asm' keyword, then there's no
17487      asm-specification.  */
17488   if (!cp_parser_is_keyword (token, RID_ASM))
17489     return NULL_TREE;
17490
17491   /* Consume the `asm' token.  */
17492   cp_lexer_consume_token (parser->lexer);
17493   /* Look for the `('.  */
17494   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17495
17496   /* Look for the string-literal.  */
17497   asm_specification = cp_parser_string_literal (parser, false, false);
17498
17499   /* Look for the `)'.  */
17500   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17501
17502   return asm_specification;
17503 }
17504
17505 /* Parse an asm-operand-list.
17506
17507    asm-operand-list:
17508      asm-operand
17509      asm-operand-list , asm-operand
17510
17511    asm-operand:
17512      string-literal ( expression )
17513      [ string-literal ] string-literal ( expression )
17514
17515    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
17516    each node is the expression.  The TREE_PURPOSE is itself a
17517    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17518    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17519    is a STRING_CST for the string literal before the parenthesis. Returns
17520    ERROR_MARK_NODE if any of the operands are invalid.  */
17521
17522 static tree
17523 cp_parser_asm_operand_list (cp_parser* parser)
17524 {
17525   tree asm_operands = NULL_TREE;
17526   bool invalid_operands = false;
17527
17528   while (true)
17529     {
17530       tree string_literal;
17531       tree expression;
17532       tree name;
17533
17534       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17535         {
17536           /* Consume the `[' token.  */
17537           cp_lexer_consume_token (parser->lexer);
17538           /* Read the operand name.  */
17539           name = cp_parser_identifier (parser);
17540           if (name != error_mark_node)
17541             name = build_string (IDENTIFIER_LENGTH (name),
17542                                  IDENTIFIER_POINTER (name));
17543           /* Look for the closing `]'.  */
17544           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17545         }
17546       else
17547         name = NULL_TREE;
17548       /* Look for the string-literal.  */
17549       string_literal = cp_parser_string_literal (parser, false, false);
17550
17551       /* Look for the `('.  */
17552       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17553       /* Parse the expression.  */
17554       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17555       /* Look for the `)'.  */
17556       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17557
17558       if (name == error_mark_node 
17559           || string_literal == error_mark_node 
17560           || expression == error_mark_node)
17561         invalid_operands = true;
17562
17563       /* Add this operand to the list.  */
17564       asm_operands = tree_cons (build_tree_list (name, string_literal),
17565                                 expression,
17566                                 asm_operands);
17567       /* If the next token is not a `,', there are no more
17568          operands.  */
17569       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17570         break;
17571       /* Consume the `,'.  */
17572       cp_lexer_consume_token (parser->lexer);
17573     }
17574
17575   return invalid_operands ? error_mark_node : nreverse (asm_operands);
17576 }
17577
17578 /* Parse an asm-clobber-list.
17579
17580    asm-clobber-list:
17581      string-literal
17582      asm-clobber-list , string-literal
17583
17584    Returns a TREE_LIST, indicating the clobbers in the order that they
17585    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
17586
17587 static tree
17588 cp_parser_asm_clobber_list (cp_parser* parser)
17589 {
17590   tree clobbers = NULL_TREE;
17591
17592   while (true)
17593     {
17594       tree string_literal;
17595
17596       /* Look for the string literal.  */
17597       string_literal = cp_parser_string_literal (parser, false, false);
17598       /* Add it to the list.  */
17599       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17600       /* If the next token is not a `,', then the list is
17601          complete.  */
17602       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17603         break;
17604       /* Consume the `,' token.  */
17605       cp_lexer_consume_token (parser->lexer);
17606     }
17607
17608   return clobbers;
17609 }
17610
17611 /* Parse an asm-label-list.
17612
17613    asm-label-list:
17614      identifier
17615      asm-label-list , identifier
17616
17617    Returns a TREE_LIST, indicating the labels in the order that they
17618    appeared.  The TREE_VALUE of each node is a label.  */
17619
17620 static tree
17621 cp_parser_asm_label_list (cp_parser* parser)
17622 {
17623   tree labels = NULL_TREE;
17624
17625   while (true)
17626     {
17627       tree identifier, label, name;
17628
17629       /* Look for the identifier.  */
17630       identifier = cp_parser_identifier (parser);
17631       if (!error_operand_p (identifier))
17632         {
17633           label = lookup_label (identifier);
17634           if (TREE_CODE (label) == LABEL_DECL)
17635             {
17636               TREE_USED (label) = 1;
17637               check_goto (label);
17638               name = build_string (IDENTIFIER_LENGTH (identifier),
17639                                    IDENTIFIER_POINTER (identifier));
17640               labels = tree_cons (name, label, labels);
17641             }
17642         }
17643       /* If the next token is not a `,', then the list is
17644          complete.  */
17645       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17646         break;
17647       /* Consume the `,' token.  */
17648       cp_lexer_consume_token (parser->lexer);
17649     }
17650
17651   return nreverse (labels);
17652 }
17653
17654 /* Parse an (optional) series of attributes.
17655
17656    attributes:
17657      attributes attribute
17658
17659    attribute:
17660      __attribute__ (( attribute-list [opt] ))
17661
17662    The return value is as for cp_parser_attribute_list.  */
17663
17664 static tree
17665 cp_parser_attributes_opt (cp_parser* parser)
17666 {
17667   tree attributes = NULL_TREE;
17668
17669   while (true)
17670     {
17671       cp_token *token;
17672       tree attribute_list;
17673
17674       /* Peek at the next token.  */
17675       token = cp_lexer_peek_token (parser->lexer);
17676       /* If it's not `__attribute__', then we're done.  */
17677       if (token->keyword != RID_ATTRIBUTE)
17678         break;
17679
17680       /* Consume the `__attribute__' keyword.  */
17681       cp_lexer_consume_token (parser->lexer);
17682       /* Look for the two `(' tokens.  */
17683       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17684       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17685
17686       /* Peek at the next token.  */
17687       token = cp_lexer_peek_token (parser->lexer);
17688       if (token->type != CPP_CLOSE_PAREN)
17689         /* Parse the attribute-list.  */
17690         attribute_list = cp_parser_attribute_list (parser);
17691       else
17692         /* If the next token is a `)', then there is no attribute
17693            list.  */
17694         attribute_list = NULL;
17695
17696       /* Look for the two `)' tokens.  */
17697       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17698       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17699
17700       /* Add these new attributes to the list.  */
17701       attributes = chainon (attributes, attribute_list);
17702     }
17703
17704   return attributes;
17705 }
17706
17707 /* Parse an attribute-list.
17708
17709    attribute-list:
17710      attribute
17711      attribute-list , attribute
17712
17713    attribute:
17714      identifier
17715      identifier ( identifier )
17716      identifier ( identifier , expression-list )
17717      identifier ( expression-list )
17718
17719    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
17720    to an attribute.  The TREE_PURPOSE of each node is the identifier
17721    indicating which attribute is in use.  The TREE_VALUE represents
17722    the arguments, if any.  */
17723
17724 static tree
17725 cp_parser_attribute_list (cp_parser* parser)
17726 {
17727   tree attribute_list = NULL_TREE;
17728   bool save_translate_strings_p = parser->translate_strings_p;
17729
17730   parser->translate_strings_p = false;
17731   while (true)
17732     {
17733       cp_token *token;
17734       tree identifier;
17735       tree attribute;
17736
17737       /* Look for the identifier.  We also allow keywords here; for
17738          example `__attribute__ ((const))' is legal.  */
17739       token = cp_lexer_peek_token (parser->lexer);
17740       if (token->type == CPP_NAME
17741           || token->type == CPP_KEYWORD)
17742         {
17743           tree arguments = NULL_TREE;
17744
17745           /* Consume the token.  */
17746           token = cp_lexer_consume_token (parser->lexer);
17747
17748           /* Save away the identifier that indicates which attribute
17749              this is.  */
17750           identifier = (token->type == CPP_KEYWORD) 
17751             /* For keywords, use the canonical spelling, not the
17752                parsed identifier.  */
17753             ? ridpointers[(int) token->keyword]
17754             : token->u.value;
17755           
17756           attribute = build_tree_list (identifier, NULL_TREE);
17757
17758           /* Peek at the next token.  */
17759           token = cp_lexer_peek_token (parser->lexer);
17760           /* If it's an `(', then parse the attribute arguments.  */
17761           if (token->type == CPP_OPEN_PAREN)
17762             {
17763               VEC(tree,gc) *vec;
17764               vec = cp_parser_parenthesized_expression_list
17765                     (parser, true, /*cast_p=*/false,
17766                      /*allow_expansion_p=*/false,
17767                      /*non_constant_p=*/NULL);
17768               if (vec == NULL)
17769                 arguments = error_mark_node;
17770               else
17771                 {
17772                   arguments = build_tree_list_vec (vec);
17773                   release_tree_vector (vec);
17774                 }
17775               /* Save the arguments away.  */
17776               TREE_VALUE (attribute) = arguments;
17777             }
17778
17779           if (arguments != error_mark_node)
17780             {
17781               /* Add this attribute to the list.  */
17782               TREE_CHAIN (attribute) = attribute_list;
17783               attribute_list = attribute;
17784             }
17785
17786           token = cp_lexer_peek_token (parser->lexer);
17787         }
17788       /* Now, look for more attributes.  If the next token isn't a
17789          `,', we're done.  */
17790       if (token->type != CPP_COMMA)
17791         break;
17792
17793       /* Consume the comma and keep going.  */
17794       cp_lexer_consume_token (parser->lexer);
17795     }
17796   parser->translate_strings_p = save_translate_strings_p;
17797
17798   /* We built up the list in reverse order.  */
17799   return nreverse (attribute_list);
17800 }
17801
17802 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
17803    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
17804    current value of the PEDANTIC flag, regardless of whether or not
17805    the `__extension__' keyword is present.  The caller is responsible
17806    for restoring the value of the PEDANTIC flag.  */
17807
17808 static bool
17809 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17810 {
17811   /* Save the old value of the PEDANTIC flag.  */
17812   *saved_pedantic = pedantic;
17813
17814   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17815     {
17816       /* Consume the `__extension__' token.  */
17817       cp_lexer_consume_token (parser->lexer);
17818       /* We're not being pedantic while the `__extension__' keyword is
17819          in effect.  */
17820       pedantic = 0;
17821
17822       return true;
17823     }
17824
17825   return false;
17826 }
17827
17828 /* Parse a label declaration.
17829
17830    label-declaration:
17831      __label__ label-declarator-seq ;
17832
17833    label-declarator-seq:
17834      identifier , label-declarator-seq
17835      identifier  */
17836
17837 static void
17838 cp_parser_label_declaration (cp_parser* parser)
17839 {
17840   /* Look for the `__label__' keyword.  */
17841   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17842
17843   while (true)
17844     {
17845       tree identifier;
17846
17847       /* Look for an identifier.  */
17848       identifier = cp_parser_identifier (parser);
17849       /* If we failed, stop.  */
17850       if (identifier == error_mark_node)
17851         break;
17852       /* Declare it as a label.  */
17853       finish_label_decl (identifier);
17854       /* If the next token is a `;', stop.  */
17855       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17856         break;
17857       /* Look for the `,' separating the label declarations.  */
17858       cp_parser_require (parser, CPP_COMMA, "%<,%>");
17859     }
17860
17861   /* Look for the final `;'.  */
17862   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17863 }
17864
17865 /* Support Functions */
17866
17867 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17868    NAME should have one of the representations used for an
17869    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17870    is returned.  If PARSER->SCOPE is a dependent type, then a
17871    SCOPE_REF is returned.
17872
17873    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17874    returned; the name was already resolved when the TEMPLATE_ID_EXPR
17875    was formed.  Abstractly, such entities should not be passed to this
17876    function, because they do not need to be looked up, but it is
17877    simpler to check for this special case here, rather than at the
17878    call-sites.
17879
17880    In cases not explicitly covered above, this function returns a
17881    DECL, OVERLOAD, or baselink representing the result of the lookup.
17882    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17883    is returned.
17884
17885    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17886    (e.g., "struct") that was used.  In that case bindings that do not
17887    refer to types are ignored.
17888
17889    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17890    ignored.
17891
17892    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17893    are ignored.
17894
17895    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17896    types.
17897
17898    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17899    TREE_LIST of candidates if name-lookup results in an ambiguity, and
17900    NULL_TREE otherwise.  */
17901
17902 static tree
17903 cp_parser_lookup_name (cp_parser *parser, tree name,
17904                        enum tag_types tag_type,
17905                        bool is_template,
17906                        bool is_namespace,
17907                        bool check_dependency,
17908                        tree *ambiguous_decls,
17909                        location_t name_location)
17910 {
17911   int flags = 0;
17912   tree decl;
17913   tree object_type = parser->context->object_type;
17914
17915   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17916     flags |= LOOKUP_COMPLAIN;
17917
17918   /* Assume that the lookup will be unambiguous.  */
17919   if (ambiguous_decls)
17920     *ambiguous_decls = NULL_TREE;
17921
17922   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17923      no longer valid.  Note that if we are parsing tentatively, and
17924      the parse fails, OBJECT_TYPE will be automatically restored.  */
17925   parser->context->object_type = NULL_TREE;
17926
17927   if (name == error_mark_node)
17928     return error_mark_node;
17929
17930   /* A template-id has already been resolved; there is no lookup to
17931      do.  */
17932   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17933     return name;
17934   if (BASELINK_P (name))
17935     {
17936       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17937                   == TEMPLATE_ID_EXPR);
17938       return name;
17939     }
17940
17941   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17942      it should already have been checked to make sure that the name
17943      used matches the type being destroyed.  */
17944   if (TREE_CODE (name) == BIT_NOT_EXPR)
17945     {
17946       tree type;
17947
17948       /* Figure out to which type this destructor applies.  */
17949       if (parser->scope)
17950         type = parser->scope;
17951       else if (object_type)
17952         type = object_type;
17953       else
17954         type = current_class_type;
17955       /* If that's not a class type, there is no destructor.  */
17956       if (!type || !CLASS_TYPE_P (type))
17957         return error_mark_node;
17958       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17959         lazily_declare_fn (sfk_destructor, type);
17960       if (!CLASSTYPE_DESTRUCTORS (type))
17961           return error_mark_node;
17962       /* If it was a class type, return the destructor.  */
17963       return CLASSTYPE_DESTRUCTORS (type);
17964     }
17965
17966   /* By this point, the NAME should be an ordinary identifier.  If
17967      the id-expression was a qualified name, the qualifying scope is
17968      stored in PARSER->SCOPE at this point.  */
17969   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17970
17971   /* Perform the lookup.  */
17972   if (parser->scope)
17973     {
17974       bool dependent_p;
17975
17976       if (parser->scope == error_mark_node)
17977         return error_mark_node;
17978
17979       /* If the SCOPE is dependent, the lookup must be deferred until
17980          the template is instantiated -- unless we are explicitly
17981          looking up names in uninstantiated templates.  Even then, we
17982          cannot look up the name if the scope is not a class type; it
17983          might, for example, be a template type parameter.  */
17984       dependent_p = (TYPE_P (parser->scope)
17985                      && dependent_scope_p (parser->scope));
17986       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17987           && dependent_p)
17988         /* Defer lookup.  */
17989         decl = error_mark_node;
17990       else
17991         {
17992           tree pushed_scope = NULL_TREE;
17993
17994           /* If PARSER->SCOPE is a dependent type, then it must be a
17995              class type, and we must not be checking dependencies;
17996              otherwise, we would have processed this lookup above.  So
17997              that PARSER->SCOPE is not considered a dependent base by
17998              lookup_member, we must enter the scope here.  */
17999           if (dependent_p)
18000             pushed_scope = push_scope (parser->scope);
18001
18002           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
18003              lookup result and the nested-name-specifier nominates a class C:
18004                * if the name specified after the nested-name-specifier, when
18005                looked up in C, is the injected-class-name of C (Clause 9), or
18006                * if the name specified after the nested-name-specifier is the
18007                same as the identifier or the simple-template-id's template-
18008                name in the last component of the nested-name-specifier,
18009              the name is instead considered to name the constructor of
18010              class C. [ Note: for example, the constructor is not an
18011              acceptable lookup result in an elaborated-type-specifier so
18012              the constructor would not be used in place of the
18013              injected-class-name. --end note ] Such a constructor name
18014              shall be used only in the declarator-id of a declaration that
18015              names a constructor or in a using-declaration.  */
18016           if (tag_type == none_type
18017               && CLASS_TYPE_P (parser->scope)
18018               && constructor_name_p (name, parser->scope))
18019             name = ctor_identifier;
18020
18021           /* If the PARSER->SCOPE is a template specialization, it
18022              may be instantiated during name lookup.  In that case,
18023              errors may be issued.  Even if we rollback the current
18024              tentative parse, those errors are valid.  */
18025           decl = lookup_qualified_name (parser->scope, name,
18026                                         tag_type != none_type,
18027                                         /*complain=*/true);
18028
18029           /* If we have a single function from a using decl, pull it out.  */
18030           if (TREE_CODE (decl) == OVERLOAD
18031               && !really_overloaded_fn (decl))
18032             decl = OVL_FUNCTION (decl);
18033
18034           if (pushed_scope)
18035             pop_scope (pushed_scope);
18036         }
18037
18038       /* If the scope is a dependent type and either we deferred lookup or
18039          we did lookup but didn't find the name, rememeber the name.  */
18040       if (decl == error_mark_node && TYPE_P (parser->scope)
18041           && dependent_type_p (parser->scope))
18042         {
18043           if (tag_type)
18044             {
18045               tree type;
18046
18047               /* The resolution to Core Issue 180 says that `struct
18048                  A::B' should be considered a type-name, even if `A'
18049                  is dependent.  */
18050               type = make_typename_type (parser->scope, name, tag_type,
18051                                          /*complain=*/tf_error);
18052               decl = TYPE_NAME (type);
18053             }
18054           else if (is_template
18055                    && (cp_parser_next_token_ends_template_argument_p (parser)
18056                        || cp_lexer_next_token_is (parser->lexer,
18057                                                   CPP_CLOSE_PAREN)))
18058             decl = make_unbound_class_template (parser->scope,
18059                                                 name, NULL_TREE,
18060                                                 /*complain=*/tf_error);
18061           else
18062             decl = build_qualified_name (/*type=*/NULL_TREE,
18063                                          parser->scope, name,
18064                                          is_template);
18065         }
18066       parser->qualifying_scope = parser->scope;
18067       parser->object_scope = NULL_TREE;
18068     }
18069   else if (object_type)
18070     {
18071       tree object_decl = NULL_TREE;
18072       /* Look up the name in the scope of the OBJECT_TYPE, unless the
18073          OBJECT_TYPE is not a class.  */
18074       if (CLASS_TYPE_P (object_type))
18075         /* If the OBJECT_TYPE is a template specialization, it may
18076            be instantiated during name lookup.  In that case, errors
18077            may be issued.  Even if we rollback the current tentative
18078            parse, those errors are valid.  */
18079         object_decl = lookup_member (object_type,
18080                                      name,
18081                                      /*protect=*/0,
18082                                      tag_type != none_type);
18083       /* Look it up in the enclosing context, too.  */
18084       decl = lookup_name_real (name, tag_type != none_type,
18085                                /*nonclass=*/0,
18086                                /*block_p=*/true, is_namespace, flags);
18087       parser->object_scope = object_type;
18088       parser->qualifying_scope = NULL_TREE;
18089       if (object_decl)
18090         decl = object_decl;
18091     }
18092   else
18093     {
18094       decl = lookup_name_real (name, tag_type != none_type,
18095                                /*nonclass=*/0,
18096                                /*block_p=*/true, is_namespace, flags);
18097       parser->qualifying_scope = NULL_TREE;
18098       parser->object_scope = NULL_TREE;
18099     }
18100
18101   /* If the lookup failed, let our caller know.  */
18102   if (!decl || decl == error_mark_node)
18103     return error_mark_node;
18104
18105   /* Pull out the template from an injected-class-name (or multiple).  */
18106   if (is_template)
18107     decl = maybe_get_template_decl_from_type_decl (decl);
18108
18109   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
18110   if (TREE_CODE (decl) == TREE_LIST)
18111     {
18112       if (ambiguous_decls)
18113         *ambiguous_decls = decl;
18114       /* The error message we have to print is too complicated for
18115          cp_parser_error, so we incorporate its actions directly.  */
18116       if (!cp_parser_simulate_error (parser))
18117         {
18118           error_at (name_location, "reference to %qD is ambiguous",
18119                     name);
18120           print_candidates (decl);
18121         }
18122       return error_mark_node;
18123     }
18124
18125   gcc_assert (DECL_P (decl)
18126               || TREE_CODE (decl) == OVERLOAD
18127               || TREE_CODE (decl) == SCOPE_REF
18128               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18129               || BASELINK_P (decl));
18130
18131   /* If we have resolved the name of a member declaration, check to
18132      see if the declaration is accessible.  When the name resolves to
18133      set of overloaded functions, accessibility is checked when
18134      overload resolution is done.
18135
18136      During an explicit instantiation, access is not checked at all,
18137      as per [temp.explicit].  */
18138   if (DECL_P (decl))
18139     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18140
18141   return decl;
18142 }
18143
18144 /* Like cp_parser_lookup_name, but for use in the typical case where
18145    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18146    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
18147
18148 static tree
18149 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18150 {
18151   return cp_parser_lookup_name (parser, name,
18152                                 none_type,
18153                                 /*is_template=*/false,
18154                                 /*is_namespace=*/false,
18155                                 /*check_dependency=*/true,
18156                                 /*ambiguous_decls=*/NULL,
18157                                 location);
18158 }
18159
18160 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18161    the current context, return the TYPE_DECL.  If TAG_NAME_P is
18162    true, the DECL indicates the class being defined in a class-head,
18163    or declared in an elaborated-type-specifier.
18164
18165    Otherwise, return DECL.  */
18166
18167 static tree
18168 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18169 {
18170   /* If the TEMPLATE_DECL is being declared as part of a class-head,
18171      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18172
18173        struct A {
18174          template <typename T> struct B;
18175        };
18176
18177        template <typename T> struct A::B {};
18178
18179      Similarly, in an elaborated-type-specifier:
18180
18181        namespace N { struct X{}; }
18182
18183        struct A {
18184          template <typename T> friend struct N::X;
18185        };
18186
18187      However, if the DECL refers to a class type, and we are in
18188      the scope of the class, then the name lookup automatically
18189      finds the TYPE_DECL created by build_self_reference rather
18190      than a TEMPLATE_DECL.  For example, in:
18191
18192        template <class T> struct S {
18193          S s;
18194        };
18195
18196      there is no need to handle such case.  */
18197
18198   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18199     return DECL_TEMPLATE_RESULT (decl);
18200
18201   return decl;
18202 }
18203
18204 /* If too many, or too few, template-parameter lists apply to the
18205    declarator, issue an error message.  Returns TRUE if all went well,
18206    and FALSE otherwise.  */
18207
18208 static bool
18209 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18210                                                 cp_declarator *declarator,
18211                                                 location_t declarator_location)
18212 {
18213   unsigned num_templates;
18214
18215   /* We haven't seen any classes that involve template parameters yet.  */
18216   num_templates = 0;
18217
18218   switch (declarator->kind)
18219     {
18220     case cdk_id:
18221       if (declarator->u.id.qualifying_scope)
18222         {
18223           tree scope;
18224
18225           scope = declarator->u.id.qualifying_scope;
18226
18227           while (scope && CLASS_TYPE_P (scope))
18228             {
18229               /* You're supposed to have one `template <...>'
18230                  for every template class, but you don't need one
18231                  for a full specialization.  For example:
18232
18233                  template <class T> struct S{};
18234                  template <> struct S<int> { void f(); };
18235                  void S<int>::f () {}
18236
18237                  is correct; there shouldn't be a `template <>' for
18238                  the definition of `S<int>::f'.  */
18239               if (!CLASSTYPE_TEMPLATE_INFO (scope))
18240                 /* If SCOPE does not have template information of any
18241                    kind, then it is not a template, nor is it nested
18242                    within a template.  */
18243                 break;
18244               if (explicit_class_specialization_p (scope))
18245                 break;
18246               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18247                 ++num_templates;
18248
18249               scope = TYPE_CONTEXT (scope);
18250             }
18251         }
18252       else if (TREE_CODE (declarator->u.id.unqualified_name)
18253                == TEMPLATE_ID_EXPR)
18254         /* If the DECLARATOR has the form `X<y>' then it uses one
18255            additional level of template parameters.  */
18256         ++num_templates;
18257
18258       return cp_parser_check_template_parameters 
18259         (parser, num_templates, declarator_location, declarator);
18260
18261
18262     case cdk_function:
18263     case cdk_array:
18264     case cdk_pointer:
18265     case cdk_reference:
18266     case cdk_ptrmem:
18267       return (cp_parser_check_declarator_template_parameters
18268               (parser, declarator->declarator, declarator_location));
18269
18270     case cdk_error:
18271       return true;
18272
18273     default:
18274       gcc_unreachable ();
18275     }
18276   return false;
18277 }
18278
18279 /* NUM_TEMPLATES were used in the current declaration.  If that is
18280    invalid, return FALSE and issue an error messages.  Otherwise,
18281    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
18282    declarator and we can print more accurate diagnostics.  */
18283
18284 static bool
18285 cp_parser_check_template_parameters (cp_parser* parser,
18286                                      unsigned num_templates,
18287                                      location_t location,
18288                                      cp_declarator *declarator)
18289 {
18290   /* If there are the same number of template classes and parameter
18291      lists, that's OK.  */
18292   if (parser->num_template_parameter_lists == num_templates)
18293     return true;
18294   /* If there are more, but only one more, then we are referring to a
18295      member template.  That's OK too.  */
18296   if (parser->num_template_parameter_lists == num_templates + 1)
18297     return true;
18298   /* If there are more template classes than parameter lists, we have
18299      something like:
18300
18301        template <class T> void S<T>::R<T>::f ();  */
18302   if (parser->num_template_parameter_lists < num_templates)
18303     {
18304       if (declarator && !current_function_decl)
18305         error_at (location, "specializing member %<%T::%E%> "
18306                   "requires %<template<>%> syntax", 
18307                   declarator->u.id.qualifying_scope,
18308                   declarator->u.id.unqualified_name);
18309       else if (declarator)
18310         error_at (location, "invalid declaration of %<%T::%E%>",
18311                   declarator->u.id.qualifying_scope,
18312                   declarator->u.id.unqualified_name);
18313       else 
18314         error_at (location, "too few template-parameter-lists");
18315       return false;
18316     }
18317   /* Otherwise, there are too many template parameter lists.  We have
18318      something like:
18319
18320      template <class T> template <class U> void S::f();  */
18321   error_at (location, "too many template-parameter-lists");
18322   return false;
18323 }
18324
18325 /* Parse an optional `::' token indicating that the following name is
18326    from the global namespace.  If so, PARSER->SCOPE is set to the
18327    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18328    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18329    Returns the new value of PARSER->SCOPE, if the `::' token is
18330    present, and NULL_TREE otherwise.  */
18331
18332 static tree
18333 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18334 {
18335   cp_token *token;
18336
18337   /* Peek at the next token.  */
18338   token = cp_lexer_peek_token (parser->lexer);
18339   /* If we're looking at a `::' token then we're starting from the
18340      global namespace, not our current location.  */
18341   if (token->type == CPP_SCOPE)
18342     {
18343       /* Consume the `::' token.  */
18344       cp_lexer_consume_token (parser->lexer);
18345       /* Set the SCOPE so that we know where to start the lookup.  */
18346       parser->scope = global_namespace;
18347       parser->qualifying_scope = global_namespace;
18348       parser->object_scope = NULL_TREE;
18349
18350       return parser->scope;
18351     }
18352   else if (!current_scope_valid_p)
18353     {
18354       parser->scope = NULL_TREE;
18355       parser->qualifying_scope = NULL_TREE;
18356       parser->object_scope = NULL_TREE;
18357     }
18358
18359   return NULL_TREE;
18360 }
18361
18362 /* Returns TRUE if the upcoming token sequence is the start of a
18363    constructor declarator.  If FRIEND_P is true, the declarator is
18364    preceded by the `friend' specifier.  */
18365
18366 static bool
18367 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18368 {
18369   bool constructor_p;
18370   tree nested_name_specifier;
18371   cp_token *next_token;
18372
18373   /* The common case is that this is not a constructor declarator, so
18374      try to avoid doing lots of work if at all possible.  It's not
18375      valid declare a constructor at function scope.  */
18376   if (parser->in_function_body)
18377     return false;
18378   /* And only certain tokens can begin a constructor declarator.  */
18379   next_token = cp_lexer_peek_token (parser->lexer);
18380   if (next_token->type != CPP_NAME
18381       && next_token->type != CPP_SCOPE
18382       && next_token->type != CPP_NESTED_NAME_SPECIFIER
18383       && next_token->type != CPP_TEMPLATE_ID)
18384     return false;
18385
18386   /* Parse tentatively; we are going to roll back all of the tokens
18387      consumed here.  */
18388   cp_parser_parse_tentatively (parser);
18389   /* Assume that we are looking at a constructor declarator.  */
18390   constructor_p = true;
18391
18392   /* Look for the optional `::' operator.  */
18393   cp_parser_global_scope_opt (parser,
18394                               /*current_scope_valid_p=*/false);
18395   /* Look for the nested-name-specifier.  */
18396   nested_name_specifier
18397     = (cp_parser_nested_name_specifier_opt (parser,
18398                                             /*typename_keyword_p=*/false,
18399                                             /*check_dependency_p=*/false,
18400                                             /*type_p=*/false,
18401                                             /*is_declaration=*/false));
18402   /* Outside of a class-specifier, there must be a
18403      nested-name-specifier.  */
18404   if (!nested_name_specifier &&
18405       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18406        || friend_p))
18407     constructor_p = false;
18408   else if (nested_name_specifier == error_mark_node)
18409     constructor_p = false;
18410
18411   /* If we have a class scope, this is easy; DR 147 says that S::S always
18412      names the constructor, and no other qualified name could.  */
18413   if (constructor_p && nested_name_specifier
18414       && TYPE_P (nested_name_specifier))
18415     {
18416       tree id = cp_parser_unqualified_id (parser,
18417                                           /*template_keyword_p=*/false,
18418                                           /*check_dependency_p=*/false,
18419                                           /*declarator_p=*/true,
18420                                           /*optional_p=*/false);
18421       if (is_overloaded_fn (id))
18422         id = DECL_NAME (get_first_fn (id));
18423       if (!constructor_name_p (id, nested_name_specifier))
18424         constructor_p = false;
18425     }
18426   /* If we still think that this might be a constructor-declarator,
18427      look for a class-name.  */
18428   else if (constructor_p)
18429     {
18430       /* If we have:
18431
18432            template <typename T> struct S {
18433              S();
18434            };
18435
18436          we must recognize that the nested `S' names a class.  */
18437       tree type_decl;
18438       type_decl = cp_parser_class_name (parser,
18439                                         /*typename_keyword_p=*/false,
18440                                         /*template_keyword_p=*/false,
18441                                         none_type,
18442                                         /*check_dependency_p=*/false,
18443                                         /*class_head_p=*/false,
18444                                         /*is_declaration=*/false);
18445       /* If there was no class-name, then this is not a constructor.  */
18446       constructor_p = !cp_parser_error_occurred (parser);
18447
18448       /* If we're still considering a constructor, we have to see a `(',
18449          to begin the parameter-declaration-clause, followed by either a
18450          `)', an `...', or a decl-specifier.  We need to check for a
18451          type-specifier to avoid being fooled into thinking that:
18452
18453            S (f) (int);
18454
18455          is a constructor.  (It is actually a function named `f' that
18456          takes one parameter (of type `int') and returns a value of type
18457          `S'.  */
18458       if (constructor_p
18459           && !cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18460         constructor_p = false;
18461
18462       if (constructor_p
18463           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18464           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18465           /* A parameter declaration begins with a decl-specifier,
18466              which is either the "attribute" keyword, a storage class
18467              specifier, or (usually) a type-specifier.  */
18468           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18469         {
18470           tree type;
18471           tree pushed_scope = NULL_TREE;
18472           unsigned saved_num_template_parameter_lists;
18473
18474           /* Names appearing in the type-specifier should be looked up
18475              in the scope of the class.  */
18476           if (current_class_type)
18477             type = NULL_TREE;
18478           else
18479             {
18480               type = TREE_TYPE (type_decl);
18481               if (TREE_CODE (type) == TYPENAME_TYPE)
18482                 {
18483                   type = resolve_typename_type (type,
18484                                                 /*only_current_p=*/false);
18485                   if (TREE_CODE (type) == TYPENAME_TYPE)
18486                     {
18487                       cp_parser_abort_tentative_parse (parser);
18488                       return false;
18489                     }
18490                 }
18491               pushed_scope = push_scope (type);
18492             }
18493
18494           /* Inside the constructor parameter list, surrounding
18495              template-parameter-lists do not apply.  */
18496           saved_num_template_parameter_lists
18497             = parser->num_template_parameter_lists;
18498           parser->num_template_parameter_lists = 0;
18499
18500           /* Look for the type-specifier.  */
18501           cp_parser_type_specifier (parser,
18502                                     CP_PARSER_FLAGS_NONE,
18503                                     /*decl_specs=*/NULL,
18504                                     /*is_declarator=*/true,
18505                                     /*declares_class_or_enum=*/NULL,
18506                                     /*is_cv_qualifier=*/NULL);
18507
18508           parser->num_template_parameter_lists
18509             = saved_num_template_parameter_lists;
18510
18511           /* Leave the scope of the class.  */
18512           if (pushed_scope)
18513             pop_scope (pushed_scope);
18514
18515           constructor_p = !cp_parser_error_occurred (parser);
18516         }
18517     }
18518
18519   /* We did not really want to consume any tokens.  */
18520   cp_parser_abort_tentative_parse (parser);
18521
18522   return constructor_p;
18523 }
18524
18525 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18526    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
18527    they must be performed once we are in the scope of the function.
18528
18529    Returns the function defined.  */
18530
18531 static tree
18532 cp_parser_function_definition_from_specifiers_and_declarator
18533   (cp_parser* parser,
18534    cp_decl_specifier_seq *decl_specifiers,
18535    tree attributes,
18536    const cp_declarator *declarator)
18537 {
18538   tree fn;
18539   bool success_p;
18540
18541   /* Begin the function-definition.  */
18542   success_p = start_function (decl_specifiers, declarator, attributes);
18543
18544   /* The things we're about to see are not directly qualified by any
18545      template headers we've seen thus far.  */
18546   reset_specialization ();
18547
18548   /* If there were names looked up in the decl-specifier-seq that we
18549      did not check, check them now.  We must wait until we are in the
18550      scope of the function to perform the checks, since the function
18551      might be a friend.  */
18552   perform_deferred_access_checks ();
18553
18554   if (!success_p)
18555     {
18556       /* Skip the entire function.  */
18557       cp_parser_skip_to_end_of_block_or_statement (parser);
18558       fn = error_mark_node;
18559     }
18560   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18561     {
18562       /* Seen already, skip it.  An error message has already been output.  */
18563       cp_parser_skip_to_end_of_block_or_statement (parser);
18564       fn = current_function_decl;
18565       current_function_decl = NULL_TREE;
18566       /* If this is a function from a class, pop the nested class.  */
18567       if (current_class_name)
18568         pop_nested_class ();
18569     }
18570   else
18571     fn = cp_parser_function_definition_after_declarator (parser,
18572                                                          /*inline_p=*/false);
18573
18574   return fn;
18575 }
18576
18577 /* Parse the part of a function-definition that follows the
18578    declarator.  INLINE_P is TRUE iff this function is an inline
18579    function defined within a class-specifier.
18580
18581    Returns the function defined.  */
18582
18583 static tree
18584 cp_parser_function_definition_after_declarator (cp_parser* parser,
18585                                                 bool inline_p)
18586 {
18587   tree fn;
18588   bool ctor_initializer_p = false;
18589   bool saved_in_unbraced_linkage_specification_p;
18590   bool saved_in_function_body;
18591   unsigned saved_num_template_parameter_lists;
18592   cp_token *token;
18593
18594   saved_in_function_body = parser->in_function_body;
18595   parser->in_function_body = true;
18596   /* If the next token is `return', then the code may be trying to
18597      make use of the "named return value" extension that G++ used to
18598      support.  */
18599   token = cp_lexer_peek_token (parser->lexer);
18600   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18601     {
18602       /* Consume the `return' keyword.  */
18603       cp_lexer_consume_token (parser->lexer);
18604       /* Look for the identifier that indicates what value is to be
18605          returned.  */
18606       cp_parser_identifier (parser);
18607       /* Issue an error message.  */
18608       error_at (token->location,
18609                 "named return values are no longer supported");
18610       /* Skip tokens until we reach the start of the function body.  */
18611       while (true)
18612         {
18613           cp_token *token = cp_lexer_peek_token (parser->lexer);
18614           if (token->type == CPP_OPEN_BRACE
18615               || token->type == CPP_EOF
18616               || token->type == CPP_PRAGMA_EOL)
18617             break;
18618           cp_lexer_consume_token (parser->lexer);
18619         }
18620     }
18621   /* The `extern' in `extern "C" void f () { ... }' does not apply to
18622      anything declared inside `f'.  */
18623   saved_in_unbraced_linkage_specification_p
18624     = parser->in_unbraced_linkage_specification_p;
18625   parser->in_unbraced_linkage_specification_p = false;
18626   /* Inside the function, surrounding template-parameter-lists do not
18627      apply.  */
18628   saved_num_template_parameter_lists
18629     = parser->num_template_parameter_lists;
18630   parser->num_template_parameter_lists = 0;
18631
18632   start_lambda_scope (current_function_decl);
18633
18634   /* If the next token is `try', then we are looking at a
18635      function-try-block.  */
18636   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18637     ctor_initializer_p = cp_parser_function_try_block (parser);
18638   /* A function-try-block includes the function-body, so we only do
18639      this next part if we're not processing a function-try-block.  */
18640   else
18641     ctor_initializer_p
18642       = cp_parser_ctor_initializer_opt_and_function_body (parser);
18643
18644   finish_lambda_scope ();
18645
18646   /* Finish the function.  */
18647   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18648                         (inline_p ? 2 : 0));
18649   /* Generate code for it, if necessary.  */
18650   expand_or_defer_fn (fn);
18651   /* Restore the saved values.  */
18652   parser->in_unbraced_linkage_specification_p
18653     = saved_in_unbraced_linkage_specification_p;
18654   parser->num_template_parameter_lists
18655     = saved_num_template_parameter_lists;
18656   parser->in_function_body = saved_in_function_body;
18657
18658   return fn;
18659 }
18660
18661 /* Parse a template-declaration, assuming that the `export' (and
18662    `extern') keywords, if present, has already been scanned.  MEMBER_P
18663    is as for cp_parser_template_declaration.  */
18664
18665 static void
18666 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18667 {
18668   tree decl = NULL_TREE;
18669   VEC (deferred_access_check,gc) *checks;
18670   tree parameter_list;
18671   bool friend_p = false;
18672   bool need_lang_pop;
18673   cp_token *token;
18674
18675   /* Look for the `template' keyword.  */
18676   token = cp_lexer_peek_token (parser->lexer);
18677   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18678     return;
18679
18680   /* And the `<'.  */
18681   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18682     return;
18683   if (at_class_scope_p () && current_function_decl)
18684     {
18685       /* 14.5.2.2 [temp.mem]
18686
18687          A local class shall not have member templates.  */
18688       error_at (token->location,
18689                 "invalid declaration of member template in local class");
18690       cp_parser_skip_to_end_of_block_or_statement (parser);
18691       return;
18692     }
18693   /* [temp]
18694
18695      A template ... shall not have C linkage.  */
18696   if (current_lang_name == lang_name_c)
18697     {
18698       error_at (token->location, "template with C linkage");
18699       /* Give it C++ linkage to avoid confusing other parts of the
18700          front end.  */
18701       push_lang_context (lang_name_cplusplus);
18702       need_lang_pop = true;
18703     }
18704   else
18705     need_lang_pop = false;
18706
18707   /* We cannot perform access checks on the template parameter
18708      declarations until we know what is being declared, just as we
18709      cannot check the decl-specifier list.  */
18710   push_deferring_access_checks (dk_deferred);
18711
18712   /* If the next token is `>', then we have an invalid
18713      specialization.  Rather than complain about an invalid template
18714      parameter, issue an error message here.  */
18715   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18716     {
18717       cp_parser_error (parser, "invalid explicit specialization");
18718       begin_specialization ();
18719       parameter_list = NULL_TREE;
18720     }
18721   else
18722     /* Parse the template parameters.  */
18723     parameter_list = cp_parser_template_parameter_list (parser);
18724
18725   /* Get the deferred access checks from the parameter list.  These
18726      will be checked once we know what is being declared, as for a
18727      member template the checks must be performed in the scope of the
18728      class containing the member.  */
18729   checks = get_deferred_access_checks ();
18730
18731   /* Look for the `>'.  */
18732   cp_parser_skip_to_end_of_template_parameter_list (parser);
18733   /* We just processed one more parameter list.  */
18734   ++parser->num_template_parameter_lists;
18735   /* If the next token is `template', there are more template
18736      parameters.  */
18737   if (cp_lexer_next_token_is_keyword (parser->lexer,
18738                                       RID_TEMPLATE))
18739     cp_parser_template_declaration_after_export (parser, member_p);
18740   else
18741     {
18742       /* There are no access checks when parsing a template, as we do not
18743          know if a specialization will be a friend.  */
18744       push_deferring_access_checks (dk_no_check);
18745       token = cp_lexer_peek_token (parser->lexer);
18746       decl = cp_parser_single_declaration (parser,
18747                                            checks,
18748                                            member_p,
18749                                            /*explicit_specialization_p=*/false,
18750                                            &friend_p);
18751       pop_deferring_access_checks ();
18752
18753       /* If this is a member template declaration, let the front
18754          end know.  */
18755       if (member_p && !friend_p && decl)
18756         {
18757           if (TREE_CODE (decl) == TYPE_DECL)
18758             cp_parser_check_access_in_redeclaration (decl, token->location);
18759
18760           decl = finish_member_template_decl (decl);
18761         }
18762       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18763         make_friend_class (current_class_type, TREE_TYPE (decl),
18764                            /*complain=*/true);
18765     }
18766   /* We are done with the current parameter list.  */
18767   --parser->num_template_parameter_lists;
18768
18769   pop_deferring_access_checks ();
18770
18771   /* Finish up.  */
18772   finish_template_decl (parameter_list);
18773
18774   /* Register member declarations.  */
18775   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18776     finish_member_declaration (decl);
18777   /* For the erroneous case of a template with C linkage, we pushed an
18778      implicit C++ linkage scope; exit that scope now.  */
18779   if (need_lang_pop)
18780     pop_lang_context ();
18781   /* If DECL is a function template, we must return to parse it later.
18782      (Even though there is no definition, there might be default
18783      arguments that need handling.)  */
18784   if (member_p && decl
18785       && (TREE_CODE (decl) == FUNCTION_DECL
18786           || DECL_FUNCTION_TEMPLATE_P (decl)))
18787     TREE_VALUE (parser->unparsed_functions_queues)
18788       = tree_cons (NULL_TREE, decl,
18789                    TREE_VALUE (parser->unparsed_functions_queues));
18790 }
18791
18792 /* Perform the deferred access checks from a template-parameter-list.
18793    CHECKS is a TREE_LIST of access checks, as returned by
18794    get_deferred_access_checks.  */
18795
18796 static void
18797 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18798 {
18799   ++processing_template_parmlist;
18800   perform_access_checks (checks);
18801   --processing_template_parmlist;
18802 }
18803
18804 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18805    `function-definition' sequence.  MEMBER_P is true, this declaration
18806    appears in a class scope.
18807
18808    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
18809    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
18810
18811 static tree
18812 cp_parser_single_declaration (cp_parser* parser,
18813                               VEC (deferred_access_check,gc)* checks,
18814                               bool member_p,
18815                               bool explicit_specialization_p,
18816                               bool* friend_p)
18817 {
18818   int declares_class_or_enum;
18819   tree decl = NULL_TREE;
18820   cp_decl_specifier_seq decl_specifiers;
18821   bool function_definition_p = false;
18822   cp_token *decl_spec_token_start;
18823
18824   /* This function is only used when processing a template
18825      declaration.  */
18826   gcc_assert (innermost_scope_kind () == sk_template_parms
18827               || innermost_scope_kind () == sk_template_spec);
18828
18829   /* Defer access checks until we know what is being declared.  */
18830   push_deferring_access_checks (dk_deferred);
18831
18832   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18833      alternative.  */
18834   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18835   cp_parser_decl_specifier_seq (parser,
18836                                 CP_PARSER_FLAGS_OPTIONAL,
18837                                 &decl_specifiers,
18838                                 &declares_class_or_enum);
18839   if (friend_p)
18840     *friend_p = cp_parser_friend_p (&decl_specifiers);
18841
18842   /* There are no template typedefs.  */
18843   if (decl_specifiers.specs[(int) ds_typedef])
18844     {
18845       error_at (decl_spec_token_start->location,
18846                 "template declaration of %<typedef%>");
18847       decl = error_mark_node;
18848     }
18849
18850   /* Gather up the access checks that occurred the
18851      decl-specifier-seq.  */
18852   stop_deferring_access_checks ();
18853
18854   /* Check for the declaration of a template class.  */
18855   if (declares_class_or_enum)
18856     {
18857       if (cp_parser_declares_only_class_p (parser))
18858         {
18859           decl = shadow_tag (&decl_specifiers);
18860
18861           /* In this case:
18862
18863                struct C {
18864                  friend template <typename T> struct A<T>::B;
18865                };
18866
18867              A<T>::B will be represented by a TYPENAME_TYPE, and
18868              therefore not recognized by shadow_tag.  */
18869           if (friend_p && *friend_p
18870               && !decl
18871               && decl_specifiers.type
18872               && TYPE_P (decl_specifiers.type))
18873             decl = decl_specifiers.type;
18874
18875           if (decl && decl != error_mark_node)
18876             decl = TYPE_NAME (decl);
18877           else
18878             decl = error_mark_node;
18879
18880           /* Perform access checks for template parameters.  */
18881           cp_parser_perform_template_parameter_access_checks (checks);
18882         }
18883     }
18884
18885   /* Complain about missing 'typename' or other invalid type names.  */
18886   if (!decl_specifiers.any_type_specifiers_p)
18887     cp_parser_parse_and_diagnose_invalid_type_name (parser);
18888
18889   /* If it's not a template class, try for a template function.  If
18890      the next token is a `;', then this declaration does not declare
18891      anything.  But, if there were errors in the decl-specifiers, then
18892      the error might well have come from an attempted class-specifier.
18893      In that case, there's no need to warn about a missing declarator.  */
18894   if (!decl
18895       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18896           || decl_specifiers.type != error_mark_node))
18897     {
18898       decl = cp_parser_init_declarator (parser,
18899                                         &decl_specifiers,
18900                                         checks,
18901                                         /*function_definition_allowed_p=*/true,
18902                                         member_p,
18903                                         declares_class_or_enum,
18904                                         &function_definition_p);
18905
18906     /* 7.1.1-1 [dcl.stc]
18907
18908        A storage-class-specifier shall not be specified in an explicit
18909        specialization...  */
18910     if (decl
18911         && explicit_specialization_p
18912         && decl_specifiers.storage_class != sc_none)
18913       {
18914         error_at (decl_spec_token_start->location,
18915                   "explicit template specialization cannot have a storage class");
18916         decl = error_mark_node;
18917       }
18918     }
18919
18920   pop_deferring_access_checks ();
18921
18922   /* Clear any current qualification; whatever comes next is the start
18923      of something new.  */
18924   parser->scope = NULL_TREE;
18925   parser->qualifying_scope = NULL_TREE;
18926   parser->object_scope = NULL_TREE;
18927   /* Look for a trailing `;' after the declaration.  */
18928   if (!function_definition_p
18929       && (decl == error_mark_node
18930           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18931     cp_parser_skip_to_end_of_block_or_statement (parser);
18932
18933   return decl;
18934 }
18935
18936 /* Parse a cast-expression that is not the operand of a unary "&".  */
18937
18938 static tree
18939 cp_parser_simple_cast_expression (cp_parser *parser)
18940 {
18941   return cp_parser_cast_expression (parser, /*address_p=*/false,
18942                                     /*cast_p=*/false, NULL);
18943 }
18944
18945 /* Parse a functional cast to TYPE.  Returns an expression
18946    representing the cast.  */
18947
18948 static tree
18949 cp_parser_functional_cast (cp_parser* parser, tree type)
18950 {
18951   VEC(tree,gc) *vec;
18952   tree expression_list;
18953   tree cast;
18954   bool nonconst_p;
18955
18956   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18957     {
18958       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18959       expression_list = cp_parser_braced_list (parser, &nonconst_p);
18960       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18961       if (TREE_CODE (type) == TYPE_DECL)
18962         type = TREE_TYPE (type);
18963       return finish_compound_literal (type, expression_list);
18964     }
18965
18966
18967   vec = cp_parser_parenthesized_expression_list (parser, false,
18968                                                  /*cast_p=*/true,
18969                                                  /*allow_expansion_p=*/true,
18970                                                  /*non_constant_p=*/NULL);
18971   if (vec == NULL)
18972     expression_list = error_mark_node;
18973   else
18974     {
18975       expression_list = build_tree_list_vec (vec);
18976       release_tree_vector (vec);
18977     }
18978
18979   cast = build_functional_cast (type, expression_list,
18980                                 tf_warning_or_error);
18981   /* [expr.const]/1: In an integral constant expression "only type
18982      conversions to integral or enumeration type can be used".  */
18983   if (TREE_CODE (type) == TYPE_DECL)
18984     type = TREE_TYPE (type);
18985   if (cast != error_mark_node
18986       && !cast_valid_in_integral_constant_expression_p (type)
18987       && (cp_parser_non_integral_constant_expression
18988           (parser, "a call to a constructor")))
18989     return error_mark_node;
18990   return cast;
18991 }
18992
18993 /* Save the tokens that make up the body of a member function defined
18994    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18995    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18996    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18997    for the member function.  */
18998
18999 static tree
19000 cp_parser_save_member_function_body (cp_parser* parser,
19001                                      cp_decl_specifier_seq *decl_specifiers,
19002                                      cp_declarator *declarator,
19003                                      tree attributes)
19004 {
19005   cp_token *first;
19006   cp_token *last;
19007   tree fn;
19008
19009   /* Create the FUNCTION_DECL.  */
19010   fn = grokmethod (decl_specifiers, declarator, attributes);
19011   /* If something went badly wrong, bail out now.  */
19012   if (fn == error_mark_node)
19013     {
19014       /* If there's a function-body, skip it.  */
19015       if (cp_parser_token_starts_function_definition_p
19016           (cp_lexer_peek_token (parser->lexer)))
19017         cp_parser_skip_to_end_of_block_or_statement (parser);
19018       return error_mark_node;
19019     }
19020
19021   /* Remember it, if there default args to post process.  */
19022   cp_parser_save_default_args (parser, fn);
19023
19024   /* Save away the tokens that make up the body of the
19025      function.  */
19026   first = parser->lexer->next_token;
19027   /* We can have braced-init-list mem-initializers before the fn body.  */
19028   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19029     {
19030       cp_lexer_consume_token (parser->lexer);
19031       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19032              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19033         {
19034           /* cache_group will stop after an un-nested { } pair, too.  */
19035           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19036             break;
19037
19038           /* variadic mem-inits have ... after the ')'.  */
19039           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19040             cp_lexer_consume_token (parser->lexer);
19041         }
19042     }
19043   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19044   /* Handle function try blocks.  */
19045   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19046     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19047   last = parser->lexer->next_token;
19048
19049   /* Save away the inline definition; we will process it when the
19050      class is complete.  */
19051   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19052   DECL_PENDING_INLINE_P (fn) = 1;
19053
19054   /* We need to know that this was defined in the class, so that
19055      friend templates are handled correctly.  */
19056   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19057
19058   /* Add FN to the queue of functions to be parsed later.  */
19059   TREE_VALUE (parser->unparsed_functions_queues)
19060     = tree_cons (NULL_TREE, fn,
19061                  TREE_VALUE (parser->unparsed_functions_queues));
19062
19063   return fn;
19064 }
19065
19066 /* Parse a template-argument-list, as well as the trailing ">" (but
19067    not the opening ">").  See cp_parser_template_argument_list for the
19068    return value.  */
19069
19070 static tree
19071 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19072 {
19073   tree arguments;
19074   tree saved_scope;
19075   tree saved_qualifying_scope;
19076   tree saved_object_scope;
19077   bool saved_greater_than_is_operator_p;
19078   int saved_unevaluated_operand;
19079   int saved_inhibit_evaluation_warnings;
19080
19081   /* [temp.names]
19082
19083      When parsing a template-id, the first non-nested `>' is taken as
19084      the end of the template-argument-list rather than a greater-than
19085      operator.  */
19086   saved_greater_than_is_operator_p
19087     = parser->greater_than_is_operator_p;
19088   parser->greater_than_is_operator_p = false;
19089   /* Parsing the argument list may modify SCOPE, so we save it
19090      here.  */
19091   saved_scope = parser->scope;
19092   saved_qualifying_scope = parser->qualifying_scope;
19093   saved_object_scope = parser->object_scope;
19094   /* We need to evaluate the template arguments, even though this
19095      template-id may be nested within a "sizeof".  */
19096   saved_unevaluated_operand = cp_unevaluated_operand;
19097   cp_unevaluated_operand = 0;
19098   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19099   c_inhibit_evaluation_warnings = 0;
19100   /* Parse the template-argument-list itself.  */
19101   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19102       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19103     arguments = NULL_TREE;
19104   else
19105     arguments = cp_parser_template_argument_list (parser);
19106   /* Look for the `>' that ends the template-argument-list. If we find
19107      a '>>' instead, it's probably just a typo.  */
19108   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19109     {
19110       if (cxx_dialect != cxx98)
19111         {
19112           /* In C++0x, a `>>' in a template argument list or cast
19113              expression is considered to be two separate `>'
19114              tokens. So, change the current token to a `>', but don't
19115              consume it: it will be consumed later when the outer
19116              template argument list (or cast expression) is parsed.
19117              Note that this replacement of `>' for `>>' is necessary
19118              even if we are parsing tentatively: in the tentative
19119              case, after calling
19120              cp_parser_enclosed_template_argument_list we will always
19121              throw away all of the template arguments and the first
19122              closing `>', either because the template argument list
19123              was erroneous or because we are replacing those tokens
19124              with a CPP_TEMPLATE_ID token.  The second `>' (which will
19125              not have been thrown away) is needed either to close an
19126              outer template argument list or to complete a new-style
19127              cast.  */
19128           cp_token *token = cp_lexer_peek_token (parser->lexer);
19129           token->type = CPP_GREATER;
19130         }
19131       else if (!saved_greater_than_is_operator_p)
19132         {
19133           /* If we're in a nested template argument list, the '>>' has
19134             to be a typo for '> >'. We emit the error message, but we
19135             continue parsing and we push a '>' as next token, so that
19136             the argument list will be parsed correctly.  Note that the
19137             global source location is still on the token before the
19138             '>>', so we need to say explicitly where we want it.  */
19139           cp_token *token = cp_lexer_peek_token (parser->lexer);
19140           error_at (token->location, "%<>>%> should be %<> >%> "
19141                     "within a nested template argument list");
19142
19143           token->type = CPP_GREATER;
19144         }
19145       else
19146         {
19147           /* If this is not a nested template argument list, the '>>'
19148             is a typo for '>'. Emit an error message and continue.
19149             Same deal about the token location, but here we can get it
19150             right by consuming the '>>' before issuing the diagnostic.  */
19151           cp_token *token = cp_lexer_consume_token (parser->lexer);
19152           error_at (token->location,
19153                     "spurious %<>>%>, use %<>%> to terminate "
19154                     "a template argument list");
19155         }
19156     }
19157   else
19158     cp_parser_skip_to_end_of_template_parameter_list (parser);
19159   /* The `>' token might be a greater-than operator again now.  */
19160   parser->greater_than_is_operator_p
19161     = saved_greater_than_is_operator_p;
19162   /* Restore the SAVED_SCOPE.  */
19163   parser->scope = saved_scope;
19164   parser->qualifying_scope = saved_qualifying_scope;
19165   parser->object_scope = saved_object_scope;
19166   cp_unevaluated_operand = saved_unevaluated_operand;
19167   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19168
19169   return arguments;
19170 }
19171
19172 /* MEMBER_FUNCTION is a member function, or a friend.  If default
19173    arguments, or the body of the function have not yet been parsed,
19174    parse them now.  */
19175
19176 static void
19177 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19178 {
19179   /* If this member is a template, get the underlying
19180      FUNCTION_DECL.  */
19181   if (DECL_FUNCTION_TEMPLATE_P (member_function))
19182     member_function = DECL_TEMPLATE_RESULT (member_function);
19183
19184   /* There should not be any class definitions in progress at this
19185      point; the bodies of members are only parsed outside of all class
19186      definitions.  */
19187   gcc_assert (parser->num_classes_being_defined == 0);
19188   /* While we're parsing the member functions we might encounter more
19189      classes.  We want to handle them right away, but we don't want
19190      them getting mixed up with functions that are currently in the
19191      queue.  */
19192   parser->unparsed_functions_queues
19193     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19194
19195   /* Make sure that any template parameters are in scope.  */
19196   maybe_begin_member_template_processing (member_function);
19197
19198   /* If the body of the function has not yet been parsed, parse it
19199      now.  */
19200   if (DECL_PENDING_INLINE_P (member_function))
19201     {
19202       tree function_scope;
19203       cp_token_cache *tokens;
19204
19205       /* The function is no longer pending; we are processing it.  */
19206       tokens = DECL_PENDING_INLINE_INFO (member_function);
19207       DECL_PENDING_INLINE_INFO (member_function) = NULL;
19208       DECL_PENDING_INLINE_P (member_function) = 0;
19209
19210       /* If this is a local class, enter the scope of the containing
19211          function.  */
19212       function_scope = current_function_decl;
19213       if (function_scope)
19214         push_function_context ();
19215
19216       /* Push the body of the function onto the lexer stack.  */
19217       cp_parser_push_lexer_for_tokens (parser, tokens);
19218
19219       /* Let the front end know that we going to be defining this
19220          function.  */
19221       start_preparsed_function (member_function, NULL_TREE,
19222                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
19223
19224       /* Don't do access checking if it is a templated function.  */
19225       if (processing_template_decl)
19226         push_deferring_access_checks (dk_no_check);
19227
19228       /* Now, parse the body of the function.  */
19229       cp_parser_function_definition_after_declarator (parser,
19230                                                       /*inline_p=*/true);
19231
19232       if (processing_template_decl)
19233         pop_deferring_access_checks ();
19234
19235       /* Leave the scope of the containing function.  */
19236       if (function_scope)
19237         pop_function_context ();
19238       cp_parser_pop_lexer (parser);
19239     }
19240
19241   /* Remove any template parameters from the symbol table.  */
19242   maybe_end_member_template_processing ();
19243
19244   /* Restore the queue.  */
19245   parser->unparsed_functions_queues
19246     = TREE_CHAIN (parser->unparsed_functions_queues);
19247 }
19248
19249 /* If DECL contains any default args, remember it on the unparsed
19250    functions queue.  */
19251
19252 static void
19253 cp_parser_save_default_args (cp_parser* parser, tree decl)
19254 {
19255   tree probe;
19256
19257   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19258        probe;
19259        probe = TREE_CHAIN (probe))
19260     if (TREE_PURPOSE (probe))
19261       {
19262         TREE_PURPOSE (parser->unparsed_functions_queues)
19263           = tree_cons (current_class_type, decl,
19264                        TREE_PURPOSE (parser->unparsed_functions_queues));
19265         break;
19266       }
19267 }
19268
19269 /* FN is a FUNCTION_DECL which may contains a parameter with an
19270    unparsed DEFAULT_ARG.  Parse the default args now.  This function
19271    assumes that the current scope is the scope in which the default
19272    argument should be processed.  */
19273
19274 static void
19275 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19276 {
19277   bool saved_local_variables_forbidden_p;
19278   tree parm, parmdecl;
19279
19280   /* While we're parsing the default args, we might (due to the
19281      statement expression extension) encounter more classes.  We want
19282      to handle them right away, but we don't want them getting mixed
19283      up with default args that are currently in the queue.  */
19284   parser->unparsed_functions_queues
19285     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19286
19287   /* Local variable names (and the `this' keyword) may not appear
19288      in a default argument.  */
19289   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19290   parser->local_variables_forbidden_p = true;
19291
19292   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19293          parmdecl = DECL_ARGUMENTS (fn);
19294        parm && parm != void_list_node;
19295        parm = TREE_CHAIN (parm),
19296          parmdecl = TREE_CHAIN (parmdecl))
19297     {
19298       cp_token_cache *tokens;
19299       tree default_arg = TREE_PURPOSE (parm);
19300       tree parsed_arg;
19301       VEC(tree,gc) *insts;
19302       tree copy;
19303       unsigned ix;
19304
19305       if (!default_arg)
19306         continue;
19307
19308       if (TREE_CODE (default_arg) != DEFAULT_ARG)
19309         /* This can happen for a friend declaration for a function
19310            already declared with default arguments.  */
19311         continue;
19312
19313        /* Push the saved tokens for the default argument onto the parser's
19314           lexer stack.  */
19315       tokens = DEFARG_TOKENS (default_arg);
19316       cp_parser_push_lexer_for_tokens (parser, tokens);
19317
19318       start_lambda_scope (parmdecl);
19319
19320       /* Parse the assignment-expression.  */
19321       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19322       if (parsed_arg == error_mark_node)
19323         {
19324           cp_parser_pop_lexer (parser);
19325           continue;
19326         }
19327
19328       if (!processing_template_decl)
19329         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19330
19331       TREE_PURPOSE (parm) = parsed_arg;
19332
19333       /* Update any instantiations we've already created.  */
19334       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19335            VEC_iterate (tree, insts, ix, copy); ix++)
19336         TREE_PURPOSE (copy) = parsed_arg;
19337
19338       finish_lambda_scope ();
19339
19340       /* If the token stream has not been completely used up, then
19341          there was extra junk after the end of the default
19342          argument.  */
19343       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19344         cp_parser_error (parser, "expected %<,%>");
19345
19346       /* Revert to the main lexer.  */
19347       cp_parser_pop_lexer (parser);
19348     }
19349
19350   /* Make sure no default arg is missing.  */
19351   check_default_args (fn);
19352
19353   /* Restore the state of local_variables_forbidden_p.  */
19354   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19355
19356   /* Restore the queue.  */
19357   parser->unparsed_functions_queues
19358     = TREE_CHAIN (parser->unparsed_functions_queues);
19359 }
19360
19361 /* Parse the operand of `sizeof' (or a similar operator).  Returns
19362    either a TYPE or an expression, depending on the form of the
19363    input.  The KEYWORD indicates which kind of expression we have
19364    encountered.  */
19365
19366 static tree
19367 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19368 {
19369   tree expr = NULL_TREE;
19370   const char *saved_message;
19371   char *tmp;
19372   bool saved_integral_constant_expression_p;
19373   bool saved_non_integral_constant_expression_p;
19374   bool pack_expansion_p = false;
19375
19376   /* Types cannot be defined in a `sizeof' expression.  Save away the
19377      old message.  */
19378   saved_message = parser->type_definition_forbidden_message;
19379   /* And create the new one.  */
19380   tmp = concat ("types may not be defined in %<",
19381                 IDENTIFIER_POINTER (ridpointers[keyword]),
19382                 "%> expressions", NULL);
19383   parser->type_definition_forbidden_message = tmp;
19384
19385   /* The restrictions on constant-expressions do not apply inside
19386      sizeof expressions.  */
19387   saved_integral_constant_expression_p
19388     = parser->integral_constant_expression_p;
19389   saved_non_integral_constant_expression_p
19390     = parser->non_integral_constant_expression_p;
19391   parser->integral_constant_expression_p = false;
19392
19393   /* If it's a `...', then we are computing the length of a parameter
19394      pack.  */
19395   if (keyword == RID_SIZEOF
19396       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19397     {
19398       /* Consume the `...'.  */
19399       cp_lexer_consume_token (parser->lexer);
19400       maybe_warn_variadic_templates ();
19401
19402       /* Note that this is an expansion.  */
19403       pack_expansion_p = true;
19404     }
19405
19406   /* Do not actually evaluate the expression.  */
19407   ++cp_unevaluated_operand;
19408   ++c_inhibit_evaluation_warnings;
19409   /* If it's a `(', then we might be looking at the type-id
19410      construction.  */
19411   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19412     {
19413       tree type;
19414       bool saved_in_type_id_in_expr_p;
19415
19416       /* We can't be sure yet whether we're looking at a type-id or an
19417          expression.  */
19418       cp_parser_parse_tentatively (parser);
19419       /* Consume the `('.  */
19420       cp_lexer_consume_token (parser->lexer);
19421       /* Parse the type-id.  */
19422       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19423       parser->in_type_id_in_expr_p = true;
19424       type = cp_parser_type_id (parser);
19425       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19426       /* Now, look for the trailing `)'.  */
19427       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19428       /* If all went well, then we're done.  */
19429       if (cp_parser_parse_definitely (parser))
19430         {
19431           cp_decl_specifier_seq decl_specs;
19432
19433           /* Build a trivial decl-specifier-seq.  */
19434           clear_decl_specs (&decl_specs);
19435           decl_specs.type = type;
19436
19437           /* Call grokdeclarator to figure out what type this is.  */
19438           expr = grokdeclarator (NULL,
19439                                  &decl_specs,
19440                                  TYPENAME,
19441                                  /*initialized=*/0,
19442                                  /*attrlist=*/NULL);
19443         }
19444     }
19445
19446   /* If the type-id production did not work out, then we must be
19447      looking at the unary-expression production.  */
19448   if (!expr)
19449     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19450                                        /*cast_p=*/false, NULL);
19451
19452   if (pack_expansion_p)
19453     /* Build a pack expansion. */
19454     expr = make_pack_expansion (expr);
19455
19456   /* Go back to evaluating expressions.  */
19457   --cp_unevaluated_operand;
19458   --c_inhibit_evaluation_warnings;
19459
19460   /* Free the message we created.  */
19461   free (tmp);
19462   /* And restore the old one.  */
19463   parser->type_definition_forbidden_message = saved_message;
19464   parser->integral_constant_expression_p
19465     = saved_integral_constant_expression_p;
19466   parser->non_integral_constant_expression_p
19467     = saved_non_integral_constant_expression_p;
19468
19469   return expr;
19470 }
19471
19472 /* If the current declaration has no declarator, return true.  */
19473
19474 static bool
19475 cp_parser_declares_only_class_p (cp_parser *parser)
19476 {
19477   /* If the next token is a `;' or a `,' then there is no
19478      declarator.  */
19479   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19480           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19481 }
19482
19483 /* Update the DECL_SPECS to reflect the storage class indicated by
19484    KEYWORD.  */
19485
19486 static void
19487 cp_parser_set_storage_class (cp_parser *parser,
19488                              cp_decl_specifier_seq *decl_specs,
19489                              enum rid keyword,
19490                              location_t location)
19491 {
19492   cp_storage_class storage_class;
19493
19494   if (parser->in_unbraced_linkage_specification_p)
19495     {
19496       error_at (location, "invalid use of %qD in linkage specification",
19497                 ridpointers[keyword]);
19498       return;
19499     }
19500   else if (decl_specs->storage_class != sc_none)
19501     {
19502       decl_specs->conflicting_specifiers_p = true;
19503       return;
19504     }
19505
19506   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19507       && decl_specs->specs[(int) ds_thread])
19508     {
19509       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19510       decl_specs->specs[(int) ds_thread] = 0;
19511     }
19512
19513   switch (keyword)
19514     {
19515     case RID_AUTO:
19516       storage_class = sc_auto;
19517       break;
19518     case RID_REGISTER:
19519       storage_class = sc_register;
19520       break;
19521     case RID_STATIC:
19522       storage_class = sc_static;
19523       break;
19524     case RID_EXTERN:
19525       storage_class = sc_extern;
19526       break;
19527     case RID_MUTABLE:
19528       storage_class = sc_mutable;
19529       break;
19530     default:
19531       gcc_unreachable ();
19532     }
19533   decl_specs->storage_class = storage_class;
19534
19535   /* A storage class specifier cannot be applied alongside a typedef 
19536      specifier. If there is a typedef specifier present then set 
19537      conflicting_specifiers_p which will trigger an error later
19538      on in grokdeclarator. */
19539   if (decl_specs->specs[(int)ds_typedef])
19540     decl_specs->conflicting_specifiers_p = true;
19541 }
19542
19543 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
19544    is true, the type is a user-defined type; otherwise it is a
19545    built-in type specified by a keyword.  */
19546
19547 static void
19548 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19549                               tree type_spec,
19550                               location_t location,
19551                               bool user_defined_p)
19552 {
19553   decl_specs->any_specifiers_p = true;
19554
19555   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19556      (with, for example, in "typedef int wchar_t;") we remember that
19557      this is what happened.  In system headers, we ignore these
19558      declarations so that G++ can work with system headers that are not
19559      C++-safe.  */
19560   if (decl_specs->specs[(int) ds_typedef]
19561       && !user_defined_p
19562       && (type_spec == boolean_type_node
19563           || type_spec == char16_type_node
19564           || type_spec == char32_type_node
19565           || type_spec == wchar_type_node)
19566       && (decl_specs->type
19567           || decl_specs->specs[(int) ds_long]
19568           || decl_specs->specs[(int) ds_short]
19569           || decl_specs->specs[(int) ds_unsigned]
19570           || decl_specs->specs[(int) ds_signed]))
19571     {
19572       decl_specs->redefined_builtin_type = type_spec;
19573       if (!decl_specs->type)
19574         {
19575           decl_specs->type = type_spec;
19576           decl_specs->user_defined_type_p = false;
19577           decl_specs->type_location = location;
19578         }
19579     }
19580   else if (decl_specs->type)
19581     decl_specs->multiple_types_p = true;
19582   else
19583     {
19584       decl_specs->type = type_spec;
19585       decl_specs->user_defined_type_p = user_defined_p;
19586       decl_specs->redefined_builtin_type = NULL_TREE;
19587       decl_specs->type_location = location;
19588     }
19589 }
19590
19591 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19592    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
19593
19594 static bool
19595 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19596 {
19597   return decl_specifiers->specs[(int) ds_friend] != 0;
19598 }
19599
19600 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
19601    issue an error message indicating that TOKEN_DESC was expected.
19602
19603    Returns the token consumed, if the token had the appropriate type.
19604    Otherwise, returns NULL.  */
19605
19606 static cp_token *
19607 cp_parser_require (cp_parser* parser,
19608                    enum cpp_ttype type,
19609                    const char* token_desc)
19610 {
19611   if (cp_lexer_next_token_is (parser->lexer, type))
19612     return cp_lexer_consume_token (parser->lexer);
19613   else
19614     {
19615       /* Output the MESSAGE -- unless we're parsing tentatively.  */
19616       if (!cp_parser_simulate_error (parser))
19617         {
19618           char *message = concat ("expected ", token_desc, NULL);
19619           cp_parser_error (parser, message);
19620           free (message);
19621         }
19622       return NULL;
19623     }
19624 }
19625
19626 /* An error message is produced if the next token is not '>'.
19627    All further tokens are skipped until the desired token is
19628    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
19629
19630 static void
19631 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19632 {
19633   /* Current level of '< ... >'.  */
19634   unsigned level = 0;
19635   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
19636   unsigned nesting_depth = 0;
19637
19638   /* Are we ready, yet?  If not, issue error message.  */
19639   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19640     return;
19641
19642   /* Skip tokens until the desired token is found.  */
19643   while (true)
19644     {
19645       /* Peek at the next token.  */
19646       switch (cp_lexer_peek_token (parser->lexer)->type)
19647         {
19648         case CPP_LESS:
19649           if (!nesting_depth)
19650             ++level;
19651           break;
19652
19653         case CPP_RSHIFT:
19654           if (cxx_dialect == cxx98)
19655             /* C++0x views the `>>' operator as two `>' tokens, but
19656                C++98 does not. */
19657             break;
19658           else if (!nesting_depth && level-- == 0)
19659             {
19660               /* We've hit a `>>' where the first `>' closes the
19661                  template argument list, and the second `>' is
19662                  spurious.  Just consume the `>>' and stop; we've
19663                  already produced at least one error.  */
19664               cp_lexer_consume_token (parser->lexer);
19665               return;
19666             }
19667           /* Fall through for C++0x, so we handle the second `>' in
19668              the `>>'.  */
19669
19670         case CPP_GREATER:
19671           if (!nesting_depth && level-- == 0)
19672             {
19673               /* We've reached the token we want, consume it and stop.  */
19674               cp_lexer_consume_token (parser->lexer);
19675               return;
19676             }
19677           break;
19678
19679         case CPP_OPEN_PAREN:
19680         case CPP_OPEN_SQUARE:
19681           ++nesting_depth;
19682           break;
19683
19684         case CPP_CLOSE_PAREN:
19685         case CPP_CLOSE_SQUARE:
19686           if (nesting_depth-- == 0)
19687             return;
19688           break;
19689
19690         case CPP_EOF:
19691         case CPP_PRAGMA_EOL:
19692         case CPP_SEMICOLON:
19693         case CPP_OPEN_BRACE:
19694         case CPP_CLOSE_BRACE:
19695           /* The '>' was probably forgotten, don't look further.  */
19696           return;
19697
19698         default:
19699           break;
19700         }
19701
19702       /* Consume this token.  */
19703       cp_lexer_consume_token (parser->lexer);
19704     }
19705 }
19706
19707 /* If the next token is the indicated keyword, consume it.  Otherwise,
19708    issue an error message indicating that TOKEN_DESC was expected.
19709
19710    Returns the token consumed, if the token had the appropriate type.
19711    Otherwise, returns NULL.  */
19712
19713 static cp_token *
19714 cp_parser_require_keyword (cp_parser* parser,
19715                            enum rid keyword,
19716                            const char* token_desc)
19717 {
19718   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19719
19720   if (token && token->keyword != keyword)
19721     {
19722       dyn_string_t error_msg;
19723
19724       /* Format the error message.  */
19725       error_msg = dyn_string_new (0);
19726       dyn_string_append_cstr (error_msg, "expected ");
19727       dyn_string_append_cstr (error_msg, token_desc);
19728       cp_parser_error (parser, error_msg->s);
19729       dyn_string_delete (error_msg);
19730       return NULL;
19731     }
19732
19733   return token;
19734 }
19735
19736 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19737    function-definition.  */
19738
19739 static bool
19740 cp_parser_token_starts_function_definition_p (cp_token* token)
19741 {
19742   return (/* An ordinary function-body begins with an `{'.  */
19743           token->type == CPP_OPEN_BRACE
19744           /* A ctor-initializer begins with a `:'.  */
19745           || token->type == CPP_COLON
19746           /* A function-try-block begins with `try'.  */
19747           || token->keyword == RID_TRY
19748           /* The named return value extension begins with `return'.  */
19749           || token->keyword == RID_RETURN);
19750 }
19751
19752 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19753    definition.  */
19754
19755 static bool
19756 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19757 {
19758   cp_token *token;
19759
19760   token = cp_lexer_peek_token (parser->lexer);
19761   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19762 }
19763
19764 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19765    C++0x) ending a template-argument.  */
19766
19767 static bool
19768 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19769 {
19770   cp_token *token;
19771
19772   token = cp_lexer_peek_token (parser->lexer);
19773   return (token->type == CPP_COMMA 
19774           || token->type == CPP_GREATER
19775           || token->type == CPP_ELLIPSIS
19776           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19777 }
19778
19779 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19780    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
19781
19782 static bool
19783 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19784                                                      size_t n)
19785 {
19786   cp_token *token;
19787
19788   token = cp_lexer_peek_nth_token (parser->lexer, n);
19789   if (token->type == CPP_LESS)
19790     return true;
19791   /* Check for the sequence `<::' in the original code. It would be lexed as
19792      `[:', where `[' is a digraph, and there is no whitespace before
19793      `:'.  */
19794   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19795     {
19796       cp_token *token2;
19797       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19798       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19799         return true;
19800     }
19801   return false;
19802 }
19803
19804 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19805    or none_type otherwise.  */
19806
19807 static enum tag_types
19808 cp_parser_token_is_class_key (cp_token* token)
19809 {
19810   switch (token->keyword)
19811     {
19812     case RID_CLASS:
19813       return class_type;
19814     case RID_STRUCT:
19815       return record_type;
19816     case RID_UNION:
19817       return union_type;
19818
19819     default:
19820       return none_type;
19821     }
19822 }
19823
19824 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
19825
19826 static void
19827 cp_parser_check_class_key (enum tag_types class_key, tree type)
19828 {
19829   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19830     permerror (input_location, "%qs tag used in naming %q#T",
19831             class_key == union_type ? "union"
19832              : class_key == record_type ? "struct" : "class",
19833              type);
19834 }
19835
19836 /* Issue an error message if DECL is redeclared with different
19837    access than its original declaration [class.access.spec/3].
19838    This applies to nested classes and nested class templates.
19839    [class.mem/1].  */
19840
19841 static void
19842 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19843 {
19844   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19845     return;
19846
19847   if ((TREE_PRIVATE (decl)
19848        != (current_access_specifier == access_private_node))
19849       || (TREE_PROTECTED (decl)
19850           != (current_access_specifier == access_protected_node)))
19851     error_at (location, "%qD redeclared with different access", decl);
19852 }
19853
19854 /* Look for the `template' keyword, as a syntactic disambiguator.
19855    Return TRUE iff it is present, in which case it will be
19856    consumed.  */
19857
19858 static bool
19859 cp_parser_optional_template_keyword (cp_parser *parser)
19860 {
19861   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19862     {
19863       /* The `template' keyword can only be used within templates;
19864          outside templates the parser can always figure out what is a
19865          template and what is not.  */
19866       if (!processing_template_decl)
19867         {
19868           cp_token *token = cp_lexer_peek_token (parser->lexer);
19869           error_at (token->location,
19870                     "%<template%> (as a disambiguator) is only allowed "
19871                     "within templates");
19872           /* If this part of the token stream is rescanned, the same
19873              error message would be generated.  So, we purge the token
19874              from the stream.  */
19875           cp_lexer_purge_token (parser->lexer);
19876           return false;
19877         }
19878       else
19879         {
19880           /* Consume the `template' keyword.  */
19881           cp_lexer_consume_token (parser->lexer);
19882           return true;
19883         }
19884     }
19885
19886   return false;
19887 }
19888
19889 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
19890    set PARSER->SCOPE, and perform other related actions.  */
19891
19892 static void
19893 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19894 {
19895   int i;
19896   struct tree_check *check_value;
19897   deferred_access_check *chk;
19898   VEC (deferred_access_check,gc) *checks;
19899
19900   /* Get the stored value.  */
19901   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19902   /* Perform any access checks that were deferred.  */
19903   checks = check_value->checks;
19904   if (checks)
19905     {
19906       for (i = 0 ;
19907            VEC_iterate (deferred_access_check, checks, i, chk) ;
19908            ++i)
19909         {
19910           perform_or_defer_access_check (chk->binfo,
19911                                          chk->decl,
19912                                          chk->diag_decl);
19913         }
19914     }
19915   /* Set the scope from the stored value.  */
19916   parser->scope = check_value->value;
19917   parser->qualifying_scope = check_value->qualifying_scope;
19918   parser->object_scope = NULL_TREE;
19919 }
19920
19921 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
19922    encounter the end of a block before what we were looking for.  */
19923
19924 static bool
19925 cp_parser_cache_group (cp_parser *parser,
19926                        enum cpp_ttype end,
19927                        unsigned depth)
19928 {
19929   while (true)
19930     {
19931       cp_token *token = cp_lexer_peek_token (parser->lexer);
19932
19933       /* Abort a parenthesized expression if we encounter a semicolon.  */
19934       if ((end == CPP_CLOSE_PAREN || depth == 0)
19935           && token->type == CPP_SEMICOLON)
19936         return true;
19937       /* If we've reached the end of the file, stop.  */
19938       if (token->type == CPP_EOF
19939           || (end != CPP_PRAGMA_EOL
19940               && token->type == CPP_PRAGMA_EOL))
19941         return true;
19942       if (token->type == CPP_CLOSE_BRACE && depth == 0)
19943         /* We've hit the end of an enclosing block, so there's been some
19944            kind of syntax error.  */
19945         return true;
19946
19947       /* Consume the token.  */
19948       cp_lexer_consume_token (parser->lexer);
19949       /* See if it starts a new group.  */
19950       if (token->type == CPP_OPEN_BRACE)
19951         {
19952           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19953           /* In theory this should probably check end == '}', but
19954              cp_parser_save_member_function_body needs it to exit
19955              after either '}' or ')' when called with ')'.  */
19956           if (depth == 0)
19957             return false;
19958         }
19959       else if (token->type == CPP_OPEN_PAREN)
19960         {
19961           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19962           if (depth == 0 && end == CPP_CLOSE_PAREN)
19963             return false;
19964         }
19965       else if (token->type == CPP_PRAGMA)
19966         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19967       else if (token->type == end)
19968         return false;
19969     }
19970 }
19971
19972 /* Begin parsing tentatively.  We always save tokens while parsing
19973    tentatively so that if the tentative parsing fails we can restore the
19974    tokens.  */
19975
19976 static void
19977 cp_parser_parse_tentatively (cp_parser* parser)
19978 {
19979   /* Enter a new parsing context.  */
19980   parser->context = cp_parser_context_new (parser->context);
19981   /* Begin saving tokens.  */
19982   cp_lexer_save_tokens (parser->lexer);
19983   /* In order to avoid repetitive access control error messages,
19984      access checks are queued up until we are no longer parsing
19985      tentatively.  */
19986   push_deferring_access_checks (dk_deferred);
19987 }
19988
19989 /* Commit to the currently active tentative parse.  */
19990
19991 static void
19992 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19993 {
19994   cp_parser_context *context;
19995   cp_lexer *lexer;
19996
19997   /* Mark all of the levels as committed.  */
19998   lexer = parser->lexer;
19999   for (context = parser->context; context->next; context = context->next)
20000     {
20001       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
20002         break;
20003       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
20004       while (!cp_lexer_saving_tokens (lexer))
20005         lexer = lexer->next;
20006       cp_lexer_commit_tokens (lexer);
20007     }
20008 }
20009
20010 /* Abort the currently active tentative parse.  All consumed tokens
20011    will be rolled back, and no diagnostics will be issued.  */
20012
20013 static void
20014 cp_parser_abort_tentative_parse (cp_parser* parser)
20015 {
20016   cp_parser_simulate_error (parser);
20017   /* Now, pretend that we want to see if the construct was
20018      successfully parsed.  */
20019   cp_parser_parse_definitely (parser);
20020 }
20021
20022 /* Stop parsing tentatively.  If a parse error has occurred, restore the
20023    token stream.  Otherwise, commit to the tokens we have consumed.
20024    Returns true if no error occurred; false otherwise.  */
20025
20026 static bool
20027 cp_parser_parse_definitely (cp_parser* parser)
20028 {
20029   bool error_occurred;
20030   cp_parser_context *context;
20031
20032   /* Remember whether or not an error occurred, since we are about to
20033      destroy that information.  */
20034   error_occurred = cp_parser_error_occurred (parser);
20035   /* Remove the topmost context from the stack.  */
20036   context = parser->context;
20037   parser->context = context->next;
20038   /* If no parse errors occurred, commit to the tentative parse.  */
20039   if (!error_occurred)
20040     {
20041       /* Commit to the tokens read tentatively, unless that was
20042          already done.  */
20043       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20044         cp_lexer_commit_tokens (parser->lexer);
20045
20046       pop_to_parent_deferring_access_checks ();
20047     }
20048   /* Otherwise, if errors occurred, roll back our state so that things
20049      are just as they were before we began the tentative parse.  */
20050   else
20051     {
20052       cp_lexer_rollback_tokens (parser->lexer);
20053       pop_deferring_access_checks ();
20054     }
20055   /* Add the context to the front of the free list.  */
20056   context->next = cp_parser_context_free_list;
20057   cp_parser_context_free_list = context;
20058
20059   return !error_occurred;
20060 }
20061
20062 /* Returns true if we are parsing tentatively and are not committed to
20063    this tentative parse.  */
20064
20065 static bool
20066 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20067 {
20068   return (cp_parser_parsing_tentatively (parser)
20069           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20070 }
20071
20072 /* Returns nonzero iff an error has occurred during the most recent
20073    tentative parse.  */
20074
20075 static bool
20076 cp_parser_error_occurred (cp_parser* parser)
20077 {
20078   return (cp_parser_parsing_tentatively (parser)
20079           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20080 }
20081
20082 /* Returns nonzero if GNU extensions are allowed.  */
20083
20084 static bool
20085 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20086 {
20087   return parser->allow_gnu_extensions_p;
20088 }
20089 \f
20090 /* Objective-C++ Productions */
20091
20092
20093 /* Parse an Objective-C expression, which feeds into a primary-expression
20094    above.
20095
20096    objc-expression:
20097      objc-message-expression
20098      objc-string-literal
20099      objc-encode-expression
20100      objc-protocol-expression
20101      objc-selector-expression
20102
20103   Returns a tree representation of the expression.  */
20104
20105 static tree
20106 cp_parser_objc_expression (cp_parser* parser)
20107 {
20108   /* Try to figure out what kind of declaration is present.  */
20109   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20110
20111   switch (kwd->type)
20112     {
20113     case CPP_OPEN_SQUARE:
20114       return cp_parser_objc_message_expression (parser);
20115
20116     case CPP_OBJC_STRING:
20117       kwd = cp_lexer_consume_token (parser->lexer);
20118       return objc_build_string_object (kwd->u.value);
20119
20120     case CPP_KEYWORD:
20121       switch (kwd->keyword)
20122         {
20123         case RID_AT_ENCODE:
20124           return cp_parser_objc_encode_expression (parser);
20125
20126         case RID_AT_PROTOCOL:
20127           return cp_parser_objc_protocol_expression (parser);
20128
20129         case RID_AT_SELECTOR:
20130           return cp_parser_objc_selector_expression (parser);
20131
20132         default:
20133           break;
20134         }
20135     default:
20136       error_at (kwd->location,
20137                 "misplaced %<@%D%> Objective-C++ construct",
20138                 kwd->u.value);
20139       cp_parser_skip_to_end_of_block_or_statement (parser);
20140     }
20141
20142   return error_mark_node;
20143 }
20144
20145 /* Parse an Objective-C message expression.
20146
20147    objc-message-expression:
20148      [ objc-message-receiver objc-message-args ]
20149
20150    Returns a representation of an Objective-C message.  */
20151
20152 static tree
20153 cp_parser_objc_message_expression (cp_parser* parser)
20154 {
20155   tree receiver, messageargs;
20156
20157   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
20158   receiver = cp_parser_objc_message_receiver (parser);
20159   messageargs = cp_parser_objc_message_args (parser);
20160   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20161
20162   return objc_build_message_expr (build_tree_list (receiver, messageargs));
20163 }
20164
20165 /* Parse an objc-message-receiver.
20166
20167    objc-message-receiver:
20168      expression
20169      simple-type-specifier
20170
20171   Returns a representation of the type or expression.  */
20172
20173 static tree
20174 cp_parser_objc_message_receiver (cp_parser* parser)
20175 {
20176   tree rcv;
20177
20178   /* An Objective-C message receiver may be either (1) a type
20179      or (2) an expression.  */
20180   cp_parser_parse_tentatively (parser);
20181   rcv = cp_parser_expression (parser, false, NULL);
20182
20183   if (cp_parser_parse_definitely (parser))
20184     return rcv;
20185
20186   rcv = cp_parser_simple_type_specifier (parser,
20187                                          /*decl_specs=*/NULL,
20188                                          CP_PARSER_FLAGS_NONE);
20189
20190   return objc_get_class_reference (rcv);
20191 }
20192
20193 /* Parse the arguments and selectors comprising an Objective-C message.
20194
20195    objc-message-args:
20196      objc-selector
20197      objc-selector-args
20198      objc-selector-args , objc-comma-args
20199
20200    objc-selector-args:
20201      objc-selector [opt] : assignment-expression
20202      objc-selector-args objc-selector [opt] : assignment-expression
20203
20204    objc-comma-args:
20205      assignment-expression
20206      objc-comma-args , assignment-expression
20207
20208    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20209    selector arguments and TREE_VALUE containing a list of comma
20210    arguments.  */
20211
20212 static tree
20213 cp_parser_objc_message_args (cp_parser* parser)
20214 {
20215   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20216   bool maybe_unary_selector_p = true;
20217   cp_token *token = cp_lexer_peek_token (parser->lexer);
20218
20219   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20220     {
20221       tree selector = NULL_TREE, arg;
20222
20223       if (token->type != CPP_COLON)
20224         selector = cp_parser_objc_selector (parser);
20225
20226       /* Detect if we have a unary selector.  */
20227       if (maybe_unary_selector_p
20228           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20229         return build_tree_list (selector, NULL_TREE);
20230
20231       maybe_unary_selector_p = false;
20232       cp_parser_require (parser, CPP_COLON, "%<:%>");
20233       arg = cp_parser_assignment_expression (parser, false, NULL);
20234
20235       sel_args
20236         = chainon (sel_args,
20237                    build_tree_list (selector, arg));
20238
20239       token = cp_lexer_peek_token (parser->lexer);
20240     }
20241
20242   /* Handle non-selector arguments, if any. */
20243   while (token->type == CPP_COMMA)
20244     {
20245       tree arg;
20246
20247       cp_lexer_consume_token (parser->lexer);
20248       arg = cp_parser_assignment_expression (parser, false, NULL);
20249
20250       addl_args
20251         = chainon (addl_args,
20252                    build_tree_list (NULL_TREE, arg));
20253
20254       token = cp_lexer_peek_token (parser->lexer);
20255     }
20256
20257   return build_tree_list (sel_args, addl_args);
20258 }
20259
20260 /* Parse an Objective-C encode expression.
20261
20262    objc-encode-expression:
20263      @encode objc-typename
20264
20265    Returns an encoded representation of the type argument.  */
20266
20267 static tree
20268 cp_parser_objc_encode_expression (cp_parser* parser)
20269 {
20270   tree type;
20271   cp_token *token;
20272
20273   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
20274   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20275   token = cp_lexer_peek_token (parser->lexer);
20276   type = complete_type (cp_parser_type_id (parser));
20277   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20278
20279   if (!type)
20280     {
20281       error_at (token->location, 
20282                 "%<@encode%> must specify a type as an argument");
20283       return error_mark_node;
20284     }
20285
20286   return objc_build_encode_expr (type);
20287 }
20288
20289 /* Parse an Objective-C @defs expression.  */
20290
20291 static tree
20292 cp_parser_objc_defs_expression (cp_parser *parser)
20293 {
20294   tree name;
20295
20296   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
20297   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20298   name = cp_parser_identifier (parser);
20299   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20300
20301   return objc_get_class_ivars (name);
20302 }
20303
20304 /* Parse an Objective-C protocol expression.
20305
20306   objc-protocol-expression:
20307     @protocol ( identifier )
20308
20309   Returns a representation of the protocol expression.  */
20310
20311 static tree
20312 cp_parser_objc_protocol_expression (cp_parser* parser)
20313 {
20314   tree proto;
20315
20316   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20317   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20318   proto = cp_parser_identifier (parser);
20319   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20320
20321   return objc_build_protocol_expr (proto);
20322 }
20323
20324 /* Parse an Objective-C selector expression.
20325
20326    objc-selector-expression:
20327      @selector ( objc-method-signature )
20328
20329    objc-method-signature:
20330      objc-selector
20331      objc-selector-seq
20332
20333    objc-selector-seq:
20334      objc-selector :
20335      objc-selector-seq objc-selector :
20336
20337   Returns a representation of the method selector.  */
20338
20339 static tree
20340 cp_parser_objc_selector_expression (cp_parser* parser)
20341 {
20342   tree sel_seq = NULL_TREE;
20343   bool maybe_unary_selector_p = true;
20344   cp_token *token;
20345   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20346
20347   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
20348   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20349   token = cp_lexer_peek_token (parser->lexer);
20350
20351   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20352          || token->type == CPP_SCOPE)
20353     {
20354       tree selector = NULL_TREE;
20355
20356       if (token->type != CPP_COLON
20357           || token->type == CPP_SCOPE)
20358         selector = cp_parser_objc_selector (parser);
20359
20360       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20361           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20362         {
20363           /* Detect if we have a unary selector.  */
20364           if (maybe_unary_selector_p)
20365             {
20366               sel_seq = selector;
20367               goto finish_selector;
20368             }
20369           else
20370             {
20371               cp_parser_error (parser, "expected %<:%>");
20372             }
20373         }
20374       maybe_unary_selector_p = false;
20375       token = cp_lexer_consume_token (parser->lexer);
20376
20377       if (token->type == CPP_SCOPE)
20378         {
20379           sel_seq
20380             = chainon (sel_seq,
20381                        build_tree_list (selector, NULL_TREE));
20382           sel_seq
20383             = chainon (sel_seq,
20384                        build_tree_list (NULL_TREE, NULL_TREE));
20385         }
20386       else
20387         sel_seq
20388           = chainon (sel_seq,
20389                      build_tree_list (selector, NULL_TREE));
20390
20391       token = cp_lexer_peek_token (parser->lexer);
20392     }
20393
20394  finish_selector:
20395   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20396
20397   return objc_build_selector_expr (loc, sel_seq);
20398 }
20399
20400 /* Parse a list of identifiers.
20401
20402    objc-identifier-list:
20403      identifier
20404      objc-identifier-list , identifier
20405
20406    Returns a TREE_LIST of identifier nodes.  */
20407
20408 static tree
20409 cp_parser_objc_identifier_list (cp_parser* parser)
20410 {
20411   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20412   cp_token *sep = cp_lexer_peek_token (parser->lexer);
20413
20414   while (sep->type == CPP_COMMA)
20415     {
20416       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20417       list = chainon (list,
20418                       build_tree_list (NULL_TREE,
20419                                        cp_parser_identifier (parser)));
20420       sep = cp_lexer_peek_token (parser->lexer);
20421     }
20422
20423   return list;
20424 }
20425
20426 /* Parse an Objective-C alias declaration.
20427
20428    objc-alias-declaration:
20429      @compatibility_alias identifier identifier ;
20430
20431    This function registers the alias mapping with the Objective-C front end.
20432    It returns nothing.  */
20433
20434 static void
20435 cp_parser_objc_alias_declaration (cp_parser* parser)
20436 {
20437   tree alias, orig;
20438
20439   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
20440   alias = cp_parser_identifier (parser);
20441   orig = cp_parser_identifier (parser);
20442   objc_declare_alias (alias, orig);
20443   cp_parser_consume_semicolon_at_end_of_statement (parser);
20444 }
20445
20446 /* Parse an Objective-C class forward-declaration.
20447
20448    objc-class-declaration:
20449      @class objc-identifier-list ;
20450
20451    The function registers the forward declarations with the Objective-C
20452    front end.  It returns nothing.  */
20453
20454 static void
20455 cp_parser_objc_class_declaration (cp_parser* parser)
20456 {
20457   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
20458   objc_declare_class (cp_parser_objc_identifier_list (parser));
20459   cp_parser_consume_semicolon_at_end_of_statement (parser);
20460 }
20461
20462 /* Parse a list of Objective-C protocol references.
20463
20464    objc-protocol-refs-opt:
20465      objc-protocol-refs [opt]
20466
20467    objc-protocol-refs:
20468      < objc-identifier-list >
20469
20470    Returns a TREE_LIST of identifiers, if any.  */
20471
20472 static tree
20473 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20474 {
20475   tree protorefs = NULL_TREE;
20476
20477   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20478     {
20479       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
20480       protorefs = cp_parser_objc_identifier_list (parser);
20481       cp_parser_require (parser, CPP_GREATER, "%<>%>");
20482     }
20483
20484   return protorefs;
20485 }
20486
20487 /* Parse a Objective-C visibility specification.  */
20488
20489 static void
20490 cp_parser_objc_visibility_spec (cp_parser* parser)
20491 {
20492   cp_token *vis = cp_lexer_peek_token (parser->lexer);
20493
20494   switch (vis->keyword)
20495     {
20496     case RID_AT_PRIVATE:
20497       objc_set_visibility (2);
20498       break;
20499     case RID_AT_PROTECTED:
20500       objc_set_visibility (0);
20501       break;
20502     case RID_AT_PUBLIC:
20503       objc_set_visibility (1);
20504       break;
20505     default:
20506       return;
20507     }
20508
20509   /* Eat '@private'/'@protected'/'@public'.  */
20510   cp_lexer_consume_token (parser->lexer);
20511 }
20512
20513 /* Parse an Objective-C method type.  */
20514
20515 static void
20516 cp_parser_objc_method_type (cp_parser* parser)
20517 {
20518   objc_set_method_type
20519    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20520     ? PLUS_EXPR
20521     : MINUS_EXPR);
20522 }
20523
20524 /* Parse an Objective-C protocol qualifier.  */
20525
20526 static tree
20527 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20528 {
20529   tree quals = NULL_TREE, node;
20530   cp_token *token = cp_lexer_peek_token (parser->lexer);
20531
20532   node = token->u.value;
20533
20534   while (node && TREE_CODE (node) == IDENTIFIER_NODE
20535          && (node == ridpointers [(int) RID_IN]
20536              || node == ridpointers [(int) RID_OUT]
20537              || node == ridpointers [(int) RID_INOUT]
20538              || node == ridpointers [(int) RID_BYCOPY]
20539              || node == ridpointers [(int) RID_BYREF]
20540              || node == ridpointers [(int) RID_ONEWAY]))
20541     {
20542       quals = tree_cons (NULL_TREE, node, quals);
20543       cp_lexer_consume_token (parser->lexer);
20544       token = cp_lexer_peek_token (parser->lexer);
20545       node = token->u.value;
20546     }
20547
20548   return quals;
20549 }
20550
20551 /* Parse an Objective-C typename.  */
20552
20553 static tree
20554 cp_parser_objc_typename (cp_parser* parser)
20555 {
20556   tree type_name = NULL_TREE;
20557
20558   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20559     {
20560       tree proto_quals, cp_type = NULL_TREE;
20561
20562       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20563       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20564
20565       /* An ObjC type name may consist of just protocol qualifiers, in which
20566          case the type shall default to 'id'.  */
20567       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20568         cp_type = cp_parser_type_id (parser);
20569
20570       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20571       type_name = build_tree_list (proto_quals, cp_type);
20572     }
20573
20574   return type_name;
20575 }
20576
20577 /* Check to see if TYPE refers to an Objective-C selector name.  */
20578
20579 static bool
20580 cp_parser_objc_selector_p (enum cpp_ttype type)
20581 {
20582   return (type == CPP_NAME || type == CPP_KEYWORD
20583           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20584           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20585           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20586           || type == CPP_XOR || type == CPP_XOR_EQ);
20587 }
20588
20589 /* Parse an Objective-C selector.  */
20590
20591 static tree
20592 cp_parser_objc_selector (cp_parser* parser)
20593 {
20594   cp_token *token = cp_lexer_consume_token (parser->lexer);
20595
20596   if (!cp_parser_objc_selector_p (token->type))
20597     {
20598       error_at (token->location, "invalid Objective-C++ selector name");
20599       return error_mark_node;
20600     }
20601
20602   /* C++ operator names are allowed to appear in ObjC selectors.  */
20603   switch (token->type)
20604     {
20605     case CPP_AND_AND: return get_identifier ("and");
20606     case CPP_AND_EQ: return get_identifier ("and_eq");
20607     case CPP_AND: return get_identifier ("bitand");
20608     case CPP_OR: return get_identifier ("bitor");
20609     case CPP_COMPL: return get_identifier ("compl");
20610     case CPP_NOT: return get_identifier ("not");
20611     case CPP_NOT_EQ: return get_identifier ("not_eq");
20612     case CPP_OR_OR: return get_identifier ("or");
20613     case CPP_OR_EQ: return get_identifier ("or_eq");
20614     case CPP_XOR: return get_identifier ("xor");
20615     case CPP_XOR_EQ: return get_identifier ("xor_eq");
20616     default: return token->u.value;
20617     }
20618 }
20619
20620 /* Parse an Objective-C params list.  */
20621
20622 static tree
20623 cp_parser_objc_method_keyword_params (cp_parser* parser)
20624 {
20625   tree params = NULL_TREE;
20626   bool maybe_unary_selector_p = true;
20627   cp_token *token = cp_lexer_peek_token (parser->lexer);
20628
20629   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20630     {
20631       tree selector = NULL_TREE, type_name, identifier;
20632
20633       if (token->type != CPP_COLON)
20634         selector = cp_parser_objc_selector (parser);
20635
20636       /* Detect if we have a unary selector.  */
20637       if (maybe_unary_selector_p
20638           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20639         return selector;
20640
20641       maybe_unary_selector_p = false;
20642       cp_parser_require (parser, CPP_COLON, "%<:%>");
20643       type_name = cp_parser_objc_typename (parser);
20644       identifier = cp_parser_identifier (parser);
20645
20646       params
20647         = chainon (params,
20648                    objc_build_keyword_decl (selector,
20649                                             type_name,
20650                                             identifier));
20651
20652       token = cp_lexer_peek_token (parser->lexer);
20653     }
20654
20655   return params;
20656 }
20657
20658 /* Parse the non-keyword Objective-C params.  */
20659
20660 static tree
20661 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20662 {
20663   tree params = make_node (TREE_LIST);
20664   cp_token *token = cp_lexer_peek_token (parser->lexer);
20665   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
20666
20667   while (token->type == CPP_COMMA)
20668     {
20669       cp_parameter_declarator *parmdecl;
20670       tree parm;
20671
20672       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20673       token = cp_lexer_peek_token (parser->lexer);
20674
20675       if (token->type == CPP_ELLIPSIS)
20676         {
20677           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
20678           *ellipsisp = true;
20679           break;
20680         }
20681
20682       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20683       parm = grokdeclarator (parmdecl->declarator,
20684                              &parmdecl->decl_specifiers,
20685                              PARM, /*initialized=*/0,
20686                              /*attrlist=*/NULL);
20687
20688       chainon (params, build_tree_list (NULL_TREE, parm));
20689       token = cp_lexer_peek_token (parser->lexer);
20690     }
20691
20692   return params;
20693 }
20694
20695 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
20696
20697 static void
20698 cp_parser_objc_interstitial_code (cp_parser* parser)
20699 {
20700   cp_token *token = cp_lexer_peek_token (parser->lexer);
20701
20702   /* If the next token is `extern' and the following token is a string
20703      literal, then we have a linkage specification.  */
20704   if (token->keyword == RID_EXTERN
20705       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20706     cp_parser_linkage_specification (parser);
20707   /* Handle #pragma, if any.  */
20708   else if (token->type == CPP_PRAGMA)
20709     cp_parser_pragma (parser, pragma_external);
20710   /* Allow stray semicolons.  */
20711   else if (token->type == CPP_SEMICOLON)
20712     cp_lexer_consume_token (parser->lexer);
20713   /* Finally, try to parse a block-declaration, or a function-definition.  */
20714   else
20715     cp_parser_block_declaration (parser, /*statement_p=*/false);
20716 }
20717
20718 /* Parse a method signature.  */
20719
20720 static tree
20721 cp_parser_objc_method_signature (cp_parser* parser)
20722 {
20723   tree rettype, kwdparms, optparms;
20724   bool ellipsis = false;
20725
20726   cp_parser_objc_method_type (parser);
20727   rettype = cp_parser_objc_typename (parser);
20728   kwdparms = cp_parser_objc_method_keyword_params (parser);
20729   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20730
20731   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20732 }
20733
20734 /* Pars an Objective-C method prototype list.  */
20735
20736 static void
20737 cp_parser_objc_method_prototype_list (cp_parser* parser)
20738 {
20739   cp_token *token = cp_lexer_peek_token (parser->lexer);
20740
20741   while (token->keyword != RID_AT_END)
20742     {
20743       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20744         {
20745           objc_add_method_declaration
20746            (cp_parser_objc_method_signature (parser));
20747           cp_parser_consume_semicolon_at_end_of_statement (parser);
20748         }
20749       else
20750         /* Allow for interspersed non-ObjC++ code.  */
20751         cp_parser_objc_interstitial_code (parser);
20752
20753       token = cp_lexer_peek_token (parser->lexer);
20754     }
20755
20756   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20757   objc_finish_interface ();
20758 }
20759
20760 /* Parse an Objective-C method definition list.  */
20761
20762 static void
20763 cp_parser_objc_method_definition_list (cp_parser* parser)
20764 {
20765   cp_token *token = cp_lexer_peek_token (parser->lexer);
20766
20767   while (token->keyword != RID_AT_END)
20768     {
20769       tree meth;
20770
20771       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20772         {
20773           push_deferring_access_checks (dk_deferred);
20774           objc_start_method_definition
20775            (cp_parser_objc_method_signature (parser));
20776
20777           /* For historical reasons, we accept an optional semicolon.  */
20778           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20779             cp_lexer_consume_token (parser->lexer);
20780
20781           perform_deferred_access_checks ();
20782           stop_deferring_access_checks ();
20783           meth = cp_parser_function_definition_after_declarator (parser,
20784                                                                  false);
20785           pop_deferring_access_checks ();
20786           objc_finish_method_definition (meth);
20787         }
20788       else
20789         /* Allow for interspersed non-ObjC++ code.  */
20790         cp_parser_objc_interstitial_code (parser);
20791
20792       token = cp_lexer_peek_token (parser->lexer);
20793     }
20794
20795   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20796   objc_finish_implementation ();
20797 }
20798
20799 /* Parse Objective-C ivars.  */
20800
20801 static void
20802 cp_parser_objc_class_ivars (cp_parser* parser)
20803 {
20804   cp_token *token = cp_lexer_peek_token (parser->lexer);
20805
20806   if (token->type != CPP_OPEN_BRACE)
20807     return;     /* No ivars specified.  */
20808
20809   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
20810   token = cp_lexer_peek_token (parser->lexer);
20811
20812   while (token->type != CPP_CLOSE_BRACE)
20813     {
20814       cp_decl_specifier_seq declspecs;
20815       int decl_class_or_enum_p;
20816       tree prefix_attributes;
20817
20818       cp_parser_objc_visibility_spec (parser);
20819
20820       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20821         break;
20822
20823       cp_parser_decl_specifier_seq (parser,
20824                                     CP_PARSER_FLAGS_OPTIONAL,
20825                                     &declspecs,
20826                                     &decl_class_or_enum_p);
20827       prefix_attributes = declspecs.attributes;
20828       declspecs.attributes = NULL_TREE;
20829
20830       /* Keep going until we hit the `;' at the end of the
20831          declaration.  */
20832       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20833         {
20834           tree width = NULL_TREE, attributes, first_attribute, decl;
20835           cp_declarator *declarator = NULL;
20836           int ctor_dtor_or_conv_p;
20837
20838           /* Check for a (possibly unnamed) bitfield declaration.  */
20839           token = cp_lexer_peek_token (parser->lexer);
20840           if (token->type == CPP_COLON)
20841             goto eat_colon;
20842
20843           if (token->type == CPP_NAME
20844               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20845                   == CPP_COLON))
20846             {
20847               /* Get the name of the bitfield.  */
20848               declarator = make_id_declarator (NULL_TREE,
20849                                                cp_parser_identifier (parser),
20850                                                sfk_none);
20851
20852              eat_colon:
20853               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20854               /* Get the width of the bitfield.  */
20855               width
20856                 = cp_parser_constant_expression (parser,
20857                                                  /*allow_non_constant=*/false,
20858                                                  NULL);
20859             }
20860           else
20861             {
20862               /* Parse the declarator.  */
20863               declarator
20864                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20865                                         &ctor_dtor_or_conv_p,
20866                                         /*parenthesized_p=*/NULL,
20867                                         /*member_p=*/false);
20868             }
20869
20870           /* Look for attributes that apply to the ivar.  */
20871           attributes = cp_parser_attributes_opt (parser);
20872           /* Remember which attributes are prefix attributes and
20873              which are not.  */
20874           first_attribute = attributes;
20875           /* Combine the attributes.  */
20876           attributes = chainon (prefix_attributes, attributes);
20877
20878           if (width)
20879               /* Create the bitfield declaration.  */
20880               decl = grokbitfield (declarator, &declspecs,
20881                                    width,
20882                                    attributes);
20883           else
20884             decl = grokfield (declarator, &declspecs,
20885                               NULL_TREE, /*init_const_expr_p=*/false,
20886                               NULL_TREE, attributes);
20887
20888           /* Add the instance variable.  */
20889           objc_add_instance_variable (decl);
20890
20891           /* Reset PREFIX_ATTRIBUTES.  */
20892           while (attributes && TREE_CHAIN (attributes) != first_attribute)
20893             attributes = TREE_CHAIN (attributes);
20894           if (attributes)
20895             TREE_CHAIN (attributes) = NULL_TREE;
20896
20897           token = cp_lexer_peek_token (parser->lexer);
20898
20899           if (token->type == CPP_COMMA)
20900             {
20901               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20902               continue;
20903             }
20904           break;
20905         }
20906
20907       cp_parser_consume_semicolon_at_end_of_statement (parser);
20908       token = cp_lexer_peek_token (parser->lexer);
20909     }
20910
20911   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
20912   /* For historical reasons, we accept an optional semicolon.  */
20913   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20914     cp_lexer_consume_token (parser->lexer);
20915 }
20916
20917 /* Parse an Objective-C protocol declaration.  */
20918
20919 static void
20920 cp_parser_objc_protocol_declaration (cp_parser* parser)
20921 {
20922   tree proto, protorefs;
20923   cp_token *tok;
20924
20925   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20926   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20927     {
20928       tok = cp_lexer_peek_token (parser->lexer);
20929       error_at (tok->location, "identifier expected after %<@protocol%>");
20930       goto finish;
20931     }
20932
20933   /* See if we have a forward declaration or a definition.  */
20934   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20935
20936   /* Try a forward declaration first.  */
20937   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20938     {
20939       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20940      finish:
20941       cp_parser_consume_semicolon_at_end_of_statement (parser);
20942     }
20943
20944   /* Ok, we got a full-fledged definition (or at least should).  */
20945   else
20946     {
20947       proto = cp_parser_identifier (parser);
20948       protorefs = cp_parser_objc_protocol_refs_opt (parser);
20949       objc_start_protocol (proto, protorefs);
20950       cp_parser_objc_method_prototype_list (parser);
20951     }
20952 }
20953
20954 /* Parse an Objective-C superclass or category.  */
20955
20956 static void
20957 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20958                                                           tree *categ)
20959 {
20960   cp_token *next = cp_lexer_peek_token (parser->lexer);
20961
20962   *super = *categ = NULL_TREE;
20963   if (next->type == CPP_COLON)
20964     {
20965       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20966       *super = cp_parser_identifier (parser);
20967     }
20968   else if (next->type == CPP_OPEN_PAREN)
20969     {
20970       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20971       *categ = cp_parser_identifier (parser);
20972       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20973     }
20974 }
20975
20976 /* Parse an Objective-C class interface.  */
20977
20978 static void
20979 cp_parser_objc_class_interface (cp_parser* parser)
20980 {
20981   tree name, super, categ, protos;
20982
20983   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20984   name = cp_parser_identifier (parser);
20985   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20986   protos = cp_parser_objc_protocol_refs_opt (parser);
20987
20988   /* We have either a class or a category on our hands.  */
20989   if (categ)
20990     objc_start_category_interface (name, categ, protos);
20991   else
20992     {
20993       objc_start_class_interface (name, super, protos);
20994       /* Handle instance variable declarations, if any.  */
20995       cp_parser_objc_class_ivars (parser);
20996       objc_continue_interface ();
20997     }
20998
20999   cp_parser_objc_method_prototype_list (parser);
21000 }
21001
21002 /* Parse an Objective-C class implementation.  */
21003
21004 static void
21005 cp_parser_objc_class_implementation (cp_parser* parser)
21006 {
21007   tree name, super, categ;
21008
21009   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
21010   name = cp_parser_identifier (parser);
21011   cp_parser_objc_superclass_or_category (parser, &super, &categ);
21012
21013   /* We have either a class or a category on our hands.  */
21014   if (categ)
21015     objc_start_category_implementation (name, categ);
21016   else
21017     {
21018       objc_start_class_implementation (name, super);
21019       /* Handle instance variable declarations, if any.  */
21020       cp_parser_objc_class_ivars (parser);
21021       objc_continue_implementation ();
21022     }
21023
21024   cp_parser_objc_method_definition_list (parser);
21025 }
21026
21027 /* Consume the @end token and finish off the implementation.  */
21028
21029 static void
21030 cp_parser_objc_end_implementation (cp_parser* parser)
21031 {
21032   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
21033   objc_finish_implementation ();
21034 }
21035
21036 /* Parse an Objective-C declaration.  */
21037
21038 static void
21039 cp_parser_objc_declaration (cp_parser* parser)
21040 {
21041   /* Try to figure out what kind of declaration is present.  */
21042   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21043
21044   switch (kwd->keyword)
21045     {
21046     case RID_AT_ALIAS:
21047       cp_parser_objc_alias_declaration (parser);
21048       break;
21049     case RID_AT_CLASS:
21050       cp_parser_objc_class_declaration (parser);
21051       break;
21052     case RID_AT_PROTOCOL:
21053       cp_parser_objc_protocol_declaration (parser);
21054       break;
21055     case RID_AT_INTERFACE:
21056       cp_parser_objc_class_interface (parser);
21057       break;
21058     case RID_AT_IMPLEMENTATION:
21059       cp_parser_objc_class_implementation (parser);
21060       break;
21061     case RID_AT_END:
21062       cp_parser_objc_end_implementation (parser);
21063       break;
21064     default:
21065       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21066                 kwd->u.value);
21067       cp_parser_skip_to_end_of_block_or_statement (parser);
21068     }
21069 }
21070
21071 /* Parse an Objective-C try-catch-finally statement.
21072
21073    objc-try-catch-finally-stmt:
21074      @try compound-statement objc-catch-clause-seq [opt]
21075        objc-finally-clause [opt]
21076
21077    objc-catch-clause-seq:
21078      objc-catch-clause objc-catch-clause-seq [opt]
21079
21080    objc-catch-clause:
21081      @catch ( exception-declaration ) compound-statement
21082
21083    objc-finally-clause
21084      @finally compound-statement
21085
21086    Returns NULL_TREE.  */
21087
21088 static tree
21089 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21090   location_t location;
21091   tree stmt;
21092
21093   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
21094   location = cp_lexer_peek_token (parser->lexer)->location;
21095   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21096      node, lest it get absorbed into the surrounding block.  */
21097   stmt = push_stmt_list ();
21098   cp_parser_compound_statement (parser, NULL, false);
21099   objc_begin_try_stmt (location, pop_stmt_list (stmt));
21100
21101   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21102     {
21103       cp_parameter_declarator *parmdecl;
21104       tree parm;
21105
21106       cp_lexer_consume_token (parser->lexer);
21107       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21108       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21109       parm = grokdeclarator (parmdecl->declarator,
21110                              &parmdecl->decl_specifiers,
21111                              PARM, /*initialized=*/0,
21112                              /*attrlist=*/NULL);
21113       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21114       objc_begin_catch_clause (parm);
21115       cp_parser_compound_statement (parser, NULL, false);
21116       objc_finish_catch_clause ();
21117     }
21118
21119   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21120     {
21121       cp_lexer_consume_token (parser->lexer);
21122       location = cp_lexer_peek_token (parser->lexer)->location;
21123       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21124          node, lest it get absorbed into the surrounding block.  */
21125       stmt = push_stmt_list ();
21126       cp_parser_compound_statement (parser, NULL, false);
21127       objc_build_finally_clause (location, pop_stmt_list (stmt));
21128     }
21129
21130   return objc_finish_try_stmt ();
21131 }
21132
21133 /* Parse an Objective-C synchronized statement.
21134
21135    objc-synchronized-stmt:
21136      @synchronized ( expression ) compound-statement
21137
21138    Returns NULL_TREE.  */
21139
21140 static tree
21141 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21142   location_t location;
21143   tree lock, stmt;
21144
21145   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21146
21147   location = cp_lexer_peek_token (parser->lexer)->location;
21148   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21149   lock = cp_parser_expression (parser, false, NULL);
21150   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21151
21152   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21153      node, lest it get absorbed into the surrounding block.  */
21154   stmt = push_stmt_list ();
21155   cp_parser_compound_statement (parser, NULL, false);
21156
21157   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21158 }
21159
21160 /* Parse an Objective-C throw statement.
21161
21162    objc-throw-stmt:
21163      @throw assignment-expression [opt] ;
21164
21165    Returns a constructed '@throw' statement.  */
21166
21167 static tree
21168 cp_parser_objc_throw_statement (cp_parser *parser) {
21169   tree expr = NULL_TREE;
21170   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21171
21172   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21173
21174   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21175     expr = cp_parser_assignment_expression (parser, false, NULL);
21176
21177   cp_parser_consume_semicolon_at_end_of_statement (parser);
21178
21179   return objc_build_throw_stmt (loc, expr);
21180 }
21181
21182 /* Parse an Objective-C statement.  */
21183
21184 static tree
21185 cp_parser_objc_statement (cp_parser * parser) {
21186   /* Try to figure out what kind of declaration is present.  */
21187   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21188
21189   switch (kwd->keyword)
21190     {
21191     case RID_AT_TRY:
21192       return cp_parser_objc_try_catch_finally_statement (parser);
21193     case RID_AT_SYNCHRONIZED:
21194       return cp_parser_objc_synchronized_statement (parser);
21195     case RID_AT_THROW:
21196       return cp_parser_objc_throw_statement (parser);
21197     default:
21198       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21199                kwd->u.value);
21200       cp_parser_skip_to_end_of_block_or_statement (parser);
21201     }
21202
21203   return error_mark_node;
21204 }
21205 \f
21206 /* OpenMP 2.5 parsing routines.  */
21207
21208 /* Returns name of the next clause.
21209    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21210    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
21211    returned and the token is consumed.  */
21212
21213 static pragma_omp_clause
21214 cp_parser_omp_clause_name (cp_parser *parser)
21215 {
21216   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21217
21218   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21219     result = PRAGMA_OMP_CLAUSE_IF;
21220   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21221     result = PRAGMA_OMP_CLAUSE_DEFAULT;
21222   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21223     result = PRAGMA_OMP_CLAUSE_PRIVATE;
21224   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21225     {
21226       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21227       const char *p = IDENTIFIER_POINTER (id);
21228
21229       switch (p[0])
21230         {
21231         case 'c':
21232           if (!strcmp ("collapse", p))
21233             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21234           else if (!strcmp ("copyin", p))
21235             result = PRAGMA_OMP_CLAUSE_COPYIN;
21236           else if (!strcmp ("copyprivate", p))
21237             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21238           break;
21239         case 'f':
21240           if (!strcmp ("firstprivate", p))
21241             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21242           break;
21243         case 'l':
21244           if (!strcmp ("lastprivate", p))
21245             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21246           break;
21247         case 'n':
21248           if (!strcmp ("nowait", p))
21249             result = PRAGMA_OMP_CLAUSE_NOWAIT;
21250           else if (!strcmp ("num_threads", p))
21251             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21252           break;
21253         case 'o':
21254           if (!strcmp ("ordered", p))
21255             result = PRAGMA_OMP_CLAUSE_ORDERED;
21256           break;
21257         case 'r':
21258           if (!strcmp ("reduction", p))
21259             result = PRAGMA_OMP_CLAUSE_REDUCTION;
21260           break;
21261         case 's':
21262           if (!strcmp ("schedule", p))
21263             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21264           else if (!strcmp ("shared", p))
21265             result = PRAGMA_OMP_CLAUSE_SHARED;
21266           break;
21267         case 'u':
21268           if (!strcmp ("untied", p))
21269             result = PRAGMA_OMP_CLAUSE_UNTIED;
21270           break;
21271         }
21272     }
21273
21274   if (result != PRAGMA_OMP_CLAUSE_NONE)
21275     cp_lexer_consume_token (parser->lexer);
21276
21277   return result;
21278 }
21279
21280 /* Validate that a clause of the given type does not already exist.  */
21281
21282 static void
21283 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21284                            const char *name, location_t location)
21285 {
21286   tree c;
21287
21288   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21289     if (OMP_CLAUSE_CODE (c) == code)
21290       {
21291         error_at (location, "too many %qs clauses", name);
21292         break;
21293       }
21294 }
21295
21296 /* OpenMP 2.5:
21297    variable-list:
21298      identifier
21299      variable-list , identifier
21300
21301    In addition, we match a closing parenthesis.  An opening parenthesis
21302    will have been consumed by the caller.
21303
21304    If KIND is nonzero, create the appropriate node and install the decl
21305    in OMP_CLAUSE_DECL and add the node to the head of the list.
21306
21307    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21308    return the list created.  */
21309
21310 static tree
21311 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21312                                 tree list)
21313 {
21314   cp_token *token;
21315   while (1)
21316     {
21317       tree name, decl;
21318
21319       token = cp_lexer_peek_token (parser->lexer);
21320       name = cp_parser_id_expression (parser, /*template_p=*/false,
21321                                       /*check_dependency_p=*/true,
21322                                       /*template_p=*/NULL,
21323                                       /*declarator_p=*/false,
21324                                       /*optional_p=*/false);
21325       if (name == error_mark_node)
21326         goto skip_comma;
21327
21328       decl = cp_parser_lookup_name_simple (parser, name, token->location);
21329       if (decl == error_mark_node)
21330         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21331       else if (kind != 0)
21332         {
21333           tree u = build_omp_clause (token->location, kind);
21334           OMP_CLAUSE_DECL (u) = decl;
21335           OMP_CLAUSE_CHAIN (u) = list;
21336           list = u;
21337         }
21338       else
21339         list = tree_cons (decl, NULL_TREE, list);
21340
21341     get_comma:
21342       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21343         break;
21344       cp_lexer_consume_token (parser->lexer);
21345     }
21346
21347   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21348     {
21349       int ending;
21350
21351       /* Try to resync to an unnested comma.  Copied from
21352          cp_parser_parenthesized_expression_list.  */
21353     skip_comma:
21354       ending = cp_parser_skip_to_closing_parenthesis (parser,
21355                                                       /*recovering=*/true,
21356                                                       /*or_comma=*/true,
21357                                                       /*consume_paren=*/true);
21358       if (ending < 0)
21359         goto get_comma;
21360     }
21361
21362   return list;
21363 }
21364
21365 /* Similarly, but expect leading and trailing parenthesis.  This is a very
21366    common case for omp clauses.  */
21367
21368 static tree
21369 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21370 {
21371   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21372     return cp_parser_omp_var_list_no_open (parser, kind, list);
21373   return list;
21374 }
21375
21376 /* OpenMP 3.0:
21377    collapse ( constant-expression ) */
21378
21379 static tree
21380 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21381 {
21382   tree c, num;
21383   location_t loc;
21384   HOST_WIDE_INT n;
21385
21386   loc = cp_lexer_peek_token (parser->lexer)->location;
21387   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21388     return list;
21389
21390   num = cp_parser_constant_expression (parser, false, NULL);
21391
21392   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21393     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21394                                            /*or_comma=*/false,
21395                                            /*consume_paren=*/true);
21396
21397   if (num == error_mark_node)
21398     return list;
21399   num = fold_non_dependent_expr (num);
21400   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21401       || !host_integerp (num, 0)
21402       || (n = tree_low_cst (num, 0)) <= 0
21403       || (int) n != n)
21404     {
21405       error_at (loc, "collapse argument needs positive constant integer expression");
21406       return list;
21407     }
21408
21409   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21410   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21411   OMP_CLAUSE_CHAIN (c) = list;
21412   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21413
21414   return c;
21415 }
21416
21417 /* OpenMP 2.5:
21418    default ( shared | none ) */
21419
21420 static tree
21421 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21422 {
21423   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21424   tree c;
21425
21426   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21427     return list;
21428   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21429     {
21430       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21431       const char *p = IDENTIFIER_POINTER (id);
21432
21433       switch (p[0])
21434         {
21435         case 'n':
21436           if (strcmp ("none", p) != 0)
21437             goto invalid_kind;
21438           kind = OMP_CLAUSE_DEFAULT_NONE;
21439           break;
21440
21441         case 's':
21442           if (strcmp ("shared", p) != 0)
21443             goto invalid_kind;
21444           kind = OMP_CLAUSE_DEFAULT_SHARED;
21445           break;
21446
21447         default:
21448           goto invalid_kind;
21449         }
21450
21451       cp_lexer_consume_token (parser->lexer);
21452     }
21453   else
21454     {
21455     invalid_kind:
21456       cp_parser_error (parser, "expected %<none%> or %<shared%>");
21457     }
21458
21459   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21460     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21461                                            /*or_comma=*/false,
21462                                            /*consume_paren=*/true);
21463
21464   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21465     return list;
21466
21467   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21468   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21469   OMP_CLAUSE_CHAIN (c) = list;
21470   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21471
21472   return c;
21473 }
21474
21475 /* OpenMP 2.5:
21476    if ( expression ) */
21477
21478 static tree
21479 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21480 {
21481   tree t, c;
21482
21483   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21484     return list;
21485
21486   t = cp_parser_condition (parser);
21487
21488   if (t == error_mark_node
21489       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21490     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21491                                            /*or_comma=*/false,
21492                                            /*consume_paren=*/true);
21493
21494   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21495
21496   c = build_omp_clause (location, OMP_CLAUSE_IF);
21497   OMP_CLAUSE_IF_EXPR (c) = t;
21498   OMP_CLAUSE_CHAIN (c) = list;
21499
21500   return c;
21501 }
21502
21503 /* OpenMP 2.5:
21504    nowait */
21505
21506 static tree
21507 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21508                              tree list, location_t location)
21509 {
21510   tree c;
21511
21512   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21513
21514   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21515   OMP_CLAUSE_CHAIN (c) = list;
21516   return c;
21517 }
21518
21519 /* OpenMP 2.5:
21520    num_threads ( expression ) */
21521
21522 static tree
21523 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21524                                   location_t location)
21525 {
21526   tree t, c;
21527
21528   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21529     return list;
21530
21531   t = cp_parser_expression (parser, false, NULL);
21532
21533   if (t == error_mark_node
21534       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21535     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21536                                            /*or_comma=*/false,
21537                                            /*consume_paren=*/true);
21538
21539   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21540                              "num_threads", location);
21541
21542   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21543   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21544   OMP_CLAUSE_CHAIN (c) = list;
21545
21546   return c;
21547 }
21548
21549 /* OpenMP 2.5:
21550    ordered */
21551
21552 static tree
21553 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21554                               tree list, location_t location)
21555 {
21556   tree c;
21557
21558   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21559                              "ordered", location);
21560
21561   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21562   OMP_CLAUSE_CHAIN (c) = list;
21563   return c;
21564 }
21565
21566 /* OpenMP 2.5:
21567    reduction ( reduction-operator : variable-list )
21568
21569    reduction-operator:
21570      One of: + * - & ^ | && || */
21571
21572 static tree
21573 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21574 {
21575   enum tree_code code;
21576   tree nlist, c;
21577
21578   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21579     return list;
21580
21581   switch (cp_lexer_peek_token (parser->lexer)->type)
21582     {
21583     case CPP_PLUS:
21584       code = PLUS_EXPR;
21585       break;
21586     case CPP_MULT:
21587       code = MULT_EXPR;
21588       break;
21589     case CPP_MINUS:
21590       code = MINUS_EXPR;
21591       break;
21592     case CPP_AND:
21593       code = BIT_AND_EXPR;
21594       break;
21595     case CPP_XOR:
21596       code = BIT_XOR_EXPR;
21597       break;
21598     case CPP_OR:
21599       code = BIT_IOR_EXPR;
21600       break;
21601     case CPP_AND_AND:
21602       code = TRUTH_ANDIF_EXPR;
21603       break;
21604     case CPP_OR_OR:
21605       code = TRUTH_ORIF_EXPR;
21606       break;
21607     default:
21608       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21609                                "%<|%>, %<&&%>, or %<||%>");
21610     resync_fail:
21611       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21612                                              /*or_comma=*/false,
21613                                              /*consume_paren=*/true);
21614       return list;
21615     }
21616   cp_lexer_consume_token (parser->lexer);
21617
21618   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21619     goto resync_fail;
21620
21621   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21622   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21623     OMP_CLAUSE_REDUCTION_CODE (c) = code;
21624
21625   return nlist;
21626 }
21627
21628 /* OpenMP 2.5:
21629    schedule ( schedule-kind )
21630    schedule ( schedule-kind , expression )
21631
21632    schedule-kind:
21633      static | dynamic | guided | runtime | auto  */
21634
21635 static tree
21636 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21637 {
21638   tree c, t;
21639
21640   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21641     return list;
21642
21643   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21644
21645   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21646     {
21647       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21648       const char *p = IDENTIFIER_POINTER (id);
21649
21650       switch (p[0])
21651         {
21652         case 'd':
21653           if (strcmp ("dynamic", p) != 0)
21654             goto invalid_kind;
21655           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21656           break;
21657
21658         case 'g':
21659           if (strcmp ("guided", p) != 0)
21660             goto invalid_kind;
21661           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21662           break;
21663
21664         case 'r':
21665           if (strcmp ("runtime", p) != 0)
21666             goto invalid_kind;
21667           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21668           break;
21669
21670         default:
21671           goto invalid_kind;
21672         }
21673     }
21674   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21675     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21676   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21677     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21678   else
21679     goto invalid_kind;
21680   cp_lexer_consume_token (parser->lexer);
21681
21682   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21683     {
21684       cp_token *token;
21685       cp_lexer_consume_token (parser->lexer);
21686
21687       token = cp_lexer_peek_token (parser->lexer);
21688       t = cp_parser_assignment_expression (parser, false, NULL);
21689
21690       if (t == error_mark_node)
21691         goto resync_fail;
21692       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21693         error_at (token->location, "schedule %<runtime%> does not take "
21694                   "a %<chunk_size%> parameter");
21695       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21696         error_at (token->location, "schedule %<auto%> does not take "
21697                   "a %<chunk_size%> parameter");
21698       else
21699         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21700
21701       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21702         goto resync_fail;
21703     }
21704   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21705     goto resync_fail;
21706
21707   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21708   OMP_CLAUSE_CHAIN (c) = list;
21709   return c;
21710
21711  invalid_kind:
21712   cp_parser_error (parser, "invalid schedule kind");
21713  resync_fail:
21714   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21715                                          /*or_comma=*/false,
21716                                          /*consume_paren=*/true);
21717   return list;
21718 }
21719
21720 /* OpenMP 3.0:
21721    untied */
21722
21723 static tree
21724 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21725                              tree list, location_t location)
21726 {
21727   tree c;
21728
21729   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21730
21731   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21732   OMP_CLAUSE_CHAIN (c) = list;
21733   return c;
21734 }
21735
21736 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
21737    is a bitmask in MASK.  Return the list of clauses found; the result
21738    of clause default goes in *pdefault.  */
21739
21740 static tree
21741 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21742                            const char *where, cp_token *pragma_tok)
21743 {
21744   tree clauses = NULL;
21745   bool first = true;
21746   cp_token *token = NULL;
21747
21748   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21749     {
21750       pragma_omp_clause c_kind;
21751       const char *c_name;
21752       tree prev = clauses;
21753
21754       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21755         cp_lexer_consume_token (parser->lexer);
21756
21757       token = cp_lexer_peek_token (parser->lexer);
21758       c_kind = cp_parser_omp_clause_name (parser);
21759       first = false;
21760
21761       switch (c_kind)
21762         {
21763         case PRAGMA_OMP_CLAUSE_COLLAPSE:
21764           clauses = cp_parser_omp_clause_collapse (parser, clauses,
21765                                                    token->location);
21766           c_name = "collapse";
21767           break;
21768         case PRAGMA_OMP_CLAUSE_COPYIN:
21769           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21770           c_name = "copyin";
21771           break;
21772         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21773           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21774                                             clauses);
21775           c_name = "copyprivate";
21776           break;
21777         case PRAGMA_OMP_CLAUSE_DEFAULT:
21778           clauses = cp_parser_omp_clause_default (parser, clauses,
21779                                                   token->location);
21780           c_name = "default";
21781           break;
21782         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21783           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21784                                             clauses);
21785           c_name = "firstprivate";
21786           break;
21787         case PRAGMA_OMP_CLAUSE_IF:
21788           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21789           c_name = "if";
21790           break;
21791         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21792           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21793                                             clauses);
21794           c_name = "lastprivate";
21795           break;
21796         case PRAGMA_OMP_CLAUSE_NOWAIT:
21797           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21798           c_name = "nowait";
21799           break;
21800         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21801           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21802                                                       token->location);
21803           c_name = "num_threads";
21804           break;
21805         case PRAGMA_OMP_CLAUSE_ORDERED:
21806           clauses = cp_parser_omp_clause_ordered (parser, clauses,
21807                                                   token->location);
21808           c_name = "ordered";
21809           break;
21810         case PRAGMA_OMP_CLAUSE_PRIVATE:
21811           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21812                                             clauses);
21813           c_name = "private";
21814           break;
21815         case PRAGMA_OMP_CLAUSE_REDUCTION:
21816           clauses = cp_parser_omp_clause_reduction (parser, clauses);
21817           c_name = "reduction";
21818           break;
21819         case PRAGMA_OMP_CLAUSE_SCHEDULE:
21820           clauses = cp_parser_omp_clause_schedule (parser, clauses,
21821                                                    token->location);
21822           c_name = "schedule";
21823           break;
21824         case PRAGMA_OMP_CLAUSE_SHARED:
21825           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21826                                             clauses);
21827           c_name = "shared";
21828           break;
21829         case PRAGMA_OMP_CLAUSE_UNTIED:
21830           clauses = cp_parser_omp_clause_untied (parser, clauses,
21831                                                  token->location);
21832           c_name = "nowait";
21833           break;
21834         default:
21835           cp_parser_error (parser, "expected %<#pragma omp%> clause");
21836           goto saw_error;
21837         }
21838
21839       if (((mask >> c_kind) & 1) == 0)
21840         {
21841           /* Remove the invalid clause(s) from the list to avoid
21842              confusing the rest of the compiler.  */
21843           clauses = prev;
21844           error_at (token->location, "%qs is not valid for %qs", c_name, where);
21845         }
21846     }
21847  saw_error:
21848   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21849   return finish_omp_clauses (clauses);
21850 }
21851
21852 /* OpenMP 2.5:
21853    structured-block:
21854      statement
21855
21856    In practice, we're also interested in adding the statement to an
21857    outer node.  So it is convenient if we work around the fact that
21858    cp_parser_statement calls add_stmt.  */
21859
21860 static unsigned
21861 cp_parser_begin_omp_structured_block (cp_parser *parser)
21862 {
21863   unsigned save = parser->in_statement;
21864
21865   /* Only move the values to IN_OMP_BLOCK if they weren't false.
21866      This preserves the "not within loop or switch" style error messages
21867      for nonsense cases like
21868         void foo() {
21869         #pragma omp single
21870           break;
21871         }
21872   */
21873   if (parser->in_statement)
21874     parser->in_statement = IN_OMP_BLOCK;
21875
21876   return save;
21877 }
21878
21879 static void
21880 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21881 {
21882   parser->in_statement = save;
21883 }
21884
21885 static tree
21886 cp_parser_omp_structured_block (cp_parser *parser)
21887 {
21888   tree stmt = begin_omp_structured_block ();
21889   unsigned int save = cp_parser_begin_omp_structured_block (parser);
21890
21891   cp_parser_statement (parser, NULL_TREE, false, NULL);
21892
21893   cp_parser_end_omp_structured_block (parser, save);
21894   return finish_omp_structured_block (stmt);
21895 }
21896
21897 /* OpenMP 2.5:
21898    # pragma omp atomic new-line
21899      expression-stmt
21900
21901    expression-stmt:
21902      x binop= expr | x++ | ++x | x-- | --x
21903    binop:
21904      +, *, -, /, &, ^, |, <<, >>
21905
21906   where x is an lvalue expression with scalar type.  */
21907
21908 static void
21909 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21910 {
21911   tree lhs, rhs;
21912   enum tree_code code;
21913
21914   cp_parser_require_pragma_eol (parser, pragma_tok);
21915
21916   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21917                                     /*cast_p=*/false, NULL);
21918   switch (TREE_CODE (lhs))
21919     {
21920     case ERROR_MARK:
21921       goto saw_error;
21922
21923     case PREINCREMENT_EXPR:
21924     case POSTINCREMENT_EXPR:
21925       lhs = TREE_OPERAND (lhs, 0);
21926       code = PLUS_EXPR;
21927       rhs = integer_one_node;
21928       break;
21929
21930     case PREDECREMENT_EXPR:
21931     case POSTDECREMENT_EXPR:
21932       lhs = TREE_OPERAND (lhs, 0);
21933       code = MINUS_EXPR;
21934       rhs = integer_one_node;
21935       break;
21936
21937     default:
21938       switch (cp_lexer_peek_token (parser->lexer)->type)
21939         {
21940         case CPP_MULT_EQ:
21941           code = MULT_EXPR;
21942           break;
21943         case CPP_DIV_EQ:
21944           code = TRUNC_DIV_EXPR;
21945           break;
21946         case CPP_PLUS_EQ:
21947           code = PLUS_EXPR;
21948           break;
21949         case CPP_MINUS_EQ:
21950           code = MINUS_EXPR;
21951           break;
21952         case CPP_LSHIFT_EQ:
21953           code = LSHIFT_EXPR;
21954           break;
21955         case CPP_RSHIFT_EQ:
21956           code = RSHIFT_EXPR;
21957           break;
21958         case CPP_AND_EQ:
21959           code = BIT_AND_EXPR;
21960           break;
21961         case CPP_OR_EQ:
21962           code = BIT_IOR_EXPR;
21963           break;
21964         case CPP_XOR_EQ:
21965           code = BIT_XOR_EXPR;
21966           break;
21967         default:
21968           cp_parser_error (parser,
21969                            "invalid operator for %<#pragma omp atomic%>");
21970           goto saw_error;
21971         }
21972       cp_lexer_consume_token (parser->lexer);
21973
21974       rhs = cp_parser_expression (parser, false, NULL);
21975       if (rhs == error_mark_node)
21976         goto saw_error;
21977       break;
21978     }
21979   finish_omp_atomic (code, lhs, rhs);
21980   cp_parser_consume_semicolon_at_end_of_statement (parser);
21981   return;
21982
21983  saw_error:
21984   cp_parser_skip_to_end_of_block_or_statement (parser);
21985 }
21986
21987
21988 /* OpenMP 2.5:
21989    # pragma omp barrier new-line  */
21990
21991 static void
21992 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21993 {
21994   cp_parser_require_pragma_eol (parser, pragma_tok);
21995   finish_omp_barrier ();
21996 }
21997
21998 /* OpenMP 2.5:
21999    # pragma omp critical [(name)] new-line
22000      structured-block  */
22001
22002 static tree
22003 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
22004 {
22005   tree stmt, name = NULL;
22006
22007   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22008     {
22009       cp_lexer_consume_token (parser->lexer);
22010
22011       name = cp_parser_identifier (parser);
22012
22013       if (name == error_mark_node
22014           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22015         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22016                                                /*or_comma=*/false,
22017                                                /*consume_paren=*/true);
22018       if (name == error_mark_node)
22019         name = NULL;
22020     }
22021   cp_parser_require_pragma_eol (parser, pragma_tok);
22022
22023   stmt = cp_parser_omp_structured_block (parser);
22024   return c_finish_omp_critical (input_location, stmt, name);
22025 }
22026
22027 /* OpenMP 2.5:
22028    # pragma omp flush flush-vars[opt] new-line
22029
22030    flush-vars:
22031      ( variable-list ) */
22032
22033 static void
22034 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22035 {
22036   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22037     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22038   cp_parser_require_pragma_eol (parser, pragma_tok);
22039
22040   finish_omp_flush ();
22041 }
22042
22043 /* Helper function, to parse omp for increment expression.  */
22044
22045 static tree
22046 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22047 {
22048   tree cond = cp_parser_binary_expression (parser, false, true,
22049                                            PREC_NOT_OPERATOR, NULL);
22050   bool overloaded_p;
22051
22052   if (cond == error_mark_node
22053       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22054     {
22055       cp_parser_skip_to_end_of_statement (parser);
22056       return error_mark_node;
22057     }
22058
22059   switch (TREE_CODE (cond))
22060     {
22061     case GT_EXPR:
22062     case GE_EXPR:
22063     case LT_EXPR:
22064     case LE_EXPR:
22065       break;
22066     default:
22067       return error_mark_node;
22068     }
22069
22070   /* If decl is an iterator, preserve LHS and RHS of the relational
22071      expr until finish_omp_for.  */
22072   if (decl
22073       && (type_dependent_expression_p (decl)
22074           || CLASS_TYPE_P (TREE_TYPE (decl))))
22075     return cond;
22076
22077   return build_x_binary_op (TREE_CODE (cond),
22078                             TREE_OPERAND (cond, 0), ERROR_MARK,
22079                             TREE_OPERAND (cond, 1), ERROR_MARK,
22080                             &overloaded_p, tf_warning_or_error);
22081 }
22082
22083 /* Helper function, to parse omp for increment expression.  */
22084
22085 static tree
22086 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22087 {
22088   cp_token *token = cp_lexer_peek_token (parser->lexer);
22089   enum tree_code op;
22090   tree lhs, rhs;
22091   cp_id_kind idk;
22092   bool decl_first;
22093
22094   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22095     {
22096       op = (token->type == CPP_PLUS_PLUS
22097             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22098       cp_lexer_consume_token (parser->lexer);
22099       lhs = cp_parser_cast_expression (parser, false, false, NULL);
22100       if (lhs != decl)
22101         return error_mark_node;
22102       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22103     }
22104
22105   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22106   if (lhs != decl)
22107     return error_mark_node;
22108
22109   token = cp_lexer_peek_token (parser->lexer);
22110   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22111     {
22112       op = (token->type == CPP_PLUS_PLUS
22113             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22114       cp_lexer_consume_token (parser->lexer);
22115       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22116     }
22117
22118   op = cp_parser_assignment_operator_opt (parser);
22119   if (op == ERROR_MARK)
22120     return error_mark_node;
22121
22122   if (op != NOP_EXPR)
22123     {
22124       rhs = cp_parser_assignment_expression (parser, false, NULL);
22125       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22126       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22127     }
22128
22129   lhs = cp_parser_binary_expression (parser, false, false,
22130                                      PREC_ADDITIVE_EXPRESSION, NULL);
22131   token = cp_lexer_peek_token (parser->lexer);
22132   decl_first = lhs == decl;
22133   if (decl_first)
22134     lhs = NULL_TREE;
22135   if (token->type != CPP_PLUS
22136       && token->type != CPP_MINUS)
22137     return error_mark_node;
22138
22139   do
22140     {
22141       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22142       cp_lexer_consume_token (parser->lexer);
22143       rhs = cp_parser_binary_expression (parser, false, false,
22144                                          PREC_ADDITIVE_EXPRESSION, NULL);
22145       token = cp_lexer_peek_token (parser->lexer);
22146       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22147         {
22148           if (lhs == NULL_TREE)
22149             {
22150               if (op == PLUS_EXPR)
22151                 lhs = rhs;
22152               else
22153                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22154             }
22155           else
22156             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22157                                      NULL, tf_warning_or_error);
22158         }
22159     }
22160   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22161
22162   if (!decl_first)
22163     {
22164       if (rhs != decl || op == MINUS_EXPR)
22165         return error_mark_node;
22166       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22167     }
22168   else
22169     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22170
22171   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22172 }
22173
22174 /* Parse the restricted form of the for statement allowed by OpenMP.  */
22175
22176 static tree
22177 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22178 {
22179   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22180   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22181   tree this_pre_body, cl;
22182   location_t loc_first;
22183   bool collapse_err = false;
22184   int i, collapse = 1, nbraces = 0;
22185
22186   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22187     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22188       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22189
22190   gcc_assert (collapse >= 1);
22191
22192   declv = make_tree_vec (collapse);
22193   initv = make_tree_vec (collapse);
22194   condv = make_tree_vec (collapse);
22195   incrv = make_tree_vec (collapse);
22196
22197   loc_first = cp_lexer_peek_token (parser->lexer)->location;
22198
22199   for (i = 0; i < collapse; i++)
22200     {
22201       int bracecount = 0;
22202       bool add_private_clause = false;
22203       location_t loc;
22204
22205       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22206         {
22207           cp_parser_error (parser, "for statement expected");
22208           return NULL;
22209         }
22210       loc = cp_lexer_consume_token (parser->lexer)->location;
22211
22212       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22213         return NULL;
22214
22215       init = decl = real_decl = NULL;
22216       this_pre_body = push_stmt_list ();
22217       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22218         {
22219           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22220
22221              init-expr:
22222                        var = lb
22223                        integer-type var = lb
22224                        random-access-iterator-type var = lb
22225                        pointer-type var = lb
22226           */
22227           cp_decl_specifier_seq type_specifiers;
22228
22229           /* First, try to parse as an initialized declaration.  See
22230              cp_parser_condition, from whence the bulk of this is copied.  */
22231
22232           cp_parser_parse_tentatively (parser);
22233           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22234                                         /*is_trailing_return=*/false,
22235                                         &type_specifiers);
22236           if (cp_parser_parse_definitely (parser))
22237             {
22238               /* If parsing a type specifier seq succeeded, then this
22239                  MUST be a initialized declaration.  */
22240               tree asm_specification, attributes;
22241               cp_declarator *declarator;
22242
22243               declarator = cp_parser_declarator (parser,
22244                                                  CP_PARSER_DECLARATOR_NAMED,
22245                                                  /*ctor_dtor_or_conv_p=*/NULL,
22246                                                  /*parenthesized_p=*/NULL,
22247                                                  /*member_p=*/false);
22248               attributes = cp_parser_attributes_opt (parser);
22249               asm_specification = cp_parser_asm_specification_opt (parser);
22250
22251               if (declarator == cp_error_declarator) 
22252                 cp_parser_skip_to_end_of_statement (parser);
22253
22254               else 
22255                 {
22256                   tree pushed_scope, auto_node;
22257
22258                   decl = start_decl (declarator, &type_specifiers,
22259                                      SD_INITIALIZED, attributes,
22260                                      /*prefix_attributes=*/NULL_TREE,
22261                                      &pushed_scope);
22262
22263                   auto_node = type_uses_auto (TREE_TYPE (decl));
22264                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22265                     {
22266                       if (cp_lexer_next_token_is (parser->lexer, 
22267                                                   CPP_OPEN_PAREN))
22268                         error ("parenthesized initialization is not allowed in "
22269                                "OpenMP %<for%> loop");
22270                       else
22271                         /* Trigger an error.  */
22272                         cp_parser_require (parser, CPP_EQ, "%<=%>");
22273
22274                       init = error_mark_node;
22275                       cp_parser_skip_to_end_of_statement (parser);
22276                     }
22277                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
22278                            || type_dependent_expression_p (decl)
22279                            || auto_node)
22280                     {
22281                       bool is_direct_init, is_non_constant_init;
22282
22283                       init = cp_parser_initializer (parser,
22284                                                     &is_direct_init,
22285                                                     &is_non_constant_init);
22286
22287                       if (auto_node && describable_type (init))
22288                         {
22289                           TREE_TYPE (decl)
22290                             = do_auto_deduction (TREE_TYPE (decl), init,
22291                                                  auto_node);
22292
22293                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
22294                               && !type_dependent_expression_p (decl))
22295                             goto non_class;
22296                         }
22297                       
22298                       cp_finish_decl (decl, init, !is_non_constant_init,
22299                                       asm_specification,
22300                                       LOOKUP_ONLYCONVERTING);
22301                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
22302                         {
22303                           for_block
22304                             = tree_cons (NULL, this_pre_body, for_block);
22305                           init = NULL_TREE;
22306                         }
22307                       else
22308                         init = pop_stmt_list (this_pre_body);
22309                       this_pre_body = NULL_TREE;
22310                     }
22311                   else
22312                     {
22313                       /* Consume '='.  */
22314                       cp_lexer_consume_token (parser->lexer);
22315                       init = cp_parser_assignment_expression (parser, false, NULL);
22316
22317                     non_class:
22318                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22319                         init = error_mark_node;
22320                       else
22321                         cp_finish_decl (decl, NULL_TREE,
22322                                         /*init_const_expr_p=*/false,
22323                                         asm_specification,
22324                                         LOOKUP_ONLYCONVERTING);
22325                     }
22326
22327                   if (pushed_scope)
22328                     pop_scope (pushed_scope);
22329                 }
22330             }
22331           else 
22332             {
22333               cp_id_kind idk;
22334               /* If parsing a type specifier sequence failed, then
22335                  this MUST be a simple expression.  */
22336               cp_parser_parse_tentatively (parser);
22337               decl = cp_parser_primary_expression (parser, false, false,
22338                                                    false, &idk);
22339               if (!cp_parser_error_occurred (parser)
22340                   && decl
22341                   && DECL_P (decl)
22342                   && CLASS_TYPE_P (TREE_TYPE (decl)))
22343                 {
22344                   tree rhs;
22345
22346                   cp_parser_parse_definitely (parser);
22347                   cp_parser_require (parser, CPP_EQ, "%<=%>");
22348                   rhs = cp_parser_assignment_expression (parser, false, NULL);
22349                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22350                                                          rhs,
22351                                                          tf_warning_or_error));
22352                   add_private_clause = true;
22353                 }
22354               else
22355                 {
22356                   decl = NULL;
22357                   cp_parser_abort_tentative_parse (parser);
22358                   init = cp_parser_expression (parser, false, NULL);
22359                   if (init)
22360                     {
22361                       if (TREE_CODE (init) == MODIFY_EXPR
22362                           || TREE_CODE (init) == MODOP_EXPR)
22363                         real_decl = TREE_OPERAND (init, 0);
22364                     }
22365                 }
22366             }
22367         }
22368       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22369       if (this_pre_body)
22370         {
22371           this_pre_body = pop_stmt_list (this_pre_body);
22372           if (pre_body)
22373             {
22374               tree t = pre_body;
22375               pre_body = push_stmt_list ();
22376               add_stmt (t);
22377               add_stmt (this_pre_body);
22378               pre_body = pop_stmt_list (pre_body);
22379             }
22380           else
22381             pre_body = this_pre_body;
22382         }
22383
22384       if (decl)
22385         real_decl = decl;
22386       if (par_clauses != NULL && real_decl != NULL_TREE)
22387         {
22388           tree *c;
22389           for (c = par_clauses; *c ; )
22390             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22391                 && OMP_CLAUSE_DECL (*c) == real_decl)
22392               {
22393                 error_at (loc, "iteration variable %qD"
22394                           " should not be firstprivate", real_decl);
22395                 *c = OMP_CLAUSE_CHAIN (*c);
22396               }
22397             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22398                      && OMP_CLAUSE_DECL (*c) == real_decl)
22399               {
22400                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22401                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
22402                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22403                 OMP_CLAUSE_DECL (l) = real_decl;
22404                 OMP_CLAUSE_CHAIN (l) = clauses;
22405                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22406                 clauses = l;
22407                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22408                 CP_OMP_CLAUSE_INFO (*c) = NULL;
22409                 add_private_clause = false;
22410               }
22411             else
22412               {
22413                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22414                     && OMP_CLAUSE_DECL (*c) == real_decl)
22415                   add_private_clause = false;
22416                 c = &OMP_CLAUSE_CHAIN (*c);
22417               }
22418         }
22419
22420       if (add_private_clause)
22421         {
22422           tree c;
22423           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22424             {
22425               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22426                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22427                   && OMP_CLAUSE_DECL (c) == decl)
22428                 break;
22429               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22430                        && OMP_CLAUSE_DECL (c) == decl)
22431                 error_at (loc, "iteration variable %qD "
22432                           "should not be firstprivate",
22433                           decl);
22434               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22435                        && OMP_CLAUSE_DECL (c) == decl)
22436                 error_at (loc, "iteration variable %qD should not be reduction",
22437                           decl);
22438             }
22439           if (c == NULL)
22440             {
22441               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22442               OMP_CLAUSE_DECL (c) = decl;
22443               c = finish_omp_clauses (c);
22444               if (c)
22445                 {
22446                   OMP_CLAUSE_CHAIN (c) = clauses;
22447                   clauses = c;
22448                 }
22449             }
22450         }
22451
22452       cond = NULL;
22453       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22454         cond = cp_parser_omp_for_cond (parser, decl);
22455       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22456
22457       incr = NULL;
22458       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22459         {
22460           /* If decl is an iterator, preserve the operator on decl
22461              until finish_omp_for.  */
22462           if (decl
22463               && (type_dependent_expression_p (decl)
22464                   || CLASS_TYPE_P (TREE_TYPE (decl))))
22465             incr = cp_parser_omp_for_incr (parser, decl);
22466           else
22467             incr = cp_parser_expression (parser, false, NULL);
22468         }
22469
22470       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22471         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22472                                                /*or_comma=*/false,
22473                                                /*consume_paren=*/true);
22474
22475       TREE_VEC_ELT (declv, i) = decl;
22476       TREE_VEC_ELT (initv, i) = init;
22477       TREE_VEC_ELT (condv, i) = cond;
22478       TREE_VEC_ELT (incrv, i) = incr;
22479
22480       if (i == collapse - 1)
22481         break;
22482
22483       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22484          in between the collapsed for loops to be still considered perfectly
22485          nested.  Hopefully the final version clarifies this.
22486          For now handle (multiple) {'s and empty statements.  */
22487       cp_parser_parse_tentatively (parser);
22488       do
22489         {
22490           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22491             break;
22492           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22493             {
22494               cp_lexer_consume_token (parser->lexer);
22495               bracecount++;
22496             }
22497           else if (bracecount
22498                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22499             cp_lexer_consume_token (parser->lexer);
22500           else
22501             {
22502               loc = cp_lexer_peek_token (parser->lexer)->location;
22503               error_at (loc, "not enough collapsed for loops");
22504               collapse_err = true;
22505               cp_parser_abort_tentative_parse (parser);
22506               declv = NULL_TREE;
22507               break;
22508             }
22509         }
22510       while (1);
22511
22512       if (declv)
22513         {
22514           cp_parser_parse_definitely (parser);
22515           nbraces += bracecount;
22516         }
22517     }
22518
22519   /* Note that we saved the original contents of this flag when we entered
22520      the structured block, and so we don't need to re-save it here.  */
22521   parser->in_statement = IN_OMP_FOR;
22522
22523   /* Note that the grammar doesn't call for a structured block here,
22524      though the loop as a whole is a structured block.  */
22525   body = push_stmt_list ();
22526   cp_parser_statement (parser, NULL_TREE, false, NULL);
22527   body = pop_stmt_list (body);
22528
22529   if (declv == NULL_TREE)
22530     ret = NULL_TREE;
22531   else
22532     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22533                           pre_body, clauses);
22534
22535   while (nbraces)
22536     {
22537       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22538         {
22539           cp_lexer_consume_token (parser->lexer);
22540           nbraces--;
22541         }
22542       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22543         cp_lexer_consume_token (parser->lexer);
22544       else
22545         {
22546           if (!collapse_err)
22547             {
22548               error_at (cp_lexer_peek_token (parser->lexer)->location,
22549                         "collapsed loops not perfectly nested");
22550             }
22551           collapse_err = true;
22552           cp_parser_statement_seq_opt (parser, NULL);
22553           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22554             break;
22555         }
22556     }
22557
22558   while (for_block)
22559     {
22560       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22561       for_block = TREE_CHAIN (for_block);
22562     }
22563
22564   return ret;
22565 }
22566
22567 /* OpenMP 2.5:
22568    #pragma omp for for-clause[optseq] new-line
22569      for-loop  */
22570
22571 #define OMP_FOR_CLAUSE_MASK                             \
22572         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22573         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22574         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22575         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22576         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
22577         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
22578         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
22579         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22580
22581 static tree
22582 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22583 {
22584   tree clauses, sb, ret;
22585   unsigned int save;
22586
22587   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22588                                        "#pragma omp for", pragma_tok);
22589
22590   sb = begin_omp_structured_block ();
22591   save = cp_parser_begin_omp_structured_block (parser);
22592
22593   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22594
22595   cp_parser_end_omp_structured_block (parser, save);
22596   add_stmt (finish_omp_structured_block (sb));
22597
22598   return ret;
22599 }
22600
22601 /* OpenMP 2.5:
22602    # pragma omp master new-line
22603      structured-block  */
22604
22605 static tree
22606 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22607 {
22608   cp_parser_require_pragma_eol (parser, pragma_tok);
22609   return c_finish_omp_master (input_location,
22610                               cp_parser_omp_structured_block (parser));
22611 }
22612
22613 /* OpenMP 2.5:
22614    # pragma omp ordered new-line
22615      structured-block  */
22616
22617 static tree
22618 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22619 {
22620   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22621   cp_parser_require_pragma_eol (parser, pragma_tok);
22622   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22623 }
22624
22625 /* OpenMP 2.5:
22626
22627    section-scope:
22628      { section-sequence }
22629
22630    section-sequence:
22631      section-directive[opt] structured-block
22632      section-sequence section-directive structured-block  */
22633
22634 static tree
22635 cp_parser_omp_sections_scope (cp_parser *parser)
22636 {
22637   tree stmt, substmt;
22638   bool error_suppress = false;
22639   cp_token *tok;
22640
22641   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22642     return NULL_TREE;
22643
22644   stmt = push_stmt_list ();
22645
22646   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22647     {
22648       unsigned save;
22649
22650       substmt = begin_omp_structured_block ();
22651       save = cp_parser_begin_omp_structured_block (parser);
22652
22653       while (1)
22654         {
22655           cp_parser_statement (parser, NULL_TREE, false, NULL);
22656
22657           tok = cp_lexer_peek_token (parser->lexer);
22658           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22659             break;
22660           if (tok->type == CPP_CLOSE_BRACE)
22661             break;
22662           if (tok->type == CPP_EOF)
22663             break;
22664         }
22665
22666       cp_parser_end_omp_structured_block (parser, save);
22667       substmt = finish_omp_structured_block (substmt);
22668       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22669       add_stmt (substmt);
22670     }
22671
22672   while (1)
22673     {
22674       tok = cp_lexer_peek_token (parser->lexer);
22675       if (tok->type == CPP_CLOSE_BRACE)
22676         break;
22677       if (tok->type == CPP_EOF)
22678         break;
22679
22680       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22681         {
22682           cp_lexer_consume_token (parser->lexer);
22683           cp_parser_require_pragma_eol (parser, tok);
22684           error_suppress = false;
22685         }
22686       else if (!error_suppress)
22687         {
22688           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22689           error_suppress = true;
22690         }
22691
22692       substmt = cp_parser_omp_structured_block (parser);
22693       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22694       add_stmt (substmt);
22695     }
22696   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22697
22698   substmt = pop_stmt_list (stmt);
22699
22700   stmt = make_node (OMP_SECTIONS);
22701   TREE_TYPE (stmt) = void_type_node;
22702   OMP_SECTIONS_BODY (stmt) = substmt;
22703
22704   add_stmt (stmt);
22705   return stmt;
22706 }
22707
22708 /* OpenMP 2.5:
22709    # pragma omp sections sections-clause[optseq] newline
22710      sections-scope  */
22711
22712 #define OMP_SECTIONS_CLAUSE_MASK                        \
22713         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22714         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22715         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22716         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22717         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22718
22719 static tree
22720 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22721 {
22722   tree clauses, ret;
22723
22724   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22725                                        "#pragma omp sections", pragma_tok);
22726
22727   ret = cp_parser_omp_sections_scope (parser);
22728   if (ret)
22729     OMP_SECTIONS_CLAUSES (ret) = clauses;
22730
22731   return ret;
22732 }
22733
22734 /* OpenMP 2.5:
22735    # pragma parallel parallel-clause new-line
22736    # pragma parallel for parallel-for-clause new-line
22737    # pragma parallel sections parallel-sections-clause new-line  */
22738
22739 #define OMP_PARALLEL_CLAUSE_MASK                        \
22740         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22741         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22742         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22743         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22744         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
22745         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
22746         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22747         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22748
22749 static tree
22750 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22751 {
22752   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22753   const char *p_name = "#pragma omp parallel";
22754   tree stmt, clauses, par_clause, ws_clause, block;
22755   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22756   unsigned int save;
22757   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22758
22759   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22760     {
22761       cp_lexer_consume_token (parser->lexer);
22762       p_kind = PRAGMA_OMP_PARALLEL_FOR;
22763       p_name = "#pragma omp parallel for";
22764       mask |= OMP_FOR_CLAUSE_MASK;
22765       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22766     }
22767   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22768     {
22769       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22770       const char *p = IDENTIFIER_POINTER (id);
22771       if (strcmp (p, "sections") == 0)
22772         {
22773           cp_lexer_consume_token (parser->lexer);
22774           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22775           p_name = "#pragma omp parallel sections";
22776           mask |= OMP_SECTIONS_CLAUSE_MASK;
22777           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22778         }
22779     }
22780
22781   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22782   block = begin_omp_parallel ();
22783   save = cp_parser_begin_omp_structured_block (parser);
22784
22785   switch (p_kind)
22786     {
22787     case PRAGMA_OMP_PARALLEL:
22788       cp_parser_statement (parser, NULL_TREE, false, NULL);
22789       par_clause = clauses;
22790       break;
22791
22792     case PRAGMA_OMP_PARALLEL_FOR:
22793       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22794       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22795       break;
22796
22797     case PRAGMA_OMP_PARALLEL_SECTIONS:
22798       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22799       stmt = cp_parser_omp_sections_scope (parser);
22800       if (stmt)
22801         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22802       break;
22803
22804     default:
22805       gcc_unreachable ();
22806     }
22807
22808   cp_parser_end_omp_structured_block (parser, save);
22809   stmt = finish_omp_parallel (par_clause, block);
22810   if (p_kind != PRAGMA_OMP_PARALLEL)
22811     OMP_PARALLEL_COMBINED (stmt) = 1;
22812   return stmt;
22813 }
22814
22815 /* OpenMP 2.5:
22816    # pragma omp single single-clause[optseq] new-line
22817      structured-block  */
22818
22819 #define OMP_SINGLE_CLAUSE_MASK                          \
22820         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22821         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22822         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
22823         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22824
22825 static tree
22826 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22827 {
22828   tree stmt = make_node (OMP_SINGLE);
22829   TREE_TYPE (stmt) = void_type_node;
22830
22831   OMP_SINGLE_CLAUSES (stmt)
22832     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22833                                  "#pragma omp single", pragma_tok);
22834   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22835
22836   return add_stmt (stmt);
22837 }
22838
22839 /* OpenMP 3.0:
22840    # pragma omp task task-clause[optseq] new-line
22841      structured-block  */
22842
22843 #define OMP_TASK_CLAUSE_MASK                            \
22844         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22845         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
22846         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22847         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22848         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22849         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22850
22851 static tree
22852 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22853 {
22854   tree clauses, block;
22855   unsigned int save;
22856
22857   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22858                                        "#pragma omp task", pragma_tok);
22859   block = begin_omp_task ();
22860   save = cp_parser_begin_omp_structured_block (parser);
22861   cp_parser_statement (parser, NULL_TREE, false, NULL);
22862   cp_parser_end_omp_structured_block (parser, save);
22863   return finish_omp_task (clauses, block);
22864 }
22865
22866 /* OpenMP 3.0:
22867    # pragma omp taskwait new-line  */
22868
22869 static void
22870 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22871 {
22872   cp_parser_require_pragma_eol (parser, pragma_tok);
22873   finish_omp_taskwait ();
22874 }
22875
22876 /* OpenMP 2.5:
22877    # pragma omp threadprivate (variable-list) */
22878
22879 static void
22880 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22881 {
22882   tree vars;
22883
22884   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22885   cp_parser_require_pragma_eol (parser, pragma_tok);
22886
22887   finish_omp_threadprivate (vars);
22888 }
22889
22890 /* Main entry point to OpenMP statement pragmas.  */
22891
22892 static void
22893 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22894 {
22895   tree stmt;
22896
22897   switch (pragma_tok->pragma_kind)
22898     {
22899     case PRAGMA_OMP_ATOMIC:
22900       cp_parser_omp_atomic (parser, pragma_tok);
22901       return;
22902     case PRAGMA_OMP_CRITICAL:
22903       stmt = cp_parser_omp_critical (parser, pragma_tok);
22904       break;
22905     case PRAGMA_OMP_FOR:
22906       stmt = cp_parser_omp_for (parser, pragma_tok);
22907       break;
22908     case PRAGMA_OMP_MASTER:
22909       stmt = cp_parser_omp_master (parser, pragma_tok);
22910       break;
22911     case PRAGMA_OMP_ORDERED:
22912       stmt = cp_parser_omp_ordered (parser, pragma_tok);
22913       break;
22914     case PRAGMA_OMP_PARALLEL:
22915       stmt = cp_parser_omp_parallel (parser, pragma_tok);
22916       break;
22917     case PRAGMA_OMP_SECTIONS:
22918       stmt = cp_parser_omp_sections (parser, pragma_tok);
22919       break;
22920     case PRAGMA_OMP_SINGLE:
22921       stmt = cp_parser_omp_single (parser, pragma_tok);
22922       break;
22923     case PRAGMA_OMP_TASK:
22924       stmt = cp_parser_omp_task (parser, pragma_tok);
22925       break;
22926     default:
22927       gcc_unreachable ();
22928     }
22929
22930   if (stmt)
22931     SET_EXPR_LOCATION (stmt, pragma_tok->location);
22932 }
22933 \f
22934 /* The parser.  */
22935
22936 static GTY (()) cp_parser *the_parser;
22937
22938 \f
22939 /* Special handling for the first token or line in the file.  The first
22940    thing in the file might be #pragma GCC pch_preprocess, which loads a
22941    PCH file, which is a GC collection point.  So we need to handle this
22942    first pragma without benefit of an existing lexer structure.
22943
22944    Always returns one token to the caller in *FIRST_TOKEN.  This is
22945    either the true first token of the file, or the first token after
22946    the initial pragma.  */
22947
22948 static void
22949 cp_parser_initial_pragma (cp_token *first_token)
22950 {
22951   tree name = NULL;
22952
22953   cp_lexer_get_preprocessor_token (NULL, first_token);
22954   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22955     return;
22956
22957   cp_lexer_get_preprocessor_token (NULL, first_token);
22958   if (first_token->type == CPP_STRING)
22959     {
22960       name = first_token->u.value;
22961
22962       cp_lexer_get_preprocessor_token (NULL, first_token);
22963       if (first_token->type != CPP_PRAGMA_EOL)
22964         error_at (first_token->location,
22965                   "junk at end of %<#pragma GCC pch_preprocess%>");
22966     }
22967   else
22968     error_at (first_token->location, "expected string literal");
22969
22970   /* Skip to the end of the pragma.  */
22971   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22972     cp_lexer_get_preprocessor_token (NULL, first_token);
22973
22974   /* Now actually load the PCH file.  */
22975   if (name)
22976     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22977
22978   /* Read one more token to return to our caller.  We have to do this
22979      after reading the PCH file in, since its pointers have to be
22980      live.  */
22981   cp_lexer_get_preprocessor_token (NULL, first_token);
22982 }
22983
22984 /* Normal parsing of a pragma token.  Here we can (and must) use the
22985    regular lexer.  */
22986
22987 static bool
22988 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22989 {
22990   cp_token *pragma_tok;
22991   unsigned int id;
22992
22993   pragma_tok = cp_lexer_consume_token (parser->lexer);
22994   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22995   parser->lexer->in_pragma = true;
22996
22997   id = pragma_tok->pragma_kind;
22998   switch (id)
22999     {
23000     case PRAGMA_GCC_PCH_PREPROCESS:
23001       error_at (pragma_tok->location,
23002                 "%<#pragma GCC pch_preprocess%> must be first");
23003       break;
23004
23005     case PRAGMA_OMP_BARRIER:
23006       switch (context)
23007         {
23008         case pragma_compound:
23009           cp_parser_omp_barrier (parser, pragma_tok);
23010           return false;
23011         case pragma_stmt:
23012           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
23013                     "used in compound statements");
23014           break;
23015         default:
23016           goto bad_stmt;
23017         }
23018       break;
23019
23020     case PRAGMA_OMP_FLUSH:
23021       switch (context)
23022         {
23023         case pragma_compound:
23024           cp_parser_omp_flush (parser, pragma_tok);
23025           return false;
23026         case pragma_stmt:
23027           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23028                     "used in compound statements");
23029           break;
23030         default:
23031           goto bad_stmt;
23032         }
23033       break;
23034
23035     case PRAGMA_OMP_TASKWAIT:
23036       switch (context)
23037         {
23038         case pragma_compound:
23039           cp_parser_omp_taskwait (parser, pragma_tok);
23040           return false;
23041         case pragma_stmt:
23042           error_at (pragma_tok->location,
23043                     "%<#pragma omp taskwait%> may only be "
23044                     "used in compound statements");
23045           break;
23046         default:
23047           goto bad_stmt;
23048         }
23049       break;
23050
23051     case PRAGMA_OMP_THREADPRIVATE:
23052       cp_parser_omp_threadprivate (parser, pragma_tok);
23053       return false;
23054
23055     case PRAGMA_OMP_ATOMIC:
23056     case PRAGMA_OMP_CRITICAL:
23057     case PRAGMA_OMP_FOR:
23058     case PRAGMA_OMP_MASTER:
23059     case PRAGMA_OMP_ORDERED:
23060     case PRAGMA_OMP_PARALLEL:
23061     case PRAGMA_OMP_SECTIONS:
23062     case PRAGMA_OMP_SINGLE:
23063     case PRAGMA_OMP_TASK:
23064       if (context == pragma_external)
23065         goto bad_stmt;
23066       cp_parser_omp_construct (parser, pragma_tok);
23067       return true;
23068
23069     case PRAGMA_OMP_SECTION:
23070       error_at (pragma_tok->location, 
23071                 "%<#pragma omp section%> may only be used in "
23072                 "%<#pragma omp sections%> construct");
23073       break;
23074
23075     default:
23076       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23077       c_invoke_pragma_handler (id);
23078       break;
23079
23080     bad_stmt:
23081       cp_parser_error (parser, "expected declaration specifiers");
23082       break;
23083     }
23084
23085   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23086   return false;
23087 }
23088
23089 /* The interface the pragma parsers have to the lexer.  */
23090
23091 enum cpp_ttype
23092 pragma_lex (tree *value)
23093 {
23094   cp_token *tok;
23095   enum cpp_ttype ret;
23096
23097   tok = cp_lexer_peek_token (the_parser->lexer);
23098
23099   ret = tok->type;
23100   *value = tok->u.value;
23101
23102   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23103     ret = CPP_EOF;
23104   else if (ret == CPP_STRING)
23105     *value = cp_parser_string_literal (the_parser, false, false);
23106   else
23107     {
23108       cp_lexer_consume_token (the_parser->lexer);
23109       if (ret == CPP_KEYWORD)
23110         ret = CPP_NAME;
23111     }
23112
23113   return ret;
23114 }
23115
23116 \f
23117 /* External interface.  */
23118
23119 /* Parse one entire translation unit.  */
23120
23121 void
23122 c_parse_file (void)
23123 {
23124   static bool already_called = false;
23125
23126   if (already_called)
23127     {
23128       sorry ("inter-module optimizations not implemented for C++");
23129       return;
23130     }
23131   already_called = true;
23132
23133   the_parser = cp_parser_new ();
23134   push_deferring_access_checks (flag_access_control
23135                                 ? dk_no_deferred : dk_no_check);
23136   cp_parser_translation_unit (the_parser);
23137   the_parser = NULL;
23138 }
23139
23140 #include "gt-cp-parser.h"