OSDN Git Service

690f2c05eb0028c6c701da4697d960f08969d924
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "intl.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-common.h"
41 #include "plugin.h"
42
43 \f
44 /* The lexer.  */
45
46 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47    and c-lex.c) and the C++ parser.  */
48
49 /* A token's value and its associated deferred access checks and
50    qualifying scope.  */
51
52 struct GTY(()) tree_check {
53   /* The value associated with the token.  */
54   tree value;
55   /* The checks that have been associated with value.  */
56   VEC (deferred_access_check, gc)* checks;
57   /* The token's qualifying scope (used when it is a
58      CPP_NESTED_NAME_SPECIFIER).  */
59   tree qualifying_scope;
60 };
61
62 /* A C++ token.  */
63
64 typedef struct GTY (()) cp_token {
65   /* The kind of token.  */
66   ENUM_BITFIELD (cpp_ttype) type : 8;
67   /* If this token is a keyword, this value indicates which keyword.
68      Otherwise, this value is RID_MAX.  */
69   ENUM_BITFIELD (rid) keyword : 8;
70   /* Token flags.  */
71   unsigned char flags;
72   /* Identifier for the pragma.  */
73   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74   /* True if this token is from a context where it is implicitly extern "C" */
75   BOOL_BITFIELD implicit_extern_c : 1;
76   /* True for a CPP_NAME token that is not a keyword (i.e., for which
77      KEYWORD is RID_MAX) iff this name was looked up and found to be
78      ambiguous.  An error has already been reported.  */
79   BOOL_BITFIELD ambiguous_p : 1;
80   /* The location at which this token was found.  */
81   location_t location;
82   /* The value associated with this token, if any.  */
83   union cp_token_value {
84     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
85     struct tree_check* GTY((tag ("1"))) tree_check_value;
86     /* Use for all other tokens.  */
87     tree GTY((tag ("0"))) value;
88   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89 } cp_token;
90
91 /* We use a stack of token pointer for saving token sets.  */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
95
96 static cp_token eof_token =
97 {
98   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
99 };
100
101 /* The cp_lexer structure represents the C++ lexer.  It is responsible
102    for managing the token stream from the preprocessor and supplying
103    it to the parser.  Tokens are never added to the cp_lexer after
104    it is created.  */
105
106 typedef struct GTY (()) cp_lexer {
107   /* The memory allocated for the buffer.  NULL if this lexer does not
108      own the token buffer.  */
109   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
110   /* If the lexer owns the buffer, this is the number of tokens in the
111      buffer.  */
112   size_t buffer_length;
113
114   /* A pointer just past the last available token.  The tokens
115      in this lexer are [buffer, last_token).  */
116   cp_token_position GTY ((skip)) last_token;
117
118   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
119      no more available tokens.  */
120   cp_token_position GTY ((skip)) next_token;
121
122   /* A stack indicating positions at which cp_lexer_save_tokens was
123      called.  The top entry is the most recent position at which we
124      began saving tokens.  If the stack is non-empty, we are saving
125      tokens.  */
126   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
127
128   /* The next lexer in a linked list of lexers.  */
129   struct cp_lexer *next;
130
131   /* True if we should output debugging information.  */
132   bool debugging_p;
133
134   /* True if we're in the context of parsing a pragma, and should not
135      increment past the end-of-line marker.  */
136   bool in_pragma;
137 } cp_lexer;
138
139 /* cp_token_cache is a range of tokens.  There is no need to represent
140    allocate heap memory for it, since tokens are never removed from the
141    lexer's array.  There is also no need for the GC to walk through
142    a cp_token_cache, since everything in here is referenced through
143    a lexer.  */
144
145 typedef struct GTY(()) cp_token_cache {
146   /* The beginning of the token range.  */
147   cp_token * GTY((skip)) first;
148
149   /* Points immediately after the last token in the range.  */
150   cp_token * GTY ((skip)) last;
151 } cp_token_cache;
152
153 /* Prototypes.  */
154
155 static cp_lexer *cp_lexer_new_main
156   (void);
157 static cp_lexer *cp_lexer_new_from_tokens
158   (cp_token_cache *tokens);
159 static void cp_lexer_destroy
160   (cp_lexer *);
161 static int cp_lexer_saving_tokens
162   (const cp_lexer *);
163 static cp_token_position cp_lexer_token_position
164   (cp_lexer *, bool);
165 static cp_token *cp_lexer_token_at
166   (cp_lexer *, cp_token_position);
167 static void cp_lexer_get_preprocessor_token
168   (cp_lexer *, cp_token *);
169 static inline cp_token *cp_lexer_peek_token
170   (cp_lexer *);
171 static cp_token *cp_lexer_peek_nth_token
172   (cp_lexer *, size_t);
173 static inline bool cp_lexer_next_token_is
174   (cp_lexer *, enum cpp_ttype);
175 static bool cp_lexer_next_token_is_not
176   (cp_lexer *, enum cpp_ttype);
177 static bool cp_lexer_next_token_is_keyword
178   (cp_lexer *, enum rid);
179 static cp_token *cp_lexer_consume_token
180   (cp_lexer *);
181 static void cp_lexer_purge_token
182   (cp_lexer *);
183 static void cp_lexer_purge_tokens_after
184   (cp_lexer *, cp_token_position);
185 static void cp_lexer_save_tokens
186   (cp_lexer *);
187 static void cp_lexer_commit_tokens
188   (cp_lexer *);
189 static void cp_lexer_rollback_tokens
190   (cp_lexer *);
191 #ifdef ENABLE_CHECKING
192 static void cp_lexer_print_token
193   (FILE *, cp_token *);
194 static inline bool cp_lexer_debugging_p
195   (cp_lexer *);
196 static void cp_lexer_start_debugging
197   (cp_lexer *) ATTRIBUTE_UNUSED;
198 static void cp_lexer_stop_debugging
199   (cp_lexer *) ATTRIBUTE_UNUSED;
200 #else
201 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
202    about passing NULL to functions that require non-NULL arguments
203    (fputs, fprintf).  It will never be used, so all we need is a value
204    of the right type that's guaranteed not to be NULL.  */
205 #define cp_lexer_debug_stream stdout
206 #define cp_lexer_print_token(str, tok) (void) 0
207 #define cp_lexer_debugging_p(lexer) 0
208 #endif /* ENABLE_CHECKING */
209
210 static cp_token_cache *cp_token_cache_new
211   (cp_token *, cp_token *);
212
213 static void cp_parser_initial_pragma
214   (cp_token *);
215
216 /* Manifest constants.  */
217 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
218 #define CP_SAVED_TOKEN_STACK 5
219
220 /* A token type for keywords, as opposed to ordinary identifiers.  */
221 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
222
223 /* A token type for template-ids.  If a template-id is processed while
224    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
225    the value of the CPP_TEMPLATE_ID is whatever was returned by
226    cp_parser_template_id.  */
227 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
228
229 /* A token type for nested-name-specifiers.  If a
230    nested-name-specifier is processed while parsing tentatively, it is
231    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
232    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
233    cp_parser_nested_name_specifier_opt.  */
234 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
235
236 /* A token type for tokens that are not tokens at all; these are used
237    to represent slots in the array where there used to be a token
238    that has now been deleted.  */
239 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
240
241 /* The number of token types, including C++-specific ones.  */
242 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
243
244 /* Variables.  */
245
246 #ifdef ENABLE_CHECKING
247 /* The stream to which debugging output should be written.  */
248 static FILE *cp_lexer_debug_stream;
249 #endif /* ENABLE_CHECKING */
250
251 /* Nonzero if we are parsing an unevaluated operand: an operand to
252    sizeof, typeof, or alignof.  */
253 int cp_unevaluated_operand;
254
255 /* Create a new main C++ lexer, the lexer that gets tokens from the
256    preprocessor.  */
257
258 static cp_lexer *
259 cp_lexer_new_main (void)
260 {
261   cp_token first_token;
262   cp_lexer *lexer;
263   cp_token *pos;
264   size_t alloc;
265   size_t space;
266   cp_token *buffer;
267
268   /* It's possible that parsing the first pragma will load a PCH file,
269      which is a GC collection point.  So we have to do that before
270      allocating any memory.  */
271   cp_parser_initial_pragma (&first_token);
272
273   c_common_no_more_pch ();
274
275   /* Allocate the memory.  */
276   lexer = GGC_CNEW (cp_lexer);
277
278 #ifdef ENABLE_CHECKING
279   /* Initially we are not debugging.  */
280   lexer->debugging_p = false;
281 #endif /* ENABLE_CHECKING */
282   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
283                                    CP_SAVED_TOKEN_STACK);
284
285   /* Create the buffer.  */
286   alloc = CP_LEXER_BUFFER_SIZE;
287   buffer = GGC_NEWVEC (cp_token, alloc);
288
289   /* Put the first token in the buffer.  */
290   space = alloc;
291   pos = buffer;
292   *pos = first_token;
293
294   /* Get the remaining tokens from the preprocessor.  */
295   while (pos->type != CPP_EOF)
296     {
297       pos++;
298       if (!--space)
299         {
300           space = alloc;
301           alloc *= 2;
302           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
303           pos = buffer + space;
304         }
305       cp_lexer_get_preprocessor_token (lexer, pos);
306     }
307   lexer->buffer = buffer;
308   lexer->buffer_length = alloc - space;
309   lexer->last_token = pos;
310   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
311
312   /* Subsequent preprocessor diagnostics should use compiler
313      diagnostic functions to get the compiler source location.  */
314   done_lexing = true;
315
316   gcc_assert (lexer->next_token->type != CPP_PURGED);
317   return lexer;
318 }
319
320 /* Create a new lexer whose token stream is primed with the tokens in
321    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
322
323 static cp_lexer *
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
325 {
326   cp_token *first = cache->first;
327   cp_token *last = cache->last;
328   cp_lexer *lexer = GGC_CNEW (cp_lexer);
329
330   /* We do not own the buffer.  */
331   lexer->buffer = NULL;
332   lexer->buffer_length = 0;
333   lexer->next_token = first == last ? &eof_token : first;
334   lexer->last_token = last;
335
336   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337                                    CP_SAVED_TOKEN_STACK);
338
339 #ifdef ENABLE_CHECKING
340   /* Initially we are not debugging.  */
341   lexer->debugging_p = false;
342 #endif
343
344   gcc_assert (lexer->next_token->type != CPP_PURGED);
345   return lexer;
346 }
347
348 /* Frees all resources associated with LEXER.  */
349
350 static void
351 cp_lexer_destroy (cp_lexer *lexer)
352 {
353   if (lexer->buffer)
354     ggc_free (lexer->buffer);
355   VEC_free (cp_token_position, heap, lexer->saved_tokens);
356   ggc_free (lexer);
357 }
358
359 /* Returns nonzero if debugging information should be output.  */
360
361 #ifdef ENABLE_CHECKING
362
363 static inline bool
364 cp_lexer_debugging_p (cp_lexer *lexer)
365 {
366   return lexer->debugging_p;
367 }
368
369 #endif /* ENABLE_CHECKING */
370
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373 {
374   gcc_assert (!previous_p || lexer->next_token != &eof_token);
375
376   return lexer->next_token - previous_p;
377 }
378
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
381 {
382   return pos;
383 }
384
385 /* nonzero if we are presently saving tokens.  */
386
387 static inline int
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
389 {
390   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
391 }
392
393 /* Store the next token from the preprocessor in *TOKEN.  Return true
394    if we reach EOF.  If LEXER is NULL, assume we are handling an
395    initial #pragma pch_preprocess, and thus want the lexer to return
396    processed strings.  */
397
398 static void
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400 {
401   static int is_extern_c = 0;
402
403    /* Get a new token from the preprocessor.  */
404   token->type
405     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
407   token->keyword = RID_MAX;
408   token->pragma_kind = PRAGMA_NONE;
409
410   /* On some systems, some header files are surrounded by an
411      implicit extern "C" block.  Set a flag in the token if it
412      comes from such a header.  */
413   is_extern_c += pending_lang_change;
414   pending_lang_change = 0;
415   token->implicit_extern_c = is_extern_c > 0;
416
417   /* Check to see if this token is a keyword.  */
418   if (token->type == CPP_NAME)
419     {
420       if (C_IS_RESERVED_WORD (token->u.value))
421         {
422           /* Mark this token as a keyword.  */
423           token->type = CPP_KEYWORD;
424           /* Record which keyword.  */
425           token->keyword = C_RID_CODE (token->u.value);
426         }
427       else
428         {
429           if (warn_cxx0x_compat
430               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
431               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
432             {
433               /* Warn about the C++0x keyword (but still treat it as
434                  an identifier).  */
435               warning (OPT_Wc__0x_compat, 
436                        "identifier %qE will become a keyword in C++0x",
437                        token->u.value);
438
439               /* Clear out the C_RID_CODE so we don't warn about this
440                  particular identifier-turned-keyword again.  */
441               C_SET_RID_CODE (token->u.value, RID_MAX);
442             }
443
444           token->ambiguous_p = false;
445           token->keyword = RID_MAX;
446         }
447     }
448   /* Handle Objective-C++ keywords.  */
449   else if (token->type == CPP_AT_NAME)
450     {
451       token->type = CPP_KEYWORD;
452       switch (C_RID_CODE (token->u.value))
453         {
454         /* Map 'class' to '@class', 'private' to '@private', etc.  */
455         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
456         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
457         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
458         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
459         case RID_THROW: token->keyword = RID_AT_THROW; break;
460         case RID_TRY: token->keyword = RID_AT_TRY; break;
461         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
462         default: token->keyword = C_RID_CODE (token->u.value);
463         }
464     }
465   else if (token->type == CPP_PRAGMA)
466     {
467       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
468       token->pragma_kind = ((enum pragma_kind)
469                             TREE_INT_CST_LOW (token->u.value));
470       token->u.value = NULL_TREE;
471     }
472 }
473
474 /* Update the globals input_location and the input file stack from TOKEN.  */
475 static inline void
476 cp_lexer_set_source_position_from_token (cp_token *token)
477 {
478   if (token->type != CPP_EOF)
479     {
480       input_location = token->location;
481     }
482 }
483
484 /* Return a pointer to the next token in the token stream, but do not
485    consume it.  */
486
487 static inline cp_token *
488 cp_lexer_peek_token (cp_lexer *lexer)
489 {
490   if (cp_lexer_debugging_p (lexer))
491     {
492       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
493       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
494       putc ('\n', cp_lexer_debug_stream);
495     }
496   return lexer->next_token;
497 }
498
499 /* Return true if the next token has the indicated TYPE.  */
500
501 static inline bool
502 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
503 {
504   return cp_lexer_peek_token (lexer)->type == type;
505 }
506
507 /* Return true if the next token does not have the indicated TYPE.  */
508
509 static inline bool
510 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
511 {
512   return !cp_lexer_next_token_is (lexer, type);
513 }
514
515 /* Return true if the next token is the indicated KEYWORD.  */
516
517 static inline bool
518 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
519 {
520   return cp_lexer_peek_token (lexer)->keyword == keyword;
521 }
522
523 /* Return true if the next token is not the indicated KEYWORD.  */
524
525 static inline bool
526 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
527 {
528   return cp_lexer_peek_token (lexer)->keyword != keyword;
529 }
530
531 /* Return true if the next token is a keyword for a decl-specifier.  */
532
533 static bool
534 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
535 {
536   cp_token *token;
537
538   token = cp_lexer_peek_token (lexer);
539   switch (token->keyword) 
540     {
541       /* auto specifier: storage-class-specifier in C++,
542          simple-type-specifier in C++0x.  */
543     case RID_AUTO:
544       /* Storage classes.  */
545     case RID_REGISTER:
546     case RID_STATIC:
547     case RID_EXTERN:
548     case RID_MUTABLE:
549     case RID_THREAD:
550       /* Elaborated type specifiers.  */
551     case RID_ENUM:
552     case RID_CLASS:
553     case RID_STRUCT:
554     case RID_UNION:
555     case RID_TYPENAME:
556       /* Simple type specifiers.  */
557     case RID_CHAR:
558     case RID_CHAR16:
559     case RID_CHAR32:
560     case RID_WCHAR:
561     case RID_BOOL:
562     case RID_SHORT:
563     case RID_INT:
564     case RID_LONG:
565     case RID_SIGNED:
566     case RID_UNSIGNED:
567     case RID_FLOAT:
568     case RID_DOUBLE:
569     case RID_VOID:
570       /* GNU extensions.  */ 
571     case RID_ATTRIBUTE:
572     case RID_TYPEOF:
573       /* C++0x extensions.  */
574     case RID_DECLTYPE:
575       return true;
576
577     default:
578       return false;
579     }
580 }
581
582 /* Return a pointer to the Nth token in the token stream.  If N is 1,
583    then this is precisely equivalent to cp_lexer_peek_token (except
584    that it is not inline).  One would like to disallow that case, but
585    there is one case (cp_parser_nth_token_starts_template_id) where
586    the caller passes a variable for N and it might be 1.  */
587
588 static cp_token *
589 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
590 {
591   cp_token *token;
592
593   /* N is 1-based, not zero-based.  */
594   gcc_assert (n > 0);
595
596   if (cp_lexer_debugging_p (lexer))
597     fprintf (cp_lexer_debug_stream,
598              "cp_lexer: peeking ahead %ld at token: ", (long)n);
599
600   --n;
601   token = lexer->next_token;
602   gcc_assert (!n || token != &eof_token);
603   while (n != 0)
604     {
605       ++token;
606       if (token == lexer->last_token)
607         {
608           token = &eof_token;
609           break;
610         }
611
612       if (token->type != CPP_PURGED)
613         --n;
614     }
615
616   if (cp_lexer_debugging_p (lexer))
617     {
618       cp_lexer_print_token (cp_lexer_debug_stream, token);
619       putc ('\n', cp_lexer_debug_stream);
620     }
621
622   return token;
623 }
624
625 /* Return the next token, and advance the lexer's next_token pointer
626    to point to the next non-purged token.  */
627
628 static cp_token *
629 cp_lexer_consume_token (cp_lexer* lexer)
630 {
631   cp_token *token = lexer->next_token;
632
633   gcc_assert (token != &eof_token);
634   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
635
636   do
637     {
638       lexer->next_token++;
639       if (lexer->next_token == lexer->last_token)
640         {
641           lexer->next_token = &eof_token;
642           break;
643         }
644
645     }
646   while (lexer->next_token->type == CPP_PURGED);
647
648   cp_lexer_set_source_position_from_token (token);
649
650   /* Provide debugging output.  */
651   if (cp_lexer_debugging_p (lexer))
652     {
653       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
654       cp_lexer_print_token (cp_lexer_debug_stream, token);
655       putc ('\n', cp_lexer_debug_stream);
656     }
657
658   return token;
659 }
660
661 /* Permanently remove the next token from the token stream, and
662    advance the next_token pointer to refer to the next non-purged
663    token.  */
664
665 static void
666 cp_lexer_purge_token (cp_lexer *lexer)
667 {
668   cp_token *tok = lexer->next_token;
669
670   gcc_assert (tok != &eof_token);
671   tok->type = CPP_PURGED;
672   tok->location = UNKNOWN_LOCATION;
673   tok->u.value = NULL_TREE;
674   tok->keyword = RID_MAX;
675
676   do
677     {
678       tok++;
679       if (tok == lexer->last_token)
680         {
681           tok = &eof_token;
682           break;
683         }
684     }
685   while (tok->type == CPP_PURGED);
686   lexer->next_token = tok;
687 }
688
689 /* Permanently remove all tokens after TOK, up to, but not
690    including, the token that will be returned next by
691    cp_lexer_peek_token.  */
692
693 static void
694 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
695 {
696   cp_token *peek = lexer->next_token;
697
698   if (peek == &eof_token)
699     peek = lexer->last_token;
700
701   gcc_assert (tok < peek);
702
703   for ( tok += 1; tok != peek; tok += 1)
704     {
705       tok->type = CPP_PURGED;
706       tok->location = UNKNOWN_LOCATION;
707       tok->u.value = NULL_TREE;
708       tok->keyword = RID_MAX;
709     }
710 }
711
712 /* Begin saving tokens.  All tokens consumed after this point will be
713    preserved.  */
714
715 static void
716 cp_lexer_save_tokens (cp_lexer* lexer)
717 {
718   /* Provide debugging output.  */
719   if (cp_lexer_debugging_p (lexer))
720     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
721
722   VEC_safe_push (cp_token_position, heap,
723                  lexer->saved_tokens, lexer->next_token);
724 }
725
726 /* Commit to the portion of the token stream most recently saved.  */
727
728 static void
729 cp_lexer_commit_tokens (cp_lexer* lexer)
730 {
731   /* Provide debugging output.  */
732   if (cp_lexer_debugging_p (lexer))
733     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
734
735   VEC_pop (cp_token_position, lexer->saved_tokens);
736 }
737
738 /* Return all tokens saved since the last call to cp_lexer_save_tokens
739    to the token stream.  Stop saving tokens.  */
740
741 static void
742 cp_lexer_rollback_tokens (cp_lexer* lexer)
743 {
744   /* Provide debugging output.  */
745   if (cp_lexer_debugging_p (lexer))
746     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
747
748   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
749 }
750
751 /* Print a representation of the TOKEN on the STREAM.  */
752
753 #ifdef ENABLE_CHECKING
754
755 static void
756 cp_lexer_print_token (FILE * stream, cp_token *token)
757 {
758   /* We don't use cpp_type2name here because the parser defines
759      a few tokens of its own.  */
760   static const char *const token_names[] = {
761     /* cpplib-defined token types */
762 #define OP(e, s) #e,
763 #define TK(e, s) #e,
764     TTYPE_TABLE
765 #undef OP
766 #undef TK
767     /* C++ parser token types - see "Manifest constants", above.  */
768     "KEYWORD",
769     "TEMPLATE_ID",
770     "NESTED_NAME_SPECIFIER",
771     "PURGED"
772   };
773
774   /* If we have a name for the token, print it out.  Otherwise, we
775      simply give the numeric code.  */
776   gcc_assert (token->type < ARRAY_SIZE(token_names));
777   fputs (token_names[token->type], stream);
778
779   /* For some tokens, print the associated data.  */
780   switch (token->type)
781     {
782     case CPP_KEYWORD:
783       /* Some keywords have a value that is not an IDENTIFIER_NODE.
784          For example, `struct' is mapped to an INTEGER_CST.  */
785       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
786         break;
787       /* else fall through */
788     case CPP_NAME:
789       fputs (IDENTIFIER_POINTER (token->u.value), stream);
790       break;
791
792     case CPP_STRING:
793     case CPP_STRING16:
794     case CPP_STRING32:
795     case CPP_WSTRING:
796     case CPP_UTF8STRING:
797       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
798       break;
799
800     default:
801       break;
802     }
803 }
804
805 /* Start emitting debugging information.  */
806
807 static void
808 cp_lexer_start_debugging (cp_lexer* lexer)
809 {
810   lexer->debugging_p = true;
811 }
812
813 /* Stop emitting debugging information.  */
814
815 static void
816 cp_lexer_stop_debugging (cp_lexer* lexer)
817 {
818   lexer->debugging_p = false;
819 }
820
821 #endif /* ENABLE_CHECKING */
822
823 /* Create a new cp_token_cache, representing a range of tokens.  */
824
825 static cp_token_cache *
826 cp_token_cache_new (cp_token *first, cp_token *last)
827 {
828   cp_token_cache *cache = GGC_NEW (cp_token_cache);
829   cache->first = first;
830   cache->last = last;
831   return cache;
832 }
833
834 \f
835 /* Decl-specifiers.  */
836
837 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
838
839 static void
840 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
841 {
842   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
843 }
844
845 /* Declarators.  */
846
847 /* Nothing other than the parser should be creating declarators;
848    declarators are a semi-syntactic representation of C++ entities.
849    Other parts of the front end that need to create entities (like
850    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
851
852 static cp_declarator *make_call_declarator
853   (cp_declarator *, tree, cp_cv_quals, tree, tree);
854 static cp_declarator *make_array_declarator
855   (cp_declarator *, tree);
856 static cp_declarator *make_pointer_declarator
857   (cp_cv_quals, cp_declarator *);
858 static cp_declarator *make_reference_declarator
859   (cp_cv_quals, cp_declarator *, bool);
860 static cp_parameter_declarator *make_parameter_declarator
861   (cp_decl_specifier_seq *, cp_declarator *, tree);
862 static cp_declarator *make_ptrmem_declarator
863   (cp_cv_quals, tree, cp_declarator *);
864
865 /* An erroneous declarator.  */
866 static cp_declarator *cp_error_declarator;
867
868 /* The obstack on which declarators and related data structures are
869    allocated.  */
870 static struct obstack declarator_obstack;
871
872 /* Alloc BYTES from the declarator memory pool.  */
873
874 static inline void *
875 alloc_declarator (size_t bytes)
876 {
877   return obstack_alloc (&declarator_obstack, bytes);
878 }
879
880 /* Allocate a declarator of the indicated KIND.  Clear fields that are
881    common to all declarators.  */
882
883 static cp_declarator *
884 make_declarator (cp_declarator_kind kind)
885 {
886   cp_declarator *declarator;
887
888   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
889   declarator->kind = kind;
890   declarator->attributes = NULL_TREE;
891   declarator->declarator = NULL;
892   declarator->parameter_pack_p = false;
893
894   return declarator;
895 }
896
897 /* Make a declarator for a generalized identifier.  If
898    QUALIFYING_SCOPE is non-NULL, the identifier is
899    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
900    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
901    is, if any.   */
902
903 static cp_declarator *
904 make_id_declarator (tree qualifying_scope, tree unqualified_name,
905                     special_function_kind sfk)
906 {
907   cp_declarator *declarator;
908
909   /* It is valid to write:
910
911        class C { void f(); };
912        typedef C D;
913        void D::f();
914
915      The standard is not clear about whether `typedef const C D' is
916      legal; as of 2002-09-15 the committee is considering that
917      question.  EDG 3.0 allows that syntax.  Therefore, we do as
918      well.  */
919   if (qualifying_scope && TYPE_P (qualifying_scope))
920     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
921
922   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
923               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
924               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
925
926   declarator = make_declarator (cdk_id);
927   declarator->u.id.qualifying_scope = qualifying_scope;
928   declarator->u.id.unqualified_name = unqualified_name;
929   declarator->u.id.sfk = sfk;
930   
931   return declarator;
932 }
933
934 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
935    of modifiers such as const or volatile to apply to the pointer
936    type, represented as identifiers.  */
937
938 cp_declarator *
939 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
940 {
941   cp_declarator *declarator;
942
943   declarator = make_declarator (cdk_pointer);
944   declarator->declarator = target;
945   declarator->u.pointer.qualifiers = cv_qualifiers;
946   declarator->u.pointer.class_type = NULL_TREE;
947   if (target)
948     {
949       declarator->parameter_pack_p = target->parameter_pack_p;
950       target->parameter_pack_p = false;
951     }
952   else
953     declarator->parameter_pack_p = false;
954
955   return declarator;
956 }
957
958 /* Like make_pointer_declarator -- but for references.  */
959
960 cp_declarator *
961 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
962                            bool rvalue_ref)
963 {
964   cp_declarator *declarator;
965
966   declarator = make_declarator (cdk_reference);
967   declarator->declarator = target;
968   declarator->u.reference.qualifiers = cv_qualifiers;
969   declarator->u.reference.rvalue_ref = rvalue_ref;
970   if (target)
971     {
972       declarator->parameter_pack_p = target->parameter_pack_p;
973       target->parameter_pack_p = false;
974     }
975   else
976     declarator->parameter_pack_p = false;
977
978   return declarator;
979 }
980
981 /* Like make_pointer_declarator -- but for a pointer to a non-static
982    member of CLASS_TYPE.  */
983
984 cp_declarator *
985 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
986                         cp_declarator *pointee)
987 {
988   cp_declarator *declarator;
989
990   declarator = make_declarator (cdk_ptrmem);
991   declarator->declarator = pointee;
992   declarator->u.pointer.qualifiers = cv_qualifiers;
993   declarator->u.pointer.class_type = class_type;
994
995   if (pointee)
996     {
997       declarator->parameter_pack_p = pointee->parameter_pack_p;
998       pointee->parameter_pack_p = false;
999     }
1000   else
1001     declarator->parameter_pack_p = false;
1002
1003   return declarator;
1004 }
1005
1006 /* Make a declarator for the function given by TARGET, with the
1007    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1008    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1009    indicates what exceptions can be thrown.  */
1010
1011 cp_declarator *
1012 make_call_declarator (cp_declarator *target,
1013                       tree parms,
1014                       cp_cv_quals cv_qualifiers,
1015                       tree exception_specification,
1016                       tree late_return_type)
1017 {
1018   cp_declarator *declarator;
1019
1020   declarator = make_declarator (cdk_function);
1021   declarator->declarator = target;
1022   declarator->u.function.parameters = parms;
1023   declarator->u.function.qualifiers = cv_qualifiers;
1024   declarator->u.function.exception_specification = exception_specification;
1025   declarator->u.function.late_return_type = late_return_type;
1026   if (target)
1027     {
1028       declarator->parameter_pack_p = target->parameter_pack_p;
1029       target->parameter_pack_p = false;
1030     }
1031   else
1032     declarator->parameter_pack_p = false;
1033
1034   return declarator;
1035 }
1036
1037 /* Make a declarator for an array of BOUNDS elements, each of which is
1038    defined by ELEMENT.  */
1039
1040 cp_declarator *
1041 make_array_declarator (cp_declarator *element, tree bounds)
1042 {
1043   cp_declarator *declarator;
1044
1045   declarator = make_declarator (cdk_array);
1046   declarator->declarator = element;
1047   declarator->u.array.bounds = bounds;
1048   if (element)
1049     {
1050       declarator->parameter_pack_p = element->parameter_pack_p;
1051       element->parameter_pack_p = false;
1052     }
1053   else
1054     declarator->parameter_pack_p = false;
1055
1056   return declarator;
1057 }
1058
1059 /* Determine whether the declarator we've seen so far can be a
1060    parameter pack, when followed by an ellipsis.  */
1061 static bool 
1062 declarator_can_be_parameter_pack (cp_declarator *declarator)
1063 {
1064   /* Search for a declarator name, or any other declarator that goes
1065      after the point where the ellipsis could appear in a parameter
1066      pack. If we find any of these, then this declarator can not be
1067      made into a parameter pack.  */
1068   bool found = false;
1069   while (declarator && !found)
1070     {
1071       switch ((int)declarator->kind)
1072         {
1073         case cdk_id:
1074         case cdk_array:
1075           found = true;
1076           break;
1077
1078         case cdk_error:
1079           return true;
1080
1081         default:
1082           declarator = declarator->declarator;
1083           break;
1084         }
1085     }
1086
1087   return !found;
1088 }
1089
1090 cp_parameter_declarator *no_parameters;
1091
1092 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1093    DECLARATOR and DEFAULT_ARGUMENT.  */
1094
1095 cp_parameter_declarator *
1096 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1097                            cp_declarator *declarator,
1098                            tree default_argument)
1099 {
1100   cp_parameter_declarator *parameter;
1101
1102   parameter = ((cp_parameter_declarator *)
1103                alloc_declarator (sizeof (cp_parameter_declarator)));
1104   parameter->next = NULL;
1105   if (decl_specifiers)
1106     parameter->decl_specifiers = *decl_specifiers;
1107   else
1108     clear_decl_specs (&parameter->decl_specifiers);
1109   parameter->declarator = declarator;
1110   parameter->default_argument = default_argument;
1111   parameter->ellipsis_p = false;
1112
1113   return parameter;
1114 }
1115
1116 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1117
1118 static bool
1119 function_declarator_p (const cp_declarator *declarator)
1120 {
1121   while (declarator)
1122     {
1123       if (declarator->kind == cdk_function
1124           && declarator->declarator->kind == cdk_id)
1125         return true;
1126       if (declarator->kind == cdk_id
1127           || declarator->kind == cdk_error)
1128         return false;
1129       declarator = declarator->declarator;
1130     }
1131   return false;
1132 }
1133  
1134 /* The parser.  */
1135
1136 /* Overview
1137    --------
1138
1139    A cp_parser parses the token stream as specified by the C++
1140    grammar.  Its job is purely parsing, not semantic analysis.  For
1141    example, the parser breaks the token stream into declarators,
1142    expressions, statements, and other similar syntactic constructs.
1143    It does not check that the types of the expressions on either side
1144    of an assignment-statement are compatible, or that a function is
1145    not declared with a parameter of type `void'.
1146
1147    The parser invokes routines elsewhere in the compiler to perform
1148    semantic analysis and to build up the abstract syntax tree for the
1149    code processed.
1150
1151    The parser (and the template instantiation code, which is, in a
1152    way, a close relative of parsing) are the only parts of the
1153    compiler that should be calling push_scope and pop_scope, or
1154    related functions.  The parser (and template instantiation code)
1155    keeps track of what scope is presently active; everything else
1156    should simply honor that.  (The code that generates static
1157    initializers may also need to set the scope, in order to check
1158    access control correctly when emitting the initializers.)
1159
1160    Methodology
1161    -----------
1162
1163    The parser is of the standard recursive-descent variety.  Upcoming
1164    tokens in the token stream are examined in order to determine which
1165    production to use when parsing a non-terminal.  Some C++ constructs
1166    require arbitrary look ahead to disambiguate.  For example, it is
1167    impossible, in the general case, to tell whether a statement is an
1168    expression or declaration without scanning the entire statement.
1169    Therefore, the parser is capable of "parsing tentatively."  When the
1170    parser is not sure what construct comes next, it enters this mode.
1171    Then, while we attempt to parse the construct, the parser queues up
1172    error messages, rather than issuing them immediately, and saves the
1173    tokens it consumes.  If the construct is parsed successfully, the
1174    parser "commits", i.e., it issues any queued error messages and
1175    the tokens that were being preserved are permanently discarded.
1176    If, however, the construct is not parsed successfully, the parser
1177    rolls back its state completely so that it can resume parsing using
1178    a different alternative.
1179
1180    Future Improvements
1181    -------------------
1182
1183    The performance of the parser could probably be improved substantially.
1184    We could often eliminate the need to parse tentatively by looking ahead
1185    a little bit.  In some places, this approach might not entirely eliminate
1186    the need to parse tentatively, but it might still speed up the average
1187    case.  */
1188
1189 /* Flags that are passed to some parsing functions.  These values can
1190    be bitwise-ored together.  */
1191
1192 enum
1193 {
1194   /* No flags.  */
1195   CP_PARSER_FLAGS_NONE = 0x0,
1196   /* The construct is optional.  If it is not present, then no error
1197      should be issued.  */
1198   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1199   /* When parsing a type-specifier, treat user-defined type-names
1200      as non-type identifiers.  */
1201   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1202   /* When parsing a type-specifier, do not try to parse a class-specifier
1203      or enum-specifier.  */
1204   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1205 };
1206
1207 /* This type is used for parameters and variables which hold
1208    combinations of the above flags.  */
1209 typedef int cp_parser_flags;
1210
1211 /* The different kinds of declarators we want to parse.  */
1212
1213 typedef enum cp_parser_declarator_kind
1214 {
1215   /* We want an abstract declarator.  */
1216   CP_PARSER_DECLARATOR_ABSTRACT,
1217   /* We want a named declarator.  */
1218   CP_PARSER_DECLARATOR_NAMED,
1219   /* We don't mind, but the name must be an unqualified-id.  */
1220   CP_PARSER_DECLARATOR_EITHER
1221 } cp_parser_declarator_kind;
1222
1223 /* The precedence values used to parse binary expressions.  The minimum value
1224    of PREC must be 1, because zero is reserved to quickly discriminate
1225    binary operators from other tokens.  */
1226
1227 enum cp_parser_prec
1228 {
1229   PREC_NOT_OPERATOR,
1230   PREC_LOGICAL_OR_EXPRESSION,
1231   PREC_LOGICAL_AND_EXPRESSION,
1232   PREC_INCLUSIVE_OR_EXPRESSION,
1233   PREC_EXCLUSIVE_OR_EXPRESSION,
1234   PREC_AND_EXPRESSION,
1235   PREC_EQUALITY_EXPRESSION,
1236   PREC_RELATIONAL_EXPRESSION,
1237   PREC_SHIFT_EXPRESSION,
1238   PREC_ADDITIVE_EXPRESSION,
1239   PREC_MULTIPLICATIVE_EXPRESSION,
1240   PREC_PM_EXPRESSION,
1241   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1242 };
1243
1244 /* A mapping from a token type to a corresponding tree node type, with a
1245    precedence value.  */
1246
1247 typedef struct cp_parser_binary_operations_map_node
1248 {
1249   /* The token type.  */
1250   enum cpp_ttype token_type;
1251   /* The corresponding tree code.  */
1252   enum tree_code tree_type;
1253   /* The precedence of this operator.  */
1254   enum cp_parser_prec prec;
1255 } cp_parser_binary_operations_map_node;
1256
1257 /* The status of a tentative parse.  */
1258
1259 typedef enum cp_parser_status_kind
1260 {
1261   /* No errors have occurred.  */
1262   CP_PARSER_STATUS_KIND_NO_ERROR,
1263   /* An error has occurred.  */
1264   CP_PARSER_STATUS_KIND_ERROR,
1265   /* We are committed to this tentative parse, whether or not an error
1266      has occurred.  */
1267   CP_PARSER_STATUS_KIND_COMMITTED
1268 } cp_parser_status_kind;
1269
1270 typedef struct cp_parser_expression_stack_entry
1271 {
1272   /* Left hand side of the binary operation we are currently
1273      parsing.  */
1274   tree lhs;
1275   /* Original tree code for left hand side, if it was a binary
1276      expression itself (used for -Wparentheses).  */
1277   enum tree_code lhs_type;
1278   /* Tree code for the binary operation we are parsing.  */
1279   enum tree_code tree_type;
1280   /* Precedence of the binary operation we are parsing.  */
1281   enum cp_parser_prec prec;
1282 } cp_parser_expression_stack_entry;
1283
1284 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1285    entries because precedence levels on the stack are monotonically
1286    increasing.  */
1287 typedef struct cp_parser_expression_stack_entry
1288   cp_parser_expression_stack[NUM_PREC_VALUES];
1289
1290 /* Context that is saved and restored when parsing tentatively.  */
1291 typedef struct GTY (()) cp_parser_context {
1292   /* If this is a tentative parsing context, the status of the
1293      tentative parse.  */
1294   enum cp_parser_status_kind status;
1295   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1296      that are looked up in this context must be looked up both in the
1297      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1298      the context of the containing expression.  */
1299   tree object_type;
1300
1301   /* The next parsing context in the stack.  */
1302   struct cp_parser_context *next;
1303 } cp_parser_context;
1304
1305 /* Prototypes.  */
1306
1307 /* Constructors and destructors.  */
1308
1309 static cp_parser_context *cp_parser_context_new
1310   (cp_parser_context *);
1311
1312 /* Class variables.  */
1313
1314 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1315
1316 /* The operator-precedence table used by cp_parser_binary_expression.
1317    Transformed into an associative array (binops_by_token) by
1318    cp_parser_new.  */
1319
1320 static const cp_parser_binary_operations_map_node binops[] = {
1321   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1322   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1323
1324   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1325   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1326   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1327
1328   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1329   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1330
1331   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1332   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1333
1334   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1335   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1336   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1337   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1338
1339   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1340   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1341
1342   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1343
1344   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1345
1346   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1347
1348   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1349
1350   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1351 };
1352
1353 /* The same as binops, but initialized by cp_parser_new so that
1354    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1355    for speed.  */
1356 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1357
1358 /* Constructors and destructors.  */
1359
1360 /* Construct a new context.  The context below this one on the stack
1361    is given by NEXT.  */
1362
1363 static cp_parser_context *
1364 cp_parser_context_new (cp_parser_context* next)
1365 {
1366   cp_parser_context *context;
1367
1368   /* Allocate the storage.  */
1369   if (cp_parser_context_free_list != NULL)
1370     {
1371       /* Pull the first entry from the free list.  */
1372       context = cp_parser_context_free_list;
1373       cp_parser_context_free_list = context->next;
1374       memset (context, 0, sizeof (*context));
1375     }
1376   else
1377     context = GGC_CNEW (cp_parser_context);
1378
1379   /* No errors have occurred yet in this context.  */
1380   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1381   /* If this is not the bottommost context, copy information that we
1382      need from the previous context.  */
1383   if (next)
1384     {
1385       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1386          expression, then we are parsing one in this context, too.  */
1387       context->object_type = next->object_type;
1388       /* Thread the stack.  */
1389       context->next = next;
1390     }
1391
1392   return context;
1393 }
1394
1395 /* The cp_parser structure represents the C++ parser.  */
1396
1397 typedef struct GTY(()) cp_parser {
1398   /* The lexer from which we are obtaining tokens.  */
1399   cp_lexer *lexer;
1400
1401   /* The scope in which names should be looked up.  If NULL_TREE, then
1402      we look up names in the scope that is currently open in the
1403      source program.  If non-NULL, this is either a TYPE or
1404      NAMESPACE_DECL for the scope in which we should look.  It can
1405      also be ERROR_MARK, when we've parsed a bogus scope.
1406
1407      This value is not cleared automatically after a name is looked
1408      up, so we must be careful to clear it before starting a new look
1409      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1410      will look up `Z' in the scope of `X', rather than the current
1411      scope.)  Unfortunately, it is difficult to tell when name lookup
1412      is complete, because we sometimes peek at a token, look it up,
1413      and then decide not to consume it.   */
1414   tree scope;
1415
1416   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1417      last lookup took place.  OBJECT_SCOPE is used if an expression
1418      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1419      respectively.  QUALIFYING_SCOPE is used for an expression of the
1420      form "X::Y"; it refers to X.  */
1421   tree object_scope;
1422   tree qualifying_scope;
1423
1424   /* A stack of parsing contexts.  All but the bottom entry on the
1425      stack will be tentative contexts.
1426
1427      We parse tentatively in order to determine which construct is in
1428      use in some situations.  For example, in order to determine
1429      whether a statement is an expression-statement or a
1430      declaration-statement we parse it tentatively as a
1431      declaration-statement.  If that fails, we then reparse the same
1432      token stream as an expression-statement.  */
1433   cp_parser_context *context;
1434
1435   /* True if we are parsing GNU C++.  If this flag is not set, then
1436      GNU extensions are not recognized.  */
1437   bool allow_gnu_extensions_p;
1438
1439   /* TRUE if the `>' token should be interpreted as the greater-than
1440      operator.  FALSE if it is the end of a template-id or
1441      template-parameter-list. In C++0x mode, this flag also applies to
1442      `>>' tokens, which are viewed as two consecutive `>' tokens when
1443      this flag is FALSE.  */
1444   bool greater_than_is_operator_p;
1445
1446   /* TRUE if default arguments are allowed within a parameter list
1447      that starts at this point. FALSE if only a gnu extension makes
1448      them permissible.  */
1449   bool default_arg_ok_p;
1450
1451   /* TRUE if we are parsing an integral constant-expression.  See
1452      [expr.const] for a precise definition.  */
1453   bool integral_constant_expression_p;
1454
1455   /* TRUE if we are parsing an integral constant-expression -- but a
1456      non-constant expression should be permitted as well.  This flag
1457      is used when parsing an array bound so that GNU variable-length
1458      arrays are tolerated.  */
1459   bool allow_non_integral_constant_expression_p;
1460
1461   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1462      been seen that makes the expression non-constant.  */
1463   bool non_integral_constant_expression_p;
1464
1465   /* TRUE if local variable names and `this' are forbidden in the
1466      current context.  */
1467   bool local_variables_forbidden_p;
1468
1469   /* TRUE if the declaration we are parsing is part of a
1470      linkage-specification of the form `extern string-literal
1471      declaration'.  */
1472   bool in_unbraced_linkage_specification_p;
1473
1474   /* TRUE if we are presently parsing a declarator, after the
1475      direct-declarator.  */
1476   bool in_declarator_p;
1477
1478   /* TRUE if we are presently parsing a template-argument-list.  */
1479   bool in_template_argument_list_p;
1480
1481   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1482      to IN_OMP_BLOCK if parsing OpenMP structured block and
1483      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1484      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1485      iteration-statement, OpenMP block or loop within that switch.  */
1486 #define IN_SWITCH_STMT          1
1487 #define IN_ITERATION_STMT       2
1488 #define IN_OMP_BLOCK            4
1489 #define IN_OMP_FOR              8
1490 #define IN_IF_STMT             16
1491   unsigned char in_statement;
1492
1493   /* TRUE if we are presently parsing the body of a switch statement.
1494      Note that this doesn't quite overlap with in_statement above.
1495      The difference relates to giving the right sets of error messages:
1496      "case not in switch" vs "break statement used with OpenMP...".  */
1497   bool in_switch_statement_p;
1498
1499   /* TRUE if we are parsing a type-id in an expression context.  In
1500      such a situation, both "type (expr)" and "type (type)" are valid
1501      alternatives.  */
1502   bool in_type_id_in_expr_p;
1503
1504   /* TRUE if we are currently in a header file where declarations are
1505      implicitly extern "C".  */
1506   bool implicit_extern_c;
1507
1508   /* TRUE if strings in expressions should be translated to the execution
1509      character set.  */
1510   bool translate_strings_p;
1511
1512   /* TRUE if we are presently parsing the body of a function, but not
1513      a local class.  */
1514   bool in_function_body;
1515
1516   /* If non-NULL, then we are parsing a construct where new type
1517      definitions are not permitted.  The string stored here will be
1518      issued as an error message if a type is defined.  */
1519   const char *type_definition_forbidden_message;
1520
1521   /* A list of lists. The outer list is a stack, used for member
1522      functions of local classes. At each level there are two sub-list,
1523      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1524      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1525      TREE_VALUE's. The functions are chained in reverse declaration
1526      order.
1527
1528      The TREE_PURPOSE sublist contains those functions with default
1529      arguments that need post processing, and the TREE_VALUE sublist
1530      contains those functions with definitions that need post
1531      processing.
1532
1533      These lists can only be processed once the outermost class being
1534      defined is complete.  */
1535   tree unparsed_functions_queues;
1536
1537   /* The number of classes whose definitions are currently in
1538      progress.  */
1539   unsigned num_classes_being_defined;
1540
1541   /* The number of template parameter lists that apply directly to the
1542      current declaration.  */
1543   unsigned num_template_parameter_lists;
1544 } cp_parser;
1545
1546 /* Prototypes.  */
1547
1548 /* Constructors and destructors.  */
1549
1550 static cp_parser *cp_parser_new
1551   (void);
1552
1553 /* Routines to parse various constructs.
1554
1555    Those that return `tree' will return the error_mark_node (rather
1556    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1557    Sometimes, they will return an ordinary node if error-recovery was
1558    attempted, even though a parse error occurred.  So, to check
1559    whether or not a parse error occurred, you should always use
1560    cp_parser_error_occurred.  If the construct is optional (indicated
1561    either by an `_opt' in the name of the function that does the
1562    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1563    the construct is not present.  */
1564
1565 /* Lexical conventions [gram.lex]  */
1566
1567 static tree cp_parser_identifier
1568   (cp_parser *);
1569 static tree cp_parser_string_literal
1570   (cp_parser *, bool, bool);
1571
1572 /* Basic concepts [gram.basic]  */
1573
1574 static bool cp_parser_translation_unit
1575   (cp_parser *);
1576
1577 /* Expressions [gram.expr]  */
1578
1579 static tree cp_parser_primary_expression
1580   (cp_parser *, bool, bool, bool, cp_id_kind *);
1581 static tree cp_parser_id_expression
1582   (cp_parser *, bool, bool, bool *, bool, bool);
1583 static tree cp_parser_unqualified_id
1584   (cp_parser *, bool, bool, bool, bool);
1585 static tree cp_parser_nested_name_specifier_opt
1586   (cp_parser *, bool, bool, bool, bool);
1587 static tree cp_parser_nested_name_specifier
1588   (cp_parser *, bool, bool, bool, bool);
1589 static tree cp_parser_qualifying_entity
1590   (cp_parser *, bool, bool, bool, bool, bool);
1591 static tree cp_parser_postfix_expression
1592   (cp_parser *, bool, bool, bool, cp_id_kind *);
1593 static tree cp_parser_postfix_open_square_expression
1594   (cp_parser *, tree, bool);
1595 static tree cp_parser_postfix_dot_deref_expression
1596   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1597 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1598   (cp_parser *, bool, bool, bool, bool *);
1599 static void cp_parser_pseudo_destructor_name
1600   (cp_parser *, tree *, tree *);
1601 static tree cp_parser_unary_expression
1602   (cp_parser *, bool, bool, cp_id_kind *);
1603 static enum tree_code cp_parser_unary_operator
1604   (cp_token *);
1605 static tree cp_parser_new_expression
1606   (cp_parser *);
1607 static VEC(tree,gc) *cp_parser_new_placement
1608   (cp_parser *);
1609 static tree cp_parser_new_type_id
1610   (cp_parser *, tree *);
1611 static cp_declarator *cp_parser_new_declarator_opt
1612   (cp_parser *);
1613 static cp_declarator *cp_parser_direct_new_declarator
1614   (cp_parser *);
1615 static VEC(tree,gc) *cp_parser_new_initializer
1616   (cp_parser *);
1617 static tree cp_parser_delete_expression
1618   (cp_parser *);
1619 static tree cp_parser_cast_expression
1620   (cp_parser *, bool, bool, cp_id_kind *);
1621 static tree cp_parser_binary_expression
1622   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1623 static tree cp_parser_question_colon_clause
1624   (cp_parser *, tree);
1625 static tree cp_parser_assignment_expression
1626   (cp_parser *, bool, cp_id_kind *);
1627 static enum tree_code cp_parser_assignment_operator_opt
1628   (cp_parser *);
1629 static tree cp_parser_expression
1630   (cp_parser *, bool, cp_id_kind *);
1631 static tree cp_parser_constant_expression
1632   (cp_parser *, bool, bool *);
1633 static tree cp_parser_builtin_offsetof
1634   (cp_parser *);
1635 static tree cp_parser_lambda_expression
1636   (cp_parser *);
1637 static void cp_parser_lambda_introducer
1638   (cp_parser *, tree);
1639 static void cp_parser_lambda_declarator_opt
1640   (cp_parser *, tree);
1641 static void cp_parser_lambda_body
1642   (cp_parser *, tree);
1643
1644 /* Statements [gram.stmt.stmt]  */
1645
1646 static void cp_parser_statement
1647   (cp_parser *, tree, bool, bool *);
1648 static void cp_parser_label_for_labeled_statement
1649   (cp_parser *);
1650 static tree cp_parser_expression_statement
1651   (cp_parser *, tree);
1652 static tree cp_parser_compound_statement
1653   (cp_parser *, tree, bool);
1654 static void cp_parser_statement_seq_opt
1655   (cp_parser *, tree);
1656 static tree cp_parser_selection_statement
1657   (cp_parser *, bool *);
1658 static tree cp_parser_condition
1659   (cp_parser *);
1660 static tree cp_parser_iteration_statement
1661   (cp_parser *);
1662 static void cp_parser_for_init_statement
1663   (cp_parser *);
1664 static tree cp_parser_jump_statement
1665   (cp_parser *);
1666 static void cp_parser_declaration_statement
1667   (cp_parser *);
1668
1669 static tree cp_parser_implicitly_scoped_statement
1670   (cp_parser *, bool *);
1671 static void cp_parser_already_scoped_statement
1672   (cp_parser *);
1673
1674 /* Declarations [gram.dcl.dcl] */
1675
1676 static void cp_parser_declaration_seq_opt
1677   (cp_parser *);
1678 static void cp_parser_declaration
1679   (cp_parser *);
1680 static void cp_parser_block_declaration
1681   (cp_parser *, bool);
1682 static void cp_parser_simple_declaration
1683   (cp_parser *, bool);
1684 static void cp_parser_decl_specifier_seq
1685   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1686 static tree cp_parser_storage_class_specifier_opt
1687   (cp_parser *);
1688 static tree cp_parser_function_specifier_opt
1689   (cp_parser *, cp_decl_specifier_seq *);
1690 static tree cp_parser_type_specifier
1691   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1692    int *, bool *);
1693 static tree cp_parser_simple_type_specifier
1694   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1695 static tree cp_parser_type_name
1696   (cp_parser *);
1697 static tree cp_parser_nonclass_name 
1698   (cp_parser* parser);
1699 static tree cp_parser_elaborated_type_specifier
1700   (cp_parser *, bool, bool);
1701 static tree cp_parser_enum_specifier
1702   (cp_parser *);
1703 static void cp_parser_enumerator_list
1704   (cp_parser *, tree);
1705 static void cp_parser_enumerator_definition
1706   (cp_parser *, tree);
1707 static tree cp_parser_namespace_name
1708   (cp_parser *);
1709 static void cp_parser_namespace_definition
1710   (cp_parser *);
1711 static void cp_parser_namespace_body
1712   (cp_parser *);
1713 static tree cp_parser_qualified_namespace_specifier
1714   (cp_parser *);
1715 static void cp_parser_namespace_alias_definition
1716   (cp_parser *);
1717 static bool cp_parser_using_declaration
1718   (cp_parser *, bool);
1719 static void cp_parser_using_directive
1720   (cp_parser *);
1721 static void cp_parser_asm_definition
1722   (cp_parser *);
1723 static void cp_parser_linkage_specification
1724   (cp_parser *);
1725 static void cp_parser_static_assert
1726   (cp_parser *, bool);
1727 static tree cp_parser_decltype
1728   (cp_parser *);
1729
1730 /* Declarators [gram.dcl.decl] */
1731
1732 static tree cp_parser_init_declarator
1733   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1734 static cp_declarator *cp_parser_declarator
1735   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1736 static cp_declarator *cp_parser_direct_declarator
1737   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1738 static enum tree_code cp_parser_ptr_operator
1739   (cp_parser *, tree *, cp_cv_quals *);
1740 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1741   (cp_parser *);
1742 static tree cp_parser_late_return_type_opt
1743   (cp_parser *);
1744 static tree cp_parser_declarator_id
1745   (cp_parser *, bool);
1746 static tree cp_parser_type_id
1747   (cp_parser *);
1748 static tree cp_parser_template_type_arg
1749   (cp_parser *);
1750 static tree cp_parser_trailing_type_id (cp_parser *);
1751 static tree cp_parser_type_id_1
1752   (cp_parser *, bool, bool);
1753 static void cp_parser_type_specifier_seq
1754   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1755 static tree cp_parser_parameter_declaration_clause
1756   (cp_parser *);
1757 static tree cp_parser_parameter_declaration_list
1758   (cp_parser *, bool *);
1759 static cp_parameter_declarator *cp_parser_parameter_declaration
1760   (cp_parser *, bool, bool *);
1761 static tree cp_parser_default_argument 
1762   (cp_parser *, bool);
1763 static void cp_parser_function_body
1764   (cp_parser *);
1765 static tree cp_parser_initializer
1766   (cp_parser *, bool *, bool *);
1767 static tree cp_parser_initializer_clause
1768   (cp_parser *, bool *);
1769 static tree cp_parser_braced_list
1770   (cp_parser*, bool*);
1771 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1772   (cp_parser *, bool *);
1773
1774 static bool cp_parser_ctor_initializer_opt_and_function_body
1775   (cp_parser *);
1776
1777 /* Classes [gram.class] */
1778
1779 static tree cp_parser_class_name
1780   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1781 static tree cp_parser_class_specifier
1782   (cp_parser *);
1783 static tree cp_parser_class_head
1784   (cp_parser *, bool *, tree *, tree *);
1785 static enum tag_types cp_parser_class_key
1786   (cp_parser *);
1787 static void cp_parser_member_specification_opt
1788   (cp_parser *);
1789 static void cp_parser_member_declaration
1790   (cp_parser *);
1791 static tree cp_parser_pure_specifier
1792   (cp_parser *);
1793 static tree cp_parser_constant_initializer
1794   (cp_parser *);
1795
1796 /* Derived classes [gram.class.derived] */
1797
1798 static tree cp_parser_base_clause
1799   (cp_parser *);
1800 static tree cp_parser_base_specifier
1801   (cp_parser *);
1802
1803 /* Special member functions [gram.special] */
1804
1805 static tree cp_parser_conversion_function_id
1806   (cp_parser *);
1807 static tree cp_parser_conversion_type_id
1808   (cp_parser *);
1809 static cp_declarator *cp_parser_conversion_declarator_opt
1810   (cp_parser *);
1811 static bool cp_parser_ctor_initializer_opt
1812   (cp_parser *);
1813 static void cp_parser_mem_initializer_list
1814   (cp_parser *);
1815 static tree cp_parser_mem_initializer
1816   (cp_parser *);
1817 static tree cp_parser_mem_initializer_id
1818   (cp_parser *);
1819
1820 /* Overloading [gram.over] */
1821
1822 static tree cp_parser_operator_function_id
1823   (cp_parser *);
1824 static tree cp_parser_operator
1825   (cp_parser *);
1826
1827 /* Templates [gram.temp] */
1828
1829 static void cp_parser_template_declaration
1830   (cp_parser *, bool);
1831 static tree cp_parser_template_parameter_list
1832   (cp_parser *);
1833 static tree cp_parser_template_parameter
1834   (cp_parser *, bool *, bool *);
1835 static tree cp_parser_type_parameter
1836   (cp_parser *, bool *);
1837 static tree cp_parser_template_id
1838   (cp_parser *, bool, bool, bool);
1839 static tree cp_parser_template_name
1840   (cp_parser *, bool, bool, bool, bool *);
1841 static tree cp_parser_template_argument_list
1842   (cp_parser *);
1843 static tree cp_parser_template_argument
1844   (cp_parser *);
1845 static void cp_parser_explicit_instantiation
1846   (cp_parser *);
1847 static void cp_parser_explicit_specialization
1848   (cp_parser *);
1849
1850 /* Exception handling [gram.exception] */
1851
1852 static tree cp_parser_try_block
1853   (cp_parser *);
1854 static bool cp_parser_function_try_block
1855   (cp_parser *);
1856 static void cp_parser_handler_seq
1857   (cp_parser *);
1858 static void cp_parser_handler
1859   (cp_parser *);
1860 static tree cp_parser_exception_declaration
1861   (cp_parser *);
1862 static tree cp_parser_throw_expression
1863   (cp_parser *);
1864 static tree cp_parser_exception_specification_opt
1865   (cp_parser *);
1866 static tree cp_parser_type_id_list
1867   (cp_parser *);
1868
1869 /* GNU Extensions */
1870
1871 static tree cp_parser_asm_specification_opt
1872   (cp_parser *);
1873 static tree cp_parser_asm_operand_list
1874   (cp_parser *);
1875 static tree cp_parser_asm_clobber_list
1876   (cp_parser *);
1877 static tree cp_parser_asm_label_list
1878   (cp_parser *);
1879 static tree cp_parser_attributes_opt
1880   (cp_parser *);
1881 static tree cp_parser_attribute_list
1882   (cp_parser *);
1883 static bool cp_parser_extension_opt
1884   (cp_parser *, int *);
1885 static void cp_parser_label_declaration
1886   (cp_parser *);
1887
1888 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1889 static bool cp_parser_pragma
1890   (cp_parser *, enum pragma_context);
1891
1892 /* Objective-C++ Productions */
1893
1894 static tree cp_parser_objc_message_receiver
1895   (cp_parser *);
1896 static tree cp_parser_objc_message_args
1897   (cp_parser *);
1898 static tree cp_parser_objc_message_expression
1899   (cp_parser *);
1900 static tree cp_parser_objc_encode_expression
1901   (cp_parser *);
1902 static tree cp_parser_objc_defs_expression
1903   (cp_parser *);
1904 static tree cp_parser_objc_protocol_expression
1905   (cp_parser *);
1906 static tree cp_parser_objc_selector_expression
1907   (cp_parser *);
1908 static tree cp_parser_objc_expression
1909   (cp_parser *);
1910 static bool cp_parser_objc_selector_p
1911   (enum cpp_ttype);
1912 static tree cp_parser_objc_selector
1913   (cp_parser *);
1914 static tree cp_parser_objc_protocol_refs_opt
1915   (cp_parser *);
1916 static void cp_parser_objc_declaration
1917   (cp_parser *);
1918 static tree cp_parser_objc_statement
1919   (cp_parser *);
1920
1921 /* Utility Routines */
1922
1923 static tree cp_parser_lookup_name
1924   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1925 static tree cp_parser_lookup_name_simple
1926   (cp_parser *, tree, location_t);
1927 static tree cp_parser_maybe_treat_template_as_class
1928   (tree, bool);
1929 static bool cp_parser_check_declarator_template_parameters
1930   (cp_parser *, cp_declarator *, location_t);
1931 static bool cp_parser_check_template_parameters
1932   (cp_parser *, unsigned, location_t, cp_declarator *);
1933 static tree cp_parser_simple_cast_expression
1934   (cp_parser *);
1935 static tree cp_parser_global_scope_opt
1936   (cp_parser *, bool);
1937 static bool cp_parser_constructor_declarator_p
1938   (cp_parser *, bool);
1939 static tree cp_parser_function_definition_from_specifiers_and_declarator
1940   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1941 static tree cp_parser_function_definition_after_declarator
1942   (cp_parser *, bool);
1943 static void cp_parser_template_declaration_after_export
1944   (cp_parser *, bool);
1945 static void cp_parser_perform_template_parameter_access_checks
1946   (VEC (deferred_access_check,gc)*);
1947 static tree cp_parser_single_declaration
1948   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1949 static tree cp_parser_functional_cast
1950   (cp_parser *, tree);
1951 static tree cp_parser_save_member_function_body
1952   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1953 static tree cp_parser_enclosed_template_argument_list
1954   (cp_parser *);
1955 static void cp_parser_save_default_args
1956   (cp_parser *, tree);
1957 static void cp_parser_late_parsing_for_member
1958   (cp_parser *, tree);
1959 static void cp_parser_late_parsing_default_args
1960   (cp_parser *, tree);
1961 static tree cp_parser_sizeof_operand
1962   (cp_parser *, enum rid);
1963 static tree cp_parser_trait_expr
1964   (cp_parser *, enum rid);
1965 static bool cp_parser_declares_only_class_p
1966   (cp_parser *);
1967 static void cp_parser_set_storage_class
1968   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1969 static void cp_parser_set_decl_spec_type
1970   (cp_decl_specifier_seq *, tree, location_t, bool);
1971 static bool cp_parser_friend_p
1972   (const cp_decl_specifier_seq *);
1973 static cp_token *cp_parser_require
1974   (cp_parser *, enum cpp_ttype, const char *);
1975 static cp_token *cp_parser_require_keyword
1976   (cp_parser *, enum rid, const char *);
1977 static bool cp_parser_token_starts_function_definition_p
1978   (cp_token *);
1979 static bool cp_parser_next_token_starts_class_definition_p
1980   (cp_parser *);
1981 static bool cp_parser_next_token_ends_template_argument_p
1982   (cp_parser *);
1983 static bool cp_parser_nth_token_starts_template_argument_list_p
1984   (cp_parser *, size_t);
1985 static enum tag_types cp_parser_token_is_class_key
1986   (cp_token *);
1987 static void cp_parser_check_class_key
1988   (enum tag_types, tree type);
1989 static void cp_parser_check_access_in_redeclaration
1990   (tree type, location_t location);
1991 static bool cp_parser_optional_template_keyword
1992   (cp_parser *);
1993 static void cp_parser_pre_parsed_nested_name_specifier
1994   (cp_parser *);
1995 static bool cp_parser_cache_group
1996   (cp_parser *, enum cpp_ttype, unsigned);
1997 static void cp_parser_parse_tentatively
1998   (cp_parser *);
1999 static void cp_parser_commit_to_tentative_parse
2000   (cp_parser *);
2001 static void cp_parser_abort_tentative_parse
2002   (cp_parser *);
2003 static bool cp_parser_parse_definitely
2004   (cp_parser *);
2005 static inline bool cp_parser_parsing_tentatively
2006   (cp_parser *);
2007 static bool cp_parser_uncommitted_to_tentative_parse_p
2008   (cp_parser *);
2009 static void cp_parser_error
2010   (cp_parser *, const char *);
2011 static void cp_parser_name_lookup_error
2012   (cp_parser *, tree, tree, const char *, location_t);
2013 static bool cp_parser_simulate_error
2014   (cp_parser *);
2015 static bool cp_parser_check_type_definition
2016   (cp_parser *);
2017 static void cp_parser_check_for_definition_in_return_type
2018   (cp_declarator *, tree, location_t type_location);
2019 static void cp_parser_check_for_invalid_template_id
2020   (cp_parser *, tree, location_t location);
2021 static bool cp_parser_non_integral_constant_expression
2022   (cp_parser *, const char *);
2023 static void cp_parser_diagnose_invalid_type_name
2024   (cp_parser *, tree, tree, location_t);
2025 static bool cp_parser_parse_and_diagnose_invalid_type_name
2026   (cp_parser *);
2027 static int cp_parser_skip_to_closing_parenthesis
2028   (cp_parser *, bool, bool, bool);
2029 static void cp_parser_skip_to_end_of_statement
2030   (cp_parser *);
2031 static void cp_parser_consume_semicolon_at_end_of_statement
2032   (cp_parser *);
2033 static void cp_parser_skip_to_end_of_block_or_statement
2034   (cp_parser *);
2035 static bool cp_parser_skip_to_closing_brace
2036   (cp_parser *);
2037 static void cp_parser_skip_to_end_of_template_parameter_list
2038   (cp_parser *);
2039 static void cp_parser_skip_to_pragma_eol
2040   (cp_parser*, cp_token *);
2041 static bool cp_parser_error_occurred
2042   (cp_parser *);
2043 static bool cp_parser_allow_gnu_extensions_p
2044   (cp_parser *);
2045 static bool cp_parser_is_string_literal
2046   (cp_token *);
2047 static bool cp_parser_is_keyword
2048   (cp_token *, enum rid);
2049 static tree cp_parser_make_typename_type
2050   (cp_parser *, tree, tree, location_t location);
2051 static cp_declarator * cp_parser_make_indirect_declarator
2052   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2053
2054 /* Returns nonzero if we are parsing tentatively.  */
2055
2056 static inline bool
2057 cp_parser_parsing_tentatively (cp_parser* parser)
2058 {
2059   return parser->context->next != NULL;
2060 }
2061
2062 /* Returns nonzero if TOKEN is a string literal.  */
2063
2064 static bool
2065 cp_parser_is_string_literal (cp_token* token)
2066 {
2067   return (token->type == CPP_STRING ||
2068           token->type == CPP_STRING16 ||
2069           token->type == CPP_STRING32 ||
2070           token->type == CPP_WSTRING ||
2071           token->type == CPP_UTF8STRING);
2072 }
2073
2074 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2075
2076 static bool
2077 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2078 {
2079   return token->keyword == keyword;
2080 }
2081
2082 /* If not parsing tentatively, issue a diagnostic of the form
2083       FILE:LINE: MESSAGE before TOKEN
2084    where TOKEN is the next token in the input stream.  MESSAGE
2085    (specified by the caller) is usually of the form "expected
2086    OTHER-TOKEN".  */
2087
2088 static void
2089 cp_parser_error (cp_parser* parser, const char* message)
2090 {
2091   if (!cp_parser_simulate_error (parser))
2092     {
2093       cp_token *token = cp_lexer_peek_token (parser->lexer);
2094       /* This diagnostic makes more sense if it is tagged to the line
2095          of the token we just peeked at.  */
2096       cp_lexer_set_source_position_from_token (token);
2097
2098       if (token->type == CPP_PRAGMA)
2099         {
2100           error_at (token->location,
2101                     "%<#pragma%> is not allowed here");
2102           cp_parser_skip_to_pragma_eol (parser, token);
2103           return;
2104         }
2105
2106       c_parse_error (message,
2107                      /* Because c_parser_error does not understand
2108                         CPP_KEYWORD, keywords are treated like
2109                         identifiers.  */
2110                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2111                      token->u.value, token->flags);
2112     }
2113 }
2114
2115 /* Issue an error about name-lookup failing.  NAME is the
2116    IDENTIFIER_NODE DECL is the result of
2117    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2118    the thing that we hoped to find.  */
2119
2120 static void
2121 cp_parser_name_lookup_error (cp_parser* parser,
2122                              tree name,
2123                              tree decl,
2124                              const char* desired,
2125                              location_t location)
2126 {
2127   /* If name lookup completely failed, tell the user that NAME was not
2128      declared.  */
2129   if (decl == error_mark_node)
2130     {
2131       if (parser->scope && parser->scope != global_namespace)
2132         error_at (location, "%<%E::%E%> has not been declared",
2133                   parser->scope, name);
2134       else if (parser->scope == global_namespace)
2135         error_at (location, "%<::%E%> has not been declared", name);
2136       else if (parser->object_scope
2137                && !CLASS_TYPE_P (parser->object_scope))
2138         error_at (location, "request for member %qE in non-class type %qT",
2139                   name, parser->object_scope);
2140       else if (parser->object_scope)
2141         error_at (location, "%<%T::%E%> has not been declared",
2142                   parser->object_scope, name);
2143       else
2144         error_at (location, "%qE has not been declared", name);
2145     }
2146   else if (parser->scope && parser->scope != global_namespace)
2147     error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2148   else if (parser->scope == global_namespace)
2149     error_at (location, "%<::%E%> %s", name, desired);
2150   else
2151     error_at (location, "%qE %s", name, desired);
2152 }
2153
2154 /* If we are parsing tentatively, remember that an error has occurred
2155    during this tentative parse.  Returns true if the error was
2156    simulated; false if a message should be issued by the caller.  */
2157
2158 static bool
2159 cp_parser_simulate_error (cp_parser* parser)
2160 {
2161   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2162     {
2163       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2164       return true;
2165     }
2166   return false;
2167 }
2168
2169 /* Check for repeated decl-specifiers.  */
2170
2171 static void
2172 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2173                            location_t location)
2174 {
2175   int ds;
2176
2177   for (ds = ds_first; ds != ds_last; ++ds)
2178     {
2179       unsigned count = decl_specs->specs[ds];
2180       if (count < 2)
2181         continue;
2182       /* The "long" specifier is a special case because of "long long".  */
2183       if (ds == ds_long)
2184         {
2185           if (count > 2)
2186             error_at (location, "%<long long long%> is too long for GCC");
2187           else 
2188             pedwarn_cxx98 (location, OPT_Wlong_long, 
2189                            "ISO C++ 1998 does not support %<long long%>");
2190         }
2191       else if (count > 1)
2192         {
2193           static const char *const decl_spec_names[] = {
2194             "signed",
2195             "unsigned",
2196             "short",
2197             "long",
2198             "const",
2199             "volatile",
2200             "restrict",
2201             "inline",
2202             "virtual",
2203             "explicit",
2204             "friend",
2205             "typedef",
2206             "constexpr",
2207             "__complex",
2208             "__thread"
2209           };
2210           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2211         }
2212     }
2213 }
2214
2215 /* This function is called when a type is defined.  If type
2216    definitions are forbidden at this point, an error message is
2217    issued.  */
2218
2219 static bool
2220 cp_parser_check_type_definition (cp_parser* parser)
2221 {
2222   /* If types are forbidden here, issue a message.  */
2223   if (parser->type_definition_forbidden_message)
2224     {
2225       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2226          in the message need to be interpreted.  */
2227       error (parser->type_definition_forbidden_message);
2228       return false;
2229     }
2230   return true;
2231 }
2232
2233 /* This function is called when the DECLARATOR is processed.  The TYPE
2234    was a type defined in the decl-specifiers.  If it is invalid to
2235    define a type in the decl-specifiers for DECLARATOR, an error is
2236    issued. TYPE_LOCATION is the location of TYPE and is used
2237    for error reporting.  */
2238
2239 static void
2240 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2241                                                tree type, location_t type_location)
2242 {
2243   /* [dcl.fct] forbids type definitions in return types.
2244      Unfortunately, it's not easy to know whether or not we are
2245      processing a return type until after the fact.  */
2246   while (declarator
2247          && (declarator->kind == cdk_pointer
2248              || declarator->kind == cdk_reference
2249              || declarator->kind == cdk_ptrmem))
2250     declarator = declarator->declarator;
2251   if (declarator
2252       && declarator->kind == cdk_function)
2253     {
2254       error_at (type_location,
2255                 "new types may not be defined in a return type");
2256       inform (type_location, 
2257               "(perhaps a semicolon is missing after the definition of %qT)",
2258               type);
2259     }
2260 }
2261
2262 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2263    "<" in any valid C++ program.  If the next token is indeed "<",
2264    issue a message warning the user about what appears to be an
2265    invalid attempt to form a template-id. LOCATION is the location
2266    of the type-specifier (TYPE) */
2267
2268 static void
2269 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2270                                          tree type, location_t location)
2271 {
2272   cp_token_position start = 0;
2273
2274   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2275     {
2276       if (TYPE_P (type))
2277         error_at (location, "%qT is not a template", type);
2278       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2279         error_at (location, "%qE is not a template", type);
2280       else
2281         error_at (location, "invalid template-id");
2282       /* Remember the location of the invalid "<".  */
2283       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2284         start = cp_lexer_token_position (parser->lexer, true);
2285       /* Consume the "<".  */
2286       cp_lexer_consume_token (parser->lexer);
2287       /* Parse the template arguments.  */
2288       cp_parser_enclosed_template_argument_list (parser);
2289       /* Permanently remove the invalid template arguments so that
2290          this error message is not issued again.  */
2291       if (start)
2292         cp_lexer_purge_tokens_after (parser->lexer, start);
2293     }
2294 }
2295
2296 /* If parsing an integral constant-expression, issue an error message
2297    about the fact that THING appeared and return true.  Otherwise,
2298    return false.  In either case, set
2299    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2300
2301 static bool
2302 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2303                                             const char *thing)
2304 {
2305   parser->non_integral_constant_expression_p = true;
2306   if (parser->integral_constant_expression_p)
2307     {
2308       if (!parser->allow_non_integral_constant_expression_p)
2309         {
2310           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2311              in the message need to be interpreted.  */
2312           char *message = concat (thing,
2313                                   " cannot appear in a constant-expression",
2314                                   NULL);
2315           error (message);
2316           free (message);
2317           return true;
2318         }
2319     }
2320   return false;
2321 }
2322
2323 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2324    qualifying scope (or NULL, if none) for ID.  This function commits
2325    to the current active tentative parse, if any.  (Otherwise, the
2326    problematic construct might be encountered again later, resulting
2327    in duplicate error messages.) LOCATION is the location of ID.  */
2328
2329 static void
2330 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2331                                       tree scope, tree id,
2332                                       location_t location)
2333 {
2334   tree decl, old_scope;
2335   /* Try to lookup the identifier.  */
2336   old_scope = parser->scope;
2337   parser->scope = scope;
2338   decl = cp_parser_lookup_name_simple (parser, id, location);
2339   parser->scope = old_scope;
2340   /* If the lookup found a template-name, it means that the user forgot
2341   to specify an argument list. Emit a useful error message.  */
2342   if (TREE_CODE (decl) == TEMPLATE_DECL)
2343     error_at (location,
2344               "invalid use of template-name %qE without an argument list",
2345               decl);
2346   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2347     error_at (location, "invalid use of destructor %qD as a type", id);
2348   else if (TREE_CODE (decl) == TYPE_DECL)
2349     /* Something like 'unsigned A a;'  */
2350     error_at (location, "invalid combination of multiple type-specifiers");
2351   else if (!parser->scope)
2352     {
2353       /* Issue an error message.  */
2354       error_at (location, "%qE does not name a type", id);
2355       /* If we're in a template class, it's possible that the user was
2356          referring to a type from a base class.  For example:
2357
2358            template <typename T> struct A { typedef T X; };
2359            template <typename T> struct B : public A<T> { X x; };
2360
2361          The user should have said "typename A<T>::X".  */
2362       if (processing_template_decl && current_class_type
2363           && TYPE_BINFO (current_class_type))
2364         {
2365           tree b;
2366
2367           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2368                b;
2369                b = TREE_CHAIN (b))
2370             {
2371               tree base_type = BINFO_TYPE (b);
2372               if (CLASS_TYPE_P (base_type)
2373                   && dependent_type_p (base_type))
2374                 {
2375                   tree field;
2376                   /* Go from a particular instantiation of the
2377                      template (which will have an empty TYPE_FIELDs),
2378                      to the main version.  */
2379                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2380                   for (field = TYPE_FIELDS (base_type);
2381                        field;
2382                        field = TREE_CHAIN (field))
2383                     if (TREE_CODE (field) == TYPE_DECL
2384                         && DECL_NAME (field) == id)
2385                       {
2386                         inform (location, 
2387                                 "(perhaps %<typename %T::%E%> was intended)",
2388                                 BINFO_TYPE (b), id);
2389                         break;
2390                       }
2391                   if (field)
2392                     break;
2393                 }
2394             }
2395         }
2396     }
2397   /* Here we diagnose qualified-ids where the scope is actually correct,
2398      but the identifier does not resolve to a valid type name.  */
2399   else if (parser->scope != error_mark_node)
2400     {
2401       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2402         error_at (location, "%qE in namespace %qE does not name a type",
2403                   id, parser->scope);
2404       else if (CLASS_TYPE_P (parser->scope)
2405                && constructor_name_p (id, parser->scope))
2406         {
2407           /* A<T>::A<T>() */
2408           error_at (location, "%<%T::%E%> names the constructor, not"
2409                     " the type", parser->scope, id);
2410           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2411             error_at (location, "and %qT has no template constructors",
2412                       parser->scope);
2413         }
2414       else if (TYPE_P (parser->scope)
2415                && dependent_scope_p (parser->scope))
2416         error_at (location, "need %<typename%> before %<%T::%E%> because "
2417                   "%qT is a dependent scope",
2418                   parser->scope, id, parser->scope);
2419       else if (TYPE_P (parser->scope))
2420         error_at (location, "%qE in class %qT does not name a type",
2421                   id, parser->scope);
2422       else
2423         gcc_unreachable ();
2424     }
2425   cp_parser_commit_to_tentative_parse (parser);
2426 }
2427
2428 /* Check for a common situation where a type-name should be present,
2429    but is not, and issue a sensible error message.  Returns true if an
2430    invalid type-name was detected.
2431
2432    The situation handled by this function are variable declarations of the
2433    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2434    Usually, `ID' should name a type, but if we got here it means that it
2435    does not. We try to emit the best possible error message depending on
2436    how exactly the id-expression looks like.  */
2437
2438 static bool
2439 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2440 {
2441   tree id;
2442   cp_token *token = cp_lexer_peek_token (parser->lexer);
2443
2444   /* Avoid duplicate error about ambiguous lookup.  */
2445   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2446     {
2447       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2448       if (next->type == CPP_NAME && next->ambiguous_p)
2449         goto out;
2450     }
2451
2452   cp_parser_parse_tentatively (parser);
2453   id = cp_parser_id_expression (parser,
2454                                 /*template_keyword_p=*/false,
2455                                 /*check_dependency_p=*/true,
2456                                 /*template_p=*/NULL,
2457                                 /*declarator_p=*/true,
2458                                 /*optional_p=*/false);
2459   /* If the next token is a (, this is a function with no explicit return
2460      type, i.e. constructor, destructor or conversion op.  */
2461   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2462       || TREE_CODE (id) == TYPE_DECL)
2463     {
2464       cp_parser_abort_tentative_parse (parser);
2465       return false;
2466     }
2467   if (!cp_parser_parse_definitely (parser))
2468     return false;
2469
2470   /* Emit a diagnostic for the invalid type.  */
2471   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2472                                         id, token->location);
2473  out:
2474   /* If we aren't in the middle of a declarator (i.e. in a
2475      parameter-declaration-clause), skip to the end of the declaration;
2476      there's no point in trying to process it.  */
2477   if (!parser->in_declarator_p)
2478     cp_parser_skip_to_end_of_block_or_statement (parser);
2479   return true;
2480 }
2481
2482 /* Consume tokens up to, and including, the next non-nested closing `)'.
2483    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2484    are doing error recovery. Returns -1 if OR_COMMA is true and we
2485    found an unnested comma.  */
2486
2487 static int
2488 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2489                                        bool recovering,
2490                                        bool or_comma,
2491                                        bool consume_paren)
2492 {
2493   unsigned paren_depth = 0;
2494   unsigned brace_depth = 0;
2495   unsigned square_depth = 0;
2496
2497   if (recovering && !or_comma
2498       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2499     return 0;
2500
2501   while (true)
2502     {
2503       cp_token * token = cp_lexer_peek_token (parser->lexer);
2504
2505       switch (token->type)
2506         {
2507         case CPP_EOF:
2508         case CPP_PRAGMA_EOL:
2509           /* If we've run out of tokens, then there is no closing `)'.  */
2510           return 0;
2511
2512         /* This is good for lambda expression capture-lists.  */
2513         case CPP_OPEN_SQUARE:
2514           ++square_depth;
2515           break;
2516         case CPP_CLOSE_SQUARE:
2517           if (!square_depth--)
2518             return 0;
2519           break;
2520
2521         case CPP_SEMICOLON:
2522           /* This matches the processing in skip_to_end_of_statement.  */
2523           if (!brace_depth)
2524             return 0;
2525           break;
2526
2527         case CPP_OPEN_BRACE:
2528           ++brace_depth;
2529           break;
2530         case CPP_CLOSE_BRACE:
2531           if (!brace_depth--)
2532             return 0;
2533           break;
2534
2535         case CPP_COMMA:
2536           if (recovering && or_comma && !brace_depth && !paren_depth
2537               && !square_depth)
2538             return -1;
2539           break;
2540
2541         case CPP_OPEN_PAREN:
2542           if (!brace_depth)
2543             ++paren_depth;
2544           break;
2545
2546         case CPP_CLOSE_PAREN:
2547           if (!brace_depth && !paren_depth--)
2548             {
2549               if (consume_paren)
2550                 cp_lexer_consume_token (parser->lexer);
2551               return 1;
2552             }
2553           break;
2554
2555         default:
2556           break;
2557         }
2558
2559       /* Consume the token.  */
2560       cp_lexer_consume_token (parser->lexer);
2561     }
2562 }
2563
2564 /* Consume tokens until we reach the end of the current statement.
2565    Normally, that will be just before consuming a `;'.  However, if a
2566    non-nested `}' comes first, then we stop before consuming that.  */
2567
2568 static void
2569 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2570 {
2571   unsigned nesting_depth = 0;
2572
2573   while (true)
2574     {
2575       cp_token *token = cp_lexer_peek_token (parser->lexer);
2576
2577       switch (token->type)
2578         {
2579         case CPP_EOF:
2580         case CPP_PRAGMA_EOL:
2581           /* If we've run out of tokens, stop.  */
2582           return;
2583
2584         case CPP_SEMICOLON:
2585           /* If the next token is a `;', we have reached the end of the
2586              statement.  */
2587           if (!nesting_depth)
2588             return;
2589           break;
2590
2591         case CPP_CLOSE_BRACE:
2592           /* If this is a non-nested '}', stop before consuming it.
2593              That way, when confronted with something like:
2594
2595                { 3 + }
2596
2597              we stop before consuming the closing '}', even though we
2598              have not yet reached a `;'.  */
2599           if (nesting_depth == 0)
2600             return;
2601
2602           /* If it is the closing '}' for a block that we have
2603              scanned, stop -- but only after consuming the token.
2604              That way given:
2605
2606                 void f g () { ... }
2607                 typedef int I;
2608
2609              we will stop after the body of the erroneously declared
2610              function, but before consuming the following `typedef'
2611              declaration.  */
2612           if (--nesting_depth == 0)
2613             {
2614               cp_lexer_consume_token (parser->lexer);
2615               return;
2616             }
2617
2618         case CPP_OPEN_BRACE:
2619           ++nesting_depth;
2620           break;
2621
2622         default:
2623           break;
2624         }
2625
2626       /* Consume the token.  */
2627       cp_lexer_consume_token (parser->lexer);
2628     }
2629 }
2630
2631 /* This function is called at the end of a statement or declaration.
2632    If the next token is a semicolon, it is consumed; otherwise, error
2633    recovery is attempted.  */
2634
2635 static void
2636 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2637 {
2638   /* Look for the trailing `;'.  */
2639   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2640     {
2641       /* If there is additional (erroneous) input, skip to the end of
2642          the statement.  */
2643       cp_parser_skip_to_end_of_statement (parser);
2644       /* If the next token is now a `;', consume it.  */
2645       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2646         cp_lexer_consume_token (parser->lexer);
2647     }
2648 }
2649
2650 /* Skip tokens until we have consumed an entire block, or until we
2651    have consumed a non-nested `;'.  */
2652
2653 static void
2654 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2655 {
2656   int nesting_depth = 0;
2657
2658   while (nesting_depth >= 0)
2659     {
2660       cp_token *token = cp_lexer_peek_token (parser->lexer);
2661
2662       switch (token->type)
2663         {
2664         case CPP_EOF:
2665         case CPP_PRAGMA_EOL:
2666           /* If we've run out of tokens, stop.  */
2667           return;
2668
2669         case CPP_SEMICOLON:
2670           /* Stop if this is an unnested ';'. */
2671           if (!nesting_depth)
2672             nesting_depth = -1;
2673           break;
2674
2675         case CPP_CLOSE_BRACE:
2676           /* Stop if this is an unnested '}', or closes the outermost
2677              nesting level.  */
2678           nesting_depth--;
2679           if (nesting_depth < 0)
2680             return;
2681           if (!nesting_depth)
2682             nesting_depth = -1;
2683           break;
2684
2685         case CPP_OPEN_BRACE:
2686           /* Nest. */
2687           nesting_depth++;
2688           break;
2689
2690         default:
2691           break;
2692         }
2693
2694       /* Consume the token.  */
2695       cp_lexer_consume_token (parser->lexer);
2696     }
2697 }
2698
2699 /* Skip tokens until a non-nested closing curly brace is the next
2700    token, or there are no more tokens. Return true in the first case,
2701    false otherwise.  */
2702
2703 static bool
2704 cp_parser_skip_to_closing_brace (cp_parser *parser)
2705 {
2706   unsigned nesting_depth = 0;
2707
2708   while (true)
2709     {
2710       cp_token *token = cp_lexer_peek_token (parser->lexer);
2711
2712       switch (token->type)
2713         {
2714         case CPP_EOF:
2715         case CPP_PRAGMA_EOL:
2716           /* If we've run out of tokens, stop.  */
2717           return false;
2718
2719         case CPP_CLOSE_BRACE:
2720           /* If the next token is a non-nested `}', then we have reached
2721              the end of the current block.  */
2722           if (nesting_depth-- == 0)
2723             return true;
2724           break;
2725
2726         case CPP_OPEN_BRACE:
2727           /* If it the next token is a `{', then we are entering a new
2728              block.  Consume the entire block.  */
2729           ++nesting_depth;
2730           break;
2731
2732         default:
2733           break;
2734         }
2735
2736       /* Consume the token.  */
2737       cp_lexer_consume_token (parser->lexer);
2738     }
2739 }
2740
2741 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2742    parameter is the PRAGMA token, allowing us to purge the entire pragma
2743    sequence.  */
2744
2745 static void
2746 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2747 {
2748   cp_token *token;
2749
2750   parser->lexer->in_pragma = false;
2751
2752   do
2753     token = cp_lexer_consume_token (parser->lexer);
2754   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2755
2756   /* Ensure that the pragma is not parsed again.  */
2757   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2758 }
2759
2760 /* Require pragma end of line, resyncing with it as necessary.  The
2761    arguments are as for cp_parser_skip_to_pragma_eol.  */
2762
2763 static void
2764 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2765 {
2766   parser->lexer->in_pragma = false;
2767   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2768     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2769 }
2770
2771 /* This is a simple wrapper around make_typename_type. When the id is
2772    an unresolved identifier node, we can provide a superior diagnostic
2773    using cp_parser_diagnose_invalid_type_name.  */
2774
2775 static tree
2776 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2777                               tree id, location_t id_location)
2778 {
2779   tree result;
2780   if (TREE_CODE (id) == IDENTIFIER_NODE)
2781     {
2782       result = make_typename_type (scope, id, typename_type,
2783                                    /*complain=*/tf_none);
2784       if (result == error_mark_node)
2785         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2786       return result;
2787     }
2788   return make_typename_type (scope, id, typename_type, tf_error);
2789 }
2790
2791 /* This is a wrapper around the
2792    make_{pointer,ptrmem,reference}_declarator functions that decides
2793    which one to call based on the CODE and CLASS_TYPE arguments. The
2794    CODE argument should be one of the values returned by
2795    cp_parser_ptr_operator. */
2796 static cp_declarator *
2797 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2798                                     cp_cv_quals cv_qualifiers,
2799                                     cp_declarator *target)
2800 {
2801   if (code == ERROR_MARK)
2802     return cp_error_declarator;
2803
2804   if (code == INDIRECT_REF)
2805     if (class_type == NULL_TREE)
2806       return make_pointer_declarator (cv_qualifiers, target);
2807     else
2808       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2809   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2810     return make_reference_declarator (cv_qualifiers, target, false);
2811   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2812     return make_reference_declarator (cv_qualifiers, target, true);
2813   gcc_unreachable ();
2814 }
2815
2816 /* Create a new C++ parser.  */
2817
2818 static cp_parser *
2819 cp_parser_new (void)
2820 {
2821   cp_parser *parser;
2822   cp_lexer *lexer;
2823   unsigned i;
2824
2825   /* cp_lexer_new_main is called before calling ggc_alloc because
2826      cp_lexer_new_main might load a PCH file.  */
2827   lexer = cp_lexer_new_main ();
2828
2829   /* Initialize the binops_by_token so that we can get the tree
2830      directly from the token.  */
2831   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2832     binops_by_token[binops[i].token_type] = binops[i];
2833
2834   parser = GGC_CNEW (cp_parser);
2835   parser->lexer = lexer;
2836   parser->context = cp_parser_context_new (NULL);
2837
2838   /* For now, we always accept GNU extensions.  */
2839   parser->allow_gnu_extensions_p = 1;
2840
2841   /* The `>' token is a greater-than operator, not the end of a
2842      template-id.  */
2843   parser->greater_than_is_operator_p = true;
2844
2845   parser->default_arg_ok_p = true;
2846
2847   /* We are not parsing a constant-expression.  */
2848   parser->integral_constant_expression_p = false;
2849   parser->allow_non_integral_constant_expression_p = false;
2850   parser->non_integral_constant_expression_p = false;
2851
2852   /* Local variable names are not forbidden.  */
2853   parser->local_variables_forbidden_p = false;
2854
2855   /* We are not processing an `extern "C"' declaration.  */
2856   parser->in_unbraced_linkage_specification_p = false;
2857
2858   /* We are not processing a declarator.  */
2859   parser->in_declarator_p = false;
2860
2861   /* We are not processing a template-argument-list.  */
2862   parser->in_template_argument_list_p = false;
2863
2864   /* We are not in an iteration statement.  */
2865   parser->in_statement = 0;
2866
2867   /* We are not in a switch statement.  */
2868   parser->in_switch_statement_p = false;
2869
2870   /* We are not parsing a type-id inside an expression.  */
2871   parser->in_type_id_in_expr_p = false;
2872
2873   /* Declarations aren't implicitly extern "C".  */
2874   parser->implicit_extern_c = false;
2875
2876   /* String literals should be translated to the execution character set.  */
2877   parser->translate_strings_p = true;
2878
2879   /* We are not parsing a function body.  */
2880   parser->in_function_body = false;
2881
2882   /* The unparsed function queue is empty.  */
2883   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2884
2885   /* There are no classes being defined.  */
2886   parser->num_classes_being_defined = 0;
2887
2888   /* No template parameters apply.  */
2889   parser->num_template_parameter_lists = 0;
2890
2891   return parser;
2892 }
2893
2894 /* Create a cp_lexer structure which will emit the tokens in CACHE
2895    and push it onto the parser's lexer stack.  This is used for delayed
2896    parsing of in-class method bodies and default arguments, and should
2897    not be confused with tentative parsing.  */
2898 static void
2899 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2900 {
2901   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2902   lexer->next = parser->lexer;
2903   parser->lexer = lexer;
2904
2905   /* Move the current source position to that of the first token in the
2906      new lexer.  */
2907   cp_lexer_set_source_position_from_token (lexer->next_token);
2908 }
2909
2910 /* Pop the top lexer off the parser stack.  This is never used for the
2911    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2912 static void
2913 cp_parser_pop_lexer (cp_parser *parser)
2914 {
2915   cp_lexer *lexer = parser->lexer;
2916   parser->lexer = lexer->next;
2917   cp_lexer_destroy (lexer);
2918
2919   /* Put the current source position back where it was before this
2920      lexer was pushed.  */
2921   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2922 }
2923
2924 /* Lexical conventions [gram.lex]  */
2925
2926 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2927    identifier.  */
2928
2929 static tree
2930 cp_parser_identifier (cp_parser* parser)
2931 {
2932   cp_token *token;
2933
2934   /* Look for the identifier.  */
2935   token = cp_parser_require (parser, CPP_NAME, "identifier");
2936   /* Return the value.  */
2937   return token ? token->u.value : error_mark_node;
2938 }
2939
2940 /* Parse a sequence of adjacent string constants.  Returns a
2941    TREE_STRING representing the combined, nul-terminated string
2942    constant.  If TRANSLATE is true, translate the string to the
2943    execution character set.  If WIDE_OK is true, a wide string is
2944    invalid here.
2945
2946    C++98 [lex.string] says that if a narrow string literal token is
2947    adjacent to a wide string literal token, the behavior is undefined.
2948    However, C99 6.4.5p4 says that this results in a wide string literal.
2949    We follow C99 here, for consistency with the C front end.
2950
2951    This code is largely lifted from lex_string() in c-lex.c.
2952
2953    FUTURE: ObjC++ will need to handle @-strings here.  */
2954 static tree
2955 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2956 {
2957   tree value;
2958   size_t count;
2959   struct obstack str_ob;
2960   cpp_string str, istr, *strs;
2961   cp_token *tok;
2962   enum cpp_ttype type;
2963
2964   tok = cp_lexer_peek_token (parser->lexer);
2965   if (!cp_parser_is_string_literal (tok))
2966     {
2967       cp_parser_error (parser, "expected string-literal");
2968       return error_mark_node;
2969     }
2970
2971   type = tok->type;
2972
2973   /* Try to avoid the overhead of creating and destroying an obstack
2974      for the common case of just one string.  */
2975   if (!cp_parser_is_string_literal
2976       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2977     {
2978       cp_lexer_consume_token (parser->lexer);
2979
2980       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2981       str.len = TREE_STRING_LENGTH (tok->u.value);
2982       count = 1;
2983
2984       strs = &str;
2985     }
2986   else
2987     {
2988       gcc_obstack_init (&str_ob);
2989       count = 0;
2990
2991       do
2992         {
2993           cp_lexer_consume_token (parser->lexer);
2994           count++;
2995           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2996           str.len = TREE_STRING_LENGTH (tok->u.value);
2997
2998           if (type != tok->type)
2999             {
3000               if (type == CPP_STRING)
3001                 type = tok->type;
3002               else if (tok->type != CPP_STRING)
3003                 error_at (tok->location,
3004                           "unsupported non-standard concatenation "
3005                           "of string literals");
3006             }
3007
3008           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3009
3010           tok = cp_lexer_peek_token (parser->lexer);
3011         }
3012       while (cp_parser_is_string_literal (tok));
3013
3014       strs = (cpp_string *) obstack_finish (&str_ob);
3015     }
3016
3017   if (type != CPP_STRING && !wide_ok)
3018     {
3019       cp_parser_error (parser, "a wide string is invalid in this context");
3020       type = CPP_STRING;
3021     }
3022
3023   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3024       (parse_in, strs, count, &istr, type))
3025     {
3026       value = build_string (istr.len, (const char *)istr.text);
3027       free (CONST_CAST (unsigned char *, istr.text));
3028
3029       switch (type)
3030         {
3031         default:
3032         case CPP_STRING:
3033         case CPP_UTF8STRING:
3034           TREE_TYPE (value) = char_array_type_node;
3035           break;
3036         case CPP_STRING16:
3037           TREE_TYPE (value) = char16_array_type_node;
3038           break;
3039         case CPP_STRING32:
3040           TREE_TYPE (value) = char32_array_type_node;
3041           break;
3042         case CPP_WSTRING:
3043           TREE_TYPE (value) = wchar_array_type_node;
3044           break;
3045         }
3046
3047       value = fix_string_type (value);
3048     }
3049   else
3050     /* cpp_interpret_string has issued an error.  */
3051     value = error_mark_node;
3052
3053   if (count > 1)
3054     obstack_free (&str_ob, 0);
3055
3056   return value;
3057 }
3058
3059
3060 /* Basic concepts [gram.basic]  */
3061
3062 /* Parse a translation-unit.
3063
3064    translation-unit:
3065      declaration-seq [opt]
3066
3067    Returns TRUE if all went well.  */
3068
3069 static bool
3070 cp_parser_translation_unit (cp_parser* parser)
3071 {
3072   /* The address of the first non-permanent object on the declarator
3073      obstack.  */
3074   static void *declarator_obstack_base;
3075
3076   bool success;
3077
3078   /* Create the declarator obstack, if necessary.  */
3079   if (!cp_error_declarator)
3080     {
3081       gcc_obstack_init (&declarator_obstack);
3082       /* Create the error declarator.  */
3083       cp_error_declarator = make_declarator (cdk_error);
3084       /* Create the empty parameter list.  */
3085       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3086       /* Remember where the base of the declarator obstack lies.  */
3087       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3088     }
3089
3090   cp_parser_declaration_seq_opt (parser);
3091
3092   /* If there are no tokens left then all went well.  */
3093   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3094     {
3095       /* Get rid of the token array; we don't need it any more.  */
3096       cp_lexer_destroy (parser->lexer);
3097       parser->lexer = NULL;
3098
3099       /* This file might have been a context that's implicitly extern
3100          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3101       if (parser->implicit_extern_c)
3102         {
3103           pop_lang_context ();
3104           parser->implicit_extern_c = false;
3105         }
3106
3107       /* Finish up.  */
3108       finish_translation_unit ();
3109
3110       success = true;
3111     }
3112   else
3113     {
3114       cp_parser_error (parser, "expected declaration");
3115       success = false;
3116     }
3117
3118   /* Make sure the declarator obstack was fully cleaned up.  */
3119   gcc_assert (obstack_next_free (&declarator_obstack)
3120               == declarator_obstack_base);
3121
3122   /* All went well.  */
3123   return success;
3124 }
3125
3126 /* Expressions [gram.expr] */
3127
3128 /* Parse a primary-expression.
3129
3130    primary-expression:
3131      literal
3132      this
3133      ( expression )
3134      id-expression
3135
3136    GNU Extensions:
3137
3138    primary-expression:
3139      ( compound-statement )
3140      __builtin_va_arg ( assignment-expression , type-id )
3141      __builtin_offsetof ( type-id , offsetof-expression )
3142
3143    C++ Extensions:
3144      __has_nothrow_assign ( type-id )   
3145      __has_nothrow_constructor ( type-id )
3146      __has_nothrow_copy ( type-id )
3147      __has_trivial_assign ( type-id )   
3148      __has_trivial_constructor ( type-id )
3149      __has_trivial_copy ( type-id )
3150      __has_trivial_destructor ( type-id )
3151      __has_virtual_destructor ( type-id )     
3152      __is_abstract ( type-id )
3153      __is_base_of ( type-id , type-id )
3154      __is_class ( type-id )
3155      __is_convertible_to ( type-id , type-id )     
3156      __is_empty ( type-id )
3157      __is_enum ( type-id )
3158      __is_pod ( type-id )
3159      __is_polymorphic ( type-id )
3160      __is_union ( type-id )
3161
3162    Objective-C++ Extension:
3163
3164    primary-expression:
3165      objc-expression
3166
3167    literal:
3168      __null
3169
3170    ADDRESS_P is true iff this expression was immediately preceded by
3171    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3172    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3173    true iff this expression is a template argument.
3174
3175    Returns a representation of the expression.  Upon return, *IDK
3176    indicates what kind of id-expression (if any) was present.  */
3177
3178 static tree
3179 cp_parser_primary_expression (cp_parser *parser,
3180                               bool address_p,
3181                               bool cast_p,
3182                               bool template_arg_p,
3183                               cp_id_kind *idk)
3184 {
3185   cp_token *token = NULL;
3186
3187   /* Assume the primary expression is not an id-expression.  */
3188   *idk = CP_ID_KIND_NONE;
3189
3190   /* Peek at the next token.  */
3191   token = cp_lexer_peek_token (parser->lexer);
3192   switch (token->type)
3193     {
3194       /* literal:
3195            integer-literal
3196            character-literal
3197            floating-literal
3198            string-literal
3199            boolean-literal  */
3200     case CPP_CHAR:
3201     case CPP_CHAR16:
3202     case CPP_CHAR32:
3203     case CPP_WCHAR:
3204     case CPP_NUMBER:
3205       token = cp_lexer_consume_token (parser->lexer);
3206       if (TREE_CODE (token->u.value) == FIXED_CST)
3207         {
3208           error_at (token->location,
3209                     "fixed-point types not supported in C++");
3210           return error_mark_node;
3211         }
3212       /* Floating-point literals are only allowed in an integral
3213          constant expression if they are cast to an integral or
3214          enumeration type.  */
3215       if (TREE_CODE (token->u.value) == REAL_CST
3216           && parser->integral_constant_expression_p
3217           && pedantic)
3218         {
3219           /* CAST_P will be set even in invalid code like "int(2.7 +
3220              ...)".   Therefore, we have to check that the next token
3221              is sure to end the cast.  */
3222           if (cast_p)
3223             {
3224               cp_token *next_token;
3225
3226               next_token = cp_lexer_peek_token (parser->lexer);
3227               if (/* The comma at the end of an
3228                      enumerator-definition.  */
3229                   next_token->type != CPP_COMMA
3230                   /* The curly brace at the end of an enum-specifier.  */
3231                   && next_token->type != CPP_CLOSE_BRACE
3232                   /* The end of a statement.  */
3233                   && next_token->type != CPP_SEMICOLON
3234                   /* The end of the cast-expression.  */
3235                   && next_token->type != CPP_CLOSE_PAREN
3236                   /* The end of an array bound.  */
3237                   && next_token->type != CPP_CLOSE_SQUARE
3238                   /* The closing ">" in a template-argument-list.  */
3239                   && (next_token->type != CPP_GREATER
3240                       || parser->greater_than_is_operator_p)
3241                   /* C++0x only: A ">>" treated like two ">" tokens,
3242                      in a template-argument-list.  */
3243                   && (next_token->type != CPP_RSHIFT
3244                       || (cxx_dialect == cxx98)
3245                       || parser->greater_than_is_operator_p))
3246                 cast_p = false;
3247             }
3248
3249           /* If we are within a cast, then the constraint that the
3250              cast is to an integral or enumeration type will be
3251              checked at that point.  If we are not within a cast, then
3252              this code is invalid.  */
3253           if (!cast_p)
3254             cp_parser_non_integral_constant_expression
3255               (parser, "floating-point literal");
3256         }
3257       return token->u.value;
3258
3259     case CPP_STRING:
3260     case CPP_STRING16:
3261     case CPP_STRING32:
3262     case CPP_WSTRING:
3263     case CPP_UTF8STRING:
3264       /* ??? Should wide strings be allowed when parser->translate_strings_p
3265          is false (i.e. in attributes)?  If not, we can kill the third
3266          argument to cp_parser_string_literal.  */
3267       return cp_parser_string_literal (parser,
3268                                        parser->translate_strings_p,
3269                                        true);
3270
3271     case CPP_OPEN_PAREN:
3272       {
3273         tree expr;
3274         bool saved_greater_than_is_operator_p;
3275
3276         /* Consume the `('.  */
3277         cp_lexer_consume_token (parser->lexer);
3278         /* Within a parenthesized expression, a `>' token is always
3279            the greater-than operator.  */
3280         saved_greater_than_is_operator_p
3281           = parser->greater_than_is_operator_p;
3282         parser->greater_than_is_operator_p = true;
3283         /* If we see `( { ' then we are looking at the beginning of
3284            a GNU statement-expression.  */
3285         if (cp_parser_allow_gnu_extensions_p (parser)
3286             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3287           {
3288             /* Statement-expressions are not allowed by the standard.  */
3289             pedwarn (token->location, OPT_pedantic, 
3290                      "ISO C++ forbids braced-groups within expressions");
3291
3292             /* And they're not allowed outside of a function-body; you
3293                cannot, for example, write:
3294
3295                  int i = ({ int j = 3; j + 1; });
3296
3297                at class or namespace scope.  */
3298             if (!parser->in_function_body
3299                 || parser->in_template_argument_list_p)
3300               {
3301                 error_at (token->location,
3302                           "statement-expressions are not allowed outside "
3303                           "functions nor in template-argument lists");
3304                 cp_parser_skip_to_end_of_block_or_statement (parser);
3305                 expr = error_mark_node;
3306               }
3307             else
3308               {
3309                 /* Start the statement-expression.  */
3310                 expr = begin_stmt_expr ();
3311                 /* Parse the compound-statement.  */
3312                 cp_parser_compound_statement (parser, expr, false);
3313                 /* Finish up.  */
3314                 expr = finish_stmt_expr (expr, false);
3315               }
3316           }
3317         else
3318           {
3319             /* Parse the parenthesized expression.  */
3320             expr = cp_parser_expression (parser, cast_p, idk);
3321             /* Let the front end know that this expression was
3322                enclosed in parentheses. This matters in case, for
3323                example, the expression is of the form `A::B', since
3324                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3325                not.  */
3326             finish_parenthesized_expr (expr);
3327           }
3328         /* The `>' token might be the end of a template-id or
3329            template-parameter-list now.  */
3330         parser->greater_than_is_operator_p
3331           = saved_greater_than_is_operator_p;
3332         /* Consume the `)'.  */
3333         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3334           cp_parser_skip_to_end_of_statement (parser);
3335
3336         return expr;
3337       }
3338
3339     case CPP_OPEN_SQUARE:
3340       if (c_dialect_objc ())
3341         /* We have an Objective-C++ message. */
3342         return cp_parser_objc_expression (parser);
3343       maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3344       return cp_parser_lambda_expression (parser);
3345
3346     case CPP_OBJC_STRING:
3347       if (c_dialect_objc ())
3348         /* We have an Objective-C++ string literal. */
3349         return cp_parser_objc_expression (parser);
3350       cp_parser_error (parser, "expected primary-expression");
3351       return error_mark_node;
3352
3353     case CPP_KEYWORD:
3354       switch (token->keyword)
3355         {
3356           /* These two are the boolean literals.  */
3357         case RID_TRUE:
3358           cp_lexer_consume_token (parser->lexer);
3359           return boolean_true_node;
3360         case RID_FALSE:
3361           cp_lexer_consume_token (parser->lexer);
3362           return boolean_false_node;
3363
3364           /* The `__null' literal.  */
3365         case RID_NULL:
3366           cp_lexer_consume_token (parser->lexer);
3367           return null_node;
3368
3369           /* Recognize the `this' keyword.  */
3370         case RID_THIS:
3371           cp_lexer_consume_token (parser->lexer);
3372           if (parser->local_variables_forbidden_p)
3373             {
3374               error_at (token->location,
3375                         "%<this%> may not be used in this context");
3376               return error_mark_node;
3377             }
3378           /* Pointers cannot appear in constant-expressions.  */
3379           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3380             return error_mark_node;
3381           return finish_this_expr ();
3382
3383           /* The `operator' keyword can be the beginning of an
3384              id-expression.  */
3385         case RID_OPERATOR:
3386           goto id_expression;
3387
3388         case RID_FUNCTION_NAME:
3389         case RID_PRETTY_FUNCTION_NAME:
3390         case RID_C99_FUNCTION_NAME:
3391           {
3392             const char *name;
3393
3394             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3395                __func__ are the names of variables -- but they are
3396                treated specially.  Therefore, they are handled here,
3397                rather than relying on the generic id-expression logic
3398                below.  Grammatically, these names are id-expressions.
3399
3400                Consume the token.  */
3401             token = cp_lexer_consume_token (parser->lexer);
3402
3403             switch (token->keyword)
3404               {
3405               case RID_FUNCTION_NAME:
3406                 name = "%<__FUNCTION__%>";
3407                 break;
3408               case RID_PRETTY_FUNCTION_NAME:
3409                 name = "%<__PRETTY_FUNCTION__%>";
3410                 break;
3411               case RID_C99_FUNCTION_NAME:
3412                 name = "%<__func__%>";
3413                 break;
3414               default:
3415                 gcc_unreachable ();
3416               }
3417
3418             if (cp_parser_non_integral_constant_expression (parser, name))
3419               return error_mark_node;
3420
3421             /* Look up the name.  */
3422             return finish_fname (token->u.value);
3423           }
3424
3425         case RID_VA_ARG:
3426           {
3427             tree expression;
3428             tree type;
3429
3430             /* The `__builtin_va_arg' construct is used to handle
3431                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3432             cp_lexer_consume_token (parser->lexer);
3433             /* Look for the opening `('.  */
3434             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3435             /* Now, parse the assignment-expression.  */
3436             expression = cp_parser_assignment_expression (parser,
3437                                                           /*cast_p=*/false, NULL);
3438             /* Look for the `,'.  */
3439             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3440             /* Parse the type-id.  */
3441             type = cp_parser_type_id (parser);
3442             /* Look for the closing `)'.  */
3443             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3444             /* Using `va_arg' in a constant-expression is not
3445                allowed.  */
3446             if (cp_parser_non_integral_constant_expression (parser,
3447                                                             "%<va_arg%>"))
3448               return error_mark_node;
3449             return build_x_va_arg (expression, type);
3450           }
3451
3452         case RID_OFFSETOF:
3453           return cp_parser_builtin_offsetof (parser);
3454
3455         case RID_HAS_NOTHROW_ASSIGN:
3456         case RID_HAS_NOTHROW_CONSTRUCTOR:
3457         case RID_HAS_NOTHROW_COPY:        
3458         case RID_HAS_TRIVIAL_ASSIGN:
3459         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3460         case RID_HAS_TRIVIAL_COPY:        
3461         case RID_HAS_TRIVIAL_DESTRUCTOR:
3462         case RID_HAS_VIRTUAL_DESTRUCTOR:
3463         case RID_IS_ABSTRACT:
3464         case RID_IS_BASE_OF:
3465         case RID_IS_CLASS:
3466         case RID_IS_CONVERTIBLE_TO:
3467         case RID_IS_EMPTY:
3468         case RID_IS_ENUM:
3469         case RID_IS_POD:
3470         case RID_IS_POLYMORPHIC:
3471         case RID_IS_STD_LAYOUT:
3472         case RID_IS_TRIVIAL:
3473         case RID_IS_UNION:
3474           return cp_parser_trait_expr (parser, token->keyword);
3475
3476         /* Objective-C++ expressions.  */
3477         case RID_AT_ENCODE:
3478         case RID_AT_PROTOCOL:
3479         case RID_AT_SELECTOR:
3480           return cp_parser_objc_expression (parser);
3481
3482         default:
3483           cp_parser_error (parser, "expected primary-expression");
3484           return error_mark_node;
3485         }
3486
3487       /* An id-expression can start with either an identifier, a
3488          `::' as the beginning of a qualified-id, or the "operator"
3489          keyword.  */
3490     case CPP_NAME:
3491     case CPP_SCOPE:
3492     case CPP_TEMPLATE_ID:
3493     case CPP_NESTED_NAME_SPECIFIER:
3494       {
3495         tree id_expression;
3496         tree decl;
3497         const char *error_msg;
3498         bool template_p;
3499         bool done;
3500         cp_token *id_expr_token;
3501
3502       id_expression:
3503         /* Parse the id-expression.  */
3504         id_expression
3505           = cp_parser_id_expression (parser,
3506                                      /*template_keyword_p=*/false,
3507                                      /*check_dependency_p=*/true,
3508                                      &template_p,
3509                                      /*declarator_p=*/false,
3510                                      /*optional_p=*/false);
3511         if (id_expression == error_mark_node)
3512           return error_mark_node;
3513         id_expr_token = token;
3514         token = cp_lexer_peek_token (parser->lexer);
3515         done = (token->type != CPP_OPEN_SQUARE
3516                 && token->type != CPP_OPEN_PAREN
3517                 && token->type != CPP_DOT
3518                 && token->type != CPP_DEREF
3519                 && token->type != CPP_PLUS_PLUS
3520                 && token->type != CPP_MINUS_MINUS);
3521         /* If we have a template-id, then no further lookup is
3522            required.  If the template-id was for a template-class, we
3523            will sometimes have a TYPE_DECL at this point.  */
3524         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3525                  || TREE_CODE (id_expression) == TYPE_DECL)
3526           decl = id_expression;
3527         /* Look up the name.  */
3528         else
3529           {
3530             tree ambiguous_decls;
3531
3532             /* If we already know that this lookup is ambiguous, then
3533                we've already issued an error message; there's no reason
3534                to check again.  */
3535             if (id_expr_token->type == CPP_NAME
3536                 && id_expr_token->ambiguous_p)
3537               {
3538                 cp_parser_simulate_error (parser);
3539                 return error_mark_node;
3540               }
3541
3542             decl = cp_parser_lookup_name (parser, id_expression,
3543                                           none_type,
3544                                           template_p,
3545                                           /*is_namespace=*/false,
3546                                           /*check_dependency=*/true,
3547                                           &ambiguous_decls,
3548                                           id_expr_token->location);
3549             /* If the lookup was ambiguous, an error will already have
3550                been issued.  */
3551             if (ambiguous_decls)
3552               return error_mark_node;
3553
3554             /* In Objective-C++, an instance variable (ivar) may be preferred
3555                to whatever cp_parser_lookup_name() found.  */
3556             decl = objc_lookup_ivar (decl, id_expression);
3557
3558             /* If name lookup gives us a SCOPE_REF, then the
3559                qualifying scope was dependent.  */
3560             if (TREE_CODE (decl) == SCOPE_REF)
3561               {
3562                 /* At this point, we do not know if DECL is a valid
3563                    integral constant expression.  We assume that it is
3564                    in fact such an expression, so that code like:
3565
3566                       template <int N> struct A {
3567                         int a[B<N>::i];
3568                       };
3569                      
3570                    is accepted.  At template-instantiation time, we
3571                    will check that B<N>::i is actually a constant.  */
3572                 return decl;
3573               }
3574             /* Check to see if DECL is a local variable in a context
3575                where that is forbidden.  */
3576             if (parser->local_variables_forbidden_p
3577                 && local_variable_p (decl))
3578               {
3579                 /* It might be that we only found DECL because we are
3580                    trying to be generous with pre-ISO scoping rules.
3581                    For example, consider:
3582
3583                      int i;
3584                      void g() {
3585                        for (int i = 0; i < 10; ++i) {}
3586                        extern void f(int j = i);
3587                      }
3588
3589                    Here, name look up will originally find the out
3590                    of scope `i'.  We need to issue a warning message,
3591                    but then use the global `i'.  */
3592                 decl = check_for_out_of_scope_variable (decl);
3593                 if (local_variable_p (decl))
3594                   {
3595                     error_at (id_expr_token->location,
3596                               "local variable %qD may not appear in this context",
3597                               decl);
3598                     return error_mark_node;
3599                   }
3600               }
3601           }
3602
3603         decl = (finish_id_expression
3604                 (id_expression, decl, parser->scope,
3605                  idk,
3606                  parser->integral_constant_expression_p,
3607                  parser->allow_non_integral_constant_expression_p,
3608                  &parser->non_integral_constant_expression_p,
3609                  template_p, done, address_p,
3610                  template_arg_p,
3611                  &error_msg,
3612                  id_expr_token->location));
3613         if (error_msg)
3614           cp_parser_error (parser, error_msg);
3615         return decl;
3616       }
3617
3618       /* Anything else is an error.  */
3619     default:
3620       cp_parser_error (parser, "expected primary-expression");
3621       return error_mark_node;
3622     }
3623 }
3624
3625 /* Parse an id-expression.
3626
3627    id-expression:
3628      unqualified-id
3629      qualified-id
3630
3631    qualified-id:
3632      :: [opt] nested-name-specifier template [opt] unqualified-id
3633      :: identifier
3634      :: operator-function-id
3635      :: template-id
3636
3637    Return a representation of the unqualified portion of the
3638    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3639    a `::' or nested-name-specifier.
3640
3641    Often, if the id-expression was a qualified-id, the caller will
3642    want to make a SCOPE_REF to represent the qualified-id.  This
3643    function does not do this in order to avoid wastefully creating
3644    SCOPE_REFs when they are not required.
3645
3646    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3647    `template' keyword.
3648
3649    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3650    uninstantiated templates.
3651
3652    If *TEMPLATE_P is non-NULL, it is set to true iff the
3653    `template' keyword is used to explicitly indicate that the entity
3654    named is a template.
3655
3656    If DECLARATOR_P is true, the id-expression is appearing as part of
3657    a declarator, rather than as part of an expression.  */
3658
3659 static tree
3660 cp_parser_id_expression (cp_parser *parser,
3661                          bool template_keyword_p,
3662                          bool check_dependency_p,
3663                          bool *template_p,
3664                          bool declarator_p,
3665                          bool optional_p)
3666 {
3667   bool global_scope_p;
3668   bool nested_name_specifier_p;
3669
3670   /* Assume the `template' keyword was not used.  */
3671   if (template_p)
3672     *template_p = template_keyword_p;
3673
3674   /* Look for the optional `::' operator.  */
3675   global_scope_p
3676     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3677        != NULL_TREE);
3678   /* Look for the optional nested-name-specifier.  */
3679   nested_name_specifier_p
3680     = (cp_parser_nested_name_specifier_opt (parser,
3681                                             /*typename_keyword_p=*/false,
3682                                             check_dependency_p,
3683                                             /*type_p=*/false,
3684                                             declarator_p)
3685        != NULL_TREE);
3686   /* If there is a nested-name-specifier, then we are looking at
3687      the first qualified-id production.  */
3688   if (nested_name_specifier_p)
3689     {
3690       tree saved_scope;
3691       tree saved_object_scope;
3692       tree saved_qualifying_scope;
3693       tree unqualified_id;
3694       bool is_template;
3695
3696       /* See if the next token is the `template' keyword.  */
3697       if (!template_p)
3698         template_p = &is_template;
3699       *template_p = cp_parser_optional_template_keyword (parser);
3700       /* Name lookup we do during the processing of the
3701          unqualified-id might obliterate SCOPE.  */
3702       saved_scope = parser->scope;
3703       saved_object_scope = parser->object_scope;
3704       saved_qualifying_scope = parser->qualifying_scope;
3705       /* Process the final unqualified-id.  */
3706       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3707                                                  check_dependency_p,
3708                                                  declarator_p,
3709                                                  /*optional_p=*/false);
3710       /* Restore the SAVED_SCOPE for our caller.  */
3711       parser->scope = saved_scope;
3712       parser->object_scope = saved_object_scope;
3713       parser->qualifying_scope = saved_qualifying_scope;
3714
3715       return unqualified_id;
3716     }
3717   /* Otherwise, if we are in global scope, then we are looking at one
3718      of the other qualified-id productions.  */
3719   else if (global_scope_p)
3720     {
3721       cp_token *token;
3722       tree id;
3723
3724       /* Peek at the next token.  */
3725       token = cp_lexer_peek_token (parser->lexer);
3726
3727       /* If it's an identifier, and the next token is not a "<", then
3728          we can avoid the template-id case.  This is an optimization
3729          for this common case.  */
3730       if (token->type == CPP_NAME
3731           && !cp_parser_nth_token_starts_template_argument_list_p
3732                (parser, 2))
3733         return cp_parser_identifier (parser);
3734
3735       cp_parser_parse_tentatively (parser);
3736       /* Try a template-id.  */
3737       id = cp_parser_template_id (parser,
3738                                   /*template_keyword_p=*/false,
3739                                   /*check_dependency_p=*/true,
3740                                   declarator_p);
3741       /* If that worked, we're done.  */
3742       if (cp_parser_parse_definitely (parser))
3743         return id;
3744
3745       /* Peek at the next token.  (Changes in the token buffer may
3746          have invalidated the pointer obtained above.)  */
3747       token = cp_lexer_peek_token (parser->lexer);
3748
3749       switch (token->type)
3750         {
3751         case CPP_NAME:
3752           return cp_parser_identifier (parser);
3753
3754         case CPP_KEYWORD:
3755           if (token->keyword == RID_OPERATOR)
3756             return cp_parser_operator_function_id (parser);
3757           /* Fall through.  */
3758
3759         default:
3760           cp_parser_error (parser, "expected id-expression");
3761           return error_mark_node;
3762         }
3763     }
3764   else
3765     return cp_parser_unqualified_id (parser, template_keyword_p,
3766                                      /*check_dependency_p=*/true,
3767                                      declarator_p,
3768                                      optional_p);
3769 }
3770
3771 /* Parse an unqualified-id.
3772
3773    unqualified-id:
3774      identifier
3775      operator-function-id
3776      conversion-function-id
3777      ~ class-name
3778      template-id
3779
3780    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3781    keyword, in a construct like `A::template ...'.
3782
3783    Returns a representation of unqualified-id.  For the `identifier'
3784    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3785    production a BIT_NOT_EXPR is returned; the operand of the
3786    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3787    other productions, see the documentation accompanying the
3788    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3789    names are looked up in uninstantiated templates.  If DECLARATOR_P
3790    is true, the unqualified-id is appearing as part of a declarator,
3791    rather than as part of an expression.  */
3792
3793 static tree
3794 cp_parser_unqualified_id (cp_parser* parser,
3795                           bool template_keyword_p,
3796                           bool check_dependency_p,
3797                           bool declarator_p,
3798                           bool optional_p)
3799 {
3800   cp_token *token;
3801
3802   /* Peek at the next token.  */
3803   token = cp_lexer_peek_token (parser->lexer);
3804
3805   switch (token->type)
3806     {
3807     case CPP_NAME:
3808       {
3809         tree id;
3810
3811         /* We don't know yet whether or not this will be a
3812            template-id.  */
3813         cp_parser_parse_tentatively (parser);
3814         /* Try a template-id.  */
3815         id = cp_parser_template_id (parser, template_keyword_p,
3816                                     check_dependency_p,
3817                                     declarator_p);
3818         /* If it worked, we're done.  */
3819         if (cp_parser_parse_definitely (parser))
3820           return id;
3821         /* Otherwise, it's an ordinary identifier.  */
3822         return cp_parser_identifier (parser);
3823       }
3824
3825     case CPP_TEMPLATE_ID:
3826       return cp_parser_template_id (parser, template_keyword_p,
3827                                     check_dependency_p,
3828                                     declarator_p);
3829
3830     case CPP_COMPL:
3831       {
3832         tree type_decl;
3833         tree qualifying_scope;
3834         tree object_scope;
3835         tree scope;
3836         bool done;
3837
3838         /* Consume the `~' token.  */
3839         cp_lexer_consume_token (parser->lexer);
3840         /* Parse the class-name.  The standard, as written, seems to
3841            say that:
3842
3843              template <typename T> struct S { ~S (); };
3844              template <typename T> S<T>::~S() {}
3845
3846            is invalid, since `~' must be followed by a class-name, but
3847            `S<T>' is dependent, and so not known to be a class.
3848            That's not right; we need to look in uninstantiated
3849            templates.  A further complication arises from:
3850
3851              template <typename T> void f(T t) {
3852                t.T::~T();
3853              }
3854
3855            Here, it is not possible to look up `T' in the scope of `T'
3856            itself.  We must look in both the current scope, and the
3857            scope of the containing complete expression.
3858
3859            Yet another issue is:
3860
3861              struct S {
3862                int S;
3863                ~S();
3864              };
3865
3866              S::~S() {}
3867
3868            The standard does not seem to say that the `S' in `~S'
3869            should refer to the type `S' and not the data member
3870            `S::S'.  */
3871
3872         /* DR 244 says that we look up the name after the "~" in the
3873            same scope as we looked up the qualifying name.  That idea
3874            isn't fully worked out; it's more complicated than that.  */
3875         scope = parser->scope;
3876         object_scope = parser->object_scope;
3877         qualifying_scope = parser->qualifying_scope;
3878
3879         /* Check for invalid scopes.  */
3880         if (scope == error_mark_node)
3881           {
3882             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3883               cp_lexer_consume_token (parser->lexer);
3884             return error_mark_node;
3885           }
3886         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3887           {
3888             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3889               error_at (token->location,
3890                         "scope %qT before %<~%> is not a class-name",
3891                         scope);
3892             cp_parser_simulate_error (parser);
3893             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3894               cp_lexer_consume_token (parser->lexer);
3895             return error_mark_node;
3896           }
3897         gcc_assert (!scope || TYPE_P (scope));
3898
3899         /* If the name is of the form "X::~X" it's OK.  */
3900         token = cp_lexer_peek_token (parser->lexer);
3901         if (scope
3902             && token->type == CPP_NAME
3903             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3904                 != CPP_LESS)
3905             && constructor_name_p (token->u.value, scope))
3906           {
3907             cp_lexer_consume_token (parser->lexer);
3908             return build_nt (BIT_NOT_EXPR, scope);
3909           }
3910
3911         /* If there was an explicit qualification (S::~T), first look
3912            in the scope given by the qualification (i.e., S).
3913
3914            Note: in the calls to cp_parser_class_name below we pass
3915            typename_type so that lookup finds the injected-class-name
3916            rather than the constructor.  */
3917         done = false;
3918         type_decl = NULL_TREE;
3919         if (scope)
3920           {
3921             cp_parser_parse_tentatively (parser);
3922             type_decl = cp_parser_class_name (parser,
3923                                               /*typename_keyword_p=*/false,
3924                                               /*template_keyword_p=*/false,
3925                                               typename_type,
3926                                               /*check_dependency=*/false,
3927                                               /*class_head_p=*/false,
3928                                               declarator_p);
3929             if (cp_parser_parse_definitely (parser))
3930               done = true;
3931           }
3932         /* In "N::S::~S", look in "N" as well.  */
3933         if (!done && scope && qualifying_scope)
3934           {
3935             cp_parser_parse_tentatively (parser);
3936             parser->scope = qualifying_scope;
3937             parser->object_scope = NULL_TREE;
3938             parser->qualifying_scope = NULL_TREE;
3939             type_decl
3940               = cp_parser_class_name (parser,
3941                                       /*typename_keyword_p=*/false,
3942                                       /*template_keyword_p=*/false,
3943                                       typename_type,
3944                                       /*check_dependency=*/false,
3945                                       /*class_head_p=*/false,
3946                                       declarator_p);
3947             if (cp_parser_parse_definitely (parser))
3948               done = true;
3949           }
3950         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3951         else if (!done && object_scope)
3952           {
3953             cp_parser_parse_tentatively (parser);
3954             parser->scope = object_scope;
3955             parser->object_scope = NULL_TREE;
3956             parser->qualifying_scope = NULL_TREE;
3957             type_decl
3958               = cp_parser_class_name (parser,
3959                                       /*typename_keyword_p=*/false,
3960                                       /*template_keyword_p=*/false,
3961                                       typename_type,
3962                                       /*check_dependency=*/false,
3963                                       /*class_head_p=*/false,
3964                                       declarator_p);
3965             if (cp_parser_parse_definitely (parser))
3966               done = true;
3967           }
3968         /* Look in the surrounding context.  */
3969         if (!done)
3970           {
3971             parser->scope = NULL_TREE;
3972             parser->object_scope = NULL_TREE;
3973             parser->qualifying_scope = NULL_TREE;
3974             if (processing_template_decl)
3975               cp_parser_parse_tentatively (parser);
3976             type_decl
3977               = cp_parser_class_name (parser,
3978                                       /*typename_keyword_p=*/false,
3979                                       /*template_keyword_p=*/false,
3980                                       typename_type,
3981                                       /*check_dependency=*/false,
3982                                       /*class_head_p=*/false,
3983                                       declarator_p);
3984             if (processing_template_decl
3985                 && ! cp_parser_parse_definitely (parser))
3986               {
3987                 /* We couldn't find a type with this name, so just accept
3988                    it and check for a match at instantiation time.  */
3989                 type_decl = cp_parser_identifier (parser);
3990                 if (type_decl != error_mark_node)
3991                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3992                 return type_decl;
3993               }
3994           }
3995         /* If an error occurred, assume that the name of the
3996            destructor is the same as the name of the qualifying
3997            class.  That allows us to keep parsing after running
3998            into ill-formed destructor names.  */
3999         if (type_decl == error_mark_node && scope)
4000           return build_nt (BIT_NOT_EXPR, scope);
4001         else if (type_decl == error_mark_node)
4002           return error_mark_node;
4003
4004         /* Check that destructor name and scope match.  */
4005         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4006           {
4007             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4008               error_at (token->location,
4009                         "declaration of %<~%T%> as member of %qT",
4010                         type_decl, scope);
4011             cp_parser_simulate_error (parser);
4012             return error_mark_node;
4013           }
4014
4015         /* [class.dtor]
4016
4017            A typedef-name that names a class shall not be used as the
4018            identifier in the declarator for a destructor declaration.  */
4019         if (declarator_p
4020             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4021             && !DECL_SELF_REFERENCE_P (type_decl)
4022             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4023           error_at (token->location,
4024                     "typedef-name %qD used as destructor declarator",
4025                     type_decl);
4026
4027         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4028       }
4029
4030     case CPP_KEYWORD:
4031       if (token->keyword == RID_OPERATOR)
4032         {
4033           tree id;
4034
4035           /* This could be a template-id, so we try that first.  */
4036           cp_parser_parse_tentatively (parser);
4037           /* Try a template-id.  */
4038           id = cp_parser_template_id (parser, template_keyword_p,
4039                                       /*check_dependency_p=*/true,
4040                                       declarator_p);
4041           /* If that worked, we're done.  */
4042           if (cp_parser_parse_definitely (parser))
4043             return id;
4044           /* We still don't know whether we're looking at an
4045              operator-function-id or a conversion-function-id.  */
4046           cp_parser_parse_tentatively (parser);
4047           /* Try an operator-function-id.  */
4048           id = cp_parser_operator_function_id (parser);
4049           /* If that didn't work, try a conversion-function-id.  */
4050           if (!cp_parser_parse_definitely (parser))
4051             id = cp_parser_conversion_function_id (parser);
4052
4053           return id;
4054         }
4055       /* Fall through.  */
4056
4057     default:
4058       if (optional_p)
4059         return NULL_TREE;
4060       cp_parser_error (parser, "expected unqualified-id");
4061       return error_mark_node;
4062     }
4063 }
4064
4065 /* Parse an (optional) nested-name-specifier.
4066
4067    nested-name-specifier: [C++98]
4068      class-or-namespace-name :: nested-name-specifier [opt]
4069      class-or-namespace-name :: template nested-name-specifier [opt]
4070
4071    nested-name-specifier: [C++0x]
4072      type-name ::
4073      namespace-name ::
4074      nested-name-specifier identifier ::
4075      nested-name-specifier template [opt] simple-template-id ::
4076
4077    PARSER->SCOPE should be set appropriately before this function is
4078    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4079    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4080    in name lookups.
4081
4082    Sets PARSER->SCOPE to the class (TYPE) or namespace
4083    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4084    it unchanged if there is no nested-name-specifier.  Returns the new
4085    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4086
4087    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4088    part of a declaration and/or decl-specifier.  */
4089
4090 static tree
4091 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4092                                      bool typename_keyword_p,
4093                                      bool check_dependency_p,
4094                                      bool type_p,
4095                                      bool is_declaration)
4096 {
4097   bool success = false;
4098   cp_token_position start = 0;
4099   cp_token *token;
4100
4101   /* Remember where the nested-name-specifier starts.  */
4102   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4103     {
4104       start = cp_lexer_token_position (parser->lexer, false);
4105       push_deferring_access_checks (dk_deferred);
4106     }
4107
4108   while (true)
4109     {
4110       tree new_scope;
4111       tree old_scope;
4112       tree saved_qualifying_scope;
4113       bool template_keyword_p;
4114
4115       /* Spot cases that cannot be the beginning of a
4116          nested-name-specifier.  */
4117       token = cp_lexer_peek_token (parser->lexer);
4118
4119       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4120          the already parsed nested-name-specifier.  */
4121       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4122         {
4123           /* Grab the nested-name-specifier and continue the loop.  */
4124           cp_parser_pre_parsed_nested_name_specifier (parser);
4125           /* If we originally encountered this nested-name-specifier
4126              with IS_DECLARATION set to false, we will not have
4127              resolved TYPENAME_TYPEs, so we must do so here.  */
4128           if (is_declaration
4129               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4130             {
4131               new_scope = resolve_typename_type (parser->scope,
4132                                                  /*only_current_p=*/false);
4133               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4134                 parser->scope = new_scope;
4135             }
4136           success = true;
4137           continue;
4138         }
4139
4140       /* Spot cases that cannot be the beginning of a
4141          nested-name-specifier.  On the second and subsequent times
4142          through the loop, we look for the `template' keyword.  */
4143       if (success && token->keyword == RID_TEMPLATE)
4144         ;
4145       /* A template-id can start a nested-name-specifier.  */
4146       else if (token->type == CPP_TEMPLATE_ID)
4147         ;
4148       else
4149         {
4150           /* If the next token is not an identifier, then it is
4151              definitely not a type-name or namespace-name.  */
4152           if (token->type != CPP_NAME)
4153             break;
4154           /* If the following token is neither a `<' (to begin a
4155              template-id), nor a `::', then we are not looking at a
4156              nested-name-specifier.  */
4157           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4158           if (token->type != CPP_SCOPE
4159               && !cp_parser_nth_token_starts_template_argument_list_p
4160                   (parser, 2))
4161             break;
4162         }
4163
4164       /* The nested-name-specifier is optional, so we parse
4165          tentatively.  */
4166       cp_parser_parse_tentatively (parser);
4167
4168       /* Look for the optional `template' keyword, if this isn't the
4169          first time through the loop.  */
4170       if (success)
4171         template_keyword_p = cp_parser_optional_template_keyword (parser);
4172       else
4173         template_keyword_p = false;
4174
4175       /* Save the old scope since the name lookup we are about to do
4176          might destroy it.  */
4177       old_scope = parser->scope;
4178       saved_qualifying_scope = parser->qualifying_scope;
4179       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4180          look up names in "X<T>::I" in order to determine that "Y" is
4181          a template.  So, if we have a typename at this point, we make
4182          an effort to look through it.  */
4183       if (is_declaration
4184           && !typename_keyword_p
4185           && parser->scope
4186           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4187         parser->scope = resolve_typename_type (parser->scope,
4188                                                /*only_current_p=*/false);
4189       /* Parse the qualifying entity.  */
4190       new_scope
4191         = cp_parser_qualifying_entity (parser,
4192                                        typename_keyword_p,
4193                                        template_keyword_p,
4194                                        check_dependency_p,
4195                                        type_p,
4196                                        is_declaration);
4197       /* Look for the `::' token.  */
4198       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4199
4200       /* If we found what we wanted, we keep going; otherwise, we're
4201          done.  */
4202       if (!cp_parser_parse_definitely (parser))
4203         {
4204           bool error_p = false;
4205
4206           /* Restore the OLD_SCOPE since it was valid before the
4207              failed attempt at finding the last
4208              class-or-namespace-name.  */
4209           parser->scope = old_scope;
4210           parser->qualifying_scope = saved_qualifying_scope;
4211           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4212             break;
4213           /* If the next token is an identifier, and the one after
4214              that is a `::', then any valid interpretation would have
4215              found a class-or-namespace-name.  */
4216           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4217                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4218                      == CPP_SCOPE)
4219                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4220                      != CPP_COMPL))
4221             {
4222               token = cp_lexer_consume_token (parser->lexer);
4223               if (!error_p)
4224                 {
4225                   if (!token->ambiguous_p)
4226                     {
4227                       tree decl;
4228                       tree ambiguous_decls;
4229
4230                       decl = cp_parser_lookup_name (parser, token->u.value,
4231                                                     none_type,
4232                                                     /*is_template=*/false,
4233                                                     /*is_namespace=*/false,
4234                                                     /*check_dependency=*/true,
4235                                                     &ambiguous_decls,
4236                                                     token->location);
4237                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4238                         error_at (token->location,
4239                                   "%qD used without template parameters",
4240                                   decl);
4241                       else if (ambiguous_decls)
4242                         {
4243                           error_at (token->location,
4244                                     "reference to %qD is ambiguous",
4245                                     token->u.value);
4246                           print_candidates (ambiguous_decls);
4247                           decl = error_mark_node;
4248                         }
4249                       else
4250                         {
4251                           const char* msg = "is not a class or namespace";
4252                           if (cxx_dialect != cxx98)
4253                             msg = "is not a class, namespace, or enumeration";
4254                           cp_parser_name_lookup_error
4255                             (parser, token->u.value, decl, msg,
4256                              token->location);
4257                         }
4258                     }
4259                   parser->scope = error_mark_node;
4260                   error_p = true;
4261                   /* Treat this as a successful nested-name-specifier
4262                      due to:
4263
4264                      [basic.lookup.qual]
4265
4266                      If the name found is not a class-name (clause
4267                      _class_) or namespace-name (_namespace.def_), the
4268                      program is ill-formed.  */
4269                   success = true;
4270                 }
4271               cp_lexer_consume_token (parser->lexer);
4272             }
4273           break;
4274         }
4275       /* We've found one valid nested-name-specifier.  */
4276       success = true;
4277       /* Name lookup always gives us a DECL.  */
4278       if (TREE_CODE (new_scope) == TYPE_DECL)
4279         new_scope = TREE_TYPE (new_scope);
4280       /* Uses of "template" must be followed by actual templates.  */
4281       if (template_keyword_p
4282           && !(CLASS_TYPE_P (new_scope)
4283                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4284                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4285                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4286           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4287                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4288                    == TEMPLATE_ID_EXPR)))
4289         permerror (input_location, TYPE_P (new_scope)
4290                    ? "%qT is not a template"
4291                    : "%qD is not a template",
4292                    new_scope);
4293       /* If it is a class scope, try to complete it; we are about to
4294          be looking up names inside the class.  */
4295       if (TYPE_P (new_scope)
4296           /* Since checking types for dependency can be expensive,
4297              avoid doing it if the type is already complete.  */
4298           && !COMPLETE_TYPE_P (new_scope)
4299           /* Do not try to complete dependent types.  */
4300           && !dependent_type_p (new_scope))
4301         {
4302           new_scope = complete_type (new_scope);
4303           /* If it is a typedef to current class, use the current
4304              class instead, as the typedef won't have any names inside
4305              it yet.  */
4306           if (!COMPLETE_TYPE_P (new_scope)
4307               && currently_open_class (new_scope))
4308             new_scope = TYPE_MAIN_VARIANT (new_scope);
4309         }
4310       /* Make sure we look in the right scope the next time through
4311          the loop.  */
4312       parser->scope = new_scope;
4313     }
4314
4315   /* If parsing tentatively, replace the sequence of tokens that makes
4316      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4317      token.  That way, should we re-parse the token stream, we will
4318      not have to repeat the effort required to do the parse, nor will
4319      we issue duplicate error messages.  */
4320   if (success && start)
4321     {
4322       cp_token *token;
4323
4324       token = cp_lexer_token_at (parser->lexer, start);
4325       /* Reset the contents of the START token.  */
4326       token->type = CPP_NESTED_NAME_SPECIFIER;
4327       /* Retrieve any deferred checks.  Do not pop this access checks yet
4328          so the memory will not be reclaimed during token replacing below.  */
4329       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4330       token->u.tree_check_value->value = parser->scope;
4331       token->u.tree_check_value->checks = get_deferred_access_checks ();
4332       token->u.tree_check_value->qualifying_scope =
4333         parser->qualifying_scope;
4334       token->keyword = RID_MAX;
4335
4336       /* Purge all subsequent tokens.  */
4337       cp_lexer_purge_tokens_after (parser->lexer, start);
4338     }
4339
4340   if (start)
4341     pop_to_parent_deferring_access_checks ();
4342
4343   return success ? parser->scope : NULL_TREE;
4344 }
4345
4346 /* Parse a nested-name-specifier.  See
4347    cp_parser_nested_name_specifier_opt for details.  This function
4348    behaves identically, except that it will an issue an error if no
4349    nested-name-specifier is present.  */
4350
4351 static tree
4352 cp_parser_nested_name_specifier (cp_parser *parser,
4353                                  bool typename_keyword_p,
4354                                  bool check_dependency_p,
4355                                  bool type_p,
4356                                  bool is_declaration)
4357 {
4358   tree scope;
4359
4360   /* Look for the nested-name-specifier.  */
4361   scope = cp_parser_nested_name_specifier_opt (parser,
4362                                                typename_keyword_p,
4363                                                check_dependency_p,
4364                                                type_p,
4365                                                is_declaration);
4366   /* If it was not present, issue an error message.  */
4367   if (!scope)
4368     {
4369       cp_parser_error (parser, "expected nested-name-specifier");
4370       parser->scope = NULL_TREE;
4371     }
4372
4373   return scope;
4374 }
4375
4376 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4377    this is either a class-name or a namespace-name (which corresponds
4378    to the class-or-namespace-name production in the grammar). For
4379    C++0x, it can also be a type-name that refers to an enumeration
4380    type.
4381
4382    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4383    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4384    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4385    TYPE_P is TRUE iff the next name should be taken as a class-name,
4386    even the same name is declared to be another entity in the same
4387    scope.
4388
4389    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4390    specified by the class-or-namespace-name.  If neither is found the
4391    ERROR_MARK_NODE is returned.  */
4392
4393 static tree
4394 cp_parser_qualifying_entity (cp_parser *parser,
4395                              bool typename_keyword_p,
4396                              bool template_keyword_p,
4397                              bool check_dependency_p,
4398                              bool type_p,
4399                              bool is_declaration)
4400 {
4401   tree saved_scope;
4402   tree saved_qualifying_scope;
4403   tree saved_object_scope;
4404   tree scope;
4405   bool only_class_p;
4406   bool successful_parse_p;
4407
4408   /* Before we try to parse the class-name, we must save away the
4409      current PARSER->SCOPE since cp_parser_class_name will destroy
4410      it.  */
4411   saved_scope = parser->scope;
4412   saved_qualifying_scope = parser->qualifying_scope;
4413   saved_object_scope = parser->object_scope;
4414   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4415      there is no need to look for a namespace-name.  */
4416   only_class_p = template_keyword_p 
4417     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4418   if (!only_class_p)
4419     cp_parser_parse_tentatively (parser);
4420   scope = cp_parser_class_name (parser,
4421                                 typename_keyword_p,
4422                                 template_keyword_p,
4423                                 type_p ? class_type : none_type,
4424                                 check_dependency_p,
4425                                 /*class_head_p=*/false,
4426                                 is_declaration);
4427   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4428   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4429   if (!only_class_p 
4430       && cxx_dialect != cxx98
4431       && !successful_parse_p)
4432     {
4433       /* Restore the saved scope.  */
4434       parser->scope = saved_scope;
4435       parser->qualifying_scope = saved_qualifying_scope;
4436       parser->object_scope = saved_object_scope;
4437
4438       /* Parse tentatively.  */
4439       cp_parser_parse_tentatively (parser);
4440      
4441       /* Parse a typedef-name or enum-name.  */
4442       scope = cp_parser_nonclass_name (parser);
4443       successful_parse_p = cp_parser_parse_definitely (parser);
4444     }
4445   /* If that didn't work, try for a namespace-name.  */
4446   if (!only_class_p && !successful_parse_p)
4447     {
4448       /* Restore the saved scope.  */
4449       parser->scope = saved_scope;
4450       parser->qualifying_scope = saved_qualifying_scope;
4451       parser->object_scope = saved_object_scope;
4452       /* If we are not looking at an identifier followed by the scope
4453          resolution operator, then this is not part of a
4454          nested-name-specifier.  (Note that this function is only used
4455          to parse the components of a nested-name-specifier.)  */
4456       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4457           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4458         return error_mark_node;
4459       scope = cp_parser_namespace_name (parser);
4460     }
4461
4462   return scope;
4463 }
4464
4465 /* Parse a postfix-expression.
4466
4467    postfix-expression:
4468      primary-expression
4469      postfix-expression [ expression ]
4470      postfix-expression ( expression-list [opt] )
4471      simple-type-specifier ( expression-list [opt] )
4472      typename :: [opt] nested-name-specifier identifier
4473        ( expression-list [opt] )
4474      typename :: [opt] nested-name-specifier template [opt] template-id
4475        ( expression-list [opt] )
4476      postfix-expression . template [opt] id-expression
4477      postfix-expression -> template [opt] id-expression
4478      postfix-expression . pseudo-destructor-name
4479      postfix-expression -> pseudo-destructor-name
4480      postfix-expression ++
4481      postfix-expression --
4482      dynamic_cast < type-id > ( expression )
4483      static_cast < type-id > ( expression )
4484      reinterpret_cast < type-id > ( expression )
4485      const_cast < type-id > ( expression )
4486      typeid ( expression )
4487      typeid ( type-id )
4488
4489    GNU Extension:
4490
4491    postfix-expression:
4492      ( type-id ) { initializer-list , [opt] }
4493
4494    This extension is a GNU version of the C99 compound-literal
4495    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4496    but they are essentially the same concept.)
4497
4498    If ADDRESS_P is true, the postfix expression is the operand of the
4499    `&' operator.  CAST_P is true if this expression is the target of a
4500    cast.
4501
4502    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4503    class member access expressions [expr.ref].
4504
4505    Returns a representation of the expression.  */
4506
4507 static tree
4508 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4509                               bool member_access_only_p,
4510                               cp_id_kind * pidk_return)
4511 {
4512   cp_token *token;
4513   enum rid keyword;
4514   cp_id_kind idk = CP_ID_KIND_NONE;
4515   tree postfix_expression = NULL_TREE;
4516   bool is_member_access = false;
4517
4518   /* Peek at the next token.  */
4519   token = cp_lexer_peek_token (parser->lexer);
4520   /* Some of the productions are determined by keywords.  */
4521   keyword = token->keyword;
4522   switch (keyword)
4523     {
4524     case RID_DYNCAST:
4525     case RID_STATCAST:
4526     case RID_REINTCAST:
4527     case RID_CONSTCAST:
4528       {
4529         tree type;
4530         tree expression;
4531         const char *saved_message;
4532
4533         /* All of these can be handled in the same way from the point
4534            of view of parsing.  Begin by consuming the token
4535            identifying the cast.  */
4536         cp_lexer_consume_token (parser->lexer);
4537
4538         /* New types cannot be defined in the cast.  */
4539         saved_message = parser->type_definition_forbidden_message;
4540         parser->type_definition_forbidden_message
4541           = G_("types may not be defined in casts");
4542
4543         /* Look for the opening `<'.  */
4544         cp_parser_require (parser, CPP_LESS, "%<<%>");
4545         /* Parse the type to which we are casting.  */
4546         type = cp_parser_type_id (parser);
4547         /* Look for the closing `>'.  */
4548         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4549         /* Restore the old message.  */
4550         parser->type_definition_forbidden_message = saved_message;
4551
4552         /* And the expression which is being cast.  */
4553         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4554         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4555         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4556
4557         /* Only type conversions to integral or enumeration types
4558            can be used in constant-expressions.  */
4559         if (!cast_valid_in_integral_constant_expression_p (type)
4560             && (cp_parser_non_integral_constant_expression
4561                 (parser,
4562                  "a cast to a type other than an integral or "
4563                  "enumeration type")))
4564           return error_mark_node;
4565
4566         switch (keyword)
4567           {
4568           case RID_DYNCAST:
4569             postfix_expression
4570               = build_dynamic_cast (type, expression, tf_warning_or_error);
4571             break;
4572           case RID_STATCAST:
4573             postfix_expression
4574               = build_static_cast (type, expression, tf_warning_or_error);
4575             break;
4576           case RID_REINTCAST:
4577             postfix_expression
4578               = build_reinterpret_cast (type, expression, 
4579                                         tf_warning_or_error);
4580             break;
4581           case RID_CONSTCAST:
4582             postfix_expression
4583               = build_const_cast (type, expression, tf_warning_or_error);
4584             break;
4585           default:
4586             gcc_unreachable ();
4587           }
4588       }
4589       break;
4590
4591     case RID_TYPEID:
4592       {
4593         tree type;
4594         const char *saved_message;
4595         bool saved_in_type_id_in_expr_p;
4596
4597         /* Consume the `typeid' token.  */
4598         cp_lexer_consume_token (parser->lexer);
4599         /* Look for the `(' token.  */
4600         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4601         /* Types cannot be defined in a `typeid' expression.  */
4602         saved_message = parser->type_definition_forbidden_message;
4603         parser->type_definition_forbidden_message
4604           = G_("types may not be defined in a %<typeid%> expression");
4605         /* We can't be sure yet whether we're looking at a type-id or an
4606            expression.  */
4607         cp_parser_parse_tentatively (parser);
4608         /* Try a type-id first.  */
4609         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4610         parser->in_type_id_in_expr_p = true;
4611         type = cp_parser_type_id (parser);
4612         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4613         /* Look for the `)' token.  Otherwise, we can't be sure that
4614            we're not looking at an expression: consider `typeid (int
4615            (3))', for example.  */
4616         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4617         /* If all went well, simply lookup the type-id.  */
4618         if (cp_parser_parse_definitely (parser))
4619           postfix_expression = get_typeid (type);
4620         /* Otherwise, fall back to the expression variant.  */
4621         else
4622           {
4623             tree expression;
4624
4625             /* Look for an expression.  */
4626             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4627             /* Compute its typeid.  */
4628             postfix_expression = build_typeid (expression);
4629             /* Look for the `)' token.  */
4630             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4631           }
4632         /* Restore the saved message.  */
4633         parser->type_definition_forbidden_message = saved_message;
4634         /* `typeid' may not appear in an integral constant expression.  */
4635         if (cp_parser_non_integral_constant_expression(parser,
4636                                                        "%<typeid%> operator"))
4637           return error_mark_node;
4638       }
4639       break;
4640
4641     case RID_TYPENAME:
4642       {
4643         tree type;
4644         /* The syntax permitted here is the same permitted for an
4645            elaborated-type-specifier.  */
4646         type = cp_parser_elaborated_type_specifier (parser,
4647                                                     /*is_friend=*/false,
4648                                                     /*is_declaration=*/false);
4649         postfix_expression = cp_parser_functional_cast (parser, type);
4650       }
4651       break;
4652
4653     default:
4654       {
4655         tree type;
4656
4657         /* If the next thing is a simple-type-specifier, we may be
4658            looking at a functional cast.  We could also be looking at
4659            an id-expression.  So, we try the functional cast, and if
4660            that doesn't work we fall back to the primary-expression.  */
4661         cp_parser_parse_tentatively (parser);
4662         /* Look for the simple-type-specifier.  */
4663         type = cp_parser_simple_type_specifier (parser,
4664                                                 /*decl_specs=*/NULL,
4665                                                 CP_PARSER_FLAGS_NONE);
4666         /* Parse the cast itself.  */
4667         if (!cp_parser_error_occurred (parser))
4668           postfix_expression
4669             = cp_parser_functional_cast (parser, type);
4670         /* If that worked, we're done.  */
4671         if (cp_parser_parse_definitely (parser))
4672           break;
4673
4674         /* If the functional-cast didn't work out, try a
4675            compound-literal.  */
4676         if (cp_parser_allow_gnu_extensions_p (parser)
4677             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4678           {
4679             VEC(constructor_elt,gc) *initializer_list = NULL;
4680             bool saved_in_type_id_in_expr_p;
4681
4682             cp_parser_parse_tentatively (parser);
4683             /* Consume the `('.  */
4684             cp_lexer_consume_token (parser->lexer);
4685             /* Parse the type.  */
4686             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4687             parser->in_type_id_in_expr_p = true;
4688             type = cp_parser_type_id (parser);
4689             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4690             /* Look for the `)'.  */
4691             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4692             /* Look for the `{'.  */
4693             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4694             /* If things aren't going well, there's no need to
4695                keep going.  */
4696             if (!cp_parser_error_occurred (parser))
4697               {
4698                 bool non_constant_p;
4699                 /* Parse the initializer-list.  */
4700                 initializer_list
4701                   = cp_parser_initializer_list (parser, &non_constant_p);
4702                 /* Allow a trailing `,'.  */
4703                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4704                   cp_lexer_consume_token (parser->lexer);
4705                 /* Look for the final `}'.  */
4706                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4707               }
4708             /* If that worked, we're definitely looking at a
4709                compound-literal expression.  */
4710             if (cp_parser_parse_definitely (parser))
4711               {
4712                 /* Warn the user that a compound literal is not
4713                    allowed in standard C++.  */
4714                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4715                 /* For simplicity, we disallow compound literals in
4716                    constant-expressions.  We could
4717                    allow compound literals of integer type, whose
4718                    initializer was a constant, in constant
4719                    expressions.  Permitting that usage, as a further
4720                    extension, would not change the meaning of any
4721                    currently accepted programs.  (Of course, as
4722                    compound literals are not part of ISO C++, the
4723                    standard has nothing to say.)  */
4724                 if (cp_parser_non_integral_constant_expression 
4725                     (parser, "non-constant compound literals"))
4726                   {
4727                     postfix_expression = error_mark_node;
4728                     break;
4729                   }
4730                 /* Form the representation of the compound-literal.  */
4731                 postfix_expression
4732                   = (finish_compound_literal
4733                      (type, build_constructor (init_list_type_node,
4734                                                initializer_list)));
4735                 break;
4736               }
4737           }
4738
4739         /* It must be a primary-expression.  */
4740         postfix_expression
4741           = cp_parser_primary_expression (parser, address_p, cast_p,
4742                                           /*template_arg_p=*/false,
4743                                           &idk);
4744       }
4745       break;
4746     }
4747
4748   /* Keep looping until the postfix-expression is complete.  */
4749   while (true)
4750     {
4751       if (idk == CP_ID_KIND_UNQUALIFIED
4752           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4753           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4754         /* It is not a Koenig lookup function call.  */
4755         postfix_expression
4756           = unqualified_name_lookup_error (postfix_expression);
4757
4758       /* Peek at the next token.  */
4759       token = cp_lexer_peek_token (parser->lexer);
4760
4761       switch (token->type)
4762         {
4763         case CPP_OPEN_SQUARE:
4764           postfix_expression
4765             = cp_parser_postfix_open_square_expression (parser,
4766                                                         postfix_expression,
4767                                                         false);
4768           idk = CP_ID_KIND_NONE;
4769           is_member_access = false;
4770           break;
4771
4772         case CPP_OPEN_PAREN:
4773           /* postfix-expression ( expression-list [opt] ) */
4774           {
4775             bool koenig_p;
4776             bool is_builtin_constant_p;
4777             bool saved_integral_constant_expression_p = false;
4778             bool saved_non_integral_constant_expression_p = false;
4779             VEC(tree,gc) *args;
4780
4781             is_member_access = false;
4782
4783             is_builtin_constant_p
4784               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4785             if (is_builtin_constant_p)
4786               {
4787                 /* The whole point of __builtin_constant_p is to allow
4788                    non-constant expressions to appear as arguments.  */
4789                 saved_integral_constant_expression_p
4790                   = parser->integral_constant_expression_p;
4791                 saved_non_integral_constant_expression_p
4792                   = parser->non_integral_constant_expression_p;
4793                 parser->integral_constant_expression_p = false;
4794               }
4795             args = (cp_parser_parenthesized_expression_list
4796                     (parser, /*is_attribute_list=*/false,
4797                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4798                      /*non_constant_p=*/NULL));
4799             if (is_builtin_constant_p)
4800               {
4801                 parser->integral_constant_expression_p
4802                   = saved_integral_constant_expression_p;
4803                 parser->non_integral_constant_expression_p
4804                   = saved_non_integral_constant_expression_p;
4805               }
4806
4807             if (args == NULL)
4808               {
4809                 postfix_expression = error_mark_node;
4810                 break;
4811               }
4812
4813             /* Function calls are not permitted in
4814                constant-expressions.  */
4815             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4816                 && cp_parser_non_integral_constant_expression (parser,
4817                                                                "a function call"))
4818               {
4819                 postfix_expression = error_mark_node;
4820                 release_tree_vector (args);
4821                 break;
4822               }
4823
4824             koenig_p = false;
4825             if (idk == CP_ID_KIND_UNQUALIFIED
4826                 || idk == CP_ID_KIND_TEMPLATE_ID)
4827               {
4828                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4829                   {
4830                     if (!VEC_empty (tree, args))
4831                       {
4832                         koenig_p = true;
4833                         if (!any_type_dependent_arguments_p (args))
4834                           postfix_expression
4835                             = perform_koenig_lookup (postfix_expression, args);
4836                       }
4837                     else
4838                       postfix_expression
4839                         = unqualified_fn_lookup_error (postfix_expression);
4840                   }
4841                 /* We do not perform argument-dependent lookup if
4842                    normal lookup finds a non-function, in accordance
4843                    with the expected resolution of DR 218.  */
4844                 else if (!VEC_empty (tree, args)
4845                          && is_overloaded_fn (postfix_expression))
4846                   {
4847                     tree fn = get_first_fn (postfix_expression);
4848                     fn = STRIP_TEMPLATE (fn);
4849
4850                     /* Do not do argument dependent lookup if regular
4851                        lookup finds a member function or a block-scope
4852                        function declaration.  [basic.lookup.argdep]/3  */
4853                     if (!DECL_FUNCTION_MEMBER_P (fn)
4854                         && !DECL_LOCAL_FUNCTION_P (fn))
4855                       {
4856                         koenig_p = true;
4857                         if (!any_type_dependent_arguments_p (args))
4858                           postfix_expression
4859                             = perform_koenig_lookup (postfix_expression, args);
4860                       }
4861                   }
4862               }
4863
4864             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4865               {
4866                 tree instance = TREE_OPERAND (postfix_expression, 0);
4867                 tree fn = TREE_OPERAND (postfix_expression, 1);
4868
4869                 if (processing_template_decl
4870                     && (type_dependent_expression_p (instance)
4871                         || (!BASELINK_P (fn)
4872                             && TREE_CODE (fn) != FIELD_DECL)
4873                         || type_dependent_expression_p (fn)
4874                         || any_type_dependent_arguments_p (args)))
4875                   {
4876                     postfix_expression
4877                       = build_nt_call_vec (postfix_expression, args);
4878                     release_tree_vector (args);
4879                     break;
4880                   }
4881
4882                 if (BASELINK_P (fn))
4883                   {
4884                   postfix_expression
4885                     = (build_new_method_call
4886                        (instance, fn, &args, NULL_TREE,
4887                         (idk == CP_ID_KIND_QUALIFIED
4888                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4889                         /*fn_p=*/NULL,
4890                         tf_warning_or_error));
4891                   }
4892                 else
4893                   postfix_expression
4894                     = finish_call_expr (postfix_expression, &args,
4895                                         /*disallow_virtual=*/false,
4896                                         /*koenig_p=*/false,
4897                                         tf_warning_or_error);
4898               }
4899             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4900                      || TREE_CODE (postfix_expression) == MEMBER_REF
4901                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4902               postfix_expression = (build_offset_ref_call_from_tree
4903                                     (postfix_expression, &args));
4904             else if (idk == CP_ID_KIND_QUALIFIED)
4905               /* A call to a static class member, or a namespace-scope
4906                  function.  */
4907               postfix_expression
4908                 = finish_call_expr (postfix_expression, &args,
4909                                     /*disallow_virtual=*/true,
4910                                     koenig_p,
4911                                     tf_warning_or_error);
4912             else
4913               /* All other function calls.  */
4914               postfix_expression
4915                 = finish_call_expr (postfix_expression, &args,
4916                                     /*disallow_virtual=*/false,
4917                                     koenig_p,
4918                                     tf_warning_or_error);
4919
4920             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4921             idk = CP_ID_KIND_NONE;
4922
4923             release_tree_vector (args);
4924           }
4925           break;
4926
4927         case CPP_DOT:
4928         case CPP_DEREF:
4929           /* postfix-expression . template [opt] id-expression
4930              postfix-expression . pseudo-destructor-name
4931              postfix-expression -> template [opt] id-expression
4932              postfix-expression -> pseudo-destructor-name */
4933
4934           /* Consume the `.' or `->' operator.  */
4935           cp_lexer_consume_token (parser->lexer);
4936
4937           postfix_expression
4938             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4939                                                       postfix_expression,
4940                                                       false, &idk,
4941                                                       token->location);
4942
4943           is_member_access = true;
4944           break;
4945
4946         case CPP_PLUS_PLUS:
4947           /* postfix-expression ++  */
4948           /* Consume the `++' token.  */
4949           cp_lexer_consume_token (parser->lexer);
4950           /* Generate a representation for the complete expression.  */
4951           postfix_expression
4952             = finish_increment_expr (postfix_expression,
4953                                      POSTINCREMENT_EXPR);
4954           /* Increments may not appear in constant-expressions.  */
4955           if (cp_parser_non_integral_constant_expression (parser,
4956                                                           "an increment"))
4957             postfix_expression = error_mark_node;
4958           idk = CP_ID_KIND_NONE;
4959           is_member_access = false;
4960           break;
4961
4962         case CPP_MINUS_MINUS:
4963           /* postfix-expression -- */
4964           /* Consume the `--' token.  */
4965           cp_lexer_consume_token (parser->lexer);
4966           /* Generate a representation for the complete expression.  */
4967           postfix_expression
4968             = finish_increment_expr (postfix_expression,
4969                                      POSTDECREMENT_EXPR);
4970           /* Decrements may not appear in constant-expressions.  */
4971           if (cp_parser_non_integral_constant_expression (parser,
4972                                                           "a decrement"))
4973             postfix_expression = error_mark_node;
4974           idk = CP_ID_KIND_NONE;
4975           is_member_access = false;
4976           break;
4977
4978         default:
4979           if (pidk_return != NULL)
4980             * pidk_return = idk;
4981           if (member_access_only_p)
4982             return is_member_access? postfix_expression : error_mark_node;
4983           else
4984             return postfix_expression;
4985         }
4986     }
4987
4988   /* We should never get here.  */
4989   gcc_unreachable ();
4990   return error_mark_node;
4991 }
4992
4993 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4994    by cp_parser_builtin_offsetof.  We're looking for
4995
4996      postfix-expression [ expression ]
4997
4998    FOR_OFFSETOF is set if we're being called in that context, which
4999    changes how we deal with integer constant expressions.  */
5000
5001 static tree
5002 cp_parser_postfix_open_square_expression (cp_parser *parser,
5003                                           tree postfix_expression,
5004                                           bool for_offsetof)
5005 {
5006   tree index;
5007
5008   /* Consume the `[' token.  */
5009   cp_lexer_consume_token (parser->lexer);
5010
5011   /* Parse the index expression.  */
5012   /* ??? For offsetof, there is a question of what to allow here.  If
5013      offsetof is not being used in an integral constant expression context,
5014      then we *could* get the right answer by computing the value at runtime.
5015      If we are in an integral constant expression context, then we might
5016      could accept any constant expression; hard to say without analysis.
5017      Rather than open the barn door too wide right away, allow only integer
5018      constant expressions here.  */
5019   if (for_offsetof)
5020     index = cp_parser_constant_expression (parser, false, NULL);
5021   else
5022     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5023
5024   /* Look for the closing `]'.  */
5025   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5026
5027   /* Build the ARRAY_REF.  */
5028   postfix_expression = grok_array_decl (postfix_expression, index);
5029
5030   /* When not doing offsetof, array references are not permitted in
5031      constant-expressions.  */
5032   if (!for_offsetof
5033       && (cp_parser_non_integral_constant_expression
5034           (parser, "an array reference")))
5035     postfix_expression = error_mark_node;
5036
5037   return postfix_expression;
5038 }
5039
5040 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5041    by cp_parser_builtin_offsetof.  We're looking for
5042
5043      postfix-expression . template [opt] id-expression
5044      postfix-expression . pseudo-destructor-name
5045      postfix-expression -> template [opt] id-expression
5046      postfix-expression -> pseudo-destructor-name
5047
5048    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5049    limits what of the above we'll actually accept, but nevermind.
5050    TOKEN_TYPE is the "." or "->" token, which will already have been
5051    removed from the stream.  */
5052
5053 static tree
5054 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5055                                         enum cpp_ttype token_type,
5056                                         tree postfix_expression,
5057                                         bool for_offsetof, cp_id_kind *idk,
5058                                         location_t location)
5059 {
5060   tree name;
5061   bool dependent_p;
5062   bool pseudo_destructor_p;
5063   tree scope = NULL_TREE;
5064
5065   /* If this is a `->' operator, dereference the pointer.  */
5066   if (token_type == CPP_DEREF)
5067     postfix_expression = build_x_arrow (postfix_expression);
5068   /* Check to see whether or not the expression is type-dependent.  */
5069   dependent_p = type_dependent_expression_p (postfix_expression);
5070   /* The identifier following the `->' or `.' is not qualified.  */
5071   parser->scope = NULL_TREE;
5072   parser->qualifying_scope = NULL_TREE;
5073   parser->object_scope = NULL_TREE;
5074   *idk = CP_ID_KIND_NONE;
5075
5076   /* Enter the scope corresponding to the type of the object
5077      given by the POSTFIX_EXPRESSION.  */
5078   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5079     {
5080       scope = TREE_TYPE (postfix_expression);
5081       /* According to the standard, no expression should ever have
5082          reference type.  Unfortunately, we do not currently match
5083          the standard in this respect in that our internal representation
5084          of an expression may have reference type even when the standard
5085          says it does not.  Therefore, we have to manually obtain the
5086          underlying type here.  */
5087       scope = non_reference (scope);
5088       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5089       if (scope == unknown_type_node)
5090         {
5091           error_at (location, "%qE does not have class type",
5092                     postfix_expression);
5093           scope = NULL_TREE;
5094         }
5095       else
5096         scope = complete_type_or_else (scope, NULL_TREE);
5097       /* Let the name lookup machinery know that we are processing a
5098          class member access expression.  */
5099       parser->context->object_type = scope;
5100       /* If something went wrong, we want to be able to discern that case,
5101          as opposed to the case where there was no SCOPE due to the type
5102          of expression being dependent.  */
5103       if (!scope)
5104         scope = error_mark_node;
5105       /* If the SCOPE was erroneous, make the various semantic analysis
5106          functions exit quickly -- and without issuing additional error
5107          messages.  */
5108       if (scope == error_mark_node)
5109         postfix_expression = error_mark_node;
5110     }
5111
5112   /* Assume this expression is not a pseudo-destructor access.  */
5113   pseudo_destructor_p = false;
5114
5115   /* If the SCOPE is a scalar type, then, if this is a valid program,
5116      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5117      is type dependent, it can be pseudo-destructor-name or something else.
5118      Try to parse it as pseudo-destructor-name first.  */
5119   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5120     {
5121       tree s;
5122       tree type;
5123
5124       cp_parser_parse_tentatively (parser);
5125       /* Parse the pseudo-destructor-name.  */
5126       s = NULL_TREE;
5127       cp_parser_pseudo_destructor_name (parser, &s, &type);
5128       if (dependent_p
5129           && (cp_parser_error_occurred (parser)
5130               || TREE_CODE (type) != TYPE_DECL
5131               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5132         cp_parser_abort_tentative_parse (parser);
5133       else if (cp_parser_parse_definitely (parser))
5134         {
5135           pseudo_destructor_p = true;
5136           postfix_expression
5137             = finish_pseudo_destructor_expr (postfix_expression,
5138                                              s, TREE_TYPE (type));
5139         }
5140     }
5141
5142   if (!pseudo_destructor_p)
5143     {
5144       /* If the SCOPE is not a scalar type, we are looking at an
5145          ordinary class member access expression, rather than a
5146          pseudo-destructor-name.  */
5147       bool template_p;
5148       cp_token *token = cp_lexer_peek_token (parser->lexer);
5149       /* Parse the id-expression.  */
5150       name = (cp_parser_id_expression
5151               (parser,
5152                cp_parser_optional_template_keyword (parser),
5153                /*check_dependency_p=*/true,
5154                &template_p,
5155                /*declarator_p=*/false,
5156                /*optional_p=*/false));
5157       /* In general, build a SCOPE_REF if the member name is qualified.
5158          However, if the name was not dependent and has already been
5159          resolved; there is no need to build the SCOPE_REF.  For example;
5160
5161              struct X { void f(); };
5162              template <typename T> void f(T* t) { t->X::f(); }
5163
5164          Even though "t" is dependent, "X::f" is not and has been resolved
5165          to a BASELINK; there is no need to include scope information.  */
5166
5167       /* But we do need to remember that there was an explicit scope for
5168          virtual function calls.  */
5169       if (parser->scope)
5170         *idk = CP_ID_KIND_QUALIFIED;
5171
5172       /* If the name is a template-id that names a type, we will get a
5173          TYPE_DECL here.  That is invalid code.  */
5174       if (TREE_CODE (name) == TYPE_DECL)
5175         {
5176           error_at (token->location, "invalid use of %qD", name);
5177           postfix_expression = error_mark_node;
5178         }
5179       else
5180         {
5181           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5182             {
5183               name = build_qualified_name (/*type=*/NULL_TREE,
5184                                            parser->scope,
5185                                            name,
5186                                            template_p);
5187               parser->scope = NULL_TREE;
5188               parser->qualifying_scope = NULL_TREE;
5189               parser->object_scope = NULL_TREE;
5190             }
5191           if (scope && name && BASELINK_P (name))
5192             adjust_result_of_qualified_name_lookup
5193               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5194           postfix_expression
5195             = finish_class_member_access_expr (postfix_expression, name,
5196                                                template_p, 
5197                                                tf_warning_or_error);
5198         }
5199     }
5200
5201   /* We no longer need to look up names in the scope of the object on
5202      the left-hand side of the `.' or `->' operator.  */
5203   parser->context->object_type = NULL_TREE;
5204
5205   /* Outside of offsetof, these operators may not appear in
5206      constant-expressions.  */
5207   if (!for_offsetof
5208       && (cp_parser_non_integral_constant_expression
5209           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5210     postfix_expression = error_mark_node;
5211
5212   return postfix_expression;
5213 }
5214
5215 /* Parse a parenthesized expression-list.
5216
5217    expression-list:
5218      assignment-expression
5219      expression-list, assignment-expression
5220
5221    attribute-list:
5222      expression-list
5223      identifier
5224      identifier, expression-list
5225
5226    CAST_P is true if this expression is the target of a cast.
5227
5228    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5229    argument pack.
5230
5231    Returns a vector of trees.  Each element is a representation of an
5232    assignment-expression.  NULL is returned if the ( and or ) are
5233    missing.  An empty, but allocated, vector is returned on no
5234    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is true
5235    if this is really an attribute list being parsed.  If
5236    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5237    not all of the expressions in the list were constant.  */
5238
5239 static VEC(tree,gc) *
5240 cp_parser_parenthesized_expression_list (cp_parser* parser,
5241                                          bool is_attribute_list,
5242                                          bool cast_p,
5243                                          bool allow_expansion_p,
5244                                          bool *non_constant_p)
5245 {
5246   VEC(tree,gc) *expression_list;
5247   bool fold_expr_p = is_attribute_list;
5248   tree identifier = NULL_TREE;
5249   bool saved_greater_than_is_operator_p;
5250
5251   /* Assume all the expressions will be constant.  */
5252   if (non_constant_p)
5253     *non_constant_p = false;
5254
5255   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5256     return NULL;
5257
5258   expression_list = make_tree_vector ();
5259
5260   /* Within a parenthesized expression, a `>' token is always
5261      the greater-than operator.  */
5262   saved_greater_than_is_operator_p
5263     = parser->greater_than_is_operator_p;
5264   parser->greater_than_is_operator_p = true;
5265
5266   /* Consume expressions until there are no more.  */
5267   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5268     while (true)
5269       {
5270         tree expr;
5271
5272         /* At the beginning of attribute lists, check to see if the
5273            next token is an identifier.  */
5274         if (is_attribute_list
5275             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5276           {
5277             cp_token *token;
5278
5279             /* Consume the identifier.  */
5280             token = cp_lexer_consume_token (parser->lexer);
5281             /* Save the identifier.  */
5282             identifier = token->u.value;
5283           }
5284         else
5285           {
5286             bool expr_non_constant_p;
5287
5288             /* Parse the next assignment-expression.  */
5289             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5290               {
5291                 /* A braced-init-list.  */
5292                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5293                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5294                 if (non_constant_p && expr_non_constant_p)
5295                   *non_constant_p = true;
5296               }
5297             else if (non_constant_p)
5298               {
5299                 expr = (cp_parser_constant_expression
5300                         (parser, /*allow_non_constant_p=*/true,
5301                          &expr_non_constant_p));
5302                 if (expr_non_constant_p)
5303                   *non_constant_p = true;
5304               }
5305             else
5306               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5307
5308             if (fold_expr_p)
5309               expr = fold_non_dependent_expr (expr);
5310
5311             /* If we have an ellipsis, then this is an expression
5312                expansion.  */
5313             if (allow_expansion_p
5314                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5315               {
5316                 /* Consume the `...'.  */
5317                 cp_lexer_consume_token (parser->lexer);
5318
5319                 /* Build the argument pack.  */
5320                 expr = make_pack_expansion (expr);
5321               }
5322
5323              /* Add it to the list.  We add error_mark_node
5324                 expressions to the list, so that we can still tell if
5325                 the correct form for a parenthesized expression-list
5326                 is found. That gives better errors.  */
5327             VEC_safe_push (tree, gc, expression_list, expr);
5328
5329             if (expr == error_mark_node)
5330               goto skip_comma;
5331           }
5332
5333         /* After the first item, attribute lists look the same as
5334            expression lists.  */
5335         is_attribute_list = false;
5336
5337       get_comma:;
5338         /* If the next token isn't a `,', then we are done.  */
5339         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5340           break;
5341
5342         /* Otherwise, consume the `,' and keep going.  */
5343         cp_lexer_consume_token (parser->lexer);
5344       }
5345
5346   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5347     {
5348       int ending;
5349
5350     skip_comma:;
5351       /* We try and resync to an unnested comma, as that will give the
5352          user better diagnostics.  */
5353       ending = cp_parser_skip_to_closing_parenthesis (parser,
5354                                                       /*recovering=*/true,
5355                                                       /*or_comma=*/true,
5356                                                       /*consume_paren=*/true);
5357       if (ending < 0)
5358         goto get_comma;
5359       if (!ending)
5360         {
5361           parser->greater_than_is_operator_p
5362             = saved_greater_than_is_operator_p;
5363           return NULL;
5364         }
5365     }
5366
5367   parser->greater_than_is_operator_p
5368     = saved_greater_than_is_operator_p;
5369
5370   if (identifier)
5371     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5372
5373   return expression_list;
5374 }
5375
5376 /* Parse a pseudo-destructor-name.
5377
5378    pseudo-destructor-name:
5379      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5380      :: [opt] nested-name-specifier template template-id :: ~ type-name
5381      :: [opt] nested-name-specifier [opt] ~ type-name
5382
5383    If either of the first two productions is used, sets *SCOPE to the
5384    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5385    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5386    or ERROR_MARK_NODE if the parse fails.  */
5387
5388 static void
5389 cp_parser_pseudo_destructor_name (cp_parser* parser,
5390                                   tree* scope,
5391                                   tree* type)
5392 {
5393   bool nested_name_specifier_p;
5394
5395   /* Assume that things will not work out.  */
5396   *type = error_mark_node;
5397
5398   /* Look for the optional `::' operator.  */
5399   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5400   /* Look for the optional nested-name-specifier.  */
5401   nested_name_specifier_p
5402     = (cp_parser_nested_name_specifier_opt (parser,
5403                                             /*typename_keyword_p=*/false,
5404                                             /*check_dependency_p=*/true,
5405                                             /*type_p=*/false,
5406                                             /*is_declaration=*/false)
5407        != NULL_TREE);
5408   /* Now, if we saw a nested-name-specifier, we might be doing the
5409      second production.  */
5410   if (nested_name_specifier_p
5411       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5412     {
5413       /* Consume the `template' keyword.  */
5414       cp_lexer_consume_token (parser->lexer);
5415       /* Parse the template-id.  */
5416       cp_parser_template_id (parser,
5417                              /*template_keyword_p=*/true,
5418                              /*check_dependency_p=*/false,
5419                              /*is_declaration=*/true);
5420       /* Look for the `::' token.  */
5421       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5422     }
5423   /* If the next token is not a `~', then there might be some
5424      additional qualification.  */
5425   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5426     {
5427       /* At this point, we're looking for "type-name :: ~".  The type-name
5428          must not be a class-name, since this is a pseudo-destructor.  So,
5429          it must be either an enum-name, or a typedef-name -- both of which
5430          are just identifiers.  So, we peek ahead to check that the "::"
5431          and "~" tokens are present; if they are not, then we can avoid
5432          calling type_name.  */
5433       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5434           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5435           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5436         {
5437           cp_parser_error (parser, "non-scalar type");
5438           return;
5439         }
5440
5441       /* Look for the type-name.  */
5442       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5443       if (*scope == error_mark_node)
5444         return;
5445
5446       /* Look for the `::' token.  */
5447       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5448     }
5449   else
5450     *scope = NULL_TREE;
5451
5452   /* Look for the `~'.  */
5453   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5454   /* Look for the type-name again.  We are not responsible for
5455      checking that it matches the first type-name.  */
5456   *type = cp_parser_nonclass_name (parser);
5457 }
5458
5459 /* Parse a unary-expression.
5460
5461    unary-expression:
5462      postfix-expression
5463      ++ cast-expression
5464      -- cast-expression
5465      unary-operator cast-expression
5466      sizeof unary-expression
5467      sizeof ( type-id )
5468      new-expression
5469      delete-expression
5470
5471    GNU Extensions:
5472
5473    unary-expression:
5474      __extension__ cast-expression
5475      __alignof__ unary-expression
5476      __alignof__ ( type-id )
5477      __real__ cast-expression
5478      __imag__ cast-expression
5479      && identifier
5480
5481    ADDRESS_P is true iff the unary-expression is appearing as the
5482    operand of the `&' operator.   CAST_P is true if this expression is
5483    the target of a cast.
5484
5485    Returns a representation of the expression.  */
5486
5487 static tree
5488 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5489                             cp_id_kind * pidk)
5490 {
5491   cp_token *token;
5492   enum tree_code unary_operator;
5493
5494   /* Peek at the next token.  */
5495   token = cp_lexer_peek_token (parser->lexer);
5496   /* Some keywords give away the kind of expression.  */
5497   if (token->type == CPP_KEYWORD)
5498     {
5499       enum rid keyword = token->keyword;
5500
5501       switch (keyword)
5502         {
5503         case RID_ALIGNOF:
5504         case RID_SIZEOF:
5505           {
5506             tree operand;
5507             enum tree_code op;
5508
5509             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5510             /* Consume the token.  */
5511             cp_lexer_consume_token (parser->lexer);
5512             /* Parse the operand.  */
5513             operand = cp_parser_sizeof_operand (parser, keyword);
5514
5515             if (TYPE_P (operand))
5516               return cxx_sizeof_or_alignof_type (operand, op, true);
5517             else
5518               return cxx_sizeof_or_alignof_expr (operand, op, true);
5519           }
5520
5521         case RID_NEW:
5522           return cp_parser_new_expression (parser);
5523
5524         case RID_DELETE:
5525           return cp_parser_delete_expression (parser);
5526
5527         case RID_EXTENSION:
5528           {
5529             /* The saved value of the PEDANTIC flag.  */
5530             int saved_pedantic;
5531             tree expr;
5532
5533             /* Save away the PEDANTIC flag.  */
5534             cp_parser_extension_opt (parser, &saved_pedantic);
5535             /* Parse the cast-expression.  */
5536             expr = cp_parser_simple_cast_expression (parser);
5537             /* Restore the PEDANTIC flag.  */
5538             pedantic = saved_pedantic;
5539
5540             return expr;
5541           }
5542
5543         case RID_REALPART:
5544         case RID_IMAGPART:
5545           {
5546             tree expression;
5547
5548             /* Consume the `__real__' or `__imag__' token.  */
5549             cp_lexer_consume_token (parser->lexer);
5550             /* Parse the cast-expression.  */
5551             expression = cp_parser_simple_cast_expression (parser);
5552             /* Create the complete representation.  */
5553             return build_x_unary_op ((keyword == RID_REALPART
5554                                       ? REALPART_EXPR : IMAGPART_EXPR),
5555                                      expression,
5556                                      tf_warning_or_error);
5557           }
5558           break;
5559
5560         default:
5561           break;
5562         }
5563     }
5564
5565   /* Look for the `:: new' and `:: delete', which also signal the
5566      beginning of a new-expression, or delete-expression,
5567      respectively.  If the next token is `::', then it might be one of
5568      these.  */
5569   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5570     {
5571       enum rid keyword;
5572
5573       /* See if the token after the `::' is one of the keywords in
5574          which we're interested.  */
5575       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5576       /* If it's `new', we have a new-expression.  */
5577       if (keyword == RID_NEW)
5578         return cp_parser_new_expression (parser);
5579       /* Similarly, for `delete'.  */
5580       else if (keyword == RID_DELETE)
5581         return cp_parser_delete_expression (parser);
5582     }
5583
5584   /* Look for a unary operator.  */
5585   unary_operator = cp_parser_unary_operator (token);
5586   /* The `++' and `--' operators can be handled similarly, even though
5587      they are not technically unary-operators in the grammar.  */
5588   if (unary_operator == ERROR_MARK)
5589     {
5590       if (token->type == CPP_PLUS_PLUS)
5591         unary_operator = PREINCREMENT_EXPR;
5592       else if (token->type == CPP_MINUS_MINUS)
5593         unary_operator = PREDECREMENT_EXPR;
5594       /* Handle the GNU address-of-label extension.  */
5595       else if (cp_parser_allow_gnu_extensions_p (parser)
5596                && token->type == CPP_AND_AND)
5597         {
5598           tree identifier;
5599           tree expression;
5600           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5601
5602           /* Consume the '&&' token.  */
5603           cp_lexer_consume_token (parser->lexer);
5604           /* Look for the identifier.  */
5605           identifier = cp_parser_identifier (parser);
5606           /* Create an expression representing the address.  */
5607           expression = finish_label_address_expr (identifier, loc);
5608           if (cp_parser_non_integral_constant_expression (parser,
5609                                                 "the address of a label"))
5610             expression = error_mark_node;
5611           return expression;
5612         }
5613     }
5614   if (unary_operator != ERROR_MARK)
5615     {
5616       tree cast_expression;
5617       tree expression = error_mark_node;
5618       const char *non_constant_p = NULL;
5619
5620       /* Consume the operator token.  */
5621       token = cp_lexer_consume_token (parser->lexer);
5622       /* Parse the cast-expression.  */
5623       cast_expression
5624         = cp_parser_cast_expression (parser,
5625                                      unary_operator == ADDR_EXPR,
5626                                      /*cast_p=*/false, pidk);
5627       /* Now, build an appropriate representation.  */
5628       switch (unary_operator)
5629         {
5630         case INDIRECT_REF:
5631           non_constant_p = "%<*%>";
5632           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5633                                              tf_warning_or_error);
5634           break;
5635
5636         case ADDR_EXPR:
5637           non_constant_p = "%<&%>";
5638           /* Fall through.  */
5639         case BIT_NOT_EXPR:
5640           expression = build_x_unary_op (unary_operator, cast_expression,
5641                                          tf_warning_or_error);
5642           break;
5643
5644         case PREINCREMENT_EXPR:
5645         case PREDECREMENT_EXPR:
5646           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5647                             ? "%<++%>" : "%<--%>");
5648           /* Fall through.  */
5649         case UNARY_PLUS_EXPR:
5650         case NEGATE_EXPR:
5651         case TRUTH_NOT_EXPR:
5652           expression = finish_unary_op_expr (unary_operator, cast_expression);
5653           break;
5654
5655         default:
5656           gcc_unreachable ();
5657         }
5658
5659       if (non_constant_p
5660           && cp_parser_non_integral_constant_expression (parser,
5661                                                          non_constant_p))
5662         expression = error_mark_node;
5663
5664       return expression;
5665     }
5666
5667   return cp_parser_postfix_expression (parser, address_p, cast_p,
5668                                        /*member_access_only_p=*/false,
5669                                        pidk);
5670 }
5671
5672 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5673    unary-operator, the corresponding tree code is returned.  */
5674
5675 static enum tree_code
5676 cp_parser_unary_operator (cp_token* token)
5677 {
5678   switch (token->type)
5679     {
5680     case CPP_MULT:
5681       return INDIRECT_REF;
5682
5683     case CPP_AND:
5684       return ADDR_EXPR;
5685
5686     case CPP_PLUS:
5687       return UNARY_PLUS_EXPR;
5688
5689     case CPP_MINUS:
5690       return NEGATE_EXPR;
5691
5692     case CPP_NOT:
5693       return TRUTH_NOT_EXPR;
5694
5695     case CPP_COMPL:
5696       return BIT_NOT_EXPR;
5697
5698     default:
5699       return ERROR_MARK;
5700     }
5701 }
5702
5703 /* Parse a new-expression.
5704
5705    new-expression:
5706      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5707      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5708
5709    Returns a representation of the expression.  */
5710
5711 static tree
5712 cp_parser_new_expression (cp_parser* parser)
5713 {
5714   bool global_scope_p;
5715   VEC(tree,gc) *placement;
5716   tree type;
5717   VEC(tree,gc) *initializer;
5718   tree nelts;
5719   tree ret;
5720
5721   /* Look for the optional `::' operator.  */
5722   global_scope_p
5723     = (cp_parser_global_scope_opt (parser,
5724                                    /*current_scope_valid_p=*/false)
5725        != NULL_TREE);
5726   /* Look for the `new' operator.  */
5727   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5728   /* There's no easy way to tell a new-placement from the
5729      `( type-id )' construct.  */
5730   cp_parser_parse_tentatively (parser);
5731   /* Look for a new-placement.  */
5732   placement = cp_parser_new_placement (parser);
5733   /* If that didn't work out, there's no new-placement.  */
5734   if (!cp_parser_parse_definitely (parser))
5735     {
5736       if (placement != NULL)
5737         release_tree_vector (placement);
5738       placement = NULL;
5739     }
5740
5741   /* If the next token is a `(', then we have a parenthesized
5742      type-id.  */
5743   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5744     {
5745       cp_token *token;
5746       /* Consume the `('.  */
5747       cp_lexer_consume_token (parser->lexer);
5748       /* Parse the type-id.  */
5749       type = cp_parser_type_id (parser);
5750       /* Look for the closing `)'.  */
5751       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5752       token = cp_lexer_peek_token (parser->lexer);
5753       /* There should not be a direct-new-declarator in this production,
5754          but GCC used to allowed this, so we check and emit a sensible error
5755          message for this case.  */
5756       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5757         {
5758           error_at (token->location,
5759                     "array bound forbidden after parenthesized type-id");
5760           inform (token->location, 
5761                   "try removing the parentheses around the type-id");
5762           cp_parser_direct_new_declarator (parser);
5763         }
5764       nelts = NULL_TREE;
5765     }
5766   /* Otherwise, there must be a new-type-id.  */
5767   else
5768     type = cp_parser_new_type_id (parser, &nelts);
5769
5770   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5771   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5772       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5773     initializer = cp_parser_new_initializer (parser);
5774   else
5775     initializer = NULL;
5776
5777   /* A new-expression may not appear in an integral constant
5778      expression.  */
5779   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5780     ret = error_mark_node;
5781   else
5782     {
5783       /* Create a representation of the new-expression.  */
5784       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5785                        tf_warning_or_error);
5786     }
5787
5788   if (placement != NULL)
5789     release_tree_vector (placement);
5790   if (initializer != NULL)
5791     release_tree_vector (initializer);
5792
5793   return ret;
5794 }
5795
5796 /* Parse a new-placement.
5797
5798    new-placement:
5799      ( expression-list )
5800
5801    Returns the same representation as for an expression-list.  */
5802
5803 static VEC(tree,gc) *
5804 cp_parser_new_placement (cp_parser* parser)
5805 {
5806   VEC(tree,gc) *expression_list;
5807
5808   /* Parse the expression-list.  */
5809   expression_list = (cp_parser_parenthesized_expression_list
5810                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5811                       /*non_constant_p=*/NULL));
5812
5813   return expression_list;
5814 }
5815
5816 /* Parse a new-type-id.
5817
5818    new-type-id:
5819      type-specifier-seq new-declarator [opt]
5820
5821    Returns the TYPE allocated.  If the new-type-id indicates an array
5822    type, *NELTS is set to the number of elements in the last array
5823    bound; the TYPE will not include the last array bound.  */
5824
5825 static tree
5826 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5827 {
5828   cp_decl_specifier_seq type_specifier_seq;
5829   cp_declarator *new_declarator;
5830   cp_declarator *declarator;
5831   cp_declarator *outer_declarator;
5832   const char *saved_message;
5833   tree type;
5834
5835   /* The type-specifier sequence must not contain type definitions.
5836      (It cannot contain declarations of new types either, but if they
5837      are not definitions we will catch that because they are not
5838      complete.)  */
5839   saved_message = parser->type_definition_forbidden_message;
5840   parser->type_definition_forbidden_message
5841     = G_("types may not be defined in a new-type-id");
5842   /* Parse the type-specifier-seq.  */
5843   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5844                                 /*is_trailing_return=*/false,
5845                                 &type_specifier_seq);
5846   /* Restore the old message.  */
5847   parser->type_definition_forbidden_message = saved_message;
5848   /* Parse the new-declarator.  */
5849   new_declarator = cp_parser_new_declarator_opt (parser);
5850
5851   /* Determine the number of elements in the last array dimension, if
5852      any.  */
5853   *nelts = NULL_TREE;
5854   /* Skip down to the last array dimension.  */
5855   declarator = new_declarator;
5856   outer_declarator = NULL;
5857   while (declarator && (declarator->kind == cdk_pointer
5858                         || declarator->kind == cdk_ptrmem))
5859     {
5860       outer_declarator = declarator;
5861       declarator = declarator->declarator;
5862     }
5863   while (declarator
5864          && declarator->kind == cdk_array
5865          && declarator->declarator
5866          && declarator->declarator->kind == cdk_array)
5867     {
5868       outer_declarator = declarator;
5869       declarator = declarator->declarator;
5870     }
5871
5872   if (declarator && declarator->kind == cdk_array)
5873     {
5874       *nelts = declarator->u.array.bounds;
5875       if (*nelts == error_mark_node)
5876         *nelts = integer_one_node;
5877
5878       if (outer_declarator)
5879         outer_declarator->declarator = declarator->declarator;
5880       else
5881         new_declarator = NULL;
5882     }
5883
5884   type = groktypename (&type_specifier_seq, new_declarator, false);
5885   return type;
5886 }
5887
5888 /* Parse an (optional) new-declarator.
5889
5890    new-declarator:
5891      ptr-operator new-declarator [opt]
5892      direct-new-declarator
5893
5894    Returns the declarator.  */
5895
5896 static cp_declarator *
5897 cp_parser_new_declarator_opt (cp_parser* parser)
5898 {
5899   enum tree_code code;
5900   tree type;
5901   cp_cv_quals cv_quals;
5902
5903   /* We don't know if there's a ptr-operator next, or not.  */
5904   cp_parser_parse_tentatively (parser);
5905   /* Look for a ptr-operator.  */
5906   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5907   /* If that worked, look for more new-declarators.  */
5908   if (cp_parser_parse_definitely (parser))
5909     {
5910       cp_declarator *declarator;
5911
5912       /* Parse another optional declarator.  */
5913       declarator = cp_parser_new_declarator_opt (parser);
5914
5915       return cp_parser_make_indirect_declarator
5916         (code, type, cv_quals, declarator);
5917     }
5918
5919   /* If the next token is a `[', there is a direct-new-declarator.  */
5920   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5921     return cp_parser_direct_new_declarator (parser);
5922
5923   return NULL;
5924 }
5925
5926 /* Parse a direct-new-declarator.
5927
5928    direct-new-declarator:
5929      [ expression ]
5930      direct-new-declarator [constant-expression]
5931
5932    */
5933
5934 static cp_declarator *
5935 cp_parser_direct_new_declarator (cp_parser* parser)
5936 {
5937   cp_declarator *declarator = NULL;
5938
5939   while (true)
5940     {
5941       tree expression;
5942
5943       /* Look for the opening `['.  */
5944       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5945       /* The first expression is not required to be constant.  */
5946       if (!declarator)
5947         {
5948           cp_token *token = cp_lexer_peek_token (parser->lexer);
5949           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5950           /* The standard requires that the expression have integral
5951              type.  DR 74 adds enumeration types.  We believe that the
5952              real intent is that these expressions be handled like the
5953              expression in a `switch' condition, which also allows
5954              classes with a single conversion to integral or
5955              enumeration type.  */
5956           if (!processing_template_decl)
5957             {
5958               expression
5959                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5960                                               expression,
5961                                               /*complain=*/true);
5962               if (!expression)
5963                 {
5964                   error_at (token->location,
5965                             "expression in new-declarator must have integral "
5966                             "or enumeration type");
5967                   expression = error_mark_node;
5968                 }
5969             }
5970         }
5971       /* But all the other expressions must be.  */
5972       else
5973         expression
5974           = cp_parser_constant_expression (parser,
5975                                            /*allow_non_constant=*/false,
5976                                            NULL);
5977       /* Look for the closing `]'.  */
5978       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5979
5980       /* Add this bound to the declarator.  */
5981       declarator = make_array_declarator (declarator, expression);
5982
5983       /* If the next token is not a `[', then there are no more
5984          bounds.  */
5985       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5986         break;
5987     }
5988
5989   return declarator;
5990 }
5991
5992 /* Parse a new-initializer.
5993
5994    new-initializer:
5995      ( expression-list [opt] )
5996      braced-init-list
5997
5998    Returns a representation of the expression-list.  */
5999
6000 static VEC(tree,gc) *
6001 cp_parser_new_initializer (cp_parser* parser)
6002 {
6003   VEC(tree,gc) *expression_list;
6004
6005   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6006     {
6007       tree t;
6008       bool expr_non_constant_p;
6009       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6010       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6011       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6012       expression_list = make_tree_vector_single (t);
6013     }
6014   else
6015     expression_list = (cp_parser_parenthesized_expression_list
6016                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
6017                         /*non_constant_p=*/NULL));
6018
6019   return expression_list;
6020 }
6021
6022 /* Parse a delete-expression.
6023
6024    delete-expression:
6025      :: [opt] delete cast-expression
6026      :: [opt] delete [ ] cast-expression
6027
6028    Returns a representation of the expression.  */
6029
6030 static tree
6031 cp_parser_delete_expression (cp_parser* parser)
6032 {
6033   bool global_scope_p;
6034   bool array_p;
6035   tree expression;
6036
6037   /* Look for the optional `::' operator.  */
6038   global_scope_p
6039     = (cp_parser_global_scope_opt (parser,
6040                                    /*current_scope_valid_p=*/false)
6041        != NULL_TREE);
6042   /* Look for the `delete' keyword.  */
6043   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6044   /* See if the array syntax is in use.  */
6045   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6046     {
6047       /* Consume the `[' token.  */
6048       cp_lexer_consume_token (parser->lexer);
6049       /* Look for the `]' token.  */
6050       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6051       /* Remember that this is the `[]' construct.  */
6052       array_p = true;
6053     }
6054   else
6055     array_p = false;
6056
6057   /* Parse the cast-expression.  */
6058   expression = cp_parser_simple_cast_expression (parser);
6059
6060   /* A delete-expression may not appear in an integral constant
6061      expression.  */
6062   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6063     return error_mark_node;
6064
6065   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6066 }
6067
6068 /* Returns true if TOKEN may start a cast-expression and false
6069    otherwise.  */
6070
6071 static bool
6072 cp_parser_token_starts_cast_expression (cp_token *token)
6073 {
6074   switch (token->type)
6075     {
6076     case CPP_COMMA:
6077     case CPP_SEMICOLON:
6078     case CPP_QUERY:
6079     case CPP_COLON:
6080     case CPP_CLOSE_SQUARE:
6081     case CPP_CLOSE_PAREN:
6082     case CPP_CLOSE_BRACE:
6083     case CPP_DOT:
6084     case CPP_DOT_STAR:
6085     case CPP_DEREF:
6086     case CPP_DEREF_STAR:
6087     case CPP_DIV:
6088     case CPP_MOD:
6089     case CPP_LSHIFT:
6090     case CPP_RSHIFT:
6091     case CPP_LESS:
6092     case CPP_GREATER:
6093     case CPP_LESS_EQ:
6094     case CPP_GREATER_EQ:
6095     case CPP_EQ_EQ:
6096     case CPP_NOT_EQ:
6097     case CPP_EQ:
6098     case CPP_MULT_EQ:
6099     case CPP_DIV_EQ:
6100     case CPP_MOD_EQ:
6101     case CPP_PLUS_EQ:
6102     case CPP_MINUS_EQ:
6103     case CPP_RSHIFT_EQ:
6104     case CPP_LSHIFT_EQ:
6105     case CPP_AND_EQ:
6106     case CPP_XOR_EQ:
6107     case CPP_OR_EQ:
6108     case CPP_XOR:
6109     case CPP_OR:
6110     case CPP_OR_OR:
6111     case CPP_EOF:
6112       return false;
6113
6114       /* '[' may start a primary-expression in obj-c++.  */
6115     case CPP_OPEN_SQUARE:
6116       return c_dialect_objc ();
6117
6118     default:
6119       return true;
6120     }
6121 }
6122
6123 /* Parse a cast-expression.
6124
6125    cast-expression:
6126      unary-expression
6127      ( type-id ) cast-expression
6128
6129    ADDRESS_P is true iff the unary-expression is appearing as the
6130    operand of the `&' operator.   CAST_P is true if this expression is
6131    the target of a cast.
6132
6133    Returns a representation of the expression.  */
6134
6135 static tree
6136 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6137                            cp_id_kind * pidk)
6138 {
6139   /* If it's a `(', then we might be looking at a cast.  */
6140   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6141     {
6142       tree type = NULL_TREE;
6143       tree expr = NULL_TREE;
6144       bool compound_literal_p;
6145       const char *saved_message;
6146
6147       /* There's no way to know yet whether or not this is a cast.
6148          For example, `(int (3))' is a unary-expression, while `(int)
6149          3' is a cast.  So, we resort to parsing tentatively.  */
6150       cp_parser_parse_tentatively (parser);
6151       /* Types may not be defined in a cast.  */
6152       saved_message = parser->type_definition_forbidden_message;
6153       parser->type_definition_forbidden_message
6154         = G_("types may not be defined in casts");
6155       /* Consume the `('.  */
6156       cp_lexer_consume_token (parser->lexer);
6157       /* A very tricky bit is that `(struct S) { 3 }' is a
6158          compound-literal (which we permit in C++ as an extension).
6159          But, that construct is not a cast-expression -- it is a
6160          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6161          is legal; if the compound-literal were a cast-expression,
6162          you'd need an extra set of parentheses.)  But, if we parse
6163          the type-id, and it happens to be a class-specifier, then we
6164          will commit to the parse at that point, because we cannot
6165          undo the action that is done when creating a new class.  So,
6166          then we cannot back up and do a postfix-expression.
6167
6168          Therefore, we scan ahead to the closing `)', and check to see
6169          if the token after the `)' is a `{'.  If so, we are not
6170          looking at a cast-expression.
6171
6172          Save tokens so that we can put them back.  */
6173       cp_lexer_save_tokens (parser->lexer);
6174       /* Skip tokens until the next token is a closing parenthesis.
6175          If we find the closing `)', and the next token is a `{', then
6176          we are looking at a compound-literal.  */
6177       compound_literal_p
6178         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6179                                                   /*consume_paren=*/true)
6180            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6181       /* Roll back the tokens we skipped.  */
6182       cp_lexer_rollback_tokens (parser->lexer);
6183       /* If we were looking at a compound-literal, simulate an error
6184          so that the call to cp_parser_parse_definitely below will
6185          fail.  */
6186       if (compound_literal_p)
6187         cp_parser_simulate_error (parser);
6188       else
6189         {
6190           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6191           parser->in_type_id_in_expr_p = true;
6192           /* Look for the type-id.  */
6193           type = cp_parser_type_id (parser);
6194           /* Look for the closing `)'.  */
6195           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6196           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6197         }
6198
6199       /* Restore the saved message.  */
6200       parser->type_definition_forbidden_message = saved_message;
6201
6202       /* At this point this can only be either a cast or a
6203          parenthesized ctor such as `(T ())' that looks like a cast to
6204          function returning T.  */
6205       if (!cp_parser_error_occurred (parser)
6206           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6207                                                      (parser->lexer)))
6208         {
6209           cp_parser_parse_definitely (parser);
6210           expr = cp_parser_cast_expression (parser,
6211                                             /*address_p=*/false,
6212                                             /*cast_p=*/true, pidk);
6213
6214           /* Warn about old-style casts, if so requested.  */
6215           if (warn_old_style_cast
6216               && !in_system_header
6217               && !VOID_TYPE_P (type)
6218               && current_lang_name != lang_name_c)
6219             warning (OPT_Wold_style_cast, "use of old-style cast");
6220
6221           /* Only type conversions to integral or enumeration types
6222              can be used in constant-expressions.  */
6223           if (!cast_valid_in_integral_constant_expression_p (type)
6224               && (cp_parser_non_integral_constant_expression
6225                   (parser,
6226                    "a cast to a type other than an integral or "
6227                    "enumeration type")))
6228             return error_mark_node;
6229
6230           /* Perform the cast.  */
6231           expr = build_c_cast (input_location, type, expr);
6232           return expr;
6233         }
6234       else 
6235         cp_parser_abort_tentative_parse (parser);
6236     }
6237
6238   /* If we get here, then it's not a cast, so it must be a
6239      unary-expression.  */
6240   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6241 }
6242
6243 /* Parse a binary expression of the general form:
6244
6245    pm-expression:
6246      cast-expression
6247      pm-expression .* cast-expression
6248      pm-expression ->* cast-expression
6249
6250    multiplicative-expression:
6251      pm-expression
6252      multiplicative-expression * pm-expression
6253      multiplicative-expression / pm-expression
6254      multiplicative-expression % pm-expression
6255
6256    additive-expression:
6257      multiplicative-expression
6258      additive-expression + multiplicative-expression
6259      additive-expression - multiplicative-expression
6260
6261    shift-expression:
6262      additive-expression
6263      shift-expression << additive-expression
6264      shift-expression >> additive-expression
6265
6266    relational-expression:
6267      shift-expression
6268      relational-expression < shift-expression
6269      relational-expression > shift-expression
6270      relational-expression <= shift-expression
6271      relational-expression >= shift-expression
6272
6273   GNU Extension:
6274
6275    relational-expression:
6276      relational-expression <? shift-expression
6277      relational-expression >? shift-expression
6278
6279    equality-expression:
6280      relational-expression
6281      equality-expression == relational-expression
6282      equality-expression != relational-expression
6283
6284    and-expression:
6285      equality-expression
6286      and-expression & equality-expression
6287
6288    exclusive-or-expression:
6289      and-expression
6290      exclusive-or-expression ^ and-expression
6291
6292    inclusive-or-expression:
6293      exclusive-or-expression
6294      inclusive-or-expression | exclusive-or-expression
6295
6296    logical-and-expression:
6297      inclusive-or-expression
6298      logical-and-expression && inclusive-or-expression
6299
6300    logical-or-expression:
6301      logical-and-expression
6302      logical-or-expression || logical-and-expression
6303
6304    All these are implemented with a single function like:
6305
6306    binary-expression:
6307      simple-cast-expression
6308      binary-expression <token> binary-expression
6309
6310    CAST_P is true if this expression is the target of a cast.
6311
6312    The binops_by_token map is used to get the tree codes for each <token> type.
6313    binary-expressions are associated according to a precedence table.  */
6314
6315 #define TOKEN_PRECEDENCE(token)                              \
6316 (((token->type == CPP_GREATER                                \
6317    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6318   && !parser->greater_than_is_operator_p)                    \
6319  ? PREC_NOT_OPERATOR                                         \
6320  : binops_by_token[token->type].prec)
6321
6322 static tree
6323 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6324                              bool no_toplevel_fold_p,
6325                              enum cp_parser_prec prec,
6326                              cp_id_kind * pidk)
6327 {
6328   cp_parser_expression_stack stack;
6329   cp_parser_expression_stack_entry *sp = &stack[0];
6330   tree lhs, rhs;
6331   cp_token *token;
6332   enum tree_code tree_type, lhs_type, rhs_type;
6333   enum cp_parser_prec new_prec, lookahead_prec;
6334   bool overloaded_p;
6335
6336   /* Parse the first expression.  */
6337   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6338   lhs_type = ERROR_MARK;
6339
6340   for (;;)
6341     {
6342       /* Get an operator token.  */
6343       token = cp_lexer_peek_token (parser->lexer);
6344
6345       if (warn_cxx0x_compat
6346           && token->type == CPP_RSHIFT
6347           && !parser->greater_than_is_operator_p)
6348         {
6349           if (warning_at (token->location, OPT_Wc__0x_compat, 
6350                           "%<>>%> operator will be treated as"
6351                           " two right angle brackets in C++0x"))
6352             inform (token->location,
6353                     "suggest parentheses around %<>>%> expression");
6354         }
6355
6356       new_prec = TOKEN_PRECEDENCE (token);
6357
6358       /* Popping an entry off the stack means we completed a subexpression:
6359          - either we found a token which is not an operator (`>' where it is not
6360            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6361            will happen repeatedly;
6362          - or, we found an operator which has lower priority.  This is the case
6363            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6364            parsing `3 * 4'.  */
6365       if (new_prec <= prec)
6366         {
6367           if (sp == stack)
6368             break;
6369           else
6370             goto pop;
6371         }
6372
6373      get_rhs:
6374       tree_type = binops_by_token[token->type].tree_type;
6375
6376       /* We used the operator token.  */
6377       cp_lexer_consume_token (parser->lexer);
6378
6379       /* For "false && x" or "true || x", x will never be executed;
6380          disable warnings while evaluating it.  */
6381       if (tree_type == TRUTH_ANDIF_EXPR)
6382         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6383       else if (tree_type == TRUTH_ORIF_EXPR)
6384         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6385
6386       /* Extract another operand.  It may be the RHS of this expression
6387          or the LHS of a new, higher priority expression.  */
6388       rhs = cp_parser_simple_cast_expression (parser);
6389       rhs_type = ERROR_MARK;
6390
6391       /* Get another operator token.  Look up its precedence to avoid
6392          building a useless (immediately popped) stack entry for common
6393          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6394       token = cp_lexer_peek_token (parser->lexer);
6395       lookahead_prec = TOKEN_PRECEDENCE (token);
6396       if (lookahead_prec > new_prec)
6397         {
6398           /* ... and prepare to parse the RHS of the new, higher priority
6399              expression.  Since precedence levels on the stack are
6400              monotonically increasing, we do not have to care about
6401              stack overflows.  */
6402           sp->prec = prec;
6403           sp->tree_type = tree_type;
6404           sp->lhs = lhs;
6405           sp->lhs_type = lhs_type;
6406           sp++;
6407           lhs = rhs;
6408           lhs_type = rhs_type;
6409           prec = new_prec;
6410           new_prec = lookahead_prec;
6411           goto get_rhs;
6412
6413          pop:
6414           lookahead_prec = new_prec;
6415           /* If the stack is not empty, we have parsed into LHS the right side
6416              (`4' in the example above) of an expression we had suspended.
6417              We can use the information on the stack to recover the LHS (`3')
6418              from the stack together with the tree code (`MULT_EXPR'), and
6419              the precedence of the higher level subexpression
6420              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6421              which will be used to actually build the additive expression.  */
6422           --sp;
6423           prec = sp->prec;
6424           tree_type = sp->tree_type;
6425           rhs = lhs;
6426           rhs_type = lhs_type;
6427           lhs = sp->lhs;
6428           lhs_type = sp->lhs_type;
6429         }
6430
6431       /* Undo the disabling of warnings done above.  */
6432       if (tree_type == TRUTH_ANDIF_EXPR)
6433         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6434       else if (tree_type == TRUTH_ORIF_EXPR)
6435         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6436
6437       overloaded_p = false;
6438       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6439          ERROR_MARK for everything that is not a binary expression.
6440          This makes warn_about_parentheses miss some warnings that
6441          involve unary operators.  For unary expressions we should
6442          pass the correct tree_code unless the unary expression was
6443          surrounded by parentheses.
6444       */
6445       if (no_toplevel_fold_p
6446           && lookahead_prec <= prec
6447           && sp == stack
6448           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6449         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6450       else
6451         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6452                                  &overloaded_p, tf_warning_or_error);
6453       lhs_type = tree_type;
6454
6455       /* If the binary operator required the use of an overloaded operator,
6456          then this expression cannot be an integral constant-expression.
6457          An overloaded operator can be used even if both operands are
6458          otherwise permissible in an integral constant-expression if at
6459          least one of the operands is of enumeration type.  */
6460
6461       if (overloaded_p
6462           && (cp_parser_non_integral_constant_expression
6463               (parser, "calls to overloaded operators")))
6464         return error_mark_node;
6465     }
6466
6467   return lhs;
6468 }
6469
6470
6471 /* Parse the `? expression : assignment-expression' part of a
6472    conditional-expression.  The LOGICAL_OR_EXPR is the
6473    logical-or-expression that started the conditional-expression.
6474    Returns a representation of the entire conditional-expression.
6475
6476    This routine is used by cp_parser_assignment_expression.
6477
6478      ? expression : assignment-expression
6479
6480    GNU Extensions:
6481
6482      ? : assignment-expression */
6483
6484 static tree
6485 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6486 {
6487   tree expr;
6488   tree assignment_expr;
6489
6490   /* Consume the `?' token.  */
6491   cp_lexer_consume_token (parser->lexer);
6492   if (cp_parser_allow_gnu_extensions_p (parser)
6493       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6494     {
6495       /* Implicit true clause.  */
6496       expr = NULL_TREE;
6497       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6498     }
6499   else
6500     {
6501       /* Parse the expression.  */
6502       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6503       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6504       c_inhibit_evaluation_warnings +=
6505         ((logical_or_expr == truthvalue_true_node)
6506          - (logical_or_expr == truthvalue_false_node));
6507     }
6508
6509   /* The next token should be a `:'.  */
6510   cp_parser_require (parser, CPP_COLON, "%<:%>");
6511   /* Parse the assignment-expression.  */
6512   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6513   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6514
6515   /* Build the conditional-expression.  */
6516   return build_x_conditional_expr (logical_or_expr,
6517                                    expr,
6518                                    assignment_expr,
6519                                    tf_warning_or_error);
6520 }
6521
6522 /* Parse an assignment-expression.
6523
6524    assignment-expression:
6525      conditional-expression
6526      logical-or-expression assignment-operator assignment_expression
6527      throw-expression
6528
6529    CAST_P is true if this expression is the target of a cast.
6530
6531    Returns a representation for the expression.  */
6532
6533 static tree
6534 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6535                                  cp_id_kind * pidk)
6536 {
6537   tree expr;
6538
6539   /* If the next token is the `throw' keyword, then we're looking at
6540      a throw-expression.  */
6541   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6542     expr = cp_parser_throw_expression (parser);
6543   /* Otherwise, it must be that we are looking at a
6544      logical-or-expression.  */
6545   else
6546     {
6547       /* Parse the binary expressions (logical-or-expression).  */
6548       expr = cp_parser_binary_expression (parser, cast_p, false,
6549                                           PREC_NOT_OPERATOR, pidk);
6550       /* If the next token is a `?' then we're actually looking at a
6551          conditional-expression.  */
6552       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6553         return cp_parser_question_colon_clause (parser, expr);
6554       else
6555         {
6556           enum tree_code assignment_operator;
6557
6558           /* If it's an assignment-operator, we're using the second
6559              production.  */
6560           assignment_operator
6561             = cp_parser_assignment_operator_opt (parser);
6562           if (assignment_operator != ERROR_MARK)
6563             {
6564               bool non_constant_p;
6565
6566               /* Parse the right-hand side of the assignment.  */
6567               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6568
6569               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6570                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6571
6572               /* An assignment may not appear in a
6573                  constant-expression.  */
6574               if (cp_parser_non_integral_constant_expression (parser,
6575                                                               "an assignment"))
6576                 return error_mark_node;
6577               /* Build the assignment expression.  */
6578               expr = build_x_modify_expr (expr,
6579                                           assignment_operator,
6580                                           rhs,
6581                                           tf_warning_or_error);
6582             }
6583         }
6584     }
6585
6586   return expr;
6587 }
6588
6589 /* Parse an (optional) assignment-operator.
6590
6591    assignment-operator: one of
6592      = *= /= %= += -= >>= <<= &= ^= |=
6593
6594    GNU Extension:
6595
6596    assignment-operator: one of
6597      <?= >?=
6598
6599    If the next token is an assignment operator, the corresponding tree
6600    code is returned, and the token is consumed.  For example, for
6601    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6602    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6603    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6604    operator, ERROR_MARK is returned.  */
6605
6606 static enum tree_code
6607 cp_parser_assignment_operator_opt (cp_parser* parser)
6608 {
6609   enum tree_code op;
6610   cp_token *token;
6611
6612   /* Peek at the next token.  */
6613   token = cp_lexer_peek_token (parser->lexer);
6614
6615   switch (token->type)
6616     {
6617     case CPP_EQ:
6618       op = NOP_EXPR;
6619       break;
6620
6621     case CPP_MULT_EQ:
6622       op = MULT_EXPR;
6623       break;
6624
6625     case CPP_DIV_EQ:
6626       op = TRUNC_DIV_EXPR;
6627       break;
6628
6629     case CPP_MOD_EQ:
6630       op = TRUNC_MOD_EXPR;
6631       break;
6632
6633     case CPP_PLUS_EQ:
6634       op = PLUS_EXPR;
6635       break;
6636
6637     case CPP_MINUS_EQ:
6638       op = MINUS_EXPR;
6639       break;
6640
6641     case CPP_RSHIFT_EQ:
6642       op = RSHIFT_EXPR;
6643       break;
6644
6645     case CPP_LSHIFT_EQ:
6646       op = LSHIFT_EXPR;
6647       break;
6648
6649     case CPP_AND_EQ:
6650       op = BIT_AND_EXPR;
6651       break;
6652
6653     case CPP_XOR_EQ:
6654       op = BIT_XOR_EXPR;
6655       break;
6656
6657     case CPP_OR_EQ:
6658       op = BIT_IOR_EXPR;
6659       break;
6660
6661     default:
6662       /* Nothing else is an assignment operator.  */
6663       op = ERROR_MARK;
6664     }
6665
6666   /* If it was an assignment operator, consume it.  */
6667   if (op != ERROR_MARK)
6668     cp_lexer_consume_token (parser->lexer);
6669
6670   return op;
6671 }
6672
6673 /* Parse an expression.
6674
6675    expression:
6676      assignment-expression
6677      expression , assignment-expression
6678
6679    CAST_P is true if this expression is the target of a cast.
6680
6681    Returns a representation of the expression.  */
6682
6683 static tree
6684 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6685 {
6686   tree expression = NULL_TREE;
6687
6688   while (true)
6689     {
6690       tree assignment_expression;
6691
6692       /* Parse the next assignment-expression.  */
6693       assignment_expression
6694         = cp_parser_assignment_expression (parser, cast_p, pidk);
6695       /* If this is the first assignment-expression, we can just
6696          save it away.  */
6697       if (!expression)
6698         expression = assignment_expression;
6699       else
6700         expression = build_x_compound_expr (expression,
6701                                             assignment_expression,
6702                                             tf_warning_or_error);
6703       /* If the next token is not a comma, then we are done with the
6704          expression.  */
6705       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6706         break;
6707       /* Consume the `,'.  */
6708       cp_lexer_consume_token (parser->lexer);
6709       /* A comma operator cannot appear in a constant-expression.  */
6710       if (cp_parser_non_integral_constant_expression (parser,
6711                                                       "a comma operator"))
6712         expression = error_mark_node;
6713     }
6714
6715   return expression;
6716 }
6717
6718 /* Parse a constant-expression.
6719
6720    constant-expression:
6721      conditional-expression
6722
6723   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6724   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6725   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6726   is false, NON_CONSTANT_P should be NULL.  */
6727
6728 static tree
6729 cp_parser_constant_expression (cp_parser* parser,
6730                                bool allow_non_constant_p,
6731                                bool *non_constant_p)
6732 {
6733   bool saved_integral_constant_expression_p;
6734   bool saved_allow_non_integral_constant_expression_p;
6735   bool saved_non_integral_constant_expression_p;
6736   tree expression;
6737
6738   /* It might seem that we could simply parse the
6739      conditional-expression, and then check to see if it were
6740      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6741      one that the compiler can figure out is constant, possibly after
6742      doing some simplifications or optimizations.  The standard has a
6743      precise definition of constant-expression, and we must honor
6744      that, even though it is somewhat more restrictive.
6745
6746      For example:
6747
6748        int i[(2, 3)];
6749
6750      is not a legal declaration, because `(2, 3)' is not a
6751      constant-expression.  The `,' operator is forbidden in a
6752      constant-expression.  However, GCC's constant-folding machinery
6753      will fold this operation to an INTEGER_CST for `3'.  */
6754
6755   /* Save the old settings.  */
6756   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6757   saved_allow_non_integral_constant_expression_p
6758     = parser->allow_non_integral_constant_expression_p;
6759   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6760   /* We are now parsing a constant-expression.  */
6761   parser->integral_constant_expression_p = true;
6762   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6763   parser->non_integral_constant_expression_p = false;
6764   /* Although the grammar says "conditional-expression", we parse an
6765      "assignment-expression", which also permits "throw-expression"
6766      and the use of assignment operators.  In the case that
6767      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6768      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6769      actually essential that we look for an assignment-expression.
6770      For example, cp_parser_initializer_clauses uses this function to
6771      determine whether a particular assignment-expression is in fact
6772      constant.  */
6773   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6774   /* Restore the old settings.  */
6775   parser->integral_constant_expression_p
6776     = saved_integral_constant_expression_p;
6777   parser->allow_non_integral_constant_expression_p
6778     = saved_allow_non_integral_constant_expression_p;
6779   if (allow_non_constant_p)
6780     *non_constant_p = parser->non_integral_constant_expression_p;
6781   else if (parser->non_integral_constant_expression_p)
6782     expression = error_mark_node;
6783   parser->non_integral_constant_expression_p
6784     = saved_non_integral_constant_expression_p;
6785
6786   return expression;
6787 }
6788
6789 /* Parse __builtin_offsetof.
6790
6791    offsetof-expression:
6792      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6793
6794    offsetof-member-designator:
6795      id-expression
6796      | offsetof-member-designator "." id-expression
6797      | offsetof-member-designator "[" expression "]"
6798      | offsetof-member-designator "->" id-expression  */
6799
6800 static tree
6801 cp_parser_builtin_offsetof (cp_parser *parser)
6802 {
6803   int save_ice_p, save_non_ice_p;
6804   tree type, expr;
6805   cp_id_kind dummy;
6806   cp_token *token;
6807
6808   /* We're about to accept non-integral-constant things, but will
6809      definitely yield an integral constant expression.  Save and
6810      restore these values around our local parsing.  */
6811   save_ice_p = parser->integral_constant_expression_p;
6812   save_non_ice_p = parser->non_integral_constant_expression_p;
6813
6814   /* Consume the "__builtin_offsetof" token.  */
6815   cp_lexer_consume_token (parser->lexer);
6816   /* Consume the opening `('.  */
6817   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6818   /* Parse the type-id.  */
6819   type = cp_parser_type_id (parser);
6820   /* Look for the `,'.  */
6821   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6822   token = cp_lexer_peek_token (parser->lexer);
6823
6824   /* Build the (type *)null that begins the traditional offsetof macro.  */
6825   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6826                             tf_warning_or_error);
6827
6828   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6829   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6830                                                  true, &dummy, token->location);
6831   while (true)
6832     {
6833       token = cp_lexer_peek_token (parser->lexer);
6834       switch (token->type)
6835         {
6836         case CPP_OPEN_SQUARE:
6837           /* offsetof-member-designator "[" expression "]" */
6838           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6839           break;
6840
6841         case CPP_DEREF:
6842           /* offsetof-member-designator "->" identifier */
6843           expr = grok_array_decl (expr, integer_zero_node);
6844           /* FALLTHRU */
6845
6846         case CPP_DOT:
6847           /* offsetof-member-designator "." identifier */
6848           cp_lexer_consume_token (parser->lexer);
6849           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6850                                                          expr, true, &dummy,
6851                                                          token->location);
6852           break;
6853
6854         case CPP_CLOSE_PAREN:
6855           /* Consume the ")" token.  */
6856           cp_lexer_consume_token (parser->lexer);
6857           goto success;
6858
6859         default:
6860           /* Error.  We know the following require will fail, but
6861              that gives the proper error message.  */
6862           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6863           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6864           expr = error_mark_node;
6865           goto failure;
6866         }
6867     }
6868
6869  success:
6870   /* If we're processing a template, we can't finish the semantics yet.
6871      Otherwise we can fold the entire expression now.  */
6872   if (processing_template_decl)
6873     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6874   else
6875     expr = finish_offsetof (expr);
6876
6877  failure:
6878   parser->integral_constant_expression_p = save_ice_p;
6879   parser->non_integral_constant_expression_p = save_non_ice_p;
6880
6881   return expr;
6882 }
6883
6884 /* Parse a trait expression.  */
6885
6886 static tree
6887 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6888 {
6889   cp_trait_kind kind;
6890   tree type1, type2 = NULL_TREE;
6891   bool binary = false;
6892   cp_decl_specifier_seq decl_specs;
6893
6894   switch (keyword)
6895     {
6896     case RID_HAS_NOTHROW_ASSIGN:
6897       kind = CPTK_HAS_NOTHROW_ASSIGN;
6898       break;
6899     case RID_HAS_NOTHROW_CONSTRUCTOR:
6900       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6901       break;
6902     case RID_HAS_NOTHROW_COPY:
6903       kind = CPTK_HAS_NOTHROW_COPY;
6904       break;
6905     case RID_HAS_TRIVIAL_ASSIGN:
6906       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6907       break;
6908     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6909       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6910       break;
6911     case RID_HAS_TRIVIAL_COPY:
6912       kind = CPTK_HAS_TRIVIAL_COPY;
6913       break;
6914     case RID_HAS_TRIVIAL_DESTRUCTOR:
6915       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6916       break;
6917     case RID_HAS_VIRTUAL_DESTRUCTOR:
6918       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6919       break;
6920     case RID_IS_ABSTRACT:
6921       kind = CPTK_IS_ABSTRACT;
6922       break;
6923     case RID_IS_BASE_OF:
6924       kind = CPTK_IS_BASE_OF;
6925       binary = true;
6926       break;
6927     case RID_IS_CLASS:
6928       kind = CPTK_IS_CLASS;
6929       break;
6930     case RID_IS_CONVERTIBLE_TO:
6931       kind = CPTK_IS_CONVERTIBLE_TO;
6932       binary = true;
6933       break;
6934     case RID_IS_EMPTY:
6935       kind = CPTK_IS_EMPTY;
6936       break;
6937     case RID_IS_ENUM:
6938       kind = CPTK_IS_ENUM;
6939       break;
6940     case RID_IS_POD:
6941       kind = CPTK_IS_POD;
6942       break;
6943     case RID_IS_POLYMORPHIC:
6944       kind = CPTK_IS_POLYMORPHIC;
6945       break;
6946     case RID_IS_STD_LAYOUT:
6947       kind = CPTK_IS_STD_LAYOUT;
6948       break;
6949     case RID_IS_TRIVIAL:
6950       kind = CPTK_IS_TRIVIAL;
6951       break;
6952     case RID_IS_UNION:
6953       kind = CPTK_IS_UNION;
6954       break;
6955     default:
6956       gcc_unreachable ();
6957     }
6958
6959   /* Consume the token.  */
6960   cp_lexer_consume_token (parser->lexer);
6961
6962   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6963
6964   type1 = cp_parser_type_id (parser);
6965
6966   if (type1 == error_mark_node)
6967     return error_mark_node;
6968
6969   /* Build a trivial decl-specifier-seq.  */
6970   clear_decl_specs (&decl_specs);
6971   decl_specs.type = type1;
6972
6973   /* Call grokdeclarator to figure out what type this is.  */
6974   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6975                           /*initialized=*/0, /*attrlist=*/NULL);
6976
6977   if (binary)
6978     {
6979       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6980  
6981       type2 = cp_parser_type_id (parser);
6982
6983       if (type2 == error_mark_node)
6984         return error_mark_node;
6985
6986       /* Build a trivial decl-specifier-seq.  */
6987       clear_decl_specs (&decl_specs);
6988       decl_specs.type = type2;
6989
6990       /* Call grokdeclarator to figure out what type this is.  */
6991       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6992                               /*initialized=*/0, /*attrlist=*/NULL);
6993     }
6994
6995   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6996
6997   /* Complete the trait expression, which may mean either processing
6998      the trait expr now or saving it for template instantiation.  */
6999   return finish_trait_expr (kind, type1, type2);
7000 }
7001
7002 /* Lambdas that appear in variable initializer or default argument scope
7003    get that in their mangling, so we need to record it.  We might as well
7004    use the count for function and namespace scopes as well.  */
7005 static GTY(()) tree lambda_scope;
7006 static GTY(()) int lambda_count;
7007 typedef struct GTY(()) tree_int
7008 {
7009   tree t;
7010   int i;
7011 } tree_int;
7012 DEF_VEC_O(tree_int);
7013 DEF_VEC_ALLOC_O(tree_int,gc);
7014 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7015
7016 static void
7017 start_lambda_scope (tree decl)
7018 {
7019   tree_int ti;
7020   gcc_assert (decl);
7021   /* Once we're inside a function, we ignore other scopes and just push
7022      the function again so that popping works properly.  */
7023   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7024     decl = current_function_decl;
7025   ti.t = lambda_scope;
7026   ti.i = lambda_count;
7027   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7028   if (lambda_scope != decl)
7029     {
7030       /* Don't reset the count if we're still in the same function.  */
7031       lambda_scope = decl;
7032       lambda_count = 0;
7033     }
7034 }
7035
7036 static void
7037 record_lambda_scope (tree lambda)
7038 {
7039   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7040   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7041 }
7042
7043 static void
7044 finish_lambda_scope (void)
7045 {
7046   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7047   if (lambda_scope != p->t)
7048     {
7049       lambda_scope = p->t;
7050       lambda_count = p->i;
7051     }
7052   VEC_pop (tree_int, lambda_scope_stack);
7053 }
7054
7055 /* Parse a lambda expression.
7056
7057    lambda-expression:
7058      lambda-introducer lambda-declarator [opt] compound-statement
7059
7060    Returns a representation of the expression.  */
7061
7062 static tree
7063 cp_parser_lambda_expression (cp_parser* parser)
7064 {
7065   tree lambda_expr = build_lambda_expr ();
7066   tree type;
7067
7068   LAMBDA_EXPR_LOCATION (lambda_expr)
7069     = cp_lexer_peek_token (parser->lexer)->location;
7070
7071   /* We may be in the middle of deferred access check.  Disable
7072      it now.  */
7073   push_deferring_access_checks (dk_no_deferred);
7074
7075   cp_parser_lambda_introducer (parser, lambda_expr);
7076
7077   type = begin_lambda_type (lambda_expr);
7078
7079   record_lambda_scope (lambda_expr);
7080
7081   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7082   determine_visibility (TYPE_NAME (type));
7083
7084   /* Now that we've started the type, add the capture fields for any
7085      explicit captures.  */
7086   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7087
7088   {
7089     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7090     unsigned int saved_num_template_parameter_lists
7091         = parser->num_template_parameter_lists;
7092
7093     parser->num_template_parameter_lists = 0;
7094
7095     /* By virtue of defining a local class, a lambda expression has access to
7096        the private variables of enclosing classes.  */
7097
7098     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7099
7100     cp_parser_lambda_body (parser, lambda_expr);
7101
7102     /* The capture list was built up in reverse order; fix that now.  */
7103     {
7104       tree newlist = NULL_TREE;
7105       tree elt, next;
7106
7107       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7108            elt; elt = next)
7109         {
7110           tree field = TREE_PURPOSE (elt);
7111           char *buf;
7112
7113           next = TREE_CHAIN (elt);
7114           TREE_CHAIN (elt) = newlist;
7115           newlist = elt;
7116
7117           /* Also add __ to the beginning of the field name so that code
7118              outside the lambda body can't see the captured name.  We could
7119              just remove the name entirely, but this is more useful for
7120              debugging.  */
7121           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7122             /* The 'this' capture already starts with __.  */
7123             continue;
7124
7125           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7126           buf[1] = buf[0] = '_';
7127           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7128                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7129           DECL_NAME (field) = get_identifier (buf);
7130         }
7131       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7132     }
7133
7134     maybe_add_lambda_conv_op (type);
7135
7136     type = finish_struct (type, /*attributes=*/NULL_TREE);
7137
7138     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7139   }
7140
7141   pop_deferring_access_checks ();
7142
7143   return build_lambda_object (lambda_expr);
7144 }
7145
7146 /* Parse the beginning of a lambda expression.
7147
7148    lambda-introducer:
7149      [ lambda-capture [opt] ]
7150
7151    LAMBDA_EXPR is the current representation of the lambda expression.  */
7152
7153 static void
7154 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7155 {
7156   /* Need commas after the first capture.  */
7157   bool first = true;
7158
7159   /* Eat the leading `['.  */
7160   cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7161
7162   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7163   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7164       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7165     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7166   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7167     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7168
7169   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7170     {
7171       cp_lexer_consume_token (parser->lexer);
7172       first = false;
7173     }
7174
7175   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7176     {
7177       cp_token* capture_token;
7178       tree capture_id;
7179       tree capture_init_expr;
7180       cp_id_kind idk = CP_ID_KIND_NONE;
7181       bool explicit_init_p = false;
7182
7183       enum capture_kind_type
7184       {
7185         BY_COPY,
7186         BY_REFERENCE
7187       };
7188       enum capture_kind_type capture_kind = BY_COPY;
7189
7190       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7191         {
7192           error ("expected end of capture-list");
7193           return;
7194         }
7195
7196       if (first)
7197         first = false;
7198       else
7199         cp_parser_require (parser, CPP_COMMA, "%<,%>");
7200
7201       /* Possibly capture `this'.  */
7202       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7203         {
7204           cp_lexer_consume_token (parser->lexer);
7205           add_capture (lambda_expr,
7206                        /*id=*/get_identifier ("__this"),
7207                        /*initializer=*/finish_this_expr(),
7208                        /*by_reference_p=*/false,
7209                        explicit_init_p);
7210           continue;
7211         }
7212
7213       /* Remember whether we want to capture as a reference or not.  */
7214       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7215         {
7216           capture_kind = BY_REFERENCE;
7217           cp_lexer_consume_token (parser->lexer);
7218         }
7219
7220       /* Get the identifier.  */
7221       capture_token = cp_lexer_peek_token (parser->lexer);
7222       capture_id = cp_parser_identifier (parser);
7223
7224       if (capture_id == error_mark_node)
7225         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7226            delimiters, but I modified this to stop on unnested ']' as well.  It
7227            was already changed to stop on unnested '}', so the
7228            "closing_parenthesis" name is no more misleading with my change.  */
7229         {
7230           cp_parser_skip_to_closing_parenthesis (parser,
7231                                                  /*recovering=*/true,
7232                                                  /*or_comma=*/true,
7233                                                  /*consume_paren=*/true);
7234           break;
7235         }
7236
7237       /* Find the initializer for this capture.  */
7238       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7239         {
7240           /* An explicit expression exists.  */
7241           cp_lexer_consume_token (parser->lexer);
7242           pedwarn (input_location, OPT_pedantic,
7243                    "ISO C++ does not allow initializers "
7244                    "in lambda expression capture lists");
7245           capture_init_expr = cp_parser_assignment_expression (parser,
7246                                                                /*cast_p=*/true,
7247                                                                &idk);
7248           explicit_init_p = true;
7249         }
7250       else
7251         {
7252           const char* error_msg;
7253
7254           /* Turn the identifier into an id-expression.  */
7255           capture_init_expr
7256             = cp_parser_lookup_name
7257                 (parser,
7258                  capture_id,
7259                  none_type,
7260                  /*is_template=*/false,
7261                  /*is_namespace=*/false,
7262                  /*check_dependency=*/true,
7263                  /*ambiguous_decls=*/NULL,
7264                  capture_token->location);
7265
7266           capture_init_expr
7267             = finish_id_expression
7268                 (capture_id,
7269                  capture_init_expr,
7270                  parser->scope,
7271                  &idk,
7272                  /*integral_constant_expression_p=*/false,
7273                  /*allow_non_integral_constant_expression_p=*/false,
7274                  /*non_integral_constant_expression_p=*/NULL,
7275                  /*template_p=*/false,
7276                  /*done=*/true,
7277                  /*address_p=*/false,
7278                  /*template_arg_p=*/false,
7279                  &error_msg,
7280                  capture_token->location);
7281         }
7282
7283       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7284         capture_init_expr
7285           = unqualified_name_lookup_error (capture_init_expr);
7286
7287       add_capture (lambda_expr,
7288                    capture_id,
7289                    capture_init_expr,
7290                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7291                    explicit_init_p);
7292     }
7293
7294   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7295 }
7296
7297 /* Parse the (optional) middle of a lambda expression.
7298
7299    lambda-declarator:
7300      ( parameter-declaration-clause [opt] )
7301        attribute-specifier [opt]
7302        mutable [opt]
7303        exception-specification [opt]
7304        lambda-return-type-clause [opt]
7305
7306    LAMBDA_EXPR is the current representation of the lambda expression.  */
7307
7308 static void
7309 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7310 {
7311   /* 5.1.1.4 of the standard says:
7312        If a lambda-expression does not include a lambda-declarator, it is as if
7313        the lambda-declarator were ().
7314      This means an empty parameter list, no attributes, and no exception
7315      specification.  */
7316   tree param_list = void_list_node;
7317   tree attributes = NULL_TREE;
7318   tree exception_spec = NULL_TREE;
7319   tree t;
7320
7321   /* The lambda-declarator is optional, but must begin with an opening
7322      parenthesis if present.  */
7323   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7324     {
7325       cp_lexer_consume_token (parser->lexer);
7326
7327       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7328
7329       /* Parse parameters.  */
7330       param_list = cp_parser_parameter_declaration_clause (parser);
7331
7332       /* Default arguments shall not be specified in the
7333          parameter-declaration-clause of a lambda-declarator.  */
7334       for (t = param_list; t; t = TREE_CHAIN (t))
7335         if (TREE_PURPOSE (t))
7336           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7337                    "default argument specified for lambda parameter");
7338
7339       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7340
7341       attributes = cp_parser_attributes_opt (parser);
7342
7343       /* Parse optional `mutable' keyword.  */
7344       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7345         {
7346           cp_lexer_consume_token (parser->lexer);
7347           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7348         }
7349
7350       /* Parse optional exception specification.  */
7351       exception_spec = cp_parser_exception_specification_opt (parser);
7352
7353       /* Parse optional trailing return type.  */
7354       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7355         {
7356           cp_lexer_consume_token (parser->lexer);
7357           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7358         }
7359
7360       /* The function parameters must be in scope all the way until after the
7361          trailing-return-type in case of decltype.  */
7362       for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7363         pop_binding (DECL_NAME (t), t);
7364
7365       leave_scope ();
7366     }
7367
7368   /* Create the function call operator.
7369
7370      Messing with declarators like this is no uglier than building up the
7371      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7372      other code.  */
7373   {
7374     cp_decl_specifier_seq return_type_specs;
7375     cp_declarator* declarator;
7376     tree fco;
7377     int quals;
7378     void *p;
7379
7380     clear_decl_specs (&return_type_specs);
7381     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7382       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7383     else
7384       /* Maybe we will deduce the return type later, but we can use void
7385          as a placeholder return type anyways.  */
7386       return_type_specs.type = void_type_node;
7387
7388     p = obstack_alloc (&declarator_obstack, 0);
7389
7390     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7391                                      sfk_none);
7392
7393     quals = TYPE_UNQUALIFIED;
7394     if (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) == NULL_TREE
7395         && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
7396       {
7397         /* A lambda with no captures has a static op() and a conversion op
7398            to function type.  */
7399         if (LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7400           error ("lambda expression with no captures declared mutable");
7401         return_type_specs.storage_class = sc_static;
7402       }
7403     else if (!LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7404       quals = TYPE_QUAL_CONST;
7405     declarator = make_call_declarator (declarator, param_list, quals,
7406                                        exception_spec,
7407                                        /*late_return_type=*/NULL_TREE);
7408
7409     fco = grokmethod (&return_type_specs,
7410                       declarator,
7411                       attributes);
7412     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7413     DECL_ARTIFICIAL (fco) = 1;
7414
7415     finish_member_declaration (fco);
7416
7417     obstack_free (&declarator_obstack, p);
7418   }
7419 }
7420
7421 /* Parse the body of a lambda expression, which is simply
7422
7423    compound-statement
7424
7425    but which requires special handling.
7426    LAMBDA_EXPR is the current representation of the lambda expression.  */
7427
7428 static void
7429 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7430 {
7431   bool nested = (current_function_decl != NULL_TREE);
7432   if (nested)
7433     push_function_context ();
7434
7435   /* Finish the function call operator
7436      - class_specifier
7437      + late_parsing_for_member
7438      + function_definition_after_declarator
7439      + ctor_initializer_opt_and_function_body  */
7440   {
7441     tree fco = lambda_function (lambda_expr);
7442     tree body;
7443     bool done = false;
7444
7445     /* Let the front end know that we are going to be defining this
7446        function.  */
7447     start_preparsed_function (fco,
7448                               NULL_TREE,
7449                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7450
7451     start_lambda_scope (fco);
7452     body = begin_function_body ();
7453
7454     /* 5.1.1.4 of the standard says:
7455          If a lambda-expression does not include a trailing-return-type, it
7456          is as if the trailing-return-type denotes the following type:
7457           * if the compound-statement is of the form
7458                { return attribute-specifier [opt] expression ; }
7459              the type of the returned expression after lvalue-to-rvalue
7460              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7461              (_conv.array_ 4.2), and function-to-pointer conversion
7462              (_conv.func_ 4.3);
7463           * otherwise, void.  */
7464
7465     /* In a lambda that has neither a lambda-return-type-clause
7466        nor a deducible form, errors should be reported for return statements
7467        in the body.  Since we used void as the placeholder return type, parsing
7468        the body as usual will give such desired behavior.  */
7469     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7470         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7471         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7472         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7473       {
7474         tree compound_stmt;
7475         tree expr = NULL_TREE;
7476         cp_id_kind idk = CP_ID_KIND_NONE;
7477
7478         /* Parse tentatively in case there's more after the initial return
7479            statement.  */
7480         cp_parser_parse_tentatively (parser);
7481
7482         cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7483         cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7484
7485         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7486
7487         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7488         cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7489
7490         if (cp_parser_parse_definitely (parser))
7491           {
7492             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7493
7494             compound_stmt = begin_compound_stmt (0);
7495             /* Will get error here if type not deduced yet.  */
7496             finish_return_stmt (expr);
7497             finish_compound_stmt (compound_stmt);
7498
7499             done = true;
7500           }
7501       }
7502
7503     if (!done)
7504       {
7505         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7506           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7507         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7508            cp_parser_compound_stmt does not pass it.  */
7509         cp_parser_function_body (parser);
7510         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7511       }
7512
7513     finish_function_body (body);
7514     finish_lambda_scope ();
7515
7516     /* Finish the function and generate code for it if necessary.  */
7517     expand_or_defer_fn (finish_function (/*inline*/2));
7518   }
7519
7520   if (nested)
7521     pop_function_context();
7522 }
7523
7524 /* Statements [gram.stmt.stmt]  */
7525
7526 /* Parse a statement.
7527
7528    statement:
7529      labeled-statement
7530      expression-statement
7531      compound-statement
7532      selection-statement
7533      iteration-statement
7534      jump-statement
7535      declaration-statement
7536      try-block
7537
7538   IN_COMPOUND is true when the statement is nested inside a
7539   cp_parser_compound_statement; this matters for certain pragmas.
7540
7541   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7542   is a (possibly labeled) if statement which is not enclosed in braces
7543   and has an else clause.  This is used to implement -Wparentheses.  */
7544
7545 static void
7546 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7547                      bool in_compound, bool *if_p)
7548 {
7549   tree statement;
7550   cp_token *token;
7551   location_t statement_location;
7552
7553  restart:
7554   if (if_p != NULL)
7555     *if_p = false;
7556   /* There is no statement yet.  */
7557   statement = NULL_TREE;
7558   /* Peek at the next token.  */
7559   token = cp_lexer_peek_token (parser->lexer);
7560   /* Remember the location of the first token in the statement.  */
7561   statement_location = token->location;
7562   /* If this is a keyword, then that will often determine what kind of
7563      statement we have.  */
7564   if (token->type == CPP_KEYWORD)
7565     {
7566       enum rid keyword = token->keyword;
7567
7568       switch (keyword)
7569         {
7570         case RID_CASE:
7571         case RID_DEFAULT:
7572           /* Looks like a labeled-statement with a case label.
7573              Parse the label, and then use tail recursion to parse
7574              the statement.  */
7575           cp_parser_label_for_labeled_statement (parser);
7576           goto restart;
7577
7578         case RID_IF:
7579         case RID_SWITCH:
7580           statement = cp_parser_selection_statement (parser, if_p);
7581           break;
7582
7583         case RID_WHILE:
7584         case RID_DO:
7585         case RID_FOR:
7586           statement = cp_parser_iteration_statement (parser);
7587           break;
7588
7589         case RID_BREAK:
7590         case RID_CONTINUE:
7591         case RID_RETURN:
7592         case RID_GOTO:
7593           statement = cp_parser_jump_statement (parser);
7594           break;
7595
7596           /* Objective-C++ exception-handling constructs.  */
7597         case RID_AT_TRY:
7598         case RID_AT_CATCH:
7599         case RID_AT_FINALLY:
7600         case RID_AT_SYNCHRONIZED:
7601         case RID_AT_THROW:
7602           statement = cp_parser_objc_statement (parser);
7603           break;
7604
7605         case RID_TRY:
7606           statement = cp_parser_try_block (parser);
7607           break;
7608
7609         case RID_NAMESPACE:
7610           /* This must be a namespace alias definition.  */
7611           cp_parser_declaration_statement (parser);
7612           return;
7613           
7614         default:
7615           /* It might be a keyword like `int' that can start a
7616              declaration-statement.  */
7617           break;
7618         }
7619     }
7620   else if (token->type == CPP_NAME)
7621     {
7622       /* If the next token is a `:', then we are looking at a
7623          labeled-statement.  */
7624       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7625       if (token->type == CPP_COLON)
7626         {
7627           /* Looks like a labeled-statement with an ordinary label.
7628              Parse the label, and then use tail recursion to parse
7629              the statement.  */
7630           cp_parser_label_for_labeled_statement (parser);
7631           goto restart;
7632         }
7633     }
7634   /* Anything that starts with a `{' must be a compound-statement.  */
7635   else if (token->type == CPP_OPEN_BRACE)
7636     statement = cp_parser_compound_statement (parser, NULL, false);
7637   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7638      a statement all its own.  */
7639   else if (token->type == CPP_PRAGMA)
7640     {
7641       /* Only certain OpenMP pragmas are attached to statements, and thus
7642          are considered statements themselves.  All others are not.  In
7643          the context of a compound, accept the pragma as a "statement" and
7644          return so that we can check for a close brace.  Otherwise we
7645          require a real statement and must go back and read one.  */
7646       if (in_compound)
7647         cp_parser_pragma (parser, pragma_compound);
7648       else if (!cp_parser_pragma (parser, pragma_stmt))
7649         goto restart;
7650       return;
7651     }
7652   else if (token->type == CPP_EOF)
7653     {
7654       cp_parser_error (parser, "expected statement");
7655       return;
7656     }
7657
7658   /* Everything else must be a declaration-statement or an
7659      expression-statement.  Try for the declaration-statement
7660      first, unless we are looking at a `;', in which case we know that
7661      we have an expression-statement.  */
7662   if (!statement)
7663     {
7664       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7665         {
7666           cp_parser_parse_tentatively (parser);
7667           /* Try to parse the declaration-statement.  */
7668           cp_parser_declaration_statement (parser);
7669           /* If that worked, we're done.  */
7670           if (cp_parser_parse_definitely (parser))
7671             return;
7672         }
7673       /* Look for an expression-statement instead.  */
7674       statement = cp_parser_expression_statement (parser, in_statement_expr);
7675     }
7676
7677   /* Set the line number for the statement.  */
7678   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7679     SET_EXPR_LOCATION (statement, statement_location);
7680 }
7681
7682 /* Parse the label for a labeled-statement, i.e.
7683
7684    identifier :
7685    case constant-expression :
7686    default :
7687
7688    GNU Extension:
7689    case constant-expression ... constant-expression : statement
7690
7691    When a label is parsed without errors, the label is added to the
7692    parse tree by the finish_* functions, so this function doesn't
7693    have to return the label.  */
7694
7695 static void
7696 cp_parser_label_for_labeled_statement (cp_parser* parser)
7697 {
7698   cp_token *token;
7699   tree label = NULL_TREE;
7700
7701   /* The next token should be an identifier.  */
7702   token = cp_lexer_peek_token (parser->lexer);
7703   if (token->type != CPP_NAME
7704       && token->type != CPP_KEYWORD)
7705     {
7706       cp_parser_error (parser, "expected labeled-statement");
7707       return;
7708     }
7709
7710   switch (token->keyword)
7711     {
7712     case RID_CASE:
7713       {
7714         tree expr, expr_hi;
7715         cp_token *ellipsis;
7716
7717         /* Consume the `case' token.  */
7718         cp_lexer_consume_token (parser->lexer);
7719         /* Parse the constant-expression.  */
7720         expr = cp_parser_constant_expression (parser,
7721                                               /*allow_non_constant_p=*/false,
7722                                               NULL);
7723
7724         ellipsis = cp_lexer_peek_token (parser->lexer);
7725         if (ellipsis->type == CPP_ELLIPSIS)
7726           {
7727             /* Consume the `...' token.  */
7728             cp_lexer_consume_token (parser->lexer);
7729             expr_hi =
7730               cp_parser_constant_expression (parser,
7731                                              /*allow_non_constant_p=*/false,
7732                                              NULL);
7733             /* We don't need to emit warnings here, as the common code
7734                will do this for us.  */
7735           }
7736         else
7737           expr_hi = NULL_TREE;
7738
7739         if (parser->in_switch_statement_p)
7740           finish_case_label (token->location, expr, expr_hi);
7741         else
7742           error_at (token->location,
7743                     "case label %qE not within a switch statement",
7744                     expr);
7745       }
7746       break;
7747
7748     case RID_DEFAULT:
7749       /* Consume the `default' token.  */
7750       cp_lexer_consume_token (parser->lexer);
7751
7752       if (parser->in_switch_statement_p)
7753         finish_case_label (token->location, NULL_TREE, NULL_TREE);
7754       else
7755         error_at (token->location, "case label not within a switch statement");
7756       break;
7757
7758     default:
7759       /* Anything else must be an ordinary label.  */
7760       label = finish_label_stmt (cp_parser_identifier (parser));
7761       break;
7762     }
7763
7764   /* Require the `:' token.  */
7765   cp_parser_require (parser, CPP_COLON, "%<:%>");
7766
7767   /* An ordinary label may optionally be followed by attributes.
7768      However, this is only permitted if the attributes are then
7769      followed by a semicolon.  This is because, for backward
7770      compatibility, when parsing
7771        lab: __attribute__ ((unused)) int i;
7772      we want the attribute to attach to "i", not "lab".  */
7773   if (label != NULL_TREE
7774       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7775     {
7776       tree attrs;
7777
7778       cp_parser_parse_tentatively (parser);
7779       attrs = cp_parser_attributes_opt (parser);
7780       if (attrs == NULL_TREE
7781           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7782         cp_parser_abort_tentative_parse (parser);
7783       else if (!cp_parser_parse_definitely (parser))
7784         ;
7785       else
7786         cplus_decl_attributes (&label, attrs, 0);
7787     }
7788 }
7789
7790 /* Parse an expression-statement.
7791
7792    expression-statement:
7793      expression [opt] ;
7794
7795    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7796    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7797    indicates whether this expression-statement is part of an
7798    expression statement.  */
7799
7800 static tree
7801 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7802 {
7803   tree statement = NULL_TREE;
7804   cp_token *token = cp_lexer_peek_token (parser->lexer);
7805
7806   /* If the next token is a ';', then there is no expression
7807      statement.  */
7808   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7809     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7810
7811   /* Give a helpful message for "A<T>::type t;" and the like.  */
7812   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7813       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
7814     {
7815       if (TREE_CODE (statement) == SCOPE_REF)
7816         error_at (token->location, "need %<typename%> before %qE because "
7817                   "%qT is a dependent scope",
7818                   statement, TREE_OPERAND (statement, 0));
7819       else if (is_overloaded_fn (statement)
7820                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
7821         {
7822           /* A::A a; */
7823           tree fn = get_first_fn (statement);
7824           error_at (token->location,
7825                     "%<%T::%D%> names the constructor, not the type",
7826                     DECL_CONTEXT (fn), DECL_NAME (fn));
7827         }
7828     }
7829
7830   /* Consume the final `;'.  */
7831   cp_parser_consume_semicolon_at_end_of_statement (parser);
7832
7833   if (in_statement_expr
7834       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7835     /* This is the final expression statement of a statement
7836        expression.  */
7837     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7838   else if (statement)
7839     statement = finish_expr_stmt (statement);
7840   else
7841     finish_stmt ();
7842
7843   return statement;
7844 }
7845
7846 /* Parse a compound-statement.
7847
7848    compound-statement:
7849      { statement-seq [opt] }
7850
7851    GNU extension:
7852
7853    compound-statement:
7854      { label-declaration-seq [opt] statement-seq [opt] }
7855
7856    label-declaration-seq:
7857      label-declaration
7858      label-declaration-seq label-declaration
7859
7860    Returns a tree representing the statement.  */
7861
7862 static tree
7863 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7864                               bool in_try)
7865 {
7866   tree compound_stmt;
7867
7868   /* Consume the `{'.  */
7869   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7870     return error_mark_node;
7871   /* Begin the compound-statement.  */
7872   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7873   /* If the next keyword is `__label__' we have a label declaration.  */
7874   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7875     cp_parser_label_declaration (parser);
7876   /* Parse an (optional) statement-seq.  */
7877   cp_parser_statement_seq_opt (parser, in_statement_expr);
7878   /* Finish the compound-statement.  */
7879   finish_compound_stmt (compound_stmt);
7880   /* Consume the `}'.  */
7881   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7882
7883   return compound_stmt;
7884 }
7885
7886 /* Parse an (optional) statement-seq.
7887
7888    statement-seq:
7889      statement
7890      statement-seq [opt] statement  */
7891
7892 static void
7893 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7894 {
7895   /* Scan statements until there aren't any more.  */
7896   while (true)
7897     {
7898       cp_token *token = cp_lexer_peek_token (parser->lexer);
7899
7900       /* If we're looking at a `}', then we've run out of statements.  */
7901       if (token->type == CPP_CLOSE_BRACE
7902           || token->type == CPP_EOF
7903           || token->type == CPP_PRAGMA_EOL)
7904         break;
7905       
7906       /* If we are in a compound statement and find 'else' then
7907          something went wrong.  */
7908       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7909         {
7910           if (parser->in_statement & IN_IF_STMT) 
7911             break;
7912           else
7913             {
7914               token = cp_lexer_consume_token (parser->lexer);
7915               error_at (token->location, "%<else%> without a previous %<if%>");
7916             }
7917         }
7918
7919       /* Parse the statement.  */
7920       cp_parser_statement (parser, in_statement_expr, true, NULL);
7921     }
7922 }
7923
7924 /* Parse a selection-statement.
7925
7926    selection-statement:
7927      if ( condition ) statement
7928      if ( condition ) statement else statement
7929      switch ( condition ) statement
7930
7931    Returns the new IF_STMT or SWITCH_STMT.
7932
7933    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7934    is a (possibly labeled) if statement which is not enclosed in
7935    braces and has an else clause.  This is used to implement
7936    -Wparentheses.  */
7937
7938 static tree
7939 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7940 {
7941   cp_token *token;
7942   enum rid keyword;
7943
7944   if (if_p != NULL)
7945     *if_p = false;
7946
7947   /* Peek at the next token.  */
7948   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7949
7950   /* See what kind of keyword it is.  */
7951   keyword = token->keyword;
7952   switch (keyword)
7953     {
7954     case RID_IF:
7955     case RID_SWITCH:
7956       {
7957         tree statement;
7958         tree condition;
7959
7960         /* Look for the `('.  */
7961         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7962           {
7963             cp_parser_skip_to_end_of_statement (parser);
7964             return error_mark_node;
7965           }
7966
7967         /* Begin the selection-statement.  */
7968         if (keyword == RID_IF)
7969           statement = begin_if_stmt ();
7970         else
7971           statement = begin_switch_stmt ();
7972
7973         /* Parse the condition.  */
7974         condition = cp_parser_condition (parser);
7975         /* Look for the `)'.  */
7976         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7977           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7978                                                  /*consume_paren=*/true);
7979
7980         if (keyword == RID_IF)
7981           {
7982             bool nested_if;
7983             unsigned char in_statement;
7984
7985             /* Add the condition.  */
7986             finish_if_stmt_cond (condition, statement);
7987
7988             /* Parse the then-clause.  */
7989             in_statement = parser->in_statement;
7990             parser->in_statement |= IN_IF_STMT;
7991             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7992               {
7993                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7994                 add_stmt (build_empty_stmt (loc));
7995                 cp_lexer_consume_token (parser->lexer);
7996                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7997                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7998                               "empty body in an %<if%> statement");
7999                 nested_if = false;
8000               }
8001             else
8002               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8003             parser->in_statement = in_statement;
8004
8005             finish_then_clause (statement);
8006
8007             /* If the next token is `else', parse the else-clause.  */
8008             if (cp_lexer_next_token_is_keyword (parser->lexer,
8009                                                 RID_ELSE))
8010               {
8011                 /* Consume the `else' keyword.  */
8012                 cp_lexer_consume_token (parser->lexer);
8013                 begin_else_clause (statement);
8014                 /* Parse the else-clause.  */
8015                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8016                   {
8017                     location_t loc;
8018                     loc = cp_lexer_peek_token (parser->lexer)->location;
8019                     warning_at (loc,
8020                                 OPT_Wempty_body, "suggest braces around "
8021                                 "empty body in an %<else%> statement");
8022                     add_stmt (build_empty_stmt (loc));
8023                     cp_lexer_consume_token (parser->lexer);
8024                   }
8025                 else
8026                   cp_parser_implicitly_scoped_statement (parser, NULL);
8027
8028                 finish_else_clause (statement);
8029
8030                 /* If we are currently parsing a then-clause, then
8031                    IF_P will not be NULL.  We set it to true to
8032                    indicate that this if statement has an else clause.
8033                    This may trigger the Wparentheses warning below
8034                    when we get back up to the parent if statement.  */
8035                 if (if_p != NULL)
8036                   *if_p = true;
8037               }
8038             else
8039               {
8040                 /* This if statement does not have an else clause.  If
8041                    NESTED_IF is true, then the then-clause is an if
8042                    statement which does have an else clause.  We warn
8043                    about the potential ambiguity.  */
8044                 if (nested_if)
8045                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8046                               "suggest explicit braces to avoid ambiguous"
8047                               " %<else%>");
8048               }
8049
8050             /* Now we're all done with the if-statement.  */
8051             finish_if_stmt (statement);
8052           }
8053         else
8054           {
8055             bool in_switch_statement_p;
8056             unsigned char in_statement;
8057
8058             /* Add the condition.  */
8059             finish_switch_cond (condition, statement);
8060
8061             /* Parse the body of the switch-statement.  */
8062             in_switch_statement_p = parser->in_switch_statement_p;
8063             in_statement = parser->in_statement;
8064             parser->in_switch_statement_p = true;
8065             parser->in_statement |= IN_SWITCH_STMT;
8066             cp_parser_implicitly_scoped_statement (parser, NULL);
8067             parser->in_switch_statement_p = in_switch_statement_p;
8068             parser->in_statement = in_statement;
8069
8070             /* Now we're all done with the switch-statement.  */
8071             finish_switch_stmt (statement);
8072           }
8073
8074         return statement;
8075       }
8076       break;
8077
8078     default:
8079       cp_parser_error (parser, "expected selection-statement");
8080       return error_mark_node;
8081     }
8082 }
8083
8084 /* Parse a condition.
8085
8086    condition:
8087      expression
8088      type-specifier-seq declarator = initializer-clause
8089      type-specifier-seq declarator braced-init-list
8090
8091    GNU Extension:
8092
8093    condition:
8094      type-specifier-seq declarator asm-specification [opt]
8095        attributes [opt] = assignment-expression
8096
8097    Returns the expression that should be tested.  */
8098
8099 static tree
8100 cp_parser_condition (cp_parser* parser)
8101 {
8102   cp_decl_specifier_seq type_specifiers;
8103   const char *saved_message;
8104
8105   /* Try the declaration first.  */
8106   cp_parser_parse_tentatively (parser);
8107   /* New types are not allowed in the type-specifier-seq for a
8108      condition.  */
8109   saved_message = parser->type_definition_forbidden_message;
8110   parser->type_definition_forbidden_message
8111     = G_("types may not be defined in conditions");
8112   /* Parse the type-specifier-seq.  */
8113   cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8114                                 /*is_trailing_return=*/false,
8115                                 &type_specifiers);
8116   /* Restore the saved message.  */
8117   parser->type_definition_forbidden_message = saved_message;
8118   /* If all is well, we might be looking at a declaration.  */
8119   if (!cp_parser_error_occurred (parser))
8120     {
8121       tree decl;
8122       tree asm_specification;
8123       tree attributes;
8124       cp_declarator *declarator;
8125       tree initializer = NULL_TREE;
8126
8127       /* Parse the declarator.  */
8128       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8129                                          /*ctor_dtor_or_conv_p=*/NULL,
8130                                          /*parenthesized_p=*/NULL,
8131                                          /*member_p=*/false);
8132       /* Parse the attributes.  */
8133       attributes = cp_parser_attributes_opt (parser);
8134       /* Parse the asm-specification.  */
8135       asm_specification = cp_parser_asm_specification_opt (parser);
8136       /* If the next token is not an `=' or '{', then we might still be
8137          looking at an expression.  For example:
8138
8139            if (A(a).x)
8140
8141          looks like a decl-specifier-seq and a declarator -- but then
8142          there is no `=', so this is an expression.  */
8143       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8144           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8145         cp_parser_simulate_error (parser);
8146         
8147       /* If we did see an `=' or '{', then we are looking at a declaration
8148          for sure.  */
8149       if (cp_parser_parse_definitely (parser))
8150         {
8151           tree pushed_scope;
8152           bool non_constant_p;
8153           bool flags = LOOKUP_ONLYCONVERTING;
8154
8155           /* Create the declaration.  */
8156           decl = start_decl (declarator, &type_specifiers,
8157                              /*initialized_p=*/true,
8158                              attributes, /*prefix_attributes=*/NULL_TREE,
8159                              &pushed_scope);
8160
8161           /* Parse the initializer.  */
8162           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8163             {
8164               initializer = cp_parser_braced_list (parser, &non_constant_p);
8165               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8166               flags = 0;
8167             }
8168           else
8169             {
8170               /* Consume the `='.  */
8171               cp_parser_require (parser, CPP_EQ, "%<=%>");
8172               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8173             }
8174           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8175             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8176
8177           if (!non_constant_p)
8178             initializer = fold_non_dependent_expr (initializer);
8179
8180           /* Process the initializer.  */
8181           cp_finish_decl (decl,
8182                           initializer, !non_constant_p,
8183                           asm_specification,
8184                           flags);
8185
8186           if (pushed_scope)
8187             pop_scope (pushed_scope);
8188
8189           return convert_from_reference (decl);
8190         }
8191     }
8192   /* If we didn't even get past the declarator successfully, we are
8193      definitely not looking at a declaration.  */
8194   else
8195     cp_parser_abort_tentative_parse (parser);
8196
8197   /* Otherwise, we are looking at an expression.  */
8198   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8199 }
8200
8201 /* Parse an iteration-statement.
8202
8203    iteration-statement:
8204      while ( condition ) statement
8205      do statement while ( expression ) ;
8206      for ( for-init-statement condition [opt] ; expression [opt] )
8207        statement
8208
8209    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
8210
8211 static tree
8212 cp_parser_iteration_statement (cp_parser* parser)
8213 {
8214   cp_token *token;
8215   enum rid keyword;
8216   tree statement;
8217   unsigned char in_statement;
8218
8219   /* Peek at the next token.  */
8220   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8221   if (!token)
8222     return error_mark_node;
8223
8224   /* Remember whether or not we are already within an iteration
8225      statement.  */
8226   in_statement = parser->in_statement;
8227
8228   /* See what kind of keyword it is.  */
8229   keyword = token->keyword;
8230   switch (keyword)
8231     {
8232     case RID_WHILE:
8233       {
8234         tree condition;
8235
8236         /* Begin the while-statement.  */
8237         statement = begin_while_stmt ();
8238         /* Look for the `('.  */
8239         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8240         /* Parse the condition.  */
8241         condition = cp_parser_condition (parser);
8242         finish_while_stmt_cond (condition, statement);
8243         /* Look for the `)'.  */
8244         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8245         /* Parse the dependent statement.  */
8246         parser->in_statement = IN_ITERATION_STMT;
8247         cp_parser_already_scoped_statement (parser);
8248         parser->in_statement = in_statement;
8249         /* We're done with the while-statement.  */
8250         finish_while_stmt (statement);
8251       }
8252       break;
8253
8254     case RID_DO:
8255       {
8256         tree expression;
8257
8258         /* Begin the do-statement.  */
8259         statement = begin_do_stmt ();
8260         /* Parse the body of the do-statement.  */
8261         parser->in_statement = IN_ITERATION_STMT;
8262         cp_parser_implicitly_scoped_statement (parser, NULL);
8263         parser->in_statement = in_statement;
8264         finish_do_body (statement);
8265         /* Look for the `while' keyword.  */
8266         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8267         /* Look for the `('.  */
8268         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8269         /* Parse the expression.  */
8270         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8271         /* We're done with the do-statement.  */
8272         finish_do_stmt (expression, statement);
8273         /* Look for the `)'.  */
8274         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8275         /* Look for the `;'.  */
8276         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8277       }
8278       break;
8279
8280     case RID_FOR:
8281       {
8282         tree condition = NULL_TREE;
8283         tree expression = NULL_TREE;
8284
8285         /* Begin the for-statement.  */
8286         statement = begin_for_stmt ();
8287         /* Look for the `('.  */
8288         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8289         /* Parse the initialization.  */
8290         cp_parser_for_init_statement (parser);
8291         finish_for_init_stmt (statement);
8292
8293         /* If there's a condition, process it.  */
8294         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8295           condition = cp_parser_condition (parser);
8296         finish_for_cond (condition, statement);
8297         /* Look for the `;'.  */
8298         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8299
8300         /* If there's an expression, process it.  */
8301         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8302           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8303         finish_for_expr (expression, statement);
8304         /* Look for the `)'.  */
8305         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8306
8307         /* Parse the body of the for-statement.  */
8308         parser->in_statement = IN_ITERATION_STMT;
8309         cp_parser_already_scoped_statement (parser);
8310         parser->in_statement = in_statement;
8311
8312         /* We're done with the for-statement.  */
8313         finish_for_stmt (statement);
8314       }
8315       break;
8316
8317     default:
8318       cp_parser_error (parser, "expected iteration-statement");
8319       statement = error_mark_node;
8320       break;
8321     }
8322
8323   return statement;
8324 }
8325
8326 /* Parse a for-init-statement.
8327
8328    for-init-statement:
8329      expression-statement
8330      simple-declaration  */
8331
8332 static void
8333 cp_parser_for_init_statement (cp_parser* parser)
8334 {
8335   /* If the next token is a `;', then we have an empty
8336      expression-statement.  Grammatically, this is also a
8337      simple-declaration, but an invalid one, because it does not
8338      declare anything.  Therefore, if we did not handle this case
8339      specially, we would issue an error message about an invalid
8340      declaration.  */
8341   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8342     {
8343       /* We're going to speculatively look for a declaration, falling back
8344          to an expression, if necessary.  */
8345       cp_parser_parse_tentatively (parser);
8346       /* Parse the declaration.  */
8347       cp_parser_simple_declaration (parser,
8348                                     /*function_definition_allowed_p=*/false);
8349       /* If the tentative parse failed, then we shall need to look for an
8350          expression-statement.  */
8351       if (cp_parser_parse_definitely (parser))
8352         return;
8353     }
8354
8355   cp_parser_expression_statement (parser, false);
8356 }
8357
8358 /* Parse a jump-statement.
8359
8360    jump-statement:
8361      break ;
8362      continue ;
8363      return expression [opt] ;
8364      return braced-init-list ;
8365      goto identifier ;
8366
8367    GNU extension:
8368
8369    jump-statement:
8370      goto * expression ;
8371
8372    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
8373
8374 static tree
8375 cp_parser_jump_statement (cp_parser* parser)
8376 {
8377   tree statement = error_mark_node;
8378   cp_token *token;
8379   enum rid keyword;
8380   unsigned char in_statement;
8381
8382   /* Peek at the next token.  */
8383   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8384   if (!token)
8385     return error_mark_node;
8386
8387   /* See what kind of keyword it is.  */
8388   keyword = token->keyword;
8389   switch (keyword)
8390     {
8391     case RID_BREAK:
8392       in_statement = parser->in_statement & ~IN_IF_STMT;      
8393       switch (in_statement)
8394         {
8395         case 0:
8396           error_at (token->location, "break statement not within loop or switch");
8397           break;
8398         default:
8399           gcc_assert ((in_statement & IN_SWITCH_STMT)
8400                       || in_statement == IN_ITERATION_STMT);
8401           statement = finish_break_stmt ();
8402           break;
8403         case IN_OMP_BLOCK:
8404           error_at (token->location, "invalid exit from OpenMP structured block");
8405           break;
8406         case IN_OMP_FOR:
8407           error_at (token->location, "break statement used with OpenMP for loop");
8408           break;
8409         }
8410       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8411       break;
8412
8413     case RID_CONTINUE:
8414       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8415         {
8416         case 0:
8417           error_at (token->location, "continue statement not within a loop");
8418           break;
8419         case IN_ITERATION_STMT:
8420         case IN_OMP_FOR:
8421           statement = finish_continue_stmt ();
8422           break;
8423         case IN_OMP_BLOCK:
8424           error_at (token->location, "invalid exit from OpenMP structured block");
8425           break;
8426         default:
8427           gcc_unreachable ();
8428         }
8429       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8430       break;
8431
8432     case RID_RETURN:
8433       {
8434         tree expr;
8435         bool expr_non_constant_p;
8436
8437         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8438           {
8439             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8440             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8441           }
8442         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8443           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8444         else
8445           /* If the next token is a `;', then there is no
8446              expression.  */
8447           expr = NULL_TREE;
8448         /* Build the return-statement.  */
8449         statement = finish_return_stmt (expr);
8450         /* Look for the final `;'.  */
8451         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8452       }
8453       break;
8454
8455     case RID_GOTO:
8456       /* Create the goto-statement.  */
8457       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8458         {
8459           /* Issue a warning about this use of a GNU extension.  */
8460           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8461           /* Consume the '*' token.  */
8462           cp_lexer_consume_token (parser->lexer);
8463           /* Parse the dependent expression.  */
8464           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8465         }
8466       else
8467         finish_goto_stmt (cp_parser_identifier (parser));
8468       /* Look for the final `;'.  */
8469       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8470       break;
8471
8472     default:
8473       cp_parser_error (parser, "expected jump-statement");
8474       break;
8475     }
8476
8477   return statement;
8478 }
8479
8480 /* Parse a declaration-statement.
8481
8482    declaration-statement:
8483      block-declaration  */
8484
8485 static void
8486 cp_parser_declaration_statement (cp_parser* parser)
8487 {
8488   void *p;
8489
8490   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8491   p = obstack_alloc (&declarator_obstack, 0);
8492
8493  /* Parse the block-declaration.  */
8494   cp_parser_block_declaration (parser, /*statement_p=*/true);
8495
8496   /* Free any declarators allocated.  */
8497   obstack_free (&declarator_obstack, p);
8498
8499   /* Finish off the statement.  */
8500   finish_stmt ();
8501 }
8502
8503 /* Some dependent statements (like `if (cond) statement'), are
8504    implicitly in their own scope.  In other words, if the statement is
8505    a single statement (as opposed to a compound-statement), it is
8506    none-the-less treated as if it were enclosed in braces.  Any
8507    declarations appearing in the dependent statement are out of scope
8508    after control passes that point.  This function parses a statement,
8509    but ensures that is in its own scope, even if it is not a
8510    compound-statement.
8511
8512    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8513    is a (possibly labeled) if statement which is not enclosed in
8514    braces and has an else clause.  This is used to implement
8515    -Wparentheses.
8516
8517    Returns the new statement.  */
8518
8519 static tree
8520 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8521 {
8522   tree statement;
8523
8524   if (if_p != NULL)
8525     *if_p = false;
8526
8527   /* Mark if () ; with a special NOP_EXPR.  */
8528   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8529     {
8530       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8531       cp_lexer_consume_token (parser->lexer);
8532       statement = add_stmt (build_empty_stmt (loc));
8533     }
8534   /* if a compound is opened, we simply parse the statement directly.  */
8535   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8536     statement = cp_parser_compound_statement (parser, NULL, false);
8537   /* If the token is not a `{', then we must take special action.  */
8538   else
8539     {
8540       /* Create a compound-statement.  */
8541       statement = begin_compound_stmt (0);
8542       /* Parse the dependent-statement.  */
8543       cp_parser_statement (parser, NULL_TREE, false, if_p);
8544       /* Finish the dummy compound-statement.  */
8545       finish_compound_stmt (statement);
8546     }
8547
8548   /* Return the statement.  */
8549   return statement;
8550 }
8551
8552 /* For some dependent statements (like `while (cond) statement'), we
8553    have already created a scope.  Therefore, even if the dependent
8554    statement is a compound-statement, we do not want to create another
8555    scope.  */
8556
8557 static void
8558 cp_parser_already_scoped_statement (cp_parser* parser)
8559 {
8560   /* If the token is a `{', then we must take special action.  */
8561   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8562     cp_parser_statement (parser, NULL_TREE, false, NULL);
8563   else
8564     {
8565       /* Avoid calling cp_parser_compound_statement, so that we
8566          don't create a new scope.  Do everything else by hand.  */
8567       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8568       /* If the next keyword is `__label__' we have a label declaration.  */
8569       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8570         cp_parser_label_declaration (parser);
8571       /* Parse an (optional) statement-seq.  */
8572       cp_parser_statement_seq_opt (parser, NULL_TREE);
8573       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8574     }
8575 }
8576
8577 /* Declarations [gram.dcl.dcl] */
8578
8579 /* Parse an optional declaration-sequence.
8580
8581    declaration-seq:
8582      declaration
8583      declaration-seq declaration  */
8584
8585 static void
8586 cp_parser_declaration_seq_opt (cp_parser* parser)
8587 {
8588   while (true)
8589     {
8590       cp_token *token;
8591
8592       token = cp_lexer_peek_token (parser->lexer);
8593
8594       if (token->type == CPP_CLOSE_BRACE
8595           || token->type == CPP_EOF
8596           || token->type == CPP_PRAGMA_EOL)
8597         break;
8598
8599       if (token->type == CPP_SEMICOLON)
8600         {
8601           /* A declaration consisting of a single semicolon is
8602              invalid.  Allow it unless we're being pedantic.  */
8603           cp_lexer_consume_token (parser->lexer);
8604           if (!in_system_header)
8605             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8606           continue;
8607         }
8608
8609       /* If we're entering or exiting a region that's implicitly
8610          extern "C", modify the lang context appropriately.  */
8611       if (!parser->implicit_extern_c && token->implicit_extern_c)
8612         {
8613           push_lang_context (lang_name_c);
8614           parser->implicit_extern_c = true;
8615         }
8616       else if (parser->implicit_extern_c && !token->implicit_extern_c)
8617         {
8618           pop_lang_context ();
8619           parser->implicit_extern_c = false;
8620         }
8621
8622       if (token->type == CPP_PRAGMA)
8623         {
8624           /* A top-level declaration can consist solely of a #pragma.
8625              A nested declaration cannot, so this is done here and not
8626              in cp_parser_declaration.  (A #pragma at block scope is
8627              handled in cp_parser_statement.)  */
8628           cp_parser_pragma (parser, pragma_external);
8629           continue;
8630         }
8631
8632       /* Parse the declaration itself.  */
8633       cp_parser_declaration (parser);
8634     }
8635 }
8636
8637 /* Parse a declaration.
8638
8639    declaration:
8640      block-declaration
8641      function-definition
8642      template-declaration
8643      explicit-instantiation
8644      explicit-specialization
8645      linkage-specification
8646      namespace-definition
8647
8648    GNU extension:
8649
8650    declaration:
8651       __extension__ declaration */
8652
8653 static void
8654 cp_parser_declaration (cp_parser* parser)
8655 {
8656   cp_token token1;
8657   cp_token token2;
8658   int saved_pedantic;
8659   void *p;
8660
8661   /* Check for the `__extension__' keyword.  */
8662   if (cp_parser_extension_opt (parser, &saved_pedantic))
8663     {
8664       /* Parse the qualified declaration.  */
8665       cp_parser_declaration (parser);
8666       /* Restore the PEDANTIC flag.  */
8667       pedantic = saved_pedantic;
8668
8669       return;
8670     }
8671
8672   /* Try to figure out what kind of declaration is present.  */
8673   token1 = *cp_lexer_peek_token (parser->lexer);
8674
8675   if (token1.type != CPP_EOF)
8676     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8677   else
8678     {
8679       token2.type = CPP_EOF;
8680       token2.keyword = RID_MAX;
8681     }
8682
8683   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8684   p = obstack_alloc (&declarator_obstack, 0);
8685
8686   /* If the next token is `extern' and the following token is a string
8687      literal, then we have a linkage specification.  */
8688   if (token1.keyword == RID_EXTERN
8689       && cp_parser_is_string_literal (&token2))
8690     cp_parser_linkage_specification (parser);
8691   /* If the next token is `template', then we have either a template
8692      declaration, an explicit instantiation, or an explicit
8693      specialization.  */
8694   else if (token1.keyword == RID_TEMPLATE)
8695     {
8696       /* `template <>' indicates a template specialization.  */
8697       if (token2.type == CPP_LESS
8698           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8699         cp_parser_explicit_specialization (parser);
8700       /* `template <' indicates a template declaration.  */
8701       else if (token2.type == CPP_LESS)
8702         cp_parser_template_declaration (parser, /*member_p=*/false);
8703       /* Anything else must be an explicit instantiation.  */
8704       else
8705         cp_parser_explicit_instantiation (parser);
8706     }
8707   /* If the next token is `export', then we have a template
8708      declaration.  */
8709   else if (token1.keyword == RID_EXPORT)
8710     cp_parser_template_declaration (parser, /*member_p=*/false);
8711   /* If the next token is `extern', 'static' or 'inline' and the one
8712      after that is `template', we have a GNU extended explicit
8713      instantiation directive.  */
8714   else if (cp_parser_allow_gnu_extensions_p (parser)
8715            && (token1.keyword == RID_EXTERN
8716                || token1.keyword == RID_STATIC
8717                || token1.keyword == RID_INLINE)
8718            && token2.keyword == RID_TEMPLATE)
8719     cp_parser_explicit_instantiation (parser);
8720   /* If the next token is `namespace', check for a named or unnamed
8721      namespace definition.  */
8722   else if (token1.keyword == RID_NAMESPACE
8723            && (/* A named namespace definition.  */
8724                (token2.type == CPP_NAME
8725                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8726                     != CPP_EQ))
8727                /* An unnamed namespace definition.  */
8728                || token2.type == CPP_OPEN_BRACE
8729                || token2.keyword == RID_ATTRIBUTE))
8730     cp_parser_namespace_definition (parser);
8731   /* An inline (associated) namespace definition.  */
8732   else if (token1.keyword == RID_INLINE
8733            && token2.keyword == RID_NAMESPACE)
8734     cp_parser_namespace_definition (parser);
8735   /* Objective-C++ declaration/definition.  */
8736   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8737     cp_parser_objc_declaration (parser);
8738   /* We must have either a block declaration or a function
8739      definition.  */
8740   else
8741     /* Try to parse a block-declaration, or a function-definition.  */
8742     cp_parser_block_declaration (parser, /*statement_p=*/false);
8743
8744   /* Free any declarators allocated.  */
8745   obstack_free (&declarator_obstack, p);
8746 }
8747
8748 /* Parse a block-declaration.
8749
8750    block-declaration:
8751      simple-declaration
8752      asm-definition
8753      namespace-alias-definition
8754      using-declaration
8755      using-directive
8756
8757    GNU Extension:
8758
8759    block-declaration:
8760      __extension__ block-declaration
8761
8762    C++0x Extension:
8763
8764    block-declaration:
8765      static_assert-declaration
8766
8767    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8768    part of a declaration-statement.  */
8769
8770 static void
8771 cp_parser_block_declaration (cp_parser *parser,
8772                              bool      statement_p)
8773 {
8774   cp_token *token1;
8775   int saved_pedantic;
8776
8777   /* Check for the `__extension__' keyword.  */
8778   if (cp_parser_extension_opt (parser, &saved_pedantic))
8779     {
8780       /* Parse the qualified declaration.  */
8781       cp_parser_block_declaration (parser, statement_p);
8782       /* Restore the PEDANTIC flag.  */
8783       pedantic = saved_pedantic;
8784
8785       return;
8786     }
8787
8788   /* Peek at the next token to figure out which kind of declaration is
8789      present.  */
8790   token1 = cp_lexer_peek_token (parser->lexer);
8791
8792   /* If the next keyword is `asm', we have an asm-definition.  */
8793   if (token1->keyword == RID_ASM)
8794     {
8795       if (statement_p)
8796         cp_parser_commit_to_tentative_parse (parser);
8797       cp_parser_asm_definition (parser);
8798     }
8799   /* If the next keyword is `namespace', we have a
8800      namespace-alias-definition.  */
8801   else if (token1->keyword == RID_NAMESPACE)
8802     cp_parser_namespace_alias_definition (parser);
8803   /* If the next keyword is `using', we have either a
8804      using-declaration or a using-directive.  */
8805   else if (token1->keyword == RID_USING)
8806     {
8807       cp_token *token2;
8808
8809       if (statement_p)
8810         cp_parser_commit_to_tentative_parse (parser);
8811       /* If the token after `using' is `namespace', then we have a
8812          using-directive.  */
8813       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8814       if (token2->keyword == RID_NAMESPACE)
8815         cp_parser_using_directive (parser);
8816       /* Otherwise, it's a using-declaration.  */
8817       else
8818         cp_parser_using_declaration (parser,
8819                                      /*access_declaration_p=*/false);
8820     }
8821   /* If the next keyword is `__label__' we have a misplaced label
8822      declaration.  */
8823   else if (token1->keyword == RID_LABEL)
8824     {
8825       cp_lexer_consume_token (parser->lexer);
8826       error_at (token1->location, "%<__label__%> not at the beginning of a block");
8827       cp_parser_skip_to_end_of_statement (parser);
8828       /* If the next token is now a `;', consume it.  */
8829       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8830         cp_lexer_consume_token (parser->lexer);
8831     }
8832   /* If the next token is `static_assert' we have a static assertion.  */
8833   else if (token1->keyword == RID_STATIC_ASSERT)
8834     cp_parser_static_assert (parser, /*member_p=*/false);
8835   /* Anything else must be a simple-declaration.  */
8836   else
8837     cp_parser_simple_declaration (parser, !statement_p);
8838 }
8839
8840 /* Parse a simple-declaration.
8841
8842    simple-declaration:
8843      decl-specifier-seq [opt] init-declarator-list [opt] ;
8844
8845    init-declarator-list:
8846      init-declarator
8847      init-declarator-list , init-declarator
8848
8849    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8850    function-definition as a simple-declaration.  */
8851
8852 static void
8853 cp_parser_simple_declaration (cp_parser* parser,
8854                               bool function_definition_allowed_p)
8855 {
8856   cp_decl_specifier_seq decl_specifiers;
8857   int declares_class_or_enum;
8858   bool saw_declarator;
8859
8860   /* Defer access checks until we know what is being declared; the
8861      checks for names appearing in the decl-specifier-seq should be
8862      done as if we were in the scope of the thing being declared.  */
8863   push_deferring_access_checks (dk_deferred);
8864
8865   /* Parse the decl-specifier-seq.  We have to keep track of whether
8866      or not the decl-specifier-seq declares a named class or
8867      enumeration type, since that is the only case in which the
8868      init-declarator-list is allowed to be empty.
8869
8870      [dcl.dcl]
8871
8872      In a simple-declaration, the optional init-declarator-list can be
8873      omitted only when declaring a class or enumeration, that is when
8874      the decl-specifier-seq contains either a class-specifier, an
8875      elaborated-type-specifier, or an enum-specifier.  */
8876   cp_parser_decl_specifier_seq (parser,
8877                                 CP_PARSER_FLAGS_OPTIONAL,
8878                                 &decl_specifiers,
8879                                 &declares_class_or_enum);
8880   /* We no longer need to defer access checks.  */
8881   stop_deferring_access_checks ();
8882
8883   /* In a block scope, a valid declaration must always have a
8884      decl-specifier-seq.  By not trying to parse declarators, we can
8885      resolve the declaration/expression ambiguity more quickly.  */
8886   if (!function_definition_allowed_p
8887       && !decl_specifiers.any_specifiers_p)
8888     {
8889       cp_parser_error (parser, "expected declaration");
8890       goto done;
8891     }
8892
8893   /* If the next two tokens are both identifiers, the code is
8894      erroneous. The usual cause of this situation is code like:
8895
8896        T t;
8897
8898      where "T" should name a type -- but does not.  */
8899   if (!decl_specifiers.any_type_specifiers_p
8900       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8901     {
8902       /* If parsing tentatively, we should commit; we really are
8903          looking at a declaration.  */
8904       cp_parser_commit_to_tentative_parse (parser);
8905       /* Give up.  */
8906       goto done;
8907     }
8908
8909   /* If we have seen at least one decl-specifier, and the next token
8910      is not a parenthesis, then we must be looking at a declaration.
8911      (After "int (" we might be looking at a functional cast.)  */
8912   if (decl_specifiers.any_specifiers_p
8913       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8914       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8915       && !cp_parser_error_occurred (parser))
8916     cp_parser_commit_to_tentative_parse (parser);
8917
8918   /* Keep going until we hit the `;' at the end of the simple
8919      declaration.  */
8920   saw_declarator = false;
8921   while (cp_lexer_next_token_is_not (parser->lexer,
8922                                      CPP_SEMICOLON))
8923     {
8924       cp_token *token;
8925       bool function_definition_p;
8926       tree decl;
8927
8928       if (saw_declarator)
8929         {
8930           /* If we are processing next declarator, coma is expected */
8931           token = cp_lexer_peek_token (parser->lexer);
8932           gcc_assert (token->type == CPP_COMMA);
8933           cp_lexer_consume_token (parser->lexer);
8934         }
8935       else
8936         saw_declarator = true;
8937
8938       /* Parse the init-declarator.  */
8939       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8940                                         /*checks=*/NULL,
8941                                         function_definition_allowed_p,
8942                                         /*member_p=*/false,
8943                                         declares_class_or_enum,
8944                                         &function_definition_p);
8945       /* If an error occurred while parsing tentatively, exit quickly.
8946          (That usually happens when in the body of a function; each
8947          statement is treated as a declaration-statement until proven
8948          otherwise.)  */
8949       if (cp_parser_error_occurred (parser))
8950         goto done;
8951       /* Handle function definitions specially.  */
8952       if (function_definition_p)
8953         {
8954           /* If the next token is a `,', then we are probably
8955              processing something like:
8956
8957                void f() {}, *p;
8958
8959              which is erroneous.  */
8960           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8961             {
8962               cp_token *token = cp_lexer_peek_token (parser->lexer);
8963               error_at (token->location,
8964                         "mixing"
8965                         " declarations and function-definitions is forbidden");
8966             }
8967           /* Otherwise, we're done with the list of declarators.  */
8968           else
8969             {
8970               pop_deferring_access_checks ();
8971               return;
8972             }
8973         }
8974       /* The next token should be either a `,' or a `;'.  */
8975       token = cp_lexer_peek_token (parser->lexer);
8976       /* If it's a `,', there are more declarators to come.  */
8977       if (token->type == CPP_COMMA)
8978         /* will be consumed next time around */;
8979       /* If it's a `;', we are done.  */
8980       else if (token->type == CPP_SEMICOLON)
8981         break;
8982       /* Anything else is an error.  */
8983       else
8984         {
8985           /* If we have already issued an error message we don't need
8986              to issue another one.  */
8987           if (decl != error_mark_node
8988               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8989             cp_parser_error (parser, "expected %<,%> or %<;%>");
8990           /* Skip tokens until we reach the end of the statement.  */
8991           cp_parser_skip_to_end_of_statement (parser);
8992           /* If the next token is now a `;', consume it.  */
8993           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8994             cp_lexer_consume_token (parser->lexer);
8995           goto done;
8996         }
8997       /* After the first time around, a function-definition is not
8998          allowed -- even if it was OK at first.  For example:
8999
9000            int i, f() {}
9001
9002          is not valid.  */
9003       function_definition_allowed_p = false;
9004     }
9005
9006   /* Issue an error message if no declarators are present, and the
9007      decl-specifier-seq does not itself declare a class or
9008      enumeration.  */
9009   if (!saw_declarator)
9010     {
9011       if (cp_parser_declares_only_class_p (parser))
9012         shadow_tag (&decl_specifiers);
9013       /* Perform any deferred access checks.  */
9014       perform_deferred_access_checks ();
9015     }
9016
9017   /* Consume the `;'.  */
9018   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9019
9020  done:
9021   pop_deferring_access_checks ();
9022 }
9023
9024 /* Parse a decl-specifier-seq.
9025
9026    decl-specifier-seq:
9027      decl-specifier-seq [opt] decl-specifier
9028
9029    decl-specifier:
9030      storage-class-specifier
9031      type-specifier
9032      function-specifier
9033      friend
9034      typedef
9035
9036    GNU Extension:
9037
9038    decl-specifier:
9039      attributes
9040
9041    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9042
9043    The parser flags FLAGS is used to control type-specifier parsing.
9044
9045    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9046    flags:
9047
9048      1: one of the decl-specifiers is an elaborated-type-specifier
9049         (i.e., a type declaration)
9050      2: one of the decl-specifiers is an enum-specifier or a
9051         class-specifier (i.e., a type definition)
9052
9053    */
9054
9055 static void
9056 cp_parser_decl_specifier_seq (cp_parser* parser,
9057                               cp_parser_flags flags,
9058                               cp_decl_specifier_seq *decl_specs,
9059                               int* declares_class_or_enum)
9060 {
9061   bool constructor_possible_p = !parser->in_declarator_p;
9062   cp_token *start_token = NULL;
9063
9064   /* Clear DECL_SPECS.  */
9065   clear_decl_specs (decl_specs);
9066
9067   /* Assume no class or enumeration type is declared.  */
9068   *declares_class_or_enum = 0;
9069
9070   /* Keep reading specifiers until there are no more to read.  */
9071   while (true)
9072     {
9073       bool constructor_p;
9074       bool found_decl_spec;
9075       cp_token *token;
9076
9077       /* Peek at the next token.  */
9078       token = cp_lexer_peek_token (parser->lexer);
9079
9080       /* Save the first token of the decl spec list for error
9081          reporting.  */
9082       if (!start_token)
9083         start_token = token;
9084       /* Handle attributes.  */
9085       if (token->keyword == RID_ATTRIBUTE)
9086         {
9087           /* Parse the attributes.  */
9088           decl_specs->attributes
9089             = chainon (decl_specs->attributes,
9090                        cp_parser_attributes_opt (parser));
9091           continue;
9092         }
9093       /* Assume we will find a decl-specifier keyword.  */
9094       found_decl_spec = true;
9095       /* If the next token is an appropriate keyword, we can simply
9096          add it to the list.  */
9097       switch (token->keyword)
9098         {
9099           /* decl-specifier:
9100                friend
9101                constexpr */
9102         case RID_FRIEND:
9103           if (!at_class_scope_p ())
9104             {
9105               error_at (token->location, "%<friend%> used outside of class");
9106               cp_lexer_purge_token (parser->lexer);
9107             }
9108           else
9109             {
9110               ++decl_specs->specs[(int) ds_friend];
9111               /* Consume the token.  */
9112               cp_lexer_consume_token (parser->lexer);
9113             }
9114           break;
9115
9116         case RID_CONSTEXPR:
9117           ++decl_specs->specs[(int) ds_constexpr];
9118           cp_lexer_consume_token (parser->lexer);
9119           break;
9120
9121           /* function-specifier:
9122                inline
9123                virtual
9124                explicit  */
9125         case RID_INLINE:
9126         case RID_VIRTUAL:
9127         case RID_EXPLICIT:
9128           cp_parser_function_specifier_opt (parser, decl_specs);
9129           break;
9130
9131           /* decl-specifier:
9132                typedef  */
9133         case RID_TYPEDEF:
9134           ++decl_specs->specs[(int) ds_typedef];
9135           /* Consume the token.  */
9136           cp_lexer_consume_token (parser->lexer);
9137           /* A constructor declarator cannot appear in a typedef.  */
9138           constructor_possible_p = false;
9139           /* The "typedef" keyword can only occur in a declaration; we
9140              may as well commit at this point.  */
9141           cp_parser_commit_to_tentative_parse (parser);
9142
9143           if (decl_specs->storage_class != sc_none)
9144             decl_specs->conflicting_specifiers_p = true;
9145           break;
9146
9147           /* storage-class-specifier:
9148                auto
9149                register
9150                static
9151                extern
9152                mutable
9153
9154              GNU Extension:
9155                thread  */
9156         case RID_AUTO:
9157           if (cxx_dialect == cxx98) 
9158             {
9159               /* Consume the token.  */
9160               cp_lexer_consume_token (parser->lexer);
9161
9162               /* Complain about `auto' as a storage specifier, if
9163                  we're complaining about C++0x compatibility.  */
9164               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9165                           " will change meaning in C++0x; please remove it");
9166
9167               /* Set the storage class anyway.  */
9168               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9169                                            token->location);
9170             }
9171           else
9172             /* C++0x auto type-specifier.  */
9173             found_decl_spec = false;
9174           break;
9175
9176         case RID_REGISTER:
9177         case RID_STATIC:
9178         case RID_EXTERN:
9179         case RID_MUTABLE:
9180           /* Consume the token.  */
9181           cp_lexer_consume_token (parser->lexer);
9182           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9183                                        token->location);
9184           break;
9185         case RID_THREAD:
9186           /* Consume the token.  */
9187           cp_lexer_consume_token (parser->lexer);
9188           ++decl_specs->specs[(int) ds_thread];
9189           break;
9190
9191         default:
9192           /* We did not yet find a decl-specifier yet.  */
9193           found_decl_spec = false;
9194           break;
9195         }
9196
9197       /* Constructors are a special case.  The `S' in `S()' is not a
9198          decl-specifier; it is the beginning of the declarator.  */
9199       constructor_p
9200         = (!found_decl_spec
9201            && constructor_possible_p
9202            && (cp_parser_constructor_declarator_p
9203                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9204
9205       /* If we don't have a DECL_SPEC yet, then we must be looking at
9206          a type-specifier.  */
9207       if (!found_decl_spec && !constructor_p)
9208         {
9209           int decl_spec_declares_class_or_enum;
9210           bool is_cv_qualifier;
9211           tree type_spec;
9212
9213           type_spec
9214             = cp_parser_type_specifier (parser, flags,
9215                                         decl_specs,
9216                                         /*is_declaration=*/true,
9217                                         &decl_spec_declares_class_or_enum,
9218                                         &is_cv_qualifier);
9219           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9220
9221           /* If this type-specifier referenced a user-defined type
9222              (a typedef, class-name, etc.), then we can't allow any
9223              more such type-specifiers henceforth.
9224
9225              [dcl.spec]
9226
9227              The longest sequence of decl-specifiers that could
9228              possibly be a type name is taken as the
9229              decl-specifier-seq of a declaration.  The sequence shall
9230              be self-consistent as described below.
9231
9232              [dcl.type]
9233
9234              As a general rule, at most one type-specifier is allowed
9235              in the complete decl-specifier-seq of a declaration.  The
9236              only exceptions are the following:
9237
9238              -- const or volatile can be combined with any other
9239                 type-specifier.
9240
9241              -- signed or unsigned can be combined with char, long,
9242                 short, or int.
9243
9244              -- ..
9245
9246              Example:
9247
9248                typedef char* Pc;
9249                void g (const int Pc);
9250
9251              Here, Pc is *not* part of the decl-specifier seq; it's
9252              the declarator.  Therefore, once we see a type-specifier
9253              (other than a cv-qualifier), we forbid any additional
9254              user-defined types.  We *do* still allow things like `int
9255              int' to be considered a decl-specifier-seq, and issue the
9256              error message later.  */
9257           if (type_spec && !is_cv_qualifier)
9258             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9259           /* A constructor declarator cannot follow a type-specifier.  */
9260           if (type_spec)
9261             {
9262               constructor_possible_p = false;
9263               found_decl_spec = true;
9264               if (!is_cv_qualifier)
9265                 decl_specs->any_type_specifiers_p = true;
9266             }
9267         }
9268
9269       /* If we still do not have a DECL_SPEC, then there are no more
9270          decl-specifiers.  */
9271       if (!found_decl_spec)
9272         break;
9273
9274       decl_specs->any_specifiers_p = true;
9275       /* After we see one decl-specifier, further decl-specifiers are
9276          always optional.  */
9277       flags |= CP_PARSER_FLAGS_OPTIONAL;
9278     }
9279
9280   cp_parser_check_decl_spec (decl_specs, start_token->location);
9281
9282   /* Don't allow a friend specifier with a class definition.  */
9283   if (decl_specs->specs[(int) ds_friend] != 0
9284       && (*declares_class_or_enum & 2))
9285     error_at (start_token->location,
9286               "class definition may not be declared a friend");
9287 }
9288
9289 /* Parse an (optional) storage-class-specifier.
9290
9291    storage-class-specifier:
9292      auto
9293      register
9294      static
9295      extern
9296      mutable
9297
9298    GNU Extension:
9299
9300    storage-class-specifier:
9301      thread
9302
9303    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9304
9305 static tree
9306 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9307 {
9308   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9309     {
9310     case RID_AUTO:
9311       if (cxx_dialect != cxx98)
9312         return NULL_TREE;
9313       /* Fall through for C++98.  */
9314
9315     case RID_REGISTER:
9316     case RID_STATIC:
9317     case RID_EXTERN:
9318     case RID_MUTABLE:
9319     case RID_THREAD:
9320       /* Consume the token.  */
9321       return cp_lexer_consume_token (parser->lexer)->u.value;
9322
9323     default:
9324       return NULL_TREE;
9325     }
9326 }
9327
9328 /* Parse an (optional) function-specifier.
9329
9330    function-specifier:
9331      inline
9332      virtual
9333      explicit
9334
9335    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9336    Updates DECL_SPECS, if it is non-NULL.  */
9337
9338 static tree
9339 cp_parser_function_specifier_opt (cp_parser* parser,
9340                                   cp_decl_specifier_seq *decl_specs)
9341 {
9342   cp_token *token = cp_lexer_peek_token (parser->lexer);
9343   switch (token->keyword)
9344     {
9345     case RID_INLINE:
9346       if (decl_specs)
9347         ++decl_specs->specs[(int) ds_inline];
9348       break;
9349
9350     case RID_VIRTUAL:
9351       /* 14.5.2.3 [temp.mem]
9352
9353          A member function template shall not be virtual.  */
9354       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9355         error_at (token->location, "templates may not be %<virtual%>");
9356       else if (decl_specs)
9357         ++decl_specs->specs[(int) ds_virtual];
9358       break;
9359
9360     case RID_EXPLICIT:
9361       if (decl_specs)
9362         ++decl_specs->specs[(int) ds_explicit];
9363       break;
9364
9365     default:
9366       return NULL_TREE;
9367     }
9368
9369   /* Consume the token.  */
9370   return cp_lexer_consume_token (parser->lexer)->u.value;
9371 }
9372
9373 /* Parse a linkage-specification.
9374
9375    linkage-specification:
9376      extern string-literal { declaration-seq [opt] }
9377      extern string-literal declaration  */
9378
9379 static void
9380 cp_parser_linkage_specification (cp_parser* parser)
9381 {
9382   tree linkage;
9383
9384   /* Look for the `extern' keyword.  */
9385   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9386
9387   /* Look for the string-literal.  */
9388   linkage = cp_parser_string_literal (parser, false, false);
9389
9390   /* Transform the literal into an identifier.  If the literal is a
9391      wide-character string, or contains embedded NULs, then we can't
9392      handle it as the user wants.  */
9393   if (strlen (TREE_STRING_POINTER (linkage))
9394       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9395     {
9396       cp_parser_error (parser, "invalid linkage-specification");
9397       /* Assume C++ linkage.  */
9398       linkage = lang_name_cplusplus;
9399     }
9400   else
9401     linkage = get_identifier (TREE_STRING_POINTER (linkage));
9402
9403   /* We're now using the new linkage.  */
9404   push_lang_context (linkage);
9405
9406   /* If the next token is a `{', then we're using the first
9407      production.  */
9408   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9409     {
9410       /* Consume the `{' token.  */
9411       cp_lexer_consume_token (parser->lexer);
9412       /* Parse the declarations.  */
9413       cp_parser_declaration_seq_opt (parser);
9414       /* Look for the closing `}'.  */
9415       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9416     }
9417   /* Otherwise, there's just one declaration.  */
9418   else
9419     {
9420       bool saved_in_unbraced_linkage_specification_p;
9421
9422       saved_in_unbraced_linkage_specification_p
9423         = parser->in_unbraced_linkage_specification_p;
9424       parser->in_unbraced_linkage_specification_p = true;
9425       cp_parser_declaration (parser);
9426       parser->in_unbraced_linkage_specification_p
9427         = saved_in_unbraced_linkage_specification_p;
9428     }
9429
9430   /* We're done with the linkage-specification.  */
9431   pop_lang_context ();
9432 }
9433
9434 /* Parse a static_assert-declaration.
9435
9436    static_assert-declaration:
9437      static_assert ( constant-expression , string-literal ) ; 
9438
9439    If MEMBER_P, this static_assert is a class member.  */
9440
9441 static void 
9442 cp_parser_static_assert(cp_parser *parser, bool member_p)
9443 {
9444   tree condition;
9445   tree message;
9446   cp_token *token;
9447   location_t saved_loc;
9448
9449   /* Peek at the `static_assert' token so we can keep track of exactly
9450      where the static assertion started.  */
9451   token = cp_lexer_peek_token (parser->lexer);
9452   saved_loc = token->location;
9453
9454   /* Look for the `static_assert' keyword.  */
9455   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
9456                                   "%<static_assert%>"))
9457     return;
9458
9459   /*  We know we are in a static assertion; commit to any tentative
9460       parse.  */
9461   if (cp_parser_parsing_tentatively (parser))
9462     cp_parser_commit_to_tentative_parse (parser);
9463
9464   /* Parse the `(' starting the static assertion condition.  */
9465   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9466
9467   /* Parse the constant-expression.  */
9468   condition = 
9469     cp_parser_constant_expression (parser,
9470                                    /*allow_non_constant_p=*/false,
9471                                    /*non_constant_p=*/NULL);
9472
9473   /* Parse the separating `,'.  */
9474   cp_parser_require (parser, CPP_COMMA, "%<,%>");
9475
9476   /* Parse the string-literal message.  */
9477   message = cp_parser_string_literal (parser, 
9478                                       /*translate=*/false,
9479                                       /*wide_ok=*/true);
9480
9481   /* A `)' completes the static assertion.  */
9482   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9483     cp_parser_skip_to_closing_parenthesis (parser, 
9484                                            /*recovering=*/true, 
9485                                            /*or_comma=*/false,
9486                                            /*consume_paren=*/true);
9487
9488   /* A semicolon terminates the declaration.  */
9489   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9490
9491   /* Complete the static assertion, which may mean either processing 
9492      the static assert now or saving it for template instantiation.  */
9493   finish_static_assert (condition, message, saved_loc, member_p);
9494 }
9495
9496 /* Parse a `decltype' type. Returns the type. 
9497
9498    simple-type-specifier:
9499      decltype ( expression )  */
9500
9501 static tree
9502 cp_parser_decltype (cp_parser *parser)
9503 {
9504   tree expr;
9505   bool id_expression_or_member_access_p = false;
9506   const char *saved_message;
9507   bool saved_integral_constant_expression_p;
9508   bool saved_non_integral_constant_expression_p;
9509   cp_token *id_expr_start_token;
9510
9511   /* Look for the `decltype' token.  */
9512   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9513     return error_mark_node;
9514
9515   /* Types cannot be defined in a `decltype' expression.  Save away the
9516      old message.  */
9517   saved_message = parser->type_definition_forbidden_message;
9518
9519   /* And create the new one.  */
9520   parser->type_definition_forbidden_message
9521     = G_("types may not be defined in %<decltype%> expressions");
9522
9523   /* The restrictions on constant-expressions do not apply inside
9524      decltype expressions.  */
9525   saved_integral_constant_expression_p
9526     = parser->integral_constant_expression_p;
9527   saved_non_integral_constant_expression_p
9528     = parser->non_integral_constant_expression_p;
9529   parser->integral_constant_expression_p = false;
9530
9531   /* Do not actually evaluate the expression.  */
9532   ++cp_unevaluated_operand;
9533
9534   /* Do not warn about problems with the expression.  */
9535   ++c_inhibit_evaluation_warnings;
9536
9537   /* Parse the opening `('.  */
9538   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9539     return error_mark_node;
9540   
9541   /* First, try parsing an id-expression.  */
9542   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9543   cp_parser_parse_tentatively (parser);
9544   expr = cp_parser_id_expression (parser,
9545                                   /*template_keyword_p=*/false,
9546                                   /*check_dependency_p=*/true,
9547                                   /*template_p=*/NULL,
9548                                   /*declarator_p=*/false,
9549                                   /*optional_p=*/false);
9550
9551   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9552     {
9553       bool non_integral_constant_expression_p = false;
9554       tree id_expression = expr;
9555       cp_id_kind idk;
9556       const char *error_msg;
9557
9558       if (TREE_CODE (expr) == IDENTIFIER_NODE)
9559         /* Lookup the name we got back from the id-expression.  */
9560         expr = cp_parser_lookup_name (parser, expr,
9561                                       none_type,
9562                                       /*is_template=*/false,
9563                                       /*is_namespace=*/false,
9564                                       /*check_dependency=*/true,
9565                                       /*ambiguous_decls=*/NULL,
9566                                       id_expr_start_token->location);
9567
9568       if (expr
9569           && expr != error_mark_node
9570           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9571           && TREE_CODE (expr) != TYPE_DECL
9572           && (TREE_CODE (expr) != BIT_NOT_EXPR
9573               || !TYPE_P (TREE_OPERAND (expr, 0)))
9574           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9575         {
9576           /* Complete lookup of the id-expression.  */
9577           expr = (finish_id_expression
9578                   (id_expression, expr, parser->scope, &idk,
9579                    /*integral_constant_expression_p=*/false,
9580                    /*allow_non_integral_constant_expression_p=*/true,
9581                    &non_integral_constant_expression_p,
9582                    /*template_p=*/false,
9583                    /*done=*/true,
9584                    /*address_p=*/false,
9585                    /*template_arg_p=*/false,
9586                    &error_msg,
9587                    id_expr_start_token->location));
9588
9589           if (expr == error_mark_node)
9590             /* We found an id-expression, but it was something that we
9591                should not have found. This is an error, not something
9592                we can recover from, so note that we found an
9593                id-expression and we'll recover as gracefully as
9594                possible.  */
9595             id_expression_or_member_access_p = true;
9596         }
9597
9598       if (expr 
9599           && expr != error_mark_node
9600           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9601         /* We have an id-expression.  */
9602         id_expression_or_member_access_p = true;
9603     }
9604
9605   if (!id_expression_or_member_access_p)
9606     {
9607       /* Abort the id-expression parse.  */
9608       cp_parser_abort_tentative_parse (parser);
9609
9610       /* Parsing tentatively, again.  */
9611       cp_parser_parse_tentatively (parser);
9612
9613       /* Parse a class member access.  */
9614       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9615                                            /*cast_p=*/false,
9616                                            /*member_access_only_p=*/true, NULL);
9617
9618       if (expr 
9619           && expr != error_mark_node
9620           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9621         /* We have an id-expression.  */
9622         id_expression_or_member_access_p = true;
9623     }
9624
9625   if (id_expression_or_member_access_p)
9626     /* We have parsed the complete id-expression or member access.  */
9627     cp_parser_parse_definitely (parser);
9628   else
9629     {
9630       bool saved_greater_than_is_operator_p;
9631
9632       /* Abort our attempt to parse an id-expression or member access
9633          expression.  */
9634       cp_parser_abort_tentative_parse (parser);
9635
9636       /* Within a parenthesized expression, a `>' token is always
9637          the greater-than operator.  */
9638       saved_greater_than_is_operator_p
9639         = parser->greater_than_is_operator_p;
9640       parser->greater_than_is_operator_p = true;
9641
9642       /* Parse a full expression.  */
9643       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9644
9645       /* The `>' token might be the end of a template-id or
9646          template-parameter-list now.  */
9647       parser->greater_than_is_operator_p
9648         = saved_greater_than_is_operator_p;
9649     }
9650
9651   /* Go back to evaluating expressions.  */
9652   --cp_unevaluated_operand;
9653   --c_inhibit_evaluation_warnings;
9654
9655   /* Restore the old message and the integral constant expression
9656      flags.  */
9657   parser->type_definition_forbidden_message = saved_message;
9658   parser->integral_constant_expression_p
9659     = saved_integral_constant_expression_p;
9660   parser->non_integral_constant_expression_p
9661     = saved_non_integral_constant_expression_p;
9662
9663   if (expr == error_mark_node)
9664     {
9665       /* Skip everything up to the closing `)'.  */
9666       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9667                                              /*consume_paren=*/true);
9668       return error_mark_node;
9669     }
9670   
9671   /* Parse to the closing `)'.  */
9672   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9673     {
9674       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9675                                              /*consume_paren=*/true);
9676       return error_mark_node;
9677     }
9678
9679   return finish_decltype_type (expr, id_expression_or_member_access_p);
9680 }
9681
9682 /* Special member functions [gram.special] */
9683
9684 /* Parse a conversion-function-id.
9685
9686    conversion-function-id:
9687      operator conversion-type-id
9688
9689    Returns an IDENTIFIER_NODE representing the operator.  */
9690
9691 static tree
9692 cp_parser_conversion_function_id (cp_parser* parser)
9693 {
9694   tree type;
9695   tree saved_scope;
9696   tree saved_qualifying_scope;
9697   tree saved_object_scope;
9698   tree pushed_scope = NULL_TREE;
9699
9700   /* Look for the `operator' token.  */
9701   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9702     return error_mark_node;
9703   /* When we parse the conversion-type-id, the current scope will be
9704      reset.  However, we need that information in able to look up the
9705      conversion function later, so we save it here.  */
9706   saved_scope = parser->scope;
9707   saved_qualifying_scope = parser->qualifying_scope;
9708   saved_object_scope = parser->object_scope;
9709   /* We must enter the scope of the class so that the names of
9710      entities declared within the class are available in the
9711      conversion-type-id.  For example, consider:
9712
9713        struct S {
9714          typedef int I;
9715          operator I();
9716        };
9717
9718        S::operator I() { ... }
9719
9720      In order to see that `I' is a type-name in the definition, we
9721      must be in the scope of `S'.  */
9722   if (saved_scope)
9723     pushed_scope = push_scope (saved_scope);
9724   /* Parse the conversion-type-id.  */
9725   type = cp_parser_conversion_type_id (parser);
9726   /* Leave the scope of the class, if any.  */
9727   if (pushed_scope)
9728     pop_scope (pushed_scope);
9729   /* Restore the saved scope.  */
9730   parser->scope = saved_scope;
9731   parser->qualifying_scope = saved_qualifying_scope;
9732   parser->object_scope = saved_object_scope;
9733   /* If the TYPE is invalid, indicate failure.  */
9734   if (type == error_mark_node)
9735     return error_mark_node;
9736   return mangle_conv_op_name_for_type (type);
9737 }
9738
9739 /* Parse a conversion-type-id:
9740
9741    conversion-type-id:
9742      type-specifier-seq conversion-declarator [opt]
9743
9744    Returns the TYPE specified.  */
9745
9746 static tree
9747 cp_parser_conversion_type_id (cp_parser* parser)
9748 {
9749   tree attributes;
9750   cp_decl_specifier_seq type_specifiers;
9751   cp_declarator *declarator;
9752   tree type_specified;
9753
9754   /* Parse the attributes.  */
9755   attributes = cp_parser_attributes_opt (parser);
9756   /* Parse the type-specifiers.  */
9757   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9758                                 /*is_trailing_return=*/false,
9759                                 &type_specifiers);
9760   /* If that didn't work, stop.  */
9761   if (type_specifiers.type == error_mark_node)
9762     return error_mark_node;
9763   /* Parse the conversion-declarator.  */
9764   declarator = cp_parser_conversion_declarator_opt (parser);
9765
9766   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9767                                     /*initialized=*/0, &attributes);
9768   if (attributes)
9769     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9770
9771   /* Don't give this error when parsing tentatively.  This happens to
9772      work because we always parse this definitively once.  */
9773   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9774       && type_uses_auto (type_specified))
9775     {
9776       error ("invalid use of %<auto%> in conversion operator");
9777       return error_mark_node;
9778     }
9779
9780   return type_specified;
9781 }
9782
9783 /* Parse an (optional) conversion-declarator.
9784
9785    conversion-declarator:
9786      ptr-operator conversion-declarator [opt]
9787
9788    */
9789
9790 static cp_declarator *
9791 cp_parser_conversion_declarator_opt (cp_parser* parser)
9792 {
9793   enum tree_code code;
9794   tree class_type;
9795   cp_cv_quals cv_quals;
9796
9797   /* We don't know if there's a ptr-operator next, or not.  */
9798   cp_parser_parse_tentatively (parser);
9799   /* Try the ptr-operator.  */
9800   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9801   /* If it worked, look for more conversion-declarators.  */
9802   if (cp_parser_parse_definitely (parser))
9803     {
9804       cp_declarator *declarator;
9805
9806       /* Parse another optional declarator.  */
9807       declarator = cp_parser_conversion_declarator_opt (parser);
9808
9809       return cp_parser_make_indirect_declarator
9810         (code, class_type, cv_quals, declarator);
9811    }
9812
9813   return NULL;
9814 }
9815
9816 /* Parse an (optional) ctor-initializer.
9817
9818    ctor-initializer:
9819      : mem-initializer-list
9820
9821    Returns TRUE iff the ctor-initializer was actually present.  */
9822
9823 static bool
9824 cp_parser_ctor_initializer_opt (cp_parser* parser)
9825 {
9826   /* If the next token is not a `:', then there is no
9827      ctor-initializer.  */
9828   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9829     {
9830       /* Do default initialization of any bases and members.  */
9831       if (DECL_CONSTRUCTOR_P (current_function_decl))
9832         finish_mem_initializers (NULL_TREE);
9833
9834       return false;
9835     }
9836
9837   /* Consume the `:' token.  */
9838   cp_lexer_consume_token (parser->lexer);
9839   /* And the mem-initializer-list.  */
9840   cp_parser_mem_initializer_list (parser);
9841
9842   return true;
9843 }
9844
9845 /* Parse a mem-initializer-list.
9846
9847    mem-initializer-list:
9848      mem-initializer ... [opt]
9849      mem-initializer ... [opt] , mem-initializer-list  */
9850
9851 static void
9852 cp_parser_mem_initializer_list (cp_parser* parser)
9853 {
9854   tree mem_initializer_list = NULL_TREE;
9855   cp_token *token = cp_lexer_peek_token (parser->lexer);
9856
9857   /* Let the semantic analysis code know that we are starting the
9858      mem-initializer-list.  */
9859   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9860     error_at (token->location,
9861               "only constructors take base initializers");
9862
9863   /* Loop through the list.  */
9864   while (true)
9865     {
9866       tree mem_initializer;
9867
9868       token = cp_lexer_peek_token (parser->lexer);
9869       /* Parse the mem-initializer.  */
9870       mem_initializer = cp_parser_mem_initializer (parser);
9871       /* If the next token is a `...', we're expanding member initializers. */
9872       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9873         {
9874           /* Consume the `...'. */
9875           cp_lexer_consume_token (parser->lexer);
9876
9877           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9878              can be expanded but members cannot. */
9879           if (mem_initializer != error_mark_node
9880               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9881             {
9882               error_at (token->location,
9883                         "cannot expand initializer for member %<%D%>",
9884                         TREE_PURPOSE (mem_initializer));
9885               mem_initializer = error_mark_node;
9886             }
9887
9888           /* Construct the pack expansion type. */
9889           if (mem_initializer != error_mark_node)
9890             mem_initializer = make_pack_expansion (mem_initializer);
9891         }
9892       /* Add it to the list, unless it was erroneous.  */
9893       if (mem_initializer != error_mark_node)
9894         {
9895           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9896           mem_initializer_list = mem_initializer;
9897         }
9898       /* If the next token is not a `,', we're done.  */
9899       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9900         break;
9901       /* Consume the `,' token.  */
9902       cp_lexer_consume_token (parser->lexer);
9903     }
9904
9905   /* Perform semantic analysis.  */
9906   if (DECL_CONSTRUCTOR_P (current_function_decl))
9907     finish_mem_initializers (mem_initializer_list);
9908 }
9909
9910 /* Parse a mem-initializer.
9911
9912    mem-initializer:
9913      mem-initializer-id ( expression-list [opt] )
9914      mem-initializer-id braced-init-list
9915
9916    GNU extension:
9917
9918    mem-initializer:
9919      ( expression-list [opt] )
9920
9921    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9922    class) or FIELD_DECL (for a non-static data member) to initialize;
9923    the TREE_VALUE is the expression-list.  An empty initialization
9924    list is represented by void_list_node.  */
9925
9926 static tree
9927 cp_parser_mem_initializer (cp_parser* parser)
9928 {
9929   tree mem_initializer_id;
9930   tree expression_list;
9931   tree member;
9932   cp_token *token = cp_lexer_peek_token (parser->lexer);
9933
9934   /* Find out what is being initialized.  */
9935   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9936     {
9937       permerror (token->location,
9938                  "anachronistic old-style base class initializer");
9939       mem_initializer_id = NULL_TREE;
9940     }
9941   else
9942     {
9943       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9944       if (mem_initializer_id == error_mark_node)
9945         return mem_initializer_id;
9946     }
9947   member = expand_member_init (mem_initializer_id);
9948   if (member && !DECL_P (member))
9949     in_base_initializer = 1;
9950
9951   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9952     {
9953       bool expr_non_constant_p;
9954       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9955       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9956       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9957       expression_list = build_tree_list (NULL_TREE, expression_list);
9958     }
9959   else
9960     {
9961       VEC(tree,gc)* vec;
9962       vec = cp_parser_parenthesized_expression_list (parser, false,
9963                                                      /*cast_p=*/false,
9964                                                      /*allow_expansion_p=*/true,
9965                                                      /*non_constant_p=*/NULL);
9966       if (vec == NULL)
9967         return error_mark_node;
9968       expression_list = build_tree_list_vec (vec);
9969       release_tree_vector (vec);
9970     }
9971
9972   if (expression_list == error_mark_node)
9973     return error_mark_node;
9974   if (!expression_list)
9975     expression_list = void_type_node;
9976
9977   in_base_initializer = 0;
9978
9979   return member ? build_tree_list (member, expression_list) : error_mark_node;
9980 }
9981
9982 /* Parse a mem-initializer-id.
9983
9984    mem-initializer-id:
9985      :: [opt] nested-name-specifier [opt] class-name
9986      identifier
9987
9988    Returns a TYPE indicating the class to be initializer for the first
9989    production.  Returns an IDENTIFIER_NODE indicating the data member
9990    to be initialized for the second production.  */
9991
9992 static tree
9993 cp_parser_mem_initializer_id (cp_parser* parser)
9994 {
9995   bool global_scope_p;
9996   bool nested_name_specifier_p;
9997   bool template_p = false;
9998   tree id;
9999
10000   cp_token *token = cp_lexer_peek_token (parser->lexer);
10001
10002   /* `typename' is not allowed in this context ([temp.res]).  */
10003   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10004     {
10005       error_at (token->location, 
10006                 "keyword %<typename%> not allowed in this context (a qualified "
10007                 "member initializer is implicitly a type)");
10008       cp_lexer_consume_token (parser->lexer);
10009     }
10010   /* Look for the optional `::' operator.  */
10011   global_scope_p
10012     = (cp_parser_global_scope_opt (parser,
10013                                    /*current_scope_valid_p=*/false)
10014        != NULL_TREE);
10015   /* Look for the optional nested-name-specifier.  The simplest way to
10016      implement:
10017
10018        [temp.res]
10019
10020        The keyword `typename' is not permitted in a base-specifier or
10021        mem-initializer; in these contexts a qualified name that
10022        depends on a template-parameter is implicitly assumed to be a
10023        type name.
10024
10025      is to assume that we have seen the `typename' keyword at this
10026      point.  */
10027   nested_name_specifier_p
10028     = (cp_parser_nested_name_specifier_opt (parser,
10029                                             /*typename_keyword_p=*/true,
10030                                             /*check_dependency_p=*/true,
10031                                             /*type_p=*/true,
10032                                             /*is_declaration=*/true)
10033        != NULL_TREE);
10034   if (nested_name_specifier_p)
10035     template_p = cp_parser_optional_template_keyword (parser);
10036   /* If there is a `::' operator or a nested-name-specifier, then we
10037      are definitely looking for a class-name.  */
10038   if (global_scope_p || nested_name_specifier_p)
10039     return cp_parser_class_name (parser,
10040                                  /*typename_keyword_p=*/true,
10041                                  /*template_keyword_p=*/template_p,
10042                                  typename_type,
10043                                  /*check_dependency_p=*/true,
10044                                  /*class_head_p=*/false,
10045                                  /*is_declaration=*/true);
10046   /* Otherwise, we could also be looking for an ordinary identifier.  */
10047   cp_parser_parse_tentatively (parser);
10048   /* Try a class-name.  */
10049   id = cp_parser_class_name (parser,
10050                              /*typename_keyword_p=*/true,
10051                              /*template_keyword_p=*/false,
10052                              none_type,
10053                              /*check_dependency_p=*/true,
10054                              /*class_head_p=*/false,
10055                              /*is_declaration=*/true);
10056   /* If we found one, we're done.  */
10057   if (cp_parser_parse_definitely (parser))
10058     return id;
10059   /* Otherwise, look for an ordinary identifier.  */
10060   return cp_parser_identifier (parser);
10061 }
10062
10063 /* Overloading [gram.over] */
10064
10065 /* Parse an operator-function-id.
10066
10067    operator-function-id:
10068      operator operator
10069
10070    Returns an IDENTIFIER_NODE for the operator which is a
10071    human-readable spelling of the identifier, e.g., `operator +'.  */
10072
10073 static tree
10074 cp_parser_operator_function_id (cp_parser* parser)
10075 {
10076   /* Look for the `operator' keyword.  */
10077   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10078     return error_mark_node;
10079   /* And then the name of the operator itself.  */
10080   return cp_parser_operator (parser);
10081 }
10082
10083 /* Parse an operator.
10084
10085    operator:
10086      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10087      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10088      || ++ -- , ->* -> () []
10089
10090    GNU Extensions:
10091
10092    operator:
10093      <? >? <?= >?=
10094
10095    Returns an IDENTIFIER_NODE for the operator which is a
10096    human-readable spelling of the identifier, e.g., `operator +'.  */
10097
10098 static tree
10099 cp_parser_operator (cp_parser* parser)
10100 {
10101   tree id = NULL_TREE;
10102   cp_token *token;
10103
10104   /* Peek at the next token.  */
10105   token = cp_lexer_peek_token (parser->lexer);
10106   /* Figure out which operator we have.  */
10107   switch (token->type)
10108     {
10109     case CPP_KEYWORD:
10110       {
10111         enum tree_code op;
10112
10113         /* The keyword should be either `new' or `delete'.  */
10114         if (token->keyword == RID_NEW)
10115           op = NEW_EXPR;
10116         else if (token->keyword == RID_DELETE)
10117           op = DELETE_EXPR;
10118         else
10119           break;
10120
10121         /* Consume the `new' or `delete' token.  */
10122         cp_lexer_consume_token (parser->lexer);
10123
10124         /* Peek at the next token.  */
10125         token = cp_lexer_peek_token (parser->lexer);
10126         /* If it's a `[' token then this is the array variant of the
10127            operator.  */
10128         if (token->type == CPP_OPEN_SQUARE)
10129           {
10130             /* Consume the `[' token.  */
10131             cp_lexer_consume_token (parser->lexer);
10132             /* Look for the `]' token.  */
10133             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10134             id = ansi_opname (op == NEW_EXPR
10135                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10136           }
10137         /* Otherwise, we have the non-array variant.  */
10138         else
10139           id = ansi_opname (op);
10140
10141         return id;
10142       }
10143
10144     case CPP_PLUS:
10145       id = ansi_opname (PLUS_EXPR);
10146       break;
10147
10148     case CPP_MINUS:
10149       id = ansi_opname (MINUS_EXPR);
10150       break;
10151
10152     case CPP_MULT:
10153       id = ansi_opname (MULT_EXPR);
10154       break;
10155
10156     case CPP_DIV:
10157       id = ansi_opname (TRUNC_DIV_EXPR);
10158       break;
10159
10160     case CPP_MOD:
10161       id = ansi_opname (TRUNC_MOD_EXPR);
10162       break;
10163
10164     case CPP_XOR:
10165       id = ansi_opname (BIT_XOR_EXPR);
10166       break;
10167
10168     case CPP_AND:
10169       id = ansi_opname (BIT_AND_EXPR);
10170       break;
10171
10172     case CPP_OR:
10173       id = ansi_opname (BIT_IOR_EXPR);
10174       break;
10175
10176     case CPP_COMPL:
10177       id = ansi_opname (BIT_NOT_EXPR);
10178       break;
10179
10180     case CPP_NOT:
10181       id = ansi_opname (TRUTH_NOT_EXPR);
10182       break;
10183
10184     case CPP_EQ:
10185       id = ansi_assopname (NOP_EXPR);
10186       break;
10187
10188     case CPP_LESS:
10189       id = ansi_opname (LT_EXPR);
10190       break;
10191
10192     case CPP_GREATER:
10193       id = ansi_opname (GT_EXPR);
10194       break;
10195
10196     case CPP_PLUS_EQ:
10197       id = ansi_assopname (PLUS_EXPR);
10198       break;
10199
10200     case CPP_MINUS_EQ:
10201       id = ansi_assopname (MINUS_EXPR);
10202       break;
10203
10204     case CPP_MULT_EQ:
10205       id = ansi_assopname (MULT_EXPR);
10206       break;
10207
10208     case CPP_DIV_EQ:
10209       id = ansi_assopname (TRUNC_DIV_EXPR);
10210       break;
10211
10212     case CPP_MOD_EQ:
10213       id = ansi_assopname (TRUNC_MOD_EXPR);
10214       break;
10215
10216     case CPP_XOR_EQ:
10217       id = ansi_assopname (BIT_XOR_EXPR);
10218       break;
10219
10220     case CPP_AND_EQ:
10221       id = ansi_assopname (BIT_AND_EXPR);
10222       break;
10223
10224     case CPP_OR_EQ:
10225       id = ansi_assopname (BIT_IOR_EXPR);
10226       break;
10227
10228     case CPP_LSHIFT:
10229       id = ansi_opname (LSHIFT_EXPR);
10230       break;
10231
10232     case CPP_RSHIFT:
10233       id = ansi_opname (RSHIFT_EXPR);
10234       break;
10235
10236     case CPP_LSHIFT_EQ:
10237       id = ansi_assopname (LSHIFT_EXPR);
10238       break;
10239
10240     case CPP_RSHIFT_EQ:
10241       id = ansi_assopname (RSHIFT_EXPR);
10242       break;
10243
10244     case CPP_EQ_EQ:
10245       id = ansi_opname (EQ_EXPR);
10246       break;
10247
10248     case CPP_NOT_EQ:
10249       id = ansi_opname (NE_EXPR);
10250       break;
10251
10252     case CPP_LESS_EQ:
10253       id = ansi_opname (LE_EXPR);
10254       break;
10255
10256     case CPP_GREATER_EQ:
10257       id = ansi_opname (GE_EXPR);
10258       break;
10259
10260     case CPP_AND_AND:
10261       id = ansi_opname (TRUTH_ANDIF_EXPR);
10262       break;
10263
10264     case CPP_OR_OR:
10265       id = ansi_opname (TRUTH_ORIF_EXPR);
10266       break;
10267
10268     case CPP_PLUS_PLUS:
10269       id = ansi_opname (POSTINCREMENT_EXPR);
10270       break;
10271
10272     case CPP_MINUS_MINUS:
10273       id = ansi_opname (PREDECREMENT_EXPR);
10274       break;
10275
10276     case CPP_COMMA:
10277       id = ansi_opname (COMPOUND_EXPR);
10278       break;
10279
10280     case CPP_DEREF_STAR:
10281       id = ansi_opname (MEMBER_REF);
10282       break;
10283
10284     case CPP_DEREF:
10285       id = ansi_opname (COMPONENT_REF);
10286       break;
10287
10288     case CPP_OPEN_PAREN:
10289       /* Consume the `('.  */
10290       cp_lexer_consume_token (parser->lexer);
10291       /* Look for the matching `)'.  */
10292       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10293       return ansi_opname (CALL_EXPR);
10294
10295     case CPP_OPEN_SQUARE:
10296       /* Consume the `['.  */
10297       cp_lexer_consume_token (parser->lexer);
10298       /* Look for the matching `]'.  */
10299       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10300       return ansi_opname (ARRAY_REF);
10301
10302     default:
10303       /* Anything else is an error.  */
10304       break;
10305     }
10306
10307   /* If we have selected an identifier, we need to consume the
10308      operator token.  */
10309   if (id)
10310     cp_lexer_consume_token (parser->lexer);
10311   /* Otherwise, no valid operator name was present.  */
10312   else
10313     {
10314       cp_parser_error (parser, "expected operator");
10315       id = error_mark_node;
10316     }
10317
10318   return id;
10319 }
10320
10321 /* Parse a template-declaration.
10322
10323    template-declaration:
10324      export [opt] template < template-parameter-list > declaration
10325
10326    If MEMBER_P is TRUE, this template-declaration occurs within a
10327    class-specifier.
10328
10329    The grammar rule given by the standard isn't correct.  What
10330    is really meant is:
10331
10332    template-declaration:
10333      export [opt] template-parameter-list-seq
10334        decl-specifier-seq [opt] init-declarator [opt] ;
10335      export [opt] template-parameter-list-seq
10336        function-definition
10337
10338    template-parameter-list-seq:
10339      template-parameter-list-seq [opt]
10340      template < template-parameter-list >  */
10341
10342 static void
10343 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10344 {
10345   /* Check for `export'.  */
10346   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10347     {
10348       /* Consume the `export' token.  */
10349       cp_lexer_consume_token (parser->lexer);
10350       /* Warn that we do not support `export'.  */
10351       warning (0, "keyword %<export%> not implemented, and will be ignored");
10352     }
10353
10354   cp_parser_template_declaration_after_export (parser, member_p);
10355 }
10356
10357 /* Parse a template-parameter-list.
10358
10359    template-parameter-list:
10360      template-parameter
10361      template-parameter-list , template-parameter
10362
10363    Returns a TREE_LIST.  Each node represents a template parameter.
10364    The nodes are connected via their TREE_CHAINs.  */
10365
10366 static tree
10367 cp_parser_template_parameter_list (cp_parser* parser)
10368 {
10369   tree parameter_list = NULL_TREE;
10370
10371   begin_template_parm_list ();
10372   while (true)
10373     {
10374       tree parameter;
10375       bool is_non_type;
10376       bool is_parameter_pack;
10377       location_t parm_loc;
10378
10379       /* Parse the template-parameter.  */
10380       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10381       parameter = cp_parser_template_parameter (parser, 
10382                                                 &is_non_type,
10383                                                 &is_parameter_pack);
10384       /* Add it to the list.  */
10385       if (parameter != error_mark_node)
10386         parameter_list = process_template_parm (parameter_list,
10387                                                 parm_loc,
10388                                                 parameter,
10389                                                 is_non_type,
10390                                                 is_parameter_pack);
10391       else
10392        {
10393          tree err_parm = build_tree_list (parameter, parameter);
10394          TREE_VALUE (err_parm) = error_mark_node;
10395          parameter_list = chainon (parameter_list, err_parm);
10396        }
10397
10398       /* If the next token is not a `,', we're done.  */
10399       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10400         break;
10401       /* Otherwise, consume the `,' token.  */
10402       cp_lexer_consume_token (parser->lexer);
10403     }
10404
10405   return end_template_parm_list (parameter_list);
10406 }
10407
10408 /* Parse a template-parameter.
10409
10410    template-parameter:
10411      type-parameter
10412      parameter-declaration
10413
10414    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
10415    the parameter.  The TREE_PURPOSE is the default value, if any.
10416    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
10417    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
10418    set to true iff this parameter is a parameter pack. */
10419
10420 static tree
10421 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10422                               bool *is_parameter_pack)
10423 {
10424   cp_token *token;
10425   cp_parameter_declarator *parameter_declarator;
10426   cp_declarator *id_declarator;
10427   tree parm;
10428
10429   /* Assume it is a type parameter or a template parameter.  */
10430   *is_non_type = false;
10431   /* Assume it not a parameter pack. */
10432   *is_parameter_pack = false;
10433   /* Peek at the next token.  */
10434   token = cp_lexer_peek_token (parser->lexer);
10435   /* If it is `class' or `template', we have a type-parameter.  */
10436   if (token->keyword == RID_TEMPLATE)
10437     return cp_parser_type_parameter (parser, is_parameter_pack);
10438   /* If it is `class' or `typename' we do not know yet whether it is a
10439      type parameter or a non-type parameter.  Consider:
10440
10441        template <typename T, typename T::X X> ...
10442
10443      or:
10444
10445        template <class C, class D*> ...
10446
10447      Here, the first parameter is a type parameter, and the second is
10448      a non-type parameter.  We can tell by looking at the token after
10449      the identifier -- if it is a `,', `=', or `>' then we have a type
10450      parameter.  */
10451   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10452     {
10453       /* Peek at the token after `class' or `typename'.  */
10454       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10455       /* If it's an ellipsis, we have a template type parameter
10456          pack. */
10457       if (token->type == CPP_ELLIPSIS)
10458         return cp_parser_type_parameter (parser, is_parameter_pack);
10459       /* If it's an identifier, skip it.  */
10460       if (token->type == CPP_NAME)
10461         token = cp_lexer_peek_nth_token (parser->lexer, 3);
10462       /* Now, see if the token looks like the end of a template
10463          parameter.  */
10464       if (token->type == CPP_COMMA
10465           || token->type == CPP_EQ
10466           || token->type == CPP_GREATER)
10467         return cp_parser_type_parameter (parser, is_parameter_pack);
10468     }
10469
10470   /* Otherwise, it is a non-type parameter.
10471
10472      [temp.param]
10473
10474      When parsing a default template-argument for a non-type
10475      template-parameter, the first non-nested `>' is taken as the end
10476      of the template parameter-list rather than a greater-than
10477      operator.  */
10478   *is_non_type = true;
10479   parameter_declarator
10480      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10481                                         /*parenthesized_p=*/NULL);
10482
10483   /* If the parameter declaration is marked as a parameter pack, set
10484      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10485      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10486      grokdeclarator. */
10487   if (parameter_declarator
10488       && parameter_declarator->declarator
10489       && parameter_declarator->declarator->parameter_pack_p)
10490     {
10491       *is_parameter_pack = true;
10492       parameter_declarator->declarator->parameter_pack_p = false;
10493     }
10494
10495   /* If the next token is an ellipsis, and we don't already have it
10496      marked as a parameter pack, then we have a parameter pack (that
10497      has no declarator).  */
10498   if (!*is_parameter_pack
10499       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10500       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10501     {
10502       /* Consume the `...'.  */
10503       cp_lexer_consume_token (parser->lexer);
10504       maybe_warn_variadic_templates ();
10505       
10506       *is_parameter_pack = true;
10507     }
10508   /* We might end up with a pack expansion as the type of the non-type
10509      template parameter, in which case this is a non-type template
10510      parameter pack.  */
10511   else if (parameter_declarator
10512            && parameter_declarator->decl_specifiers.type
10513            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10514     {
10515       *is_parameter_pack = true;
10516       parameter_declarator->decl_specifiers.type = 
10517         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10518     }
10519
10520   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10521     {
10522       /* Parameter packs cannot have default arguments.  However, a
10523          user may try to do so, so we'll parse them and give an
10524          appropriate diagnostic here.  */
10525
10526       /* Consume the `='.  */
10527       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10528       cp_lexer_consume_token (parser->lexer);
10529       
10530       /* Find the name of the parameter pack.  */     
10531       id_declarator = parameter_declarator->declarator;
10532       while (id_declarator && id_declarator->kind != cdk_id)
10533         id_declarator = id_declarator->declarator;
10534       
10535       if (id_declarator && id_declarator->kind == cdk_id)
10536         error_at (start_token->location,
10537                   "template parameter pack %qD cannot have a default argument",
10538                   id_declarator->u.id.unqualified_name);
10539       else
10540         error_at (start_token->location,
10541                   "template parameter pack cannot have a default argument");
10542       
10543       /* Parse the default argument, but throw away the result.  */
10544       cp_parser_default_argument (parser, /*template_parm_p=*/true);
10545     }
10546
10547   parm = grokdeclarator (parameter_declarator->declarator,
10548                          &parameter_declarator->decl_specifiers,
10549                          TPARM, /*initialized=*/0,
10550                          /*attrlist=*/NULL);
10551   if (parm == error_mark_node)
10552     return error_mark_node;
10553
10554   return build_tree_list (parameter_declarator->default_argument, parm);
10555 }
10556
10557 /* Parse a type-parameter.
10558
10559    type-parameter:
10560      class identifier [opt]
10561      class identifier [opt] = type-id
10562      typename identifier [opt]
10563      typename identifier [opt] = type-id
10564      template < template-parameter-list > class identifier [opt]
10565      template < template-parameter-list > class identifier [opt]
10566        = id-expression
10567
10568    GNU Extension (variadic templates):
10569
10570    type-parameter:
10571      class ... identifier [opt]
10572      typename ... identifier [opt]
10573
10574    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
10575    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
10576    the declaration of the parameter.
10577
10578    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10579
10580 static tree
10581 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10582 {
10583   cp_token *token;
10584   tree parameter;
10585
10586   /* Look for a keyword to tell us what kind of parameter this is.  */
10587   token = cp_parser_require (parser, CPP_KEYWORD,
10588                              "%<class%>, %<typename%>, or %<template%>");
10589   if (!token)
10590     return error_mark_node;
10591
10592   switch (token->keyword)
10593     {
10594     case RID_CLASS:
10595     case RID_TYPENAME:
10596       {
10597         tree identifier;
10598         tree default_argument;
10599
10600         /* If the next token is an ellipsis, we have a template
10601            argument pack. */
10602         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10603           {
10604             /* Consume the `...' token. */
10605             cp_lexer_consume_token (parser->lexer);
10606             maybe_warn_variadic_templates ();
10607
10608             *is_parameter_pack = true;
10609           }
10610
10611         /* If the next token is an identifier, then it names the
10612            parameter.  */
10613         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10614           identifier = cp_parser_identifier (parser);
10615         else
10616           identifier = NULL_TREE;
10617
10618         /* Create the parameter.  */
10619         parameter = finish_template_type_parm (class_type_node, identifier);
10620
10621         /* If the next token is an `=', we have a default argument.  */
10622         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10623           {
10624             /* Consume the `=' token.  */
10625             cp_lexer_consume_token (parser->lexer);
10626             /* Parse the default-argument.  */
10627             push_deferring_access_checks (dk_no_deferred);
10628             default_argument = cp_parser_type_id (parser);
10629
10630             /* Template parameter packs cannot have default
10631                arguments. */
10632             if (*is_parameter_pack)
10633               {
10634                 if (identifier)
10635                   error_at (token->location,
10636                             "template parameter pack %qD cannot have a "
10637                             "default argument", identifier);
10638                 else
10639                   error_at (token->location,
10640                             "template parameter packs cannot have "
10641                             "default arguments");
10642                 default_argument = NULL_TREE;
10643               }
10644             pop_deferring_access_checks ();
10645           }
10646         else
10647           default_argument = NULL_TREE;
10648
10649         /* Create the combined representation of the parameter and the
10650            default argument.  */
10651         parameter = build_tree_list (default_argument, parameter);
10652       }
10653       break;
10654
10655     case RID_TEMPLATE:
10656       {
10657         tree identifier;
10658         tree default_argument;
10659
10660         /* Look for the `<'.  */
10661         cp_parser_require (parser, CPP_LESS, "%<<%>");
10662         /* Parse the template-parameter-list.  */
10663         cp_parser_template_parameter_list (parser);
10664         /* Look for the `>'.  */
10665         cp_parser_require (parser, CPP_GREATER, "%<>%>");
10666         /* Look for the `class' keyword.  */
10667         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10668         /* If the next token is an ellipsis, we have a template
10669            argument pack. */
10670         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10671           {
10672             /* Consume the `...' token. */
10673             cp_lexer_consume_token (parser->lexer);
10674             maybe_warn_variadic_templates ();
10675
10676             *is_parameter_pack = true;
10677           }
10678         /* If the next token is an `=', then there is a
10679            default-argument.  If the next token is a `>', we are at
10680            the end of the parameter-list.  If the next token is a `,',
10681            then we are at the end of this parameter.  */
10682         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10683             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10684             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10685           {
10686             identifier = cp_parser_identifier (parser);
10687             /* Treat invalid names as if the parameter were nameless.  */
10688             if (identifier == error_mark_node)
10689               identifier = NULL_TREE;
10690           }
10691         else
10692           identifier = NULL_TREE;
10693
10694         /* Create the template parameter.  */
10695         parameter = finish_template_template_parm (class_type_node,
10696                                                    identifier);
10697
10698         /* If the next token is an `=', then there is a
10699            default-argument.  */
10700         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10701           {
10702             bool is_template;
10703
10704             /* Consume the `='.  */
10705             cp_lexer_consume_token (parser->lexer);
10706             /* Parse the id-expression.  */
10707             push_deferring_access_checks (dk_no_deferred);
10708             /* save token before parsing the id-expression, for error
10709                reporting */
10710             token = cp_lexer_peek_token (parser->lexer);
10711             default_argument
10712               = cp_parser_id_expression (parser,
10713                                          /*template_keyword_p=*/false,
10714                                          /*check_dependency_p=*/true,
10715                                          /*template_p=*/&is_template,
10716                                          /*declarator_p=*/false,
10717                                          /*optional_p=*/false);
10718             if (TREE_CODE (default_argument) == TYPE_DECL)
10719               /* If the id-expression was a template-id that refers to
10720                  a template-class, we already have the declaration here,
10721                  so no further lookup is needed.  */
10722                  ;
10723             else
10724               /* Look up the name.  */
10725               default_argument
10726                 = cp_parser_lookup_name (parser, default_argument,
10727                                          none_type,
10728                                          /*is_template=*/is_template,
10729                                          /*is_namespace=*/false,
10730                                          /*check_dependency=*/true,
10731                                          /*ambiguous_decls=*/NULL,
10732                                          token->location);
10733             /* See if the default argument is valid.  */
10734             default_argument
10735               = check_template_template_default_arg (default_argument);
10736
10737             /* Template parameter packs cannot have default
10738                arguments. */
10739             if (*is_parameter_pack)
10740               {
10741                 if (identifier)
10742                   error_at (token->location,
10743                             "template parameter pack %qD cannot "
10744                             "have a default argument",
10745                             identifier);
10746                 else
10747                   error_at (token->location, "template parameter packs cannot "
10748                             "have default arguments");
10749                 default_argument = NULL_TREE;
10750               }
10751             pop_deferring_access_checks ();
10752           }
10753         else
10754           default_argument = NULL_TREE;
10755
10756         /* Create the combined representation of the parameter and the
10757            default argument.  */
10758         parameter = build_tree_list (default_argument, parameter);
10759       }
10760       break;
10761
10762     default:
10763       gcc_unreachable ();
10764       break;
10765     }
10766
10767   return parameter;
10768 }
10769
10770 /* Parse a template-id.
10771
10772    template-id:
10773      template-name < template-argument-list [opt] >
10774
10775    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10776    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10777    returned.  Otherwise, if the template-name names a function, or set
10778    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10779    names a class, returns a TYPE_DECL for the specialization.
10780
10781    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10782    uninstantiated templates.  */
10783
10784 static tree
10785 cp_parser_template_id (cp_parser *parser,
10786                        bool template_keyword_p,
10787                        bool check_dependency_p,
10788                        bool is_declaration)
10789 {
10790   int i;
10791   tree templ;
10792   tree arguments;
10793   tree template_id;
10794   cp_token_position start_of_id = 0;
10795   deferred_access_check *chk;
10796   VEC (deferred_access_check,gc) *access_check;
10797   cp_token *next_token = NULL, *next_token_2 = NULL;
10798   bool is_identifier;
10799
10800   /* If the next token corresponds to a template-id, there is no need
10801      to reparse it.  */
10802   next_token = cp_lexer_peek_token (parser->lexer);
10803   if (next_token->type == CPP_TEMPLATE_ID)
10804     {
10805       struct tree_check *check_value;
10806
10807       /* Get the stored value.  */
10808       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10809       /* Perform any access checks that were deferred.  */
10810       access_check = check_value->checks;
10811       if (access_check)
10812         {
10813           for (i = 0 ;
10814                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10815                ++i)
10816             {
10817               perform_or_defer_access_check (chk->binfo,
10818                                              chk->decl,
10819                                              chk->diag_decl);
10820             }
10821         }
10822       /* Return the stored value.  */
10823       return check_value->value;
10824     }
10825
10826   /* Avoid performing name lookup if there is no possibility of
10827      finding a template-id.  */
10828   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10829       || (next_token->type == CPP_NAME
10830           && !cp_parser_nth_token_starts_template_argument_list_p
10831                (parser, 2)))
10832     {
10833       cp_parser_error (parser, "expected template-id");
10834       return error_mark_node;
10835     }
10836
10837   /* Remember where the template-id starts.  */
10838   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10839     start_of_id = cp_lexer_token_position (parser->lexer, false);
10840
10841   push_deferring_access_checks (dk_deferred);
10842
10843   /* Parse the template-name.  */
10844   is_identifier = false;
10845   templ = cp_parser_template_name (parser, template_keyword_p,
10846                                    check_dependency_p,
10847                                    is_declaration,
10848                                    &is_identifier);
10849   if (templ == error_mark_node || is_identifier)
10850     {
10851       pop_deferring_access_checks ();
10852       return templ;
10853     }
10854
10855   /* If we find the sequence `[:' after a template-name, it's probably
10856      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10857      parse correctly the argument list.  */
10858   next_token = cp_lexer_peek_token (parser->lexer);
10859   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10860   if (next_token->type == CPP_OPEN_SQUARE
10861       && next_token->flags & DIGRAPH
10862       && next_token_2->type == CPP_COLON
10863       && !(next_token_2->flags & PREV_WHITE))
10864     {
10865       cp_parser_parse_tentatively (parser);
10866       /* Change `:' into `::'.  */
10867       next_token_2->type = CPP_SCOPE;
10868       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10869          CPP_LESS.  */
10870       cp_lexer_consume_token (parser->lexer);
10871
10872       /* Parse the arguments.  */
10873       arguments = cp_parser_enclosed_template_argument_list (parser);
10874       if (!cp_parser_parse_definitely (parser))
10875         {
10876           /* If we couldn't parse an argument list, then we revert our changes
10877              and return simply an error. Maybe this is not a template-id
10878              after all.  */
10879           next_token_2->type = CPP_COLON;
10880           cp_parser_error (parser, "expected %<<%>");
10881           pop_deferring_access_checks ();
10882           return error_mark_node;
10883         }
10884       /* Otherwise, emit an error about the invalid digraph, but continue
10885          parsing because we got our argument list.  */
10886       if (permerror (next_token->location,
10887                      "%<<::%> cannot begin a template-argument list"))
10888         {
10889           static bool hint = false;
10890           inform (next_token->location,
10891                   "%<<:%> is an alternate spelling for %<[%>."
10892                   " Insert whitespace between %<<%> and %<::%>");
10893           if (!hint && !flag_permissive)
10894             {
10895               inform (next_token->location, "(if you use %<-fpermissive%>"
10896                       " G++ will accept your code)");
10897               hint = true;
10898             }
10899         }
10900     }
10901   else
10902     {
10903       /* Look for the `<' that starts the template-argument-list.  */
10904       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10905         {
10906           pop_deferring_access_checks ();
10907           return error_mark_node;
10908         }
10909       /* Parse the arguments.  */
10910       arguments = cp_parser_enclosed_template_argument_list (parser);
10911     }
10912
10913   /* Build a representation of the specialization.  */
10914   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10915     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10916   else if (DECL_CLASS_TEMPLATE_P (templ)
10917            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10918     {
10919       bool entering_scope;
10920       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10921          template (rather than some instantiation thereof) only if
10922          is not nested within some other construct.  For example, in
10923          "template <typename T> void f(T) { A<T>::", A<T> is just an
10924          instantiation of A.  */
10925       entering_scope = (template_parm_scope_p ()
10926                         && cp_lexer_next_token_is (parser->lexer,
10927                                                    CPP_SCOPE));
10928       template_id
10929         = finish_template_type (templ, arguments, entering_scope);
10930     }
10931   else
10932     {
10933       /* If it's not a class-template or a template-template, it should be
10934          a function-template.  */
10935       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10936                    || TREE_CODE (templ) == OVERLOAD
10937                    || BASELINK_P (templ)));
10938
10939       template_id = lookup_template_function (templ, arguments);
10940     }
10941
10942   /* If parsing tentatively, replace the sequence of tokens that makes
10943      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10944      should we re-parse the token stream, we will not have to repeat
10945      the effort required to do the parse, nor will we issue duplicate
10946      error messages about problems during instantiation of the
10947      template.  */
10948   if (start_of_id)
10949     {
10950       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10951
10952       /* Reset the contents of the START_OF_ID token.  */
10953       token->type = CPP_TEMPLATE_ID;
10954       /* Retrieve any deferred checks.  Do not pop this access checks yet
10955          so the memory will not be reclaimed during token replacing below.  */
10956       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10957       token->u.tree_check_value->value = template_id;
10958       token->u.tree_check_value->checks = get_deferred_access_checks ();
10959       token->keyword = RID_MAX;
10960
10961       /* Purge all subsequent tokens.  */
10962       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10963
10964       /* ??? Can we actually assume that, if template_id ==
10965          error_mark_node, we will have issued a diagnostic to the
10966          user, as opposed to simply marking the tentative parse as
10967          failed?  */
10968       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10969         error_at (token->location, "parse error in template argument list");
10970     }
10971
10972   pop_deferring_access_checks ();
10973   return template_id;
10974 }
10975
10976 /* Parse a template-name.
10977
10978    template-name:
10979      identifier
10980
10981    The standard should actually say:
10982
10983    template-name:
10984      identifier
10985      operator-function-id
10986
10987    A defect report has been filed about this issue.
10988
10989    A conversion-function-id cannot be a template name because they cannot
10990    be part of a template-id. In fact, looking at this code:
10991
10992    a.operator K<int>()
10993
10994    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10995    It is impossible to call a templated conversion-function-id with an
10996    explicit argument list, since the only allowed template parameter is
10997    the type to which it is converting.
10998
10999    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11000    `template' keyword, in a construction like:
11001
11002      T::template f<3>()
11003
11004    In that case `f' is taken to be a template-name, even though there
11005    is no way of knowing for sure.
11006
11007    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11008    name refers to a set of overloaded functions, at least one of which
11009    is a template, or an IDENTIFIER_NODE with the name of the template,
11010    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11011    names are looked up inside uninstantiated templates.  */
11012
11013 static tree
11014 cp_parser_template_name (cp_parser* parser,
11015                          bool template_keyword_p,
11016                          bool check_dependency_p,
11017                          bool is_declaration,
11018                          bool *is_identifier)
11019 {
11020   tree identifier;
11021   tree decl;
11022   tree fns;
11023   cp_token *token = cp_lexer_peek_token (parser->lexer);
11024
11025   /* If the next token is `operator', then we have either an
11026      operator-function-id or a conversion-function-id.  */
11027   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11028     {
11029       /* We don't know whether we're looking at an
11030          operator-function-id or a conversion-function-id.  */
11031       cp_parser_parse_tentatively (parser);
11032       /* Try an operator-function-id.  */
11033       identifier = cp_parser_operator_function_id (parser);
11034       /* If that didn't work, try a conversion-function-id.  */
11035       if (!cp_parser_parse_definitely (parser))
11036         {
11037           cp_parser_error (parser, "expected template-name");
11038           return error_mark_node;
11039         }
11040     }
11041   /* Look for the identifier.  */
11042   else
11043     identifier = cp_parser_identifier (parser);
11044
11045   /* If we didn't find an identifier, we don't have a template-id.  */
11046   if (identifier == error_mark_node)
11047     return error_mark_node;
11048
11049   /* If the name immediately followed the `template' keyword, then it
11050      is a template-name.  However, if the next token is not `<', then
11051      we do not treat it as a template-name, since it is not being used
11052      as part of a template-id.  This enables us to handle constructs
11053      like:
11054
11055        template <typename T> struct S { S(); };
11056        template <typename T> S<T>::S();
11057
11058      correctly.  We would treat `S' as a template -- if it were `S<T>'
11059      -- but we do not if there is no `<'.  */
11060
11061   if (processing_template_decl
11062       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11063     {
11064       /* In a declaration, in a dependent context, we pretend that the
11065          "template" keyword was present in order to improve error
11066          recovery.  For example, given:
11067
11068            template <typename T> void f(T::X<int>);
11069
11070          we want to treat "X<int>" as a template-id.  */
11071       if (is_declaration
11072           && !template_keyword_p
11073           && parser->scope && TYPE_P (parser->scope)
11074           && check_dependency_p
11075           && dependent_scope_p (parser->scope)
11076           /* Do not do this for dtors (or ctors), since they never
11077              need the template keyword before their name.  */
11078           && !constructor_name_p (identifier, parser->scope))
11079         {
11080           cp_token_position start = 0;
11081
11082           /* Explain what went wrong.  */
11083           error_at (token->location, "non-template %qD used as template",
11084                     identifier);
11085           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11086                   parser->scope, identifier);
11087           /* If parsing tentatively, find the location of the "<" token.  */
11088           if (cp_parser_simulate_error (parser))
11089             start = cp_lexer_token_position (parser->lexer, true);
11090           /* Parse the template arguments so that we can issue error
11091              messages about them.  */
11092           cp_lexer_consume_token (parser->lexer);
11093           cp_parser_enclosed_template_argument_list (parser);
11094           /* Skip tokens until we find a good place from which to
11095              continue parsing.  */
11096           cp_parser_skip_to_closing_parenthesis (parser,
11097                                                  /*recovering=*/true,
11098                                                  /*or_comma=*/true,
11099                                                  /*consume_paren=*/false);
11100           /* If parsing tentatively, permanently remove the
11101              template argument list.  That will prevent duplicate
11102              error messages from being issued about the missing
11103              "template" keyword.  */
11104           if (start)
11105             cp_lexer_purge_tokens_after (parser->lexer, start);
11106           if (is_identifier)
11107             *is_identifier = true;
11108           return identifier;
11109         }
11110
11111       /* If the "template" keyword is present, then there is generally
11112          no point in doing name-lookup, so we just return IDENTIFIER.
11113          But, if the qualifying scope is non-dependent then we can
11114          (and must) do name-lookup normally.  */
11115       if (template_keyword_p
11116           && (!parser->scope
11117               || (TYPE_P (parser->scope)
11118                   && dependent_type_p (parser->scope))))
11119         return identifier;
11120     }
11121
11122   /* Look up the name.  */
11123   decl = cp_parser_lookup_name (parser, identifier,
11124                                 none_type,
11125                                 /*is_template=*/true,
11126                                 /*is_namespace=*/false,
11127                                 check_dependency_p,
11128                                 /*ambiguous_decls=*/NULL,
11129                                 token->location);
11130
11131   /* If DECL is a template, then the name was a template-name.  */
11132   if (TREE_CODE (decl) == TEMPLATE_DECL)
11133     ;
11134   else
11135     {
11136       tree fn = NULL_TREE;
11137
11138       /* The standard does not explicitly indicate whether a name that
11139          names a set of overloaded declarations, some of which are
11140          templates, is a template-name.  However, such a name should
11141          be a template-name; otherwise, there is no way to form a
11142          template-id for the overloaded templates.  */
11143       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11144       if (TREE_CODE (fns) == OVERLOAD)
11145         for (fn = fns; fn; fn = OVL_NEXT (fn))
11146           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11147             break;
11148
11149       if (!fn)
11150         {
11151           /* The name does not name a template.  */
11152           cp_parser_error (parser, "expected template-name");
11153           return error_mark_node;
11154         }
11155     }
11156
11157   /* If DECL is dependent, and refers to a function, then just return
11158      its name; we will look it up again during template instantiation.  */
11159   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11160     {
11161       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11162       if (TYPE_P (scope) && dependent_type_p (scope))
11163         return identifier;
11164     }
11165
11166   return decl;
11167 }
11168
11169 /* Parse a template-argument-list.
11170
11171    template-argument-list:
11172      template-argument ... [opt]
11173      template-argument-list , template-argument ... [opt]
11174
11175    Returns a TREE_VEC containing the arguments.  */
11176
11177 static tree
11178 cp_parser_template_argument_list (cp_parser* parser)
11179 {
11180   tree fixed_args[10];
11181   unsigned n_args = 0;
11182   unsigned alloced = 10;
11183   tree *arg_ary = fixed_args;
11184   tree vec;
11185   bool saved_in_template_argument_list_p;
11186   bool saved_ice_p;
11187   bool saved_non_ice_p;
11188
11189   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11190   parser->in_template_argument_list_p = true;
11191   /* Even if the template-id appears in an integral
11192      constant-expression, the contents of the argument list do
11193      not.  */
11194   saved_ice_p = parser->integral_constant_expression_p;
11195   parser->integral_constant_expression_p = false;
11196   saved_non_ice_p = parser->non_integral_constant_expression_p;
11197   parser->non_integral_constant_expression_p = false;
11198   /* Parse the arguments.  */
11199   do
11200     {
11201       tree argument;
11202
11203       if (n_args)
11204         /* Consume the comma.  */
11205         cp_lexer_consume_token (parser->lexer);
11206
11207       /* Parse the template-argument.  */
11208       argument = cp_parser_template_argument (parser);
11209
11210       /* If the next token is an ellipsis, we're expanding a template
11211          argument pack. */
11212       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11213         {
11214           if (argument == error_mark_node)
11215             {
11216               cp_token *token = cp_lexer_peek_token (parser->lexer);
11217               error_at (token->location,
11218                         "expected parameter pack before %<...%>");
11219             }
11220           /* Consume the `...' token. */
11221           cp_lexer_consume_token (parser->lexer);
11222
11223           /* Make the argument into a TYPE_PACK_EXPANSION or
11224              EXPR_PACK_EXPANSION. */
11225           argument = make_pack_expansion (argument);
11226         }
11227
11228       if (n_args == alloced)
11229         {
11230           alloced *= 2;
11231
11232           if (arg_ary == fixed_args)
11233             {
11234               arg_ary = XNEWVEC (tree, alloced);
11235               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11236             }
11237           else
11238             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11239         }
11240       arg_ary[n_args++] = argument;
11241     }
11242   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11243
11244   vec = make_tree_vec (n_args);
11245
11246   while (n_args--)
11247     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11248
11249   if (arg_ary != fixed_args)
11250     free (arg_ary);
11251   parser->non_integral_constant_expression_p = saved_non_ice_p;
11252   parser->integral_constant_expression_p = saved_ice_p;
11253   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11254 #ifdef ENABLE_CHECKING
11255   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11256 #endif
11257   return vec;
11258 }
11259
11260 /* Parse a template-argument.
11261
11262    template-argument:
11263      assignment-expression
11264      type-id
11265      id-expression
11266
11267    The representation is that of an assignment-expression, type-id, or
11268    id-expression -- except that the qualified id-expression is
11269    evaluated, so that the value returned is either a DECL or an
11270    OVERLOAD.
11271
11272    Although the standard says "assignment-expression", it forbids
11273    throw-expressions or assignments in the template argument.
11274    Therefore, we use "conditional-expression" instead.  */
11275
11276 static tree
11277 cp_parser_template_argument (cp_parser* parser)
11278 {
11279   tree argument;
11280   bool template_p;
11281   bool address_p;
11282   bool maybe_type_id = false;
11283   cp_token *token = NULL, *argument_start_token = NULL;
11284   cp_id_kind idk;
11285
11286   /* There's really no way to know what we're looking at, so we just
11287      try each alternative in order.
11288
11289        [temp.arg]
11290
11291        In a template-argument, an ambiguity between a type-id and an
11292        expression is resolved to a type-id, regardless of the form of
11293        the corresponding template-parameter.
11294
11295      Therefore, we try a type-id first.  */
11296   cp_parser_parse_tentatively (parser);
11297   argument = cp_parser_template_type_arg (parser);
11298   /* If there was no error parsing the type-id but the next token is a
11299      '>>', our behavior depends on which dialect of C++ we're
11300      parsing. In C++98, we probably found a typo for '> >'. But there
11301      are type-id which are also valid expressions. For instance:
11302
11303      struct X { int operator >> (int); };
11304      template <int V> struct Foo {};
11305      Foo<X () >> 5> r;
11306
11307      Here 'X()' is a valid type-id of a function type, but the user just
11308      wanted to write the expression "X() >> 5". Thus, we remember that we
11309      found a valid type-id, but we still try to parse the argument as an
11310      expression to see what happens. 
11311
11312      In C++0x, the '>>' will be considered two separate '>'
11313      tokens.  */
11314   if (!cp_parser_error_occurred (parser)
11315       && cxx_dialect == cxx98
11316       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11317     {
11318       maybe_type_id = true;
11319       cp_parser_abort_tentative_parse (parser);
11320     }
11321   else
11322     {
11323       /* If the next token isn't a `,' or a `>', then this argument wasn't
11324       really finished. This means that the argument is not a valid
11325       type-id.  */
11326       if (!cp_parser_next_token_ends_template_argument_p (parser))
11327         cp_parser_error (parser, "expected template-argument");
11328       /* If that worked, we're done.  */
11329       if (cp_parser_parse_definitely (parser))
11330         return argument;
11331     }
11332   /* We're still not sure what the argument will be.  */
11333   cp_parser_parse_tentatively (parser);
11334   /* Try a template.  */
11335   argument_start_token = cp_lexer_peek_token (parser->lexer);
11336   argument = cp_parser_id_expression (parser,
11337                                       /*template_keyword_p=*/false,
11338                                       /*check_dependency_p=*/true,
11339                                       &template_p,
11340                                       /*declarator_p=*/false,
11341                                       /*optional_p=*/false);
11342   /* If the next token isn't a `,' or a `>', then this argument wasn't
11343      really finished.  */
11344   if (!cp_parser_next_token_ends_template_argument_p (parser))
11345     cp_parser_error (parser, "expected template-argument");
11346   if (!cp_parser_error_occurred (parser))
11347     {
11348       /* Figure out what is being referred to.  If the id-expression
11349          was for a class template specialization, then we will have a
11350          TYPE_DECL at this point.  There is no need to do name lookup
11351          at this point in that case.  */
11352       if (TREE_CODE (argument) != TYPE_DECL)
11353         argument = cp_parser_lookup_name (parser, argument,
11354                                           none_type,
11355                                           /*is_template=*/template_p,
11356                                           /*is_namespace=*/false,
11357                                           /*check_dependency=*/true,
11358                                           /*ambiguous_decls=*/NULL,
11359                                           argument_start_token->location);
11360       if (TREE_CODE (argument) != TEMPLATE_DECL
11361           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11362         cp_parser_error (parser, "expected template-name");
11363     }
11364   if (cp_parser_parse_definitely (parser))
11365     return argument;
11366   /* It must be a non-type argument.  There permitted cases are given
11367      in [temp.arg.nontype]:
11368
11369      -- an integral constant-expression of integral or enumeration
11370         type; or
11371
11372      -- the name of a non-type template-parameter; or
11373
11374      -- the name of an object or function with external linkage...
11375
11376      -- the address of an object or function with external linkage...
11377
11378      -- a pointer to member...  */
11379   /* Look for a non-type template parameter.  */
11380   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11381     {
11382       cp_parser_parse_tentatively (parser);
11383       argument = cp_parser_primary_expression (parser,
11384                                                /*address_p=*/false,
11385                                                /*cast_p=*/false,
11386                                                /*template_arg_p=*/true,
11387                                                &idk);
11388       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11389           || !cp_parser_next_token_ends_template_argument_p (parser))
11390         cp_parser_simulate_error (parser);
11391       if (cp_parser_parse_definitely (parser))
11392         return argument;
11393     }
11394
11395   /* If the next token is "&", the argument must be the address of an
11396      object or function with external linkage.  */
11397   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11398   if (address_p)
11399     cp_lexer_consume_token (parser->lexer);
11400   /* See if we might have an id-expression.  */
11401   token = cp_lexer_peek_token (parser->lexer);
11402   if (token->type == CPP_NAME
11403       || token->keyword == RID_OPERATOR
11404       || token->type == CPP_SCOPE
11405       || token->type == CPP_TEMPLATE_ID
11406       || token->type == CPP_NESTED_NAME_SPECIFIER)
11407     {
11408       cp_parser_parse_tentatively (parser);
11409       argument = cp_parser_primary_expression (parser,
11410                                                address_p,
11411                                                /*cast_p=*/false,
11412                                                /*template_arg_p=*/true,
11413                                                &idk);
11414       if (cp_parser_error_occurred (parser)
11415           || !cp_parser_next_token_ends_template_argument_p (parser))
11416         cp_parser_abort_tentative_parse (parser);
11417       else
11418         {
11419           tree probe;
11420
11421           if (TREE_CODE (argument) == INDIRECT_REF)
11422             {
11423               gcc_assert (REFERENCE_REF_P (argument));
11424               argument = TREE_OPERAND (argument, 0);
11425             }
11426
11427           /* If we're in a template, we represent a qualified-id referring
11428              to a static data member as a SCOPE_REF even if the scope isn't
11429              dependent so that we can check access control later.  */
11430           probe = argument;
11431           if (TREE_CODE (probe) == SCOPE_REF)
11432             probe = TREE_OPERAND (probe, 1);
11433           if (TREE_CODE (probe) == VAR_DECL)
11434             {
11435               /* A variable without external linkage might still be a
11436                  valid constant-expression, so no error is issued here
11437                  if the external-linkage check fails.  */
11438               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11439                 cp_parser_simulate_error (parser);
11440             }
11441           else if (is_overloaded_fn (argument))
11442             /* All overloaded functions are allowed; if the external
11443                linkage test does not pass, an error will be issued
11444                later.  */
11445             ;
11446           else if (address_p
11447                    && (TREE_CODE (argument) == OFFSET_REF
11448                        || TREE_CODE (argument) == SCOPE_REF))
11449             /* A pointer-to-member.  */
11450             ;
11451           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11452             ;
11453           else
11454             cp_parser_simulate_error (parser);
11455
11456           if (cp_parser_parse_definitely (parser))
11457             {
11458               if (address_p)
11459                 argument = build_x_unary_op (ADDR_EXPR, argument,
11460                                              tf_warning_or_error);
11461               return argument;
11462             }
11463         }
11464     }
11465   /* If the argument started with "&", there are no other valid
11466      alternatives at this point.  */
11467   if (address_p)
11468     {
11469       cp_parser_error (parser, "invalid non-type template argument");
11470       return error_mark_node;
11471     }
11472
11473   /* If the argument wasn't successfully parsed as a type-id followed
11474      by '>>', the argument can only be a constant expression now.
11475      Otherwise, we try parsing the constant-expression tentatively,
11476      because the argument could really be a type-id.  */
11477   if (maybe_type_id)
11478     cp_parser_parse_tentatively (parser);
11479   argument = cp_parser_constant_expression (parser,
11480                                             /*allow_non_constant_p=*/false,
11481                                             /*non_constant_p=*/NULL);
11482   argument = fold_non_dependent_expr (argument);
11483   if (!maybe_type_id)
11484     return argument;
11485   if (!cp_parser_next_token_ends_template_argument_p (parser))
11486     cp_parser_error (parser, "expected template-argument");
11487   if (cp_parser_parse_definitely (parser))
11488     return argument;
11489   /* We did our best to parse the argument as a non type-id, but that
11490      was the only alternative that matched (albeit with a '>' after
11491      it). We can assume it's just a typo from the user, and a
11492      diagnostic will then be issued.  */
11493   return cp_parser_template_type_arg (parser);
11494 }
11495
11496 /* Parse an explicit-instantiation.
11497
11498    explicit-instantiation:
11499      template declaration
11500
11501    Although the standard says `declaration', what it really means is:
11502
11503    explicit-instantiation:
11504      template decl-specifier-seq [opt] declarator [opt] ;
11505
11506    Things like `template int S<int>::i = 5, int S<double>::j;' are not
11507    supposed to be allowed.  A defect report has been filed about this
11508    issue.
11509
11510    GNU Extension:
11511
11512    explicit-instantiation:
11513      storage-class-specifier template
11514        decl-specifier-seq [opt] declarator [opt] ;
11515      function-specifier template
11516        decl-specifier-seq [opt] declarator [opt] ;  */
11517
11518 static void
11519 cp_parser_explicit_instantiation (cp_parser* parser)
11520 {
11521   int declares_class_or_enum;
11522   cp_decl_specifier_seq decl_specifiers;
11523   tree extension_specifier = NULL_TREE;
11524
11525   /* Look for an (optional) storage-class-specifier or
11526      function-specifier.  */
11527   if (cp_parser_allow_gnu_extensions_p (parser))
11528     {
11529       extension_specifier
11530         = cp_parser_storage_class_specifier_opt (parser);
11531       if (!extension_specifier)
11532         extension_specifier
11533           = cp_parser_function_specifier_opt (parser,
11534                                               /*decl_specs=*/NULL);
11535     }
11536
11537   /* Look for the `template' keyword.  */
11538   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11539   /* Let the front end know that we are processing an explicit
11540      instantiation.  */
11541   begin_explicit_instantiation ();
11542   /* [temp.explicit] says that we are supposed to ignore access
11543      control while processing explicit instantiation directives.  */
11544   push_deferring_access_checks (dk_no_check);
11545   /* Parse a decl-specifier-seq.  */
11546   cp_parser_decl_specifier_seq (parser,
11547                                 CP_PARSER_FLAGS_OPTIONAL,
11548                                 &decl_specifiers,
11549                                 &declares_class_or_enum);
11550   /* If there was exactly one decl-specifier, and it declared a class,
11551      and there's no declarator, then we have an explicit type
11552      instantiation.  */
11553   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11554     {
11555       tree type;
11556
11557       type = check_tag_decl (&decl_specifiers);
11558       /* Turn access control back on for names used during
11559          template instantiation.  */
11560       pop_deferring_access_checks ();
11561       if (type)
11562         do_type_instantiation (type, extension_specifier,
11563                                /*complain=*/tf_error);
11564     }
11565   else
11566     {
11567       cp_declarator *declarator;
11568       tree decl;
11569
11570       /* Parse the declarator.  */
11571       declarator
11572         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11573                                 /*ctor_dtor_or_conv_p=*/NULL,
11574                                 /*parenthesized_p=*/NULL,
11575                                 /*member_p=*/false);
11576       if (declares_class_or_enum & 2)
11577         cp_parser_check_for_definition_in_return_type (declarator,
11578                                                        decl_specifiers.type,
11579                                                        decl_specifiers.type_location);
11580       if (declarator != cp_error_declarator)
11581         {
11582           decl = grokdeclarator (declarator, &decl_specifiers,
11583                                  NORMAL, 0, &decl_specifiers.attributes);
11584           /* Turn access control back on for names used during
11585              template instantiation.  */
11586           pop_deferring_access_checks ();
11587           /* Do the explicit instantiation.  */
11588           do_decl_instantiation (decl, extension_specifier);
11589         }
11590       else
11591         {
11592           pop_deferring_access_checks ();
11593           /* Skip the body of the explicit instantiation.  */
11594           cp_parser_skip_to_end_of_statement (parser);
11595         }
11596     }
11597   /* We're done with the instantiation.  */
11598   end_explicit_instantiation ();
11599
11600   cp_parser_consume_semicolon_at_end_of_statement (parser);
11601 }
11602
11603 /* Parse an explicit-specialization.
11604
11605    explicit-specialization:
11606      template < > declaration
11607
11608    Although the standard says `declaration', what it really means is:
11609
11610    explicit-specialization:
11611      template <> decl-specifier [opt] init-declarator [opt] ;
11612      template <> function-definition
11613      template <> explicit-specialization
11614      template <> template-declaration  */
11615
11616 static void
11617 cp_parser_explicit_specialization (cp_parser* parser)
11618 {
11619   bool need_lang_pop;
11620   cp_token *token = cp_lexer_peek_token (parser->lexer);
11621
11622   /* Look for the `template' keyword.  */
11623   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11624   /* Look for the `<'.  */
11625   cp_parser_require (parser, CPP_LESS, "%<<%>");
11626   /* Look for the `>'.  */
11627   cp_parser_require (parser, CPP_GREATER, "%<>%>");
11628   /* We have processed another parameter list.  */
11629   ++parser->num_template_parameter_lists;
11630   /* [temp]
11631
11632      A template ... explicit specialization ... shall not have C
11633      linkage.  */
11634   if (current_lang_name == lang_name_c)
11635     {
11636       error_at (token->location, "template specialization with C linkage");
11637       /* Give it C++ linkage to avoid confusing other parts of the
11638          front end.  */
11639       push_lang_context (lang_name_cplusplus);
11640       need_lang_pop = true;
11641     }
11642   else
11643     need_lang_pop = false;
11644   /* Let the front end know that we are beginning a specialization.  */
11645   if (!begin_specialization ())
11646     {
11647       end_specialization ();
11648       return;
11649     }
11650
11651   /* If the next keyword is `template', we need to figure out whether
11652      or not we're looking a template-declaration.  */
11653   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11654     {
11655       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11656           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11657         cp_parser_template_declaration_after_export (parser,
11658                                                      /*member_p=*/false);
11659       else
11660         cp_parser_explicit_specialization (parser);
11661     }
11662   else
11663     /* Parse the dependent declaration.  */
11664     cp_parser_single_declaration (parser,
11665                                   /*checks=*/NULL,
11666                                   /*member_p=*/false,
11667                                   /*explicit_specialization_p=*/true,
11668                                   /*friend_p=*/NULL);
11669   /* We're done with the specialization.  */
11670   end_specialization ();
11671   /* For the erroneous case of a template with C linkage, we pushed an
11672      implicit C++ linkage scope; exit that scope now.  */
11673   if (need_lang_pop)
11674     pop_lang_context ();
11675   /* We're done with this parameter list.  */
11676   --parser->num_template_parameter_lists;
11677 }
11678
11679 /* Parse a type-specifier.
11680
11681    type-specifier:
11682      simple-type-specifier
11683      class-specifier
11684      enum-specifier
11685      elaborated-type-specifier
11686      cv-qualifier
11687
11688    GNU Extension:
11689
11690    type-specifier:
11691      __complex__
11692
11693    Returns a representation of the type-specifier.  For a
11694    class-specifier, enum-specifier, or elaborated-type-specifier, a
11695    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11696
11697    The parser flags FLAGS is used to control type-specifier parsing.
11698
11699    If IS_DECLARATION is TRUE, then this type-specifier is appearing
11700    in a decl-specifier-seq.
11701
11702    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11703    class-specifier, enum-specifier, or elaborated-type-specifier, then
11704    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
11705    if a type is declared; 2 if it is defined.  Otherwise, it is set to
11706    zero.
11707
11708    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11709    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11710    is set to FALSE.  */
11711
11712 static tree
11713 cp_parser_type_specifier (cp_parser* parser,
11714                           cp_parser_flags flags,
11715                           cp_decl_specifier_seq *decl_specs,
11716                           bool is_declaration,
11717                           int* declares_class_or_enum,
11718                           bool* is_cv_qualifier)
11719 {
11720   tree type_spec = NULL_TREE;
11721   cp_token *token;
11722   enum rid keyword;
11723   cp_decl_spec ds = ds_last;
11724
11725   /* Assume this type-specifier does not declare a new type.  */
11726   if (declares_class_or_enum)
11727     *declares_class_or_enum = 0;
11728   /* And that it does not specify a cv-qualifier.  */
11729   if (is_cv_qualifier)
11730     *is_cv_qualifier = false;
11731   /* Peek at the next token.  */
11732   token = cp_lexer_peek_token (parser->lexer);
11733
11734   /* If we're looking at a keyword, we can use that to guide the
11735      production we choose.  */
11736   keyword = token->keyword;
11737   switch (keyword)
11738     {
11739     case RID_ENUM:
11740       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11741         goto elaborated_type_specifier;
11742
11743       /* Look for the enum-specifier.  */
11744       type_spec = cp_parser_enum_specifier (parser);
11745       /* If that worked, we're done.  */
11746       if (type_spec)
11747         {
11748           if (declares_class_or_enum)
11749             *declares_class_or_enum = 2;
11750           if (decl_specs)
11751             cp_parser_set_decl_spec_type (decl_specs,
11752                                           type_spec,
11753                                           token->location,
11754                                           /*user_defined_p=*/true);
11755           return type_spec;
11756         }
11757       else
11758         goto elaborated_type_specifier;
11759
11760       /* Any of these indicate either a class-specifier, or an
11761          elaborated-type-specifier.  */
11762     case RID_CLASS:
11763     case RID_STRUCT:
11764     case RID_UNION:
11765       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11766         goto elaborated_type_specifier;
11767
11768       /* Parse tentatively so that we can back up if we don't find a
11769          class-specifier.  */
11770       cp_parser_parse_tentatively (parser);
11771       /* Look for the class-specifier.  */
11772       type_spec = cp_parser_class_specifier (parser);
11773       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11774       /* If that worked, we're done.  */
11775       if (cp_parser_parse_definitely (parser))
11776         {
11777           if (declares_class_or_enum)
11778             *declares_class_or_enum = 2;
11779           if (decl_specs)
11780             cp_parser_set_decl_spec_type (decl_specs,
11781                                           type_spec,
11782                                           token->location,
11783                                           /*user_defined_p=*/true);
11784           return type_spec;
11785         }
11786
11787       /* Fall through.  */
11788     elaborated_type_specifier:
11789       /* We're declaring (not defining) a class or enum.  */
11790       if (declares_class_or_enum)
11791         *declares_class_or_enum = 1;
11792
11793       /* Fall through.  */
11794     case RID_TYPENAME:
11795       /* Look for an elaborated-type-specifier.  */
11796       type_spec
11797         = (cp_parser_elaborated_type_specifier
11798            (parser,
11799             decl_specs && decl_specs->specs[(int) ds_friend],
11800             is_declaration));
11801       if (decl_specs)
11802         cp_parser_set_decl_spec_type (decl_specs,
11803                                       type_spec,
11804                                       token->location,
11805                                       /*user_defined_p=*/true);
11806       return type_spec;
11807
11808     case RID_CONST:
11809       ds = ds_const;
11810       if (is_cv_qualifier)
11811         *is_cv_qualifier = true;
11812       break;
11813
11814     case RID_VOLATILE:
11815       ds = ds_volatile;
11816       if (is_cv_qualifier)
11817         *is_cv_qualifier = true;
11818       break;
11819
11820     case RID_RESTRICT:
11821       ds = ds_restrict;
11822       if (is_cv_qualifier)
11823         *is_cv_qualifier = true;
11824       break;
11825
11826     case RID_COMPLEX:
11827       /* The `__complex__' keyword is a GNU extension.  */
11828       ds = ds_complex;
11829       break;
11830
11831     default:
11832       break;
11833     }
11834
11835   /* Handle simple keywords.  */
11836   if (ds != ds_last)
11837     {
11838       if (decl_specs)
11839         {
11840           ++decl_specs->specs[(int)ds];
11841           decl_specs->any_specifiers_p = true;
11842         }
11843       return cp_lexer_consume_token (parser->lexer)->u.value;
11844     }
11845
11846   /* If we do not already have a type-specifier, assume we are looking
11847      at a simple-type-specifier.  */
11848   type_spec = cp_parser_simple_type_specifier (parser,
11849                                                decl_specs,
11850                                                flags);
11851
11852   /* If we didn't find a type-specifier, and a type-specifier was not
11853      optional in this context, issue an error message.  */
11854   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11855     {
11856       cp_parser_error (parser, "expected type specifier");
11857       return error_mark_node;
11858     }
11859
11860   return type_spec;
11861 }
11862
11863 /* Parse a simple-type-specifier.
11864
11865    simple-type-specifier:
11866      :: [opt] nested-name-specifier [opt] type-name
11867      :: [opt] nested-name-specifier template template-id
11868      char
11869      wchar_t
11870      bool
11871      short
11872      int
11873      long
11874      signed
11875      unsigned
11876      float
11877      double
11878      void
11879
11880    C++0x Extension:
11881
11882    simple-type-specifier:
11883      auto
11884      decltype ( expression )   
11885      char16_t
11886      char32_t
11887
11888    GNU Extension:
11889
11890    simple-type-specifier:
11891      __typeof__ unary-expression
11892      __typeof__ ( type-id )
11893
11894    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11895    appropriately updated.  */
11896
11897 static tree
11898 cp_parser_simple_type_specifier (cp_parser* parser,
11899                                  cp_decl_specifier_seq *decl_specs,
11900                                  cp_parser_flags flags)
11901 {
11902   tree type = NULL_TREE;
11903   cp_token *token;
11904
11905   /* Peek at the next token.  */
11906   token = cp_lexer_peek_token (parser->lexer);
11907
11908   /* If we're looking at a keyword, things are easy.  */
11909   switch (token->keyword)
11910     {
11911     case RID_CHAR:
11912       if (decl_specs)
11913         decl_specs->explicit_char_p = true;
11914       type = char_type_node;
11915       break;
11916     case RID_CHAR16:
11917       type = char16_type_node;
11918       break;
11919     case RID_CHAR32:
11920       type = char32_type_node;
11921       break;
11922     case RID_WCHAR:
11923       type = wchar_type_node;
11924       break;
11925     case RID_BOOL:
11926       type = boolean_type_node;
11927       break;
11928     case RID_SHORT:
11929       if (decl_specs)
11930         ++decl_specs->specs[(int) ds_short];
11931       type = short_integer_type_node;
11932       break;
11933     case RID_INT:
11934       if (decl_specs)
11935         decl_specs->explicit_int_p = true;
11936       type = integer_type_node;
11937       break;
11938     case RID_LONG:
11939       if (decl_specs)
11940         ++decl_specs->specs[(int) ds_long];
11941       type = long_integer_type_node;
11942       break;
11943     case RID_SIGNED:
11944       if (decl_specs)
11945         ++decl_specs->specs[(int) ds_signed];
11946       type = integer_type_node;
11947       break;
11948     case RID_UNSIGNED:
11949       if (decl_specs)
11950         ++decl_specs->specs[(int) ds_unsigned];
11951       type = unsigned_type_node;
11952       break;
11953     case RID_FLOAT:
11954       type = float_type_node;
11955       break;
11956     case RID_DOUBLE:
11957       type = double_type_node;
11958       break;
11959     case RID_VOID:
11960       type = void_type_node;
11961       break;
11962       
11963     case RID_AUTO:
11964       maybe_warn_cpp0x (CPP0X_AUTO);
11965       type = make_auto ();
11966       break;
11967
11968     case RID_DECLTYPE:
11969       /* Parse the `decltype' type.  */
11970       type = cp_parser_decltype (parser);
11971
11972       if (decl_specs)
11973         cp_parser_set_decl_spec_type (decl_specs, type,
11974                                       token->location,
11975                                       /*user_defined_p=*/true);
11976
11977       return type;
11978
11979     case RID_TYPEOF:
11980       /* Consume the `typeof' token.  */
11981       cp_lexer_consume_token (parser->lexer);
11982       /* Parse the operand to `typeof'.  */
11983       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11984       /* If it is not already a TYPE, take its type.  */
11985       if (!TYPE_P (type))
11986         type = finish_typeof (type);
11987
11988       if (decl_specs)
11989         cp_parser_set_decl_spec_type (decl_specs, type,
11990                                       token->location,
11991                                       /*user_defined_p=*/true);
11992
11993       return type;
11994
11995     default:
11996       break;
11997     }
11998
11999   /* If the type-specifier was for a built-in type, we're done.  */
12000   if (type)
12001     {
12002       /* Record the type.  */
12003       if (decl_specs
12004           && (token->keyword != RID_SIGNED
12005               && token->keyword != RID_UNSIGNED
12006               && token->keyword != RID_SHORT
12007               && token->keyword != RID_LONG))
12008         cp_parser_set_decl_spec_type (decl_specs,
12009                                       type,
12010                                       token->location,
12011                                       /*user_defined=*/false);
12012       if (decl_specs)
12013         decl_specs->any_specifiers_p = true;
12014
12015       /* Consume the token.  */
12016       cp_lexer_consume_token (parser->lexer);
12017
12018       /* There is no valid C++ program where a non-template type is
12019          followed by a "<".  That usually indicates that the user thought
12020          that the type was a template.  */
12021       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12022
12023       return TYPE_NAME (type);
12024     }
12025
12026   /* The type-specifier must be a user-defined type.  */
12027   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12028     {
12029       bool qualified_p;
12030       bool global_p;
12031
12032       /* Don't gobble tokens or issue error messages if this is an
12033          optional type-specifier.  */
12034       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12035         cp_parser_parse_tentatively (parser);
12036
12037       /* Look for the optional `::' operator.  */
12038       global_p
12039         = (cp_parser_global_scope_opt (parser,
12040                                        /*current_scope_valid_p=*/false)
12041            != NULL_TREE);
12042       /* Look for the nested-name specifier.  */
12043       qualified_p
12044         = (cp_parser_nested_name_specifier_opt (parser,
12045                                                 /*typename_keyword_p=*/false,
12046                                                 /*check_dependency_p=*/true,
12047                                                 /*type_p=*/false,
12048                                                 /*is_declaration=*/false)
12049            != NULL_TREE);
12050       token = cp_lexer_peek_token (parser->lexer);
12051       /* If we have seen a nested-name-specifier, and the next token
12052          is `template', then we are using the template-id production.  */
12053       if (parser->scope
12054           && cp_parser_optional_template_keyword (parser))
12055         {
12056           /* Look for the template-id.  */
12057           type = cp_parser_template_id (parser,
12058                                         /*template_keyword_p=*/true,
12059                                         /*check_dependency_p=*/true,
12060                                         /*is_declaration=*/false);
12061           /* If the template-id did not name a type, we are out of
12062              luck.  */
12063           if (TREE_CODE (type) != TYPE_DECL)
12064             {
12065               cp_parser_error (parser, "expected template-id for type");
12066               type = NULL_TREE;
12067             }
12068         }
12069       /* Otherwise, look for a type-name.  */
12070       else
12071         type = cp_parser_type_name (parser);
12072       /* Keep track of all name-lookups performed in class scopes.  */
12073       if (type
12074           && !global_p
12075           && !qualified_p
12076           && TREE_CODE (type) == TYPE_DECL
12077           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12078         maybe_note_name_used_in_class (DECL_NAME (type), type);
12079       /* If it didn't work out, we don't have a TYPE.  */
12080       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12081           && !cp_parser_parse_definitely (parser))
12082         type = NULL_TREE;
12083       if (type && decl_specs)
12084         cp_parser_set_decl_spec_type (decl_specs, type,
12085                                       token->location,
12086                                       /*user_defined=*/true);
12087     }
12088
12089   /* If we didn't get a type-name, issue an error message.  */
12090   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12091     {
12092       cp_parser_error (parser, "expected type-name");
12093       return error_mark_node;
12094     }
12095
12096   /* There is no valid C++ program where a non-template type is
12097      followed by a "<".  That usually indicates that the user thought
12098      that the type was a template.  */
12099   if (type && type != error_mark_node)
12100     {
12101       /* As a last-ditch effort, see if TYPE is an Objective-C type.
12102          If it is, then the '<'...'>' enclose protocol names rather than
12103          template arguments, and so everything is fine.  */
12104       if (c_dialect_objc ()
12105           && (objc_is_id (type) || objc_is_class_name (type)))
12106         {
12107           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12108           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12109
12110           /* Clobber the "unqualified" type previously entered into
12111              DECL_SPECS with the new, improved protocol-qualified version.  */
12112           if (decl_specs)
12113             decl_specs->type = qual_type;
12114
12115           return qual_type;
12116         }
12117
12118       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12119                                                token->location);
12120     }
12121
12122   return type;
12123 }
12124
12125 /* Parse a type-name.
12126
12127    type-name:
12128      class-name
12129      enum-name
12130      typedef-name
12131
12132    enum-name:
12133      identifier
12134
12135    typedef-name:
12136      identifier
12137
12138    Returns a TYPE_DECL for the type.  */
12139
12140 static tree
12141 cp_parser_type_name (cp_parser* parser)
12142 {
12143   tree type_decl;
12144
12145   /* We can't know yet whether it is a class-name or not.  */
12146   cp_parser_parse_tentatively (parser);
12147   /* Try a class-name.  */
12148   type_decl = cp_parser_class_name (parser,
12149                                     /*typename_keyword_p=*/false,
12150                                     /*template_keyword_p=*/false,
12151                                     none_type,
12152                                     /*check_dependency_p=*/true,
12153                                     /*class_head_p=*/false,
12154                                     /*is_declaration=*/false);
12155   /* If it's not a class-name, keep looking.  */
12156   if (!cp_parser_parse_definitely (parser))
12157     {
12158       /* It must be a typedef-name or an enum-name.  */
12159       return cp_parser_nonclass_name (parser);
12160     }
12161
12162   return type_decl;
12163 }
12164
12165 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12166
12167    enum-name:
12168      identifier
12169
12170    typedef-name:
12171      identifier
12172
12173    Returns a TYPE_DECL for the type.  */
12174
12175 static tree
12176 cp_parser_nonclass_name (cp_parser* parser)
12177 {
12178   tree type_decl;
12179   tree identifier;
12180
12181   cp_token *token = cp_lexer_peek_token (parser->lexer);
12182   identifier = cp_parser_identifier (parser);
12183   if (identifier == error_mark_node)
12184     return error_mark_node;
12185
12186   /* Look up the type-name.  */
12187   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12188
12189   if (TREE_CODE (type_decl) != TYPE_DECL
12190       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12191     {
12192       /* See if this is an Objective-C type.  */
12193       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12194       tree type = objc_get_protocol_qualified_type (identifier, protos);
12195       if (type)
12196         type_decl = TYPE_NAME (type);
12197     }
12198   
12199   /* Issue an error if we did not find a type-name.  */
12200   if (TREE_CODE (type_decl) != TYPE_DECL)
12201     {
12202       if (!cp_parser_simulate_error (parser))
12203         cp_parser_name_lookup_error (parser, identifier, type_decl,
12204                                      "is not a type", token->location);
12205       return error_mark_node;
12206     }
12207   /* Remember that the name was used in the definition of the
12208      current class so that we can check later to see if the
12209      meaning would have been different after the class was
12210      entirely defined.  */
12211   else if (type_decl != error_mark_node
12212            && !parser->scope)
12213     maybe_note_name_used_in_class (identifier, type_decl);
12214   
12215   return type_decl;
12216 }
12217
12218 /* Parse an elaborated-type-specifier.  Note that the grammar given
12219    here incorporates the resolution to DR68.
12220
12221    elaborated-type-specifier:
12222      class-key :: [opt] nested-name-specifier [opt] identifier
12223      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12224      enum-key :: [opt] nested-name-specifier [opt] identifier
12225      typename :: [opt] nested-name-specifier identifier
12226      typename :: [opt] nested-name-specifier template [opt]
12227        template-id
12228
12229    GNU extension:
12230
12231    elaborated-type-specifier:
12232      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12233      class-key attributes :: [opt] nested-name-specifier [opt]
12234                template [opt] template-id
12235      enum attributes :: [opt] nested-name-specifier [opt] identifier
12236
12237    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12238    declared `friend'.  If IS_DECLARATION is TRUE, then this
12239    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12240    something is being declared.
12241
12242    Returns the TYPE specified.  */
12243
12244 static tree
12245 cp_parser_elaborated_type_specifier (cp_parser* parser,
12246                                      bool is_friend,
12247                                      bool is_declaration)
12248 {
12249   enum tag_types tag_type;
12250   tree identifier;
12251   tree type = NULL_TREE;
12252   tree attributes = NULL_TREE;
12253   tree globalscope;
12254   cp_token *token = NULL;
12255
12256   /* See if we're looking at the `enum' keyword.  */
12257   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12258     {
12259       /* Consume the `enum' token.  */
12260       cp_lexer_consume_token (parser->lexer);
12261       /* Remember that it's an enumeration type.  */
12262       tag_type = enum_type;
12263       /* Parse the optional `struct' or `class' key (for C++0x scoped
12264          enums).  */
12265       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12266           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12267         {
12268           if (cxx_dialect == cxx98)
12269             maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12270
12271           /* Consume the `struct' or `class'.  */
12272           cp_lexer_consume_token (parser->lexer);
12273         }
12274       /* Parse the attributes.  */
12275       attributes = cp_parser_attributes_opt (parser);
12276     }
12277   /* Or, it might be `typename'.  */
12278   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12279                                            RID_TYPENAME))
12280     {
12281       /* Consume the `typename' token.  */
12282       cp_lexer_consume_token (parser->lexer);
12283       /* Remember that it's a `typename' type.  */
12284       tag_type = typename_type;
12285     }
12286   /* Otherwise it must be a class-key.  */
12287   else
12288     {
12289       tag_type = cp_parser_class_key (parser);
12290       if (tag_type == none_type)
12291         return error_mark_node;
12292       /* Parse the attributes.  */
12293       attributes = cp_parser_attributes_opt (parser);
12294     }
12295
12296   /* Look for the `::' operator.  */
12297   globalscope =  cp_parser_global_scope_opt (parser,
12298                                              /*current_scope_valid_p=*/false);
12299   /* Look for the nested-name-specifier.  */
12300   if (tag_type == typename_type && !globalscope)
12301     {
12302       if (!cp_parser_nested_name_specifier (parser,
12303                                            /*typename_keyword_p=*/true,
12304                                            /*check_dependency_p=*/true,
12305                                            /*type_p=*/true,
12306                                             is_declaration))
12307         return error_mark_node;
12308     }
12309   else
12310     /* Even though `typename' is not present, the proposed resolution
12311        to Core Issue 180 says that in `class A<T>::B', `B' should be
12312        considered a type-name, even if `A<T>' is dependent.  */
12313     cp_parser_nested_name_specifier_opt (parser,
12314                                          /*typename_keyword_p=*/true,
12315                                          /*check_dependency_p=*/true,
12316                                          /*type_p=*/true,
12317                                          is_declaration);
12318  /* For everything but enumeration types, consider a template-id.
12319     For an enumeration type, consider only a plain identifier.  */
12320   if (tag_type != enum_type)
12321     {
12322       bool template_p = false;
12323       tree decl;
12324
12325       /* Allow the `template' keyword.  */
12326       template_p = cp_parser_optional_template_keyword (parser);
12327       /* If we didn't see `template', we don't know if there's a
12328          template-id or not.  */
12329       if (!template_p)
12330         cp_parser_parse_tentatively (parser);
12331       /* Parse the template-id.  */
12332       token = cp_lexer_peek_token (parser->lexer);
12333       decl = cp_parser_template_id (parser, template_p,
12334                                     /*check_dependency_p=*/true,
12335                                     is_declaration);
12336       /* If we didn't find a template-id, look for an ordinary
12337          identifier.  */
12338       if (!template_p && !cp_parser_parse_definitely (parser))
12339         ;
12340       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12341          in effect, then we must assume that, upon instantiation, the
12342          template will correspond to a class.  */
12343       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12344                && tag_type == typename_type)
12345         type = make_typename_type (parser->scope, decl,
12346                                    typename_type,
12347                                    /*complain=*/tf_error);
12348       /* If the `typename' keyword is in effect and DECL is not a type
12349          decl. Then type is non existant.   */
12350       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12351         type = NULL_TREE; 
12352       else 
12353         type = TREE_TYPE (decl);
12354     }
12355
12356   if (!type)
12357     {
12358       token = cp_lexer_peek_token (parser->lexer);
12359       identifier = cp_parser_identifier (parser);
12360
12361       if (identifier == error_mark_node)
12362         {
12363           parser->scope = NULL_TREE;
12364           return error_mark_node;
12365         }
12366
12367       /* For a `typename', we needn't call xref_tag.  */
12368       if (tag_type == typename_type
12369           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12370         return cp_parser_make_typename_type (parser, parser->scope,
12371                                              identifier,
12372                                              token->location);
12373       /* Look up a qualified name in the usual way.  */
12374       if (parser->scope)
12375         {
12376           tree decl;
12377           tree ambiguous_decls;
12378
12379           decl = cp_parser_lookup_name (parser, identifier,
12380                                         tag_type,
12381                                         /*is_template=*/false,
12382                                         /*is_namespace=*/false,
12383                                         /*check_dependency=*/true,
12384                                         &ambiguous_decls,
12385                                         token->location);
12386
12387           /* If the lookup was ambiguous, an error will already have been
12388              issued.  */
12389           if (ambiguous_decls)
12390             return error_mark_node;
12391
12392           /* If we are parsing friend declaration, DECL may be a
12393              TEMPLATE_DECL tree node here.  However, we need to check
12394              whether this TEMPLATE_DECL results in valid code.  Consider
12395              the following example:
12396
12397                namespace N {
12398                  template <class T> class C {};
12399                }
12400                class X {
12401                  template <class T> friend class N::C; // #1, valid code
12402                };
12403                template <class T> class Y {
12404                  friend class N::C;                    // #2, invalid code
12405                };
12406
12407              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12408              name lookup of `N::C'.  We see that friend declaration must
12409              be template for the code to be valid.  Note that
12410              processing_template_decl does not work here since it is
12411              always 1 for the above two cases.  */
12412
12413           decl = (cp_parser_maybe_treat_template_as_class
12414                   (decl, /*tag_name_p=*/is_friend
12415                          && parser->num_template_parameter_lists));
12416
12417           if (TREE_CODE (decl) != TYPE_DECL)
12418             {
12419               cp_parser_diagnose_invalid_type_name (parser,
12420                                                     parser->scope,
12421                                                     identifier,
12422                                                     token->location);
12423               return error_mark_node;
12424             }
12425
12426           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12427             {
12428               bool allow_template = (parser->num_template_parameter_lists
12429                                       || DECL_SELF_REFERENCE_P (decl));
12430               type = check_elaborated_type_specifier (tag_type, decl, 
12431                                                       allow_template);
12432
12433               if (type == error_mark_node)
12434                 return error_mark_node;
12435             }
12436
12437           /* Forward declarations of nested types, such as
12438
12439                class C1::C2;
12440                class C1::C2::C3;
12441
12442              are invalid unless all components preceding the final '::'
12443              are complete.  If all enclosing types are complete, these
12444              declarations become merely pointless.
12445
12446              Invalid forward declarations of nested types are errors
12447              caught elsewhere in parsing.  Those that are pointless arrive
12448              here.  */
12449
12450           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12451               && !is_friend && !processing_explicit_instantiation)
12452             warning (0, "declaration %qD does not declare anything", decl);
12453
12454           type = TREE_TYPE (decl);
12455         }
12456       else
12457         {
12458           /* An elaborated-type-specifier sometimes introduces a new type and
12459              sometimes names an existing type.  Normally, the rule is that it
12460              introduces a new type only if there is not an existing type of
12461              the same name already in scope.  For example, given:
12462
12463                struct S {};
12464                void f() { struct S s; }
12465
12466              the `struct S' in the body of `f' is the same `struct S' as in
12467              the global scope; the existing definition is used.  However, if
12468              there were no global declaration, this would introduce a new
12469              local class named `S'.
12470
12471              An exception to this rule applies to the following code:
12472
12473                namespace N { struct S; }
12474
12475              Here, the elaborated-type-specifier names a new type
12476              unconditionally; even if there is already an `S' in the
12477              containing scope this declaration names a new type.
12478              This exception only applies if the elaborated-type-specifier
12479              forms the complete declaration:
12480
12481                [class.name]
12482
12483                A declaration consisting solely of `class-key identifier ;' is
12484                either a redeclaration of the name in the current scope or a
12485                forward declaration of the identifier as a class name.  It
12486                introduces the name into the current scope.
12487
12488              We are in this situation precisely when the next token is a `;'.
12489
12490              An exception to the exception is that a `friend' declaration does
12491              *not* name a new type; i.e., given:
12492
12493                struct S { friend struct T; };
12494
12495              `T' is not a new type in the scope of `S'.
12496
12497              Also, `new struct S' or `sizeof (struct S)' never results in the
12498              definition of a new type; a new type can only be declared in a
12499              declaration context.  */
12500
12501           tag_scope ts;
12502           bool template_p;
12503
12504           if (is_friend)
12505             /* Friends have special name lookup rules.  */
12506             ts = ts_within_enclosing_non_class;
12507           else if (is_declaration
12508                    && cp_lexer_next_token_is (parser->lexer,
12509                                               CPP_SEMICOLON))
12510             /* This is a `class-key identifier ;' */
12511             ts = ts_current;
12512           else
12513             ts = ts_global;
12514
12515           template_p =
12516             (parser->num_template_parameter_lists
12517              && (cp_parser_next_token_starts_class_definition_p (parser)
12518                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12519           /* An unqualified name was used to reference this type, so
12520              there were no qualifying templates.  */
12521           if (!cp_parser_check_template_parameters (parser,
12522                                                     /*num_templates=*/0,
12523                                                     token->location,
12524                                                     /*declarator=*/NULL))
12525             return error_mark_node;
12526           type = xref_tag (tag_type, identifier, ts, template_p);
12527         }
12528     }
12529
12530   if (type == error_mark_node)
12531     return error_mark_node;
12532
12533   /* Allow attributes on forward declarations of classes.  */
12534   if (attributes)
12535     {
12536       if (TREE_CODE (type) == TYPENAME_TYPE)
12537         warning (OPT_Wattributes,
12538                  "attributes ignored on uninstantiated type");
12539       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12540                && ! processing_explicit_instantiation)
12541         warning (OPT_Wattributes,
12542                  "attributes ignored on template instantiation");
12543       else if (is_declaration && cp_parser_declares_only_class_p (parser))
12544         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12545       else
12546         warning (OPT_Wattributes,
12547                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12548     }
12549
12550   if (tag_type != enum_type)
12551     cp_parser_check_class_key (tag_type, type);
12552
12553   /* A "<" cannot follow an elaborated type specifier.  If that
12554      happens, the user was probably trying to form a template-id.  */
12555   cp_parser_check_for_invalid_template_id (parser, type, token->location);
12556
12557   return type;
12558 }
12559
12560 /* Parse an enum-specifier.
12561
12562    enum-specifier:
12563      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12564
12565    enum-key:
12566      enum
12567      enum class   [C++0x]
12568      enum struct  [C++0x]
12569
12570    enum-base:   [C++0x]
12571      : type-specifier-seq
12572
12573    GNU Extensions:
12574      enum-key attributes[opt] identifier [opt] enum-base [opt] 
12575        { enumerator-list [opt] }attributes[opt]
12576
12577    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12578    if the token stream isn't an enum-specifier after all.  */
12579
12580 static tree
12581 cp_parser_enum_specifier (cp_parser* parser)
12582 {
12583   tree identifier;
12584   tree type;
12585   tree attributes;
12586   bool scoped_enum_p = false;
12587   bool has_underlying_type = false;
12588   tree underlying_type = NULL_TREE;
12589
12590   /* Parse tentatively so that we can back up if we don't find a
12591      enum-specifier.  */
12592   cp_parser_parse_tentatively (parser);
12593
12594   /* Caller guarantees that the current token is 'enum', an identifier
12595      possibly follows, and the token after that is an opening brace.
12596      If we don't have an identifier, fabricate an anonymous name for
12597      the enumeration being defined.  */
12598   cp_lexer_consume_token (parser->lexer);
12599
12600   /* Parse the "class" or "struct", which indicates a scoped
12601      enumeration type in C++0x.  */
12602   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12603       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12604     {
12605       if (cxx_dialect == cxx98)
12606         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12607
12608       /* Consume the `struct' or `class' token.  */
12609       cp_lexer_consume_token (parser->lexer);
12610
12611       scoped_enum_p = true;
12612     }
12613
12614   attributes = cp_parser_attributes_opt (parser);
12615
12616   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12617     identifier = cp_parser_identifier (parser);
12618   else
12619     identifier = make_anon_name ();
12620
12621   /* Check for the `:' that denotes a specified underlying type in C++0x.
12622      Note that a ':' could also indicate a bitfield width, however.  */
12623   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12624     {
12625       cp_decl_specifier_seq type_specifiers;
12626
12627       /* Consume the `:'.  */
12628       cp_lexer_consume_token (parser->lexer);
12629
12630       /* Parse the type-specifier-seq.  */
12631       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12632                                     /*is_trailing_return=*/false,
12633                                     &type_specifiers);
12634
12635       /* At this point this is surely not elaborated type specifier.  */
12636       if (!cp_parser_parse_definitely (parser))
12637         return NULL_TREE;
12638
12639       if (cxx_dialect == cxx98)
12640         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12641
12642       has_underlying_type = true;
12643
12644       /* If that didn't work, stop.  */
12645       if (type_specifiers.type != error_mark_node)
12646         {
12647           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12648                                             /*initialized=*/0, NULL);
12649           if (underlying_type == error_mark_node)
12650             underlying_type = NULL_TREE;
12651         }
12652     }
12653
12654   /* Look for the `{' but don't consume it yet.  */
12655   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12656     {
12657       cp_parser_error (parser, "expected %<{%>");
12658       if (has_underlying_type)
12659         return NULL_TREE;
12660     }
12661
12662   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12663     return NULL_TREE;
12664
12665   /* Issue an error message if type-definitions are forbidden here.  */
12666   if (!cp_parser_check_type_definition (parser))
12667     type = error_mark_node;
12668   else
12669     /* Create the new type.  We do this before consuming the opening
12670        brace so the enum will be recorded as being on the line of its
12671        tag (or the 'enum' keyword, if there is no tag).  */
12672     type = start_enum (identifier, underlying_type, scoped_enum_p);
12673   
12674   /* Consume the opening brace.  */
12675   cp_lexer_consume_token (parser->lexer);
12676
12677   if (type == error_mark_node)
12678     {
12679       cp_parser_skip_to_end_of_block_or_statement (parser);
12680       return error_mark_node;
12681     }
12682
12683   /* If the next token is not '}', then there are some enumerators.  */
12684   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12685     cp_parser_enumerator_list (parser, type);
12686
12687   /* Consume the final '}'.  */
12688   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12689
12690   /* Look for trailing attributes to apply to this enumeration, and
12691      apply them if appropriate.  */
12692   if (cp_parser_allow_gnu_extensions_p (parser))
12693     {
12694       tree trailing_attr = cp_parser_attributes_opt (parser);
12695       trailing_attr = chainon (trailing_attr, attributes);
12696       cplus_decl_attributes (&type,
12697                              trailing_attr,
12698                              (int) ATTR_FLAG_TYPE_IN_PLACE);
12699     }
12700
12701   /* Finish up the enumeration.  */
12702   finish_enum (type);
12703
12704   return type;
12705 }
12706
12707 /* Parse an enumerator-list.  The enumerators all have the indicated
12708    TYPE.
12709
12710    enumerator-list:
12711      enumerator-definition
12712      enumerator-list , enumerator-definition  */
12713
12714 static void
12715 cp_parser_enumerator_list (cp_parser* parser, tree type)
12716 {
12717   while (true)
12718     {
12719       /* Parse an enumerator-definition.  */
12720       cp_parser_enumerator_definition (parser, type);
12721
12722       /* If the next token is not a ',', we've reached the end of
12723          the list.  */
12724       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12725         break;
12726       /* Otherwise, consume the `,' and keep going.  */
12727       cp_lexer_consume_token (parser->lexer);
12728       /* If the next token is a `}', there is a trailing comma.  */
12729       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12730         {
12731           if (!in_system_header)
12732             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12733           break;
12734         }
12735     }
12736 }
12737
12738 /* Parse an enumerator-definition.  The enumerator has the indicated
12739    TYPE.
12740
12741    enumerator-definition:
12742      enumerator
12743      enumerator = constant-expression
12744
12745    enumerator:
12746      identifier  */
12747
12748 static void
12749 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12750 {
12751   tree identifier;
12752   tree value;
12753
12754   /* Look for the identifier.  */
12755   identifier = cp_parser_identifier (parser);
12756   if (identifier == error_mark_node)
12757     return;
12758
12759   /* If the next token is an '=', then there is an explicit value.  */
12760   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12761     {
12762       /* Consume the `=' token.  */
12763       cp_lexer_consume_token (parser->lexer);
12764       /* Parse the value.  */
12765       value = cp_parser_constant_expression (parser,
12766                                              /*allow_non_constant_p=*/false,
12767                                              NULL);
12768     }
12769   else
12770     value = NULL_TREE;
12771
12772   /* If we are processing a template, make sure the initializer of the
12773      enumerator doesn't contain any bare template parameter pack.  */
12774   if (check_for_bare_parameter_packs (value))
12775     value = error_mark_node;
12776
12777   /* Create the enumerator.  */
12778   build_enumerator (identifier, value, type);
12779 }
12780
12781 /* Parse a namespace-name.
12782
12783    namespace-name:
12784      original-namespace-name
12785      namespace-alias
12786
12787    Returns the NAMESPACE_DECL for the namespace.  */
12788
12789 static tree
12790 cp_parser_namespace_name (cp_parser* parser)
12791 {
12792   tree identifier;
12793   tree namespace_decl;
12794
12795   cp_token *token = cp_lexer_peek_token (parser->lexer);
12796
12797   /* Get the name of the namespace.  */
12798   identifier = cp_parser_identifier (parser);
12799   if (identifier == error_mark_node)
12800     return error_mark_node;
12801
12802   /* Look up the identifier in the currently active scope.  Look only
12803      for namespaces, due to:
12804
12805        [basic.lookup.udir]
12806
12807        When looking up a namespace-name in a using-directive or alias
12808        definition, only namespace names are considered.
12809
12810      And:
12811
12812        [basic.lookup.qual]
12813
12814        During the lookup of a name preceding the :: scope resolution
12815        operator, object, function, and enumerator names are ignored.
12816
12817      (Note that cp_parser_qualifying_entity only calls this
12818      function if the token after the name is the scope resolution
12819      operator.)  */
12820   namespace_decl = cp_parser_lookup_name (parser, identifier,
12821                                           none_type,
12822                                           /*is_template=*/false,
12823                                           /*is_namespace=*/true,
12824                                           /*check_dependency=*/true,
12825                                           /*ambiguous_decls=*/NULL,
12826                                           token->location);
12827   /* If it's not a namespace, issue an error.  */
12828   if (namespace_decl == error_mark_node
12829       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12830     {
12831       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12832         error_at (token->location, "%qD is not a namespace-name", identifier);
12833       cp_parser_error (parser, "expected namespace-name");
12834       namespace_decl = error_mark_node;
12835     }
12836
12837   return namespace_decl;
12838 }
12839
12840 /* Parse a namespace-definition.
12841
12842    namespace-definition:
12843      named-namespace-definition
12844      unnamed-namespace-definition
12845
12846    named-namespace-definition:
12847      original-namespace-definition
12848      extension-namespace-definition
12849
12850    original-namespace-definition:
12851      namespace identifier { namespace-body }
12852
12853    extension-namespace-definition:
12854      namespace original-namespace-name { namespace-body }
12855
12856    unnamed-namespace-definition:
12857      namespace { namespace-body } */
12858
12859 static void
12860 cp_parser_namespace_definition (cp_parser* parser)
12861 {
12862   tree identifier, attribs;
12863   bool has_visibility;
12864   bool is_inline;
12865
12866   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12867     {
12868       is_inline = true;
12869       cp_lexer_consume_token (parser->lexer);
12870     }
12871   else
12872     is_inline = false;
12873
12874   /* Look for the `namespace' keyword.  */
12875   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12876
12877   /* Get the name of the namespace.  We do not attempt to distinguish
12878      between an original-namespace-definition and an
12879      extension-namespace-definition at this point.  The semantic
12880      analysis routines are responsible for that.  */
12881   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12882     identifier = cp_parser_identifier (parser);
12883   else
12884     identifier = NULL_TREE;
12885
12886   /* Parse any specified attributes.  */
12887   attribs = cp_parser_attributes_opt (parser);
12888
12889   /* Look for the `{' to start the namespace.  */
12890   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12891   /* Start the namespace.  */
12892   push_namespace (identifier);
12893
12894   /* "inline namespace" is equivalent to a stub namespace definition
12895      followed by a strong using directive.  */
12896   if (is_inline)
12897     {
12898       tree name_space = current_namespace;
12899       /* Set up namespace association.  */
12900       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12901         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12902                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12903       /* Import the contents of the inline namespace.  */
12904       pop_namespace ();
12905       do_using_directive (name_space);
12906       push_namespace (identifier);
12907     }
12908
12909   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12910
12911   /* Parse the body of the namespace.  */
12912   cp_parser_namespace_body (parser);
12913
12914 #ifdef HANDLE_PRAGMA_VISIBILITY
12915   if (has_visibility)
12916     pop_visibility (1);
12917 #endif
12918
12919   /* Finish the namespace.  */
12920   pop_namespace ();
12921   /* Look for the final `}'.  */
12922   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12923 }
12924
12925 /* Parse a namespace-body.
12926
12927    namespace-body:
12928      declaration-seq [opt]  */
12929
12930 static void
12931 cp_parser_namespace_body (cp_parser* parser)
12932 {
12933   cp_parser_declaration_seq_opt (parser);
12934 }
12935
12936 /* Parse a namespace-alias-definition.
12937
12938    namespace-alias-definition:
12939      namespace identifier = qualified-namespace-specifier ;  */
12940
12941 static void
12942 cp_parser_namespace_alias_definition (cp_parser* parser)
12943 {
12944   tree identifier;
12945   tree namespace_specifier;
12946
12947   cp_token *token = cp_lexer_peek_token (parser->lexer);
12948
12949   /* Look for the `namespace' keyword.  */
12950   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12951   /* Look for the identifier.  */
12952   identifier = cp_parser_identifier (parser);
12953   if (identifier == error_mark_node)
12954     return;
12955   /* Look for the `=' token.  */
12956   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12957       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12958     {
12959       error_at (token->location, "%<namespace%> definition is not allowed here");
12960       /* Skip the definition.  */
12961       cp_lexer_consume_token (parser->lexer);
12962       if (cp_parser_skip_to_closing_brace (parser))
12963         cp_lexer_consume_token (parser->lexer);
12964       return;
12965     }
12966   cp_parser_require (parser, CPP_EQ, "%<=%>");
12967   /* Look for the qualified-namespace-specifier.  */
12968   namespace_specifier
12969     = cp_parser_qualified_namespace_specifier (parser);
12970   /* Look for the `;' token.  */
12971   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12972
12973   /* Register the alias in the symbol table.  */
12974   do_namespace_alias (identifier, namespace_specifier);
12975 }
12976
12977 /* Parse a qualified-namespace-specifier.
12978
12979    qualified-namespace-specifier:
12980      :: [opt] nested-name-specifier [opt] namespace-name
12981
12982    Returns a NAMESPACE_DECL corresponding to the specified
12983    namespace.  */
12984
12985 static tree
12986 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12987 {
12988   /* Look for the optional `::'.  */
12989   cp_parser_global_scope_opt (parser,
12990                               /*current_scope_valid_p=*/false);
12991
12992   /* Look for the optional nested-name-specifier.  */
12993   cp_parser_nested_name_specifier_opt (parser,
12994                                        /*typename_keyword_p=*/false,
12995                                        /*check_dependency_p=*/true,
12996                                        /*type_p=*/false,
12997                                        /*is_declaration=*/true);
12998
12999   return cp_parser_namespace_name (parser);
13000 }
13001
13002 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13003    access declaration.
13004
13005    using-declaration:
13006      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13007      using :: unqualified-id ;  
13008
13009    access-declaration:
13010      qualified-id ;  
13011
13012    */
13013
13014 static bool
13015 cp_parser_using_declaration (cp_parser* parser, 
13016                              bool access_declaration_p)
13017 {
13018   cp_token *token;
13019   bool typename_p = false;
13020   bool global_scope_p;
13021   tree decl;
13022   tree identifier;
13023   tree qscope;
13024
13025   if (access_declaration_p)
13026     cp_parser_parse_tentatively (parser);
13027   else
13028     {
13029       /* Look for the `using' keyword.  */
13030       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13031       
13032       /* Peek at the next token.  */
13033       token = cp_lexer_peek_token (parser->lexer);
13034       /* See if it's `typename'.  */
13035       if (token->keyword == RID_TYPENAME)
13036         {
13037           /* Remember that we've seen it.  */
13038           typename_p = true;
13039           /* Consume the `typename' token.  */
13040           cp_lexer_consume_token (parser->lexer);
13041         }
13042     }
13043
13044   /* Look for the optional global scope qualification.  */
13045   global_scope_p
13046     = (cp_parser_global_scope_opt (parser,
13047                                    /*current_scope_valid_p=*/false)
13048        != NULL_TREE);
13049
13050   /* If we saw `typename', or didn't see `::', then there must be a
13051      nested-name-specifier present.  */
13052   if (typename_p || !global_scope_p)
13053     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13054                                               /*check_dependency_p=*/true,
13055                                               /*type_p=*/false,
13056                                               /*is_declaration=*/true);
13057   /* Otherwise, we could be in either of the two productions.  In that
13058      case, treat the nested-name-specifier as optional.  */
13059   else
13060     qscope = cp_parser_nested_name_specifier_opt (parser,
13061                                                   /*typename_keyword_p=*/false,
13062                                                   /*check_dependency_p=*/true,
13063                                                   /*type_p=*/false,
13064                                                   /*is_declaration=*/true);
13065   if (!qscope)
13066     qscope = global_namespace;
13067
13068   if (access_declaration_p && cp_parser_error_occurred (parser))
13069     /* Something has already gone wrong; there's no need to parse
13070        further.  Since an error has occurred, the return value of
13071        cp_parser_parse_definitely will be false, as required.  */
13072     return cp_parser_parse_definitely (parser);
13073
13074   token = cp_lexer_peek_token (parser->lexer);
13075   /* Parse the unqualified-id.  */
13076   identifier = cp_parser_unqualified_id (parser,
13077                                          /*template_keyword_p=*/false,
13078                                          /*check_dependency_p=*/true,
13079                                          /*declarator_p=*/true,
13080                                          /*optional_p=*/false);
13081
13082   if (access_declaration_p)
13083     {
13084       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13085         cp_parser_simulate_error (parser);
13086       if (!cp_parser_parse_definitely (parser))
13087         return false;
13088     }
13089
13090   /* The function we call to handle a using-declaration is different
13091      depending on what scope we are in.  */
13092   if (qscope == error_mark_node || identifier == error_mark_node)
13093     ;
13094   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13095            && TREE_CODE (identifier) != BIT_NOT_EXPR)
13096     /* [namespace.udecl]
13097
13098        A using declaration shall not name a template-id.  */
13099     error_at (token->location,
13100               "a template-id may not appear in a using-declaration");
13101   else
13102     {
13103       if (at_class_scope_p ())
13104         {
13105           /* Create the USING_DECL.  */
13106           decl = do_class_using_decl (parser->scope, identifier);
13107
13108           if (check_for_bare_parameter_packs (decl))
13109             return false;
13110           else
13111             /* Add it to the list of members in this class.  */
13112             finish_member_declaration (decl);
13113         }
13114       else
13115         {
13116           decl = cp_parser_lookup_name_simple (parser,
13117                                                identifier,
13118                                                token->location);
13119           if (decl == error_mark_node)
13120             cp_parser_name_lookup_error (parser, identifier,
13121                                          decl, NULL,
13122                                          token->location);
13123           else if (check_for_bare_parameter_packs (decl))
13124             return false;
13125           else if (!at_namespace_scope_p ())
13126             do_local_using_decl (decl, qscope, identifier);
13127           else
13128             do_toplevel_using_decl (decl, qscope, identifier);
13129         }
13130     }
13131
13132   /* Look for the final `;'.  */
13133   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13134   
13135   return true;
13136 }
13137
13138 /* Parse a using-directive.
13139
13140    using-directive:
13141      using namespace :: [opt] nested-name-specifier [opt]
13142        namespace-name ;  */
13143
13144 static void
13145 cp_parser_using_directive (cp_parser* parser)
13146 {
13147   tree namespace_decl;
13148   tree attribs;
13149
13150   /* Look for the `using' keyword.  */
13151   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13152   /* And the `namespace' keyword.  */
13153   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13154   /* Look for the optional `::' operator.  */
13155   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13156   /* And the optional nested-name-specifier.  */
13157   cp_parser_nested_name_specifier_opt (parser,
13158                                        /*typename_keyword_p=*/false,
13159                                        /*check_dependency_p=*/true,
13160                                        /*type_p=*/false,
13161                                        /*is_declaration=*/true);
13162   /* Get the namespace being used.  */
13163   namespace_decl = cp_parser_namespace_name (parser);
13164   /* And any specified attributes.  */
13165   attribs = cp_parser_attributes_opt (parser);
13166   /* Update the symbol table.  */
13167   parse_using_directive (namespace_decl, attribs);
13168   /* Look for the final `;'.  */
13169   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13170 }
13171
13172 /* Parse an asm-definition.
13173
13174    asm-definition:
13175      asm ( string-literal ) ;
13176
13177    GNU Extension:
13178
13179    asm-definition:
13180      asm volatile [opt] ( string-literal ) ;
13181      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13182      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13183                           : asm-operand-list [opt] ) ;
13184      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13185                           : asm-operand-list [opt]
13186                           : asm-clobber-list [opt] ) ;
13187      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13188                                : asm-clobber-list [opt]
13189                                : asm-goto-list ) ;  */
13190
13191 static void
13192 cp_parser_asm_definition (cp_parser* parser)
13193 {
13194   tree string;
13195   tree outputs = NULL_TREE;
13196   tree inputs = NULL_TREE;
13197   tree clobbers = NULL_TREE;
13198   tree labels = NULL_TREE;
13199   tree asm_stmt;
13200   bool volatile_p = false;
13201   bool extended_p = false;
13202   bool invalid_inputs_p = false;
13203   bool invalid_outputs_p = false;
13204   bool goto_p = false;
13205   const char *missing = NULL;
13206
13207   /* Look for the `asm' keyword.  */
13208   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13209   /* See if the next token is `volatile'.  */
13210   if (cp_parser_allow_gnu_extensions_p (parser)
13211       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13212     {
13213       /* Remember that we saw the `volatile' keyword.  */
13214       volatile_p = true;
13215       /* Consume the token.  */
13216       cp_lexer_consume_token (parser->lexer);
13217     }
13218   if (cp_parser_allow_gnu_extensions_p (parser)
13219       && parser->in_function_body
13220       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13221     {
13222       /* Remember that we saw the `goto' keyword.  */
13223       goto_p = true;
13224       /* Consume the token.  */
13225       cp_lexer_consume_token (parser->lexer);
13226     }
13227   /* Look for the opening `('.  */
13228   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13229     return;
13230   /* Look for the string.  */
13231   string = cp_parser_string_literal (parser, false, false);
13232   if (string == error_mark_node)
13233     {
13234       cp_parser_skip_to_closing_parenthesis (parser, true, false,
13235                                              /*consume_paren=*/true);
13236       return;
13237     }
13238
13239   /* If we're allowing GNU extensions, check for the extended assembly
13240      syntax.  Unfortunately, the `:' tokens need not be separated by
13241      a space in C, and so, for compatibility, we tolerate that here
13242      too.  Doing that means that we have to treat the `::' operator as
13243      two `:' tokens.  */
13244   if (cp_parser_allow_gnu_extensions_p (parser)
13245       && parser->in_function_body
13246       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13247           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13248     {
13249       bool inputs_p = false;
13250       bool clobbers_p = false;
13251       bool labels_p = false;
13252
13253       /* The extended syntax was used.  */
13254       extended_p = true;
13255
13256       /* Look for outputs.  */
13257       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13258         {
13259           /* Consume the `:'.  */
13260           cp_lexer_consume_token (parser->lexer);
13261           /* Parse the output-operands.  */
13262           if (cp_lexer_next_token_is_not (parser->lexer,
13263                                           CPP_COLON)
13264               && cp_lexer_next_token_is_not (parser->lexer,
13265                                              CPP_SCOPE)
13266               && cp_lexer_next_token_is_not (parser->lexer,
13267                                              CPP_CLOSE_PAREN)
13268               && !goto_p)
13269             outputs = cp_parser_asm_operand_list (parser);
13270
13271             if (outputs == error_mark_node)
13272               invalid_outputs_p = true;
13273         }
13274       /* If the next token is `::', there are no outputs, and the
13275          next token is the beginning of the inputs.  */
13276       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13277         /* The inputs are coming next.  */
13278         inputs_p = true;
13279
13280       /* Look for inputs.  */
13281       if (inputs_p
13282           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13283         {
13284           /* Consume the `:' or `::'.  */
13285           cp_lexer_consume_token (parser->lexer);
13286           /* Parse the output-operands.  */
13287           if (cp_lexer_next_token_is_not (parser->lexer,
13288                                           CPP_COLON)
13289               && cp_lexer_next_token_is_not (parser->lexer,
13290                                              CPP_SCOPE)
13291               && cp_lexer_next_token_is_not (parser->lexer,
13292                                              CPP_CLOSE_PAREN))
13293             inputs = cp_parser_asm_operand_list (parser);
13294
13295             if (inputs == error_mark_node)
13296               invalid_inputs_p = true;
13297         }
13298       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13299         /* The clobbers are coming next.  */
13300         clobbers_p = true;
13301
13302       /* Look for clobbers.  */
13303       if (clobbers_p
13304           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13305         {
13306           clobbers_p = true;
13307           /* Consume the `:' or `::'.  */
13308           cp_lexer_consume_token (parser->lexer);
13309           /* Parse the clobbers.  */
13310           if (cp_lexer_next_token_is_not (parser->lexer,
13311                                           CPP_COLON)
13312               && cp_lexer_next_token_is_not (parser->lexer,
13313                                              CPP_CLOSE_PAREN))
13314             clobbers = cp_parser_asm_clobber_list (parser);
13315         }
13316       else if (goto_p
13317                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13318         /* The labels are coming next.  */
13319         labels_p = true;
13320
13321       /* Look for labels.  */
13322       if (labels_p
13323           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13324         {
13325           labels_p = true;
13326           /* Consume the `:' or `::'.  */
13327           cp_lexer_consume_token (parser->lexer);
13328           /* Parse the labels.  */
13329           labels = cp_parser_asm_label_list (parser);
13330         }
13331
13332       if (goto_p && !labels_p)
13333         missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13334     }
13335   else if (goto_p)
13336     missing = "%<:%> or %<::%>";
13337
13338   /* Look for the closing `)'.  */
13339   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13340                           missing ? missing : "%<)%>"))
13341     cp_parser_skip_to_closing_parenthesis (parser, true, false,
13342                                            /*consume_paren=*/true);
13343   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13344
13345   if (!invalid_inputs_p && !invalid_outputs_p)
13346     {
13347       /* Create the ASM_EXPR.  */
13348       if (parser->in_function_body)
13349         {
13350           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13351                                       inputs, clobbers, labels);
13352           /* If the extended syntax was not used, mark the ASM_EXPR.  */
13353           if (!extended_p)
13354             {
13355               tree temp = asm_stmt;
13356               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13357                 temp = TREE_OPERAND (temp, 0);
13358
13359               ASM_INPUT_P (temp) = 1;
13360             }
13361         }
13362       else
13363         cgraph_add_asm_node (string);
13364     }
13365 }
13366
13367 /* Declarators [gram.dcl.decl] */
13368
13369 /* Parse an init-declarator.
13370
13371    init-declarator:
13372      declarator initializer [opt]
13373
13374    GNU Extension:
13375
13376    init-declarator:
13377      declarator asm-specification [opt] attributes [opt] initializer [opt]
13378
13379    function-definition:
13380      decl-specifier-seq [opt] declarator ctor-initializer [opt]
13381        function-body
13382      decl-specifier-seq [opt] declarator function-try-block
13383
13384    GNU Extension:
13385
13386    function-definition:
13387      __extension__ function-definition
13388
13389    The DECL_SPECIFIERS apply to this declarator.  Returns a
13390    representation of the entity declared.  If MEMBER_P is TRUE, then
13391    this declarator appears in a class scope.  The new DECL created by
13392    this declarator is returned.
13393
13394    The CHECKS are access checks that should be performed once we know
13395    what entity is being declared (and, therefore, what classes have
13396    befriended it).
13397
13398    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13399    for a function-definition here as well.  If the declarator is a
13400    declarator for a function-definition, *FUNCTION_DEFINITION_P will
13401    be TRUE upon return.  By that point, the function-definition will
13402    have been completely parsed.
13403
13404    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13405    is FALSE.  */
13406
13407 static tree
13408 cp_parser_init_declarator (cp_parser* parser,
13409                            cp_decl_specifier_seq *decl_specifiers,
13410                            VEC (deferred_access_check,gc)* checks,
13411                            bool function_definition_allowed_p,
13412                            bool member_p,
13413                            int declares_class_or_enum,
13414                            bool* function_definition_p)
13415 {
13416   cp_token *token = NULL, *asm_spec_start_token = NULL,
13417            *attributes_start_token = NULL;
13418   cp_declarator *declarator;
13419   tree prefix_attributes;
13420   tree attributes;
13421   tree asm_specification;
13422   tree initializer;
13423   tree decl = NULL_TREE;
13424   tree scope;
13425   int is_initialized;
13426   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
13427      initialized with "= ..", CPP_OPEN_PAREN if initialized with
13428      "(...)".  */
13429   enum cpp_ttype initialization_kind;
13430   bool is_direct_init = false;
13431   bool is_non_constant_init;
13432   int ctor_dtor_or_conv_p;
13433   bool friend_p;
13434   tree pushed_scope = NULL;
13435
13436   /* Gather the attributes that were provided with the
13437      decl-specifiers.  */
13438   prefix_attributes = decl_specifiers->attributes;
13439
13440   /* Assume that this is not the declarator for a function
13441      definition.  */
13442   if (function_definition_p)
13443     *function_definition_p = false;
13444
13445   /* Defer access checks while parsing the declarator; we cannot know
13446      what names are accessible until we know what is being
13447      declared.  */
13448   resume_deferring_access_checks ();
13449
13450   /* Parse the declarator.  */
13451   token = cp_lexer_peek_token (parser->lexer);
13452   declarator
13453     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13454                             &ctor_dtor_or_conv_p,
13455                             /*parenthesized_p=*/NULL,
13456                             /*member_p=*/false);
13457   /* Gather up the deferred checks.  */
13458   stop_deferring_access_checks ();
13459
13460   /* If the DECLARATOR was erroneous, there's no need to go
13461      further.  */
13462   if (declarator == cp_error_declarator)
13463     return error_mark_node;
13464
13465   /* Check that the number of template-parameter-lists is OK.  */
13466   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13467                                                        token->location))
13468     return error_mark_node;
13469
13470   if (declares_class_or_enum & 2)
13471     cp_parser_check_for_definition_in_return_type (declarator,
13472                                                    decl_specifiers->type,
13473                                                    decl_specifiers->type_location);
13474
13475   /* Figure out what scope the entity declared by the DECLARATOR is
13476      located in.  `grokdeclarator' sometimes changes the scope, so
13477      we compute it now.  */
13478   scope = get_scope_of_declarator (declarator);
13479
13480   /* If we're allowing GNU extensions, look for an asm-specification
13481      and attributes.  */
13482   if (cp_parser_allow_gnu_extensions_p (parser))
13483     {
13484       /* Look for an asm-specification.  */
13485       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13486       asm_specification = cp_parser_asm_specification_opt (parser);
13487       /* And attributes.  */
13488       attributes_start_token = cp_lexer_peek_token (parser->lexer);
13489       attributes = cp_parser_attributes_opt (parser);
13490     }
13491   else
13492     {
13493       asm_specification = NULL_TREE;
13494       attributes = NULL_TREE;
13495     }
13496
13497   /* Peek at the next token.  */
13498   token = cp_lexer_peek_token (parser->lexer);
13499   /* Check to see if the token indicates the start of a
13500      function-definition.  */
13501   if (function_declarator_p (declarator)
13502       && cp_parser_token_starts_function_definition_p (token))
13503     {
13504       if (!function_definition_allowed_p)
13505         {
13506           /* If a function-definition should not appear here, issue an
13507              error message.  */
13508           cp_parser_error (parser,
13509                            "a function-definition is not allowed here");
13510           return error_mark_node;
13511         }
13512       else
13513         {
13514           location_t func_brace_location
13515             = cp_lexer_peek_token (parser->lexer)->location;
13516
13517           /* Neither attributes nor an asm-specification are allowed
13518              on a function-definition.  */
13519           if (asm_specification)
13520             error_at (asm_spec_start_token->location,
13521                       "an asm-specification is not allowed "
13522                       "on a function-definition");
13523           if (attributes)
13524             error_at (attributes_start_token->location,
13525                       "attributes are not allowed on a function-definition");
13526           /* This is a function-definition.  */
13527           *function_definition_p = true;
13528
13529           /* Parse the function definition.  */
13530           if (member_p)
13531             decl = cp_parser_save_member_function_body (parser,
13532                                                         decl_specifiers,
13533                                                         declarator,
13534                                                         prefix_attributes);
13535           else
13536             decl
13537               = (cp_parser_function_definition_from_specifiers_and_declarator
13538                  (parser, decl_specifiers, prefix_attributes, declarator));
13539
13540           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13541             {
13542               /* This is where the prologue starts...  */
13543               DECL_STRUCT_FUNCTION (decl)->function_start_locus
13544                 = func_brace_location;
13545             }
13546
13547           return decl;
13548         }
13549     }
13550
13551   /* [dcl.dcl]
13552
13553      Only in function declarations for constructors, destructors, and
13554      type conversions can the decl-specifier-seq be omitted.
13555
13556      We explicitly postpone this check past the point where we handle
13557      function-definitions because we tolerate function-definitions
13558      that are missing their return types in some modes.  */
13559   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13560     {
13561       cp_parser_error (parser,
13562                        "expected constructor, destructor, or type conversion");
13563       return error_mark_node;
13564     }
13565
13566   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
13567   if (token->type == CPP_EQ
13568       || token->type == CPP_OPEN_PAREN
13569       || token->type == CPP_OPEN_BRACE)
13570     {
13571       is_initialized = SD_INITIALIZED;
13572       initialization_kind = token->type;
13573
13574       if (token->type == CPP_EQ
13575           && function_declarator_p (declarator))
13576         {
13577           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13578           if (t2->keyword == RID_DEFAULT)
13579             is_initialized = SD_DEFAULTED;
13580           else if (t2->keyword == RID_DELETE)
13581             is_initialized = SD_DELETED;
13582         }
13583     }
13584   else
13585     {
13586       /* If the init-declarator isn't initialized and isn't followed by a
13587          `,' or `;', it's not a valid init-declarator.  */
13588       if (token->type != CPP_COMMA
13589           && token->type != CPP_SEMICOLON)
13590         {
13591           cp_parser_error (parser, "expected initializer");
13592           return error_mark_node;
13593         }
13594       is_initialized = SD_UNINITIALIZED;
13595       initialization_kind = CPP_EOF;
13596     }
13597
13598   /* Because start_decl has side-effects, we should only call it if we
13599      know we're going ahead.  By this point, we know that we cannot
13600      possibly be looking at any other construct.  */
13601   cp_parser_commit_to_tentative_parse (parser);
13602
13603   /* If the decl specifiers were bad, issue an error now that we're
13604      sure this was intended to be a declarator.  Then continue
13605      declaring the variable(s), as int, to try to cut down on further
13606      errors.  */
13607   if (decl_specifiers->any_specifiers_p
13608       && decl_specifiers->type == error_mark_node)
13609     {
13610       cp_parser_error (parser, "invalid type in declaration");
13611       decl_specifiers->type = integer_type_node;
13612     }
13613
13614   /* Check to see whether or not this declaration is a friend.  */
13615   friend_p = cp_parser_friend_p (decl_specifiers);
13616
13617   /* Enter the newly declared entry in the symbol table.  If we're
13618      processing a declaration in a class-specifier, we wait until
13619      after processing the initializer.  */
13620   if (!member_p)
13621     {
13622       if (parser->in_unbraced_linkage_specification_p)
13623         decl_specifiers->storage_class = sc_extern;
13624       decl = start_decl (declarator, decl_specifiers,
13625                          is_initialized, attributes, prefix_attributes,
13626                          &pushed_scope);
13627     }
13628   else if (scope)
13629     /* Enter the SCOPE.  That way unqualified names appearing in the
13630        initializer will be looked up in SCOPE.  */
13631     pushed_scope = push_scope (scope);
13632
13633   /* Perform deferred access control checks, now that we know in which
13634      SCOPE the declared entity resides.  */
13635   if (!member_p && decl)
13636     {
13637       tree saved_current_function_decl = NULL_TREE;
13638
13639       /* If the entity being declared is a function, pretend that we
13640          are in its scope.  If it is a `friend', it may have access to
13641          things that would not otherwise be accessible.  */
13642       if (TREE_CODE (decl) == FUNCTION_DECL)
13643         {
13644           saved_current_function_decl = current_function_decl;
13645           current_function_decl = decl;
13646         }
13647
13648       /* Perform access checks for template parameters.  */
13649       cp_parser_perform_template_parameter_access_checks (checks);
13650
13651       /* Perform the access control checks for the declarator and the
13652          decl-specifiers.  */
13653       perform_deferred_access_checks ();
13654
13655       /* Restore the saved value.  */
13656       if (TREE_CODE (decl) == FUNCTION_DECL)
13657         current_function_decl = saved_current_function_decl;
13658     }
13659
13660   /* Parse the initializer.  */
13661   initializer = NULL_TREE;
13662   is_direct_init = false;
13663   is_non_constant_init = true;
13664   if (is_initialized)
13665     {
13666       if (function_declarator_p (declarator))
13667         {
13668           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13669            if (initialization_kind == CPP_EQ)
13670              initializer = cp_parser_pure_specifier (parser);
13671            else
13672              {
13673                /* If the declaration was erroneous, we don't really
13674                   know what the user intended, so just silently
13675                   consume the initializer.  */
13676                if (decl != error_mark_node)
13677                  error_at (initializer_start_token->location,
13678                            "initializer provided for function");
13679                cp_parser_skip_to_closing_parenthesis (parser,
13680                                                       /*recovering=*/true,
13681                                                       /*or_comma=*/false,
13682                                                       /*consume_paren=*/true);
13683              }
13684         }
13685       else
13686         {
13687           /* We want to record the extra mangling scope for in-class
13688              initializers of class members and initializers of static data
13689              member templates.  The former is a C++0x feature which isn't
13690              implemented yet, and I expect it will involve deferring
13691              parsing of the initializer until end of class as with default
13692              arguments.  So right here we only handle the latter.  */
13693           if (!member_p && processing_template_decl)
13694             start_lambda_scope (decl);
13695           initializer = cp_parser_initializer (parser,
13696                                                &is_direct_init,
13697                                                &is_non_constant_init);
13698           if (!member_p && processing_template_decl)
13699             finish_lambda_scope ();
13700         }
13701     }
13702
13703   /* The old parser allows attributes to appear after a parenthesized
13704      initializer.  Mark Mitchell proposed removing this functionality
13705      on the GCC mailing lists on 2002-08-13.  This parser accepts the
13706      attributes -- but ignores them.  */
13707   if (cp_parser_allow_gnu_extensions_p (parser)
13708       && initialization_kind == CPP_OPEN_PAREN)
13709     if (cp_parser_attributes_opt (parser))
13710       warning (OPT_Wattributes,
13711                "attributes after parenthesized initializer ignored");
13712
13713   /* For an in-class declaration, use `grokfield' to create the
13714      declaration.  */
13715   if (member_p)
13716     {
13717       if (pushed_scope)
13718         {
13719           pop_scope (pushed_scope);
13720           pushed_scope = false;
13721         }
13722       decl = grokfield (declarator, decl_specifiers,
13723                         initializer, !is_non_constant_init,
13724                         /*asmspec=*/NULL_TREE,
13725                         prefix_attributes);
13726       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13727         cp_parser_save_default_args (parser, decl);
13728     }
13729
13730   /* Finish processing the declaration.  But, skip friend
13731      declarations.  */
13732   if (!friend_p && decl && decl != error_mark_node)
13733     {
13734       cp_finish_decl (decl,
13735                       initializer, !is_non_constant_init,
13736                       asm_specification,
13737                       /* If the initializer is in parentheses, then this is
13738                          a direct-initialization, which means that an
13739                          `explicit' constructor is OK.  Otherwise, an
13740                          `explicit' constructor cannot be used.  */
13741                       ((is_direct_init || !is_initialized)
13742                        ? 0 : LOOKUP_ONLYCONVERTING));
13743     }
13744   else if ((cxx_dialect != cxx98) && friend_p
13745            && decl && TREE_CODE (decl) == FUNCTION_DECL)
13746     /* Core issue #226 (C++0x only): A default template-argument
13747        shall not be specified in a friend class template
13748        declaration. */
13749     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
13750                              /*is_partial=*/0, /*is_friend_decl=*/1);
13751
13752   if (!friend_p && pushed_scope)
13753     pop_scope (pushed_scope);
13754
13755   return decl;
13756 }
13757
13758 /* Parse a declarator.
13759
13760    declarator:
13761      direct-declarator
13762      ptr-operator declarator
13763
13764    abstract-declarator:
13765      ptr-operator abstract-declarator [opt]
13766      direct-abstract-declarator
13767
13768    GNU Extensions:
13769
13770    declarator:
13771      attributes [opt] direct-declarator
13772      attributes [opt] ptr-operator declarator
13773
13774    abstract-declarator:
13775      attributes [opt] ptr-operator abstract-declarator [opt]
13776      attributes [opt] direct-abstract-declarator
13777
13778    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13779    detect constructor, destructor or conversion operators. It is set
13780    to -1 if the declarator is a name, and +1 if it is a
13781    function. Otherwise it is set to zero. Usually you just want to
13782    test for >0, but internally the negative value is used.
13783
13784    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13785    a decl-specifier-seq unless it declares a constructor, destructor,
13786    or conversion.  It might seem that we could check this condition in
13787    semantic analysis, rather than parsing, but that makes it difficult
13788    to handle something like `f()'.  We want to notice that there are
13789    no decl-specifiers, and therefore realize that this is an
13790    expression, not a declaration.)
13791
13792    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13793    the declarator is a direct-declarator of the form "(...)".
13794
13795    MEMBER_P is true iff this declarator is a member-declarator.  */
13796
13797 static cp_declarator *
13798 cp_parser_declarator (cp_parser* parser,
13799                       cp_parser_declarator_kind dcl_kind,
13800                       int* ctor_dtor_or_conv_p,
13801                       bool* parenthesized_p,
13802                       bool member_p)
13803 {
13804   cp_declarator *declarator;
13805   enum tree_code code;
13806   cp_cv_quals cv_quals;
13807   tree class_type;
13808   tree attributes = NULL_TREE;
13809
13810   /* Assume this is not a constructor, destructor, or type-conversion
13811      operator.  */
13812   if (ctor_dtor_or_conv_p)
13813     *ctor_dtor_or_conv_p = 0;
13814
13815   if (cp_parser_allow_gnu_extensions_p (parser))
13816     attributes = cp_parser_attributes_opt (parser);
13817
13818   /* Check for the ptr-operator production.  */
13819   cp_parser_parse_tentatively (parser);
13820   /* Parse the ptr-operator.  */
13821   code = cp_parser_ptr_operator (parser,
13822                                  &class_type,
13823                                  &cv_quals);
13824   /* If that worked, then we have a ptr-operator.  */
13825   if (cp_parser_parse_definitely (parser))
13826     {
13827       /* If a ptr-operator was found, then this declarator was not
13828          parenthesized.  */
13829       if (parenthesized_p)
13830         *parenthesized_p = true;
13831       /* The dependent declarator is optional if we are parsing an
13832          abstract-declarator.  */
13833       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13834         cp_parser_parse_tentatively (parser);
13835
13836       /* Parse the dependent declarator.  */
13837       declarator = cp_parser_declarator (parser, dcl_kind,
13838                                          /*ctor_dtor_or_conv_p=*/NULL,
13839                                          /*parenthesized_p=*/NULL,
13840                                          /*member_p=*/false);
13841
13842       /* If we are parsing an abstract-declarator, we must handle the
13843          case where the dependent declarator is absent.  */
13844       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13845           && !cp_parser_parse_definitely (parser))
13846         declarator = NULL;
13847
13848       declarator = cp_parser_make_indirect_declarator
13849         (code, class_type, cv_quals, declarator);
13850     }
13851   /* Everything else is a direct-declarator.  */
13852   else
13853     {
13854       if (parenthesized_p)
13855         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13856                                                    CPP_OPEN_PAREN);
13857       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13858                                                 ctor_dtor_or_conv_p,
13859                                                 member_p);
13860     }
13861
13862   if (attributes && declarator && declarator != cp_error_declarator)
13863     declarator->attributes = attributes;
13864
13865   return declarator;
13866 }
13867
13868 /* Parse a direct-declarator or direct-abstract-declarator.
13869
13870    direct-declarator:
13871      declarator-id
13872      direct-declarator ( parameter-declaration-clause )
13873        cv-qualifier-seq [opt]
13874        exception-specification [opt]
13875      direct-declarator [ constant-expression [opt] ]
13876      ( declarator )
13877
13878    direct-abstract-declarator:
13879      direct-abstract-declarator [opt]
13880        ( parameter-declaration-clause )
13881        cv-qualifier-seq [opt]
13882        exception-specification [opt]
13883      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13884      ( abstract-declarator )
13885
13886    Returns a representation of the declarator.  DCL_KIND is
13887    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13888    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13889    we are parsing a direct-declarator.  It is
13890    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13891    of ambiguity we prefer an abstract declarator, as per
13892    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13893    cp_parser_declarator.  */
13894
13895 static cp_declarator *
13896 cp_parser_direct_declarator (cp_parser* parser,
13897                              cp_parser_declarator_kind dcl_kind,
13898                              int* ctor_dtor_or_conv_p,
13899                              bool member_p)
13900 {
13901   cp_token *token;
13902   cp_declarator *declarator = NULL;
13903   tree scope = NULL_TREE;
13904   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13905   bool saved_in_declarator_p = parser->in_declarator_p;
13906   bool first = true;
13907   tree pushed_scope = NULL_TREE;
13908
13909   while (true)
13910     {
13911       /* Peek at the next token.  */
13912       token = cp_lexer_peek_token (parser->lexer);
13913       if (token->type == CPP_OPEN_PAREN)
13914         {
13915           /* This is either a parameter-declaration-clause, or a
13916              parenthesized declarator. When we know we are parsing a
13917              named declarator, it must be a parenthesized declarator
13918              if FIRST is true. For instance, `(int)' is a
13919              parameter-declaration-clause, with an omitted
13920              direct-abstract-declarator. But `((*))', is a
13921              parenthesized abstract declarator. Finally, when T is a
13922              template parameter `(T)' is a
13923              parameter-declaration-clause, and not a parenthesized
13924              named declarator.
13925
13926              We first try and parse a parameter-declaration-clause,
13927              and then try a nested declarator (if FIRST is true).
13928
13929              It is not an error for it not to be a
13930              parameter-declaration-clause, even when FIRST is
13931              false. Consider,
13932
13933                int i (int);
13934                int i (3);
13935
13936              The first is the declaration of a function while the
13937              second is the definition of a variable, including its
13938              initializer.
13939
13940              Having seen only the parenthesis, we cannot know which of
13941              these two alternatives should be selected.  Even more
13942              complex are examples like:
13943
13944                int i (int (a));
13945                int i (int (3));
13946
13947              The former is a function-declaration; the latter is a
13948              variable initialization.
13949
13950              Thus again, we try a parameter-declaration-clause, and if
13951              that fails, we back out and return.  */
13952
13953           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13954             {
13955               tree params;
13956               unsigned saved_num_template_parameter_lists;
13957               bool is_declarator = false;
13958               tree t;
13959
13960               /* In a member-declarator, the only valid interpretation
13961                  of a parenthesis is the start of a
13962                  parameter-declaration-clause.  (It is invalid to
13963                  initialize a static data member with a parenthesized
13964                  initializer; only the "=" form of initialization is
13965                  permitted.)  */
13966               if (!member_p)
13967                 cp_parser_parse_tentatively (parser);
13968
13969               /* Consume the `('.  */
13970               cp_lexer_consume_token (parser->lexer);
13971               if (first)
13972                 {
13973                   /* If this is going to be an abstract declarator, we're
13974                      in a declarator and we can't have default args.  */
13975                   parser->default_arg_ok_p = false;
13976                   parser->in_declarator_p = true;
13977                 }
13978
13979               /* Inside the function parameter list, surrounding
13980                  template-parameter-lists do not apply.  */
13981               saved_num_template_parameter_lists
13982                 = parser->num_template_parameter_lists;
13983               parser->num_template_parameter_lists = 0;
13984
13985               begin_scope (sk_function_parms, NULL_TREE);
13986
13987               /* Parse the parameter-declaration-clause.  */
13988               params = cp_parser_parameter_declaration_clause (parser);
13989
13990               parser->num_template_parameter_lists
13991                 = saved_num_template_parameter_lists;
13992
13993               /* If all went well, parse the cv-qualifier-seq and the
13994                  exception-specification.  */
13995               if (member_p || cp_parser_parse_definitely (parser))
13996                 {
13997                   cp_cv_quals cv_quals;
13998                   tree exception_specification;
13999                   tree late_return;
14000
14001                   is_declarator = true;
14002
14003                   if (ctor_dtor_or_conv_p)
14004                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14005                   first = false;
14006                   /* Consume the `)'.  */
14007                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
14008
14009                   /* Parse the cv-qualifier-seq.  */
14010                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14011                   /* And the exception-specification.  */
14012                   exception_specification
14013                     = cp_parser_exception_specification_opt (parser);
14014
14015                   late_return
14016                     = cp_parser_late_return_type_opt (parser);
14017
14018                   /* Create the function-declarator.  */
14019                   declarator = make_call_declarator (declarator,
14020                                                      params,
14021                                                      cv_quals,
14022                                                      exception_specification,
14023                                                      late_return);
14024                   /* Any subsequent parameter lists are to do with
14025                      return type, so are not those of the declared
14026                      function.  */
14027                   parser->default_arg_ok_p = false;
14028                 }
14029
14030               /* Remove the function parms from scope.  */
14031               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14032                 pop_binding (DECL_NAME (t), t);
14033               leave_scope();
14034
14035               if (is_declarator)
14036                 /* Repeat the main loop.  */
14037                 continue;
14038             }
14039
14040           /* If this is the first, we can try a parenthesized
14041              declarator.  */
14042           if (first)
14043             {
14044               bool saved_in_type_id_in_expr_p;
14045
14046               parser->default_arg_ok_p = saved_default_arg_ok_p;
14047               parser->in_declarator_p = saved_in_declarator_p;
14048
14049               /* Consume the `('.  */
14050               cp_lexer_consume_token (parser->lexer);
14051               /* Parse the nested declarator.  */
14052               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14053               parser->in_type_id_in_expr_p = true;
14054               declarator
14055                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14056                                         /*parenthesized_p=*/NULL,
14057                                         member_p);
14058               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14059               first = false;
14060               /* Expect a `)'.  */
14061               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14062                 declarator = cp_error_declarator;
14063               if (declarator == cp_error_declarator)
14064                 break;
14065
14066               goto handle_declarator;
14067             }
14068           /* Otherwise, we must be done.  */
14069           else
14070             break;
14071         }
14072       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14073                && token->type == CPP_OPEN_SQUARE)
14074         {
14075           /* Parse an array-declarator.  */
14076           tree bounds;
14077
14078           if (ctor_dtor_or_conv_p)
14079             *ctor_dtor_or_conv_p = 0;
14080
14081           first = false;
14082           parser->default_arg_ok_p = false;
14083           parser->in_declarator_p = true;
14084           /* Consume the `['.  */
14085           cp_lexer_consume_token (parser->lexer);
14086           /* Peek at the next token.  */
14087           token = cp_lexer_peek_token (parser->lexer);
14088           /* If the next token is `]', then there is no
14089              constant-expression.  */
14090           if (token->type != CPP_CLOSE_SQUARE)
14091             {
14092               bool non_constant_p;
14093
14094               bounds
14095                 = cp_parser_constant_expression (parser,
14096                                                  /*allow_non_constant=*/true,
14097                                                  &non_constant_p);
14098               if (!non_constant_p)
14099                 bounds = fold_non_dependent_expr (bounds);
14100               /* Normally, the array bound must be an integral constant
14101                  expression.  However, as an extension, we allow VLAs
14102                  in function scopes.  */
14103               else if (!parser->in_function_body)
14104                 {
14105                   error_at (token->location,
14106                             "array bound is not an integer constant");
14107                   bounds = error_mark_node;
14108                 }
14109               else if (processing_template_decl && !error_operand_p (bounds))
14110                 {
14111                   /* Remember this wasn't a constant-expression.  */
14112                   bounds = build_nop (TREE_TYPE (bounds), bounds);
14113                   TREE_SIDE_EFFECTS (bounds) = 1;
14114                 }
14115             }
14116           else
14117             bounds = NULL_TREE;
14118           /* Look for the closing `]'.  */
14119           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14120             {
14121               declarator = cp_error_declarator;
14122               break;
14123             }
14124
14125           declarator = make_array_declarator (declarator, bounds);
14126         }
14127       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14128         {
14129           {
14130             tree qualifying_scope;
14131             tree unqualified_name;
14132             special_function_kind sfk;
14133             bool abstract_ok;
14134             bool pack_expansion_p = false;
14135             cp_token *declarator_id_start_token;
14136
14137             /* Parse a declarator-id */
14138             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14139             if (abstract_ok)
14140               {
14141                 cp_parser_parse_tentatively (parser);
14142
14143                 /* If we see an ellipsis, we should be looking at a
14144                    parameter pack. */
14145                 if (token->type == CPP_ELLIPSIS)
14146                   {
14147                     /* Consume the `...' */
14148                     cp_lexer_consume_token (parser->lexer);
14149
14150                     pack_expansion_p = true;
14151                   }
14152               }
14153
14154             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14155             unqualified_name
14156               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14157             qualifying_scope = parser->scope;
14158             if (abstract_ok)
14159               {
14160                 bool okay = false;
14161
14162                 if (!unqualified_name && pack_expansion_p)
14163                   {
14164                     /* Check whether an error occurred. */
14165                     okay = !cp_parser_error_occurred (parser);
14166
14167                     /* We already consumed the ellipsis to mark a
14168                        parameter pack, but we have no way to report it,
14169                        so abort the tentative parse. We will be exiting
14170                        immediately anyway. */
14171                     cp_parser_abort_tentative_parse (parser);
14172                   }
14173                 else
14174                   okay = cp_parser_parse_definitely (parser);
14175
14176                 if (!okay)
14177                   unqualified_name = error_mark_node;
14178                 else if (unqualified_name
14179                          && (qualifying_scope
14180                              || (TREE_CODE (unqualified_name)
14181                                  != IDENTIFIER_NODE)))
14182                   {
14183                     cp_parser_error (parser, "expected unqualified-id");
14184                     unqualified_name = error_mark_node;
14185                   }
14186               }
14187
14188             if (!unqualified_name)
14189               return NULL;
14190             if (unqualified_name == error_mark_node)
14191               {
14192                 declarator = cp_error_declarator;
14193                 pack_expansion_p = false;
14194                 declarator->parameter_pack_p = false;
14195                 break;
14196               }
14197
14198             if (qualifying_scope && at_namespace_scope_p ()
14199                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14200               {
14201                 /* In the declaration of a member of a template class
14202                    outside of the class itself, the SCOPE will sometimes
14203                    be a TYPENAME_TYPE.  For example, given:
14204
14205                    template <typename T>
14206                    int S<T>::R::i = 3;
14207
14208                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
14209                    this context, we must resolve S<T>::R to an ordinary
14210                    type, rather than a typename type.
14211
14212                    The reason we normally avoid resolving TYPENAME_TYPEs
14213                    is that a specialization of `S' might render
14214                    `S<T>::R' not a type.  However, if `S' is
14215                    specialized, then this `i' will not be used, so there
14216                    is no harm in resolving the types here.  */
14217                 tree type;
14218
14219                 /* Resolve the TYPENAME_TYPE.  */
14220                 type = resolve_typename_type (qualifying_scope,
14221                                               /*only_current_p=*/false);
14222                 /* If that failed, the declarator is invalid.  */
14223                 if (TREE_CODE (type) == TYPENAME_TYPE)
14224                   {
14225                     if (typedef_variant_p (type))
14226                       error_at (declarator_id_start_token->location,
14227                                 "cannot define member of dependent typedef "
14228                                 "%qT", type);
14229                     else
14230                       error_at (declarator_id_start_token->location,
14231                                 "%<%T::%E%> is not a type",
14232                                 TYPE_CONTEXT (qualifying_scope),
14233                                 TYPE_IDENTIFIER (qualifying_scope));
14234                   }
14235                 qualifying_scope = type;
14236               }
14237
14238             sfk = sfk_none;
14239
14240             if (unqualified_name)
14241               {
14242                 tree class_type;
14243
14244                 if (qualifying_scope
14245                     && CLASS_TYPE_P (qualifying_scope))
14246                   class_type = qualifying_scope;
14247                 else
14248                   class_type = current_class_type;
14249
14250                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14251                   {
14252                     tree name_type = TREE_TYPE (unqualified_name);
14253                     if (class_type && same_type_p (name_type, class_type))
14254                       {
14255                         if (qualifying_scope
14256                             && CLASSTYPE_USE_TEMPLATE (name_type))
14257                           {
14258                             error_at (declarator_id_start_token->location,
14259                                       "invalid use of constructor as a template");
14260                             inform (declarator_id_start_token->location,
14261                                     "use %<%T::%D%> instead of %<%T::%D%> to "
14262                                     "name the constructor in a qualified name",
14263                                     class_type,
14264                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14265                                     class_type, name_type);
14266                             declarator = cp_error_declarator;
14267                             break;
14268                           }
14269                         else
14270                           unqualified_name = constructor_name (class_type);
14271                       }
14272                     else
14273                       {
14274                         /* We do not attempt to print the declarator
14275                            here because we do not have enough
14276                            information about its original syntactic
14277                            form.  */
14278                         cp_parser_error (parser, "invalid declarator");
14279                         declarator = cp_error_declarator;
14280                         break;
14281                       }
14282                   }
14283
14284                 if (class_type)
14285                   {
14286                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14287                       sfk = sfk_destructor;
14288                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14289                       sfk = sfk_conversion;
14290                     else if (/* There's no way to declare a constructor
14291                                 for an anonymous type, even if the type
14292                                 got a name for linkage purposes.  */
14293                              !TYPE_WAS_ANONYMOUS (class_type)
14294                              && constructor_name_p (unqualified_name,
14295                                                     class_type))
14296                       {
14297                         unqualified_name = constructor_name (class_type);
14298                         sfk = sfk_constructor;
14299                       }
14300                     else if (is_overloaded_fn (unqualified_name)
14301                              && DECL_CONSTRUCTOR_P (get_first_fn
14302                                                     (unqualified_name)))
14303                       sfk = sfk_constructor;
14304
14305                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
14306                       *ctor_dtor_or_conv_p = -1;
14307                   }
14308               }
14309             declarator = make_id_declarator (qualifying_scope,
14310                                              unqualified_name,
14311                                              sfk);
14312             declarator->id_loc = token->location;
14313             declarator->parameter_pack_p = pack_expansion_p;
14314
14315             if (pack_expansion_p)
14316               maybe_warn_variadic_templates ();
14317           }
14318
14319         handle_declarator:;
14320           scope = get_scope_of_declarator (declarator);
14321           if (scope)
14322             /* Any names that appear after the declarator-id for a
14323                member are looked up in the containing scope.  */
14324             pushed_scope = push_scope (scope);
14325           parser->in_declarator_p = true;
14326           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14327               || (declarator && declarator->kind == cdk_id))
14328             /* Default args are only allowed on function
14329                declarations.  */
14330             parser->default_arg_ok_p = saved_default_arg_ok_p;
14331           else
14332             parser->default_arg_ok_p = false;
14333
14334           first = false;
14335         }
14336       /* We're done.  */
14337       else
14338         break;
14339     }
14340
14341   /* For an abstract declarator, we might wind up with nothing at this
14342      point.  That's an error; the declarator is not optional.  */
14343   if (!declarator)
14344     cp_parser_error (parser, "expected declarator");
14345
14346   /* If we entered a scope, we must exit it now.  */
14347   if (pushed_scope)
14348     pop_scope (pushed_scope);
14349
14350   parser->default_arg_ok_p = saved_default_arg_ok_p;
14351   parser->in_declarator_p = saved_in_declarator_p;
14352
14353   return declarator;
14354 }
14355
14356 /* Parse a ptr-operator.
14357
14358    ptr-operator:
14359      * cv-qualifier-seq [opt]
14360      &
14361      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14362
14363    GNU Extension:
14364
14365    ptr-operator:
14366      & cv-qualifier-seq [opt]
14367
14368    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14369    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14370    an rvalue reference. In the case of a pointer-to-member, *TYPE is
14371    filled in with the TYPE containing the member.  *CV_QUALS is
14372    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14373    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
14374    Note that the tree codes returned by this function have nothing
14375    to do with the types of trees that will be eventually be created
14376    to represent the pointer or reference type being parsed. They are
14377    just constants with suggestive names. */
14378 static enum tree_code
14379 cp_parser_ptr_operator (cp_parser* parser,
14380                         tree* type,
14381                         cp_cv_quals *cv_quals)
14382 {
14383   enum tree_code code = ERROR_MARK;
14384   cp_token *token;
14385
14386   /* Assume that it's not a pointer-to-member.  */
14387   *type = NULL_TREE;
14388   /* And that there are no cv-qualifiers.  */
14389   *cv_quals = TYPE_UNQUALIFIED;
14390
14391   /* Peek at the next token.  */
14392   token = cp_lexer_peek_token (parser->lexer);
14393
14394   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
14395   if (token->type == CPP_MULT)
14396     code = INDIRECT_REF;
14397   else if (token->type == CPP_AND)
14398     code = ADDR_EXPR;
14399   else if ((cxx_dialect != cxx98) &&
14400            token->type == CPP_AND_AND) /* C++0x only */
14401     code = NON_LVALUE_EXPR;
14402
14403   if (code != ERROR_MARK)
14404     {
14405       /* Consume the `*', `&' or `&&'.  */
14406       cp_lexer_consume_token (parser->lexer);
14407
14408       /* A `*' can be followed by a cv-qualifier-seq, and so can a
14409          `&', if we are allowing GNU extensions.  (The only qualifier
14410          that can legally appear after `&' is `restrict', but that is
14411          enforced during semantic analysis.  */
14412       if (code == INDIRECT_REF
14413           || cp_parser_allow_gnu_extensions_p (parser))
14414         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14415     }
14416   else
14417     {
14418       /* Try the pointer-to-member case.  */
14419       cp_parser_parse_tentatively (parser);
14420       /* Look for the optional `::' operator.  */
14421       cp_parser_global_scope_opt (parser,
14422                                   /*current_scope_valid_p=*/false);
14423       /* Look for the nested-name specifier.  */
14424       token = cp_lexer_peek_token (parser->lexer);
14425       cp_parser_nested_name_specifier (parser,
14426                                        /*typename_keyword_p=*/false,
14427                                        /*check_dependency_p=*/true,
14428                                        /*type_p=*/false,
14429                                        /*is_declaration=*/false);
14430       /* If we found it, and the next token is a `*', then we are
14431          indeed looking at a pointer-to-member operator.  */
14432       if (!cp_parser_error_occurred (parser)
14433           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14434         {
14435           /* Indicate that the `*' operator was used.  */
14436           code = INDIRECT_REF;
14437
14438           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14439             error_at (token->location, "%qD is a namespace", parser->scope);
14440           else
14441             {
14442               /* The type of which the member is a member is given by the
14443                  current SCOPE.  */
14444               *type = parser->scope;
14445               /* The next name will not be qualified.  */
14446               parser->scope = NULL_TREE;
14447               parser->qualifying_scope = NULL_TREE;
14448               parser->object_scope = NULL_TREE;
14449               /* Look for the optional cv-qualifier-seq.  */
14450               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14451             }
14452         }
14453       /* If that didn't work we don't have a ptr-operator.  */
14454       if (!cp_parser_parse_definitely (parser))
14455         cp_parser_error (parser, "expected ptr-operator");
14456     }
14457
14458   return code;
14459 }
14460
14461 /* Parse an (optional) cv-qualifier-seq.
14462
14463    cv-qualifier-seq:
14464      cv-qualifier cv-qualifier-seq [opt]
14465
14466    cv-qualifier:
14467      const
14468      volatile
14469
14470    GNU Extension:
14471
14472    cv-qualifier:
14473      __restrict__
14474
14475    Returns a bitmask representing the cv-qualifiers.  */
14476
14477 static cp_cv_quals
14478 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14479 {
14480   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14481
14482   while (true)
14483     {
14484       cp_token *token;
14485       cp_cv_quals cv_qualifier;
14486
14487       /* Peek at the next token.  */
14488       token = cp_lexer_peek_token (parser->lexer);
14489       /* See if it's a cv-qualifier.  */
14490       switch (token->keyword)
14491         {
14492         case RID_CONST:
14493           cv_qualifier = TYPE_QUAL_CONST;
14494           break;
14495
14496         case RID_VOLATILE:
14497           cv_qualifier = TYPE_QUAL_VOLATILE;
14498           break;
14499
14500         case RID_RESTRICT:
14501           cv_qualifier = TYPE_QUAL_RESTRICT;
14502           break;
14503
14504         default:
14505           cv_qualifier = TYPE_UNQUALIFIED;
14506           break;
14507         }
14508
14509       if (!cv_qualifier)
14510         break;
14511
14512       if (cv_quals & cv_qualifier)
14513         {
14514           error_at (token->location, "duplicate cv-qualifier");
14515           cp_lexer_purge_token (parser->lexer);
14516         }
14517       else
14518         {
14519           cp_lexer_consume_token (parser->lexer);
14520           cv_quals |= cv_qualifier;
14521         }
14522     }
14523
14524   return cv_quals;
14525 }
14526
14527 /* Parse a late-specified return type, if any.  This is not a separate
14528    non-terminal, but part of a function declarator, which looks like
14529
14530    -> trailing-type-specifier-seq abstract-declarator(opt)
14531
14532    Returns the type indicated by the type-id.  */
14533
14534 static tree
14535 cp_parser_late_return_type_opt (cp_parser* parser)
14536 {
14537   cp_token *token;
14538
14539   /* Peek at the next token.  */
14540   token = cp_lexer_peek_token (parser->lexer);
14541   /* A late-specified return type is indicated by an initial '->'. */
14542   if (token->type != CPP_DEREF)
14543     return NULL_TREE;
14544
14545   /* Consume the ->.  */
14546   cp_lexer_consume_token (parser->lexer);
14547
14548   return cp_parser_trailing_type_id (parser);
14549 }
14550
14551 /* Parse a declarator-id.
14552
14553    declarator-id:
14554      id-expression
14555      :: [opt] nested-name-specifier [opt] type-name
14556
14557    In the `id-expression' case, the value returned is as for
14558    cp_parser_id_expression if the id-expression was an unqualified-id.
14559    If the id-expression was a qualified-id, then a SCOPE_REF is
14560    returned.  The first operand is the scope (either a NAMESPACE_DECL
14561    or TREE_TYPE), but the second is still just a representation of an
14562    unqualified-id.  */
14563
14564 static tree
14565 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14566 {
14567   tree id;
14568   /* The expression must be an id-expression.  Assume that qualified
14569      names are the names of types so that:
14570
14571        template <class T>
14572        int S<T>::R::i = 3;
14573
14574      will work; we must treat `S<T>::R' as the name of a type.
14575      Similarly, assume that qualified names are templates, where
14576      required, so that:
14577
14578        template <class T>
14579        int S<T>::R<T>::i = 3;
14580
14581      will work, too.  */
14582   id = cp_parser_id_expression (parser,
14583                                 /*template_keyword_p=*/false,
14584                                 /*check_dependency_p=*/false,
14585                                 /*template_p=*/NULL,
14586                                 /*declarator_p=*/true,
14587                                 optional_p);
14588   if (id && BASELINK_P (id))
14589     id = BASELINK_FUNCTIONS (id);
14590   return id;
14591 }
14592
14593 /* Parse a type-id.
14594
14595    type-id:
14596      type-specifier-seq abstract-declarator [opt]
14597
14598    Returns the TYPE specified.  */
14599
14600 static tree
14601 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14602                      bool is_trailing_return)
14603 {
14604   cp_decl_specifier_seq type_specifier_seq;
14605   cp_declarator *abstract_declarator;
14606
14607   /* Parse the type-specifier-seq.  */
14608   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14609                                 is_trailing_return,
14610                                 &type_specifier_seq);
14611   if (type_specifier_seq.type == error_mark_node)
14612     return error_mark_node;
14613
14614   /* There might or might not be an abstract declarator.  */
14615   cp_parser_parse_tentatively (parser);
14616   /* Look for the declarator.  */
14617   abstract_declarator
14618     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14619                             /*parenthesized_p=*/NULL,
14620                             /*member_p=*/false);
14621   /* Check to see if there really was a declarator.  */
14622   if (!cp_parser_parse_definitely (parser))
14623     abstract_declarator = NULL;
14624
14625   if (type_specifier_seq.type
14626       && type_uses_auto (type_specifier_seq.type))
14627     {
14628       /* A type-id with type 'auto' is only ok if the abstract declarator
14629          is a function declarator with a late-specified return type.  */
14630       if (abstract_declarator
14631           && abstract_declarator->kind == cdk_function
14632           && abstract_declarator->u.function.late_return_type)
14633         /* OK */;
14634       else
14635         {
14636           error ("invalid use of %<auto%>");
14637           return error_mark_node;
14638         }
14639     }
14640   
14641   return groktypename (&type_specifier_seq, abstract_declarator,
14642                        is_template_arg);
14643 }
14644
14645 static tree cp_parser_type_id (cp_parser *parser)
14646 {
14647   return cp_parser_type_id_1 (parser, false, false);
14648 }
14649
14650 static tree cp_parser_template_type_arg (cp_parser *parser)
14651 {
14652   return cp_parser_type_id_1 (parser, true, false);
14653 }
14654
14655 static tree cp_parser_trailing_type_id (cp_parser *parser)
14656 {
14657   return cp_parser_type_id_1 (parser, false, true);
14658 }
14659
14660 /* Parse a type-specifier-seq.
14661
14662    type-specifier-seq:
14663      type-specifier type-specifier-seq [opt]
14664
14665    GNU extension:
14666
14667    type-specifier-seq:
14668      attributes type-specifier-seq [opt]
14669
14670    If IS_DECLARATION is true, we are at the start of a "condition" or
14671    exception-declaration, so we might be followed by a declarator-id.
14672
14673    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14674    i.e. we've just seen "->".
14675
14676    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
14677
14678 static void
14679 cp_parser_type_specifier_seq (cp_parser* parser,
14680                               bool is_declaration,
14681                               bool is_trailing_return,
14682                               cp_decl_specifier_seq *type_specifier_seq)
14683 {
14684   bool seen_type_specifier = false;
14685   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14686   cp_token *start_token = NULL;
14687
14688   /* Clear the TYPE_SPECIFIER_SEQ.  */
14689   clear_decl_specs (type_specifier_seq);
14690
14691   /* In the context of a trailing return type, enum E { } is an
14692      elaborated-type-specifier followed by a function-body, not an
14693      enum-specifier.  */
14694   if (is_trailing_return)
14695     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14696
14697   /* Parse the type-specifiers and attributes.  */
14698   while (true)
14699     {
14700       tree type_specifier;
14701       bool is_cv_qualifier;
14702
14703       /* Check for attributes first.  */
14704       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14705         {
14706           type_specifier_seq->attributes =
14707             chainon (type_specifier_seq->attributes,
14708                      cp_parser_attributes_opt (parser));
14709           continue;
14710         }
14711
14712       /* record the token of the beginning of the type specifier seq,
14713          for error reporting purposes*/
14714      if (!start_token)
14715        start_token = cp_lexer_peek_token (parser->lexer);
14716
14717       /* Look for the type-specifier.  */
14718       type_specifier = cp_parser_type_specifier (parser,
14719                                                  flags,
14720                                                  type_specifier_seq,
14721                                                  /*is_declaration=*/false,
14722                                                  NULL,
14723                                                  &is_cv_qualifier);
14724       if (!type_specifier)
14725         {
14726           /* If the first type-specifier could not be found, this is not a
14727              type-specifier-seq at all.  */
14728           if (!seen_type_specifier)
14729             {
14730               cp_parser_error (parser, "expected type-specifier");
14731               type_specifier_seq->type = error_mark_node;
14732               return;
14733             }
14734           /* If subsequent type-specifiers could not be found, the
14735              type-specifier-seq is complete.  */
14736           break;
14737         }
14738
14739       seen_type_specifier = true;
14740       /* The standard says that a condition can be:
14741
14742             type-specifier-seq declarator = assignment-expression
14743
14744          However, given:
14745
14746            struct S {};
14747            if (int S = ...)
14748
14749          we should treat the "S" as a declarator, not as a
14750          type-specifier.  The standard doesn't say that explicitly for
14751          type-specifier-seq, but it does say that for
14752          decl-specifier-seq in an ordinary declaration.  Perhaps it
14753          would be clearer just to allow a decl-specifier-seq here, and
14754          then add a semantic restriction that if any decl-specifiers
14755          that are not type-specifiers appear, the program is invalid.  */
14756       if (is_declaration && !is_cv_qualifier)
14757         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14758     }
14759
14760   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14761 }
14762
14763 /* Parse a parameter-declaration-clause.
14764
14765    parameter-declaration-clause:
14766      parameter-declaration-list [opt] ... [opt]
14767      parameter-declaration-list , ...
14768
14769    Returns a representation for the parameter declarations.  A return
14770    value of NULL indicates a parameter-declaration-clause consisting
14771    only of an ellipsis.  */
14772
14773 static tree
14774 cp_parser_parameter_declaration_clause (cp_parser* parser)
14775 {
14776   tree parameters;
14777   cp_token *token;
14778   bool ellipsis_p;
14779   bool is_error;
14780
14781   /* Peek at the next token.  */
14782   token = cp_lexer_peek_token (parser->lexer);
14783   /* Check for trivial parameter-declaration-clauses.  */
14784   if (token->type == CPP_ELLIPSIS)
14785     {
14786       /* Consume the `...' token.  */
14787       cp_lexer_consume_token (parser->lexer);
14788       return NULL_TREE;
14789     }
14790   else if (token->type == CPP_CLOSE_PAREN)
14791     /* There are no parameters.  */
14792     {
14793 #ifndef NO_IMPLICIT_EXTERN_C
14794       if (in_system_header && current_class_type == NULL
14795           && current_lang_name == lang_name_c)
14796         return NULL_TREE;
14797       else
14798 #endif
14799         return void_list_node;
14800     }
14801   /* Check for `(void)', too, which is a special case.  */
14802   else if (token->keyword == RID_VOID
14803            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14804                == CPP_CLOSE_PAREN))
14805     {
14806       /* Consume the `void' token.  */
14807       cp_lexer_consume_token (parser->lexer);
14808       /* There are no parameters.  */
14809       return void_list_node;
14810     }
14811
14812   /* Parse the parameter-declaration-list.  */
14813   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14814   /* If a parse error occurred while parsing the
14815      parameter-declaration-list, then the entire
14816      parameter-declaration-clause is erroneous.  */
14817   if (is_error)
14818     return NULL;
14819
14820   /* Peek at the next token.  */
14821   token = cp_lexer_peek_token (parser->lexer);
14822   /* If it's a `,', the clause should terminate with an ellipsis.  */
14823   if (token->type == CPP_COMMA)
14824     {
14825       /* Consume the `,'.  */
14826       cp_lexer_consume_token (parser->lexer);
14827       /* Expect an ellipsis.  */
14828       ellipsis_p
14829         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14830     }
14831   /* It might also be `...' if the optional trailing `,' was
14832      omitted.  */
14833   else if (token->type == CPP_ELLIPSIS)
14834     {
14835       /* Consume the `...' token.  */
14836       cp_lexer_consume_token (parser->lexer);
14837       /* And remember that we saw it.  */
14838       ellipsis_p = true;
14839     }
14840   else
14841     ellipsis_p = false;
14842
14843   /* Finish the parameter list.  */
14844   if (!ellipsis_p)
14845     parameters = chainon (parameters, void_list_node);
14846
14847   return parameters;
14848 }
14849
14850 /* Parse a parameter-declaration-list.
14851
14852    parameter-declaration-list:
14853      parameter-declaration
14854      parameter-declaration-list , parameter-declaration
14855
14856    Returns a representation of the parameter-declaration-list, as for
14857    cp_parser_parameter_declaration_clause.  However, the
14858    `void_list_node' is never appended to the list.  Upon return,
14859    *IS_ERROR will be true iff an error occurred.  */
14860
14861 static tree
14862 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14863 {
14864   tree parameters = NULL_TREE;
14865   tree *tail = &parameters; 
14866   bool saved_in_unbraced_linkage_specification_p;
14867   int index = 0;
14868
14869   /* Assume all will go well.  */
14870   *is_error = false;
14871   /* The special considerations that apply to a function within an
14872      unbraced linkage specifications do not apply to the parameters
14873      to the function.  */
14874   saved_in_unbraced_linkage_specification_p 
14875     = parser->in_unbraced_linkage_specification_p;
14876   parser->in_unbraced_linkage_specification_p = false;
14877
14878   /* Look for more parameters.  */
14879   while (true)
14880     {
14881       cp_parameter_declarator *parameter;
14882       tree decl = error_mark_node;
14883       bool parenthesized_p;
14884       /* Parse the parameter.  */
14885       parameter
14886         = cp_parser_parameter_declaration (parser,
14887                                            /*template_parm_p=*/false,
14888                                            &parenthesized_p);
14889
14890       /* We don't know yet if the enclosing context is deprecated, so wait
14891          and warn in grokparms if appropriate.  */
14892       deprecated_state = DEPRECATED_SUPPRESS;
14893
14894       if (parameter)
14895         decl = grokdeclarator (parameter->declarator,
14896                                &parameter->decl_specifiers,
14897                                PARM,
14898                                parameter->default_argument != NULL_TREE,
14899                                &parameter->decl_specifiers.attributes);
14900
14901       deprecated_state = DEPRECATED_NORMAL;
14902
14903       /* If a parse error occurred parsing the parameter declaration,
14904          then the entire parameter-declaration-list is erroneous.  */
14905       if (decl == error_mark_node)
14906         {
14907           *is_error = true;
14908           parameters = error_mark_node;
14909           break;
14910         }
14911
14912       if (parameter->decl_specifiers.attributes)
14913         cplus_decl_attributes (&decl,
14914                                parameter->decl_specifiers.attributes,
14915                                0);
14916       if (DECL_NAME (decl))
14917         decl = pushdecl (decl);
14918
14919       if (decl != error_mark_node)
14920         {
14921           retrofit_lang_decl (decl);
14922           DECL_PARM_INDEX (decl) = ++index;
14923         }
14924
14925       /* Add the new parameter to the list.  */
14926       *tail = build_tree_list (parameter->default_argument, decl);
14927       tail = &TREE_CHAIN (*tail);
14928
14929       /* Peek at the next token.  */
14930       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14931           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14932           /* These are for Objective-C++ */
14933           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14934           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14935         /* The parameter-declaration-list is complete.  */
14936         break;
14937       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14938         {
14939           cp_token *token;
14940
14941           /* Peek at the next token.  */
14942           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14943           /* If it's an ellipsis, then the list is complete.  */
14944           if (token->type == CPP_ELLIPSIS)
14945             break;
14946           /* Otherwise, there must be more parameters.  Consume the
14947              `,'.  */
14948           cp_lexer_consume_token (parser->lexer);
14949           /* When parsing something like:
14950
14951                 int i(float f, double d)
14952
14953              we can tell after seeing the declaration for "f" that we
14954              are not looking at an initialization of a variable "i",
14955              but rather at the declaration of a function "i".
14956
14957              Due to the fact that the parsing of template arguments
14958              (as specified to a template-id) requires backtracking we
14959              cannot use this technique when inside a template argument
14960              list.  */
14961           if (!parser->in_template_argument_list_p
14962               && !parser->in_type_id_in_expr_p
14963               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14964               /* However, a parameter-declaration of the form
14965                  "foat(f)" (which is a valid declaration of a
14966                  parameter "f") can also be interpreted as an
14967                  expression (the conversion of "f" to "float").  */
14968               && !parenthesized_p)
14969             cp_parser_commit_to_tentative_parse (parser);
14970         }
14971       else
14972         {
14973           cp_parser_error (parser, "expected %<,%> or %<...%>");
14974           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14975             cp_parser_skip_to_closing_parenthesis (parser,
14976                                                    /*recovering=*/true,
14977                                                    /*or_comma=*/false,
14978                                                    /*consume_paren=*/false);
14979           break;
14980         }
14981     }
14982
14983   parser->in_unbraced_linkage_specification_p
14984     = saved_in_unbraced_linkage_specification_p;
14985
14986   return parameters;
14987 }
14988
14989 /* Parse a parameter declaration.
14990
14991    parameter-declaration:
14992      decl-specifier-seq ... [opt] declarator
14993      decl-specifier-seq declarator = assignment-expression
14994      decl-specifier-seq ... [opt] abstract-declarator [opt]
14995      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14996
14997    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14998    declares a template parameter.  (In that case, a non-nested `>'
14999    token encountered during the parsing of the assignment-expression
15000    is not interpreted as a greater-than operator.)
15001
15002    Returns a representation of the parameter, or NULL if an error
15003    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15004    true iff the declarator is of the form "(p)".  */
15005
15006 static cp_parameter_declarator *
15007 cp_parser_parameter_declaration (cp_parser *parser,
15008                                  bool template_parm_p,
15009                                  bool *parenthesized_p)
15010 {
15011   int declares_class_or_enum;
15012   cp_decl_specifier_seq decl_specifiers;
15013   cp_declarator *declarator;
15014   tree default_argument;
15015   cp_token *token = NULL, *declarator_token_start = NULL;
15016   const char *saved_message;
15017
15018   /* In a template parameter, `>' is not an operator.
15019
15020      [temp.param]
15021
15022      When parsing a default template-argument for a non-type
15023      template-parameter, the first non-nested `>' is taken as the end
15024      of the template parameter-list rather than a greater-than
15025      operator.  */
15026
15027   /* Type definitions may not appear in parameter types.  */
15028   saved_message = parser->type_definition_forbidden_message;
15029   parser->type_definition_forbidden_message
15030     = G_("types may not be defined in parameter types");
15031
15032   /* Parse the declaration-specifiers.  */
15033   cp_parser_decl_specifier_seq (parser,
15034                                 CP_PARSER_FLAGS_NONE,
15035                                 &decl_specifiers,
15036                                 &declares_class_or_enum);
15037
15038   /* Complain about missing 'typename' or other invalid type names.  */
15039   if (!decl_specifiers.any_type_specifiers_p)
15040     cp_parser_parse_and_diagnose_invalid_type_name (parser);
15041
15042   /* If an error occurred, there's no reason to attempt to parse the
15043      rest of the declaration.  */
15044   if (cp_parser_error_occurred (parser))
15045     {
15046       parser->type_definition_forbidden_message = saved_message;
15047       return NULL;
15048     }
15049
15050   /* Peek at the next token.  */
15051   token = cp_lexer_peek_token (parser->lexer);
15052
15053   /* If the next token is a `)', `,', `=', `>', or `...', then there
15054      is no declarator. However, when variadic templates are enabled,
15055      there may be a declarator following `...'.  */
15056   if (token->type == CPP_CLOSE_PAREN
15057       || token->type == CPP_COMMA
15058       || token->type == CPP_EQ
15059       || token->type == CPP_GREATER)
15060     {
15061       declarator = NULL;
15062       if (parenthesized_p)
15063         *parenthesized_p = false;
15064     }
15065   /* Otherwise, there should be a declarator.  */
15066   else
15067     {
15068       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15069       parser->default_arg_ok_p = false;
15070
15071       /* After seeing a decl-specifier-seq, if the next token is not a
15072          "(", there is no possibility that the code is a valid
15073          expression.  Therefore, if parsing tentatively, we commit at
15074          this point.  */
15075       if (!parser->in_template_argument_list_p
15076           /* In an expression context, having seen:
15077
15078                (int((char ...
15079
15080              we cannot be sure whether we are looking at a
15081              function-type (taking a "char" as a parameter) or a cast
15082              of some object of type "char" to "int".  */
15083           && !parser->in_type_id_in_expr_p
15084           && cp_parser_uncommitted_to_tentative_parse_p (parser)
15085           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15086         cp_parser_commit_to_tentative_parse (parser);
15087       /* Parse the declarator.  */
15088       declarator_token_start = token;
15089       declarator = cp_parser_declarator (parser,
15090                                          CP_PARSER_DECLARATOR_EITHER,
15091                                          /*ctor_dtor_or_conv_p=*/NULL,
15092                                          parenthesized_p,
15093                                          /*member_p=*/false);
15094       parser->default_arg_ok_p = saved_default_arg_ok_p;
15095       /* After the declarator, allow more attributes.  */
15096       decl_specifiers.attributes
15097         = chainon (decl_specifiers.attributes,
15098                    cp_parser_attributes_opt (parser));
15099     }
15100
15101   /* If the next token is an ellipsis, and we have not seen a
15102      declarator name, and the type of the declarator contains parameter
15103      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15104      a parameter pack expansion expression. Otherwise, leave the
15105      ellipsis for a C-style variadic function. */
15106   token = cp_lexer_peek_token (parser->lexer);
15107   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15108     {
15109       tree type = decl_specifiers.type;
15110
15111       if (type && DECL_P (type))
15112         type = TREE_TYPE (type);
15113
15114       if (type
15115           && TREE_CODE (type) != TYPE_PACK_EXPANSION
15116           && declarator_can_be_parameter_pack (declarator)
15117           && (!declarator || !declarator->parameter_pack_p)
15118           && uses_parameter_packs (type))
15119         {
15120           /* Consume the `...'. */
15121           cp_lexer_consume_token (parser->lexer);
15122           maybe_warn_variadic_templates ();
15123           
15124           /* Build a pack expansion type */
15125           if (declarator)
15126             declarator->parameter_pack_p = true;
15127           else
15128             decl_specifiers.type = make_pack_expansion (type);
15129         }
15130     }
15131
15132   /* The restriction on defining new types applies only to the type
15133      of the parameter, not to the default argument.  */
15134   parser->type_definition_forbidden_message = saved_message;
15135
15136   /* If the next token is `=', then process a default argument.  */
15137   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15138     {
15139       /* Consume the `='.  */
15140       cp_lexer_consume_token (parser->lexer);
15141
15142       /* If we are defining a class, then the tokens that make up the
15143          default argument must be saved and processed later.  */
15144       if (!template_parm_p && at_class_scope_p ()
15145           && TYPE_BEING_DEFINED (current_class_type)
15146           && !LAMBDA_TYPE_P (current_class_type))
15147         {
15148           unsigned depth = 0;
15149           int maybe_template_id = 0;
15150           cp_token *first_token;
15151           cp_token *token;
15152
15153           /* Add tokens until we have processed the entire default
15154              argument.  We add the range [first_token, token).  */
15155           first_token = cp_lexer_peek_token (parser->lexer);
15156           while (true)
15157             {
15158               bool done = false;
15159
15160               /* Peek at the next token.  */
15161               token = cp_lexer_peek_token (parser->lexer);
15162               /* What we do depends on what token we have.  */
15163               switch (token->type)
15164                 {
15165                   /* In valid code, a default argument must be
15166                      immediately followed by a `,' `)', or `...'.  */
15167                 case CPP_COMMA:
15168                   if (depth == 0 && maybe_template_id)
15169                     {
15170                       /* If we've seen a '<', we might be in a
15171                          template-argument-list.  Until Core issue 325 is
15172                          resolved, we don't know how this situation ought
15173                          to be handled, so try to DTRT.  We check whether
15174                          what comes after the comma is a valid parameter
15175                          declaration list.  If it is, then the comma ends
15176                          the default argument; otherwise the default
15177                          argument continues.  */
15178                       bool error = false;
15179
15180                       /* Set ITALP so cp_parser_parameter_declaration_list
15181                          doesn't decide to commit to this parse.  */
15182                       bool saved_italp = parser->in_template_argument_list_p;
15183                       parser->in_template_argument_list_p = true;
15184
15185                       cp_parser_parse_tentatively (parser);
15186                       cp_lexer_consume_token (parser->lexer);
15187                       cp_parser_parameter_declaration_list (parser, &error);
15188                       if (!cp_parser_error_occurred (parser) && !error)
15189                         done = true;
15190                       cp_parser_abort_tentative_parse (parser);
15191
15192                       parser->in_template_argument_list_p = saved_italp;
15193                       break;
15194                     }
15195                 case CPP_CLOSE_PAREN:
15196                 case CPP_ELLIPSIS:
15197                   /* If we run into a non-nested `;', `}', or `]',
15198                      then the code is invalid -- but the default
15199                      argument is certainly over.  */
15200                 case CPP_SEMICOLON:
15201                 case CPP_CLOSE_BRACE:
15202                 case CPP_CLOSE_SQUARE:
15203                   if (depth == 0)
15204                     done = true;
15205                   /* Update DEPTH, if necessary.  */
15206                   else if (token->type == CPP_CLOSE_PAREN
15207                            || token->type == CPP_CLOSE_BRACE
15208                            || token->type == CPP_CLOSE_SQUARE)
15209                     --depth;
15210                   break;
15211
15212                 case CPP_OPEN_PAREN:
15213                 case CPP_OPEN_SQUARE:
15214                 case CPP_OPEN_BRACE:
15215                   ++depth;
15216                   break;
15217
15218                 case CPP_LESS:
15219                   if (depth == 0)
15220                     /* This might be the comparison operator, or it might
15221                        start a template argument list.  */
15222                     ++maybe_template_id;
15223                   break;
15224
15225                 case CPP_RSHIFT:
15226                   if (cxx_dialect == cxx98)
15227                     break;
15228                   /* Fall through for C++0x, which treats the `>>'
15229                      operator like two `>' tokens in certain
15230                      cases.  */
15231
15232                 case CPP_GREATER:
15233                   if (depth == 0)
15234                     {
15235                       /* This might be an operator, or it might close a
15236                          template argument list.  But if a previous '<'
15237                          started a template argument list, this will have
15238                          closed it, so we can't be in one anymore.  */
15239                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15240                       if (maybe_template_id < 0)
15241                         maybe_template_id = 0;
15242                     }
15243                   break;
15244
15245                   /* If we run out of tokens, issue an error message.  */
15246                 case CPP_EOF:
15247                 case CPP_PRAGMA_EOL:
15248                   error_at (token->location, "file ends in default argument");
15249                   done = true;
15250                   break;
15251
15252                 case CPP_NAME:
15253                 case CPP_SCOPE:
15254                   /* In these cases, we should look for template-ids.
15255                      For example, if the default argument is
15256                      `X<int, double>()', we need to do name lookup to
15257                      figure out whether or not `X' is a template; if
15258                      so, the `,' does not end the default argument.
15259
15260                      That is not yet done.  */
15261                   break;
15262
15263                 default:
15264                   break;
15265                 }
15266
15267               /* If we've reached the end, stop.  */
15268               if (done)
15269                 break;
15270
15271               /* Add the token to the token block.  */
15272               token = cp_lexer_consume_token (parser->lexer);
15273             }
15274
15275           /* Create a DEFAULT_ARG to represent the unparsed default
15276              argument.  */
15277           default_argument = make_node (DEFAULT_ARG);
15278           DEFARG_TOKENS (default_argument)
15279             = cp_token_cache_new (first_token, token);
15280           DEFARG_INSTANTIATIONS (default_argument) = NULL;
15281         }
15282       /* Outside of a class definition, we can just parse the
15283          assignment-expression.  */
15284       else
15285         {
15286           token = cp_lexer_peek_token (parser->lexer);
15287           default_argument 
15288             = cp_parser_default_argument (parser, template_parm_p);
15289         }
15290
15291       if (!parser->default_arg_ok_p)
15292         {
15293           if (flag_permissive)
15294             warning (0, "deprecated use of default argument for parameter of non-function");
15295           else
15296             {
15297               error_at (token->location,
15298                         "default arguments are only "
15299                         "permitted for function parameters");
15300               default_argument = NULL_TREE;
15301             }
15302         }
15303       else if ((declarator && declarator->parameter_pack_p)
15304                || (decl_specifiers.type
15305                    && PACK_EXPANSION_P (decl_specifiers.type)))
15306         {
15307           /* Find the name of the parameter pack.  */     
15308           cp_declarator *id_declarator = declarator;
15309           while (id_declarator && id_declarator->kind != cdk_id)
15310             id_declarator = id_declarator->declarator;
15311           
15312           if (id_declarator && id_declarator->kind == cdk_id)
15313             error_at (declarator_token_start->location,
15314                       template_parm_p 
15315                       ? "template parameter pack %qD"
15316                       " cannot have a default argument"
15317                       : "parameter pack %qD cannot have a default argument",
15318                       id_declarator->u.id.unqualified_name);
15319           else
15320             error_at (declarator_token_start->location,
15321                       template_parm_p 
15322                       ? "template parameter pack cannot have a default argument"
15323                       : "parameter pack cannot have a default argument");
15324           
15325           default_argument = NULL_TREE;
15326         }
15327     }
15328   else
15329     default_argument = NULL_TREE;
15330
15331   return make_parameter_declarator (&decl_specifiers,
15332                                     declarator,
15333                                     default_argument);
15334 }
15335
15336 /* Parse a default argument and return it.
15337
15338    TEMPLATE_PARM_P is true if this is a default argument for a
15339    non-type template parameter.  */
15340 static tree
15341 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15342 {
15343   tree default_argument = NULL_TREE;
15344   bool saved_greater_than_is_operator_p;
15345   bool saved_local_variables_forbidden_p;
15346
15347   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15348      set correctly.  */
15349   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15350   parser->greater_than_is_operator_p = !template_parm_p;
15351   /* Local variable names (and the `this' keyword) may not
15352      appear in a default argument.  */
15353   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15354   parser->local_variables_forbidden_p = true;
15355   /* Parse the assignment-expression.  */
15356   if (template_parm_p)
15357     push_deferring_access_checks (dk_no_deferred);
15358   default_argument
15359     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15360   if (template_parm_p)
15361     pop_deferring_access_checks ();
15362   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15363   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15364
15365   return default_argument;
15366 }
15367
15368 /* Parse a function-body.
15369
15370    function-body:
15371      compound_statement  */
15372
15373 static void
15374 cp_parser_function_body (cp_parser *parser)
15375 {
15376   cp_parser_compound_statement (parser, NULL, false);
15377 }
15378
15379 /* Parse a ctor-initializer-opt followed by a function-body.  Return
15380    true if a ctor-initializer was present.  */
15381
15382 static bool
15383 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15384 {
15385   tree body;
15386   bool ctor_initializer_p;
15387
15388   /* Begin the function body.  */
15389   body = begin_function_body ();
15390   /* Parse the optional ctor-initializer.  */
15391   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15392   /* Parse the function-body.  */
15393   cp_parser_function_body (parser);
15394   /* Finish the function body.  */
15395   finish_function_body (body);
15396
15397   return ctor_initializer_p;
15398 }
15399
15400 /* Parse an initializer.
15401
15402    initializer:
15403      = initializer-clause
15404      ( expression-list )
15405
15406    Returns an expression representing the initializer.  If no
15407    initializer is present, NULL_TREE is returned.
15408
15409    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15410    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
15411    set to TRUE if there is no initializer present.  If there is an
15412    initializer, and it is not a constant-expression, *NON_CONSTANT_P
15413    is set to true; otherwise it is set to false.  */
15414
15415 static tree
15416 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15417                        bool* non_constant_p)
15418 {
15419   cp_token *token;
15420   tree init;
15421
15422   /* Peek at the next token.  */
15423   token = cp_lexer_peek_token (parser->lexer);
15424
15425   /* Let our caller know whether or not this initializer was
15426      parenthesized.  */
15427   *is_direct_init = (token->type != CPP_EQ);
15428   /* Assume that the initializer is constant.  */
15429   *non_constant_p = false;
15430
15431   if (token->type == CPP_EQ)
15432     {
15433       /* Consume the `='.  */
15434       cp_lexer_consume_token (parser->lexer);
15435       /* Parse the initializer-clause.  */
15436       init = cp_parser_initializer_clause (parser, non_constant_p);
15437     }
15438   else if (token->type == CPP_OPEN_PAREN)
15439     {
15440       VEC(tree,gc) *vec;
15441       vec = cp_parser_parenthesized_expression_list (parser, false,
15442                                                      /*cast_p=*/false,
15443                                                      /*allow_expansion_p=*/true,
15444                                                      non_constant_p);
15445       if (vec == NULL)
15446         return error_mark_node;
15447       init = build_tree_list_vec (vec);
15448       release_tree_vector (vec);
15449     }
15450   else if (token->type == CPP_OPEN_BRACE)
15451     {
15452       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15453       init = cp_parser_braced_list (parser, non_constant_p);
15454       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15455     }
15456   else
15457     {
15458       /* Anything else is an error.  */
15459       cp_parser_error (parser, "expected initializer");
15460       init = error_mark_node;
15461     }
15462
15463   return init;
15464 }
15465
15466 /* Parse an initializer-clause.
15467
15468    initializer-clause:
15469      assignment-expression
15470      braced-init-list
15471
15472    Returns an expression representing the initializer.
15473
15474    If the `assignment-expression' production is used the value
15475    returned is simply a representation for the expression.
15476
15477    Otherwise, calls cp_parser_braced_list.  */
15478
15479 static tree
15480 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15481 {
15482   tree initializer;
15483
15484   /* Assume the expression is constant.  */
15485   *non_constant_p = false;
15486
15487   /* If it is not a `{', then we are looking at an
15488      assignment-expression.  */
15489   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15490     {
15491       initializer
15492         = cp_parser_constant_expression (parser,
15493                                         /*allow_non_constant_p=*/true,
15494                                         non_constant_p);
15495       if (!*non_constant_p)
15496         initializer = fold_non_dependent_expr (initializer);
15497     }
15498   else
15499     initializer = cp_parser_braced_list (parser, non_constant_p);
15500
15501   return initializer;
15502 }
15503
15504 /* Parse a brace-enclosed initializer list.
15505
15506    braced-init-list:
15507      { initializer-list , [opt] }
15508      { }
15509
15510    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
15511    the elements of the initializer-list (or NULL, if the last
15512    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
15513    NULL_TREE.  There is no way to detect whether or not the optional
15514    trailing `,' was provided.  NON_CONSTANT_P is as for
15515    cp_parser_initializer.  */     
15516
15517 static tree
15518 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15519 {
15520   tree initializer;
15521
15522   /* Consume the `{' token.  */
15523   cp_lexer_consume_token (parser->lexer);
15524   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
15525   initializer = make_node (CONSTRUCTOR);
15526   /* If it's not a `}', then there is a non-trivial initializer.  */
15527   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15528     {
15529       /* Parse the initializer list.  */
15530       CONSTRUCTOR_ELTS (initializer)
15531         = cp_parser_initializer_list (parser, non_constant_p);
15532       /* A trailing `,' token is allowed.  */
15533       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15534         cp_lexer_consume_token (parser->lexer);
15535     }
15536   /* Now, there should be a trailing `}'.  */
15537   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15538   TREE_TYPE (initializer) = init_list_type_node;
15539   return initializer;
15540 }
15541
15542 /* Parse an initializer-list.
15543
15544    initializer-list:
15545      initializer-clause ... [opt]
15546      initializer-list , initializer-clause ... [opt]
15547
15548    GNU Extension:
15549
15550    initializer-list:
15551      identifier : initializer-clause
15552      initializer-list, identifier : initializer-clause
15553
15554    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
15555    for the initializer.  If the INDEX of the elt is non-NULL, it is the
15556    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
15557    as for cp_parser_initializer.  */
15558
15559 static VEC(constructor_elt,gc) *
15560 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15561 {
15562   VEC(constructor_elt,gc) *v = NULL;
15563
15564   /* Assume all of the expressions are constant.  */
15565   *non_constant_p = false;
15566
15567   /* Parse the rest of the list.  */
15568   while (true)
15569     {
15570       cp_token *token;
15571       tree identifier;
15572       tree initializer;
15573       bool clause_non_constant_p;
15574
15575       /* If the next token is an identifier and the following one is a
15576          colon, we are looking at the GNU designated-initializer
15577          syntax.  */
15578       if (cp_parser_allow_gnu_extensions_p (parser)
15579           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15580           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15581         {
15582           /* Warn the user that they are using an extension.  */
15583           pedwarn (input_location, OPT_pedantic, 
15584                    "ISO C++ does not allow designated initializers");
15585           /* Consume the identifier.  */
15586           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15587           /* Consume the `:'.  */
15588           cp_lexer_consume_token (parser->lexer);
15589         }
15590       else
15591         identifier = NULL_TREE;
15592
15593       /* Parse the initializer.  */
15594       initializer = cp_parser_initializer_clause (parser,
15595                                                   &clause_non_constant_p);
15596       /* If any clause is non-constant, so is the entire initializer.  */
15597       if (clause_non_constant_p)
15598         *non_constant_p = true;
15599
15600       /* If we have an ellipsis, this is an initializer pack
15601          expansion.  */
15602       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15603         {
15604           /* Consume the `...'.  */
15605           cp_lexer_consume_token (parser->lexer);
15606
15607           /* Turn the initializer into an initializer expansion.  */
15608           initializer = make_pack_expansion (initializer);
15609         }
15610
15611       /* Add it to the vector.  */
15612       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15613
15614       /* If the next token is not a comma, we have reached the end of
15615          the list.  */
15616       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15617         break;
15618
15619       /* Peek at the next token.  */
15620       token = cp_lexer_peek_nth_token (parser->lexer, 2);
15621       /* If the next token is a `}', then we're still done.  An
15622          initializer-clause can have a trailing `,' after the
15623          initializer-list and before the closing `}'.  */
15624       if (token->type == CPP_CLOSE_BRACE)
15625         break;
15626
15627       /* Consume the `,' token.  */
15628       cp_lexer_consume_token (parser->lexer);
15629     }
15630
15631   return v;
15632 }
15633
15634 /* Classes [gram.class] */
15635
15636 /* Parse a class-name.
15637
15638    class-name:
15639      identifier
15640      template-id
15641
15642    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15643    to indicate that names looked up in dependent types should be
15644    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
15645    keyword has been used to indicate that the name that appears next
15646    is a template.  TAG_TYPE indicates the explicit tag given before
15647    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
15648    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
15649    is the class being defined in a class-head.
15650
15651    Returns the TYPE_DECL representing the class.  */
15652
15653 static tree
15654 cp_parser_class_name (cp_parser *parser,
15655                       bool typename_keyword_p,
15656                       bool template_keyword_p,
15657                       enum tag_types tag_type,
15658                       bool check_dependency_p,
15659                       bool class_head_p,
15660                       bool is_declaration)
15661 {
15662   tree decl;
15663   tree scope;
15664   bool typename_p;
15665   cp_token *token;
15666   tree identifier = NULL_TREE;
15667
15668   /* All class-names start with an identifier.  */
15669   token = cp_lexer_peek_token (parser->lexer);
15670   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15671     {
15672       cp_parser_error (parser, "expected class-name");
15673       return error_mark_node;
15674     }
15675
15676   /* PARSER->SCOPE can be cleared when parsing the template-arguments
15677      to a template-id, so we save it here.  */
15678   scope = parser->scope;
15679   if (scope == error_mark_node)
15680     return error_mark_node;
15681
15682   /* Any name names a type if we're following the `typename' keyword
15683      in a qualified name where the enclosing scope is type-dependent.  */
15684   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15685                 && dependent_type_p (scope));
15686   /* Handle the common case (an identifier, but not a template-id)
15687      efficiently.  */
15688   if (token->type == CPP_NAME
15689       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15690     {
15691       cp_token *identifier_token;
15692       bool ambiguous_p;
15693
15694       /* Look for the identifier.  */
15695       identifier_token = cp_lexer_peek_token (parser->lexer);
15696       ambiguous_p = identifier_token->ambiguous_p;
15697       identifier = cp_parser_identifier (parser);
15698       /* If the next token isn't an identifier, we are certainly not
15699          looking at a class-name.  */
15700       if (identifier == error_mark_node)
15701         decl = error_mark_node;
15702       /* If we know this is a type-name, there's no need to look it
15703          up.  */
15704       else if (typename_p)
15705         decl = identifier;
15706       else
15707         {
15708           tree ambiguous_decls;
15709           /* If we already know that this lookup is ambiguous, then
15710              we've already issued an error message; there's no reason
15711              to check again.  */
15712           if (ambiguous_p)
15713             {
15714               cp_parser_simulate_error (parser);
15715               return error_mark_node;
15716             }
15717           /* If the next token is a `::', then the name must be a type
15718              name.
15719
15720              [basic.lookup.qual]
15721
15722              During the lookup for a name preceding the :: scope
15723              resolution operator, object, function, and enumerator
15724              names are ignored.  */
15725           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15726             tag_type = typename_type;
15727           /* Look up the name.  */
15728           decl = cp_parser_lookup_name (parser, identifier,
15729                                         tag_type,
15730                                         /*is_template=*/false,
15731                                         /*is_namespace=*/false,
15732                                         check_dependency_p,
15733                                         &ambiguous_decls,
15734                                         identifier_token->location);
15735           if (ambiguous_decls)
15736             {
15737               if (cp_parser_parsing_tentatively (parser))
15738                 cp_parser_simulate_error (parser);
15739               return error_mark_node;
15740             }
15741         }
15742     }
15743   else
15744     {
15745       /* Try a template-id.  */
15746       decl = cp_parser_template_id (parser, template_keyword_p,
15747                                     check_dependency_p,
15748                                     is_declaration);
15749       if (decl == error_mark_node)
15750         return error_mark_node;
15751     }
15752
15753   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15754
15755   /* If this is a typename, create a TYPENAME_TYPE.  */
15756   if (typename_p && decl != error_mark_node)
15757     {
15758       decl = make_typename_type (scope, decl, typename_type,
15759                                  /*complain=*/tf_error);
15760       if (decl != error_mark_node)
15761         decl = TYPE_NAME (decl);
15762     }
15763
15764   /* Check to see that it is really the name of a class.  */
15765   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15766       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15767       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15768     /* Situations like this:
15769
15770          template <typename T> struct A {
15771            typename T::template X<int>::I i;
15772          };
15773
15774        are problematic.  Is `T::template X<int>' a class-name?  The
15775        standard does not seem to be definitive, but there is no other
15776        valid interpretation of the following `::'.  Therefore, those
15777        names are considered class-names.  */
15778     {
15779       decl = make_typename_type (scope, decl, tag_type, tf_error);
15780       if (decl != error_mark_node)
15781         decl = TYPE_NAME (decl);
15782     }
15783   else if (TREE_CODE (decl) != TYPE_DECL
15784            || TREE_TYPE (decl) == error_mark_node
15785            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15786     decl = error_mark_node;
15787
15788   if (decl == error_mark_node)
15789     cp_parser_error (parser, "expected class-name");
15790   else if (identifier && !parser->scope)
15791     maybe_note_name_used_in_class (identifier, decl);
15792
15793   return decl;
15794 }
15795
15796 /* Parse a class-specifier.
15797
15798    class-specifier:
15799      class-head { member-specification [opt] }
15800
15801    Returns the TREE_TYPE representing the class.  */
15802
15803 static tree
15804 cp_parser_class_specifier (cp_parser* parser)
15805 {
15806   tree type;
15807   tree attributes = NULL_TREE;
15808   bool nested_name_specifier_p;
15809   unsigned saved_num_template_parameter_lists;
15810   bool saved_in_function_body;
15811   bool saved_in_unbraced_linkage_specification_p;
15812   tree old_scope = NULL_TREE;
15813   tree scope = NULL_TREE;
15814   tree bases;
15815
15816   push_deferring_access_checks (dk_no_deferred);
15817
15818   /* Parse the class-head.  */
15819   type = cp_parser_class_head (parser,
15820                                &nested_name_specifier_p,
15821                                &attributes,
15822                                &bases);
15823   /* If the class-head was a semantic disaster, skip the entire body
15824      of the class.  */
15825   if (!type)
15826     {
15827       cp_parser_skip_to_end_of_block_or_statement (parser);
15828       pop_deferring_access_checks ();
15829       return error_mark_node;
15830     }
15831
15832   /* Look for the `{'.  */
15833   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15834     {
15835       pop_deferring_access_checks ();
15836       return error_mark_node;
15837     }
15838
15839   /* Process the base classes. If they're invalid, skip the 
15840      entire class body.  */
15841   if (!xref_basetypes (type, bases))
15842     {
15843       /* Consuming the closing brace yields better error messages
15844          later on.  */
15845       if (cp_parser_skip_to_closing_brace (parser))
15846         cp_lexer_consume_token (parser->lexer);
15847       pop_deferring_access_checks ();
15848       return error_mark_node;
15849     }
15850
15851   /* Issue an error message if type-definitions are forbidden here.  */
15852   cp_parser_check_type_definition (parser);
15853   /* Remember that we are defining one more class.  */
15854   ++parser->num_classes_being_defined;
15855   /* Inside the class, surrounding template-parameter-lists do not
15856      apply.  */
15857   saved_num_template_parameter_lists
15858     = parser->num_template_parameter_lists;
15859   parser->num_template_parameter_lists = 0;
15860   /* We are not in a function body.  */
15861   saved_in_function_body = parser->in_function_body;
15862   parser->in_function_body = false;
15863   /* We are not immediately inside an extern "lang" block.  */
15864   saved_in_unbraced_linkage_specification_p
15865     = parser->in_unbraced_linkage_specification_p;
15866   parser->in_unbraced_linkage_specification_p = false;
15867
15868   /* Start the class.  */
15869   if (nested_name_specifier_p)
15870     {
15871       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15872       old_scope = push_inner_scope (scope);
15873     }
15874   type = begin_class_definition (type, attributes);
15875
15876   if (type == error_mark_node)
15877     /* If the type is erroneous, skip the entire body of the class.  */
15878     cp_parser_skip_to_closing_brace (parser);
15879   else
15880     /* Parse the member-specification.  */
15881     cp_parser_member_specification_opt (parser);
15882
15883   /* Look for the trailing `}'.  */
15884   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15885   /* Look for trailing attributes to apply to this class.  */
15886   if (cp_parser_allow_gnu_extensions_p (parser))
15887     attributes = cp_parser_attributes_opt (parser);
15888   if (type != error_mark_node)
15889     type = finish_struct (type, attributes);
15890   if (nested_name_specifier_p)
15891     pop_inner_scope (old_scope, scope);
15892   /* If this class is not itself within the scope of another class,
15893      then we need to parse the bodies of all of the queued function
15894      definitions.  Note that the queued functions defined in a class
15895      are not always processed immediately following the
15896      class-specifier for that class.  Consider:
15897
15898        struct A {
15899          struct B { void f() { sizeof (A); } };
15900        };
15901
15902      If `f' were processed before the processing of `A' were
15903      completed, there would be no way to compute the size of `A'.
15904      Note that the nesting we are interested in here is lexical --
15905      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15906      for:
15907
15908        struct A { struct B; };
15909        struct A::B { void f() { } };
15910
15911      there is no need to delay the parsing of `A::B::f'.  */
15912   if (--parser->num_classes_being_defined == 0)
15913     {
15914       tree queue_entry;
15915       tree fn;
15916       tree class_type = NULL_TREE;
15917       tree pushed_scope = NULL_TREE;
15918
15919       /* In a first pass, parse default arguments to the functions.
15920          Then, in a second pass, parse the bodies of the functions.
15921          This two-phased approach handles cases like:
15922
15923             struct S {
15924               void f() { g(); }
15925               void g(int i = 3);
15926             };
15927
15928          */
15929       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15930              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15931            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15932            TREE_PURPOSE (parser->unparsed_functions_queues)
15933              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15934         {
15935           fn = TREE_VALUE (queue_entry);
15936           /* If there are default arguments that have not yet been processed,
15937              take care of them now.  */
15938           if (class_type != TREE_PURPOSE (queue_entry))
15939             {
15940               if (pushed_scope)
15941                 pop_scope (pushed_scope);
15942               class_type = TREE_PURPOSE (queue_entry);
15943               pushed_scope = push_scope (class_type);
15944             }
15945           /* Make sure that any template parameters are in scope.  */
15946           maybe_begin_member_template_processing (fn);
15947           /* Parse the default argument expressions.  */
15948           cp_parser_late_parsing_default_args (parser, fn);
15949           /* Remove any template parameters from the symbol table.  */
15950           maybe_end_member_template_processing ();
15951         }
15952       if (pushed_scope)
15953         pop_scope (pushed_scope);
15954       /* Now parse the body of the functions.  */
15955       for (TREE_VALUE (parser->unparsed_functions_queues)
15956              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15957            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15958            TREE_VALUE (parser->unparsed_functions_queues)
15959              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15960         {
15961           /* Figure out which function we need to process.  */
15962           fn = TREE_VALUE (queue_entry);
15963           /* Parse the function.  */
15964           cp_parser_late_parsing_for_member (parser, fn);
15965         }
15966     }
15967
15968   /* Put back any saved access checks.  */
15969   pop_deferring_access_checks ();
15970
15971   /* Restore saved state.  */
15972   parser->in_function_body = saved_in_function_body;
15973   parser->num_template_parameter_lists
15974     = saved_num_template_parameter_lists;
15975   parser->in_unbraced_linkage_specification_p
15976     = saved_in_unbraced_linkage_specification_p;
15977
15978   return type;
15979 }
15980
15981 /* Parse a class-head.
15982
15983    class-head:
15984      class-key identifier [opt] base-clause [opt]
15985      class-key nested-name-specifier identifier base-clause [opt]
15986      class-key nested-name-specifier [opt] template-id
15987        base-clause [opt]
15988
15989    GNU Extensions:
15990      class-key attributes identifier [opt] base-clause [opt]
15991      class-key attributes nested-name-specifier identifier base-clause [opt]
15992      class-key attributes nested-name-specifier [opt] template-id
15993        base-clause [opt]
15994
15995    Upon return BASES is initialized to the list of base classes (or
15996    NULL, if there are none) in the same form returned by
15997    cp_parser_base_clause.
15998
15999    Returns the TYPE of the indicated class.  Sets
16000    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16001    involving a nested-name-specifier was used, and FALSE otherwise.
16002
16003    Returns error_mark_node if this is not a class-head.
16004
16005    Returns NULL_TREE if the class-head is syntactically valid, but
16006    semantically invalid in a way that means we should skip the entire
16007    body of the class.  */
16008
16009 static tree
16010 cp_parser_class_head (cp_parser* parser,
16011                       bool* nested_name_specifier_p,
16012                       tree *attributes_p,
16013                       tree *bases)
16014 {
16015   tree nested_name_specifier;
16016   enum tag_types class_key;
16017   tree id = NULL_TREE;
16018   tree type = NULL_TREE;
16019   tree attributes;
16020   bool template_id_p = false;
16021   bool qualified_p = false;
16022   bool invalid_nested_name_p = false;
16023   bool invalid_explicit_specialization_p = false;
16024   tree pushed_scope = NULL_TREE;
16025   unsigned num_templates;
16026   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16027   /* Assume no nested-name-specifier will be present.  */
16028   *nested_name_specifier_p = false;
16029   /* Assume no template parameter lists will be used in defining the
16030      type.  */
16031   num_templates = 0;
16032
16033   *bases = NULL_TREE;
16034
16035   /* Look for the class-key.  */
16036   class_key = cp_parser_class_key (parser);
16037   if (class_key == none_type)
16038     return error_mark_node;
16039
16040   /* Parse the attributes.  */
16041   attributes = cp_parser_attributes_opt (parser);
16042
16043   /* If the next token is `::', that is invalid -- but sometimes
16044      people do try to write:
16045
16046        struct ::S {};
16047
16048      Handle this gracefully by accepting the extra qualifier, and then
16049      issuing an error about it later if this really is a
16050      class-head.  If it turns out just to be an elaborated type
16051      specifier, remain silent.  */
16052   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16053     qualified_p = true;
16054
16055   push_deferring_access_checks (dk_no_check);
16056
16057   /* Determine the name of the class.  Begin by looking for an
16058      optional nested-name-specifier.  */
16059   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16060   nested_name_specifier
16061     = cp_parser_nested_name_specifier_opt (parser,
16062                                            /*typename_keyword_p=*/false,
16063                                            /*check_dependency_p=*/false,
16064                                            /*type_p=*/false,
16065                                            /*is_declaration=*/false);
16066   /* If there was a nested-name-specifier, then there *must* be an
16067      identifier.  */
16068   if (nested_name_specifier)
16069     {
16070       type_start_token = cp_lexer_peek_token (parser->lexer);
16071       /* Although the grammar says `identifier', it really means
16072          `class-name' or `template-name'.  You are only allowed to
16073          define a class that has already been declared with this
16074          syntax.
16075
16076          The proposed resolution for Core Issue 180 says that wherever
16077          you see `class T::X' you should treat `X' as a type-name.
16078
16079          It is OK to define an inaccessible class; for example:
16080
16081            class A { class B; };
16082            class A::B {};
16083
16084          We do not know if we will see a class-name, or a
16085          template-name.  We look for a class-name first, in case the
16086          class-name is a template-id; if we looked for the
16087          template-name first we would stop after the template-name.  */
16088       cp_parser_parse_tentatively (parser);
16089       type = cp_parser_class_name (parser,
16090                                    /*typename_keyword_p=*/false,
16091                                    /*template_keyword_p=*/false,
16092                                    class_type,
16093                                    /*check_dependency_p=*/false,
16094                                    /*class_head_p=*/true,
16095                                    /*is_declaration=*/false);
16096       /* If that didn't work, ignore the nested-name-specifier.  */
16097       if (!cp_parser_parse_definitely (parser))
16098         {
16099           invalid_nested_name_p = true;
16100           type_start_token = cp_lexer_peek_token (parser->lexer);
16101           id = cp_parser_identifier (parser);
16102           if (id == error_mark_node)
16103             id = NULL_TREE;
16104         }
16105       /* If we could not find a corresponding TYPE, treat this
16106          declaration like an unqualified declaration.  */
16107       if (type == error_mark_node)
16108         nested_name_specifier = NULL_TREE;
16109       /* Otherwise, count the number of templates used in TYPE and its
16110          containing scopes.  */
16111       else
16112         {
16113           tree scope;
16114
16115           for (scope = TREE_TYPE (type);
16116                scope && TREE_CODE (scope) != NAMESPACE_DECL;
16117                scope = (TYPE_P (scope)
16118                         ? TYPE_CONTEXT (scope)
16119                         : DECL_CONTEXT (scope)))
16120             if (TYPE_P (scope)
16121                 && CLASS_TYPE_P (scope)
16122                 && CLASSTYPE_TEMPLATE_INFO (scope)
16123                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16124                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16125               ++num_templates;
16126         }
16127     }
16128   /* Otherwise, the identifier is optional.  */
16129   else
16130     {
16131       /* We don't know whether what comes next is a template-id,
16132          an identifier, or nothing at all.  */
16133       cp_parser_parse_tentatively (parser);
16134       /* Check for a template-id.  */
16135       type_start_token = cp_lexer_peek_token (parser->lexer);
16136       id = cp_parser_template_id (parser,
16137                                   /*template_keyword_p=*/false,
16138                                   /*check_dependency_p=*/true,
16139                                   /*is_declaration=*/true);
16140       /* If that didn't work, it could still be an identifier.  */
16141       if (!cp_parser_parse_definitely (parser))
16142         {
16143           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16144             {
16145               type_start_token = cp_lexer_peek_token (parser->lexer);
16146               id = cp_parser_identifier (parser);
16147             }
16148           else
16149             id = NULL_TREE;
16150         }
16151       else
16152         {
16153           template_id_p = true;
16154           ++num_templates;
16155         }
16156     }
16157
16158   pop_deferring_access_checks ();
16159
16160   if (id)
16161     cp_parser_check_for_invalid_template_id (parser, id,
16162                                              type_start_token->location);
16163
16164   /* If it's not a `:' or a `{' then we can't really be looking at a
16165      class-head, since a class-head only appears as part of a
16166      class-specifier.  We have to detect this situation before calling
16167      xref_tag, since that has irreversible side-effects.  */
16168   if (!cp_parser_next_token_starts_class_definition_p (parser))
16169     {
16170       cp_parser_error (parser, "expected %<{%> or %<:%>");
16171       return error_mark_node;
16172     }
16173
16174   /* At this point, we're going ahead with the class-specifier, even
16175      if some other problem occurs.  */
16176   cp_parser_commit_to_tentative_parse (parser);
16177   /* Issue the error about the overly-qualified name now.  */
16178   if (qualified_p)
16179     {
16180       cp_parser_error (parser,
16181                        "global qualification of class name is invalid");
16182       return error_mark_node;
16183     }
16184   else if (invalid_nested_name_p)
16185     {
16186       cp_parser_error (parser,
16187                        "qualified name does not name a class");
16188       return error_mark_node;
16189     }
16190   else if (nested_name_specifier)
16191     {
16192       tree scope;
16193
16194       /* Reject typedef-names in class heads.  */
16195       if (!DECL_IMPLICIT_TYPEDEF_P (type))
16196         {
16197           error_at (type_start_token->location,
16198                     "invalid class name in declaration of %qD",
16199                     type);
16200           type = NULL_TREE;
16201           goto done;
16202         }
16203
16204       /* Figure out in what scope the declaration is being placed.  */
16205       scope = current_scope ();
16206       /* If that scope does not contain the scope in which the
16207          class was originally declared, the program is invalid.  */
16208       if (scope && !is_ancestor (scope, nested_name_specifier))
16209         {
16210           if (at_namespace_scope_p ())
16211             error_at (type_start_token->location,
16212                       "declaration of %qD in namespace %qD which does not "
16213                       "enclose %qD",
16214                       type, scope, nested_name_specifier);
16215           else
16216             error_at (type_start_token->location,
16217                       "declaration of %qD in %qD which does not enclose %qD",
16218                       type, scope, nested_name_specifier);
16219           type = NULL_TREE;
16220           goto done;
16221         }
16222       /* [dcl.meaning]
16223
16224          A declarator-id shall not be qualified except for the
16225          definition of a ... nested class outside of its class
16226          ... [or] the definition or explicit instantiation of a
16227          class member of a namespace outside of its namespace.  */
16228       if (scope == nested_name_specifier)
16229         {
16230           permerror (nested_name_specifier_token_start->location,
16231                      "extra qualification not allowed");
16232           nested_name_specifier = NULL_TREE;
16233           num_templates = 0;
16234         }
16235     }
16236   /* An explicit-specialization must be preceded by "template <>".  If
16237      it is not, try to recover gracefully.  */
16238   if (at_namespace_scope_p ()
16239       && parser->num_template_parameter_lists == 0
16240       && template_id_p)
16241     {
16242       error_at (type_start_token->location,
16243                 "an explicit specialization must be preceded by %<template <>%>");
16244       invalid_explicit_specialization_p = true;
16245       /* Take the same action that would have been taken by
16246          cp_parser_explicit_specialization.  */
16247       ++parser->num_template_parameter_lists;
16248       begin_specialization ();
16249     }
16250   /* There must be no "return" statements between this point and the
16251      end of this function; set "type "to the correct return value and
16252      use "goto done;" to return.  */
16253   /* Make sure that the right number of template parameters were
16254      present.  */
16255   if (!cp_parser_check_template_parameters (parser, num_templates,
16256                                             type_start_token->location,
16257                                             /*declarator=*/NULL))
16258     {
16259       /* If something went wrong, there is no point in even trying to
16260          process the class-definition.  */
16261       type = NULL_TREE;
16262       goto done;
16263     }
16264
16265   /* Look up the type.  */
16266   if (template_id_p)
16267     {
16268       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16269           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16270               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16271         {
16272           error_at (type_start_token->location,
16273                     "function template %qD redeclared as a class template", id);
16274           type = error_mark_node;
16275         }
16276       else
16277         {
16278           type = TREE_TYPE (id);
16279           type = maybe_process_partial_specialization (type);
16280         }
16281       if (nested_name_specifier)
16282         pushed_scope = push_scope (nested_name_specifier);
16283     }
16284   else if (nested_name_specifier)
16285     {
16286       tree class_type;
16287
16288       /* Given:
16289
16290             template <typename T> struct S { struct T };
16291             template <typename T> struct S<T>::T { };
16292
16293          we will get a TYPENAME_TYPE when processing the definition of
16294          `S::T'.  We need to resolve it to the actual type before we
16295          try to define it.  */
16296       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16297         {
16298           class_type = resolve_typename_type (TREE_TYPE (type),
16299                                               /*only_current_p=*/false);
16300           if (TREE_CODE (class_type) != TYPENAME_TYPE)
16301             type = TYPE_NAME (class_type);
16302           else
16303             {
16304               cp_parser_error (parser, "could not resolve typename type");
16305               type = error_mark_node;
16306             }
16307         }
16308
16309       if (maybe_process_partial_specialization (TREE_TYPE (type))
16310           == error_mark_node)
16311         {
16312           type = NULL_TREE;
16313           goto done;
16314         }
16315
16316       class_type = current_class_type;
16317       /* Enter the scope indicated by the nested-name-specifier.  */
16318       pushed_scope = push_scope (nested_name_specifier);
16319       /* Get the canonical version of this type.  */
16320       type = TYPE_MAIN_DECL (TREE_TYPE (type));
16321       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16322           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16323         {
16324           type = push_template_decl (type);
16325           if (type == error_mark_node)
16326             {
16327               type = NULL_TREE;
16328               goto done;
16329             }
16330         }
16331
16332       type = TREE_TYPE (type);
16333       *nested_name_specifier_p = true;
16334     }
16335   else      /* The name is not a nested name.  */
16336     {
16337       /* If the class was unnamed, create a dummy name.  */
16338       if (!id)
16339         id = make_anon_name ();
16340       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16341                        parser->num_template_parameter_lists);
16342     }
16343
16344   /* Indicate whether this class was declared as a `class' or as a
16345      `struct'.  */
16346   if (TREE_CODE (type) == RECORD_TYPE)
16347     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16348   cp_parser_check_class_key (class_key, type);
16349
16350   /* If this type was already complete, and we see another definition,
16351      that's an error.  */
16352   if (type != error_mark_node && COMPLETE_TYPE_P (type))
16353     {
16354       error_at (type_start_token->location, "redefinition of %q#T",
16355                 type);
16356       error_at (type_start_token->location, "previous definition of %q+#T",
16357                 type);
16358       type = NULL_TREE;
16359       goto done;
16360     }
16361   else if (type == error_mark_node)
16362     type = NULL_TREE;
16363
16364   /* We will have entered the scope containing the class; the names of
16365      base classes should be looked up in that context.  For example:
16366
16367        struct A { struct B {}; struct C; };
16368        struct A::C : B {};
16369
16370      is valid.  */
16371
16372   /* Get the list of base-classes, if there is one.  */
16373   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16374     *bases = cp_parser_base_clause (parser);
16375
16376  done:
16377   /* Leave the scope given by the nested-name-specifier.  We will
16378      enter the class scope itself while processing the members.  */
16379   if (pushed_scope)
16380     pop_scope (pushed_scope);
16381
16382   if (invalid_explicit_specialization_p)
16383     {
16384       end_specialization ();
16385       --parser->num_template_parameter_lists;
16386     }
16387   *attributes_p = attributes;
16388   return type;
16389 }
16390
16391 /* Parse a class-key.
16392
16393    class-key:
16394      class
16395      struct
16396      union
16397
16398    Returns the kind of class-key specified, or none_type to indicate
16399    error.  */
16400
16401 static enum tag_types
16402 cp_parser_class_key (cp_parser* parser)
16403 {
16404   cp_token *token;
16405   enum tag_types tag_type;
16406
16407   /* Look for the class-key.  */
16408   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16409   if (!token)
16410     return none_type;
16411
16412   /* Check to see if the TOKEN is a class-key.  */
16413   tag_type = cp_parser_token_is_class_key (token);
16414   if (!tag_type)
16415     cp_parser_error (parser, "expected class-key");
16416   return tag_type;
16417 }
16418
16419 /* Parse an (optional) member-specification.
16420
16421    member-specification:
16422      member-declaration member-specification [opt]
16423      access-specifier : member-specification [opt]  */
16424
16425 static void
16426 cp_parser_member_specification_opt (cp_parser* parser)
16427 {
16428   while (true)
16429     {
16430       cp_token *token;
16431       enum rid keyword;
16432
16433       /* Peek at the next token.  */
16434       token = cp_lexer_peek_token (parser->lexer);
16435       /* If it's a `}', or EOF then we've seen all the members.  */
16436       if (token->type == CPP_CLOSE_BRACE
16437           || token->type == CPP_EOF
16438           || token->type == CPP_PRAGMA_EOL)
16439         break;
16440
16441       /* See if this token is a keyword.  */
16442       keyword = token->keyword;
16443       switch (keyword)
16444         {
16445         case RID_PUBLIC:
16446         case RID_PROTECTED:
16447         case RID_PRIVATE:
16448           /* Consume the access-specifier.  */
16449           cp_lexer_consume_token (parser->lexer);
16450           /* Remember which access-specifier is active.  */
16451           current_access_specifier = token->u.value;
16452           /* Look for the `:'.  */
16453           cp_parser_require (parser, CPP_COLON, "%<:%>");
16454           break;
16455
16456         default:
16457           /* Accept #pragmas at class scope.  */
16458           if (token->type == CPP_PRAGMA)
16459             {
16460               cp_parser_pragma (parser, pragma_external);
16461               break;
16462             }
16463
16464           /* Otherwise, the next construction must be a
16465              member-declaration.  */
16466           cp_parser_member_declaration (parser);
16467         }
16468     }
16469 }
16470
16471 /* Parse a member-declaration.
16472
16473    member-declaration:
16474      decl-specifier-seq [opt] member-declarator-list [opt] ;
16475      function-definition ; [opt]
16476      :: [opt] nested-name-specifier template [opt] unqualified-id ;
16477      using-declaration
16478      template-declaration
16479
16480    member-declarator-list:
16481      member-declarator
16482      member-declarator-list , member-declarator
16483
16484    member-declarator:
16485      declarator pure-specifier [opt]
16486      declarator constant-initializer [opt]
16487      identifier [opt] : constant-expression
16488
16489    GNU Extensions:
16490
16491    member-declaration:
16492      __extension__ member-declaration
16493
16494    member-declarator:
16495      declarator attributes [opt] pure-specifier [opt]
16496      declarator attributes [opt] constant-initializer [opt]
16497      identifier [opt] attributes [opt] : constant-expression  
16498
16499    C++0x Extensions:
16500
16501    member-declaration:
16502      static_assert-declaration  */
16503
16504 static void
16505 cp_parser_member_declaration (cp_parser* parser)
16506 {
16507   cp_decl_specifier_seq decl_specifiers;
16508   tree prefix_attributes;
16509   tree decl;
16510   int declares_class_or_enum;
16511   bool friend_p;
16512   cp_token *token = NULL;
16513   cp_token *decl_spec_token_start = NULL;
16514   cp_token *initializer_token_start = NULL;
16515   int saved_pedantic;
16516
16517   /* Check for the `__extension__' keyword.  */
16518   if (cp_parser_extension_opt (parser, &saved_pedantic))
16519     {
16520       /* Recurse.  */
16521       cp_parser_member_declaration (parser);
16522       /* Restore the old value of the PEDANTIC flag.  */
16523       pedantic = saved_pedantic;
16524
16525       return;
16526     }
16527
16528   /* Check for a template-declaration.  */
16529   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16530     {
16531       /* An explicit specialization here is an error condition, and we
16532          expect the specialization handler to detect and report this.  */
16533       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16534           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16535         cp_parser_explicit_specialization (parser);
16536       else
16537         cp_parser_template_declaration (parser, /*member_p=*/true);
16538
16539       return;
16540     }
16541
16542   /* Check for a using-declaration.  */
16543   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16544     {
16545       /* Parse the using-declaration.  */
16546       cp_parser_using_declaration (parser,
16547                                    /*access_declaration_p=*/false);
16548       return;
16549     }
16550
16551   /* Check for @defs.  */
16552   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16553     {
16554       tree ivar, member;
16555       tree ivar_chains = cp_parser_objc_defs_expression (parser);
16556       ivar = ivar_chains;
16557       while (ivar)
16558         {
16559           member = ivar;
16560           ivar = TREE_CHAIN (member);
16561           TREE_CHAIN (member) = NULL_TREE;
16562           finish_member_declaration (member);
16563         }
16564       return;
16565     }
16566
16567   /* If the next token is `static_assert' we have a static assertion.  */
16568   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16569     {
16570       cp_parser_static_assert (parser, /*member_p=*/true);
16571       return;
16572     }
16573
16574   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16575     return;
16576
16577   /* Parse the decl-specifier-seq.  */
16578   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16579   cp_parser_decl_specifier_seq (parser,
16580                                 CP_PARSER_FLAGS_OPTIONAL,
16581                                 &decl_specifiers,
16582                                 &declares_class_or_enum);
16583   prefix_attributes = decl_specifiers.attributes;
16584   decl_specifiers.attributes = NULL_TREE;
16585   /* Check for an invalid type-name.  */
16586   if (!decl_specifiers.any_type_specifiers_p
16587       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16588     return;
16589   /* If there is no declarator, then the decl-specifier-seq should
16590      specify a type.  */
16591   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16592     {
16593       /* If there was no decl-specifier-seq, and the next token is a
16594          `;', then we have something like:
16595
16596            struct S { ; };
16597
16598          [class.mem]
16599
16600          Each member-declaration shall declare at least one member
16601          name of the class.  */
16602       if (!decl_specifiers.any_specifiers_p)
16603         {
16604           cp_token *token = cp_lexer_peek_token (parser->lexer);
16605           if (!in_system_header_at (token->location))
16606             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16607         }
16608       else
16609         {
16610           tree type;
16611
16612           /* See if this declaration is a friend.  */
16613           friend_p = cp_parser_friend_p (&decl_specifiers);
16614           /* If there were decl-specifiers, check to see if there was
16615              a class-declaration.  */
16616           type = check_tag_decl (&decl_specifiers);
16617           /* Nested classes have already been added to the class, but
16618              a `friend' needs to be explicitly registered.  */
16619           if (friend_p)
16620             {
16621               /* If the `friend' keyword was present, the friend must
16622                  be introduced with a class-key.  */
16623                if (!declares_class_or_enum)
16624                  error_at (decl_spec_token_start->location,
16625                            "a class-key must be used when declaring a friend");
16626                /* In this case:
16627
16628                     template <typename T> struct A {
16629                       friend struct A<T>::B;
16630                     };
16631
16632                   A<T>::B will be represented by a TYPENAME_TYPE, and
16633                   therefore not recognized by check_tag_decl.  */
16634                if (!type
16635                    && decl_specifiers.type
16636                    && TYPE_P (decl_specifiers.type))
16637                  type = decl_specifiers.type;
16638                if (!type || !TYPE_P (type))
16639                  error_at (decl_spec_token_start->location,
16640                            "friend declaration does not name a class or "
16641                            "function");
16642                else
16643                  make_friend_class (current_class_type, type,
16644                                     /*complain=*/true);
16645             }
16646           /* If there is no TYPE, an error message will already have
16647              been issued.  */
16648           else if (!type || type == error_mark_node)
16649             ;
16650           /* An anonymous aggregate has to be handled specially; such
16651              a declaration really declares a data member (with a
16652              particular type), as opposed to a nested class.  */
16653           else if (ANON_AGGR_TYPE_P (type))
16654             {
16655               /* Remove constructors and such from TYPE, now that we
16656                  know it is an anonymous aggregate.  */
16657               fixup_anonymous_aggr (type);
16658               /* And make the corresponding data member.  */
16659               decl = build_decl (decl_spec_token_start->location,
16660                                  FIELD_DECL, NULL_TREE, type);
16661               /* Add it to the class.  */
16662               finish_member_declaration (decl);
16663             }
16664           else
16665             cp_parser_check_access_in_redeclaration
16666                                               (TYPE_NAME (type),
16667                                                decl_spec_token_start->location);
16668         }
16669     }
16670   else
16671     {
16672       /* See if these declarations will be friends.  */
16673       friend_p = cp_parser_friend_p (&decl_specifiers);
16674
16675       /* Keep going until we hit the `;' at the end of the
16676          declaration.  */
16677       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16678         {
16679           tree attributes = NULL_TREE;
16680           tree first_attribute;
16681
16682           /* Peek at the next token.  */
16683           token = cp_lexer_peek_token (parser->lexer);
16684
16685           /* Check for a bitfield declaration.  */
16686           if (token->type == CPP_COLON
16687               || (token->type == CPP_NAME
16688                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16689                   == CPP_COLON))
16690             {
16691               tree identifier;
16692               tree width;
16693
16694               /* Get the name of the bitfield.  Note that we cannot just
16695                  check TOKEN here because it may have been invalidated by
16696                  the call to cp_lexer_peek_nth_token above.  */
16697               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16698                 identifier = cp_parser_identifier (parser);
16699               else
16700                 identifier = NULL_TREE;
16701
16702               /* Consume the `:' token.  */
16703               cp_lexer_consume_token (parser->lexer);
16704               /* Get the width of the bitfield.  */
16705               width
16706                 = cp_parser_constant_expression (parser,
16707                                                  /*allow_non_constant=*/false,
16708                                                  NULL);
16709
16710               /* Look for attributes that apply to the bitfield.  */
16711               attributes = cp_parser_attributes_opt (parser);
16712               /* Remember which attributes are prefix attributes and
16713                  which are not.  */
16714               first_attribute = attributes;
16715               /* Combine the attributes.  */
16716               attributes = chainon (prefix_attributes, attributes);
16717
16718               /* Create the bitfield declaration.  */
16719               decl = grokbitfield (identifier
16720                                    ? make_id_declarator (NULL_TREE,
16721                                                          identifier,
16722                                                          sfk_none)
16723                                    : NULL,
16724                                    &decl_specifiers,
16725                                    width,
16726                                    attributes);
16727             }
16728           else
16729             {
16730               cp_declarator *declarator;
16731               tree initializer;
16732               tree asm_specification;
16733               int ctor_dtor_or_conv_p;
16734
16735               /* Parse the declarator.  */
16736               declarator
16737                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16738                                         &ctor_dtor_or_conv_p,
16739                                         /*parenthesized_p=*/NULL,
16740                                         /*member_p=*/true);
16741
16742               /* If something went wrong parsing the declarator, make sure
16743                  that we at least consume some tokens.  */
16744               if (declarator == cp_error_declarator)
16745                 {
16746                   /* Skip to the end of the statement.  */
16747                   cp_parser_skip_to_end_of_statement (parser);
16748                   /* If the next token is not a semicolon, that is
16749                      probably because we just skipped over the body of
16750                      a function.  So, we consume a semicolon if
16751                      present, but do not issue an error message if it
16752                      is not present.  */
16753                   if (cp_lexer_next_token_is (parser->lexer,
16754                                               CPP_SEMICOLON))
16755                     cp_lexer_consume_token (parser->lexer);
16756                   return;
16757                 }
16758
16759               if (declares_class_or_enum & 2)
16760                 cp_parser_check_for_definition_in_return_type
16761                                             (declarator, decl_specifiers.type,
16762                                              decl_specifiers.type_location);
16763
16764               /* Look for an asm-specification.  */
16765               asm_specification = cp_parser_asm_specification_opt (parser);
16766               /* Look for attributes that apply to the declaration.  */
16767               attributes = cp_parser_attributes_opt (parser);
16768               /* Remember which attributes are prefix attributes and
16769                  which are not.  */
16770               first_attribute = attributes;
16771               /* Combine the attributes.  */
16772               attributes = chainon (prefix_attributes, attributes);
16773
16774               /* If it's an `=', then we have a constant-initializer or a
16775                  pure-specifier.  It is not correct to parse the
16776                  initializer before registering the member declaration
16777                  since the member declaration should be in scope while
16778                  its initializer is processed.  However, the rest of the
16779                  front end does not yet provide an interface that allows
16780                  us to handle this correctly.  */
16781               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16782                 {
16783                   /* In [class.mem]:
16784
16785                      A pure-specifier shall be used only in the declaration of
16786                      a virtual function.
16787
16788                      A member-declarator can contain a constant-initializer
16789                      only if it declares a static member of integral or
16790                      enumeration type.
16791
16792                      Therefore, if the DECLARATOR is for a function, we look
16793                      for a pure-specifier; otherwise, we look for a
16794                      constant-initializer.  When we call `grokfield', it will
16795                      perform more stringent semantics checks.  */
16796                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
16797                   if (function_declarator_p (declarator))
16798                     initializer = cp_parser_pure_specifier (parser);
16799                   else
16800                     /* Parse the initializer.  */
16801                     initializer = cp_parser_constant_initializer (parser);
16802                 }
16803               /* Otherwise, there is no initializer.  */
16804               else
16805                 initializer = NULL_TREE;
16806
16807               /* See if we are probably looking at a function
16808                  definition.  We are certainly not looking at a
16809                  member-declarator.  Calling `grokfield' has
16810                  side-effects, so we must not do it unless we are sure
16811                  that we are looking at a member-declarator.  */
16812               if (cp_parser_token_starts_function_definition_p
16813                   (cp_lexer_peek_token (parser->lexer)))
16814                 {
16815                   /* The grammar does not allow a pure-specifier to be
16816                      used when a member function is defined.  (It is
16817                      possible that this fact is an oversight in the
16818                      standard, since a pure function may be defined
16819                      outside of the class-specifier.  */
16820                   if (initializer)
16821                     error_at (initializer_token_start->location,
16822                               "pure-specifier on function-definition");
16823                   decl = cp_parser_save_member_function_body (parser,
16824                                                               &decl_specifiers,
16825                                                               declarator,
16826                                                               attributes);
16827                   /* If the member was not a friend, declare it here.  */
16828                   if (!friend_p)
16829                     finish_member_declaration (decl);
16830                   /* Peek at the next token.  */
16831                   token = cp_lexer_peek_token (parser->lexer);
16832                   /* If the next token is a semicolon, consume it.  */
16833                   if (token->type == CPP_SEMICOLON)
16834                     cp_lexer_consume_token (parser->lexer);
16835                   return;
16836                 }
16837               else
16838                 if (declarator->kind == cdk_function)
16839                   declarator->id_loc = token->location;
16840                 /* Create the declaration.  */
16841                 decl = grokfield (declarator, &decl_specifiers,
16842                                   initializer, /*init_const_expr_p=*/true,
16843                                   asm_specification,
16844                                   attributes);
16845             }
16846
16847           /* Reset PREFIX_ATTRIBUTES.  */
16848           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16849             attributes = TREE_CHAIN (attributes);
16850           if (attributes)
16851             TREE_CHAIN (attributes) = NULL_TREE;
16852
16853           /* If there is any qualification still in effect, clear it
16854              now; we will be starting fresh with the next declarator.  */
16855           parser->scope = NULL_TREE;
16856           parser->qualifying_scope = NULL_TREE;
16857           parser->object_scope = NULL_TREE;
16858           /* If it's a `,', then there are more declarators.  */
16859           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16860             cp_lexer_consume_token (parser->lexer);
16861           /* If the next token isn't a `;', then we have a parse error.  */
16862           else if (cp_lexer_next_token_is_not (parser->lexer,
16863                                                CPP_SEMICOLON))
16864             {
16865               cp_parser_error (parser, "expected %<;%>");
16866               /* Skip tokens until we find a `;'.  */
16867               cp_parser_skip_to_end_of_statement (parser);
16868
16869               break;
16870             }
16871
16872           if (decl)
16873             {
16874               /* Add DECL to the list of members.  */
16875               if (!friend_p)
16876                 finish_member_declaration (decl);
16877
16878               if (TREE_CODE (decl) == FUNCTION_DECL)
16879                 cp_parser_save_default_args (parser, decl);
16880             }
16881         }
16882     }
16883
16884   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16885 }
16886
16887 /* Parse a pure-specifier.
16888
16889    pure-specifier:
16890      = 0
16891
16892    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16893    Otherwise, ERROR_MARK_NODE is returned.  */
16894
16895 static tree
16896 cp_parser_pure_specifier (cp_parser* parser)
16897 {
16898   cp_token *token;
16899
16900   /* Look for the `=' token.  */
16901   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16902     return error_mark_node;
16903   /* Look for the `0' token.  */
16904   token = cp_lexer_peek_token (parser->lexer);
16905
16906   if (token->type == CPP_EOF
16907       || token->type == CPP_PRAGMA_EOL)
16908     return error_mark_node;
16909
16910   cp_lexer_consume_token (parser->lexer);
16911
16912   /* Accept = default or = delete in c++0x mode.  */
16913   if (token->keyword == RID_DEFAULT
16914       || token->keyword == RID_DELETE)
16915     {
16916       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
16917       return token->u.value;
16918     }
16919
16920   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16921   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16922     {
16923       cp_parser_error (parser,
16924                        "invalid pure specifier (only %<= 0%> is allowed)");
16925       cp_parser_skip_to_end_of_statement (parser);
16926       return error_mark_node;
16927     }
16928   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16929     {
16930       error_at (token->location, "templates may not be %<virtual%>");
16931       return error_mark_node;
16932     }
16933
16934   return integer_zero_node;
16935 }
16936
16937 /* Parse a constant-initializer.
16938
16939    constant-initializer:
16940      = constant-expression
16941
16942    Returns a representation of the constant-expression.  */
16943
16944 static tree
16945 cp_parser_constant_initializer (cp_parser* parser)
16946 {
16947   /* Look for the `=' token.  */
16948   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16949     return error_mark_node;
16950
16951   /* It is invalid to write:
16952
16953        struct S { static const int i = { 7 }; };
16954
16955      */
16956   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16957     {
16958       cp_parser_error (parser,
16959                        "a brace-enclosed initializer is not allowed here");
16960       /* Consume the opening brace.  */
16961       cp_lexer_consume_token (parser->lexer);
16962       /* Skip the initializer.  */
16963       cp_parser_skip_to_closing_brace (parser);
16964       /* Look for the trailing `}'.  */
16965       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16966
16967       return error_mark_node;
16968     }
16969
16970   return cp_parser_constant_expression (parser,
16971                                         /*allow_non_constant=*/false,
16972                                         NULL);
16973 }
16974
16975 /* Derived classes [gram.class.derived] */
16976
16977 /* Parse a base-clause.
16978
16979    base-clause:
16980      : base-specifier-list
16981
16982    base-specifier-list:
16983      base-specifier ... [opt]
16984      base-specifier-list , base-specifier ... [opt]
16985
16986    Returns a TREE_LIST representing the base-classes, in the order in
16987    which they were declared.  The representation of each node is as
16988    described by cp_parser_base_specifier.
16989
16990    In the case that no bases are specified, this function will return
16991    NULL_TREE, not ERROR_MARK_NODE.  */
16992
16993 static tree
16994 cp_parser_base_clause (cp_parser* parser)
16995 {
16996   tree bases = NULL_TREE;
16997
16998   /* Look for the `:' that begins the list.  */
16999   cp_parser_require (parser, CPP_COLON, "%<:%>");
17000
17001   /* Scan the base-specifier-list.  */
17002   while (true)
17003     {
17004       cp_token *token;
17005       tree base;
17006       bool pack_expansion_p = false;
17007
17008       /* Look for the base-specifier.  */
17009       base = cp_parser_base_specifier (parser);
17010       /* Look for the (optional) ellipsis. */
17011       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17012         {
17013           /* Consume the `...'. */
17014           cp_lexer_consume_token (parser->lexer);
17015
17016           pack_expansion_p = true;
17017         }
17018
17019       /* Add BASE to the front of the list.  */
17020       if (base != error_mark_node)
17021         {
17022           if (pack_expansion_p)
17023             /* Make this a pack expansion type. */
17024             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17025           
17026
17027           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17028             {
17029               TREE_CHAIN (base) = bases;
17030               bases = base;
17031             }
17032         }
17033       /* Peek at the next token.  */
17034       token = cp_lexer_peek_token (parser->lexer);
17035       /* If it's not a comma, then the list is complete.  */
17036       if (token->type != CPP_COMMA)
17037         break;
17038       /* Consume the `,'.  */
17039       cp_lexer_consume_token (parser->lexer);
17040     }
17041
17042   /* PARSER->SCOPE may still be non-NULL at this point, if the last
17043      base class had a qualified name.  However, the next name that
17044      appears is certainly not qualified.  */
17045   parser->scope = NULL_TREE;
17046   parser->qualifying_scope = NULL_TREE;
17047   parser->object_scope = NULL_TREE;
17048
17049   return nreverse (bases);
17050 }
17051
17052 /* Parse a base-specifier.
17053
17054    base-specifier:
17055      :: [opt] nested-name-specifier [opt] class-name
17056      virtual access-specifier [opt] :: [opt] nested-name-specifier
17057        [opt] class-name
17058      access-specifier virtual [opt] :: [opt] nested-name-specifier
17059        [opt] class-name
17060
17061    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
17062    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17063    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
17064    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
17065
17066 static tree
17067 cp_parser_base_specifier (cp_parser* parser)
17068 {
17069   cp_token *token;
17070   bool done = false;
17071   bool virtual_p = false;
17072   bool duplicate_virtual_error_issued_p = false;
17073   bool duplicate_access_error_issued_p = false;
17074   bool class_scope_p, template_p;
17075   tree access = access_default_node;
17076   tree type;
17077
17078   /* Process the optional `virtual' and `access-specifier'.  */
17079   while (!done)
17080     {
17081       /* Peek at the next token.  */
17082       token = cp_lexer_peek_token (parser->lexer);
17083       /* Process `virtual'.  */
17084       switch (token->keyword)
17085         {
17086         case RID_VIRTUAL:
17087           /* If `virtual' appears more than once, issue an error.  */
17088           if (virtual_p && !duplicate_virtual_error_issued_p)
17089             {
17090               cp_parser_error (parser,
17091                                "%<virtual%> specified more than once in base-specified");
17092               duplicate_virtual_error_issued_p = true;
17093             }
17094
17095           virtual_p = true;
17096
17097           /* Consume the `virtual' token.  */
17098           cp_lexer_consume_token (parser->lexer);
17099
17100           break;
17101
17102         case RID_PUBLIC:
17103         case RID_PROTECTED:
17104         case RID_PRIVATE:
17105           /* If more than one access specifier appears, issue an
17106              error.  */
17107           if (access != access_default_node
17108               && !duplicate_access_error_issued_p)
17109             {
17110               cp_parser_error (parser,
17111                                "more than one access specifier in base-specified");
17112               duplicate_access_error_issued_p = true;
17113             }
17114
17115           access = ridpointers[(int) token->keyword];
17116
17117           /* Consume the access-specifier.  */
17118           cp_lexer_consume_token (parser->lexer);
17119
17120           break;
17121
17122         default:
17123           done = true;
17124           break;
17125         }
17126     }
17127   /* It is not uncommon to see programs mechanically, erroneously, use
17128      the 'typename' keyword to denote (dependent) qualified types
17129      as base classes.  */
17130   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17131     {
17132       token = cp_lexer_peek_token (parser->lexer);
17133       if (!processing_template_decl)
17134         error_at (token->location,
17135                   "keyword %<typename%> not allowed outside of templates");
17136       else
17137         error_at (token->location,
17138                   "keyword %<typename%> not allowed in this context "
17139                   "(the base class is implicitly a type)");
17140       cp_lexer_consume_token (parser->lexer);
17141     }
17142
17143   /* Look for the optional `::' operator.  */
17144   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17145   /* Look for the nested-name-specifier.  The simplest way to
17146      implement:
17147
17148        [temp.res]
17149
17150        The keyword `typename' is not permitted in a base-specifier or
17151        mem-initializer; in these contexts a qualified name that
17152        depends on a template-parameter is implicitly assumed to be a
17153        type name.
17154
17155      is to pretend that we have seen the `typename' keyword at this
17156      point.  */
17157   cp_parser_nested_name_specifier_opt (parser,
17158                                        /*typename_keyword_p=*/true,
17159                                        /*check_dependency_p=*/true,
17160                                        typename_type,
17161                                        /*is_declaration=*/true);
17162   /* If the base class is given by a qualified name, assume that names
17163      we see are type names or templates, as appropriate.  */
17164   class_scope_p = (parser->scope && TYPE_P (parser->scope));
17165   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17166
17167   /* Finally, look for the class-name.  */
17168   type = cp_parser_class_name (parser,
17169                                class_scope_p,
17170                                template_p,
17171                                typename_type,
17172                                /*check_dependency_p=*/true,
17173                                /*class_head_p=*/false,
17174                                /*is_declaration=*/true);
17175
17176   if (type == error_mark_node)
17177     return error_mark_node;
17178
17179   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17180 }
17181
17182 /* Exception handling [gram.exception] */
17183
17184 /* Parse an (optional) exception-specification.
17185
17186    exception-specification:
17187      throw ( type-id-list [opt] )
17188
17189    Returns a TREE_LIST representing the exception-specification.  The
17190    TREE_VALUE of each node is a type.  */
17191
17192 static tree
17193 cp_parser_exception_specification_opt (cp_parser* parser)
17194 {
17195   cp_token *token;
17196   tree type_id_list;
17197
17198   /* Peek at the next token.  */
17199   token = cp_lexer_peek_token (parser->lexer);
17200   /* If it's not `throw', then there's no exception-specification.  */
17201   if (!cp_parser_is_keyword (token, RID_THROW))
17202     return NULL_TREE;
17203
17204   /* Consume the `throw'.  */
17205   cp_lexer_consume_token (parser->lexer);
17206
17207   /* Look for the `('.  */
17208   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17209
17210   /* Peek at the next token.  */
17211   token = cp_lexer_peek_token (parser->lexer);
17212   /* If it's not a `)', then there is a type-id-list.  */
17213   if (token->type != CPP_CLOSE_PAREN)
17214     {
17215       const char *saved_message;
17216
17217       /* Types may not be defined in an exception-specification.  */
17218       saved_message = parser->type_definition_forbidden_message;
17219       parser->type_definition_forbidden_message
17220         = G_("types may not be defined in an exception-specification");
17221       /* Parse the type-id-list.  */
17222       type_id_list = cp_parser_type_id_list (parser);
17223       /* Restore the saved message.  */
17224       parser->type_definition_forbidden_message = saved_message;
17225     }
17226   else
17227     type_id_list = empty_except_spec;
17228
17229   /* Look for the `)'.  */
17230   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17231
17232   return type_id_list;
17233 }
17234
17235 /* Parse an (optional) type-id-list.
17236
17237    type-id-list:
17238      type-id ... [opt]
17239      type-id-list , type-id ... [opt]
17240
17241    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
17242    in the order that the types were presented.  */
17243
17244 static tree
17245 cp_parser_type_id_list (cp_parser* parser)
17246 {
17247   tree types = NULL_TREE;
17248
17249   while (true)
17250     {
17251       cp_token *token;
17252       tree type;
17253
17254       /* Get the next type-id.  */
17255       type = cp_parser_type_id (parser);
17256       /* Parse the optional ellipsis. */
17257       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17258         {
17259           /* Consume the `...'. */
17260           cp_lexer_consume_token (parser->lexer);
17261
17262           /* Turn the type into a pack expansion expression. */
17263           type = make_pack_expansion (type);
17264         }
17265       /* Add it to the list.  */
17266       types = add_exception_specifier (types, type, /*complain=*/1);
17267       /* Peek at the next token.  */
17268       token = cp_lexer_peek_token (parser->lexer);
17269       /* If it is not a `,', we are done.  */
17270       if (token->type != CPP_COMMA)
17271         break;
17272       /* Consume the `,'.  */
17273       cp_lexer_consume_token (parser->lexer);
17274     }
17275
17276   return nreverse (types);
17277 }
17278
17279 /* Parse a try-block.
17280
17281    try-block:
17282      try compound-statement handler-seq  */
17283
17284 static tree
17285 cp_parser_try_block (cp_parser* parser)
17286 {
17287   tree try_block;
17288
17289   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17290   try_block = begin_try_block ();
17291   cp_parser_compound_statement (parser, NULL, true);
17292   finish_try_block (try_block);
17293   cp_parser_handler_seq (parser);
17294   finish_handler_sequence (try_block);
17295
17296   return try_block;
17297 }
17298
17299 /* Parse a function-try-block.
17300
17301    function-try-block:
17302      try ctor-initializer [opt] function-body handler-seq  */
17303
17304 static bool
17305 cp_parser_function_try_block (cp_parser* parser)
17306 {
17307   tree compound_stmt;
17308   tree try_block;
17309   bool ctor_initializer_p;
17310
17311   /* Look for the `try' keyword.  */
17312   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17313     return false;
17314   /* Let the rest of the front end know where we are.  */
17315   try_block = begin_function_try_block (&compound_stmt);
17316   /* Parse the function-body.  */
17317   ctor_initializer_p
17318     = cp_parser_ctor_initializer_opt_and_function_body (parser);
17319   /* We're done with the `try' part.  */
17320   finish_function_try_block (try_block);
17321   /* Parse the handlers.  */
17322   cp_parser_handler_seq (parser);
17323   /* We're done with the handlers.  */
17324   finish_function_handler_sequence (try_block, compound_stmt);
17325
17326   return ctor_initializer_p;
17327 }
17328
17329 /* Parse a handler-seq.
17330
17331    handler-seq:
17332      handler handler-seq [opt]  */
17333
17334 static void
17335 cp_parser_handler_seq (cp_parser* parser)
17336 {
17337   while (true)
17338     {
17339       cp_token *token;
17340
17341       /* Parse the handler.  */
17342       cp_parser_handler (parser);
17343       /* Peek at the next token.  */
17344       token = cp_lexer_peek_token (parser->lexer);
17345       /* If it's not `catch' then there are no more handlers.  */
17346       if (!cp_parser_is_keyword (token, RID_CATCH))
17347         break;
17348     }
17349 }
17350
17351 /* Parse a handler.
17352
17353    handler:
17354      catch ( exception-declaration ) compound-statement  */
17355
17356 static void
17357 cp_parser_handler (cp_parser* parser)
17358 {
17359   tree handler;
17360   tree declaration;
17361
17362   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17363   handler = begin_handler ();
17364   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17365   declaration = cp_parser_exception_declaration (parser);
17366   finish_handler_parms (declaration, handler);
17367   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17368   cp_parser_compound_statement (parser, NULL, false);
17369   finish_handler (handler);
17370 }
17371
17372 /* Parse an exception-declaration.
17373
17374    exception-declaration:
17375      type-specifier-seq declarator
17376      type-specifier-seq abstract-declarator
17377      type-specifier-seq
17378      ...
17379
17380    Returns a VAR_DECL for the declaration, or NULL_TREE if the
17381    ellipsis variant is used.  */
17382
17383 static tree
17384 cp_parser_exception_declaration (cp_parser* parser)
17385 {
17386   cp_decl_specifier_seq type_specifiers;
17387   cp_declarator *declarator;
17388   const char *saved_message;
17389
17390   /* If it's an ellipsis, it's easy to handle.  */
17391   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17392     {
17393       /* Consume the `...' token.  */
17394       cp_lexer_consume_token (parser->lexer);
17395       return NULL_TREE;
17396     }
17397
17398   /* Types may not be defined in exception-declarations.  */
17399   saved_message = parser->type_definition_forbidden_message;
17400   parser->type_definition_forbidden_message
17401     = G_("types may not be defined in exception-declarations");
17402
17403   /* Parse the type-specifier-seq.  */
17404   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17405                                 /*is_trailing_return=*/false,
17406                                 &type_specifiers);
17407   /* If it's a `)', then there is no declarator.  */
17408   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17409     declarator = NULL;
17410   else
17411     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17412                                        /*ctor_dtor_or_conv_p=*/NULL,
17413                                        /*parenthesized_p=*/NULL,
17414                                        /*member_p=*/false);
17415
17416   /* Restore the saved message.  */
17417   parser->type_definition_forbidden_message = saved_message;
17418
17419   if (!type_specifiers.any_specifiers_p)
17420     return error_mark_node;
17421
17422   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17423 }
17424
17425 /* Parse a throw-expression.
17426
17427    throw-expression:
17428      throw assignment-expression [opt]
17429
17430    Returns a THROW_EXPR representing the throw-expression.  */
17431
17432 static tree
17433 cp_parser_throw_expression (cp_parser* parser)
17434 {
17435   tree expression;
17436   cp_token* token;
17437
17438   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17439   token = cp_lexer_peek_token (parser->lexer);
17440   /* Figure out whether or not there is an assignment-expression
17441      following the "throw" keyword.  */
17442   if (token->type == CPP_COMMA
17443       || token->type == CPP_SEMICOLON
17444       || token->type == CPP_CLOSE_PAREN
17445       || token->type == CPP_CLOSE_SQUARE
17446       || token->type == CPP_CLOSE_BRACE
17447       || token->type == CPP_COLON)
17448     expression = NULL_TREE;
17449   else
17450     expression = cp_parser_assignment_expression (parser,
17451                                                   /*cast_p=*/false, NULL);
17452
17453   return build_throw (expression);
17454 }
17455
17456 /* GNU Extensions */
17457
17458 /* Parse an (optional) asm-specification.
17459
17460    asm-specification:
17461      asm ( string-literal )
17462
17463    If the asm-specification is present, returns a STRING_CST
17464    corresponding to the string-literal.  Otherwise, returns
17465    NULL_TREE.  */
17466
17467 static tree
17468 cp_parser_asm_specification_opt (cp_parser* parser)
17469 {
17470   cp_token *token;
17471   tree asm_specification;
17472
17473   /* Peek at the next token.  */
17474   token = cp_lexer_peek_token (parser->lexer);
17475   /* If the next token isn't the `asm' keyword, then there's no
17476      asm-specification.  */
17477   if (!cp_parser_is_keyword (token, RID_ASM))
17478     return NULL_TREE;
17479
17480   /* Consume the `asm' token.  */
17481   cp_lexer_consume_token (parser->lexer);
17482   /* Look for the `('.  */
17483   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17484
17485   /* Look for the string-literal.  */
17486   asm_specification = cp_parser_string_literal (parser, false, false);
17487
17488   /* Look for the `)'.  */
17489   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17490
17491   return asm_specification;
17492 }
17493
17494 /* Parse an asm-operand-list.
17495
17496    asm-operand-list:
17497      asm-operand
17498      asm-operand-list , asm-operand
17499
17500    asm-operand:
17501      string-literal ( expression )
17502      [ string-literal ] string-literal ( expression )
17503
17504    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
17505    each node is the expression.  The TREE_PURPOSE is itself a
17506    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17507    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17508    is a STRING_CST for the string literal before the parenthesis. Returns
17509    ERROR_MARK_NODE if any of the operands are invalid.  */
17510
17511 static tree
17512 cp_parser_asm_operand_list (cp_parser* parser)
17513 {
17514   tree asm_operands = NULL_TREE;
17515   bool invalid_operands = false;
17516
17517   while (true)
17518     {
17519       tree string_literal;
17520       tree expression;
17521       tree name;
17522
17523       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17524         {
17525           /* Consume the `[' token.  */
17526           cp_lexer_consume_token (parser->lexer);
17527           /* Read the operand name.  */
17528           name = cp_parser_identifier (parser);
17529           if (name != error_mark_node)
17530             name = build_string (IDENTIFIER_LENGTH (name),
17531                                  IDENTIFIER_POINTER (name));
17532           /* Look for the closing `]'.  */
17533           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17534         }
17535       else
17536         name = NULL_TREE;
17537       /* Look for the string-literal.  */
17538       string_literal = cp_parser_string_literal (parser, false, false);
17539
17540       /* Look for the `('.  */
17541       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17542       /* Parse the expression.  */
17543       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17544       /* Look for the `)'.  */
17545       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17546
17547       if (name == error_mark_node 
17548           || string_literal == error_mark_node 
17549           || expression == error_mark_node)
17550         invalid_operands = true;
17551
17552       /* Add this operand to the list.  */
17553       asm_operands = tree_cons (build_tree_list (name, string_literal),
17554                                 expression,
17555                                 asm_operands);
17556       /* If the next token is not a `,', there are no more
17557          operands.  */
17558       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17559         break;
17560       /* Consume the `,'.  */
17561       cp_lexer_consume_token (parser->lexer);
17562     }
17563
17564   return invalid_operands ? error_mark_node : nreverse (asm_operands);
17565 }
17566
17567 /* Parse an asm-clobber-list.
17568
17569    asm-clobber-list:
17570      string-literal
17571      asm-clobber-list , string-literal
17572
17573    Returns a TREE_LIST, indicating the clobbers in the order that they
17574    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
17575
17576 static tree
17577 cp_parser_asm_clobber_list (cp_parser* parser)
17578 {
17579   tree clobbers = NULL_TREE;
17580
17581   while (true)
17582     {
17583       tree string_literal;
17584
17585       /* Look for the string literal.  */
17586       string_literal = cp_parser_string_literal (parser, false, false);
17587       /* Add it to the list.  */
17588       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17589       /* If the next token is not a `,', then the list is
17590          complete.  */
17591       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17592         break;
17593       /* Consume the `,' token.  */
17594       cp_lexer_consume_token (parser->lexer);
17595     }
17596
17597   return clobbers;
17598 }
17599
17600 /* Parse an asm-label-list.
17601
17602    asm-label-list:
17603      identifier
17604      asm-label-list , identifier
17605
17606    Returns a TREE_LIST, indicating the labels in the order that they
17607    appeared.  The TREE_VALUE of each node is a label.  */
17608
17609 static tree
17610 cp_parser_asm_label_list (cp_parser* parser)
17611 {
17612   tree labels = NULL_TREE;
17613
17614   while (true)
17615     {
17616       tree identifier, label, name;
17617
17618       /* Look for the identifier.  */
17619       identifier = cp_parser_identifier (parser);
17620       if (!error_operand_p (identifier))
17621         {
17622           label = lookup_label (identifier);
17623           if (TREE_CODE (label) == LABEL_DECL)
17624             {
17625               TREE_USED (label) = 1;
17626               check_goto (label);
17627               name = build_string (IDENTIFIER_LENGTH (identifier),
17628                                    IDENTIFIER_POINTER (identifier));
17629               labels = tree_cons (name, label, labels);
17630             }
17631         }
17632       /* If the next token is not a `,', then the list is
17633          complete.  */
17634       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17635         break;
17636       /* Consume the `,' token.  */
17637       cp_lexer_consume_token (parser->lexer);
17638     }
17639
17640   return nreverse (labels);
17641 }
17642
17643 /* Parse an (optional) series of attributes.
17644
17645    attributes:
17646      attributes attribute
17647
17648    attribute:
17649      __attribute__ (( attribute-list [opt] ))
17650
17651    The return value is as for cp_parser_attribute_list.  */
17652
17653 static tree
17654 cp_parser_attributes_opt (cp_parser* parser)
17655 {
17656   tree attributes = NULL_TREE;
17657
17658   while (true)
17659     {
17660       cp_token *token;
17661       tree attribute_list;
17662
17663       /* Peek at the next token.  */
17664       token = cp_lexer_peek_token (parser->lexer);
17665       /* If it's not `__attribute__', then we're done.  */
17666       if (token->keyword != RID_ATTRIBUTE)
17667         break;
17668
17669       /* Consume the `__attribute__' keyword.  */
17670       cp_lexer_consume_token (parser->lexer);
17671       /* Look for the two `(' tokens.  */
17672       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17673       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17674
17675       /* Peek at the next token.  */
17676       token = cp_lexer_peek_token (parser->lexer);
17677       if (token->type != CPP_CLOSE_PAREN)
17678         /* Parse the attribute-list.  */
17679         attribute_list = cp_parser_attribute_list (parser);
17680       else
17681         /* If the next token is a `)', then there is no attribute
17682            list.  */
17683         attribute_list = NULL;
17684
17685       /* Look for the two `)' tokens.  */
17686       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17687       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17688
17689       /* Add these new attributes to the list.  */
17690       attributes = chainon (attributes, attribute_list);
17691     }
17692
17693   return attributes;
17694 }
17695
17696 /* Parse an attribute-list.
17697
17698    attribute-list:
17699      attribute
17700      attribute-list , attribute
17701
17702    attribute:
17703      identifier
17704      identifier ( identifier )
17705      identifier ( identifier , expression-list )
17706      identifier ( expression-list )
17707
17708    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
17709    to an attribute.  The TREE_PURPOSE of each node is the identifier
17710    indicating which attribute is in use.  The TREE_VALUE represents
17711    the arguments, if any.  */
17712
17713 static tree
17714 cp_parser_attribute_list (cp_parser* parser)
17715 {
17716   tree attribute_list = NULL_TREE;
17717   bool save_translate_strings_p = parser->translate_strings_p;
17718
17719   parser->translate_strings_p = false;
17720   while (true)
17721     {
17722       cp_token *token;
17723       tree identifier;
17724       tree attribute;
17725
17726       /* Look for the identifier.  We also allow keywords here; for
17727          example `__attribute__ ((const))' is legal.  */
17728       token = cp_lexer_peek_token (parser->lexer);
17729       if (token->type == CPP_NAME
17730           || token->type == CPP_KEYWORD)
17731         {
17732           tree arguments = NULL_TREE;
17733
17734           /* Consume the token.  */
17735           token = cp_lexer_consume_token (parser->lexer);
17736
17737           /* Save away the identifier that indicates which attribute
17738              this is.  */
17739           identifier = (token->type == CPP_KEYWORD) 
17740             /* For keywords, use the canonical spelling, not the
17741                parsed identifier.  */
17742             ? ridpointers[(int) token->keyword]
17743             : token->u.value;
17744           
17745           attribute = build_tree_list (identifier, NULL_TREE);
17746
17747           /* Peek at the next token.  */
17748           token = cp_lexer_peek_token (parser->lexer);
17749           /* If it's an `(', then parse the attribute arguments.  */
17750           if (token->type == CPP_OPEN_PAREN)
17751             {
17752               VEC(tree,gc) *vec;
17753               vec = cp_parser_parenthesized_expression_list
17754                     (parser, true, /*cast_p=*/false,
17755                      /*allow_expansion_p=*/false,
17756                      /*non_constant_p=*/NULL);
17757               if (vec == NULL)
17758                 arguments = error_mark_node;
17759               else
17760                 {
17761                   arguments = build_tree_list_vec (vec);
17762                   release_tree_vector (vec);
17763                 }
17764               /* Save the arguments away.  */
17765               TREE_VALUE (attribute) = arguments;
17766             }
17767
17768           if (arguments != error_mark_node)
17769             {
17770               /* Add this attribute to the list.  */
17771               TREE_CHAIN (attribute) = attribute_list;
17772               attribute_list = attribute;
17773             }
17774
17775           token = cp_lexer_peek_token (parser->lexer);
17776         }
17777       /* Now, look for more attributes.  If the next token isn't a
17778          `,', we're done.  */
17779       if (token->type != CPP_COMMA)
17780         break;
17781
17782       /* Consume the comma and keep going.  */
17783       cp_lexer_consume_token (parser->lexer);
17784     }
17785   parser->translate_strings_p = save_translate_strings_p;
17786
17787   /* We built up the list in reverse order.  */
17788   return nreverse (attribute_list);
17789 }
17790
17791 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
17792    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
17793    current value of the PEDANTIC flag, regardless of whether or not
17794    the `__extension__' keyword is present.  The caller is responsible
17795    for restoring the value of the PEDANTIC flag.  */
17796
17797 static bool
17798 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17799 {
17800   /* Save the old value of the PEDANTIC flag.  */
17801   *saved_pedantic = pedantic;
17802
17803   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17804     {
17805       /* Consume the `__extension__' token.  */
17806       cp_lexer_consume_token (parser->lexer);
17807       /* We're not being pedantic while the `__extension__' keyword is
17808          in effect.  */
17809       pedantic = 0;
17810
17811       return true;
17812     }
17813
17814   return false;
17815 }
17816
17817 /* Parse a label declaration.
17818
17819    label-declaration:
17820      __label__ label-declarator-seq ;
17821
17822    label-declarator-seq:
17823      identifier , label-declarator-seq
17824      identifier  */
17825
17826 static void
17827 cp_parser_label_declaration (cp_parser* parser)
17828 {
17829   /* Look for the `__label__' keyword.  */
17830   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17831
17832   while (true)
17833     {
17834       tree identifier;
17835
17836       /* Look for an identifier.  */
17837       identifier = cp_parser_identifier (parser);
17838       /* If we failed, stop.  */
17839       if (identifier == error_mark_node)
17840         break;
17841       /* Declare it as a label.  */
17842       finish_label_decl (identifier);
17843       /* If the next token is a `;', stop.  */
17844       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17845         break;
17846       /* Look for the `,' separating the label declarations.  */
17847       cp_parser_require (parser, CPP_COMMA, "%<,%>");
17848     }
17849
17850   /* Look for the final `;'.  */
17851   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17852 }
17853
17854 /* Support Functions */
17855
17856 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17857    NAME should have one of the representations used for an
17858    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17859    is returned.  If PARSER->SCOPE is a dependent type, then a
17860    SCOPE_REF is returned.
17861
17862    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17863    returned; the name was already resolved when the TEMPLATE_ID_EXPR
17864    was formed.  Abstractly, such entities should not be passed to this
17865    function, because they do not need to be looked up, but it is
17866    simpler to check for this special case here, rather than at the
17867    call-sites.
17868
17869    In cases not explicitly covered above, this function returns a
17870    DECL, OVERLOAD, or baselink representing the result of the lookup.
17871    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17872    is returned.
17873
17874    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17875    (e.g., "struct") that was used.  In that case bindings that do not
17876    refer to types are ignored.
17877
17878    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17879    ignored.
17880
17881    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17882    are ignored.
17883
17884    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17885    types.
17886
17887    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17888    TREE_LIST of candidates if name-lookup results in an ambiguity, and
17889    NULL_TREE otherwise.  */
17890
17891 static tree
17892 cp_parser_lookup_name (cp_parser *parser, tree name,
17893                        enum tag_types tag_type,
17894                        bool is_template,
17895                        bool is_namespace,
17896                        bool check_dependency,
17897                        tree *ambiguous_decls,
17898                        location_t name_location)
17899 {
17900   int flags = 0;
17901   tree decl;
17902   tree object_type = parser->context->object_type;
17903
17904   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17905     flags |= LOOKUP_COMPLAIN;
17906
17907   /* Assume that the lookup will be unambiguous.  */
17908   if (ambiguous_decls)
17909     *ambiguous_decls = NULL_TREE;
17910
17911   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17912      no longer valid.  Note that if we are parsing tentatively, and
17913      the parse fails, OBJECT_TYPE will be automatically restored.  */
17914   parser->context->object_type = NULL_TREE;
17915
17916   if (name == error_mark_node)
17917     return error_mark_node;
17918
17919   /* A template-id has already been resolved; there is no lookup to
17920      do.  */
17921   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17922     return name;
17923   if (BASELINK_P (name))
17924     {
17925       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17926                   == TEMPLATE_ID_EXPR);
17927       return name;
17928     }
17929
17930   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17931      it should already have been checked to make sure that the name
17932      used matches the type being destroyed.  */
17933   if (TREE_CODE (name) == BIT_NOT_EXPR)
17934     {
17935       tree type;
17936
17937       /* Figure out to which type this destructor applies.  */
17938       if (parser->scope)
17939         type = parser->scope;
17940       else if (object_type)
17941         type = object_type;
17942       else
17943         type = current_class_type;
17944       /* If that's not a class type, there is no destructor.  */
17945       if (!type || !CLASS_TYPE_P (type))
17946         return error_mark_node;
17947       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17948         lazily_declare_fn (sfk_destructor, type);
17949       if (!CLASSTYPE_DESTRUCTORS (type))
17950           return error_mark_node;
17951       /* If it was a class type, return the destructor.  */
17952       return CLASSTYPE_DESTRUCTORS (type);
17953     }
17954
17955   /* By this point, the NAME should be an ordinary identifier.  If
17956      the id-expression was a qualified name, the qualifying scope is
17957      stored in PARSER->SCOPE at this point.  */
17958   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17959
17960   /* Perform the lookup.  */
17961   if (parser->scope)
17962     {
17963       bool dependent_p;
17964
17965       if (parser->scope == error_mark_node)
17966         return error_mark_node;
17967
17968       /* If the SCOPE is dependent, the lookup must be deferred until
17969          the template is instantiated -- unless we are explicitly
17970          looking up names in uninstantiated templates.  Even then, we
17971          cannot look up the name if the scope is not a class type; it
17972          might, for example, be a template type parameter.  */
17973       dependent_p = (TYPE_P (parser->scope)
17974                      && dependent_scope_p (parser->scope));
17975       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17976           && dependent_p)
17977         /* Defer lookup.  */
17978         decl = error_mark_node;
17979       else
17980         {
17981           tree pushed_scope = NULL_TREE;
17982
17983           /* If PARSER->SCOPE is a dependent type, then it must be a
17984              class type, and we must not be checking dependencies;
17985              otherwise, we would have processed this lookup above.  So
17986              that PARSER->SCOPE is not considered a dependent base by
17987              lookup_member, we must enter the scope here.  */
17988           if (dependent_p)
17989             pushed_scope = push_scope (parser->scope);
17990
17991           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
17992              lookup result and the nested-name-specifier nominates a class C:
17993                * if the name specified after the nested-name-specifier, when
17994                looked up in C, is the injected-class-name of C (Clause 9), or
17995                * if the name specified after the nested-name-specifier is the
17996                same as the identifier or the simple-template-id's template-
17997                name in the last component of the nested-name-specifier,
17998              the name is instead considered to name the constructor of
17999              class C. [ Note: for example, the constructor is not an
18000              acceptable lookup result in an elaborated-type-specifier so
18001              the constructor would not be used in place of the
18002              injected-class-name. --end note ] Such a constructor name
18003              shall be used only in the declarator-id of a declaration that
18004              names a constructor or in a using-declaration.  */
18005           if (tag_type == none_type
18006               && CLASS_TYPE_P (parser->scope)
18007               && constructor_name_p (name, parser->scope))
18008             name = ctor_identifier;
18009
18010           /* If the PARSER->SCOPE is a template specialization, it
18011              may be instantiated during name lookup.  In that case,
18012              errors may be issued.  Even if we rollback the current
18013              tentative parse, those errors are valid.  */
18014           decl = lookup_qualified_name (parser->scope, name,
18015                                         tag_type != none_type,
18016                                         /*complain=*/true);
18017
18018           /* If we have a single function from a using decl, pull it out.  */
18019           if (TREE_CODE (decl) == OVERLOAD
18020               && !really_overloaded_fn (decl))
18021             decl = OVL_FUNCTION (decl);
18022
18023           if (pushed_scope)
18024             pop_scope (pushed_scope);
18025         }
18026
18027       /* If the scope is a dependent type and either we deferred lookup or
18028          we did lookup but didn't find the name, rememeber the name.  */
18029       if (decl == error_mark_node && TYPE_P (parser->scope)
18030           && dependent_type_p (parser->scope))
18031         {
18032           if (tag_type)
18033             {
18034               tree type;
18035
18036               /* The resolution to Core Issue 180 says that `struct
18037                  A::B' should be considered a type-name, even if `A'
18038                  is dependent.  */
18039               type = make_typename_type (parser->scope, name, tag_type,
18040                                          /*complain=*/tf_error);
18041               decl = TYPE_NAME (type);
18042             }
18043           else if (is_template
18044                    && (cp_parser_next_token_ends_template_argument_p (parser)
18045                        || cp_lexer_next_token_is (parser->lexer,
18046                                                   CPP_CLOSE_PAREN)))
18047             decl = make_unbound_class_template (parser->scope,
18048                                                 name, NULL_TREE,
18049                                                 /*complain=*/tf_error);
18050           else
18051             decl = build_qualified_name (/*type=*/NULL_TREE,
18052                                          parser->scope, name,
18053                                          is_template);
18054         }
18055       parser->qualifying_scope = parser->scope;
18056       parser->object_scope = NULL_TREE;
18057     }
18058   else if (object_type)
18059     {
18060       tree object_decl = NULL_TREE;
18061       /* Look up the name in the scope of the OBJECT_TYPE, unless the
18062          OBJECT_TYPE is not a class.  */
18063       if (CLASS_TYPE_P (object_type))
18064         /* If the OBJECT_TYPE is a template specialization, it may
18065            be instantiated during name lookup.  In that case, errors
18066            may be issued.  Even if we rollback the current tentative
18067            parse, those errors are valid.  */
18068         object_decl = lookup_member (object_type,
18069                                      name,
18070                                      /*protect=*/0,
18071                                      tag_type != none_type);
18072       /* Look it up in the enclosing context, too.  */
18073       decl = lookup_name_real (name, tag_type != none_type,
18074                                /*nonclass=*/0,
18075                                /*block_p=*/true, is_namespace, flags);
18076       parser->object_scope = object_type;
18077       parser->qualifying_scope = NULL_TREE;
18078       if (object_decl)
18079         decl = object_decl;
18080     }
18081   else
18082     {
18083       decl = lookup_name_real (name, tag_type != none_type,
18084                                /*nonclass=*/0,
18085                                /*block_p=*/true, is_namespace, flags);
18086       parser->qualifying_scope = NULL_TREE;
18087       parser->object_scope = NULL_TREE;
18088     }
18089
18090   /* If the lookup failed, let our caller know.  */
18091   if (!decl || decl == error_mark_node)
18092     return error_mark_node;
18093
18094   /* Pull out the template from an injected-class-name (or multiple).  */
18095   if (is_template)
18096     decl = maybe_get_template_decl_from_type_decl (decl);
18097
18098   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
18099   if (TREE_CODE (decl) == TREE_LIST)
18100     {
18101       if (ambiguous_decls)
18102         *ambiguous_decls = decl;
18103       /* The error message we have to print is too complicated for
18104          cp_parser_error, so we incorporate its actions directly.  */
18105       if (!cp_parser_simulate_error (parser))
18106         {
18107           error_at (name_location, "reference to %qD is ambiguous",
18108                     name);
18109           print_candidates (decl);
18110         }
18111       return error_mark_node;
18112     }
18113
18114   gcc_assert (DECL_P (decl)
18115               || TREE_CODE (decl) == OVERLOAD
18116               || TREE_CODE (decl) == SCOPE_REF
18117               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18118               || BASELINK_P (decl));
18119
18120   /* If we have resolved the name of a member declaration, check to
18121      see if the declaration is accessible.  When the name resolves to
18122      set of overloaded functions, accessibility is checked when
18123      overload resolution is done.
18124
18125      During an explicit instantiation, access is not checked at all,
18126      as per [temp.explicit].  */
18127   if (DECL_P (decl))
18128     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18129
18130   return decl;
18131 }
18132
18133 /* Like cp_parser_lookup_name, but for use in the typical case where
18134    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18135    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
18136
18137 static tree
18138 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18139 {
18140   return cp_parser_lookup_name (parser, name,
18141                                 none_type,
18142                                 /*is_template=*/false,
18143                                 /*is_namespace=*/false,
18144                                 /*check_dependency=*/true,
18145                                 /*ambiguous_decls=*/NULL,
18146                                 location);
18147 }
18148
18149 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18150    the current context, return the TYPE_DECL.  If TAG_NAME_P is
18151    true, the DECL indicates the class being defined in a class-head,
18152    or declared in an elaborated-type-specifier.
18153
18154    Otherwise, return DECL.  */
18155
18156 static tree
18157 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18158 {
18159   /* If the TEMPLATE_DECL is being declared as part of a class-head,
18160      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18161
18162        struct A {
18163          template <typename T> struct B;
18164        };
18165
18166        template <typename T> struct A::B {};
18167
18168      Similarly, in an elaborated-type-specifier:
18169
18170        namespace N { struct X{}; }
18171
18172        struct A {
18173          template <typename T> friend struct N::X;
18174        };
18175
18176      However, if the DECL refers to a class type, and we are in
18177      the scope of the class, then the name lookup automatically
18178      finds the TYPE_DECL created by build_self_reference rather
18179      than a TEMPLATE_DECL.  For example, in:
18180
18181        template <class T> struct S {
18182          S s;
18183        };
18184
18185      there is no need to handle such case.  */
18186
18187   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18188     return DECL_TEMPLATE_RESULT (decl);
18189
18190   return decl;
18191 }
18192
18193 /* If too many, or too few, template-parameter lists apply to the
18194    declarator, issue an error message.  Returns TRUE if all went well,
18195    and FALSE otherwise.  */
18196
18197 static bool
18198 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18199                                                 cp_declarator *declarator,
18200                                                 location_t declarator_location)
18201 {
18202   unsigned num_templates;
18203
18204   /* We haven't seen any classes that involve template parameters yet.  */
18205   num_templates = 0;
18206
18207   switch (declarator->kind)
18208     {
18209     case cdk_id:
18210       if (declarator->u.id.qualifying_scope)
18211         {
18212           tree scope;
18213
18214           scope = declarator->u.id.qualifying_scope;
18215
18216           while (scope && CLASS_TYPE_P (scope))
18217             {
18218               /* You're supposed to have one `template <...>'
18219                  for every template class, but you don't need one
18220                  for a full specialization.  For example:
18221
18222                  template <class T> struct S{};
18223                  template <> struct S<int> { void f(); };
18224                  void S<int>::f () {}
18225
18226                  is correct; there shouldn't be a `template <>' for
18227                  the definition of `S<int>::f'.  */
18228               if (!CLASSTYPE_TEMPLATE_INFO (scope))
18229                 /* If SCOPE does not have template information of any
18230                    kind, then it is not a template, nor is it nested
18231                    within a template.  */
18232                 break;
18233               if (explicit_class_specialization_p (scope))
18234                 break;
18235               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18236                 ++num_templates;
18237
18238               scope = TYPE_CONTEXT (scope);
18239             }
18240         }
18241       else if (TREE_CODE (declarator->u.id.unqualified_name)
18242                == TEMPLATE_ID_EXPR)
18243         /* If the DECLARATOR has the form `X<y>' then it uses one
18244            additional level of template parameters.  */
18245         ++num_templates;
18246
18247       return cp_parser_check_template_parameters 
18248         (parser, num_templates, declarator_location, declarator);
18249
18250
18251     case cdk_function:
18252     case cdk_array:
18253     case cdk_pointer:
18254     case cdk_reference:
18255     case cdk_ptrmem:
18256       return (cp_parser_check_declarator_template_parameters
18257               (parser, declarator->declarator, declarator_location));
18258
18259     case cdk_error:
18260       return true;
18261
18262     default:
18263       gcc_unreachable ();
18264     }
18265   return false;
18266 }
18267
18268 /* NUM_TEMPLATES were used in the current declaration.  If that is
18269    invalid, return FALSE and issue an error messages.  Otherwise,
18270    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
18271    declarator and we can print more accurate diagnostics.  */
18272
18273 static bool
18274 cp_parser_check_template_parameters (cp_parser* parser,
18275                                      unsigned num_templates,
18276                                      location_t location,
18277                                      cp_declarator *declarator)
18278 {
18279   /* If there are the same number of template classes and parameter
18280      lists, that's OK.  */
18281   if (parser->num_template_parameter_lists == num_templates)
18282     return true;
18283   /* If there are more, but only one more, then we are referring to a
18284      member template.  That's OK too.  */
18285   if (parser->num_template_parameter_lists == num_templates + 1)
18286     return true;
18287   /* If there are more template classes than parameter lists, we have
18288      something like:
18289
18290        template <class T> void S<T>::R<T>::f ();  */
18291   if (parser->num_template_parameter_lists < num_templates)
18292     {
18293       if (declarator && !current_function_decl)
18294         error_at (location, "specializing member %<%T::%E%> "
18295                   "requires %<template<>%> syntax", 
18296                   declarator->u.id.qualifying_scope,
18297                   declarator->u.id.unqualified_name);
18298       else if (declarator)
18299         error_at (location, "invalid declaration of %<%T::%E%>",
18300                   declarator->u.id.qualifying_scope,
18301                   declarator->u.id.unqualified_name);
18302       else 
18303         error_at (location, "too few template-parameter-lists");
18304       return false;
18305     }
18306   /* Otherwise, there are too many template parameter lists.  We have
18307      something like:
18308
18309      template <class T> template <class U> void S::f();  */
18310   error_at (location, "too many template-parameter-lists");
18311   return false;
18312 }
18313
18314 /* Parse an optional `::' token indicating that the following name is
18315    from the global namespace.  If so, PARSER->SCOPE is set to the
18316    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18317    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18318    Returns the new value of PARSER->SCOPE, if the `::' token is
18319    present, and NULL_TREE otherwise.  */
18320
18321 static tree
18322 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18323 {
18324   cp_token *token;
18325
18326   /* Peek at the next token.  */
18327   token = cp_lexer_peek_token (parser->lexer);
18328   /* If we're looking at a `::' token then we're starting from the
18329      global namespace, not our current location.  */
18330   if (token->type == CPP_SCOPE)
18331     {
18332       /* Consume the `::' token.  */
18333       cp_lexer_consume_token (parser->lexer);
18334       /* Set the SCOPE so that we know where to start the lookup.  */
18335       parser->scope = global_namespace;
18336       parser->qualifying_scope = global_namespace;
18337       parser->object_scope = NULL_TREE;
18338
18339       return parser->scope;
18340     }
18341   else if (!current_scope_valid_p)
18342     {
18343       parser->scope = NULL_TREE;
18344       parser->qualifying_scope = NULL_TREE;
18345       parser->object_scope = NULL_TREE;
18346     }
18347
18348   return NULL_TREE;
18349 }
18350
18351 /* Returns TRUE if the upcoming token sequence is the start of a
18352    constructor declarator.  If FRIEND_P is true, the declarator is
18353    preceded by the `friend' specifier.  */
18354
18355 static bool
18356 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18357 {
18358   bool constructor_p;
18359   tree nested_name_specifier;
18360   cp_token *next_token;
18361
18362   /* The common case is that this is not a constructor declarator, so
18363      try to avoid doing lots of work if at all possible.  It's not
18364      valid declare a constructor at function scope.  */
18365   if (parser->in_function_body)
18366     return false;
18367   /* And only certain tokens can begin a constructor declarator.  */
18368   next_token = cp_lexer_peek_token (parser->lexer);
18369   if (next_token->type != CPP_NAME
18370       && next_token->type != CPP_SCOPE
18371       && next_token->type != CPP_NESTED_NAME_SPECIFIER
18372       && next_token->type != CPP_TEMPLATE_ID)
18373     return false;
18374
18375   /* Parse tentatively; we are going to roll back all of the tokens
18376      consumed here.  */
18377   cp_parser_parse_tentatively (parser);
18378   /* Assume that we are looking at a constructor declarator.  */
18379   constructor_p = true;
18380
18381   /* Look for the optional `::' operator.  */
18382   cp_parser_global_scope_opt (parser,
18383                               /*current_scope_valid_p=*/false);
18384   /* Look for the nested-name-specifier.  */
18385   nested_name_specifier
18386     = (cp_parser_nested_name_specifier_opt (parser,
18387                                             /*typename_keyword_p=*/false,
18388                                             /*check_dependency_p=*/false,
18389                                             /*type_p=*/false,
18390                                             /*is_declaration=*/false));
18391   /* Outside of a class-specifier, there must be a
18392      nested-name-specifier.  */
18393   if (!nested_name_specifier &&
18394       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18395        || friend_p))
18396     constructor_p = false;
18397   else if (nested_name_specifier == error_mark_node)
18398     constructor_p = false;
18399
18400   /* If we have a class scope, this is easy; DR 147 says that S::S always
18401      names the constructor, and no other qualified name could.  */
18402   if (constructor_p && nested_name_specifier
18403       && TYPE_P (nested_name_specifier))
18404     {
18405       tree id = cp_parser_unqualified_id (parser,
18406                                           /*template_keyword_p=*/false,
18407                                           /*check_dependency_p=*/false,
18408                                           /*declarator_p=*/true,
18409                                           /*optional_p=*/false);
18410       if (is_overloaded_fn (id))
18411         id = DECL_NAME (get_first_fn (id));
18412       if (!constructor_name_p (id, nested_name_specifier))
18413         constructor_p = false;
18414     }
18415   /* If we still think that this might be a constructor-declarator,
18416      look for a class-name.  */
18417   else if (constructor_p)
18418     {
18419       /* If we have:
18420
18421            template <typename T> struct S {
18422              S();
18423            };
18424
18425          we must recognize that the nested `S' names a class.  */
18426       tree type_decl;
18427       type_decl = cp_parser_class_name (parser,
18428                                         /*typename_keyword_p=*/false,
18429                                         /*template_keyword_p=*/false,
18430                                         none_type,
18431                                         /*check_dependency_p=*/false,
18432                                         /*class_head_p=*/false,
18433                                         /*is_declaration=*/false);
18434       /* If there was no class-name, then this is not a constructor.  */
18435       constructor_p = !cp_parser_error_occurred (parser);
18436
18437       /* If we're still considering a constructor, we have to see a `(',
18438          to begin the parameter-declaration-clause, followed by either a
18439          `)', an `...', or a decl-specifier.  We need to check for a
18440          type-specifier to avoid being fooled into thinking that:
18441
18442            S (f) (int);
18443
18444          is a constructor.  (It is actually a function named `f' that
18445          takes one parameter (of type `int') and returns a value of type
18446          `S'.  */
18447       if (constructor_p
18448           && !cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18449         constructor_p = false;
18450
18451       if (constructor_p
18452           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18453           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18454           /* A parameter declaration begins with a decl-specifier,
18455              which is either the "attribute" keyword, a storage class
18456              specifier, or (usually) a type-specifier.  */
18457           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18458         {
18459           tree type;
18460           tree pushed_scope = NULL_TREE;
18461           unsigned saved_num_template_parameter_lists;
18462
18463           /* Names appearing in the type-specifier should be looked up
18464              in the scope of the class.  */
18465           if (current_class_type)
18466             type = NULL_TREE;
18467           else
18468             {
18469               type = TREE_TYPE (type_decl);
18470               if (TREE_CODE (type) == TYPENAME_TYPE)
18471                 {
18472                   type = resolve_typename_type (type,
18473                                                 /*only_current_p=*/false);
18474                   if (TREE_CODE (type) == TYPENAME_TYPE)
18475                     {
18476                       cp_parser_abort_tentative_parse (parser);
18477                       return false;
18478                     }
18479                 }
18480               pushed_scope = push_scope (type);
18481             }
18482
18483           /* Inside the constructor parameter list, surrounding
18484              template-parameter-lists do not apply.  */
18485           saved_num_template_parameter_lists
18486             = parser->num_template_parameter_lists;
18487           parser->num_template_parameter_lists = 0;
18488
18489           /* Look for the type-specifier.  */
18490           cp_parser_type_specifier (parser,
18491                                     CP_PARSER_FLAGS_NONE,
18492                                     /*decl_specs=*/NULL,
18493                                     /*is_declarator=*/true,
18494                                     /*declares_class_or_enum=*/NULL,
18495                                     /*is_cv_qualifier=*/NULL);
18496
18497           parser->num_template_parameter_lists
18498             = saved_num_template_parameter_lists;
18499
18500           /* Leave the scope of the class.  */
18501           if (pushed_scope)
18502             pop_scope (pushed_scope);
18503
18504           constructor_p = !cp_parser_error_occurred (parser);
18505         }
18506     }
18507
18508   /* We did not really want to consume any tokens.  */
18509   cp_parser_abort_tentative_parse (parser);
18510
18511   return constructor_p;
18512 }
18513
18514 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18515    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
18516    they must be performed once we are in the scope of the function.
18517
18518    Returns the function defined.  */
18519
18520 static tree
18521 cp_parser_function_definition_from_specifiers_and_declarator
18522   (cp_parser* parser,
18523    cp_decl_specifier_seq *decl_specifiers,
18524    tree attributes,
18525    const cp_declarator *declarator)
18526 {
18527   tree fn;
18528   bool success_p;
18529
18530   /* Begin the function-definition.  */
18531   success_p = start_function (decl_specifiers, declarator, attributes);
18532
18533   /* The things we're about to see are not directly qualified by any
18534      template headers we've seen thus far.  */
18535   reset_specialization ();
18536
18537   /* If there were names looked up in the decl-specifier-seq that we
18538      did not check, check them now.  We must wait until we are in the
18539      scope of the function to perform the checks, since the function
18540      might be a friend.  */
18541   perform_deferred_access_checks ();
18542
18543   if (!success_p)
18544     {
18545       /* Skip the entire function.  */
18546       cp_parser_skip_to_end_of_block_or_statement (parser);
18547       fn = error_mark_node;
18548     }
18549   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18550     {
18551       /* Seen already, skip it.  An error message has already been output.  */
18552       cp_parser_skip_to_end_of_block_or_statement (parser);
18553       fn = current_function_decl;
18554       current_function_decl = NULL_TREE;
18555       /* If this is a function from a class, pop the nested class.  */
18556       if (current_class_name)
18557         pop_nested_class ();
18558     }
18559   else
18560     fn = cp_parser_function_definition_after_declarator (parser,
18561                                                          /*inline_p=*/false);
18562
18563   return fn;
18564 }
18565
18566 /* Parse the part of a function-definition that follows the
18567    declarator.  INLINE_P is TRUE iff this function is an inline
18568    function defined within a class-specifier.
18569
18570    Returns the function defined.  */
18571
18572 static tree
18573 cp_parser_function_definition_after_declarator (cp_parser* parser,
18574                                                 bool inline_p)
18575 {
18576   tree fn;
18577   bool ctor_initializer_p = false;
18578   bool saved_in_unbraced_linkage_specification_p;
18579   bool saved_in_function_body;
18580   unsigned saved_num_template_parameter_lists;
18581   cp_token *token;
18582
18583   saved_in_function_body = parser->in_function_body;
18584   parser->in_function_body = true;
18585   /* If the next token is `return', then the code may be trying to
18586      make use of the "named return value" extension that G++ used to
18587      support.  */
18588   token = cp_lexer_peek_token (parser->lexer);
18589   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18590     {
18591       /* Consume the `return' keyword.  */
18592       cp_lexer_consume_token (parser->lexer);
18593       /* Look for the identifier that indicates what value is to be
18594          returned.  */
18595       cp_parser_identifier (parser);
18596       /* Issue an error message.  */
18597       error_at (token->location,
18598                 "named return values are no longer supported");
18599       /* Skip tokens until we reach the start of the function body.  */
18600       while (true)
18601         {
18602           cp_token *token = cp_lexer_peek_token (parser->lexer);
18603           if (token->type == CPP_OPEN_BRACE
18604               || token->type == CPP_EOF
18605               || token->type == CPP_PRAGMA_EOL)
18606             break;
18607           cp_lexer_consume_token (parser->lexer);
18608         }
18609     }
18610   /* The `extern' in `extern "C" void f () { ... }' does not apply to
18611      anything declared inside `f'.  */
18612   saved_in_unbraced_linkage_specification_p
18613     = parser->in_unbraced_linkage_specification_p;
18614   parser->in_unbraced_linkage_specification_p = false;
18615   /* Inside the function, surrounding template-parameter-lists do not
18616      apply.  */
18617   saved_num_template_parameter_lists
18618     = parser->num_template_parameter_lists;
18619   parser->num_template_parameter_lists = 0;
18620
18621   start_lambda_scope (current_function_decl);
18622
18623   /* If the next token is `try', then we are looking at a
18624      function-try-block.  */
18625   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18626     ctor_initializer_p = cp_parser_function_try_block (parser);
18627   /* A function-try-block includes the function-body, so we only do
18628      this next part if we're not processing a function-try-block.  */
18629   else
18630     ctor_initializer_p
18631       = cp_parser_ctor_initializer_opt_and_function_body (parser);
18632
18633   finish_lambda_scope ();
18634
18635   /* Finish the function.  */
18636   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18637                         (inline_p ? 2 : 0));
18638   /* Generate code for it, if necessary.  */
18639   expand_or_defer_fn (fn);
18640   /* Restore the saved values.  */
18641   parser->in_unbraced_linkage_specification_p
18642     = saved_in_unbraced_linkage_specification_p;
18643   parser->num_template_parameter_lists
18644     = saved_num_template_parameter_lists;
18645   parser->in_function_body = saved_in_function_body;
18646
18647   return fn;
18648 }
18649
18650 /* Parse a template-declaration, assuming that the `export' (and
18651    `extern') keywords, if present, has already been scanned.  MEMBER_P
18652    is as for cp_parser_template_declaration.  */
18653
18654 static void
18655 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18656 {
18657   tree decl = NULL_TREE;
18658   VEC (deferred_access_check,gc) *checks;
18659   tree parameter_list;
18660   bool friend_p = false;
18661   bool need_lang_pop;
18662   cp_token *token;
18663
18664   /* Look for the `template' keyword.  */
18665   token = cp_lexer_peek_token (parser->lexer);
18666   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18667     return;
18668
18669   /* And the `<'.  */
18670   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18671     return;
18672   if (at_class_scope_p () && current_function_decl)
18673     {
18674       /* 14.5.2.2 [temp.mem]
18675
18676          A local class shall not have member templates.  */
18677       error_at (token->location,
18678                 "invalid declaration of member template in local class");
18679       cp_parser_skip_to_end_of_block_or_statement (parser);
18680       return;
18681     }
18682   /* [temp]
18683
18684      A template ... shall not have C linkage.  */
18685   if (current_lang_name == lang_name_c)
18686     {
18687       error_at (token->location, "template with C linkage");
18688       /* Give it C++ linkage to avoid confusing other parts of the
18689          front end.  */
18690       push_lang_context (lang_name_cplusplus);
18691       need_lang_pop = true;
18692     }
18693   else
18694     need_lang_pop = false;
18695
18696   /* We cannot perform access checks on the template parameter
18697      declarations until we know what is being declared, just as we
18698      cannot check the decl-specifier list.  */
18699   push_deferring_access_checks (dk_deferred);
18700
18701   /* If the next token is `>', then we have an invalid
18702      specialization.  Rather than complain about an invalid template
18703      parameter, issue an error message here.  */
18704   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18705     {
18706       cp_parser_error (parser, "invalid explicit specialization");
18707       begin_specialization ();
18708       parameter_list = NULL_TREE;
18709     }
18710   else
18711     /* Parse the template parameters.  */
18712     parameter_list = cp_parser_template_parameter_list (parser);
18713
18714   /* Get the deferred access checks from the parameter list.  These
18715      will be checked once we know what is being declared, as for a
18716      member template the checks must be performed in the scope of the
18717      class containing the member.  */
18718   checks = get_deferred_access_checks ();
18719
18720   /* Look for the `>'.  */
18721   cp_parser_skip_to_end_of_template_parameter_list (parser);
18722   /* We just processed one more parameter list.  */
18723   ++parser->num_template_parameter_lists;
18724   /* If the next token is `template', there are more template
18725      parameters.  */
18726   if (cp_lexer_next_token_is_keyword (parser->lexer,
18727                                       RID_TEMPLATE))
18728     cp_parser_template_declaration_after_export (parser, member_p);
18729   else
18730     {
18731       /* There are no access checks when parsing a template, as we do not
18732          know if a specialization will be a friend.  */
18733       push_deferring_access_checks (dk_no_check);
18734       token = cp_lexer_peek_token (parser->lexer);
18735       decl = cp_parser_single_declaration (parser,
18736                                            checks,
18737                                            member_p,
18738                                            /*explicit_specialization_p=*/false,
18739                                            &friend_p);
18740       pop_deferring_access_checks ();
18741
18742       /* If this is a member template declaration, let the front
18743          end know.  */
18744       if (member_p && !friend_p && decl)
18745         {
18746           if (TREE_CODE (decl) == TYPE_DECL)
18747             cp_parser_check_access_in_redeclaration (decl, token->location);
18748
18749           decl = finish_member_template_decl (decl);
18750         }
18751       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18752         make_friend_class (current_class_type, TREE_TYPE (decl),
18753                            /*complain=*/true);
18754     }
18755   /* We are done with the current parameter list.  */
18756   --parser->num_template_parameter_lists;
18757
18758   pop_deferring_access_checks ();
18759
18760   /* Finish up.  */
18761   finish_template_decl (parameter_list);
18762
18763   /* Register member declarations.  */
18764   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18765     finish_member_declaration (decl);
18766   /* For the erroneous case of a template with C linkage, we pushed an
18767      implicit C++ linkage scope; exit that scope now.  */
18768   if (need_lang_pop)
18769     pop_lang_context ();
18770   /* If DECL is a function template, we must return to parse it later.
18771      (Even though there is no definition, there might be default
18772      arguments that need handling.)  */
18773   if (member_p && decl
18774       && (TREE_CODE (decl) == FUNCTION_DECL
18775           || DECL_FUNCTION_TEMPLATE_P (decl)))
18776     TREE_VALUE (parser->unparsed_functions_queues)
18777       = tree_cons (NULL_TREE, decl,
18778                    TREE_VALUE (parser->unparsed_functions_queues));
18779 }
18780
18781 /* Perform the deferred access checks from a template-parameter-list.
18782    CHECKS is a TREE_LIST of access checks, as returned by
18783    get_deferred_access_checks.  */
18784
18785 static void
18786 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18787 {
18788   ++processing_template_parmlist;
18789   perform_access_checks (checks);
18790   --processing_template_parmlist;
18791 }
18792
18793 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18794    `function-definition' sequence.  MEMBER_P is true, this declaration
18795    appears in a class scope.
18796
18797    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
18798    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
18799
18800 static tree
18801 cp_parser_single_declaration (cp_parser* parser,
18802                               VEC (deferred_access_check,gc)* checks,
18803                               bool member_p,
18804                               bool explicit_specialization_p,
18805                               bool* friend_p)
18806 {
18807   int declares_class_or_enum;
18808   tree decl = NULL_TREE;
18809   cp_decl_specifier_seq decl_specifiers;
18810   bool function_definition_p = false;
18811   cp_token *decl_spec_token_start;
18812
18813   /* This function is only used when processing a template
18814      declaration.  */
18815   gcc_assert (innermost_scope_kind () == sk_template_parms
18816               || innermost_scope_kind () == sk_template_spec);
18817
18818   /* Defer access checks until we know what is being declared.  */
18819   push_deferring_access_checks (dk_deferred);
18820
18821   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18822      alternative.  */
18823   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18824   cp_parser_decl_specifier_seq (parser,
18825                                 CP_PARSER_FLAGS_OPTIONAL,
18826                                 &decl_specifiers,
18827                                 &declares_class_or_enum);
18828   if (friend_p)
18829     *friend_p = cp_parser_friend_p (&decl_specifiers);
18830
18831   /* There are no template typedefs.  */
18832   if (decl_specifiers.specs[(int) ds_typedef])
18833     {
18834       error_at (decl_spec_token_start->location,
18835                 "template declaration of %<typedef%>");
18836       decl = error_mark_node;
18837     }
18838
18839   /* Gather up the access checks that occurred the
18840      decl-specifier-seq.  */
18841   stop_deferring_access_checks ();
18842
18843   /* Check for the declaration of a template class.  */
18844   if (declares_class_or_enum)
18845     {
18846       if (cp_parser_declares_only_class_p (parser))
18847         {
18848           decl = shadow_tag (&decl_specifiers);
18849
18850           /* In this case:
18851
18852                struct C {
18853                  friend template <typename T> struct A<T>::B;
18854                };
18855
18856              A<T>::B will be represented by a TYPENAME_TYPE, and
18857              therefore not recognized by shadow_tag.  */
18858           if (friend_p && *friend_p
18859               && !decl
18860               && decl_specifiers.type
18861               && TYPE_P (decl_specifiers.type))
18862             decl = decl_specifiers.type;
18863
18864           if (decl && decl != error_mark_node)
18865             decl = TYPE_NAME (decl);
18866           else
18867             decl = error_mark_node;
18868
18869           /* Perform access checks for template parameters.  */
18870           cp_parser_perform_template_parameter_access_checks (checks);
18871         }
18872     }
18873
18874   /* Complain about missing 'typename' or other invalid type names.  */
18875   if (!decl_specifiers.any_type_specifiers_p)
18876     cp_parser_parse_and_diagnose_invalid_type_name (parser);
18877
18878   /* If it's not a template class, try for a template function.  If
18879      the next token is a `;', then this declaration does not declare
18880      anything.  But, if there were errors in the decl-specifiers, then
18881      the error might well have come from an attempted class-specifier.
18882      In that case, there's no need to warn about a missing declarator.  */
18883   if (!decl
18884       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18885           || decl_specifiers.type != error_mark_node))
18886     {
18887       decl = cp_parser_init_declarator (parser,
18888                                         &decl_specifiers,
18889                                         checks,
18890                                         /*function_definition_allowed_p=*/true,
18891                                         member_p,
18892                                         declares_class_or_enum,
18893                                         &function_definition_p);
18894
18895     /* 7.1.1-1 [dcl.stc]
18896
18897        A storage-class-specifier shall not be specified in an explicit
18898        specialization...  */
18899     if (decl
18900         && explicit_specialization_p
18901         && decl_specifiers.storage_class != sc_none)
18902       {
18903         error_at (decl_spec_token_start->location,
18904                   "explicit template specialization cannot have a storage class");
18905         decl = error_mark_node;
18906       }
18907     }
18908
18909   pop_deferring_access_checks ();
18910
18911   /* Clear any current qualification; whatever comes next is the start
18912      of something new.  */
18913   parser->scope = NULL_TREE;
18914   parser->qualifying_scope = NULL_TREE;
18915   parser->object_scope = NULL_TREE;
18916   /* Look for a trailing `;' after the declaration.  */
18917   if (!function_definition_p
18918       && (decl == error_mark_node
18919           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18920     cp_parser_skip_to_end_of_block_or_statement (parser);
18921
18922   return decl;
18923 }
18924
18925 /* Parse a cast-expression that is not the operand of a unary "&".  */
18926
18927 static tree
18928 cp_parser_simple_cast_expression (cp_parser *parser)
18929 {
18930   return cp_parser_cast_expression (parser, /*address_p=*/false,
18931                                     /*cast_p=*/false, NULL);
18932 }
18933
18934 /* Parse a functional cast to TYPE.  Returns an expression
18935    representing the cast.  */
18936
18937 static tree
18938 cp_parser_functional_cast (cp_parser* parser, tree type)
18939 {
18940   VEC(tree,gc) *vec;
18941   tree expression_list;
18942   tree cast;
18943   bool nonconst_p;
18944
18945   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18946     {
18947       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18948       expression_list = cp_parser_braced_list (parser, &nonconst_p);
18949       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18950       if (TREE_CODE (type) == TYPE_DECL)
18951         type = TREE_TYPE (type);
18952       return finish_compound_literal (type, expression_list);
18953     }
18954
18955
18956   vec = cp_parser_parenthesized_expression_list (parser, false,
18957                                                  /*cast_p=*/true,
18958                                                  /*allow_expansion_p=*/true,
18959                                                  /*non_constant_p=*/NULL);
18960   if (vec == NULL)
18961     expression_list = error_mark_node;
18962   else
18963     {
18964       expression_list = build_tree_list_vec (vec);
18965       release_tree_vector (vec);
18966     }
18967
18968   cast = build_functional_cast (type, expression_list,
18969                                 tf_warning_or_error);
18970   /* [expr.const]/1: In an integral constant expression "only type
18971      conversions to integral or enumeration type can be used".  */
18972   if (TREE_CODE (type) == TYPE_DECL)
18973     type = TREE_TYPE (type);
18974   if (cast != error_mark_node
18975       && !cast_valid_in_integral_constant_expression_p (type)
18976       && (cp_parser_non_integral_constant_expression
18977           (parser, "a call to a constructor")))
18978     return error_mark_node;
18979   return cast;
18980 }
18981
18982 /* Save the tokens that make up the body of a member function defined
18983    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18984    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18985    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18986    for the member function.  */
18987
18988 static tree
18989 cp_parser_save_member_function_body (cp_parser* parser,
18990                                      cp_decl_specifier_seq *decl_specifiers,
18991                                      cp_declarator *declarator,
18992                                      tree attributes)
18993 {
18994   cp_token *first;
18995   cp_token *last;
18996   tree fn;
18997
18998   /* Create the FUNCTION_DECL.  */
18999   fn = grokmethod (decl_specifiers, declarator, attributes);
19000   /* If something went badly wrong, bail out now.  */
19001   if (fn == error_mark_node)
19002     {
19003       /* If there's a function-body, skip it.  */
19004       if (cp_parser_token_starts_function_definition_p
19005           (cp_lexer_peek_token (parser->lexer)))
19006         cp_parser_skip_to_end_of_block_or_statement (parser);
19007       return error_mark_node;
19008     }
19009
19010   /* Remember it, if there default args to post process.  */
19011   cp_parser_save_default_args (parser, fn);
19012
19013   /* Save away the tokens that make up the body of the
19014      function.  */
19015   first = parser->lexer->next_token;
19016   /* We can have braced-init-list mem-initializers before the fn body.  */
19017   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19018     {
19019       cp_lexer_consume_token (parser->lexer);
19020       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19021              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19022         {
19023           /* cache_group will stop after an un-nested { } pair, too.  */
19024           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19025             break;
19026
19027           /* variadic mem-inits have ... after the ')'.  */
19028           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19029             cp_lexer_consume_token (parser->lexer);
19030         }
19031     }
19032   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19033   /* Handle function try blocks.  */
19034   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19035     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19036   last = parser->lexer->next_token;
19037
19038   /* Save away the inline definition; we will process it when the
19039      class is complete.  */
19040   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19041   DECL_PENDING_INLINE_P (fn) = 1;
19042
19043   /* We need to know that this was defined in the class, so that
19044      friend templates are handled correctly.  */
19045   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19046
19047   /* Add FN to the queue of functions to be parsed later.  */
19048   TREE_VALUE (parser->unparsed_functions_queues)
19049     = tree_cons (NULL_TREE, fn,
19050                  TREE_VALUE (parser->unparsed_functions_queues));
19051
19052   return fn;
19053 }
19054
19055 /* Parse a template-argument-list, as well as the trailing ">" (but
19056    not the opening ">").  See cp_parser_template_argument_list for the
19057    return value.  */
19058
19059 static tree
19060 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19061 {
19062   tree arguments;
19063   tree saved_scope;
19064   tree saved_qualifying_scope;
19065   tree saved_object_scope;
19066   bool saved_greater_than_is_operator_p;
19067   int saved_unevaluated_operand;
19068   int saved_inhibit_evaluation_warnings;
19069
19070   /* [temp.names]
19071
19072      When parsing a template-id, the first non-nested `>' is taken as
19073      the end of the template-argument-list rather than a greater-than
19074      operator.  */
19075   saved_greater_than_is_operator_p
19076     = parser->greater_than_is_operator_p;
19077   parser->greater_than_is_operator_p = false;
19078   /* Parsing the argument list may modify SCOPE, so we save it
19079      here.  */
19080   saved_scope = parser->scope;
19081   saved_qualifying_scope = parser->qualifying_scope;
19082   saved_object_scope = parser->object_scope;
19083   /* We need to evaluate the template arguments, even though this
19084      template-id may be nested within a "sizeof".  */
19085   saved_unevaluated_operand = cp_unevaluated_operand;
19086   cp_unevaluated_operand = 0;
19087   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19088   c_inhibit_evaluation_warnings = 0;
19089   /* Parse the template-argument-list itself.  */
19090   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19091       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19092     arguments = NULL_TREE;
19093   else
19094     arguments = cp_parser_template_argument_list (parser);
19095   /* Look for the `>' that ends the template-argument-list. If we find
19096      a '>>' instead, it's probably just a typo.  */
19097   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19098     {
19099       if (cxx_dialect != cxx98)
19100         {
19101           /* In C++0x, a `>>' in a template argument list or cast
19102              expression is considered to be two separate `>'
19103              tokens. So, change the current token to a `>', but don't
19104              consume it: it will be consumed later when the outer
19105              template argument list (or cast expression) is parsed.
19106              Note that this replacement of `>' for `>>' is necessary
19107              even if we are parsing tentatively: in the tentative
19108              case, after calling
19109              cp_parser_enclosed_template_argument_list we will always
19110              throw away all of the template arguments and the first
19111              closing `>', either because the template argument list
19112              was erroneous or because we are replacing those tokens
19113              with a CPP_TEMPLATE_ID token.  The second `>' (which will
19114              not have been thrown away) is needed either to close an
19115              outer template argument list or to complete a new-style
19116              cast.  */
19117           cp_token *token = cp_lexer_peek_token (parser->lexer);
19118           token->type = CPP_GREATER;
19119         }
19120       else if (!saved_greater_than_is_operator_p)
19121         {
19122           /* If we're in a nested template argument list, the '>>' has
19123             to be a typo for '> >'. We emit the error message, but we
19124             continue parsing and we push a '>' as next token, so that
19125             the argument list will be parsed correctly.  Note that the
19126             global source location is still on the token before the
19127             '>>', so we need to say explicitly where we want it.  */
19128           cp_token *token = cp_lexer_peek_token (parser->lexer);
19129           error_at (token->location, "%<>>%> should be %<> >%> "
19130                     "within a nested template argument list");
19131
19132           token->type = CPP_GREATER;
19133         }
19134       else
19135         {
19136           /* If this is not a nested template argument list, the '>>'
19137             is a typo for '>'. Emit an error message and continue.
19138             Same deal about the token location, but here we can get it
19139             right by consuming the '>>' before issuing the diagnostic.  */
19140           cp_token *token = cp_lexer_consume_token (parser->lexer);
19141           error_at (token->location,
19142                     "spurious %<>>%>, use %<>%> to terminate "
19143                     "a template argument list");
19144         }
19145     }
19146   else
19147     cp_parser_skip_to_end_of_template_parameter_list (parser);
19148   /* The `>' token might be a greater-than operator again now.  */
19149   parser->greater_than_is_operator_p
19150     = saved_greater_than_is_operator_p;
19151   /* Restore the SAVED_SCOPE.  */
19152   parser->scope = saved_scope;
19153   parser->qualifying_scope = saved_qualifying_scope;
19154   parser->object_scope = saved_object_scope;
19155   cp_unevaluated_operand = saved_unevaluated_operand;
19156   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19157
19158   return arguments;
19159 }
19160
19161 /* MEMBER_FUNCTION is a member function, or a friend.  If default
19162    arguments, or the body of the function have not yet been parsed,
19163    parse them now.  */
19164
19165 static void
19166 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19167 {
19168   /* If this member is a template, get the underlying
19169      FUNCTION_DECL.  */
19170   if (DECL_FUNCTION_TEMPLATE_P (member_function))
19171     member_function = DECL_TEMPLATE_RESULT (member_function);
19172
19173   /* There should not be any class definitions in progress at this
19174      point; the bodies of members are only parsed outside of all class
19175      definitions.  */
19176   gcc_assert (parser->num_classes_being_defined == 0);
19177   /* While we're parsing the member functions we might encounter more
19178      classes.  We want to handle them right away, but we don't want
19179      them getting mixed up with functions that are currently in the
19180      queue.  */
19181   parser->unparsed_functions_queues
19182     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19183
19184   /* Make sure that any template parameters are in scope.  */
19185   maybe_begin_member_template_processing (member_function);
19186
19187   /* If the body of the function has not yet been parsed, parse it
19188      now.  */
19189   if (DECL_PENDING_INLINE_P (member_function))
19190     {
19191       tree function_scope;
19192       cp_token_cache *tokens;
19193
19194       /* The function is no longer pending; we are processing it.  */
19195       tokens = DECL_PENDING_INLINE_INFO (member_function);
19196       DECL_PENDING_INLINE_INFO (member_function) = NULL;
19197       DECL_PENDING_INLINE_P (member_function) = 0;
19198
19199       /* If this is a local class, enter the scope of the containing
19200          function.  */
19201       function_scope = current_function_decl;
19202       if (function_scope)
19203         push_function_context ();
19204
19205       /* Push the body of the function onto the lexer stack.  */
19206       cp_parser_push_lexer_for_tokens (parser, tokens);
19207
19208       /* Let the front end know that we going to be defining this
19209          function.  */
19210       start_preparsed_function (member_function, NULL_TREE,
19211                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
19212
19213       /* Don't do access checking if it is a templated function.  */
19214       if (processing_template_decl)
19215         push_deferring_access_checks (dk_no_check);
19216
19217       /* Now, parse the body of the function.  */
19218       cp_parser_function_definition_after_declarator (parser,
19219                                                       /*inline_p=*/true);
19220
19221       if (processing_template_decl)
19222         pop_deferring_access_checks ();
19223
19224       /* Leave the scope of the containing function.  */
19225       if (function_scope)
19226         pop_function_context ();
19227       cp_parser_pop_lexer (parser);
19228     }
19229
19230   /* Remove any template parameters from the symbol table.  */
19231   maybe_end_member_template_processing ();
19232
19233   /* Restore the queue.  */
19234   parser->unparsed_functions_queues
19235     = TREE_CHAIN (parser->unparsed_functions_queues);
19236 }
19237
19238 /* If DECL contains any default args, remember it on the unparsed
19239    functions queue.  */
19240
19241 static void
19242 cp_parser_save_default_args (cp_parser* parser, tree decl)
19243 {
19244   tree probe;
19245
19246   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19247        probe;
19248        probe = TREE_CHAIN (probe))
19249     if (TREE_PURPOSE (probe))
19250       {
19251         TREE_PURPOSE (parser->unparsed_functions_queues)
19252           = tree_cons (current_class_type, decl,
19253                        TREE_PURPOSE (parser->unparsed_functions_queues));
19254         break;
19255       }
19256 }
19257
19258 /* FN is a FUNCTION_DECL which may contains a parameter with an
19259    unparsed DEFAULT_ARG.  Parse the default args now.  This function
19260    assumes that the current scope is the scope in which the default
19261    argument should be processed.  */
19262
19263 static void
19264 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19265 {
19266   bool saved_local_variables_forbidden_p;
19267   tree parm, parmdecl;
19268
19269   /* While we're parsing the default args, we might (due to the
19270      statement expression extension) encounter more classes.  We want
19271      to handle them right away, but we don't want them getting mixed
19272      up with default args that are currently in the queue.  */
19273   parser->unparsed_functions_queues
19274     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19275
19276   /* Local variable names (and the `this' keyword) may not appear
19277      in a default argument.  */
19278   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19279   parser->local_variables_forbidden_p = true;
19280
19281   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19282          parmdecl = DECL_ARGUMENTS (fn);
19283        parm && parm != void_list_node;
19284        parm = TREE_CHAIN (parm),
19285          parmdecl = TREE_CHAIN (parmdecl))
19286     {
19287       cp_token_cache *tokens;
19288       tree default_arg = TREE_PURPOSE (parm);
19289       tree parsed_arg;
19290       VEC(tree,gc) *insts;
19291       tree copy;
19292       unsigned ix;
19293
19294       if (!default_arg)
19295         continue;
19296
19297       if (TREE_CODE (default_arg) != DEFAULT_ARG)
19298         /* This can happen for a friend declaration for a function
19299            already declared with default arguments.  */
19300         continue;
19301
19302        /* Push the saved tokens for the default argument onto the parser's
19303           lexer stack.  */
19304       tokens = DEFARG_TOKENS (default_arg);
19305       cp_parser_push_lexer_for_tokens (parser, tokens);
19306
19307       start_lambda_scope (parmdecl);
19308
19309       /* Parse the assignment-expression.  */
19310       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19311       if (parsed_arg == error_mark_node)
19312         {
19313           cp_parser_pop_lexer (parser);
19314           continue;
19315         }
19316
19317       if (!processing_template_decl)
19318         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19319
19320       TREE_PURPOSE (parm) = parsed_arg;
19321
19322       /* Update any instantiations we've already created.  */
19323       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19324            VEC_iterate (tree, insts, ix, copy); ix++)
19325         TREE_PURPOSE (copy) = parsed_arg;
19326
19327       finish_lambda_scope ();
19328
19329       /* If the token stream has not been completely used up, then
19330          there was extra junk after the end of the default
19331          argument.  */
19332       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19333         cp_parser_error (parser, "expected %<,%>");
19334
19335       /* Revert to the main lexer.  */
19336       cp_parser_pop_lexer (parser);
19337     }
19338
19339   /* Make sure no default arg is missing.  */
19340   check_default_args (fn);
19341
19342   /* Restore the state of local_variables_forbidden_p.  */
19343   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19344
19345   /* Restore the queue.  */
19346   parser->unparsed_functions_queues
19347     = TREE_CHAIN (parser->unparsed_functions_queues);
19348 }
19349
19350 /* Parse the operand of `sizeof' (or a similar operator).  Returns
19351    either a TYPE or an expression, depending on the form of the
19352    input.  The KEYWORD indicates which kind of expression we have
19353    encountered.  */
19354
19355 static tree
19356 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19357 {
19358   tree expr = NULL_TREE;
19359   const char *saved_message;
19360   char *tmp;
19361   bool saved_integral_constant_expression_p;
19362   bool saved_non_integral_constant_expression_p;
19363   bool pack_expansion_p = false;
19364
19365   /* Types cannot be defined in a `sizeof' expression.  Save away the
19366      old message.  */
19367   saved_message = parser->type_definition_forbidden_message;
19368   /* And create the new one.  */
19369   tmp = concat ("types may not be defined in %<",
19370                 IDENTIFIER_POINTER (ridpointers[keyword]),
19371                 "%> expressions", NULL);
19372   parser->type_definition_forbidden_message = tmp;
19373
19374   /* The restrictions on constant-expressions do not apply inside
19375      sizeof expressions.  */
19376   saved_integral_constant_expression_p
19377     = parser->integral_constant_expression_p;
19378   saved_non_integral_constant_expression_p
19379     = parser->non_integral_constant_expression_p;
19380   parser->integral_constant_expression_p = false;
19381
19382   /* If it's a `...', then we are computing the length of a parameter
19383      pack.  */
19384   if (keyword == RID_SIZEOF
19385       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19386     {
19387       /* Consume the `...'.  */
19388       cp_lexer_consume_token (parser->lexer);
19389       maybe_warn_variadic_templates ();
19390
19391       /* Note that this is an expansion.  */
19392       pack_expansion_p = true;
19393     }
19394
19395   /* Do not actually evaluate the expression.  */
19396   ++cp_unevaluated_operand;
19397   ++c_inhibit_evaluation_warnings;
19398   /* If it's a `(', then we might be looking at the type-id
19399      construction.  */
19400   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19401     {
19402       tree type;
19403       bool saved_in_type_id_in_expr_p;
19404
19405       /* We can't be sure yet whether we're looking at a type-id or an
19406          expression.  */
19407       cp_parser_parse_tentatively (parser);
19408       /* Consume the `('.  */
19409       cp_lexer_consume_token (parser->lexer);
19410       /* Parse the type-id.  */
19411       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19412       parser->in_type_id_in_expr_p = true;
19413       type = cp_parser_type_id (parser);
19414       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19415       /* Now, look for the trailing `)'.  */
19416       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19417       /* If all went well, then we're done.  */
19418       if (cp_parser_parse_definitely (parser))
19419         {
19420           cp_decl_specifier_seq decl_specs;
19421
19422           /* Build a trivial decl-specifier-seq.  */
19423           clear_decl_specs (&decl_specs);
19424           decl_specs.type = type;
19425
19426           /* Call grokdeclarator to figure out what type this is.  */
19427           expr = grokdeclarator (NULL,
19428                                  &decl_specs,
19429                                  TYPENAME,
19430                                  /*initialized=*/0,
19431                                  /*attrlist=*/NULL);
19432         }
19433     }
19434
19435   /* If the type-id production did not work out, then we must be
19436      looking at the unary-expression production.  */
19437   if (!expr)
19438     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19439                                        /*cast_p=*/false, NULL);
19440
19441   if (pack_expansion_p)
19442     /* Build a pack expansion. */
19443     expr = make_pack_expansion (expr);
19444
19445   /* Go back to evaluating expressions.  */
19446   --cp_unevaluated_operand;
19447   --c_inhibit_evaluation_warnings;
19448
19449   /* Free the message we created.  */
19450   free (tmp);
19451   /* And restore the old one.  */
19452   parser->type_definition_forbidden_message = saved_message;
19453   parser->integral_constant_expression_p
19454     = saved_integral_constant_expression_p;
19455   parser->non_integral_constant_expression_p
19456     = saved_non_integral_constant_expression_p;
19457
19458   return expr;
19459 }
19460
19461 /* If the current declaration has no declarator, return true.  */
19462
19463 static bool
19464 cp_parser_declares_only_class_p (cp_parser *parser)
19465 {
19466   /* If the next token is a `;' or a `,' then there is no
19467      declarator.  */
19468   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19469           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19470 }
19471
19472 /* Update the DECL_SPECS to reflect the storage class indicated by
19473    KEYWORD.  */
19474
19475 static void
19476 cp_parser_set_storage_class (cp_parser *parser,
19477                              cp_decl_specifier_seq *decl_specs,
19478                              enum rid keyword,
19479                              location_t location)
19480 {
19481   cp_storage_class storage_class;
19482
19483   if (parser->in_unbraced_linkage_specification_p)
19484     {
19485       error_at (location, "invalid use of %qD in linkage specification",
19486                 ridpointers[keyword]);
19487       return;
19488     }
19489   else if (decl_specs->storage_class != sc_none)
19490     {
19491       decl_specs->conflicting_specifiers_p = true;
19492       return;
19493     }
19494
19495   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19496       && decl_specs->specs[(int) ds_thread])
19497     {
19498       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19499       decl_specs->specs[(int) ds_thread] = 0;
19500     }
19501
19502   switch (keyword)
19503     {
19504     case RID_AUTO:
19505       storage_class = sc_auto;
19506       break;
19507     case RID_REGISTER:
19508       storage_class = sc_register;
19509       break;
19510     case RID_STATIC:
19511       storage_class = sc_static;
19512       break;
19513     case RID_EXTERN:
19514       storage_class = sc_extern;
19515       break;
19516     case RID_MUTABLE:
19517       storage_class = sc_mutable;
19518       break;
19519     default:
19520       gcc_unreachable ();
19521     }
19522   decl_specs->storage_class = storage_class;
19523
19524   /* A storage class specifier cannot be applied alongside a typedef 
19525      specifier. If there is a typedef specifier present then set 
19526      conflicting_specifiers_p which will trigger an error later
19527      on in grokdeclarator. */
19528   if (decl_specs->specs[(int)ds_typedef])
19529     decl_specs->conflicting_specifiers_p = true;
19530 }
19531
19532 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
19533    is true, the type is a user-defined type; otherwise it is a
19534    built-in type specified by a keyword.  */
19535
19536 static void
19537 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19538                               tree type_spec,
19539                               location_t location,
19540                               bool user_defined_p)
19541 {
19542   decl_specs->any_specifiers_p = true;
19543
19544   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19545      (with, for example, in "typedef int wchar_t;") we remember that
19546      this is what happened.  In system headers, we ignore these
19547      declarations so that G++ can work with system headers that are not
19548      C++-safe.  */
19549   if (decl_specs->specs[(int) ds_typedef]
19550       && !user_defined_p
19551       && (type_spec == boolean_type_node
19552           || type_spec == char16_type_node
19553           || type_spec == char32_type_node
19554           || type_spec == wchar_type_node)
19555       && (decl_specs->type
19556           || decl_specs->specs[(int) ds_long]
19557           || decl_specs->specs[(int) ds_short]
19558           || decl_specs->specs[(int) ds_unsigned]
19559           || decl_specs->specs[(int) ds_signed]))
19560     {
19561       decl_specs->redefined_builtin_type = type_spec;
19562       if (!decl_specs->type)
19563         {
19564           decl_specs->type = type_spec;
19565           decl_specs->user_defined_type_p = false;
19566           decl_specs->type_location = location;
19567         }
19568     }
19569   else if (decl_specs->type)
19570     decl_specs->multiple_types_p = true;
19571   else
19572     {
19573       decl_specs->type = type_spec;
19574       decl_specs->user_defined_type_p = user_defined_p;
19575       decl_specs->redefined_builtin_type = NULL_TREE;
19576       decl_specs->type_location = location;
19577     }
19578 }
19579
19580 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19581    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
19582
19583 static bool
19584 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19585 {
19586   return decl_specifiers->specs[(int) ds_friend] != 0;
19587 }
19588
19589 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
19590    issue an error message indicating that TOKEN_DESC was expected.
19591
19592    Returns the token consumed, if the token had the appropriate type.
19593    Otherwise, returns NULL.  */
19594
19595 static cp_token *
19596 cp_parser_require (cp_parser* parser,
19597                    enum cpp_ttype type,
19598                    const char* token_desc)
19599 {
19600   if (cp_lexer_next_token_is (parser->lexer, type))
19601     return cp_lexer_consume_token (parser->lexer);
19602   else
19603     {
19604       /* Output the MESSAGE -- unless we're parsing tentatively.  */
19605       if (!cp_parser_simulate_error (parser))
19606         {
19607           char *message = concat ("expected ", token_desc, NULL);
19608           cp_parser_error (parser, message);
19609           free (message);
19610         }
19611       return NULL;
19612     }
19613 }
19614
19615 /* An error message is produced if the next token is not '>'.
19616    All further tokens are skipped until the desired token is
19617    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
19618
19619 static void
19620 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19621 {
19622   /* Current level of '< ... >'.  */
19623   unsigned level = 0;
19624   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
19625   unsigned nesting_depth = 0;
19626
19627   /* Are we ready, yet?  If not, issue error message.  */
19628   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19629     return;
19630
19631   /* Skip tokens until the desired token is found.  */
19632   while (true)
19633     {
19634       /* Peek at the next token.  */
19635       switch (cp_lexer_peek_token (parser->lexer)->type)
19636         {
19637         case CPP_LESS:
19638           if (!nesting_depth)
19639             ++level;
19640           break;
19641
19642         case CPP_RSHIFT:
19643           if (cxx_dialect == cxx98)
19644             /* C++0x views the `>>' operator as two `>' tokens, but
19645                C++98 does not. */
19646             break;
19647           else if (!nesting_depth && level-- == 0)
19648             {
19649               /* We've hit a `>>' where the first `>' closes the
19650                  template argument list, and the second `>' is
19651                  spurious.  Just consume the `>>' and stop; we've
19652                  already produced at least one error.  */
19653               cp_lexer_consume_token (parser->lexer);
19654               return;
19655             }
19656           /* Fall through for C++0x, so we handle the second `>' in
19657              the `>>'.  */
19658
19659         case CPP_GREATER:
19660           if (!nesting_depth && level-- == 0)
19661             {
19662               /* We've reached the token we want, consume it and stop.  */
19663               cp_lexer_consume_token (parser->lexer);
19664               return;
19665             }
19666           break;
19667
19668         case CPP_OPEN_PAREN:
19669         case CPP_OPEN_SQUARE:
19670           ++nesting_depth;
19671           break;
19672
19673         case CPP_CLOSE_PAREN:
19674         case CPP_CLOSE_SQUARE:
19675           if (nesting_depth-- == 0)
19676             return;
19677           break;
19678
19679         case CPP_EOF:
19680         case CPP_PRAGMA_EOL:
19681         case CPP_SEMICOLON:
19682         case CPP_OPEN_BRACE:
19683         case CPP_CLOSE_BRACE:
19684           /* The '>' was probably forgotten, don't look further.  */
19685           return;
19686
19687         default:
19688           break;
19689         }
19690
19691       /* Consume this token.  */
19692       cp_lexer_consume_token (parser->lexer);
19693     }
19694 }
19695
19696 /* If the next token is the indicated keyword, consume it.  Otherwise,
19697    issue an error message indicating that TOKEN_DESC was expected.
19698
19699    Returns the token consumed, if the token had the appropriate type.
19700    Otherwise, returns NULL.  */
19701
19702 static cp_token *
19703 cp_parser_require_keyword (cp_parser* parser,
19704                            enum rid keyword,
19705                            const char* token_desc)
19706 {
19707   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19708
19709   if (token && token->keyword != keyword)
19710     {
19711       dyn_string_t error_msg;
19712
19713       /* Format the error message.  */
19714       error_msg = dyn_string_new (0);
19715       dyn_string_append_cstr (error_msg, "expected ");
19716       dyn_string_append_cstr (error_msg, token_desc);
19717       cp_parser_error (parser, error_msg->s);
19718       dyn_string_delete (error_msg);
19719       return NULL;
19720     }
19721
19722   return token;
19723 }
19724
19725 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19726    function-definition.  */
19727
19728 static bool
19729 cp_parser_token_starts_function_definition_p (cp_token* token)
19730 {
19731   return (/* An ordinary function-body begins with an `{'.  */
19732           token->type == CPP_OPEN_BRACE
19733           /* A ctor-initializer begins with a `:'.  */
19734           || token->type == CPP_COLON
19735           /* A function-try-block begins with `try'.  */
19736           || token->keyword == RID_TRY
19737           /* The named return value extension begins with `return'.  */
19738           || token->keyword == RID_RETURN);
19739 }
19740
19741 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19742    definition.  */
19743
19744 static bool
19745 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19746 {
19747   cp_token *token;
19748
19749   token = cp_lexer_peek_token (parser->lexer);
19750   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19751 }
19752
19753 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19754    C++0x) ending a template-argument.  */
19755
19756 static bool
19757 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19758 {
19759   cp_token *token;
19760
19761   token = cp_lexer_peek_token (parser->lexer);
19762   return (token->type == CPP_COMMA 
19763           || token->type == CPP_GREATER
19764           || token->type == CPP_ELLIPSIS
19765           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19766 }
19767
19768 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19769    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
19770
19771 static bool
19772 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19773                                                      size_t n)
19774 {
19775   cp_token *token;
19776
19777   token = cp_lexer_peek_nth_token (parser->lexer, n);
19778   if (token->type == CPP_LESS)
19779     return true;
19780   /* Check for the sequence `<::' in the original code. It would be lexed as
19781      `[:', where `[' is a digraph, and there is no whitespace before
19782      `:'.  */
19783   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19784     {
19785       cp_token *token2;
19786       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19787       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19788         return true;
19789     }
19790   return false;
19791 }
19792
19793 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19794    or none_type otherwise.  */
19795
19796 static enum tag_types
19797 cp_parser_token_is_class_key (cp_token* token)
19798 {
19799   switch (token->keyword)
19800     {
19801     case RID_CLASS:
19802       return class_type;
19803     case RID_STRUCT:
19804       return record_type;
19805     case RID_UNION:
19806       return union_type;
19807
19808     default:
19809       return none_type;
19810     }
19811 }
19812
19813 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
19814
19815 static void
19816 cp_parser_check_class_key (enum tag_types class_key, tree type)
19817 {
19818   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19819     permerror (input_location, "%qs tag used in naming %q#T",
19820             class_key == union_type ? "union"
19821              : class_key == record_type ? "struct" : "class",
19822              type);
19823 }
19824
19825 /* Issue an error message if DECL is redeclared with different
19826    access than its original declaration [class.access.spec/3].
19827    This applies to nested classes and nested class templates.
19828    [class.mem/1].  */
19829
19830 static void
19831 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19832 {
19833   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19834     return;
19835
19836   if ((TREE_PRIVATE (decl)
19837        != (current_access_specifier == access_private_node))
19838       || (TREE_PROTECTED (decl)
19839           != (current_access_specifier == access_protected_node)))
19840     error_at (location, "%qD redeclared with different access", decl);
19841 }
19842
19843 /* Look for the `template' keyword, as a syntactic disambiguator.
19844    Return TRUE iff it is present, in which case it will be
19845    consumed.  */
19846
19847 static bool
19848 cp_parser_optional_template_keyword (cp_parser *parser)
19849 {
19850   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19851     {
19852       /* The `template' keyword can only be used within templates;
19853          outside templates the parser can always figure out what is a
19854          template and what is not.  */
19855       if (!processing_template_decl)
19856         {
19857           cp_token *token = cp_lexer_peek_token (parser->lexer);
19858           error_at (token->location,
19859                     "%<template%> (as a disambiguator) is only allowed "
19860                     "within templates");
19861           /* If this part of the token stream is rescanned, the same
19862              error message would be generated.  So, we purge the token
19863              from the stream.  */
19864           cp_lexer_purge_token (parser->lexer);
19865           return false;
19866         }
19867       else
19868         {
19869           /* Consume the `template' keyword.  */
19870           cp_lexer_consume_token (parser->lexer);
19871           return true;
19872         }
19873     }
19874
19875   return false;
19876 }
19877
19878 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
19879    set PARSER->SCOPE, and perform other related actions.  */
19880
19881 static void
19882 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19883 {
19884   int i;
19885   struct tree_check *check_value;
19886   deferred_access_check *chk;
19887   VEC (deferred_access_check,gc) *checks;
19888
19889   /* Get the stored value.  */
19890   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19891   /* Perform any access checks that were deferred.  */
19892   checks = check_value->checks;
19893   if (checks)
19894     {
19895       for (i = 0 ;
19896            VEC_iterate (deferred_access_check, checks, i, chk) ;
19897            ++i)
19898         {
19899           perform_or_defer_access_check (chk->binfo,
19900                                          chk->decl,
19901                                          chk->diag_decl);
19902         }
19903     }
19904   /* Set the scope from the stored value.  */
19905   parser->scope = check_value->value;
19906   parser->qualifying_scope = check_value->qualifying_scope;
19907   parser->object_scope = NULL_TREE;
19908 }
19909
19910 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
19911    encounter the end of a block before what we were looking for.  */
19912
19913 static bool
19914 cp_parser_cache_group (cp_parser *parser,
19915                        enum cpp_ttype end,
19916                        unsigned depth)
19917 {
19918   while (true)
19919     {
19920       cp_token *token = cp_lexer_peek_token (parser->lexer);
19921
19922       /* Abort a parenthesized expression if we encounter a semicolon.  */
19923       if ((end == CPP_CLOSE_PAREN || depth == 0)
19924           && token->type == CPP_SEMICOLON)
19925         return true;
19926       /* If we've reached the end of the file, stop.  */
19927       if (token->type == CPP_EOF
19928           || (end != CPP_PRAGMA_EOL
19929               && token->type == CPP_PRAGMA_EOL))
19930         return true;
19931       if (token->type == CPP_CLOSE_BRACE && depth == 0)
19932         /* We've hit the end of an enclosing block, so there's been some
19933            kind of syntax error.  */
19934         return true;
19935
19936       /* Consume the token.  */
19937       cp_lexer_consume_token (parser->lexer);
19938       /* See if it starts a new group.  */
19939       if (token->type == CPP_OPEN_BRACE)
19940         {
19941           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19942           /* In theory this should probably check end == '}', but
19943              cp_parser_save_member_function_body needs it to exit
19944              after either '}' or ')' when called with ')'.  */
19945           if (depth == 0)
19946             return false;
19947         }
19948       else if (token->type == CPP_OPEN_PAREN)
19949         {
19950           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19951           if (depth == 0 && end == CPP_CLOSE_PAREN)
19952             return false;
19953         }
19954       else if (token->type == CPP_PRAGMA)
19955         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19956       else if (token->type == end)
19957         return false;
19958     }
19959 }
19960
19961 /* Begin parsing tentatively.  We always save tokens while parsing
19962    tentatively so that if the tentative parsing fails we can restore the
19963    tokens.  */
19964
19965 static void
19966 cp_parser_parse_tentatively (cp_parser* parser)
19967 {
19968   /* Enter a new parsing context.  */
19969   parser->context = cp_parser_context_new (parser->context);
19970   /* Begin saving tokens.  */
19971   cp_lexer_save_tokens (parser->lexer);
19972   /* In order to avoid repetitive access control error messages,
19973      access checks are queued up until we are no longer parsing
19974      tentatively.  */
19975   push_deferring_access_checks (dk_deferred);
19976 }
19977
19978 /* Commit to the currently active tentative parse.  */
19979
19980 static void
19981 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19982 {
19983   cp_parser_context *context;
19984   cp_lexer *lexer;
19985
19986   /* Mark all of the levels as committed.  */
19987   lexer = parser->lexer;
19988   for (context = parser->context; context->next; context = context->next)
19989     {
19990       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19991         break;
19992       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19993       while (!cp_lexer_saving_tokens (lexer))
19994         lexer = lexer->next;
19995       cp_lexer_commit_tokens (lexer);
19996     }
19997 }
19998
19999 /* Abort the currently active tentative parse.  All consumed tokens
20000    will be rolled back, and no diagnostics will be issued.  */
20001
20002 static void
20003 cp_parser_abort_tentative_parse (cp_parser* parser)
20004 {
20005   cp_parser_simulate_error (parser);
20006   /* Now, pretend that we want to see if the construct was
20007      successfully parsed.  */
20008   cp_parser_parse_definitely (parser);
20009 }
20010
20011 /* Stop parsing tentatively.  If a parse error has occurred, restore the
20012    token stream.  Otherwise, commit to the tokens we have consumed.
20013    Returns true if no error occurred; false otherwise.  */
20014
20015 static bool
20016 cp_parser_parse_definitely (cp_parser* parser)
20017 {
20018   bool error_occurred;
20019   cp_parser_context *context;
20020
20021   /* Remember whether or not an error occurred, since we are about to
20022      destroy that information.  */
20023   error_occurred = cp_parser_error_occurred (parser);
20024   /* Remove the topmost context from the stack.  */
20025   context = parser->context;
20026   parser->context = context->next;
20027   /* If no parse errors occurred, commit to the tentative parse.  */
20028   if (!error_occurred)
20029     {
20030       /* Commit to the tokens read tentatively, unless that was
20031          already done.  */
20032       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20033         cp_lexer_commit_tokens (parser->lexer);
20034
20035       pop_to_parent_deferring_access_checks ();
20036     }
20037   /* Otherwise, if errors occurred, roll back our state so that things
20038      are just as they were before we began the tentative parse.  */
20039   else
20040     {
20041       cp_lexer_rollback_tokens (parser->lexer);
20042       pop_deferring_access_checks ();
20043     }
20044   /* Add the context to the front of the free list.  */
20045   context->next = cp_parser_context_free_list;
20046   cp_parser_context_free_list = context;
20047
20048   return !error_occurred;
20049 }
20050
20051 /* Returns true if we are parsing tentatively and are not committed to
20052    this tentative parse.  */
20053
20054 static bool
20055 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20056 {
20057   return (cp_parser_parsing_tentatively (parser)
20058           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20059 }
20060
20061 /* Returns nonzero iff an error has occurred during the most recent
20062    tentative parse.  */
20063
20064 static bool
20065 cp_parser_error_occurred (cp_parser* parser)
20066 {
20067   return (cp_parser_parsing_tentatively (parser)
20068           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20069 }
20070
20071 /* Returns nonzero if GNU extensions are allowed.  */
20072
20073 static bool
20074 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20075 {
20076   return parser->allow_gnu_extensions_p;
20077 }
20078 \f
20079 /* Objective-C++ Productions */
20080
20081
20082 /* Parse an Objective-C expression, which feeds into a primary-expression
20083    above.
20084
20085    objc-expression:
20086      objc-message-expression
20087      objc-string-literal
20088      objc-encode-expression
20089      objc-protocol-expression
20090      objc-selector-expression
20091
20092   Returns a tree representation of the expression.  */
20093
20094 static tree
20095 cp_parser_objc_expression (cp_parser* parser)
20096 {
20097   /* Try to figure out what kind of declaration is present.  */
20098   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20099
20100   switch (kwd->type)
20101     {
20102     case CPP_OPEN_SQUARE:
20103       return cp_parser_objc_message_expression (parser);
20104
20105     case CPP_OBJC_STRING:
20106       kwd = cp_lexer_consume_token (parser->lexer);
20107       return objc_build_string_object (kwd->u.value);
20108
20109     case CPP_KEYWORD:
20110       switch (kwd->keyword)
20111         {
20112         case RID_AT_ENCODE:
20113           return cp_parser_objc_encode_expression (parser);
20114
20115         case RID_AT_PROTOCOL:
20116           return cp_parser_objc_protocol_expression (parser);
20117
20118         case RID_AT_SELECTOR:
20119           return cp_parser_objc_selector_expression (parser);
20120
20121         default:
20122           break;
20123         }
20124     default:
20125       error_at (kwd->location,
20126                 "misplaced %<@%D%> Objective-C++ construct",
20127                 kwd->u.value);
20128       cp_parser_skip_to_end_of_block_or_statement (parser);
20129     }
20130
20131   return error_mark_node;
20132 }
20133
20134 /* Parse an Objective-C message expression.
20135
20136    objc-message-expression:
20137      [ objc-message-receiver objc-message-args ]
20138
20139    Returns a representation of an Objective-C message.  */
20140
20141 static tree
20142 cp_parser_objc_message_expression (cp_parser* parser)
20143 {
20144   tree receiver, messageargs;
20145
20146   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
20147   receiver = cp_parser_objc_message_receiver (parser);
20148   messageargs = cp_parser_objc_message_args (parser);
20149   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20150
20151   return objc_build_message_expr (build_tree_list (receiver, messageargs));
20152 }
20153
20154 /* Parse an objc-message-receiver.
20155
20156    objc-message-receiver:
20157      expression
20158      simple-type-specifier
20159
20160   Returns a representation of the type or expression.  */
20161
20162 static tree
20163 cp_parser_objc_message_receiver (cp_parser* parser)
20164 {
20165   tree rcv;
20166
20167   /* An Objective-C message receiver may be either (1) a type
20168      or (2) an expression.  */
20169   cp_parser_parse_tentatively (parser);
20170   rcv = cp_parser_expression (parser, false, NULL);
20171
20172   if (cp_parser_parse_definitely (parser))
20173     return rcv;
20174
20175   rcv = cp_parser_simple_type_specifier (parser,
20176                                          /*decl_specs=*/NULL,
20177                                          CP_PARSER_FLAGS_NONE);
20178
20179   return objc_get_class_reference (rcv);
20180 }
20181
20182 /* Parse the arguments and selectors comprising an Objective-C message.
20183
20184    objc-message-args:
20185      objc-selector
20186      objc-selector-args
20187      objc-selector-args , objc-comma-args
20188
20189    objc-selector-args:
20190      objc-selector [opt] : assignment-expression
20191      objc-selector-args objc-selector [opt] : assignment-expression
20192
20193    objc-comma-args:
20194      assignment-expression
20195      objc-comma-args , assignment-expression
20196
20197    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20198    selector arguments and TREE_VALUE containing a list of comma
20199    arguments.  */
20200
20201 static tree
20202 cp_parser_objc_message_args (cp_parser* parser)
20203 {
20204   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20205   bool maybe_unary_selector_p = true;
20206   cp_token *token = cp_lexer_peek_token (parser->lexer);
20207
20208   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20209     {
20210       tree selector = NULL_TREE, arg;
20211
20212       if (token->type != CPP_COLON)
20213         selector = cp_parser_objc_selector (parser);
20214
20215       /* Detect if we have a unary selector.  */
20216       if (maybe_unary_selector_p
20217           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20218         return build_tree_list (selector, NULL_TREE);
20219
20220       maybe_unary_selector_p = false;
20221       cp_parser_require (parser, CPP_COLON, "%<:%>");
20222       arg = cp_parser_assignment_expression (parser, false, NULL);
20223
20224       sel_args
20225         = chainon (sel_args,
20226                    build_tree_list (selector, arg));
20227
20228       token = cp_lexer_peek_token (parser->lexer);
20229     }
20230
20231   /* Handle non-selector arguments, if any. */
20232   while (token->type == CPP_COMMA)
20233     {
20234       tree arg;
20235
20236       cp_lexer_consume_token (parser->lexer);
20237       arg = cp_parser_assignment_expression (parser, false, NULL);
20238
20239       addl_args
20240         = chainon (addl_args,
20241                    build_tree_list (NULL_TREE, arg));
20242
20243       token = cp_lexer_peek_token (parser->lexer);
20244     }
20245
20246   return build_tree_list (sel_args, addl_args);
20247 }
20248
20249 /* Parse an Objective-C encode expression.
20250
20251    objc-encode-expression:
20252      @encode objc-typename
20253
20254    Returns an encoded representation of the type argument.  */
20255
20256 static tree
20257 cp_parser_objc_encode_expression (cp_parser* parser)
20258 {
20259   tree type;
20260   cp_token *token;
20261
20262   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
20263   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20264   token = cp_lexer_peek_token (parser->lexer);
20265   type = complete_type (cp_parser_type_id (parser));
20266   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20267
20268   if (!type)
20269     {
20270       error_at (token->location, 
20271                 "%<@encode%> must specify a type as an argument");
20272       return error_mark_node;
20273     }
20274
20275   return objc_build_encode_expr (type);
20276 }
20277
20278 /* Parse an Objective-C @defs expression.  */
20279
20280 static tree
20281 cp_parser_objc_defs_expression (cp_parser *parser)
20282 {
20283   tree name;
20284
20285   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
20286   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20287   name = cp_parser_identifier (parser);
20288   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20289
20290   return objc_get_class_ivars (name);
20291 }
20292
20293 /* Parse an Objective-C protocol expression.
20294
20295   objc-protocol-expression:
20296     @protocol ( identifier )
20297
20298   Returns a representation of the protocol expression.  */
20299
20300 static tree
20301 cp_parser_objc_protocol_expression (cp_parser* parser)
20302 {
20303   tree proto;
20304
20305   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20306   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20307   proto = cp_parser_identifier (parser);
20308   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20309
20310   return objc_build_protocol_expr (proto);
20311 }
20312
20313 /* Parse an Objective-C selector expression.
20314
20315    objc-selector-expression:
20316      @selector ( objc-method-signature )
20317
20318    objc-method-signature:
20319      objc-selector
20320      objc-selector-seq
20321
20322    objc-selector-seq:
20323      objc-selector :
20324      objc-selector-seq objc-selector :
20325
20326   Returns a representation of the method selector.  */
20327
20328 static tree
20329 cp_parser_objc_selector_expression (cp_parser* parser)
20330 {
20331   tree sel_seq = NULL_TREE;
20332   bool maybe_unary_selector_p = true;
20333   cp_token *token;
20334   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20335
20336   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
20337   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20338   token = cp_lexer_peek_token (parser->lexer);
20339
20340   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20341          || token->type == CPP_SCOPE)
20342     {
20343       tree selector = NULL_TREE;
20344
20345       if (token->type != CPP_COLON
20346           || token->type == CPP_SCOPE)
20347         selector = cp_parser_objc_selector (parser);
20348
20349       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20350           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20351         {
20352           /* Detect if we have a unary selector.  */
20353           if (maybe_unary_selector_p)
20354             {
20355               sel_seq = selector;
20356               goto finish_selector;
20357             }
20358           else
20359             {
20360               cp_parser_error (parser, "expected %<:%>");
20361             }
20362         }
20363       maybe_unary_selector_p = false;
20364       token = cp_lexer_consume_token (parser->lexer);
20365
20366       if (token->type == CPP_SCOPE)
20367         {
20368           sel_seq
20369             = chainon (sel_seq,
20370                        build_tree_list (selector, NULL_TREE));
20371           sel_seq
20372             = chainon (sel_seq,
20373                        build_tree_list (NULL_TREE, NULL_TREE));
20374         }
20375       else
20376         sel_seq
20377           = chainon (sel_seq,
20378                      build_tree_list (selector, NULL_TREE));
20379
20380       token = cp_lexer_peek_token (parser->lexer);
20381     }
20382
20383  finish_selector:
20384   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20385
20386   return objc_build_selector_expr (loc, sel_seq);
20387 }
20388
20389 /* Parse a list of identifiers.
20390
20391    objc-identifier-list:
20392      identifier
20393      objc-identifier-list , identifier
20394
20395    Returns a TREE_LIST of identifier nodes.  */
20396
20397 static tree
20398 cp_parser_objc_identifier_list (cp_parser* parser)
20399 {
20400   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20401   cp_token *sep = cp_lexer_peek_token (parser->lexer);
20402
20403   while (sep->type == CPP_COMMA)
20404     {
20405       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20406       list = chainon (list,
20407                       build_tree_list (NULL_TREE,
20408                                        cp_parser_identifier (parser)));
20409       sep = cp_lexer_peek_token (parser->lexer);
20410     }
20411
20412   return list;
20413 }
20414
20415 /* Parse an Objective-C alias declaration.
20416
20417    objc-alias-declaration:
20418      @compatibility_alias identifier identifier ;
20419
20420    This function registers the alias mapping with the Objective-C front end.
20421    It returns nothing.  */
20422
20423 static void
20424 cp_parser_objc_alias_declaration (cp_parser* parser)
20425 {
20426   tree alias, orig;
20427
20428   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
20429   alias = cp_parser_identifier (parser);
20430   orig = cp_parser_identifier (parser);
20431   objc_declare_alias (alias, orig);
20432   cp_parser_consume_semicolon_at_end_of_statement (parser);
20433 }
20434
20435 /* Parse an Objective-C class forward-declaration.
20436
20437    objc-class-declaration:
20438      @class objc-identifier-list ;
20439
20440    The function registers the forward declarations with the Objective-C
20441    front end.  It returns nothing.  */
20442
20443 static void
20444 cp_parser_objc_class_declaration (cp_parser* parser)
20445 {
20446   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
20447   objc_declare_class (cp_parser_objc_identifier_list (parser));
20448   cp_parser_consume_semicolon_at_end_of_statement (parser);
20449 }
20450
20451 /* Parse a list of Objective-C protocol references.
20452
20453    objc-protocol-refs-opt:
20454      objc-protocol-refs [opt]
20455
20456    objc-protocol-refs:
20457      < objc-identifier-list >
20458
20459    Returns a TREE_LIST of identifiers, if any.  */
20460
20461 static tree
20462 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20463 {
20464   tree protorefs = NULL_TREE;
20465
20466   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20467     {
20468       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
20469       protorefs = cp_parser_objc_identifier_list (parser);
20470       cp_parser_require (parser, CPP_GREATER, "%<>%>");
20471     }
20472
20473   return protorefs;
20474 }
20475
20476 /* Parse a Objective-C visibility specification.  */
20477
20478 static void
20479 cp_parser_objc_visibility_spec (cp_parser* parser)
20480 {
20481   cp_token *vis = cp_lexer_peek_token (parser->lexer);
20482
20483   switch (vis->keyword)
20484     {
20485     case RID_AT_PRIVATE:
20486       objc_set_visibility (2);
20487       break;
20488     case RID_AT_PROTECTED:
20489       objc_set_visibility (0);
20490       break;
20491     case RID_AT_PUBLIC:
20492       objc_set_visibility (1);
20493       break;
20494     default:
20495       return;
20496     }
20497
20498   /* Eat '@private'/'@protected'/'@public'.  */
20499   cp_lexer_consume_token (parser->lexer);
20500 }
20501
20502 /* Parse an Objective-C method type.  */
20503
20504 static void
20505 cp_parser_objc_method_type (cp_parser* parser)
20506 {
20507   objc_set_method_type
20508    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20509     ? PLUS_EXPR
20510     : MINUS_EXPR);
20511 }
20512
20513 /* Parse an Objective-C protocol qualifier.  */
20514
20515 static tree
20516 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20517 {
20518   tree quals = NULL_TREE, node;
20519   cp_token *token = cp_lexer_peek_token (parser->lexer);
20520
20521   node = token->u.value;
20522
20523   while (node && TREE_CODE (node) == IDENTIFIER_NODE
20524          && (node == ridpointers [(int) RID_IN]
20525              || node == ridpointers [(int) RID_OUT]
20526              || node == ridpointers [(int) RID_INOUT]
20527              || node == ridpointers [(int) RID_BYCOPY]
20528              || node == ridpointers [(int) RID_BYREF]
20529              || node == ridpointers [(int) RID_ONEWAY]))
20530     {
20531       quals = tree_cons (NULL_TREE, node, quals);
20532       cp_lexer_consume_token (parser->lexer);
20533       token = cp_lexer_peek_token (parser->lexer);
20534       node = token->u.value;
20535     }
20536
20537   return quals;
20538 }
20539
20540 /* Parse an Objective-C typename.  */
20541
20542 static tree
20543 cp_parser_objc_typename (cp_parser* parser)
20544 {
20545   tree type_name = NULL_TREE;
20546
20547   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20548     {
20549       tree proto_quals, cp_type = NULL_TREE;
20550
20551       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20552       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20553
20554       /* An ObjC type name may consist of just protocol qualifiers, in which
20555          case the type shall default to 'id'.  */
20556       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20557         cp_type = cp_parser_type_id (parser);
20558
20559       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20560       type_name = build_tree_list (proto_quals, cp_type);
20561     }
20562
20563   return type_name;
20564 }
20565
20566 /* Check to see if TYPE refers to an Objective-C selector name.  */
20567
20568 static bool
20569 cp_parser_objc_selector_p (enum cpp_ttype type)
20570 {
20571   return (type == CPP_NAME || type == CPP_KEYWORD
20572           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20573           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20574           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20575           || type == CPP_XOR || type == CPP_XOR_EQ);
20576 }
20577
20578 /* Parse an Objective-C selector.  */
20579
20580 static tree
20581 cp_parser_objc_selector (cp_parser* parser)
20582 {
20583   cp_token *token = cp_lexer_consume_token (parser->lexer);
20584
20585   if (!cp_parser_objc_selector_p (token->type))
20586     {
20587       error_at (token->location, "invalid Objective-C++ selector name");
20588       return error_mark_node;
20589     }
20590
20591   /* C++ operator names are allowed to appear in ObjC selectors.  */
20592   switch (token->type)
20593     {
20594     case CPP_AND_AND: return get_identifier ("and");
20595     case CPP_AND_EQ: return get_identifier ("and_eq");
20596     case CPP_AND: return get_identifier ("bitand");
20597     case CPP_OR: return get_identifier ("bitor");
20598     case CPP_COMPL: return get_identifier ("compl");
20599     case CPP_NOT: return get_identifier ("not");
20600     case CPP_NOT_EQ: return get_identifier ("not_eq");
20601     case CPP_OR_OR: return get_identifier ("or");
20602     case CPP_OR_EQ: return get_identifier ("or_eq");
20603     case CPP_XOR: return get_identifier ("xor");
20604     case CPP_XOR_EQ: return get_identifier ("xor_eq");
20605     default: return token->u.value;
20606     }
20607 }
20608
20609 /* Parse an Objective-C params list.  */
20610
20611 static tree
20612 cp_parser_objc_method_keyword_params (cp_parser* parser)
20613 {
20614   tree params = NULL_TREE;
20615   bool maybe_unary_selector_p = true;
20616   cp_token *token = cp_lexer_peek_token (parser->lexer);
20617
20618   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20619     {
20620       tree selector = NULL_TREE, type_name, identifier;
20621
20622       if (token->type != CPP_COLON)
20623         selector = cp_parser_objc_selector (parser);
20624
20625       /* Detect if we have a unary selector.  */
20626       if (maybe_unary_selector_p
20627           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20628         return selector;
20629
20630       maybe_unary_selector_p = false;
20631       cp_parser_require (parser, CPP_COLON, "%<:%>");
20632       type_name = cp_parser_objc_typename (parser);
20633       identifier = cp_parser_identifier (parser);
20634
20635       params
20636         = chainon (params,
20637                    objc_build_keyword_decl (selector,
20638                                             type_name,
20639                                             identifier));
20640
20641       token = cp_lexer_peek_token (parser->lexer);
20642     }
20643
20644   return params;
20645 }
20646
20647 /* Parse the non-keyword Objective-C params.  */
20648
20649 static tree
20650 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20651 {
20652   tree params = make_node (TREE_LIST);
20653   cp_token *token = cp_lexer_peek_token (parser->lexer);
20654   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
20655
20656   while (token->type == CPP_COMMA)
20657     {
20658       cp_parameter_declarator *parmdecl;
20659       tree parm;
20660
20661       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20662       token = cp_lexer_peek_token (parser->lexer);
20663
20664       if (token->type == CPP_ELLIPSIS)
20665         {
20666           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
20667           *ellipsisp = true;
20668           break;
20669         }
20670
20671       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20672       parm = grokdeclarator (parmdecl->declarator,
20673                              &parmdecl->decl_specifiers,
20674                              PARM, /*initialized=*/0,
20675                              /*attrlist=*/NULL);
20676
20677       chainon (params, build_tree_list (NULL_TREE, parm));
20678       token = cp_lexer_peek_token (parser->lexer);
20679     }
20680
20681   return params;
20682 }
20683
20684 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
20685
20686 static void
20687 cp_parser_objc_interstitial_code (cp_parser* parser)
20688 {
20689   cp_token *token = cp_lexer_peek_token (parser->lexer);
20690
20691   /* If the next token is `extern' and the following token is a string
20692      literal, then we have a linkage specification.  */
20693   if (token->keyword == RID_EXTERN
20694       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20695     cp_parser_linkage_specification (parser);
20696   /* Handle #pragma, if any.  */
20697   else if (token->type == CPP_PRAGMA)
20698     cp_parser_pragma (parser, pragma_external);
20699   /* Allow stray semicolons.  */
20700   else if (token->type == CPP_SEMICOLON)
20701     cp_lexer_consume_token (parser->lexer);
20702   /* Finally, try to parse a block-declaration, or a function-definition.  */
20703   else
20704     cp_parser_block_declaration (parser, /*statement_p=*/false);
20705 }
20706
20707 /* Parse a method signature.  */
20708
20709 static tree
20710 cp_parser_objc_method_signature (cp_parser* parser)
20711 {
20712   tree rettype, kwdparms, optparms;
20713   bool ellipsis = false;
20714
20715   cp_parser_objc_method_type (parser);
20716   rettype = cp_parser_objc_typename (parser);
20717   kwdparms = cp_parser_objc_method_keyword_params (parser);
20718   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20719
20720   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20721 }
20722
20723 /* Pars an Objective-C method prototype list.  */
20724
20725 static void
20726 cp_parser_objc_method_prototype_list (cp_parser* parser)
20727 {
20728   cp_token *token = cp_lexer_peek_token (parser->lexer);
20729
20730   while (token->keyword != RID_AT_END)
20731     {
20732       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20733         {
20734           objc_add_method_declaration
20735            (cp_parser_objc_method_signature (parser));
20736           cp_parser_consume_semicolon_at_end_of_statement (parser);
20737         }
20738       else
20739         /* Allow for interspersed non-ObjC++ code.  */
20740         cp_parser_objc_interstitial_code (parser);
20741
20742       token = cp_lexer_peek_token (parser->lexer);
20743     }
20744
20745   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20746   objc_finish_interface ();
20747 }
20748
20749 /* Parse an Objective-C method definition list.  */
20750
20751 static void
20752 cp_parser_objc_method_definition_list (cp_parser* parser)
20753 {
20754   cp_token *token = cp_lexer_peek_token (parser->lexer);
20755
20756   while (token->keyword != RID_AT_END)
20757     {
20758       tree meth;
20759
20760       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20761         {
20762           push_deferring_access_checks (dk_deferred);
20763           objc_start_method_definition
20764            (cp_parser_objc_method_signature (parser));
20765
20766           /* For historical reasons, we accept an optional semicolon.  */
20767           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20768             cp_lexer_consume_token (parser->lexer);
20769
20770           perform_deferred_access_checks ();
20771           stop_deferring_access_checks ();
20772           meth = cp_parser_function_definition_after_declarator (parser,
20773                                                                  false);
20774           pop_deferring_access_checks ();
20775           objc_finish_method_definition (meth);
20776         }
20777       else
20778         /* Allow for interspersed non-ObjC++ code.  */
20779         cp_parser_objc_interstitial_code (parser);
20780
20781       token = cp_lexer_peek_token (parser->lexer);
20782     }
20783
20784   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20785   objc_finish_implementation ();
20786 }
20787
20788 /* Parse Objective-C ivars.  */
20789
20790 static void
20791 cp_parser_objc_class_ivars (cp_parser* parser)
20792 {
20793   cp_token *token = cp_lexer_peek_token (parser->lexer);
20794
20795   if (token->type != CPP_OPEN_BRACE)
20796     return;     /* No ivars specified.  */
20797
20798   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
20799   token = cp_lexer_peek_token (parser->lexer);
20800
20801   while (token->type != CPP_CLOSE_BRACE)
20802     {
20803       cp_decl_specifier_seq declspecs;
20804       int decl_class_or_enum_p;
20805       tree prefix_attributes;
20806
20807       cp_parser_objc_visibility_spec (parser);
20808
20809       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20810         break;
20811
20812       cp_parser_decl_specifier_seq (parser,
20813                                     CP_PARSER_FLAGS_OPTIONAL,
20814                                     &declspecs,
20815                                     &decl_class_or_enum_p);
20816       prefix_attributes = declspecs.attributes;
20817       declspecs.attributes = NULL_TREE;
20818
20819       /* Keep going until we hit the `;' at the end of the
20820          declaration.  */
20821       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20822         {
20823           tree width = NULL_TREE, attributes, first_attribute, decl;
20824           cp_declarator *declarator = NULL;
20825           int ctor_dtor_or_conv_p;
20826
20827           /* Check for a (possibly unnamed) bitfield declaration.  */
20828           token = cp_lexer_peek_token (parser->lexer);
20829           if (token->type == CPP_COLON)
20830             goto eat_colon;
20831
20832           if (token->type == CPP_NAME
20833               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20834                   == CPP_COLON))
20835             {
20836               /* Get the name of the bitfield.  */
20837               declarator = make_id_declarator (NULL_TREE,
20838                                                cp_parser_identifier (parser),
20839                                                sfk_none);
20840
20841              eat_colon:
20842               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20843               /* Get the width of the bitfield.  */
20844               width
20845                 = cp_parser_constant_expression (parser,
20846                                                  /*allow_non_constant=*/false,
20847                                                  NULL);
20848             }
20849           else
20850             {
20851               /* Parse the declarator.  */
20852               declarator
20853                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20854                                         &ctor_dtor_or_conv_p,
20855                                         /*parenthesized_p=*/NULL,
20856                                         /*member_p=*/false);
20857             }
20858
20859           /* Look for attributes that apply to the ivar.  */
20860           attributes = cp_parser_attributes_opt (parser);
20861           /* Remember which attributes are prefix attributes and
20862              which are not.  */
20863           first_attribute = attributes;
20864           /* Combine the attributes.  */
20865           attributes = chainon (prefix_attributes, attributes);
20866
20867           if (width)
20868               /* Create the bitfield declaration.  */
20869               decl = grokbitfield (declarator, &declspecs,
20870                                    width,
20871                                    attributes);
20872           else
20873             decl = grokfield (declarator, &declspecs,
20874                               NULL_TREE, /*init_const_expr_p=*/false,
20875                               NULL_TREE, attributes);
20876
20877           /* Add the instance variable.  */
20878           objc_add_instance_variable (decl);
20879
20880           /* Reset PREFIX_ATTRIBUTES.  */
20881           while (attributes && TREE_CHAIN (attributes) != first_attribute)
20882             attributes = TREE_CHAIN (attributes);
20883           if (attributes)
20884             TREE_CHAIN (attributes) = NULL_TREE;
20885
20886           token = cp_lexer_peek_token (parser->lexer);
20887
20888           if (token->type == CPP_COMMA)
20889             {
20890               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20891               continue;
20892             }
20893           break;
20894         }
20895
20896       cp_parser_consume_semicolon_at_end_of_statement (parser);
20897       token = cp_lexer_peek_token (parser->lexer);
20898     }
20899
20900   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
20901   /* For historical reasons, we accept an optional semicolon.  */
20902   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20903     cp_lexer_consume_token (parser->lexer);
20904 }
20905
20906 /* Parse an Objective-C protocol declaration.  */
20907
20908 static void
20909 cp_parser_objc_protocol_declaration (cp_parser* parser)
20910 {
20911   tree proto, protorefs;
20912   cp_token *tok;
20913
20914   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20915   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20916     {
20917       tok = cp_lexer_peek_token (parser->lexer);
20918       error_at (tok->location, "identifier expected after %<@protocol%>");
20919       goto finish;
20920     }
20921
20922   /* See if we have a forward declaration or a definition.  */
20923   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20924
20925   /* Try a forward declaration first.  */
20926   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20927     {
20928       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20929      finish:
20930       cp_parser_consume_semicolon_at_end_of_statement (parser);
20931     }
20932
20933   /* Ok, we got a full-fledged definition (or at least should).  */
20934   else
20935     {
20936       proto = cp_parser_identifier (parser);
20937       protorefs = cp_parser_objc_protocol_refs_opt (parser);
20938       objc_start_protocol (proto, protorefs);
20939       cp_parser_objc_method_prototype_list (parser);
20940     }
20941 }
20942
20943 /* Parse an Objective-C superclass or category.  */
20944
20945 static void
20946 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20947                                                           tree *categ)
20948 {
20949   cp_token *next = cp_lexer_peek_token (parser->lexer);
20950
20951   *super = *categ = NULL_TREE;
20952   if (next->type == CPP_COLON)
20953     {
20954       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20955       *super = cp_parser_identifier (parser);
20956     }
20957   else if (next->type == CPP_OPEN_PAREN)
20958     {
20959       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20960       *categ = cp_parser_identifier (parser);
20961       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20962     }
20963 }
20964
20965 /* Parse an Objective-C class interface.  */
20966
20967 static void
20968 cp_parser_objc_class_interface (cp_parser* parser)
20969 {
20970   tree name, super, categ, protos;
20971
20972   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20973   name = cp_parser_identifier (parser);
20974   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20975   protos = cp_parser_objc_protocol_refs_opt (parser);
20976
20977   /* We have either a class or a category on our hands.  */
20978   if (categ)
20979     objc_start_category_interface (name, categ, protos);
20980   else
20981     {
20982       objc_start_class_interface (name, super, protos);
20983       /* Handle instance variable declarations, if any.  */
20984       cp_parser_objc_class_ivars (parser);
20985       objc_continue_interface ();
20986     }
20987
20988   cp_parser_objc_method_prototype_list (parser);
20989 }
20990
20991 /* Parse an Objective-C class implementation.  */
20992
20993 static void
20994 cp_parser_objc_class_implementation (cp_parser* parser)
20995 {
20996   tree name, super, categ;
20997
20998   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20999   name = cp_parser_identifier (parser);
21000   cp_parser_objc_superclass_or_category (parser, &super, &categ);
21001
21002   /* We have either a class or a category on our hands.  */
21003   if (categ)
21004     objc_start_category_implementation (name, categ);
21005   else
21006     {
21007       objc_start_class_implementation (name, super);
21008       /* Handle instance variable declarations, if any.  */
21009       cp_parser_objc_class_ivars (parser);
21010       objc_continue_implementation ();
21011     }
21012
21013   cp_parser_objc_method_definition_list (parser);
21014 }
21015
21016 /* Consume the @end token and finish off the implementation.  */
21017
21018 static void
21019 cp_parser_objc_end_implementation (cp_parser* parser)
21020 {
21021   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
21022   objc_finish_implementation ();
21023 }
21024
21025 /* Parse an Objective-C declaration.  */
21026
21027 static void
21028 cp_parser_objc_declaration (cp_parser* parser)
21029 {
21030   /* Try to figure out what kind of declaration is present.  */
21031   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21032
21033   switch (kwd->keyword)
21034     {
21035     case RID_AT_ALIAS:
21036       cp_parser_objc_alias_declaration (parser);
21037       break;
21038     case RID_AT_CLASS:
21039       cp_parser_objc_class_declaration (parser);
21040       break;
21041     case RID_AT_PROTOCOL:
21042       cp_parser_objc_protocol_declaration (parser);
21043       break;
21044     case RID_AT_INTERFACE:
21045       cp_parser_objc_class_interface (parser);
21046       break;
21047     case RID_AT_IMPLEMENTATION:
21048       cp_parser_objc_class_implementation (parser);
21049       break;
21050     case RID_AT_END:
21051       cp_parser_objc_end_implementation (parser);
21052       break;
21053     default:
21054       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21055                 kwd->u.value);
21056       cp_parser_skip_to_end_of_block_or_statement (parser);
21057     }
21058 }
21059
21060 /* Parse an Objective-C try-catch-finally statement.
21061
21062    objc-try-catch-finally-stmt:
21063      @try compound-statement objc-catch-clause-seq [opt]
21064        objc-finally-clause [opt]
21065
21066    objc-catch-clause-seq:
21067      objc-catch-clause objc-catch-clause-seq [opt]
21068
21069    objc-catch-clause:
21070      @catch ( exception-declaration ) compound-statement
21071
21072    objc-finally-clause
21073      @finally compound-statement
21074
21075    Returns NULL_TREE.  */
21076
21077 static tree
21078 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21079   location_t location;
21080   tree stmt;
21081
21082   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
21083   location = cp_lexer_peek_token (parser->lexer)->location;
21084   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21085      node, lest it get absorbed into the surrounding block.  */
21086   stmt = push_stmt_list ();
21087   cp_parser_compound_statement (parser, NULL, false);
21088   objc_begin_try_stmt (location, pop_stmt_list (stmt));
21089
21090   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21091     {
21092       cp_parameter_declarator *parmdecl;
21093       tree parm;
21094
21095       cp_lexer_consume_token (parser->lexer);
21096       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21097       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21098       parm = grokdeclarator (parmdecl->declarator,
21099                              &parmdecl->decl_specifiers,
21100                              PARM, /*initialized=*/0,
21101                              /*attrlist=*/NULL);
21102       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21103       objc_begin_catch_clause (parm);
21104       cp_parser_compound_statement (parser, NULL, false);
21105       objc_finish_catch_clause ();
21106     }
21107
21108   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21109     {
21110       cp_lexer_consume_token (parser->lexer);
21111       location = cp_lexer_peek_token (parser->lexer)->location;
21112       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21113          node, lest it get absorbed into the surrounding block.  */
21114       stmt = push_stmt_list ();
21115       cp_parser_compound_statement (parser, NULL, false);
21116       objc_build_finally_clause (location, pop_stmt_list (stmt));
21117     }
21118
21119   return objc_finish_try_stmt ();
21120 }
21121
21122 /* Parse an Objective-C synchronized statement.
21123
21124    objc-synchronized-stmt:
21125      @synchronized ( expression ) compound-statement
21126
21127    Returns NULL_TREE.  */
21128
21129 static tree
21130 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21131   location_t location;
21132   tree lock, stmt;
21133
21134   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21135
21136   location = cp_lexer_peek_token (parser->lexer)->location;
21137   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21138   lock = cp_parser_expression (parser, false, NULL);
21139   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21140
21141   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21142      node, lest it get absorbed into the surrounding block.  */
21143   stmt = push_stmt_list ();
21144   cp_parser_compound_statement (parser, NULL, false);
21145
21146   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21147 }
21148
21149 /* Parse an Objective-C throw statement.
21150
21151    objc-throw-stmt:
21152      @throw assignment-expression [opt] ;
21153
21154    Returns a constructed '@throw' statement.  */
21155
21156 static tree
21157 cp_parser_objc_throw_statement (cp_parser *parser) {
21158   tree expr = NULL_TREE;
21159   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21160
21161   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21162
21163   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21164     expr = cp_parser_assignment_expression (parser, false, NULL);
21165
21166   cp_parser_consume_semicolon_at_end_of_statement (parser);
21167
21168   return objc_build_throw_stmt (loc, expr);
21169 }
21170
21171 /* Parse an Objective-C statement.  */
21172
21173 static tree
21174 cp_parser_objc_statement (cp_parser * parser) {
21175   /* Try to figure out what kind of declaration is present.  */
21176   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21177
21178   switch (kwd->keyword)
21179     {
21180     case RID_AT_TRY:
21181       return cp_parser_objc_try_catch_finally_statement (parser);
21182     case RID_AT_SYNCHRONIZED:
21183       return cp_parser_objc_synchronized_statement (parser);
21184     case RID_AT_THROW:
21185       return cp_parser_objc_throw_statement (parser);
21186     default:
21187       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21188                kwd->u.value);
21189       cp_parser_skip_to_end_of_block_or_statement (parser);
21190     }
21191
21192   return error_mark_node;
21193 }
21194 \f
21195 /* OpenMP 2.5 parsing routines.  */
21196
21197 /* Returns name of the next clause.
21198    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21199    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
21200    returned and the token is consumed.  */
21201
21202 static pragma_omp_clause
21203 cp_parser_omp_clause_name (cp_parser *parser)
21204 {
21205   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21206
21207   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21208     result = PRAGMA_OMP_CLAUSE_IF;
21209   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21210     result = PRAGMA_OMP_CLAUSE_DEFAULT;
21211   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21212     result = PRAGMA_OMP_CLAUSE_PRIVATE;
21213   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21214     {
21215       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21216       const char *p = IDENTIFIER_POINTER (id);
21217
21218       switch (p[0])
21219         {
21220         case 'c':
21221           if (!strcmp ("collapse", p))
21222             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21223           else if (!strcmp ("copyin", p))
21224             result = PRAGMA_OMP_CLAUSE_COPYIN;
21225           else if (!strcmp ("copyprivate", p))
21226             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21227           break;
21228         case 'f':
21229           if (!strcmp ("firstprivate", p))
21230             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21231           break;
21232         case 'l':
21233           if (!strcmp ("lastprivate", p))
21234             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21235           break;
21236         case 'n':
21237           if (!strcmp ("nowait", p))
21238             result = PRAGMA_OMP_CLAUSE_NOWAIT;
21239           else if (!strcmp ("num_threads", p))
21240             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21241           break;
21242         case 'o':
21243           if (!strcmp ("ordered", p))
21244             result = PRAGMA_OMP_CLAUSE_ORDERED;
21245           break;
21246         case 'r':
21247           if (!strcmp ("reduction", p))
21248             result = PRAGMA_OMP_CLAUSE_REDUCTION;
21249           break;
21250         case 's':
21251           if (!strcmp ("schedule", p))
21252             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21253           else if (!strcmp ("shared", p))
21254             result = PRAGMA_OMP_CLAUSE_SHARED;
21255           break;
21256         case 'u':
21257           if (!strcmp ("untied", p))
21258             result = PRAGMA_OMP_CLAUSE_UNTIED;
21259           break;
21260         }
21261     }
21262
21263   if (result != PRAGMA_OMP_CLAUSE_NONE)
21264     cp_lexer_consume_token (parser->lexer);
21265
21266   return result;
21267 }
21268
21269 /* Validate that a clause of the given type does not already exist.  */
21270
21271 static void
21272 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21273                            const char *name, location_t location)
21274 {
21275   tree c;
21276
21277   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21278     if (OMP_CLAUSE_CODE (c) == code)
21279       {
21280         error_at (location, "too many %qs clauses", name);
21281         break;
21282       }
21283 }
21284
21285 /* OpenMP 2.5:
21286    variable-list:
21287      identifier
21288      variable-list , identifier
21289
21290    In addition, we match a closing parenthesis.  An opening parenthesis
21291    will have been consumed by the caller.
21292
21293    If KIND is nonzero, create the appropriate node and install the decl
21294    in OMP_CLAUSE_DECL and add the node to the head of the list.
21295
21296    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21297    return the list created.  */
21298
21299 static tree
21300 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21301                                 tree list)
21302 {
21303   cp_token *token;
21304   while (1)
21305     {
21306       tree name, decl;
21307
21308       token = cp_lexer_peek_token (parser->lexer);
21309       name = cp_parser_id_expression (parser, /*template_p=*/false,
21310                                       /*check_dependency_p=*/true,
21311                                       /*template_p=*/NULL,
21312                                       /*declarator_p=*/false,
21313                                       /*optional_p=*/false);
21314       if (name == error_mark_node)
21315         goto skip_comma;
21316
21317       decl = cp_parser_lookup_name_simple (parser, name, token->location);
21318       if (decl == error_mark_node)
21319         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21320       else if (kind != 0)
21321         {
21322           tree u = build_omp_clause (token->location, kind);
21323           OMP_CLAUSE_DECL (u) = decl;
21324           OMP_CLAUSE_CHAIN (u) = list;
21325           list = u;
21326         }
21327       else
21328         list = tree_cons (decl, NULL_TREE, list);
21329
21330     get_comma:
21331       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21332         break;
21333       cp_lexer_consume_token (parser->lexer);
21334     }
21335
21336   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21337     {
21338       int ending;
21339
21340       /* Try to resync to an unnested comma.  Copied from
21341          cp_parser_parenthesized_expression_list.  */
21342     skip_comma:
21343       ending = cp_parser_skip_to_closing_parenthesis (parser,
21344                                                       /*recovering=*/true,
21345                                                       /*or_comma=*/true,
21346                                                       /*consume_paren=*/true);
21347       if (ending < 0)
21348         goto get_comma;
21349     }
21350
21351   return list;
21352 }
21353
21354 /* Similarly, but expect leading and trailing parenthesis.  This is a very
21355    common case for omp clauses.  */
21356
21357 static tree
21358 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21359 {
21360   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21361     return cp_parser_omp_var_list_no_open (parser, kind, list);
21362   return list;
21363 }
21364
21365 /* OpenMP 3.0:
21366    collapse ( constant-expression ) */
21367
21368 static tree
21369 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21370 {
21371   tree c, num;
21372   location_t loc;
21373   HOST_WIDE_INT n;
21374
21375   loc = cp_lexer_peek_token (parser->lexer)->location;
21376   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21377     return list;
21378
21379   num = cp_parser_constant_expression (parser, false, NULL);
21380
21381   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21382     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21383                                            /*or_comma=*/false,
21384                                            /*consume_paren=*/true);
21385
21386   if (num == error_mark_node)
21387     return list;
21388   num = fold_non_dependent_expr (num);
21389   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21390       || !host_integerp (num, 0)
21391       || (n = tree_low_cst (num, 0)) <= 0
21392       || (int) n != n)
21393     {
21394       error_at (loc, "collapse argument needs positive constant integer expression");
21395       return list;
21396     }
21397
21398   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21399   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21400   OMP_CLAUSE_CHAIN (c) = list;
21401   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21402
21403   return c;
21404 }
21405
21406 /* OpenMP 2.5:
21407    default ( shared | none ) */
21408
21409 static tree
21410 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21411 {
21412   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21413   tree c;
21414
21415   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21416     return list;
21417   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21418     {
21419       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21420       const char *p = IDENTIFIER_POINTER (id);
21421
21422       switch (p[0])
21423         {
21424         case 'n':
21425           if (strcmp ("none", p) != 0)
21426             goto invalid_kind;
21427           kind = OMP_CLAUSE_DEFAULT_NONE;
21428           break;
21429
21430         case 's':
21431           if (strcmp ("shared", p) != 0)
21432             goto invalid_kind;
21433           kind = OMP_CLAUSE_DEFAULT_SHARED;
21434           break;
21435
21436         default:
21437           goto invalid_kind;
21438         }
21439
21440       cp_lexer_consume_token (parser->lexer);
21441     }
21442   else
21443     {
21444     invalid_kind:
21445       cp_parser_error (parser, "expected %<none%> or %<shared%>");
21446     }
21447
21448   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21449     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21450                                            /*or_comma=*/false,
21451                                            /*consume_paren=*/true);
21452
21453   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21454     return list;
21455
21456   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21457   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21458   OMP_CLAUSE_CHAIN (c) = list;
21459   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21460
21461   return c;
21462 }
21463
21464 /* OpenMP 2.5:
21465    if ( expression ) */
21466
21467 static tree
21468 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21469 {
21470   tree t, c;
21471
21472   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21473     return list;
21474
21475   t = cp_parser_condition (parser);
21476
21477   if (t == error_mark_node
21478       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21479     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21480                                            /*or_comma=*/false,
21481                                            /*consume_paren=*/true);
21482
21483   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21484
21485   c = build_omp_clause (location, OMP_CLAUSE_IF);
21486   OMP_CLAUSE_IF_EXPR (c) = t;
21487   OMP_CLAUSE_CHAIN (c) = list;
21488
21489   return c;
21490 }
21491
21492 /* OpenMP 2.5:
21493    nowait */
21494
21495 static tree
21496 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21497                              tree list, location_t location)
21498 {
21499   tree c;
21500
21501   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21502
21503   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21504   OMP_CLAUSE_CHAIN (c) = list;
21505   return c;
21506 }
21507
21508 /* OpenMP 2.5:
21509    num_threads ( expression ) */
21510
21511 static tree
21512 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21513                                   location_t location)
21514 {
21515   tree t, c;
21516
21517   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21518     return list;
21519
21520   t = cp_parser_expression (parser, false, NULL);
21521
21522   if (t == error_mark_node
21523       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21524     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21525                                            /*or_comma=*/false,
21526                                            /*consume_paren=*/true);
21527
21528   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21529                              "num_threads", location);
21530
21531   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21532   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21533   OMP_CLAUSE_CHAIN (c) = list;
21534
21535   return c;
21536 }
21537
21538 /* OpenMP 2.5:
21539    ordered */
21540
21541 static tree
21542 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21543                               tree list, location_t location)
21544 {
21545   tree c;
21546
21547   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21548                              "ordered", location);
21549
21550   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21551   OMP_CLAUSE_CHAIN (c) = list;
21552   return c;
21553 }
21554
21555 /* OpenMP 2.5:
21556    reduction ( reduction-operator : variable-list )
21557
21558    reduction-operator:
21559      One of: + * - & ^ | && || */
21560
21561 static tree
21562 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21563 {
21564   enum tree_code code;
21565   tree nlist, c;
21566
21567   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21568     return list;
21569
21570   switch (cp_lexer_peek_token (parser->lexer)->type)
21571     {
21572     case CPP_PLUS:
21573       code = PLUS_EXPR;
21574       break;
21575     case CPP_MULT:
21576       code = MULT_EXPR;
21577       break;
21578     case CPP_MINUS:
21579       code = MINUS_EXPR;
21580       break;
21581     case CPP_AND:
21582       code = BIT_AND_EXPR;
21583       break;
21584     case CPP_XOR:
21585       code = BIT_XOR_EXPR;
21586       break;
21587     case CPP_OR:
21588       code = BIT_IOR_EXPR;
21589       break;
21590     case CPP_AND_AND:
21591       code = TRUTH_ANDIF_EXPR;
21592       break;
21593     case CPP_OR_OR:
21594       code = TRUTH_ORIF_EXPR;
21595       break;
21596     default:
21597       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21598                                "%<|%>, %<&&%>, or %<||%>");
21599     resync_fail:
21600       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21601                                              /*or_comma=*/false,
21602                                              /*consume_paren=*/true);
21603       return list;
21604     }
21605   cp_lexer_consume_token (parser->lexer);
21606
21607   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21608     goto resync_fail;
21609
21610   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21611   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21612     OMP_CLAUSE_REDUCTION_CODE (c) = code;
21613
21614   return nlist;
21615 }
21616
21617 /* OpenMP 2.5:
21618    schedule ( schedule-kind )
21619    schedule ( schedule-kind , expression )
21620
21621    schedule-kind:
21622      static | dynamic | guided | runtime | auto  */
21623
21624 static tree
21625 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21626 {
21627   tree c, t;
21628
21629   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21630     return list;
21631
21632   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21633
21634   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21635     {
21636       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21637       const char *p = IDENTIFIER_POINTER (id);
21638
21639       switch (p[0])
21640         {
21641         case 'd':
21642           if (strcmp ("dynamic", p) != 0)
21643             goto invalid_kind;
21644           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21645           break;
21646
21647         case 'g':
21648           if (strcmp ("guided", p) != 0)
21649             goto invalid_kind;
21650           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21651           break;
21652
21653         case 'r':
21654           if (strcmp ("runtime", p) != 0)
21655             goto invalid_kind;
21656           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21657           break;
21658
21659         default:
21660           goto invalid_kind;
21661         }
21662     }
21663   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21664     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21665   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21666     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21667   else
21668     goto invalid_kind;
21669   cp_lexer_consume_token (parser->lexer);
21670
21671   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21672     {
21673       cp_token *token;
21674       cp_lexer_consume_token (parser->lexer);
21675
21676       token = cp_lexer_peek_token (parser->lexer);
21677       t = cp_parser_assignment_expression (parser, false, NULL);
21678
21679       if (t == error_mark_node)
21680         goto resync_fail;
21681       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21682         error_at (token->location, "schedule %<runtime%> does not take "
21683                   "a %<chunk_size%> parameter");
21684       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21685         error_at (token->location, "schedule %<auto%> does not take "
21686                   "a %<chunk_size%> parameter");
21687       else
21688         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21689
21690       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21691         goto resync_fail;
21692     }
21693   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21694     goto resync_fail;
21695
21696   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21697   OMP_CLAUSE_CHAIN (c) = list;
21698   return c;
21699
21700  invalid_kind:
21701   cp_parser_error (parser, "invalid schedule kind");
21702  resync_fail:
21703   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21704                                          /*or_comma=*/false,
21705                                          /*consume_paren=*/true);
21706   return list;
21707 }
21708
21709 /* OpenMP 3.0:
21710    untied */
21711
21712 static tree
21713 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21714                              tree list, location_t location)
21715 {
21716   tree c;
21717
21718   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21719
21720   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21721   OMP_CLAUSE_CHAIN (c) = list;
21722   return c;
21723 }
21724
21725 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
21726    is a bitmask in MASK.  Return the list of clauses found; the result
21727    of clause default goes in *pdefault.  */
21728
21729 static tree
21730 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21731                            const char *where, cp_token *pragma_tok)
21732 {
21733   tree clauses = NULL;
21734   bool first = true;
21735   cp_token *token = NULL;
21736
21737   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21738     {
21739       pragma_omp_clause c_kind;
21740       const char *c_name;
21741       tree prev = clauses;
21742
21743       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21744         cp_lexer_consume_token (parser->lexer);
21745
21746       token = cp_lexer_peek_token (parser->lexer);
21747       c_kind = cp_parser_omp_clause_name (parser);
21748       first = false;
21749
21750       switch (c_kind)
21751         {
21752         case PRAGMA_OMP_CLAUSE_COLLAPSE:
21753           clauses = cp_parser_omp_clause_collapse (parser, clauses,
21754                                                    token->location);
21755           c_name = "collapse";
21756           break;
21757         case PRAGMA_OMP_CLAUSE_COPYIN:
21758           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21759           c_name = "copyin";
21760           break;
21761         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21762           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21763                                             clauses);
21764           c_name = "copyprivate";
21765           break;
21766         case PRAGMA_OMP_CLAUSE_DEFAULT:
21767           clauses = cp_parser_omp_clause_default (parser, clauses,
21768                                                   token->location);
21769           c_name = "default";
21770           break;
21771         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21772           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21773                                             clauses);
21774           c_name = "firstprivate";
21775           break;
21776         case PRAGMA_OMP_CLAUSE_IF:
21777           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21778           c_name = "if";
21779           break;
21780         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21781           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21782                                             clauses);
21783           c_name = "lastprivate";
21784           break;
21785         case PRAGMA_OMP_CLAUSE_NOWAIT:
21786           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21787           c_name = "nowait";
21788           break;
21789         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21790           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21791                                                       token->location);
21792           c_name = "num_threads";
21793           break;
21794         case PRAGMA_OMP_CLAUSE_ORDERED:
21795           clauses = cp_parser_omp_clause_ordered (parser, clauses,
21796                                                   token->location);
21797           c_name = "ordered";
21798           break;
21799         case PRAGMA_OMP_CLAUSE_PRIVATE:
21800           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21801                                             clauses);
21802           c_name = "private";
21803           break;
21804         case PRAGMA_OMP_CLAUSE_REDUCTION:
21805           clauses = cp_parser_omp_clause_reduction (parser, clauses);
21806           c_name = "reduction";
21807           break;
21808         case PRAGMA_OMP_CLAUSE_SCHEDULE:
21809           clauses = cp_parser_omp_clause_schedule (parser, clauses,
21810                                                    token->location);
21811           c_name = "schedule";
21812           break;
21813         case PRAGMA_OMP_CLAUSE_SHARED:
21814           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21815                                             clauses);
21816           c_name = "shared";
21817           break;
21818         case PRAGMA_OMP_CLAUSE_UNTIED:
21819           clauses = cp_parser_omp_clause_untied (parser, clauses,
21820                                                  token->location);
21821           c_name = "nowait";
21822           break;
21823         default:
21824           cp_parser_error (parser, "expected %<#pragma omp%> clause");
21825           goto saw_error;
21826         }
21827
21828       if (((mask >> c_kind) & 1) == 0)
21829         {
21830           /* Remove the invalid clause(s) from the list to avoid
21831              confusing the rest of the compiler.  */
21832           clauses = prev;
21833           error_at (token->location, "%qs is not valid for %qs", c_name, where);
21834         }
21835     }
21836  saw_error:
21837   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21838   return finish_omp_clauses (clauses);
21839 }
21840
21841 /* OpenMP 2.5:
21842    structured-block:
21843      statement
21844
21845    In practice, we're also interested in adding the statement to an
21846    outer node.  So it is convenient if we work around the fact that
21847    cp_parser_statement calls add_stmt.  */
21848
21849 static unsigned
21850 cp_parser_begin_omp_structured_block (cp_parser *parser)
21851 {
21852   unsigned save = parser->in_statement;
21853
21854   /* Only move the values to IN_OMP_BLOCK if they weren't false.
21855      This preserves the "not within loop or switch" style error messages
21856      for nonsense cases like
21857         void foo() {
21858         #pragma omp single
21859           break;
21860         }
21861   */
21862   if (parser->in_statement)
21863     parser->in_statement = IN_OMP_BLOCK;
21864
21865   return save;
21866 }
21867
21868 static void
21869 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21870 {
21871   parser->in_statement = save;
21872 }
21873
21874 static tree
21875 cp_parser_omp_structured_block (cp_parser *parser)
21876 {
21877   tree stmt = begin_omp_structured_block ();
21878   unsigned int save = cp_parser_begin_omp_structured_block (parser);
21879
21880   cp_parser_statement (parser, NULL_TREE, false, NULL);
21881
21882   cp_parser_end_omp_structured_block (parser, save);
21883   return finish_omp_structured_block (stmt);
21884 }
21885
21886 /* OpenMP 2.5:
21887    # pragma omp atomic new-line
21888      expression-stmt
21889
21890    expression-stmt:
21891      x binop= expr | x++ | ++x | x-- | --x
21892    binop:
21893      +, *, -, /, &, ^, |, <<, >>
21894
21895   where x is an lvalue expression with scalar type.  */
21896
21897 static void
21898 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21899 {
21900   tree lhs, rhs;
21901   enum tree_code code;
21902
21903   cp_parser_require_pragma_eol (parser, pragma_tok);
21904
21905   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21906                                     /*cast_p=*/false, NULL);
21907   switch (TREE_CODE (lhs))
21908     {
21909     case ERROR_MARK:
21910       goto saw_error;
21911
21912     case PREINCREMENT_EXPR:
21913     case POSTINCREMENT_EXPR:
21914       lhs = TREE_OPERAND (lhs, 0);
21915       code = PLUS_EXPR;
21916       rhs = integer_one_node;
21917       break;
21918
21919     case PREDECREMENT_EXPR:
21920     case POSTDECREMENT_EXPR:
21921       lhs = TREE_OPERAND (lhs, 0);
21922       code = MINUS_EXPR;
21923       rhs = integer_one_node;
21924       break;
21925
21926     default:
21927       switch (cp_lexer_peek_token (parser->lexer)->type)
21928         {
21929         case CPP_MULT_EQ:
21930           code = MULT_EXPR;
21931           break;
21932         case CPP_DIV_EQ:
21933           code = TRUNC_DIV_EXPR;
21934           break;
21935         case CPP_PLUS_EQ:
21936           code = PLUS_EXPR;
21937           break;
21938         case CPP_MINUS_EQ:
21939           code = MINUS_EXPR;
21940           break;
21941         case CPP_LSHIFT_EQ:
21942           code = LSHIFT_EXPR;
21943           break;
21944         case CPP_RSHIFT_EQ:
21945           code = RSHIFT_EXPR;
21946           break;
21947         case CPP_AND_EQ:
21948           code = BIT_AND_EXPR;
21949           break;
21950         case CPP_OR_EQ:
21951           code = BIT_IOR_EXPR;
21952           break;
21953         case CPP_XOR_EQ:
21954           code = BIT_XOR_EXPR;
21955           break;
21956         default:
21957           cp_parser_error (parser,
21958                            "invalid operator for %<#pragma omp atomic%>");
21959           goto saw_error;
21960         }
21961       cp_lexer_consume_token (parser->lexer);
21962
21963       rhs = cp_parser_expression (parser, false, NULL);
21964       if (rhs == error_mark_node)
21965         goto saw_error;
21966       break;
21967     }
21968   finish_omp_atomic (code, lhs, rhs);
21969   cp_parser_consume_semicolon_at_end_of_statement (parser);
21970   return;
21971
21972  saw_error:
21973   cp_parser_skip_to_end_of_block_or_statement (parser);
21974 }
21975
21976
21977 /* OpenMP 2.5:
21978    # pragma omp barrier new-line  */
21979
21980 static void
21981 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21982 {
21983   cp_parser_require_pragma_eol (parser, pragma_tok);
21984   finish_omp_barrier ();
21985 }
21986
21987 /* OpenMP 2.5:
21988    # pragma omp critical [(name)] new-line
21989      structured-block  */
21990
21991 static tree
21992 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21993 {
21994   tree stmt, name = NULL;
21995
21996   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21997     {
21998       cp_lexer_consume_token (parser->lexer);
21999
22000       name = cp_parser_identifier (parser);
22001
22002       if (name == error_mark_node
22003           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22004         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22005                                                /*or_comma=*/false,
22006                                                /*consume_paren=*/true);
22007       if (name == error_mark_node)
22008         name = NULL;
22009     }
22010   cp_parser_require_pragma_eol (parser, pragma_tok);
22011
22012   stmt = cp_parser_omp_structured_block (parser);
22013   return c_finish_omp_critical (input_location, stmt, name);
22014 }
22015
22016 /* OpenMP 2.5:
22017    # pragma omp flush flush-vars[opt] new-line
22018
22019    flush-vars:
22020      ( variable-list ) */
22021
22022 static void
22023 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22024 {
22025   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22026     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22027   cp_parser_require_pragma_eol (parser, pragma_tok);
22028
22029   finish_omp_flush ();
22030 }
22031
22032 /* Helper function, to parse omp for increment expression.  */
22033
22034 static tree
22035 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22036 {
22037   tree cond = cp_parser_binary_expression (parser, false, true,
22038                                            PREC_NOT_OPERATOR, NULL);
22039   bool overloaded_p;
22040
22041   if (cond == error_mark_node
22042       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22043     {
22044       cp_parser_skip_to_end_of_statement (parser);
22045       return error_mark_node;
22046     }
22047
22048   switch (TREE_CODE (cond))
22049     {
22050     case GT_EXPR:
22051     case GE_EXPR:
22052     case LT_EXPR:
22053     case LE_EXPR:
22054       break;
22055     default:
22056       return error_mark_node;
22057     }
22058
22059   /* If decl is an iterator, preserve LHS and RHS of the relational
22060      expr until finish_omp_for.  */
22061   if (decl
22062       && (type_dependent_expression_p (decl)
22063           || CLASS_TYPE_P (TREE_TYPE (decl))))
22064     return cond;
22065
22066   return build_x_binary_op (TREE_CODE (cond),
22067                             TREE_OPERAND (cond, 0), ERROR_MARK,
22068                             TREE_OPERAND (cond, 1), ERROR_MARK,
22069                             &overloaded_p, tf_warning_or_error);
22070 }
22071
22072 /* Helper function, to parse omp for increment expression.  */
22073
22074 static tree
22075 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22076 {
22077   cp_token *token = cp_lexer_peek_token (parser->lexer);
22078   enum tree_code op;
22079   tree lhs, rhs;
22080   cp_id_kind idk;
22081   bool decl_first;
22082
22083   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22084     {
22085       op = (token->type == CPP_PLUS_PLUS
22086             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22087       cp_lexer_consume_token (parser->lexer);
22088       lhs = cp_parser_cast_expression (parser, false, false, NULL);
22089       if (lhs != decl)
22090         return error_mark_node;
22091       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22092     }
22093
22094   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22095   if (lhs != decl)
22096     return error_mark_node;
22097
22098   token = cp_lexer_peek_token (parser->lexer);
22099   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22100     {
22101       op = (token->type == CPP_PLUS_PLUS
22102             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22103       cp_lexer_consume_token (parser->lexer);
22104       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22105     }
22106
22107   op = cp_parser_assignment_operator_opt (parser);
22108   if (op == ERROR_MARK)
22109     return error_mark_node;
22110
22111   if (op != NOP_EXPR)
22112     {
22113       rhs = cp_parser_assignment_expression (parser, false, NULL);
22114       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22115       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22116     }
22117
22118   lhs = cp_parser_binary_expression (parser, false, false,
22119                                      PREC_ADDITIVE_EXPRESSION, NULL);
22120   token = cp_lexer_peek_token (parser->lexer);
22121   decl_first = lhs == decl;
22122   if (decl_first)
22123     lhs = NULL_TREE;
22124   if (token->type != CPP_PLUS
22125       && token->type != CPP_MINUS)
22126     return error_mark_node;
22127
22128   do
22129     {
22130       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22131       cp_lexer_consume_token (parser->lexer);
22132       rhs = cp_parser_binary_expression (parser, false, false,
22133                                          PREC_ADDITIVE_EXPRESSION, NULL);
22134       token = cp_lexer_peek_token (parser->lexer);
22135       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22136         {
22137           if (lhs == NULL_TREE)
22138             {
22139               if (op == PLUS_EXPR)
22140                 lhs = rhs;
22141               else
22142                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22143             }
22144           else
22145             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22146                                      NULL, tf_warning_or_error);
22147         }
22148     }
22149   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22150
22151   if (!decl_first)
22152     {
22153       if (rhs != decl || op == MINUS_EXPR)
22154         return error_mark_node;
22155       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22156     }
22157   else
22158     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22159
22160   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22161 }
22162
22163 /* Parse the restricted form of the for statement allowed by OpenMP.  */
22164
22165 static tree
22166 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22167 {
22168   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22169   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22170   tree this_pre_body, cl;
22171   location_t loc_first;
22172   bool collapse_err = false;
22173   int i, collapse = 1, nbraces = 0;
22174
22175   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22176     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22177       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22178
22179   gcc_assert (collapse >= 1);
22180
22181   declv = make_tree_vec (collapse);
22182   initv = make_tree_vec (collapse);
22183   condv = make_tree_vec (collapse);
22184   incrv = make_tree_vec (collapse);
22185
22186   loc_first = cp_lexer_peek_token (parser->lexer)->location;
22187
22188   for (i = 0; i < collapse; i++)
22189     {
22190       int bracecount = 0;
22191       bool add_private_clause = false;
22192       location_t loc;
22193
22194       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22195         {
22196           cp_parser_error (parser, "for statement expected");
22197           return NULL;
22198         }
22199       loc = cp_lexer_consume_token (parser->lexer)->location;
22200
22201       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22202         return NULL;
22203
22204       init = decl = real_decl = NULL;
22205       this_pre_body = push_stmt_list ();
22206       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22207         {
22208           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22209
22210              init-expr:
22211                        var = lb
22212                        integer-type var = lb
22213                        random-access-iterator-type var = lb
22214                        pointer-type var = lb
22215           */
22216           cp_decl_specifier_seq type_specifiers;
22217
22218           /* First, try to parse as an initialized declaration.  See
22219              cp_parser_condition, from whence the bulk of this is copied.  */
22220
22221           cp_parser_parse_tentatively (parser);
22222           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22223                                         /*is_trailing_return=*/false,
22224                                         &type_specifiers);
22225           if (cp_parser_parse_definitely (parser))
22226             {
22227               /* If parsing a type specifier seq succeeded, then this
22228                  MUST be a initialized declaration.  */
22229               tree asm_specification, attributes;
22230               cp_declarator *declarator;
22231
22232               declarator = cp_parser_declarator (parser,
22233                                                  CP_PARSER_DECLARATOR_NAMED,
22234                                                  /*ctor_dtor_or_conv_p=*/NULL,
22235                                                  /*parenthesized_p=*/NULL,
22236                                                  /*member_p=*/false);
22237               attributes = cp_parser_attributes_opt (parser);
22238               asm_specification = cp_parser_asm_specification_opt (parser);
22239
22240               if (declarator == cp_error_declarator) 
22241                 cp_parser_skip_to_end_of_statement (parser);
22242
22243               else 
22244                 {
22245                   tree pushed_scope, auto_node;
22246
22247                   decl = start_decl (declarator, &type_specifiers,
22248                                      SD_INITIALIZED, attributes,
22249                                      /*prefix_attributes=*/NULL_TREE,
22250                                      &pushed_scope);
22251
22252                   auto_node = type_uses_auto (TREE_TYPE (decl));
22253                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22254                     {
22255                       if (cp_lexer_next_token_is (parser->lexer, 
22256                                                   CPP_OPEN_PAREN))
22257                         error ("parenthesized initialization is not allowed in "
22258                                "OpenMP %<for%> loop");
22259                       else
22260                         /* Trigger an error.  */
22261                         cp_parser_require (parser, CPP_EQ, "%<=%>");
22262
22263                       init = error_mark_node;
22264                       cp_parser_skip_to_end_of_statement (parser);
22265                     }
22266                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
22267                            || type_dependent_expression_p (decl)
22268                            || auto_node)
22269                     {
22270                       bool is_direct_init, is_non_constant_init;
22271
22272                       init = cp_parser_initializer (parser,
22273                                                     &is_direct_init,
22274                                                     &is_non_constant_init);
22275
22276                       if (auto_node && describable_type (init))
22277                         {
22278                           TREE_TYPE (decl)
22279                             = do_auto_deduction (TREE_TYPE (decl), init,
22280                                                  auto_node);
22281
22282                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
22283                               && !type_dependent_expression_p (decl))
22284                             goto non_class;
22285                         }
22286                       
22287                       cp_finish_decl (decl, init, !is_non_constant_init,
22288                                       asm_specification,
22289                                       LOOKUP_ONLYCONVERTING);
22290                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
22291                         {
22292                           for_block
22293                             = tree_cons (NULL, this_pre_body, for_block);
22294                           init = NULL_TREE;
22295                         }
22296                       else
22297                         init = pop_stmt_list (this_pre_body);
22298                       this_pre_body = NULL_TREE;
22299                     }
22300                   else
22301                     {
22302                       /* Consume '='.  */
22303                       cp_lexer_consume_token (parser->lexer);
22304                       init = cp_parser_assignment_expression (parser, false, NULL);
22305
22306                     non_class:
22307                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22308                         init = error_mark_node;
22309                       else
22310                         cp_finish_decl (decl, NULL_TREE,
22311                                         /*init_const_expr_p=*/false,
22312                                         asm_specification,
22313                                         LOOKUP_ONLYCONVERTING);
22314                     }
22315
22316                   if (pushed_scope)
22317                     pop_scope (pushed_scope);
22318                 }
22319             }
22320           else 
22321             {
22322               cp_id_kind idk;
22323               /* If parsing a type specifier sequence failed, then
22324                  this MUST be a simple expression.  */
22325               cp_parser_parse_tentatively (parser);
22326               decl = cp_parser_primary_expression (parser, false, false,
22327                                                    false, &idk);
22328               if (!cp_parser_error_occurred (parser)
22329                   && decl
22330                   && DECL_P (decl)
22331                   && CLASS_TYPE_P (TREE_TYPE (decl)))
22332                 {
22333                   tree rhs;
22334
22335                   cp_parser_parse_definitely (parser);
22336                   cp_parser_require (parser, CPP_EQ, "%<=%>");
22337                   rhs = cp_parser_assignment_expression (parser, false, NULL);
22338                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22339                                                          rhs,
22340                                                          tf_warning_or_error));
22341                   add_private_clause = true;
22342                 }
22343               else
22344                 {
22345                   decl = NULL;
22346                   cp_parser_abort_tentative_parse (parser);
22347                   init = cp_parser_expression (parser, false, NULL);
22348                   if (init)
22349                     {
22350                       if (TREE_CODE (init) == MODIFY_EXPR
22351                           || TREE_CODE (init) == MODOP_EXPR)
22352                         real_decl = TREE_OPERAND (init, 0);
22353                     }
22354                 }
22355             }
22356         }
22357       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22358       if (this_pre_body)
22359         {
22360           this_pre_body = pop_stmt_list (this_pre_body);
22361           if (pre_body)
22362             {
22363               tree t = pre_body;
22364               pre_body = push_stmt_list ();
22365               add_stmt (t);
22366               add_stmt (this_pre_body);
22367               pre_body = pop_stmt_list (pre_body);
22368             }
22369           else
22370             pre_body = this_pre_body;
22371         }
22372
22373       if (decl)
22374         real_decl = decl;
22375       if (par_clauses != NULL && real_decl != NULL_TREE)
22376         {
22377           tree *c;
22378           for (c = par_clauses; *c ; )
22379             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22380                 && OMP_CLAUSE_DECL (*c) == real_decl)
22381               {
22382                 error_at (loc, "iteration variable %qD"
22383                           " should not be firstprivate", real_decl);
22384                 *c = OMP_CLAUSE_CHAIN (*c);
22385               }
22386             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22387                      && OMP_CLAUSE_DECL (*c) == real_decl)
22388               {
22389                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22390                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
22391                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22392                 OMP_CLAUSE_DECL (l) = real_decl;
22393                 OMP_CLAUSE_CHAIN (l) = clauses;
22394                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22395                 clauses = l;
22396                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22397                 CP_OMP_CLAUSE_INFO (*c) = NULL;
22398                 add_private_clause = false;
22399               }
22400             else
22401               {
22402                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22403                     && OMP_CLAUSE_DECL (*c) == real_decl)
22404                   add_private_clause = false;
22405                 c = &OMP_CLAUSE_CHAIN (*c);
22406               }
22407         }
22408
22409       if (add_private_clause)
22410         {
22411           tree c;
22412           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22413             {
22414               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22415                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22416                   && OMP_CLAUSE_DECL (c) == decl)
22417                 break;
22418               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22419                        && OMP_CLAUSE_DECL (c) == decl)
22420                 error_at (loc, "iteration variable %qD "
22421                           "should not be firstprivate",
22422                           decl);
22423               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22424                        && OMP_CLAUSE_DECL (c) == decl)
22425                 error_at (loc, "iteration variable %qD should not be reduction",
22426                           decl);
22427             }
22428           if (c == NULL)
22429             {
22430               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22431               OMP_CLAUSE_DECL (c) = decl;
22432               c = finish_omp_clauses (c);
22433               if (c)
22434                 {
22435                   OMP_CLAUSE_CHAIN (c) = clauses;
22436                   clauses = c;
22437                 }
22438             }
22439         }
22440
22441       cond = NULL;
22442       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22443         cond = cp_parser_omp_for_cond (parser, decl);
22444       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22445
22446       incr = NULL;
22447       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22448         {
22449           /* If decl is an iterator, preserve the operator on decl
22450              until finish_omp_for.  */
22451           if (decl
22452               && (type_dependent_expression_p (decl)
22453                   || CLASS_TYPE_P (TREE_TYPE (decl))))
22454             incr = cp_parser_omp_for_incr (parser, decl);
22455           else
22456             incr = cp_parser_expression (parser, false, NULL);
22457         }
22458
22459       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22460         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22461                                                /*or_comma=*/false,
22462                                                /*consume_paren=*/true);
22463
22464       TREE_VEC_ELT (declv, i) = decl;
22465       TREE_VEC_ELT (initv, i) = init;
22466       TREE_VEC_ELT (condv, i) = cond;
22467       TREE_VEC_ELT (incrv, i) = incr;
22468
22469       if (i == collapse - 1)
22470         break;
22471
22472       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22473          in between the collapsed for loops to be still considered perfectly
22474          nested.  Hopefully the final version clarifies this.
22475          For now handle (multiple) {'s and empty statements.  */
22476       cp_parser_parse_tentatively (parser);
22477       do
22478         {
22479           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22480             break;
22481           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22482             {
22483               cp_lexer_consume_token (parser->lexer);
22484               bracecount++;
22485             }
22486           else if (bracecount
22487                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22488             cp_lexer_consume_token (parser->lexer);
22489           else
22490             {
22491               loc = cp_lexer_peek_token (parser->lexer)->location;
22492               error_at (loc, "not enough collapsed for loops");
22493               collapse_err = true;
22494               cp_parser_abort_tentative_parse (parser);
22495               declv = NULL_TREE;
22496               break;
22497             }
22498         }
22499       while (1);
22500
22501       if (declv)
22502         {
22503           cp_parser_parse_definitely (parser);
22504           nbraces += bracecount;
22505         }
22506     }
22507
22508   /* Note that we saved the original contents of this flag when we entered
22509      the structured block, and so we don't need to re-save it here.  */
22510   parser->in_statement = IN_OMP_FOR;
22511
22512   /* Note that the grammar doesn't call for a structured block here,
22513      though the loop as a whole is a structured block.  */
22514   body = push_stmt_list ();
22515   cp_parser_statement (parser, NULL_TREE, false, NULL);
22516   body = pop_stmt_list (body);
22517
22518   if (declv == NULL_TREE)
22519     ret = NULL_TREE;
22520   else
22521     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22522                           pre_body, clauses);
22523
22524   while (nbraces)
22525     {
22526       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22527         {
22528           cp_lexer_consume_token (parser->lexer);
22529           nbraces--;
22530         }
22531       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22532         cp_lexer_consume_token (parser->lexer);
22533       else
22534         {
22535           if (!collapse_err)
22536             {
22537               error_at (cp_lexer_peek_token (parser->lexer)->location,
22538                         "collapsed loops not perfectly nested");
22539             }
22540           collapse_err = true;
22541           cp_parser_statement_seq_opt (parser, NULL);
22542           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22543             break;
22544         }
22545     }
22546
22547   while (for_block)
22548     {
22549       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22550       for_block = TREE_CHAIN (for_block);
22551     }
22552
22553   return ret;
22554 }
22555
22556 /* OpenMP 2.5:
22557    #pragma omp for for-clause[optseq] new-line
22558      for-loop  */
22559
22560 #define OMP_FOR_CLAUSE_MASK                             \
22561         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22562         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22563         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22564         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22565         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
22566         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
22567         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
22568         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22569
22570 static tree
22571 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22572 {
22573   tree clauses, sb, ret;
22574   unsigned int save;
22575
22576   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22577                                        "#pragma omp for", pragma_tok);
22578
22579   sb = begin_omp_structured_block ();
22580   save = cp_parser_begin_omp_structured_block (parser);
22581
22582   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22583
22584   cp_parser_end_omp_structured_block (parser, save);
22585   add_stmt (finish_omp_structured_block (sb));
22586
22587   return ret;
22588 }
22589
22590 /* OpenMP 2.5:
22591    # pragma omp master new-line
22592      structured-block  */
22593
22594 static tree
22595 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22596 {
22597   cp_parser_require_pragma_eol (parser, pragma_tok);
22598   return c_finish_omp_master (input_location,
22599                               cp_parser_omp_structured_block (parser));
22600 }
22601
22602 /* OpenMP 2.5:
22603    # pragma omp ordered new-line
22604      structured-block  */
22605
22606 static tree
22607 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22608 {
22609   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22610   cp_parser_require_pragma_eol (parser, pragma_tok);
22611   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22612 }
22613
22614 /* OpenMP 2.5:
22615
22616    section-scope:
22617      { section-sequence }
22618
22619    section-sequence:
22620      section-directive[opt] structured-block
22621      section-sequence section-directive structured-block  */
22622
22623 static tree
22624 cp_parser_omp_sections_scope (cp_parser *parser)
22625 {
22626   tree stmt, substmt;
22627   bool error_suppress = false;
22628   cp_token *tok;
22629
22630   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22631     return NULL_TREE;
22632
22633   stmt = push_stmt_list ();
22634
22635   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22636     {
22637       unsigned save;
22638
22639       substmt = begin_omp_structured_block ();
22640       save = cp_parser_begin_omp_structured_block (parser);
22641
22642       while (1)
22643         {
22644           cp_parser_statement (parser, NULL_TREE, false, NULL);
22645
22646           tok = cp_lexer_peek_token (parser->lexer);
22647           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22648             break;
22649           if (tok->type == CPP_CLOSE_BRACE)
22650             break;
22651           if (tok->type == CPP_EOF)
22652             break;
22653         }
22654
22655       cp_parser_end_omp_structured_block (parser, save);
22656       substmt = finish_omp_structured_block (substmt);
22657       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22658       add_stmt (substmt);
22659     }
22660
22661   while (1)
22662     {
22663       tok = cp_lexer_peek_token (parser->lexer);
22664       if (tok->type == CPP_CLOSE_BRACE)
22665         break;
22666       if (tok->type == CPP_EOF)
22667         break;
22668
22669       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22670         {
22671           cp_lexer_consume_token (parser->lexer);
22672           cp_parser_require_pragma_eol (parser, tok);
22673           error_suppress = false;
22674         }
22675       else if (!error_suppress)
22676         {
22677           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22678           error_suppress = true;
22679         }
22680
22681       substmt = cp_parser_omp_structured_block (parser);
22682       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22683       add_stmt (substmt);
22684     }
22685   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22686
22687   substmt = pop_stmt_list (stmt);
22688
22689   stmt = make_node (OMP_SECTIONS);
22690   TREE_TYPE (stmt) = void_type_node;
22691   OMP_SECTIONS_BODY (stmt) = substmt;
22692
22693   add_stmt (stmt);
22694   return stmt;
22695 }
22696
22697 /* OpenMP 2.5:
22698    # pragma omp sections sections-clause[optseq] newline
22699      sections-scope  */
22700
22701 #define OMP_SECTIONS_CLAUSE_MASK                        \
22702         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22703         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22704         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22705         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22706         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22707
22708 static tree
22709 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22710 {
22711   tree clauses, ret;
22712
22713   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22714                                        "#pragma omp sections", pragma_tok);
22715
22716   ret = cp_parser_omp_sections_scope (parser);
22717   if (ret)
22718     OMP_SECTIONS_CLAUSES (ret) = clauses;
22719
22720   return ret;
22721 }
22722
22723 /* OpenMP 2.5:
22724    # pragma parallel parallel-clause new-line
22725    # pragma parallel for parallel-for-clause new-line
22726    # pragma parallel sections parallel-sections-clause new-line  */
22727
22728 #define OMP_PARALLEL_CLAUSE_MASK                        \
22729         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22730         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22731         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22732         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22733         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
22734         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
22735         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22736         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22737
22738 static tree
22739 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22740 {
22741   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22742   const char *p_name = "#pragma omp parallel";
22743   tree stmt, clauses, par_clause, ws_clause, block;
22744   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22745   unsigned int save;
22746   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22747
22748   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22749     {
22750       cp_lexer_consume_token (parser->lexer);
22751       p_kind = PRAGMA_OMP_PARALLEL_FOR;
22752       p_name = "#pragma omp parallel for";
22753       mask |= OMP_FOR_CLAUSE_MASK;
22754       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22755     }
22756   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22757     {
22758       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22759       const char *p = IDENTIFIER_POINTER (id);
22760       if (strcmp (p, "sections") == 0)
22761         {
22762           cp_lexer_consume_token (parser->lexer);
22763           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22764           p_name = "#pragma omp parallel sections";
22765           mask |= OMP_SECTIONS_CLAUSE_MASK;
22766           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22767         }
22768     }
22769
22770   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22771   block = begin_omp_parallel ();
22772   save = cp_parser_begin_omp_structured_block (parser);
22773
22774   switch (p_kind)
22775     {
22776     case PRAGMA_OMP_PARALLEL:
22777       cp_parser_statement (parser, NULL_TREE, false, NULL);
22778       par_clause = clauses;
22779       break;
22780
22781     case PRAGMA_OMP_PARALLEL_FOR:
22782       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22783       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22784       break;
22785
22786     case PRAGMA_OMP_PARALLEL_SECTIONS:
22787       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22788       stmt = cp_parser_omp_sections_scope (parser);
22789       if (stmt)
22790         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22791       break;
22792
22793     default:
22794       gcc_unreachable ();
22795     }
22796
22797   cp_parser_end_omp_structured_block (parser, save);
22798   stmt = finish_omp_parallel (par_clause, block);
22799   if (p_kind != PRAGMA_OMP_PARALLEL)
22800     OMP_PARALLEL_COMBINED (stmt) = 1;
22801   return stmt;
22802 }
22803
22804 /* OpenMP 2.5:
22805    # pragma omp single single-clause[optseq] new-line
22806      structured-block  */
22807
22808 #define OMP_SINGLE_CLAUSE_MASK                          \
22809         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22810         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22811         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
22812         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22813
22814 static tree
22815 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22816 {
22817   tree stmt = make_node (OMP_SINGLE);
22818   TREE_TYPE (stmt) = void_type_node;
22819
22820   OMP_SINGLE_CLAUSES (stmt)
22821     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22822                                  "#pragma omp single", pragma_tok);
22823   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22824
22825   return add_stmt (stmt);
22826 }
22827
22828 /* OpenMP 3.0:
22829    # pragma omp task task-clause[optseq] new-line
22830      structured-block  */
22831
22832 #define OMP_TASK_CLAUSE_MASK                            \
22833         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22834         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
22835         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22836         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22837         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22838         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22839
22840 static tree
22841 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22842 {
22843   tree clauses, block;
22844   unsigned int save;
22845
22846   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22847                                        "#pragma omp task", pragma_tok);
22848   block = begin_omp_task ();
22849   save = cp_parser_begin_omp_structured_block (parser);
22850   cp_parser_statement (parser, NULL_TREE, false, NULL);
22851   cp_parser_end_omp_structured_block (parser, save);
22852   return finish_omp_task (clauses, block);
22853 }
22854
22855 /* OpenMP 3.0:
22856    # pragma omp taskwait new-line  */
22857
22858 static void
22859 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22860 {
22861   cp_parser_require_pragma_eol (parser, pragma_tok);
22862   finish_omp_taskwait ();
22863 }
22864
22865 /* OpenMP 2.5:
22866    # pragma omp threadprivate (variable-list) */
22867
22868 static void
22869 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22870 {
22871   tree vars;
22872
22873   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22874   cp_parser_require_pragma_eol (parser, pragma_tok);
22875
22876   finish_omp_threadprivate (vars);
22877 }
22878
22879 /* Main entry point to OpenMP statement pragmas.  */
22880
22881 static void
22882 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22883 {
22884   tree stmt;
22885
22886   switch (pragma_tok->pragma_kind)
22887     {
22888     case PRAGMA_OMP_ATOMIC:
22889       cp_parser_omp_atomic (parser, pragma_tok);
22890       return;
22891     case PRAGMA_OMP_CRITICAL:
22892       stmt = cp_parser_omp_critical (parser, pragma_tok);
22893       break;
22894     case PRAGMA_OMP_FOR:
22895       stmt = cp_parser_omp_for (parser, pragma_tok);
22896       break;
22897     case PRAGMA_OMP_MASTER:
22898       stmt = cp_parser_omp_master (parser, pragma_tok);
22899       break;
22900     case PRAGMA_OMP_ORDERED:
22901       stmt = cp_parser_omp_ordered (parser, pragma_tok);
22902       break;
22903     case PRAGMA_OMP_PARALLEL:
22904       stmt = cp_parser_omp_parallel (parser, pragma_tok);
22905       break;
22906     case PRAGMA_OMP_SECTIONS:
22907       stmt = cp_parser_omp_sections (parser, pragma_tok);
22908       break;
22909     case PRAGMA_OMP_SINGLE:
22910       stmt = cp_parser_omp_single (parser, pragma_tok);
22911       break;
22912     case PRAGMA_OMP_TASK:
22913       stmt = cp_parser_omp_task (parser, pragma_tok);
22914       break;
22915     default:
22916       gcc_unreachable ();
22917     }
22918
22919   if (stmt)
22920     SET_EXPR_LOCATION (stmt, pragma_tok->location);
22921 }
22922 \f
22923 /* The parser.  */
22924
22925 static GTY (()) cp_parser *the_parser;
22926
22927 \f
22928 /* Special handling for the first token or line in the file.  The first
22929    thing in the file might be #pragma GCC pch_preprocess, which loads a
22930    PCH file, which is a GC collection point.  So we need to handle this
22931    first pragma without benefit of an existing lexer structure.
22932
22933    Always returns one token to the caller in *FIRST_TOKEN.  This is
22934    either the true first token of the file, or the first token after
22935    the initial pragma.  */
22936
22937 static void
22938 cp_parser_initial_pragma (cp_token *first_token)
22939 {
22940   tree name = NULL;
22941
22942   cp_lexer_get_preprocessor_token (NULL, first_token);
22943   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22944     return;
22945
22946   cp_lexer_get_preprocessor_token (NULL, first_token);
22947   if (first_token->type == CPP_STRING)
22948     {
22949       name = first_token->u.value;
22950
22951       cp_lexer_get_preprocessor_token (NULL, first_token);
22952       if (first_token->type != CPP_PRAGMA_EOL)
22953         error_at (first_token->location,
22954                   "junk at end of %<#pragma GCC pch_preprocess%>");
22955     }
22956   else
22957     error_at (first_token->location, "expected string literal");
22958
22959   /* Skip to the end of the pragma.  */
22960   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22961     cp_lexer_get_preprocessor_token (NULL, first_token);
22962
22963   /* Now actually load the PCH file.  */
22964   if (name)
22965     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22966
22967   /* Read one more token to return to our caller.  We have to do this
22968      after reading the PCH file in, since its pointers have to be
22969      live.  */
22970   cp_lexer_get_preprocessor_token (NULL, first_token);
22971 }
22972
22973 /* Normal parsing of a pragma token.  Here we can (and must) use the
22974    regular lexer.  */
22975
22976 static bool
22977 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22978 {
22979   cp_token *pragma_tok;
22980   unsigned int id;
22981
22982   pragma_tok = cp_lexer_consume_token (parser->lexer);
22983   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22984   parser->lexer->in_pragma = true;
22985
22986   id = pragma_tok->pragma_kind;
22987   switch (id)
22988     {
22989     case PRAGMA_GCC_PCH_PREPROCESS:
22990       error_at (pragma_tok->location,
22991                 "%<#pragma GCC pch_preprocess%> must be first");
22992       break;
22993
22994     case PRAGMA_OMP_BARRIER:
22995       switch (context)
22996         {
22997         case pragma_compound:
22998           cp_parser_omp_barrier (parser, pragma_tok);
22999           return false;
23000         case pragma_stmt:
23001           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
23002                     "used in compound statements");
23003           break;
23004         default:
23005           goto bad_stmt;
23006         }
23007       break;
23008
23009     case PRAGMA_OMP_FLUSH:
23010       switch (context)
23011         {
23012         case pragma_compound:
23013           cp_parser_omp_flush (parser, pragma_tok);
23014           return false;
23015         case pragma_stmt:
23016           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23017                     "used in compound statements");
23018           break;
23019         default:
23020           goto bad_stmt;
23021         }
23022       break;
23023
23024     case PRAGMA_OMP_TASKWAIT:
23025       switch (context)
23026         {
23027         case pragma_compound:
23028           cp_parser_omp_taskwait (parser, pragma_tok);
23029           return false;
23030         case pragma_stmt:
23031           error_at (pragma_tok->location,
23032                     "%<#pragma omp taskwait%> may only be "
23033                     "used in compound statements");
23034           break;
23035         default:
23036           goto bad_stmt;
23037         }
23038       break;
23039
23040     case PRAGMA_OMP_THREADPRIVATE:
23041       cp_parser_omp_threadprivate (parser, pragma_tok);
23042       return false;
23043
23044     case PRAGMA_OMP_ATOMIC:
23045     case PRAGMA_OMP_CRITICAL:
23046     case PRAGMA_OMP_FOR:
23047     case PRAGMA_OMP_MASTER:
23048     case PRAGMA_OMP_ORDERED:
23049     case PRAGMA_OMP_PARALLEL:
23050     case PRAGMA_OMP_SECTIONS:
23051     case PRAGMA_OMP_SINGLE:
23052     case PRAGMA_OMP_TASK:
23053       if (context == pragma_external)
23054         goto bad_stmt;
23055       cp_parser_omp_construct (parser, pragma_tok);
23056       return true;
23057
23058     case PRAGMA_OMP_SECTION:
23059       error_at (pragma_tok->location, 
23060                 "%<#pragma omp section%> may only be used in "
23061                 "%<#pragma omp sections%> construct");
23062       break;
23063
23064     default:
23065       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23066       c_invoke_pragma_handler (id);
23067       break;
23068
23069     bad_stmt:
23070       cp_parser_error (parser, "expected declaration specifiers");
23071       break;
23072     }
23073
23074   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23075   return false;
23076 }
23077
23078 /* The interface the pragma parsers have to the lexer.  */
23079
23080 enum cpp_ttype
23081 pragma_lex (tree *value)
23082 {
23083   cp_token *tok;
23084   enum cpp_ttype ret;
23085
23086   tok = cp_lexer_peek_token (the_parser->lexer);
23087
23088   ret = tok->type;
23089   *value = tok->u.value;
23090
23091   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23092     ret = CPP_EOF;
23093   else if (ret == CPP_STRING)
23094     *value = cp_parser_string_literal (the_parser, false, false);
23095   else
23096     {
23097       cp_lexer_consume_token (the_parser->lexer);
23098       if (ret == CPP_KEYWORD)
23099         ret = CPP_NAME;
23100     }
23101
23102   return ret;
23103 }
23104
23105 \f
23106 /* External interface.  */
23107
23108 /* Parse one entire translation unit.  */
23109
23110 void
23111 c_parse_file (void)
23112 {
23113   static bool already_called = false;
23114
23115   if (already_called)
23116     {
23117       sorry ("inter-module optimizations not implemented for C++");
23118       return;
23119     }
23120   already_called = true;
23121
23122   the_parser = cp_parser_new ();
23123   push_deferring_access_checks (flag_access_control
23124                                 ? dk_no_deferred : dk_no_check);
23125   cp_parser_translation_unit (the_parser);
23126   the_parser = NULL;
23127 }
23128
23129 #include "gt-cp-parser.h"