toplev.c (input_file_stack, [...]): Remove.
[platform/upstream/gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A token's value and its associated deferred access checks and
48    qualifying scope.  */
49
50 struct tree_check GTY(())
51 {
52   /* The value associated with the token.  */
53   tree value;
54   /* The checks that have been associated with value.  */
55   VEC (deferred_access_check, gc)* checks;
56   /* The token's qualifying scope (used when it is a
57      CPP_NESTED_NAME_SPECIFIER).  */
58   tree qualifying_scope;
59 };
60
61 /* A C++ token.  */
62
63 typedef struct cp_token GTY (())
64 {
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 system header.  */
75   BOOL_BITFIELD in_system_header : 1;
76   /* True if this token is from a context where it is implicitly extern "C" */
77   BOOL_BITFIELD implicit_extern_c : 1;
78   /* True for a CPP_NAME token that is not a keyword (i.e., for which
79      KEYWORD is RID_MAX) iff this name was looked up and found to be
80      ambiguous.  An error has already been reported.  */
81   BOOL_BITFIELD ambiguous_p : 1;
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   /* The location at which this token was found.  */
90   location_t location;
91 } cp_token;
92
93 /* We use a stack of token pointer for saving token sets.  */
94 typedef struct cp_token *cp_token_position;
95 DEF_VEC_P (cp_token_position);
96 DEF_VEC_ALLOC_P (cp_token_position,heap);
97
98 static cp_token eof_token =
99 {
100   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, false, 0, { NULL },
101   0
102 };
103
104 /* The cp_lexer structure represents the C++ lexer.  It is responsible
105    for managing the token stream from the preprocessor and supplying
106    it to the parser.  Tokens are never added to the cp_lexer after
107    it is created.  */
108
109 typedef struct cp_lexer GTY (())
110 {
111   /* The memory allocated for the buffer.  NULL if this lexer does not
112      own the token buffer.  */
113   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
114   /* If the lexer owns the buffer, this is the number of tokens in the
115      buffer.  */
116   size_t buffer_length;
117
118   /* A pointer just past the last available token.  The tokens
119      in this lexer are [buffer, last_token).  */
120   cp_token_position GTY ((skip)) last_token;
121
122   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
123      no more available tokens.  */
124   cp_token_position GTY ((skip)) next_token;
125
126   /* A stack indicating positions at which cp_lexer_save_tokens was
127      called.  The top entry is the most recent position at which we
128      began saving tokens.  If the stack is non-empty, we are saving
129      tokens.  */
130   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
131
132   /* The next lexer in a linked list of lexers.  */
133   struct cp_lexer *next;
134
135   /* True if we should output debugging information.  */
136   bool debugging_p;
137
138   /* True if we're in the context of parsing a pragma, and should not
139      increment past the end-of-line marker.  */
140   bool in_pragma;
141 } cp_lexer;
142
143 /* cp_token_cache is a range of tokens.  There is no need to represent
144    allocate heap memory for it, since tokens are never removed from the
145    lexer's array.  There is also no need for the GC to walk through
146    a cp_token_cache, since everything in here is referenced through
147    a lexer.  */
148
149 typedef struct cp_token_cache GTY(())
150 {
151   /* The beginning of the token range.  */
152   cp_token * GTY((skip)) first;
153
154   /* Points immediately after the last token in the range.  */
155   cp_token * GTY ((skip)) last;
156 } cp_token_cache;
157
158 /* Prototypes.  */
159
160 static cp_lexer *cp_lexer_new_main
161   (void);
162 static cp_lexer *cp_lexer_new_from_tokens
163   (cp_token_cache *tokens);
164 static void cp_lexer_destroy
165   (cp_lexer *);
166 static int cp_lexer_saving_tokens
167   (const cp_lexer *);
168 static cp_token_position cp_lexer_token_position
169   (cp_lexer *, bool);
170 static cp_token *cp_lexer_token_at
171   (cp_lexer *, cp_token_position);
172 static void cp_lexer_get_preprocessor_token
173   (cp_lexer *, cp_token *);
174 static inline cp_token *cp_lexer_peek_token
175   (cp_lexer *);
176 static cp_token *cp_lexer_peek_nth_token
177   (cp_lexer *, size_t);
178 static inline bool cp_lexer_next_token_is
179   (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_not
181   (cp_lexer *, enum cpp_ttype);
182 static bool cp_lexer_next_token_is_keyword
183   (cp_lexer *, enum rid);
184 static cp_token *cp_lexer_consume_token
185   (cp_lexer *);
186 static void cp_lexer_purge_token
187   (cp_lexer *);
188 static void cp_lexer_purge_tokens_after
189   (cp_lexer *, cp_token_position);
190 static void cp_lexer_save_tokens
191   (cp_lexer *);
192 static void cp_lexer_commit_tokens
193   (cp_lexer *);
194 static void cp_lexer_rollback_tokens
195   (cp_lexer *);
196 #ifdef ENABLE_CHECKING
197 static void cp_lexer_print_token
198   (FILE *, cp_token *);
199 static inline bool cp_lexer_debugging_p
200   (cp_lexer *);
201 static void cp_lexer_start_debugging
202   (cp_lexer *) ATTRIBUTE_UNUSED;
203 static void cp_lexer_stop_debugging
204   (cp_lexer *) ATTRIBUTE_UNUSED;
205 #else
206 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
207    about passing NULL to functions that require non-NULL arguments
208    (fputs, fprintf).  It will never be used, so all we need is a value
209    of the right type that's guaranteed not to be NULL.  */
210 #define cp_lexer_debug_stream stdout
211 #define cp_lexer_print_token(str, tok) (void) 0
212 #define cp_lexer_debugging_p(lexer) 0
213 #endif /* ENABLE_CHECKING */
214
215 static cp_token_cache *cp_token_cache_new
216   (cp_token *, cp_token *);
217
218 static void cp_parser_initial_pragma
219   (cp_token *);
220
221 /* Manifest constants.  */
222 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
223 #define CP_SAVED_TOKEN_STACK 5
224
225 /* A token type for keywords, as opposed to ordinary identifiers.  */
226 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
227
228 /* A token type for template-ids.  If a template-id is processed while
229    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
230    the value of the CPP_TEMPLATE_ID is whatever was returned by
231    cp_parser_template_id.  */
232 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
233
234 /* A token type for nested-name-specifiers.  If a
235    nested-name-specifier is processed while parsing tentatively, it is
236    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
237    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
238    cp_parser_nested_name_specifier_opt.  */
239 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
240
241 /* A token type for tokens that are not tokens at all; these are used
242    to represent slots in the array where there used to be a token
243    that has now been deleted.  */
244 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
245
246 /* The number of token types, including C++-specific ones.  */
247 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
248
249 /* Variables.  */
250
251 #ifdef ENABLE_CHECKING
252 /* The stream to which debugging output should be written.  */
253 static FILE *cp_lexer_debug_stream;
254 #endif /* ENABLE_CHECKING */
255
256 /* Create a new main C++ lexer, the lexer that gets tokens from the
257    preprocessor.  */
258
259 static cp_lexer *
260 cp_lexer_new_main (void)
261 {
262   cp_token first_token;
263   cp_lexer *lexer;
264   cp_token *pos;
265   size_t alloc;
266   size_t space;
267   cp_token *buffer;
268
269   /* It's possible that parsing the first pragma will load a PCH file,
270      which is a GC collection point.  So we have to do that before
271      allocating any memory.  */
272   cp_parser_initial_pragma (&first_token);
273
274   c_common_no_more_pch ();
275
276   /* Allocate the memory.  */
277   lexer = GGC_CNEW (cp_lexer);
278
279 #ifdef ENABLE_CHECKING
280   /* Initially we are not debugging.  */
281   lexer->debugging_p = false;
282 #endif /* ENABLE_CHECKING */
283   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
284                                    CP_SAVED_TOKEN_STACK);
285
286   /* Create the buffer.  */
287   alloc = CP_LEXER_BUFFER_SIZE;
288   buffer = GGC_NEWVEC (cp_token, alloc);
289
290   /* Put the first token in the buffer.  */
291   space = alloc;
292   pos = buffer;
293   *pos = first_token;
294
295   /* Get the remaining tokens from the preprocessor.  */
296   while (pos->type != CPP_EOF)
297     {
298       pos++;
299       if (!--space)
300         {
301           space = alloc;
302           alloc *= 2;
303           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
304           pos = buffer + space;
305         }
306       cp_lexer_get_preprocessor_token (lexer, pos);
307     }
308   lexer->buffer = buffer;
309   lexer->buffer_length = alloc - space;
310   lexer->last_token = pos;
311   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
312
313   /* Subsequent preprocessor diagnostics should use compiler
314      diagnostic functions to get the compiler source location.  */
315   cpp_get_options (parse_in)->client_diagnostic = true;
316   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
317
318   gcc_assert (lexer->next_token->type != CPP_PURGED);
319   return lexer;
320 }
321
322 /* Create a new lexer whose token stream is primed with the tokens in
323    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
324
325 static cp_lexer *
326 cp_lexer_new_from_tokens (cp_token_cache *cache)
327 {
328   cp_token *first = cache->first;
329   cp_token *last = cache->last;
330   cp_lexer *lexer = GGC_CNEW (cp_lexer);
331
332   /* We do not own the buffer.  */
333   lexer->buffer = NULL;
334   lexer->buffer_length = 0;
335   lexer->next_token = first == last ? &eof_token : first;
336   lexer->last_token = last;
337
338   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
339                                    CP_SAVED_TOKEN_STACK);
340
341 #ifdef ENABLE_CHECKING
342   /* Initially we are not debugging.  */
343   lexer->debugging_p = false;
344 #endif
345
346   gcc_assert (lexer->next_token->type != CPP_PURGED);
347   return lexer;
348 }
349
350 /* Frees all resources associated with LEXER.  */
351
352 static void
353 cp_lexer_destroy (cp_lexer *lexer)
354 {
355   if (lexer->buffer)
356     ggc_free (lexer->buffer);
357   VEC_free (cp_token_position, heap, lexer->saved_tokens);
358   ggc_free (lexer);
359 }
360
361 /* Returns nonzero if debugging information should be output.  */
362
363 #ifdef ENABLE_CHECKING
364
365 static inline bool
366 cp_lexer_debugging_p (cp_lexer *lexer)
367 {
368   return lexer->debugging_p;
369 }
370
371 #endif /* ENABLE_CHECKING */
372
373 static inline cp_token_position
374 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
375 {
376   gcc_assert (!previous_p || lexer->next_token != &eof_token);
377
378   return lexer->next_token - previous_p;
379 }
380
381 static inline cp_token *
382 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
383 {
384   return pos;
385 }
386
387 /* nonzero if we are presently saving tokens.  */
388
389 static inline int
390 cp_lexer_saving_tokens (const cp_lexer* lexer)
391 {
392   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
393 }
394
395 /* Store the next token from the preprocessor in *TOKEN.  Return true
396    if we reach EOF.  If LEXER is NULL, assume we are handling an
397    initial #pragma pch_preprocess, and thus want the lexer to return
398    processed strings.  */
399
400 static void
401 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
402 {
403   static int is_extern_c = 0;
404
405    /* Get a new token from the preprocessor.  */
406   token->type
407     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
408                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
409   token->keyword = RID_MAX;
410   token->pragma_kind = PRAGMA_NONE;
411   token->in_system_header = in_system_header;
412
413   /* On some systems, some header files are surrounded by an
414      implicit extern "C" block.  Set a flag in the token if it
415      comes from such a header.  */
416   is_extern_c += pending_lang_change;
417   pending_lang_change = 0;
418   token->implicit_extern_c = is_extern_c > 0;
419
420   /* Check to see if this token is a keyword.  */
421   if (token->type == CPP_NAME)
422     {
423       if (C_IS_RESERVED_WORD (token->u.value))
424         {
425           /* Mark this token as a keyword.  */
426           token->type = CPP_KEYWORD;
427           /* Record which keyword.  */
428           token->keyword = C_RID_CODE (token->u.value);
429           /* Update the value.  Some keywords are mapped to particular
430              entities, rather than simply having the value of the
431              corresponding IDENTIFIER_NODE.  For example, `__const' is
432              mapped to `const'.  */
433           token->u.value = ridpointers[token->keyword];
434         }
435       else
436         {
437           if (warn_cxx0x_compat
438               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
439               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
440             {
441               /* Warn about the C++0x keyword (but still treat it as
442                  an identifier).  */
443               warning (OPT_Wc__0x_compat, 
444                        "identifier %<%s%> will become a keyword in C++0x",
445                        IDENTIFIER_POINTER (token->u.value));
446
447               /* Clear out the C_RID_CODE so we don't warn about this
448                  particular identifier-turned-keyword again.  */
449               C_RID_CODE (token->u.value) = RID_MAX;
450             }
451
452           token->ambiguous_p = false;
453           token->keyword = RID_MAX;
454         }
455     }
456   /* Handle Objective-C++ keywords.  */
457   else if (token->type == CPP_AT_NAME)
458     {
459       token->type = CPP_KEYWORD;
460       switch (C_RID_CODE (token->u.value))
461         {
462         /* Map 'class' to '@class', 'private' to '@private', etc.  */
463         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
464         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
465         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
466         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
467         case RID_THROW: token->keyword = RID_AT_THROW; break;
468         case RID_TRY: token->keyword = RID_AT_TRY; break;
469         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
470         default: token->keyword = C_RID_CODE (token->u.value);
471         }
472     }
473   else if (token->type == CPP_PRAGMA)
474     {
475       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
476       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
477       token->u.value = NULL_TREE;
478     }
479 }
480
481 /* Update the globals input_location and in_system_header and the
482    input file stack from TOKEN.  */
483 static inline void
484 cp_lexer_set_source_position_from_token (cp_token *token)
485 {
486   if (token->type != CPP_EOF)
487     {
488       input_location = token->location;
489       in_system_header = token->in_system_header;
490     }
491 }
492
493 /* Return a pointer to the next token in the token stream, but do not
494    consume it.  */
495
496 static inline cp_token *
497 cp_lexer_peek_token (cp_lexer *lexer)
498 {
499   if (cp_lexer_debugging_p (lexer))
500     {
501       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
502       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
503       putc ('\n', cp_lexer_debug_stream);
504     }
505   return lexer->next_token;
506 }
507
508 /* Return true if the next token has the indicated TYPE.  */
509
510 static inline bool
511 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
512 {
513   return cp_lexer_peek_token (lexer)->type == type;
514 }
515
516 /* Return true if the next token does not have the indicated TYPE.  */
517
518 static inline bool
519 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
520 {
521   return !cp_lexer_next_token_is (lexer, type);
522 }
523
524 /* Return true if the next token is the indicated KEYWORD.  */
525
526 static inline bool
527 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
528 {
529   return cp_lexer_peek_token (lexer)->keyword == keyword;
530 }
531
532 /* Return true if the next token is a keyword for a decl-specifier.  */
533
534 static bool
535 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
536 {
537   cp_token *token;
538
539   token = cp_lexer_peek_token (lexer);
540   switch (token->keyword) 
541     {
542       /* Storage classes.  */
543     case RID_AUTO:
544     case RID_REGISTER:
545     case RID_STATIC:
546     case RID_EXTERN:
547     case RID_MUTABLE:
548     case RID_THREAD:
549       /* Elaborated type specifiers.  */
550     case RID_ENUM:
551     case RID_CLASS:
552     case RID_STRUCT:
553     case RID_UNION:
554     case RID_TYPENAME:
555       /* Simple type specifiers.  */
556     case RID_CHAR:
557     case RID_WCHAR:
558     case RID_BOOL:
559     case RID_SHORT:
560     case RID_INT:
561     case RID_LONG:
562     case RID_SIGNED:
563     case RID_UNSIGNED:
564     case RID_FLOAT:
565     case RID_DOUBLE:
566     case RID_VOID:
567       /* GNU extensions.  */ 
568     case RID_ATTRIBUTE:
569     case RID_TYPEOF:
570       /* C++0x extensions.  */
571     case RID_DECLTYPE:
572       return true;
573
574     default:
575       return false;
576     }
577 }
578
579 /* Return a pointer to the Nth token in the token stream.  If N is 1,
580    then this is precisely equivalent to cp_lexer_peek_token (except
581    that it is not inline).  One would like to disallow that case, but
582    there is one case (cp_parser_nth_token_starts_template_id) where
583    the caller passes a variable for N and it might be 1.  */
584
585 static cp_token *
586 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
587 {
588   cp_token *token;
589
590   /* N is 1-based, not zero-based.  */
591   gcc_assert (n > 0);
592
593   if (cp_lexer_debugging_p (lexer))
594     fprintf (cp_lexer_debug_stream,
595              "cp_lexer: peeking ahead %ld at token: ", (long)n);
596
597   --n;
598   token = lexer->next_token;
599   gcc_assert (!n || token != &eof_token);
600   while (n != 0)
601     {
602       ++token;
603       if (token == lexer->last_token)
604         {
605           token = &eof_token;
606           break;
607         }
608
609       if (token->type != CPP_PURGED)
610         --n;
611     }
612
613   if (cp_lexer_debugging_p (lexer))
614     {
615       cp_lexer_print_token (cp_lexer_debug_stream, token);
616       putc ('\n', cp_lexer_debug_stream);
617     }
618
619   return token;
620 }
621
622 /* Return the next token, and advance the lexer's next_token pointer
623    to point to the next non-purged token.  */
624
625 static cp_token *
626 cp_lexer_consume_token (cp_lexer* lexer)
627 {
628   cp_token *token = lexer->next_token;
629
630   gcc_assert (token != &eof_token);
631   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
632
633   do
634     {
635       lexer->next_token++;
636       if (lexer->next_token == lexer->last_token)
637         {
638           lexer->next_token = &eof_token;
639           break;
640         }
641
642     }
643   while (lexer->next_token->type == CPP_PURGED);
644
645   cp_lexer_set_source_position_from_token (token);
646
647   /* Provide debugging output.  */
648   if (cp_lexer_debugging_p (lexer))
649     {
650       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
651       cp_lexer_print_token (cp_lexer_debug_stream, token);
652       putc ('\n', cp_lexer_debug_stream);
653     }
654
655   return token;
656 }
657
658 /* Permanently remove the next token from the token stream, and
659    advance the next_token pointer to refer to the next non-purged
660    token.  */
661
662 static void
663 cp_lexer_purge_token (cp_lexer *lexer)
664 {
665   cp_token *tok = lexer->next_token;
666
667   gcc_assert (tok != &eof_token);
668   tok->type = CPP_PURGED;
669   tok->location = UNKNOWN_LOCATION;
670   tok->u.value = NULL_TREE;
671   tok->keyword = RID_MAX;
672
673   do
674     {
675       tok++;
676       if (tok == lexer->last_token)
677         {
678           tok = &eof_token;
679           break;
680         }
681     }
682   while (tok->type == CPP_PURGED);
683   lexer->next_token = tok;
684 }
685
686 /* Permanently remove all tokens after TOK, up to, but not
687    including, the token that will be returned next by
688    cp_lexer_peek_token.  */
689
690 static void
691 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
692 {
693   cp_token *peek = lexer->next_token;
694
695   if (peek == &eof_token)
696     peek = lexer->last_token;
697
698   gcc_assert (tok < peek);
699
700   for ( tok += 1; tok != peek; tok += 1)
701     {
702       tok->type = CPP_PURGED;
703       tok->location = UNKNOWN_LOCATION;
704       tok->u.value = NULL_TREE;
705       tok->keyword = RID_MAX;
706     }
707 }
708
709 /* Begin saving tokens.  All tokens consumed after this point will be
710    preserved.  */
711
712 static void
713 cp_lexer_save_tokens (cp_lexer* lexer)
714 {
715   /* Provide debugging output.  */
716   if (cp_lexer_debugging_p (lexer))
717     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
718
719   VEC_safe_push (cp_token_position, heap,
720                  lexer->saved_tokens, lexer->next_token);
721 }
722
723 /* Commit to the portion of the token stream most recently saved.  */
724
725 static void
726 cp_lexer_commit_tokens (cp_lexer* lexer)
727 {
728   /* Provide debugging output.  */
729   if (cp_lexer_debugging_p (lexer))
730     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
731
732   VEC_pop (cp_token_position, lexer->saved_tokens);
733 }
734
735 /* Return all tokens saved since the last call to cp_lexer_save_tokens
736    to the token stream.  Stop saving tokens.  */
737
738 static void
739 cp_lexer_rollback_tokens (cp_lexer* lexer)
740 {
741   /* Provide debugging output.  */
742   if (cp_lexer_debugging_p (lexer))
743     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
744
745   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
746 }
747
748 /* Print a representation of the TOKEN on the STREAM.  */
749
750 #ifdef ENABLE_CHECKING
751
752 static void
753 cp_lexer_print_token (FILE * stream, cp_token *token)
754 {
755   /* We don't use cpp_type2name here because the parser defines
756      a few tokens of its own.  */
757   static const char *const token_names[] = {
758     /* cpplib-defined token types */
759 #define OP(e, s) #e,
760 #define TK(e, s) #e,
761     TTYPE_TABLE
762 #undef OP
763 #undef TK
764     /* C++ parser token types - see "Manifest constants", above.  */
765     "KEYWORD",
766     "TEMPLATE_ID",
767     "NESTED_NAME_SPECIFIER",
768     "PURGED"
769   };
770
771   /* If we have a name for the token, print it out.  Otherwise, we
772      simply give the numeric code.  */
773   gcc_assert (token->type < ARRAY_SIZE(token_names));
774   fputs (token_names[token->type], stream);
775
776   /* For some tokens, print the associated data.  */
777   switch (token->type)
778     {
779     case CPP_KEYWORD:
780       /* Some keywords have a value that is not an IDENTIFIER_NODE.
781          For example, `struct' is mapped to an INTEGER_CST.  */
782       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
783         break;
784       /* else fall through */
785     case CPP_NAME:
786       fputs (IDENTIFIER_POINTER (token->u.value), stream);
787       break;
788
789     case CPP_STRING:
790     case CPP_WSTRING:
791       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
792       break;
793
794     default:
795       break;
796     }
797 }
798
799 /* Start emitting debugging information.  */
800
801 static void
802 cp_lexer_start_debugging (cp_lexer* lexer)
803 {
804   lexer->debugging_p = true;
805 }
806
807 /* Stop emitting debugging information.  */
808
809 static void
810 cp_lexer_stop_debugging (cp_lexer* lexer)
811 {
812   lexer->debugging_p = false;
813 }
814
815 #endif /* ENABLE_CHECKING */
816
817 /* Create a new cp_token_cache, representing a range of tokens.  */
818
819 static cp_token_cache *
820 cp_token_cache_new (cp_token *first, cp_token *last)
821 {
822   cp_token_cache *cache = GGC_NEW (cp_token_cache);
823   cache->first = first;
824   cache->last = last;
825   return cache;
826 }
827
828 \f
829 /* Decl-specifiers.  */
830
831 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
832
833 static void
834 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
835 {
836   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
837 }
838
839 /* Declarators.  */
840
841 /* Nothing other than the parser should be creating declarators;
842    declarators are a semi-syntactic representation of C++ entities.
843    Other parts of the front end that need to create entities (like
844    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
845
846 static cp_declarator *make_call_declarator
847   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
848 static cp_declarator *make_array_declarator
849   (cp_declarator *, tree);
850 static cp_declarator *make_pointer_declarator
851   (cp_cv_quals, cp_declarator *);
852 static cp_declarator *make_reference_declarator
853   (cp_cv_quals, cp_declarator *, bool);
854 static cp_parameter_declarator *make_parameter_declarator
855   (cp_decl_specifier_seq *, cp_declarator *, tree);
856 static cp_declarator *make_ptrmem_declarator
857   (cp_cv_quals, tree, cp_declarator *);
858
859 /* An erroneous declarator.  */
860 static cp_declarator *cp_error_declarator;
861
862 /* The obstack on which declarators and related data structures are
863    allocated.  */
864 static struct obstack declarator_obstack;
865
866 /* Alloc BYTES from the declarator memory pool.  */
867
868 static inline void *
869 alloc_declarator (size_t bytes)
870 {
871   return obstack_alloc (&declarator_obstack, bytes);
872 }
873
874 /* Allocate a declarator of the indicated KIND.  Clear fields that are
875    common to all declarators.  */
876
877 static cp_declarator *
878 make_declarator (cp_declarator_kind kind)
879 {
880   cp_declarator *declarator;
881
882   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
883   declarator->kind = kind;
884   declarator->attributes = NULL_TREE;
885   declarator->declarator = NULL;
886   declarator->parameter_pack_p = false;
887
888   return declarator;
889 }
890
891 /* Make a declarator for a generalized identifier.  If
892    QUALIFYING_SCOPE is non-NULL, the identifier is
893    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
894    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
895    is, if any.   */
896
897 static cp_declarator *
898 make_id_declarator (tree qualifying_scope, tree unqualified_name,
899                     special_function_kind sfk)
900 {
901   cp_declarator *declarator;
902
903   /* It is valid to write:
904
905        class C { void f(); };
906        typedef C D;
907        void D::f();
908
909      The standard is not clear about whether `typedef const C D' is
910      legal; as of 2002-09-15 the committee is considering that
911      question.  EDG 3.0 allows that syntax.  Therefore, we do as
912      well.  */
913   if (qualifying_scope && TYPE_P (qualifying_scope))
914     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
915
916   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
917               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
918               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
919
920   declarator = make_declarator (cdk_id);
921   declarator->u.id.qualifying_scope = qualifying_scope;
922   declarator->u.id.unqualified_name = unqualified_name;
923   declarator->u.id.sfk = sfk;
924   
925   return declarator;
926 }
927
928 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
929    of modifiers such as const or volatile to apply to the pointer
930    type, represented as identifiers.  */
931
932 cp_declarator *
933 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
934 {
935   cp_declarator *declarator;
936
937   declarator = make_declarator (cdk_pointer);
938   declarator->declarator = target;
939   declarator->u.pointer.qualifiers = cv_qualifiers;
940   declarator->u.pointer.class_type = NULL_TREE;
941   if (target)
942     {
943       declarator->parameter_pack_p = target->parameter_pack_p;
944       target->parameter_pack_p = false;
945     }
946   else
947     declarator->parameter_pack_p = false;
948
949   return declarator;
950 }
951
952 /* Like make_pointer_declarator -- but for references.  */
953
954 cp_declarator *
955 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
956                            bool rvalue_ref)
957 {
958   cp_declarator *declarator;
959
960   declarator = make_declarator (cdk_reference);
961   declarator->declarator = target;
962   declarator->u.reference.qualifiers = cv_qualifiers;
963   declarator->u.reference.rvalue_ref = rvalue_ref;
964   if (target)
965     {
966       declarator->parameter_pack_p = target->parameter_pack_p;
967       target->parameter_pack_p = false;
968     }
969   else
970     declarator->parameter_pack_p = false;
971
972   return declarator;
973 }
974
975 /* Like make_pointer_declarator -- but for a pointer to a non-static
976    member of CLASS_TYPE.  */
977
978 cp_declarator *
979 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
980                         cp_declarator *pointee)
981 {
982   cp_declarator *declarator;
983
984   declarator = make_declarator (cdk_ptrmem);
985   declarator->declarator = pointee;
986   declarator->u.pointer.qualifiers = cv_qualifiers;
987   declarator->u.pointer.class_type = class_type;
988
989   if (pointee)
990     {
991       declarator->parameter_pack_p = pointee->parameter_pack_p;
992       pointee->parameter_pack_p = false;
993     }
994   else
995     declarator->parameter_pack_p = false;
996
997   return declarator;
998 }
999
1000 /* Make a declarator for the function given by TARGET, with the
1001    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1002    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1003    indicates what exceptions can be thrown.  */
1004
1005 cp_declarator *
1006 make_call_declarator (cp_declarator *target,
1007                       cp_parameter_declarator *parms,
1008                       cp_cv_quals cv_qualifiers,
1009                       tree exception_specification)
1010 {
1011   cp_declarator *declarator;
1012
1013   declarator = make_declarator (cdk_function);
1014   declarator->declarator = target;
1015   declarator->u.function.parameters = parms;
1016   declarator->u.function.qualifiers = cv_qualifiers;
1017   declarator->u.function.exception_specification = exception_specification;
1018   if (target)
1019     {
1020       declarator->parameter_pack_p = target->parameter_pack_p;
1021       target->parameter_pack_p = false;
1022     }
1023   else
1024     declarator->parameter_pack_p = false;
1025
1026   return declarator;
1027 }
1028
1029 /* Make a declarator for an array of BOUNDS elements, each of which is
1030    defined by ELEMENT.  */
1031
1032 cp_declarator *
1033 make_array_declarator (cp_declarator *element, tree bounds)
1034 {
1035   cp_declarator *declarator;
1036
1037   declarator = make_declarator (cdk_array);
1038   declarator->declarator = element;
1039   declarator->u.array.bounds = bounds;
1040   if (element)
1041     {
1042       declarator->parameter_pack_p = element->parameter_pack_p;
1043       element->parameter_pack_p = false;
1044     }
1045   else
1046     declarator->parameter_pack_p = false;
1047
1048   return declarator;
1049 }
1050
1051 /* Determine whether the declarator we've seen so far can be a
1052    parameter pack, when followed by an ellipsis.  */
1053 static bool 
1054 declarator_can_be_parameter_pack (cp_declarator *declarator)
1055 {
1056   /* Search for a declarator name, or any other declarator that goes
1057      after the point where the ellipsis could appear in a parameter
1058      pack. If we find any of these, then this declarator can not be
1059      made into a parameter pack.  */
1060   bool found = false;
1061   while (declarator && !found)
1062     {
1063       switch ((int)declarator->kind)
1064         {
1065         case cdk_id:
1066         case cdk_array:
1067           found = true;
1068           break;
1069
1070         case cdk_error:
1071           return true;
1072
1073         default:
1074           declarator = declarator->declarator;
1075           break;
1076         }
1077     }
1078
1079   return !found;
1080 }
1081
1082 cp_parameter_declarator *no_parameters;
1083
1084 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1085    DECLARATOR and DEFAULT_ARGUMENT.  */
1086
1087 cp_parameter_declarator *
1088 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1089                            cp_declarator *declarator,
1090                            tree default_argument)
1091 {
1092   cp_parameter_declarator *parameter;
1093
1094   parameter = ((cp_parameter_declarator *)
1095                alloc_declarator (sizeof (cp_parameter_declarator)));
1096   parameter->next = NULL;
1097   if (decl_specifiers)
1098     parameter->decl_specifiers = *decl_specifiers;
1099   else
1100     clear_decl_specs (&parameter->decl_specifiers);
1101   parameter->declarator = declarator;
1102   parameter->default_argument = default_argument;
1103   parameter->ellipsis_p = false;
1104
1105   return parameter;
1106 }
1107
1108 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1109
1110 static bool
1111 function_declarator_p (const cp_declarator *declarator)
1112 {
1113   while (declarator)
1114     {
1115       if (declarator->kind == cdk_function
1116           && declarator->declarator->kind == cdk_id)
1117         return true;
1118       if (declarator->kind == cdk_id
1119           || declarator->kind == cdk_error)
1120         return false;
1121       declarator = declarator->declarator;
1122     }
1123   return false;
1124 }
1125  
1126 /* The parser.  */
1127
1128 /* Overview
1129    --------
1130
1131    A cp_parser parses the token stream as specified by the C++
1132    grammar.  Its job is purely parsing, not semantic analysis.  For
1133    example, the parser breaks the token stream into declarators,
1134    expressions, statements, and other similar syntactic constructs.
1135    It does not check that the types of the expressions on either side
1136    of an assignment-statement are compatible, or that a function is
1137    not declared with a parameter of type `void'.
1138
1139    The parser invokes routines elsewhere in the compiler to perform
1140    semantic analysis and to build up the abstract syntax tree for the
1141    code processed.
1142
1143    The parser (and the template instantiation code, which is, in a
1144    way, a close relative of parsing) are the only parts of the
1145    compiler that should be calling push_scope and pop_scope, or
1146    related functions.  The parser (and template instantiation code)
1147    keeps track of what scope is presently active; everything else
1148    should simply honor that.  (The code that generates static
1149    initializers may also need to set the scope, in order to check
1150    access control correctly when emitting the initializers.)
1151
1152    Methodology
1153    -----------
1154
1155    The parser is of the standard recursive-descent variety.  Upcoming
1156    tokens in the token stream are examined in order to determine which
1157    production to use when parsing a non-terminal.  Some C++ constructs
1158    require arbitrary look ahead to disambiguate.  For example, it is
1159    impossible, in the general case, to tell whether a statement is an
1160    expression or declaration without scanning the entire statement.
1161    Therefore, the parser is capable of "parsing tentatively."  When the
1162    parser is not sure what construct comes next, it enters this mode.
1163    Then, while we attempt to parse the construct, the parser queues up
1164    error messages, rather than issuing them immediately, and saves the
1165    tokens it consumes.  If the construct is parsed successfully, the
1166    parser "commits", i.e., it issues any queued error messages and
1167    the tokens that were being preserved are permanently discarded.
1168    If, however, the construct is not parsed successfully, the parser
1169    rolls back its state completely so that it can resume parsing using
1170    a different alternative.
1171
1172    Future Improvements
1173    -------------------
1174
1175    The performance of the parser could probably be improved substantially.
1176    We could often eliminate the need to parse tentatively by looking ahead
1177    a little bit.  In some places, this approach might not entirely eliminate
1178    the need to parse tentatively, but it might still speed up the average
1179    case.  */
1180
1181 /* Flags that are passed to some parsing functions.  These values can
1182    be bitwise-ored together.  */
1183
1184 typedef enum cp_parser_flags
1185 {
1186   /* No flags.  */
1187   CP_PARSER_FLAGS_NONE = 0x0,
1188   /* The construct is optional.  If it is not present, then no error
1189      should be issued.  */
1190   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1191   /* When parsing a type-specifier, do not allow user-defined types.  */
1192   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1193 } cp_parser_flags;
1194
1195 /* The different kinds of declarators we want to parse.  */
1196
1197 typedef enum cp_parser_declarator_kind
1198 {
1199   /* We want an abstract declarator.  */
1200   CP_PARSER_DECLARATOR_ABSTRACT,
1201   /* We want a named declarator.  */
1202   CP_PARSER_DECLARATOR_NAMED,
1203   /* We don't mind, but the name must be an unqualified-id.  */
1204   CP_PARSER_DECLARATOR_EITHER
1205 } cp_parser_declarator_kind;
1206
1207 /* The precedence values used to parse binary expressions.  The minimum value
1208    of PREC must be 1, because zero is reserved to quickly discriminate
1209    binary operators from other tokens.  */
1210
1211 enum cp_parser_prec
1212 {
1213   PREC_NOT_OPERATOR,
1214   PREC_LOGICAL_OR_EXPRESSION,
1215   PREC_LOGICAL_AND_EXPRESSION,
1216   PREC_INCLUSIVE_OR_EXPRESSION,
1217   PREC_EXCLUSIVE_OR_EXPRESSION,
1218   PREC_AND_EXPRESSION,
1219   PREC_EQUALITY_EXPRESSION,
1220   PREC_RELATIONAL_EXPRESSION,
1221   PREC_SHIFT_EXPRESSION,
1222   PREC_ADDITIVE_EXPRESSION,
1223   PREC_MULTIPLICATIVE_EXPRESSION,
1224   PREC_PM_EXPRESSION,
1225   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1226 };
1227
1228 /* A mapping from a token type to a corresponding tree node type, with a
1229    precedence value.  */
1230
1231 typedef struct cp_parser_binary_operations_map_node
1232 {
1233   /* The token type.  */
1234   enum cpp_ttype token_type;
1235   /* The corresponding tree code.  */
1236   enum tree_code tree_type;
1237   /* The precedence of this operator.  */
1238   enum cp_parser_prec prec;
1239 } cp_parser_binary_operations_map_node;
1240
1241 /* The status of a tentative parse.  */
1242
1243 typedef enum cp_parser_status_kind
1244 {
1245   /* No errors have occurred.  */
1246   CP_PARSER_STATUS_KIND_NO_ERROR,
1247   /* An error has occurred.  */
1248   CP_PARSER_STATUS_KIND_ERROR,
1249   /* We are committed to this tentative parse, whether or not an error
1250      has occurred.  */
1251   CP_PARSER_STATUS_KIND_COMMITTED
1252 } cp_parser_status_kind;
1253
1254 typedef struct cp_parser_expression_stack_entry
1255 {
1256   /* Left hand side of the binary operation we are currently
1257      parsing.  */
1258   tree lhs;
1259   /* Original tree code for left hand side, if it was a binary
1260      expression itself (used for -Wparentheses).  */
1261   enum tree_code lhs_type;
1262   /* Tree code for the binary operation we are parsing.  */
1263   enum tree_code tree_type;
1264   /* Precedence of the binary operation we are parsing.  */
1265   int prec;
1266 } cp_parser_expression_stack_entry;
1267
1268 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1269    entries because precedence levels on the stack are monotonically
1270    increasing.  */
1271 typedef struct cp_parser_expression_stack_entry
1272   cp_parser_expression_stack[NUM_PREC_VALUES];
1273
1274 /* Context that is saved and restored when parsing tentatively.  */
1275 typedef struct cp_parser_context GTY (())
1276 {
1277   /* If this is a tentative parsing context, the status of the
1278      tentative parse.  */
1279   enum cp_parser_status_kind status;
1280   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1281      that are looked up in this context must be looked up both in the
1282      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1283      the context of the containing expression.  */
1284   tree object_type;
1285
1286   /* The next parsing context in the stack.  */
1287   struct cp_parser_context *next;
1288 } cp_parser_context;
1289
1290 /* Prototypes.  */
1291
1292 /* Constructors and destructors.  */
1293
1294 static cp_parser_context *cp_parser_context_new
1295   (cp_parser_context *);
1296
1297 /* Class variables.  */
1298
1299 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1300
1301 /* The operator-precedence table used by cp_parser_binary_expression.
1302    Transformed into an associative array (binops_by_token) by
1303    cp_parser_new.  */
1304
1305 static const cp_parser_binary_operations_map_node binops[] = {
1306   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1307   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1308
1309   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1310   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1311   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1312
1313   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1314   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1315
1316   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1317   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1318
1319   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1320   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1321   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1322   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1323
1324   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1325   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1326
1327   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1328
1329   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1330
1331   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1332
1333   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1334
1335   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1336 };
1337
1338 /* The same as binops, but initialized by cp_parser_new so that
1339    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1340    for speed.  */
1341 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1342
1343 /* Constructors and destructors.  */
1344
1345 /* Construct a new context.  The context below this one on the stack
1346    is given by NEXT.  */
1347
1348 static cp_parser_context *
1349 cp_parser_context_new (cp_parser_context* next)
1350 {
1351   cp_parser_context *context;
1352
1353   /* Allocate the storage.  */
1354   if (cp_parser_context_free_list != NULL)
1355     {
1356       /* Pull the first entry from the free list.  */
1357       context = cp_parser_context_free_list;
1358       cp_parser_context_free_list = context->next;
1359       memset (context, 0, sizeof (*context));
1360     }
1361   else
1362     context = GGC_CNEW (cp_parser_context);
1363
1364   /* No errors have occurred yet in this context.  */
1365   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1366   /* If this is not the bottomost context, copy information that we
1367      need from the previous context.  */
1368   if (next)
1369     {
1370       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1371          expression, then we are parsing one in this context, too.  */
1372       context->object_type = next->object_type;
1373       /* Thread the stack.  */
1374       context->next = next;
1375     }
1376
1377   return context;
1378 }
1379
1380 /* The cp_parser structure represents the C++ parser.  */
1381
1382 typedef struct cp_parser GTY(())
1383 {
1384   /* The lexer from which we are obtaining tokens.  */
1385   cp_lexer *lexer;
1386
1387   /* The scope in which names should be looked up.  If NULL_TREE, then
1388      we look up names in the scope that is currently open in the
1389      source program.  If non-NULL, this is either a TYPE or
1390      NAMESPACE_DECL for the scope in which we should look.  It can
1391      also be ERROR_MARK, when we've parsed a bogus scope.
1392
1393      This value is not cleared automatically after a name is looked
1394      up, so we must be careful to clear it before starting a new look
1395      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1396      will look up `Z' in the scope of `X', rather than the current
1397      scope.)  Unfortunately, it is difficult to tell when name lookup
1398      is complete, because we sometimes peek at a token, look it up,
1399      and then decide not to consume it.   */
1400   tree scope;
1401
1402   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1403      last lookup took place.  OBJECT_SCOPE is used if an expression
1404      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1405      respectively.  QUALIFYING_SCOPE is used for an expression of the
1406      form "X::Y"; it refers to X.  */
1407   tree object_scope;
1408   tree qualifying_scope;
1409
1410   /* A stack of parsing contexts.  All but the bottom entry on the
1411      stack will be tentative contexts.
1412
1413      We parse tentatively in order to determine which construct is in
1414      use in some situations.  For example, in order to determine
1415      whether a statement is an expression-statement or a
1416      declaration-statement we parse it tentatively as a
1417      declaration-statement.  If that fails, we then reparse the same
1418      token stream as an expression-statement.  */
1419   cp_parser_context *context;
1420
1421   /* True if we are parsing GNU C++.  If this flag is not set, then
1422      GNU extensions are not recognized.  */
1423   bool allow_gnu_extensions_p;
1424
1425   /* TRUE if the `>' token should be interpreted as the greater-than
1426      operator.  FALSE if it is the end of a template-id or
1427      template-parameter-list. In C++0x mode, this flag also applies to
1428      `>>' tokens, which are viewed as two consecutive `>' tokens when
1429      this flag is FALSE.  */
1430   bool greater_than_is_operator_p;
1431
1432   /* TRUE if default arguments are allowed within a parameter list
1433      that starts at this point. FALSE if only a gnu extension makes
1434      them permissible.  */
1435   bool default_arg_ok_p;
1436
1437   /* TRUE if we are parsing an integral constant-expression.  See
1438      [expr.const] for a precise definition.  */
1439   bool integral_constant_expression_p;
1440
1441   /* TRUE if we are parsing an integral constant-expression -- but a
1442      non-constant expression should be permitted as well.  This flag
1443      is used when parsing an array bound so that GNU variable-length
1444      arrays are tolerated.  */
1445   bool allow_non_integral_constant_expression_p;
1446
1447   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1448      been seen that makes the expression non-constant.  */
1449   bool non_integral_constant_expression_p;
1450
1451   /* TRUE if local variable names and `this' are forbidden in the
1452      current context.  */
1453   bool local_variables_forbidden_p;
1454
1455   /* TRUE if the declaration we are parsing is part of a
1456      linkage-specification of the form `extern string-literal
1457      declaration'.  */
1458   bool in_unbraced_linkage_specification_p;
1459
1460   /* TRUE if we are presently parsing a declarator, after the
1461      direct-declarator.  */
1462   bool in_declarator_p;
1463
1464   /* TRUE if we are presently parsing a template-argument-list.  */
1465   bool in_template_argument_list_p;
1466
1467   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1468      to IN_OMP_BLOCK if parsing OpenMP structured block and
1469      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1470      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1471      iteration-statement, OpenMP block or loop within that switch.  */
1472 #define IN_SWITCH_STMT          1
1473 #define IN_ITERATION_STMT       2
1474 #define IN_OMP_BLOCK            4
1475 #define IN_OMP_FOR              8
1476 #define IN_IF_STMT             16
1477   unsigned char in_statement;
1478
1479   /* TRUE if we are presently parsing the body of a switch statement.
1480      Note that this doesn't quite overlap with in_statement above.
1481      The difference relates to giving the right sets of error messages:
1482      "case not in switch" vs "break statement used with OpenMP...".  */
1483   bool in_switch_statement_p;
1484
1485   /* TRUE if we are parsing a type-id in an expression context.  In
1486      such a situation, both "type (expr)" and "type (type)" are valid
1487      alternatives.  */
1488   bool in_type_id_in_expr_p;
1489
1490   /* TRUE if we are currently in a header file where declarations are
1491      implicitly extern "C".  */
1492   bool implicit_extern_c;
1493
1494   /* TRUE if strings in expressions should be translated to the execution
1495      character set.  */
1496   bool translate_strings_p;
1497
1498   /* TRUE if we are presently parsing the body of a function, but not
1499      a local class.  */
1500   bool in_function_body;
1501
1502   /* If non-NULL, then we are parsing a construct where new type
1503      definitions are not permitted.  The string stored here will be
1504      issued as an error message if a type is defined.  */
1505   const char *type_definition_forbidden_message;
1506
1507   /* A list of lists. The outer list is a stack, used for member
1508      functions of local classes. At each level there are two sub-list,
1509      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1510      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1511      TREE_VALUE's. The functions are chained in reverse declaration
1512      order.
1513
1514      The TREE_PURPOSE sublist contains those functions with default
1515      arguments that need post processing, and the TREE_VALUE sublist
1516      contains those functions with definitions that need post
1517      processing.
1518
1519      These lists can only be processed once the outermost class being
1520      defined is complete.  */
1521   tree unparsed_functions_queues;
1522
1523   /* The number of classes whose definitions are currently in
1524      progress.  */
1525   unsigned num_classes_being_defined;
1526
1527   /* The number of template parameter lists that apply directly to the
1528      current declaration.  */
1529   unsigned num_template_parameter_lists;
1530 } cp_parser;
1531
1532 /* Prototypes.  */
1533
1534 /* Constructors and destructors.  */
1535
1536 static cp_parser *cp_parser_new
1537   (void);
1538
1539 /* Routines to parse various constructs.
1540
1541    Those that return `tree' will return the error_mark_node (rather
1542    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1543    Sometimes, they will return an ordinary node if error-recovery was
1544    attempted, even though a parse error occurred.  So, to check
1545    whether or not a parse error occurred, you should always use
1546    cp_parser_error_occurred.  If the construct is optional (indicated
1547    either by an `_opt' in the name of the function that does the
1548    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1549    the construct is not present.  */
1550
1551 /* Lexical conventions [gram.lex]  */
1552
1553 static tree cp_parser_identifier
1554   (cp_parser *);
1555 static tree cp_parser_string_literal
1556   (cp_parser *, bool, bool);
1557
1558 /* Basic concepts [gram.basic]  */
1559
1560 static bool cp_parser_translation_unit
1561   (cp_parser *);
1562
1563 /* Expressions [gram.expr]  */
1564
1565 static tree cp_parser_primary_expression
1566   (cp_parser *, bool, bool, bool, cp_id_kind *);
1567 static tree cp_parser_id_expression
1568   (cp_parser *, bool, bool, bool *, bool, bool);
1569 static tree cp_parser_unqualified_id
1570   (cp_parser *, bool, bool, bool, bool);
1571 static tree cp_parser_nested_name_specifier_opt
1572   (cp_parser *, bool, bool, bool, bool);
1573 static tree cp_parser_nested_name_specifier
1574   (cp_parser *, bool, bool, bool, bool);
1575 static tree cp_parser_class_or_namespace_name
1576   (cp_parser *, bool, bool, bool, bool, bool);
1577 static tree cp_parser_postfix_expression
1578   (cp_parser *, bool, bool, bool);
1579 static tree cp_parser_postfix_open_square_expression
1580   (cp_parser *, tree, bool);
1581 static tree cp_parser_postfix_dot_deref_expression
1582   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1583 static tree cp_parser_parenthesized_expression_list
1584   (cp_parser *, bool, bool, bool, bool *);
1585 static void cp_parser_pseudo_destructor_name
1586   (cp_parser *, tree *, tree *);
1587 static tree cp_parser_unary_expression
1588   (cp_parser *, bool, bool);
1589 static enum tree_code cp_parser_unary_operator
1590   (cp_token *);
1591 static tree cp_parser_new_expression
1592   (cp_parser *);
1593 static tree cp_parser_new_placement
1594   (cp_parser *);
1595 static tree cp_parser_new_type_id
1596   (cp_parser *, tree *);
1597 static cp_declarator *cp_parser_new_declarator_opt
1598   (cp_parser *);
1599 static cp_declarator *cp_parser_direct_new_declarator
1600   (cp_parser *);
1601 static tree cp_parser_new_initializer
1602   (cp_parser *);
1603 static tree cp_parser_delete_expression
1604   (cp_parser *);
1605 static tree cp_parser_cast_expression
1606   (cp_parser *, bool, bool);
1607 static tree cp_parser_binary_expression
1608   (cp_parser *, bool);
1609 static tree cp_parser_question_colon_clause
1610   (cp_parser *, tree);
1611 static tree cp_parser_assignment_expression
1612   (cp_parser *, bool);
1613 static enum tree_code cp_parser_assignment_operator_opt
1614   (cp_parser *);
1615 static tree cp_parser_expression
1616   (cp_parser *, bool);
1617 static tree cp_parser_constant_expression
1618   (cp_parser *, bool, bool *);
1619 static tree cp_parser_builtin_offsetof
1620   (cp_parser *);
1621
1622 /* Statements [gram.stmt.stmt]  */
1623
1624 static void cp_parser_statement
1625   (cp_parser *, tree, bool, bool *);
1626 static void cp_parser_label_for_labeled_statement
1627   (cp_parser *);
1628 static tree cp_parser_expression_statement
1629   (cp_parser *, tree);
1630 static tree cp_parser_compound_statement
1631   (cp_parser *, tree, bool);
1632 static void cp_parser_statement_seq_opt
1633   (cp_parser *, tree);
1634 static tree cp_parser_selection_statement
1635   (cp_parser *, bool *);
1636 static tree cp_parser_condition
1637   (cp_parser *);
1638 static tree cp_parser_iteration_statement
1639   (cp_parser *);
1640 static void cp_parser_for_init_statement
1641   (cp_parser *);
1642 static tree cp_parser_jump_statement
1643   (cp_parser *);
1644 static void cp_parser_declaration_statement
1645   (cp_parser *);
1646
1647 static tree cp_parser_implicitly_scoped_statement
1648   (cp_parser *, bool *);
1649 static void cp_parser_already_scoped_statement
1650   (cp_parser *);
1651
1652 /* Declarations [gram.dcl.dcl] */
1653
1654 static void cp_parser_declaration_seq_opt
1655   (cp_parser *);
1656 static void cp_parser_declaration
1657   (cp_parser *);
1658 static void cp_parser_block_declaration
1659   (cp_parser *, bool);
1660 static void cp_parser_simple_declaration
1661   (cp_parser *, bool);
1662 static void cp_parser_decl_specifier_seq
1663   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1664 static tree cp_parser_storage_class_specifier_opt
1665   (cp_parser *);
1666 static tree cp_parser_function_specifier_opt
1667   (cp_parser *, cp_decl_specifier_seq *);
1668 static tree cp_parser_type_specifier
1669   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1670    int *, bool *);
1671 static tree cp_parser_simple_type_specifier
1672   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1673 static tree cp_parser_type_name
1674   (cp_parser *);
1675 static tree cp_parser_elaborated_type_specifier
1676   (cp_parser *, bool, bool);
1677 static tree cp_parser_enum_specifier
1678   (cp_parser *);
1679 static void cp_parser_enumerator_list
1680   (cp_parser *, tree);
1681 static void cp_parser_enumerator_definition
1682   (cp_parser *, tree);
1683 static tree cp_parser_namespace_name
1684   (cp_parser *);
1685 static void cp_parser_namespace_definition
1686   (cp_parser *);
1687 static void cp_parser_namespace_body
1688   (cp_parser *);
1689 static tree cp_parser_qualified_namespace_specifier
1690   (cp_parser *);
1691 static void cp_parser_namespace_alias_definition
1692   (cp_parser *);
1693 static bool cp_parser_using_declaration
1694   (cp_parser *, bool);
1695 static void cp_parser_using_directive
1696   (cp_parser *);
1697 static void cp_parser_asm_definition
1698   (cp_parser *);
1699 static void cp_parser_linkage_specification
1700   (cp_parser *);
1701 static void cp_parser_static_assert
1702   (cp_parser *, bool);
1703 static tree cp_parser_decltype
1704   (cp_parser *);
1705
1706 /* Declarators [gram.dcl.decl] */
1707
1708 static tree cp_parser_init_declarator
1709   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1710 static cp_declarator *cp_parser_declarator
1711   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1712 static cp_declarator *cp_parser_direct_declarator
1713   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1714 static enum tree_code cp_parser_ptr_operator
1715   (cp_parser *, tree *, cp_cv_quals *);
1716 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1717   (cp_parser *);
1718 static tree cp_parser_declarator_id
1719   (cp_parser *, bool);
1720 static tree cp_parser_type_id
1721   (cp_parser *);
1722 static void cp_parser_type_specifier_seq
1723   (cp_parser *, bool, cp_decl_specifier_seq *);
1724 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1725   (cp_parser *);
1726 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1727   (cp_parser *, bool *);
1728 static cp_parameter_declarator *cp_parser_parameter_declaration
1729   (cp_parser *, bool, bool *);
1730 static tree cp_parser_default_argument 
1731   (cp_parser *, bool);
1732 static void cp_parser_function_body
1733   (cp_parser *);
1734 static tree cp_parser_initializer
1735   (cp_parser *, bool *, bool *);
1736 static tree cp_parser_initializer_clause
1737   (cp_parser *, bool *);
1738 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1739   (cp_parser *, bool *);
1740
1741 static bool cp_parser_ctor_initializer_opt_and_function_body
1742   (cp_parser *);
1743
1744 /* Classes [gram.class] */
1745
1746 static tree cp_parser_class_name
1747   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1748 static tree cp_parser_class_specifier
1749   (cp_parser *);
1750 static tree cp_parser_class_head
1751   (cp_parser *, bool *, tree *, tree *);
1752 static enum tag_types cp_parser_class_key
1753   (cp_parser *);
1754 static void cp_parser_member_specification_opt
1755   (cp_parser *);
1756 static void cp_parser_member_declaration
1757   (cp_parser *);
1758 static tree cp_parser_pure_specifier
1759   (cp_parser *);
1760 static tree cp_parser_constant_initializer
1761   (cp_parser *);
1762
1763 /* Derived classes [gram.class.derived] */
1764
1765 static tree cp_parser_base_clause
1766   (cp_parser *);
1767 static tree cp_parser_base_specifier
1768   (cp_parser *);
1769
1770 /* Special member functions [gram.special] */
1771
1772 static tree cp_parser_conversion_function_id
1773   (cp_parser *);
1774 static tree cp_parser_conversion_type_id
1775   (cp_parser *);
1776 static cp_declarator *cp_parser_conversion_declarator_opt
1777   (cp_parser *);
1778 static bool cp_parser_ctor_initializer_opt
1779   (cp_parser *);
1780 static void cp_parser_mem_initializer_list
1781   (cp_parser *);
1782 static tree cp_parser_mem_initializer
1783   (cp_parser *);
1784 static tree cp_parser_mem_initializer_id
1785   (cp_parser *);
1786
1787 /* Overloading [gram.over] */
1788
1789 static tree cp_parser_operator_function_id
1790   (cp_parser *);
1791 static tree cp_parser_operator
1792   (cp_parser *);
1793
1794 /* Templates [gram.temp] */
1795
1796 static void cp_parser_template_declaration
1797   (cp_parser *, bool);
1798 static tree cp_parser_template_parameter_list
1799   (cp_parser *);
1800 static tree cp_parser_template_parameter
1801   (cp_parser *, bool *, bool *);
1802 static tree cp_parser_type_parameter
1803   (cp_parser *, bool *);
1804 static tree cp_parser_template_id
1805   (cp_parser *, bool, bool, bool);
1806 static tree cp_parser_template_name
1807   (cp_parser *, bool, bool, bool, bool *);
1808 static tree cp_parser_template_argument_list
1809   (cp_parser *);
1810 static tree cp_parser_template_argument
1811   (cp_parser *);
1812 static void cp_parser_explicit_instantiation
1813   (cp_parser *);
1814 static void cp_parser_explicit_specialization
1815   (cp_parser *);
1816
1817 /* Exception handling [gram.exception] */
1818
1819 static tree cp_parser_try_block
1820   (cp_parser *);
1821 static bool cp_parser_function_try_block
1822   (cp_parser *);
1823 static void cp_parser_handler_seq
1824   (cp_parser *);
1825 static void cp_parser_handler
1826   (cp_parser *);
1827 static tree cp_parser_exception_declaration
1828   (cp_parser *);
1829 static tree cp_parser_throw_expression
1830   (cp_parser *);
1831 static tree cp_parser_exception_specification_opt
1832   (cp_parser *);
1833 static tree cp_parser_type_id_list
1834   (cp_parser *);
1835
1836 /* GNU Extensions */
1837
1838 static tree cp_parser_asm_specification_opt
1839   (cp_parser *);
1840 static tree cp_parser_asm_operand_list
1841   (cp_parser *);
1842 static tree cp_parser_asm_clobber_list
1843   (cp_parser *);
1844 static tree cp_parser_attributes_opt
1845   (cp_parser *);
1846 static tree cp_parser_attribute_list
1847   (cp_parser *);
1848 static bool cp_parser_extension_opt
1849   (cp_parser *, int *);
1850 static void cp_parser_label_declaration
1851   (cp_parser *);
1852
1853 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1854 static bool cp_parser_pragma
1855   (cp_parser *, enum pragma_context);
1856
1857 /* Objective-C++ Productions */
1858
1859 static tree cp_parser_objc_message_receiver
1860   (cp_parser *);
1861 static tree cp_parser_objc_message_args
1862   (cp_parser *);
1863 static tree cp_parser_objc_message_expression
1864   (cp_parser *);
1865 static tree cp_parser_objc_encode_expression
1866   (cp_parser *);
1867 static tree cp_parser_objc_defs_expression
1868   (cp_parser *);
1869 static tree cp_parser_objc_protocol_expression
1870   (cp_parser *);
1871 static tree cp_parser_objc_selector_expression
1872   (cp_parser *);
1873 static tree cp_parser_objc_expression
1874   (cp_parser *);
1875 static bool cp_parser_objc_selector_p
1876   (enum cpp_ttype);
1877 static tree cp_parser_objc_selector
1878   (cp_parser *);
1879 static tree cp_parser_objc_protocol_refs_opt
1880   (cp_parser *);
1881 static void cp_parser_objc_declaration
1882   (cp_parser *);
1883 static tree cp_parser_objc_statement
1884   (cp_parser *);
1885
1886 /* Utility Routines */
1887
1888 static tree cp_parser_lookup_name
1889   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1890 static tree cp_parser_lookup_name_simple
1891   (cp_parser *, tree);
1892 static tree cp_parser_maybe_treat_template_as_class
1893   (tree, bool);
1894 static bool cp_parser_check_declarator_template_parameters
1895   (cp_parser *, cp_declarator *);
1896 static bool cp_parser_check_template_parameters
1897   (cp_parser *, unsigned);
1898 static tree cp_parser_simple_cast_expression
1899   (cp_parser *);
1900 static tree cp_parser_global_scope_opt
1901   (cp_parser *, bool);
1902 static bool cp_parser_constructor_declarator_p
1903   (cp_parser *, bool);
1904 static tree cp_parser_function_definition_from_specifiers_and_declarator
1905   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1906 static tree cp_parser_function_definition_after_declarator
1907   (cp_parser *, bool);
1908 static void cp_parser_template_declaration_after_export
1909   (cp_parser *, bool);
1910 static void cp_parser_perform_template_parameter_access_checks
1911   (VEC (deferred_access_check,gc)*);
1912 static tree cp_parser_single_declaration
1913   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1914 static tree cp_parser_functional_cast
1915   (cp_parser *, tree);
1916 static tree cp_parser_save_member_function_body
1917   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1918 static tree cp_parser_enclosed_template_argument_list
1919   (cp_parser *);
1920 static void cp_parser_save_default_args
1921   (cp_parser *, tree);
1922 static void cp_parser_late_parsing_for_member
1923   (cp_parser *, tree);
1924 static void cp_parser_late_parsing_default_args
1925   (cp_parser *, tree);
1926 static tree cp_parser_sizeof_operand
1927   (cp_parser *, enum rid);
1928 static tree cp_parser_trait_expr
1929   (cp_parser *, enum rid);
1930 static bool cp_parser_declares_only_class_p
1931   (cp_parser *);
1932 static void cp_parser_set_storage_class
1933   (cp_parser *, cp_decl_specifier_seq *, enum rid);
1934 static void cp_parser_set_decl_spec_type
1935   (cp_decl_specifier_seq *, tree, bool);
1936 static bool cp_parser_friend_p
1937   (const cp_decl_specifier_seq *);
1938 static cp_token *cp_parser_require
1939   (cp_parser *, enum cpp_ttype, const char *);
1940 static cp_token *cp_parser_require_keyword
1941   (cp_parser *, enum rid, const char *);
1942 static bool cp_parser_token_starts_function_definition_p
1943   (cp_token *);
1944 static bool cp_parser_next_token_starts_class_definition_p
1945   (cp_parser *);
1946 static bool cp_parser_next_token_ends_template_argument_p
1947   (cp_parser *);
1948 static bool cp_parser_nth_token_starts_template_argument_list_p
1949   (cp_parser *, size_t);
1950 static enum tag_types cp_parser_token_is_class_key
1951   (cp_token *);
1952 static void cp_parser_check_class_key
1953   (enum tag_types, tree type);
1954 static void cp_parser_check_access_in_redeclaration
1955   (tree type);
1956 static bool cp_parser_optional_template_keyword
1957   (cp_parser *);
1958 static void cp_parser_pre_parsed_nested_name_specifier
1959   (cp_parser *);
1960 static void cp_parser_cache_group
1961   (cp_parser *, enum cpp_ttype, unsigned);
1962 static void cp_parser_parse_tentatively
1963   (cp_parser *);
1964 static void cp_parser_commit_to_tentative_parse
1965   (cp_parser *);
1966 static void cp_parser_abort_tentative_parse
1967   (cp_parser *);
1968 static bool cp_parser_parse_definitely
1969   (cp_parser *);
1970 static inline bool cp_parser_parsing_tentatively
1971   (cp_parser *);
1972 static bool cp_parser_uncommitted_to_tentative_parse_p
1973   (cp_parser *);
1974 static void cp_parser_error
1975   (cp_parser *, const char *);
1976 static void cp_parser_name_lookup_error
1977   (cp_parser *, tree, tree, const char *);
1978 static bool cp_parser_simulate_error
1979   (cp_parser *);
1980 static bool cp_parser_check_type_definition
1981   (cp_parser *);
1982 static void cp_parser_check_for_definition_in_return_type
1983   (cp_declarator *, tree);
1984 static void cp_parser_check_for_invalid_template_id
1985   (cp_parser *, tree);
1986 static bool cp_parser_non_integral_constant_expression
1987   (cp_parser *, const char *);
1988 static void cp_parser_diagnose_invalid_type_name
1989   (cp_parser *, tree, tree);
1990 static bool cp_parser_parse_and_diagnose_invalid_type_name
1991   (cp_parser *);
1992 static int cp_parser_skip_to_closing_parenthesis
1993   (cp_parser *, bool, bool, bool);
1994 static void cp_parser_skip_to_end_of_statement
1995   (cp_parser *);
1996 static void cp_parser_consume_semicolon_at_end_of_statement
1997   (cp_parser *);
1998 static void cp_parser_skip_to_end_of_block_or_statement
1999   (cp_parser *);
2000 static bool cp_parser_skip_to_closing_brace
2001   (cp_parser *);
2002 static void cp_parser_skip_to_end_of_template_parameter_list
2003   (cp_parser *);
2004 static void cp_parser_skip_to_pragma_eol
2005   (cp_parser*, cp_token *);
2006 static bool cp_parser_error_occurred
2007   (cp_parser *);
2008 static bool cp_parser_allow_gnu_extensions_p
2009   (cp_parser *);
2010 static bool cp_parser_is_string_literal
2011   (cp_token *);
2012 static bool cp_parser_is_keyword
2013   (cp_token *, enum rid);
2014 static tree cp_parser_make_typename_type
2015   (cp_parser *, tree, tree);
2016 static cp_declarator * cp_parser_make_indirect_declarator
2017   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2018
2019 /* Returns nonzero if we are parsing tentatively.  */
2020
2021 static inline bool
2022 cp_parser_parsing_tentatively (cp_parser* parser)
2023 {
2024   return parser->context->next != NULL;
2025 }
2026
2027 /* Returns nonzero if TOKEN is a string literal.  */
2028
2029 static bool
2030 cp_parser_is_string_literal (cp_token* token)
2031 {
2032   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2033 }
2034
2035 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2036
2037 static bool
2038 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2039 {
2040   return token->keyword == keyword;
2041 }
2042
2043 /* If not parsing tentatively, issue a diagnostic of the form
2044       FILE:LINE: MESSAGE before TOKEN
2045    where TOKEN is the next token in the input stream.  MESSAGE
2046    (specified by the caller) is usually of the form "expected
2047    OTHER-TOKEN".  */
2048
2049 static void
2050 cp_parser_error (cp_parser* parser, const char* message)
2051 {
2052   if (!cp_parser_simulate_error (parser))
2053     {
2054       cp_token *token = cp_lexer_peek_token (parser->lexer);
2055       /* This diagnostic makes more sense if it is tagged to the line
2056          of the token we just peeked at.  */
2057       cp_lexer_set_source_position_from_token (token);
2058
2059       if (token->type == CPP_PRAGMA)
2060         {
2061           error ("%<#pragma%> is not allowed here");
2062           cp_parser_skip_to_pragma_eol (parser, token);
2063           return;
2064         }
2065
2066       c_parse_error (message,
2067                      /* Because c_parser_error does not understand
2068                         CPP_KEYWORD, keywords are treated like
2069                         identifiers.  */
2070                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2071                      token->u.value);
2072     }
2073 }
2074
2075 /* Issue an error about name-lookup failing.  NAME is the
2076    IDENTIFIER_NODE DECL is the result of
2077    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2078    the thing that we hoped to find.  */
2079
2080 static void
2081 cp_parser_name_lookup_error (cp_parser* parser,
2082                              tree name,
2083                              tree decl,
2084                              const char* desired)
2085 {
2086   /* If name lookup completely failed, tell the user that NAME was not
2087      declared.  */
2088   if (decl == error_mark_node)
2089     {
2090       if (parser->scope && parser->scope != global_namespace)
2091         error ("%<%E::%E%> has not been declared",
2092                parser->scope, name);
2093       else if (parser->scope == global_namespace)
2094         error ("%<::%E%> has not been declared", name);
2095       else if (parser->object_scope
2096                && !CLASS_TYPE_P (parser->object_scope))
2097         error ("request for member %qE in non-class type %qT",
2098                name, parser->object_scope);
2099       else if (parser->object_scope)
2100         error ("%<%T::%E%> has not been declared",
2101                parser->object_scope, name);
2102       else
2103         error ("%qE has not been declared", name);
2104     }
2105   else if (parser->scope && parser->scope != global_namespace)
2106     error ("%<%E::%E%> %s", parser->scope, name, desired);
2107   else if (parser->scope == global_namespace)
2108     error ("%<::%E%> %s", name, desired);
2109   else
2110     error ("%qE %s", name, desired);
2111 }
2112
2113 /* If we are parsing tentatively, remember that an error has occurred
2114    during this tentative parse.  Returns true if the error was
2115    simulated; false if a message should be issued by the caller.  */
2116
2117 static bool
2118 cp_parser_simulate_error (cp_parser* parser)
2119 {
2120   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2121     {
2122       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2123       return true;
2124     }
2125   return false;
2126 }
2127
2128 /* Check for repeated decl-specifiers.  */
2129
2130 static void
2131 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2132 {
2133   cp_decl_spec ds;
2134
2135   for (ds = ds_first; ds != ds_last; ++ds)
2136     {
2137       unsigned count = decl_specs->specs[(int)ds];
2138       if (count < 2)
2139         continue;
2140       /* The "long" specifier is a special case because of "long long".  */
2141       if (ds == ds_long)
2142         {
2143           if (count > 2)
2144             error ("%<long long long%> is too long for GCC");
2145           else if (pedantic && !in_system_header && warn_long_long
2146                    && cxx_dialect == cxx98)
2147             pedwarn ("ISO C++ 1998 does not support %<long long%>");
2148         }
2149       else if (count > 1)
2150         {
2151           static const char *const decl_spec_names[] = {
2152             "signed",
2153             "unsigned",
2154             "short",
2155             "long",
2156             "const",
2157             "volatile",
2158             "restrict",
2159             "inline",
2160             "virtual",
2161             "explicit",
2162             "friend",
2163             "typedef",
2164             "__complex",
2165             "__thread"
2166           };
2167           error ("duplicate %qs", decl_spec_names[(int)ds]);
2168         }
2169     }
2170 }
2171
2172 /* This function is called when a type is defined.  If type
2173    definitions are forbidden at this point, an error message is
2174    issued.  */
2175
2176 static bool
2177 cp_parser_check_type_definition (cp_parser* parser)
2178 {
2179   /* If types are forbidden here, issue a message.  */
2180   if (parser->type_definition_forbidden_message)
2181     {
2182       /* Use `%s' to print the string in case there are any escape
2183          characters in the message.  */
2184       error ("%s", parser->type_definition_forbidden_message);
2185       return false;
2186     }
2187   return true;
2188 }
2189
2190 /* This function is called when the DECLARATOR is processed.  The TYPE
2191    was a type defined in the decl-specifiers.  If it is invalid to
2192    define a type in the decl-specifiers for DECLARATOR, an error is
2193    issued.  */
2194
2195 static void
2196 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2197                                                tree type)
2198 {
2199   /* [dcl.fct] forbids type definitions in return types.
2200      Unfortunately, it's not easy to know whether or not we are
2201      processing a return type until after the fact.  */
2202   while (declarator
2203          && (declarator->kind == cdk_pointer
2204              || declarator->kind == cdk_reference
2205              || declarator->kind == cdk_ptrmem))
2206     declarator = declarator->declarator;
2207   if (declarator
2208       && declarator->kind == cdk_function)
2209     {
2210       error ("new types may not be defined in a return type");
2211       inform ("(perhaps a semicolon is missing after the definition of %qT)",
2212               type);
2213     }
2214 }
2215
2216 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2217    "<" in any valid C++ program.  If the next token is indeed "<",
2218    issue a message warning the user about what appears to be an
2219    invalid attempt to form a template-id.  */
2220
2221 static void
2222 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2223                                          tree type)
2224 {
2225   cp_token_position start = 0;
2226
2227   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2228     {
2229       if (TYPE_P (type))
2230         error ("%qT is not a template", type);
2231       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2232         error ("%qE is not a template", type);
2233       else
2234         error ("invalid template-id");
2235       /* Remember the location of the invalid "<".  */
2236       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2237         start = cp_lexer_token_position (parser->lexer, true);
2238       /* Consume the "<".  */
2239       cp_lexer_consume_token (parser->lexer);
2240       /* Parse the template arguments.  */
2241       cp_parser_enclosed_template_argument_list (parser);
2242       /* Permanently remove the invalid template arguments so that
2243          this error message is not issued again.  */
2244       if (start)
2245         cp_lexer_purge_tokens_after (parser->lexer, start);
2246     }
2247 }
2248
2249 /* If parsing an integral constant-expression, issue an error message
2250    about the fact that THING appeared and return true.  Otherwise,
2251    return false.  In either case, set
2252    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2253
2254 static bool
2255 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2256                                             const char *thing)
2257 {
2258   parser->non_integral_constant_expression_p = true;
2259   if (parser->integral_constant_expression_p)
2260     {
2261       if (!parser->allow_non_integral_constant_expression_p)
2262         {
2263           error ("%s cannot appear in a constant-expression", thing);
2264           return true;
2265         }
2266     }
2267   return false;
2268 }
2269
2270 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2271    qualifying scope (or NULL, if none) for ID.  This function commits
2272    to the current active tentative parse, if any.  (Otherwise, the
2273    problematic construct might be encountered again later, resulting
2274    in duplicate error messages.)  */
2275
2276 static void
2277 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2278 {
2279   tree decl, old_scope;
2280   /* Try to lookup the identifier.  */
2281   old_scope = parser->scope;
2282   parser->scope = scope;
2283   decl = cp_parser_lookup_name_simple (parser, id);
2284   parser->scope = old_scope;
2285   /* If the lookup found a template-name, it means that the user forgot
2286   to specify an argument list. Emit a useful error message.  */
2287   if (TREE_CODE (decl) == TEMPLATE_DECL)
2288     error ("invalid use of template-name %qE without an argument list", decl);
2289   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2290     error ("invalid use of destructor %qD as a type", id);
2291   else if (TREE_CODE (decl) == TYPE_DECL)
2292     /* Something like 'unsigned A a;'  */
2293     error ("invalid combination of multiple type-specifiers");
2294   else if (!parser->scope)
2295     {
2296       /* Issue an error message.  */
2297       error ("%qE does not name a type", id);
2298       /* If we're in a template class, it's possible that the user was
2299          referring to a type from a base class.  For example:
2300
2301            template <typename T> struct A { typedef T X; };
2302            template <typename T> struct B : public A<T> { X x; };
2303
2304          The user should have said "typename A<T>::X".  */
2305       if (processing_template_decl && current_class_type
2306           && TYPE_BINFO (current_class_type))
2307         {
2308           tree b;
2309
2310           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2311                b;
2312                b = TREE_CHAIN (b))
2313             {
2314               tree base_type = BINFO_TYPE (b);
2315               if (CLASS_TYPE_P (base_type)
2316                   && dependent_type_p (base_type))
2317                 {
2318                   tree field;
2319                   /* Go from a particular instantiation of the
2320                      template (which will have an empty TYPE_FIELDs),
2321                      to the main version.  */
2322                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2323                   for (field = TYPE_FIELDS (base_type);
2324                        field;
2325                        field = TREE_CHAIN (field))
2326                     if (TREE_CODE (field) == TYPE_DECL
2327                         && DECL_NAME (field) == id)
2328                       {
2329                         inform ("(perhaps %<typename %T::%E%> was intended)",
2330                                 BINFO_TYPE (b), id);
2331                         break;
2332                       }
2333                   if (field)
2334                     break;
2335                 }
2336             }
2337         }
2338     }
2339   /* Here we diagnose qualified-ids where the scope is actually correct,
2340      but the identifier does not resolve to a valid type name.  */
2341   else if (parser->scope != error_mark_node)
2342     {
2343       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2344         error ("%qE in namespace %qE does not name a type",
2345                id, parser->scope);
2346       else if (TYPE_P (parser->scope))
2347         error ("%qE in class %qT does not name a type", id, parser->scope);
2348       else
2349         gcc_unreachable ();
2350     }
2351   cp_parser_commit_to_tentative_parse (parser);
2352 }
2353
2354 /* Check for a common situation where a type-name should be present,
2355    but is not, and issue a sensible error message.  Returns true if an
2356    invalid type-name was detected.
2357
2358    The situation handled by this function are variable declarations of the
2359    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2360    Usually, `ID' should name a type, but if we got here it means that it
2361    does not. We try to emit the best possible error message depending on
2362    how exactly the id-expression looks like.  */
2363
2364 static bool
2365 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2366 {
2367   tree id;
2368
2369   cp_parser_parse_tentatively (parser);
2370   id = cp_parser_id_expression (parser,
2371                                 /*template_keyword_p=*/false,
2372                                 /*check_dependency_p=*/true,
2373                                 /*template_p=*/NULL,
2374                                 /*declarator_p=*/true,
2375                                 /*optional_p=*/false);
2376   /* After the id-expression, there should be a plain identifier,
2377      otherwise this is not a simple variable declaration. Also, if
2378      the scope is dependent, we cannot do much.  */
2379   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2380       || (parser->scope && TYPE_P (parser->scope)
2381           && dependent_type_p (parser->scope))
2382       || TREE_CODE (id) == TYPE_DECL)
2383     {
2384       cp_parser_abort_tentative_parse (parser);
2385       return false;
2386     }
2387   if (!cp_parser_parse_definitely (parser))
2388     return false;
2389
2390   /* Emit a diagnostic for the invalid type.  */
2391   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2392   /* Skip to the end of the declaration; there's no point in
2393      trying to process it.  */
2394   cp_parser_skip_to_end_of_block_or_statement (parser);
2395   return true;
2396 }
2397
2398 /* Consume tokens up to, and including, the next non-nested closing `)'.
2399    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2400    are doing error recovery. Returns -1 if OR_COMMA is true and we
2401    found an unnested comma.  */
2402
2403 static int
2404 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2405                                        bool recovering,
2406                                        bool or_comma,
2407                                        bool consume_paren)
2408 {
2409   unsigned paren_depth = 0;
2410   unsigned brace_depth = 0;
2411
2412   if (recovering && !or_comma
2413       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2414     return 0;
2415
2416   while (true)
2417     {
2418       cp_token * token = cp_lexer_peek_token (parser->lexer);
2419
2420       switch (token->type)
2421         {
2422         case CPP_EOF:
2423         case CPP_PRAGMA_EOL:
2424           /* If we've run out of tokens, then there is no closing `)'.  */
2425           return 0;
2426
2427         case CPP_SEMICOLON:
2428           /* This matches the processing in skip_to_end_of_statement.  */
2429           if (!brace_depth)
2430             return 0;
2431           break;
2432
2433         case CPP_OPEN_BRACE:
2434           ++brace_depth;
2435           break;
2436         case CPP_CLOSE_BRACE:
2437           if (!brace_depth--)
2438             return 0;
2439           break;
2440
2441         case CPP_COMMA:
2442           if (recovering && or_comma && !brace_depth && !paren_depth)
2443             return -1;
2444           break;
2445
2446         case CPP_OPEN_PAREN:
2447           if (!brace_depth)
2448             ++paren_depth;
2449           break;
2450
2451         case CPP_CLOSE_PAREN:
2452           if (!brace_depth && !paren_depth--)
2453             {
2454               if (consume_paren)
2455                 cp_lexer_consume_token (parser->lexer);
2456               return 1;
2457             }
2458           break;
2459
2460         default:
2461           break;
2462         }
2463
2464       /* Consume the token.  */
2465       cp_lexer_consume_token (parser->lexer);
2466     }
2467 }
2468
2469 /* Consume tokens until we reach the end of the current statement.
2470    Normally, that will be just before consuming a `;'.  However, if a
2471    non-nested `}' comes first, then we stop before consuming that.  */
2472
2473 static void
2474 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2475 {
2476   unsigned nesting_depth = 0;
2477
2478   while (true)
2479     {
2480       cp_token *token = cp_lexer_peek_token (parser->lexer);
2481
2482       switch (token->type)
2483         {
2484         case CPP_EOF:
2485         case CPP_PRAGMA_EOL:
2486           /* If we've run out of tokens, stop.  */
2487           return;
2488
2489         case CPP_SEMICOLON:
2490           /* If the next token is a `;', we have reached the end of the
2491              statement.  */
2492           if (!nesting_depth)
2493             return;
2494           break;
2495
2496         case CPP_CLOSE_BRACE:
2497           /* If this is a non-nested '}', stop before consuming it.
2498              That way, when confronted with something like:
2499
2500                { 3 + }
2501
2502              we stop before consuming the closing '}', even though we
2503              have not yet reached a `;'.  */
2504           if (nesting_depth == 0)
2505             return;
2506
2507           /* If it is the closing '}' for a block that we have
2508              scanned, stop -- but only after consuming the token.
2509              That way given:
2510
2511                 void f g () { ... }
2512                 typedef int I;
2513
2514              we will stop after the body of the erroneously declared
2515              function, but before consuming the following `typedef'
2516              declaration.  */
2517           if (--nesting_depth == 0)
2518             {
2519               cp_lexer_consume_token (parser->lexer);
2520               return;
2521             }
2522
2523         case CPP_OPEN_BRACE:
2524           ++nesting_depth;
2525           break;
2526
2527         default:
2528           break;
2529         }
2530
2531       /* Consume the token.  */
2532       cp_lexer_consume_token (parser->lexer);
2533     }
2534 }
2535
2536 /* This function is called at the end of a statement or declaration.
2537    If the next token is a semicolon, it is consumed; otherwise, error
2538    recovery is attempted.  */
2539
2540 static void
2541 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2542 {
2543   /* Look for the trailing `;'.  */
2544   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2545     {
2546       /* If there is additional (erroneous) input, skip to the end of
2547          the statement.  */
2548       cp_parser_skip_to_end_of_statement (parser);
2549       /* If the next token is now a `;', consume it.  */
2550       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2551         cp_lexer_consume_token (parser->lexer);
2552     }
2553 }
2554
2555 /* Skip tokens until we have consumed an entire block, or until we
2556    have consumed a non-nested `;'.  */
2557
2558 static void
2559 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2560 {
2561   int nesting_depth = 0;
2562
2563   while (nesting_depth >= 0)
2564     {
2565       cp_token *token = cp_lexer_peek_token (parser->lexer);
2566
2567       switch (token->type)
2568         {
2569         case CPP_EOF:
2570         case CPP_PRAGMA_EOL:
2571           /* If we've run out of tokens, stop.  */
2572           return;
2573
2574         case CPP_SEMICOLON:
2575           /* Stop if this is an unnested ';'. */
2576           if (!nesting_depth)
2577             nesting_depth = -1;
2578           break;
2579
2580         case CPP_CLOSE_BRACE:
2581           /* Stop if this is an unnested '}', or closes the outermost
2582              nesting level.  */
2583           nesting_depth--;
2584           if (!nesting_depth)
2585             nesting_depth = -1;
2586           break;
2587
2588         case CPP_OPEN_BRACE:
2589           /* Nest. */
2590           nesting_depth++;
2591           break;
2592
2593         default:
2594           break;
2595         }
2596
2597       /* Consume the token.  */
2598       cp_lexer_consume_token (parser->lexer);
2599     }
2600 }
2601
2602 /* Skip tokens until a non-nested closing curly brace is the next
2603    token, or there are no more tokens. Return true in the first case,
2604    false otherwise.  */
2605
2606 static bool
2607 cp_parser_skip_to_closing_brace (cp_parser *parser)
2608 {
2609   unsigned nesting_depth = 0;
2610
2611   while (true)
2612     {
2613       cp_token *token = cp_lexer_peek_token (parser->lexer);
2614
2615       switch (token->type)
2616         {
2617         case CPP_EOF:
2618         case CPP_PRAGMA_EOL:
2619           /* If we've run out of tokens, stop.  */
2620           return false;
2621
2622         case CPP_CLOSE_BRACE:
2623           /* If the next token is a non-nested `}', then we have reached
2624              the end of the current block.  */
2625           if (nesting_depth-- == 0)
2626             return true;
2627           break;
2628
2629         case CPP_OPEN_BRACE:
2630           /* If it the next token is a `{', then we are entering a new
2631              block.  Consume the entire block.  */
2632           ++nesting_depth;
2633           break;
2634
2635         default:
2636           break;
2637         }
2638
2639       /* Consume the token.  */
2640       cp_lexer_consume_token (parser->lexer);
2641     }
2642 }
2643
2644 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2645    parameter is the PRAGMA token, allowing us to purge the entire pragma
2646    sequence.  */
2647
2648 static void
2649 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2650 {
2651   cp_token *token;
2652
2653   parser->lexer->in_pragma = false;
2654
2655   do
2656     token = cp_lexer_consume_token (parser->lexer);
2657   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2658
2659   /* Ensure that the pragma is not parsed again.  */
2660   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2661 }
2662
2663 /* Require pragma end of line, resyncing with it as necessary.  The
2664    arguments are as for cp_parser_skip_to_pragma_eol.  */
2665
2666 static void
2667 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2668 {
2669   parser->lexer->in_pragma = false;
2670   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2671     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2672 }
2673
2674 /* This is a simple wrapper around make_typename_type. When the id is
2675    an unresolved identifier node, we can provide a superior diagnostic
2676    using cp_parser_diagnose_invalid_type_name.  */
2677
2678 static tree
2679 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2680 {
2681   tree result;
2682   if (TREE_CODE (id) == IDENTIFIER_NODE)
2683     {
2684       result = make_typename_type (scope, id, typename_type,
2685                                    /*complain=*/tf_none);
2686       if (result == error_mark_node)
2687         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2688       return result;
2689     }
2690   return make_typename_type (scope, id, typename_type, tf_error);
2691 }
2692
2693 /* This is a wrapper around the
2694    make_{pointer,ptrmem,reference}_declarator functions that decides
2695    which one to call based on the CODE and CLASS_TYPE arguments. The
2696    CODE argument should be one of the values returned by
2697    cp_parser_ptr_operator. */
2698 static cp_declarator *
2699 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2700                                     cp_cv_quals cv_qualifiers,
2701                                     cp_declarator *target)
2702 {
2703   if (code == ERROR_MARK)
2704     return cp_error_declarator;
2705
2706   if (code == INDIRECT_REF)
2707     if (class_type == NULL_TREE)
2708       return make_pointer_declarator (cv_qualifiers, target);
2709     else
2710       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2711   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2712     return make_reference_declarator (cv_qualifiers, target, false);
2713   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2714     return make_reference_declarator (cv_qualifiers, target, true);
2715   gcc_unreachable ();
2716 }
2717
2718 /* Create a new C++ parser.  */
2719
2720 static cp_parser *
2721 cp_parser_new (void)
2722 {
2723   cp_parser *parser;
2724   cp_lexer *lexer;
2725   unsigned i;
2726
2727   /* cp_lexer_new_main is called before calling ggc_alloc because
2728      cp_lexer_new_main might load a PCH file.  */
2729   lexer = cp_lexer_new_main ();
2730
2731   /* Initialize the binops_by_token so that we can get the tree
2732      directly from the token.  */
2733   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2734     binops_by_token[binops[i].token_type] = binops[i];
2735
2736   parser = GGC_CNEW (cp_parser);
2737   parser->lexer = lexer;
2738   parser->context = cp_parser_context_new (NULL);
2739
2740   /* For now, we always accept GNU extensions.  */
2741   parser->allow_gnu_extensions_p = 1;
2742
2743   /* The `>' token is a greater-than operator, not the end of a
2744      template-id.  */
2745   parser->greater_than_is_operator_p = true;
2746
2747   parser->default_arg_ok_p = true;
2748
2749   /* We are not parsing a constant-expression.  */
2750   parser->integral_constant_expression_p = false;
2751   parser->allow_non_integral_constant_expression_p = false;
2752   parser->non_integral_constant_expression_p = false;
2753
2754   /* Local variable names are not forbidden.  */
2755   parser->local_variables_forbidden_p = false;
2756
2757   /* We are not processing an `extern "C"' declaration.  */
2758   parser->in_unbraced_linkage_specification_p = false;
2759
2760   /* We are not processing a declarator.  */
2761   parser->in_declarator_p = false;
2762
2763   /* We are not processing a template-argument-list.  */
2764   parser->in_template_argument_list_p = false;
2765
2766   /* We are not in an iteration statement.  */
2767   parser->in_statement = 0;
2768
2769   /* We are not in a switch statement.  */
2770   parser->in_switch_statement_p = false;
2771
2772   /* We are not parsing a type-id inside an expression.  */
2773   parser->in_type_id_in_expr_p = false;
2774
2775   /* Declarations aren't implicitly extern "C".  */
2776   parser->implicit_extern_c = false;
2777
2778   /* String literals should be translated to the execution character set.  */
2779   parser->translate_strings_p = true;
2780
2781   /* We are not parsing a function body.  */
2782   parser->in_function_body = false;
2783
2784   /* The unparsed function queue is empty.  */
2785   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2786
2787   /* There are no classes being defined.  */
2788   parser->num_classes_being_defined = 0;
2789
2790   /* No template parameters apply.  */
2791   parser->num_template_parameter_lists = 0;
2792
2793   return parser;
2794 }
2795
2796 /* Create a cp_lexer structure which will emit the tokens in CACHE
2797    and push it onto the parser's lexer stack.  This is used for delayed
2798    parsing of in-class method bodies and default arguments, and should
2799    not be confused with tentative parsing.  */
2800 static void
2801 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2802 {
2803   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2804   lexer->next = parser->lexer;
2805   parser->lexer = lexer;
2806
2807   /* Move the current source position to that of the first token in the
2808      new lexer.  */
2809   cp_lexer_set_source_position_from_token (lexer->next_token);
2810 }
2811
2812 /* Pop the top lexer off the parser stack.  This is never used for the
2813    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2814 static void
2815 cp_parser_pop_lexer (cp_parser *parser)
2816 {
2817   cp_lexer *lexer = parser->lexer;
2818   parser->lexer = lexer->next;
2819   cp_lexer_destroy (lexer);
2820
2821   /* Put the current source position back where it was before this
2822      lexer was pushed.  */
2823   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2824 }
2825
2826 /* Lexical conventions [gram.lex]  */
2827
2828 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2829    identifier.  */
2830
2831 static tree
2832 cp_parser_identifier (cp_parser* parser)
2833 {
2834   cp_token *token;
2835
2836   /* Look for the identifier.  */
2837   token = cp_parser_require (parser, CPP_NAME, "identifier");
2838   /* Return the value.  */
2839   return token ? token->u.value : error_mark_node;
2840 }
2841
2842 /* Parse a sequence of adjacent string constants.  Returns a
2843    TREE_STRING representing the combined, nul-terminated string
2844    constant.  If TRANSLATE is true, translate the string to the
2845    execution character set.  If WIDE_OK is true, a wide string is
2846    invalid here.
2847
2848    C++98 [lex.string] says that if a narrow string literal token is
2849    adjacent to a wide string literal token, the behavior is undefined.
2850    However, C99 6.4.5p4 says that this results in a wide string literal.
2851    We follow C99 here, for consistency with the C front end.
2852
2853    This code is largely lifted from lex_string() in c-lex.c.
2854
2855    FUTURE: ObjC++ will need to handle @-strings here.  */
2856 static tree
2857 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2858 {
2859   tree value;
2860   bool wide = false;
2861   size_t count;
2862   struct obstack str_ob;
2863   cpp_string str, istr, *strs;
2864   cp_token *tok;
2865
2866   tok = cp_lexer_peek_token (parser->lexer);
2867   if (!cp_parser_is_string_literal (tok))
2868     {
2869       cp_parser_error (parser, "expected string-literal");
2870       return error_mark_node;
2871     }
2872
2873   /* Try to avoid the overhead of creating and destroying an obstack
2874      for the common case of just one string.  */
2875   if (!cp_parser_is_string_literal
2876       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2877     {
2878       cp_lexer_consume_token (parser->lexer);
2879
2880       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2881       str.len = TREE_STRING_LENGTH (tok->u.value);
2882       count = 1;
2883       if (tok->type == CPP_WSTRING)
2884         wide = true;
2885
2886       strs = &str;
2887     }
2888   else
2889     {
2890       gcc_obstack_init (&str_ob);
2891       count = 0;
2892
2893       do
2894         {
2895           cp_lexer_consume_token (parser->lexer);
2896           count++;
2897           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2898           str.len = TREE_STRING_LENGTH (tok->u.value);
2899           if (tok->type == CPP_WSTRING)
2900             wide = true;
2901
2902           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2903
2904           tok = cp_lexer_peek_token (parser->lexer);
2905         }
2906       while (cp_parser_is_string_literal (tok));
2907
2908       strs = (cpp_string *) obstack_finish (&str_ob);
2909     }
2910
2911   if (wide && !wide_ok)
2912     {
2913       cp_parser_error (parser, "a wide string is invalid in this context");
2914       wide = false;
2915     }
2916
2917   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2918       (parse_in, strs, count, &istr, wide))
2919     {
2920       value = build_string (istr.len, (const char *)istr.text);
2921       free (CONST_CAST (unsigned char *, istr.text));
2922
2923       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2924       value = fix_string_type (value);
2925     }
2926   else
2927     /* cpp_interpret_string has issued an error.  */
2928     value = error_mark_node;
2929
2930   if (count > 1)
2931     obstack_free (&str_ob, 0);
2932
2933   return value;
2934 }
2935
2936
2937 /* Basic concepts [gram.basic]  */
2938
2939 /* Parse a translation-unit.
2940
2941    translation-unit:
2942      declaration-seq [opt]
2943
2944    Returns TRUE if all went well.  */
2945
2946 static bool
2947 cp_parser_translation_unit (cp_parser* parser)
2948 {
2949   /* The address of the first non-permanent object on the declarator
2950      obstack.  */
2951   static void *declarator_obstack_base;
2952
2953   bool success;
2954
2955   /* Create the declarator obstack, if necessary.  */
2956   if (!cp_error_declarator)
2957     {
2958       gcc_obstack_init (&declarator_obstack);
2959       /* Create the error declarator.  */
2960       cp_error_declarator = make_declarator (cdk_error);
2961       /* Create the empty parameter list.  */
2962       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2963       /* Remember where the base of the declarator obstack lies.  */
2964       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2965     }
2966
2967   cp_parser_declaration_seq_opt (parser);
2968
2969   /* If there are no tokens left then all went well.  */
2970   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2971     {
2972       /* Get rid of the token array; we don't need it any more.  */
2973       cp_lexer_destroy (parser->lexer);
2974       parser->lexer = NULL;
2975
2976       /* This file might have been a context that's implicitly extern
2977          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2978       if (parser->implicit_extern_c)
2979         {
2980           pop_lang_context ();
2981           parser->implicit_extern_c = false;
2982         }
2983
2984       /* Finish up.  */
2985       finish_translation_unit ();
2986
2987       success = true;
2988     }
2989   else
2990     {
2991       cp_parser_error (parser, "expected declaration");
2992       success = false;
2993     }
2994
2995   /* Make sure the declarator obstack was fully cleaned up.  */
2996   gcc_assert (obstack_next_free (&declarator_obstack)
2997               == declarator_obstack_base);
2998
2999   /* All went well.  */
3000   return success;
3001 }
3002
3003 /* Expressions [gram.expr] */
3004
3005 /* Parse a primary-expression.
3006
3007    primary-expression:
3008      literal
3009      this
3010      ( expression )
3011      id-expression
3012
3013    GNU Extensions:
3014
3015    primary-expression:
3016      ( compound-statement )
3017      __builtin_va_arg ( assignment-expression , type-id )
3018      __builtin_offsetof ( type-id , offsetof-expression )
3019
3020    C++ Extensions:
3021      __has_nothrow_assign ( type-id )   
3022      __has_nothrow_constructor ( type-id )
3023      __has_nothrow_copy ( type-id )
3024      __has_trivial_assign ( type-id )   
3025      __has_trivial_constructor ( type-id )
3026      __has_trivial_copy ( type-id )
3027      __has_trivial_destructor ( type-id )
3028      __has_virtual_destructor ( type-id )     
3029      __is_abstract ( type-id )
3030      __is_base_of ( type-id , type-id )
3031      __is_class ( type-id )
3032      __is_convertible_to ( type-id , type-id )     
3033      __is_empty ( type-id )
3034      __is_enum ( type-id )
3035      __is_pod ( type-id )
3036      __is_polymorphic ( type-id )
3037      __is_union ( type-id )
3038
3039    Objective-C++ Extension:
3040
3041    primary-expression:
3042      objc-expression
3043
3044    literal:
3045      __null
3046
3047    ADDRESS_P is true iff this expression was immediately preceded by
3048    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3049    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3050    true iff this expression is a template argument.
3051
3052    Returns a representation of the expression.  Upon return, *IDK
3053    indicates what kind of id-expression (if any) was present.  */
3054
3055 static tree
3056 cp_parser_primary_expression (cp_parser *parser,
3057                               bool address_p,
3058                               bool cast_p,
3059                               bool template_arg_p,
3060                               cp_id_kind *idk)
3061 {
3062   cp_token *token;
3063
3064   /* Assume the primary expression is not an id-expression.  */
3065   *idk = CP_ID_KIND_NONE;
3066
3067   /* Peek at the next token.  */
3068   token = cp_lexer_peek_token (parser->lexer);
3069   switch (token->type)
3070     {
3071       /* literal:
3072            integer-literal
3073            character-literal
3074            floating-literal
3075            string-literal
3076            boolean-literal  */
3077     case CPP_CHAR:
3078     case CPP_WCHAR:
3079     case CPP_NUMBER:
3080       token = cp_lexer_consume_token (parser->lexer);
3081       /* Floating-point literals are only allowed in an integral
3082          constant expression if they are cast to an integral or
3083          enumeration type.  */
3084       if (TREE_CODE (token->u.value) == REAL_CST
3085           && parser->integral_constant_expression_p
3086           && pedantic)
3087         {
3088           /* CAST_P will be set even in invalid code like "int(2.7 +
3089              ...)".   Therefore, we have to check that the next token
3090              is sure to end the cast.  */
3091           if (cast_p)
3092             {
3093               cp_token *next_token;
3094
3095               next_token = cp_lexer_peek_token (parser->lexer);
3096               if (/* The comma at the end of an
3097                      enumerator-definition.  */
3098                   next_token->type != CPP_COMMA
3099                   /* The curly brace at the end of an enum-specifier.  */
3100                   && next_token->type != CPP_CLOSE_BRACE
3101                   /* The end of a statement.  */
3102                   && next_token->type != CPP_SEMICOLON
3103                   /* The end of the cast-expression.  */
3104                   && next_token->type != CPP_CLOSE_PAREN
3105                   /* The end of an array bound.  */
3106                   && next_token->type != CPP_CLOSE_SQUARE
3107                   /* The closing ">" in a template-argument-list.  */
3108                   && (next_token->type != CPP_GREATER
3109                       || parser->greater_than_is_operator_p)
3110                   /* C++0x only: A ">>" treated like two ">" tokens,
3111                      in a template-argument-list.  */
3112                   && (next_token->type != CPP_RSHIFT
3113                       || (cxx_dialect == cxx98)
3114                       || parser->greater_than_is_operator_p))
3115                 cast_p = false;
3116             }
3117
3118           /* If we are within a cast, then the constraint that the
3119              cast is to an integral or enumeration type will be
3120              checked at that point.  If we are not within a cast, then
3121              this code is invalid.  */
3122           if (!cast_p)
3123             cp_parser_non_integral_constant_expression
3124               (parser, "floating-point literal");
3125         }
3126       return token->u.value;
3127
3128     case CPP_STRING:
3129     case CPP_WSTRING:
3130       /* ??? Should wide strings be allowed when parser->translate_strings_p
3131          is false (i.e. in attributes)?  If not, we can kill the third
3132          argument to cp_parser_string_literal.  */
3133       return cp_parser_string_literal (parser,
3134                                        parser->translate_strings_p,
3135                                        true);
3136
3137     case CPP_OPEN_PAREN:
3138       {
3139         tree expr;
3140         bool saved_greater_than_is_operator_p;
3141
3142         /* Consume the `('.  */
3143         cp_lexer_consume_token (parser->lexer);
3144         /* Within a parenthesized expression, a `>' token is always
3145            the greater-than operator.  */
3146         saved_greater_than_is_operator_p
3147           = parser->greater_than_is_operator_p;
3148         parser->greater_than_is_operator_p = true;
3149         /* If we see `( { ' then we are looking at the beginning of
3150            a GNU statement-expression.  */
3151         if (cp_parser_allow_gnu_extensions_p (parser)
3152             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3153           {
3154             /* Statement-expressions are not allowed by the standard.  */
3155             if (pedantic)
3156               pedwarn ("ISO C++ forbids braced-groups within expressions");
3157
3158             /* And they're not allowed outside of a function-body; you
3159                cannot, for example, write:
3160
3161                  int i = ({ int j = 3; j + 1; });
3162
3163                at class or namespace scope.  */
3164             if (!parser->in_function_body
3165                 || parser->in_template_argument_list_p)
3166               {
3167                 error ("statement-expressions are not allowed outside "
3168                        "functions nor in template-argument lists");
3169                 cp_parser_skip_to_end_of_block_or_statement (parser);
3170                 expr = error_mark_node;
3171               }
3172             else
3173               {
3174                 /* Start the statement-expression.  */
3175                 expr = begin_stmt_expr ();
3176                 /* Parse the compound-statement.  */
3177                 cp_parser_compound_statement (parser, expr, false);
3178                 /* Finish up.  */
3179                 expr = finish_stmt_expr (expr, false);
3180               }
3181           }
3182         else
3183           {
3184             /* Parse the parenthesized expression.  */
3185             expr = cp_parser_expression (parser, cast_p);
3186             /* Let the front end know that this expression was
3187                enclosed in parentheses. This matters in case, for
3188                example, the expression is of the form `A::B', since
3189                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3190                not.  */
3191             finish_parenthesized_expr (expr);
3192           }
3193         /* The `>' token might be the end of a template-id or
3194            template-parameter-list now.  */
3195         parser->greater_than_is_operator_p
3196           = saved_greater_than_is_operator_p;
3197         /* Consume the `)'.  */
3198         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3199           cp_parser_skip_to_end_of_statement (parser);
3200
3201         return expr;
3202       }
3203
3204     case CPP_KEYWORD:
3205       switch (token->keyword)
3206         {
3207           /* These two are the boolean literals.  */
3208         case RID_TRUE:
3209           cp_lexer_consume_token (parser->lexer);
3210           return boolean_true_node;
3211         case RID_FALSE:
3212           cp_lexer_consume_token (parser->lexer);
3213           return boolean_false_node;
3214
3215           /* The `__null' literal.  */
3216         case RID_NULL:
3217           cp_lexer_consume_token (parser->lexer);
3218           return null_node;
3219
3220           /* Recognize the `this' keyword.  */
3221         case RID_THIS:
3222           cp_lexer_consume_token (parser->lexer);
3223           if (parser->local_variables_forbidden_p)
3224             {
3225               error ("%<this%> may not be used in this context");
3226               return error_mark_node;
3227             }
3228           /* Pointers cannot appear in constant-expressions.  */
3229           if (cp_parser_non_integral_constant_expression (parser,
3230                                                           "`this'"))
3231             return error_mark_node;
3232           return finish_this_expr ();
3233
3234           /* The `operator' keyword can be the beginning of an
3235              id-expression.  */
3236         case RID_OPERATOR:
3237           goto id_expression;
3238
3239         case RID_FUNCTION_NAME:
3240         case RID_PRETTY_FUNCTION_NAME:
3241         case RID_C99_FUNCTION_NAME:
3242           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3243              __func__ are the names of variables -- but they are
3244              treated specially.  Therefore, they are handled here,
3245              rather than relying on the generic id-expression logic
3246              below.  Grammatically, these names are id-expressions.
3247
3248              Consume the token.  */
3249           token = cp_lexer_consume_token (parser->lexer);
3250           /* Look up the name.  */
3251           return finish_fname (token->u.value);
3252
3253         case RID_VA_ARG:
3254           {
3255             tree expression;
3256             tree type;
3257
3258             /* The `__builtin_va_arg' construct is used to handle
3259                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3260             cp_lexer_consume_token (parser->lexer);
3261             /* Look for the opening `('.  */
3262             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3263             /* Now, parse the assignment-expression.  */
3264             expression = cp_parser_assignment_expression (parser,
3265                                                           /*cast_p=*/false);
3266             /* Look for the `,'.  */
3267             cp_parser_require (parser, CPP_COMMA, "`,'");
3268             /* Parse the type-id.  */
3269             type = cp_parser_type_id (parser);
3270             /* Look for the closing `)'.  */
3271             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3272             /* Using `va_arg' in a constant-expression is not
3273                allowed.  */
3274             if (cp_parser_non_integral_constant_expression (parser,
3275                                                             "`va_arg'"))
3276               return error_mark_node;
3277             return build_x_va_arg (expression, type);
3278           }
3279
3280         case RID_OFFSETOF:
3281           return cp_parser_builtin_offsetof (parser);
3282
3283         case RID_HAS_NOTHROW_ASSIGN:
3284         case RID_HAS_NOTHROW_CONSTRUCTOR:
3285         case RID_HAS_NOTHROW_COPY:        
3286         case RID_HAS_TRIVIAL_ASSIGN:
3287         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3288         case RID_HAS_TRIVIAL_COPY:        
3289         case RID_HAS_TRIVIAL_DESTRUCTOR:
3290         case RID_HAS_VIRTUAL_DESTRUCTOR:
3291         case RID_IS_ABSTRACT:
3292         case RID_IS_BASE_OF:
3293         case RID_IS_CLASS:
3294         case RID_IS_CONVERTIBLE_TO:
3295         case RID_IS_EMPTY:
3296         case RID_IS_ENUM:
3297         case RID_IS_POD:
3298         case RID_IS_POLYMORPHIC:
3299         case RID_IS_UNION:
3300           return cp_parser_trait_expr (parser, token->keyword);
3301
3302         /* Objective-C++ expressions.  */
3303         case RID_AT_ENCODE:
3304         case RID_AT_PROTOCOL:
3305         case RID_AT_SELECTOR:
3306           return cp_parser_objc_expression (parser);
3307
3308         default:
3309           cp_parser_error (parser, "expected primary-expression");
3310           return error_mark_node;
3311         }
3312
3313       /* An id-expression can start with either an identifier, a
3314          `::' as the beginning of a qualified-id, or the "operator"
3315          keyword.  */
3316     case CPP_NAME:
3317     case CPP_SCOPE:
3318     case CPP_TEMPLATE_ID:
3319     case CPP_NESTED_NAME_SPECIFIER:
3320       {
3321         tree id_expression;
3322         tree decl;
3323         const char *error_msg;
3324         bool template_p;
3325         bool done;
3326
3327       id_expression:
3328         /* Parse the id-expression.  */
3329         id_expression
3330           = cp_parser_id_expression (parser,
3331                                      /*template_keyword_p=*/false,
3332                                      /*check_dependency_p=*/true,
3333                                      &template_p,
3334                                      /*declarator_p=*/false,
3335                                      /*optional_p=*/false);
3336         if (id_expression == error_mark_node)
3337           return error_mark_node;
3338         token = cp_lexer_peek_token (parser->lexer);
3339         done = (token->type != CPP_OPEN_SQUARE
3340                 && token->type != CPP_OPEN_PAREN
3341                 && token->type != CPP_DOT
3342                 && token->type != CPP_DEREF
3343                 && token->type != CPP_PLUS_PLUS
3344                 && token->type != CPP_MINUS_MINUS);
3345         /* If we have a template-id, then no further lookup is
3346            required.  If the template-id was for a template-class, we
3347            will sometimes have a TYPE_DECL at this point.  */
3348         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3349                  || TREE_CODE (id_expression) == TYPE_DECL)
3350           decl = id_expression;
3351         /* Look up the name.  */
3352         else
3353           {
3354             tree ambiguous_decls;
3355
3356             decl = cp_parser_lookup_name (parser, id_expression,
3357                                           none_type,
3358                                           template_p,
3359                                           /*is_namespace=*/false,
3360                                           /*check_dependency=*/true,
3361                                           &ambiguous_decls);
3362             /* If the lookup was ambiguous, an error will already have
3363                been issued.  */
3364             if (ambiguous_decls)
3365               return error_mark_node;
3366
3367             /* In Objective-C++, an instance variable (ivar) may be preferred
3368                to whatever cp_parser_lookup_name() found.  */
3369             decl = objc_lookup_ivar (decl, id_expression);
3370
3371             /* If name lookup gives us a SCOPE_REF, then the
3372                qualifying scope was dependent.  */
3373             if (TREE_CODE (decl) == SCOPE_REF)
3374               {
3375                 /* At this point, we do not know if DECL is a valid
3376                    integral constant expression.  We assume that it is
3377                    in fact such an expression, so that code like:
3378
3379                       template <int N> struct A {
3380                         int a[B<N>::i];
3381                       };
3382                      
3383                    is accepted.  At template-instantiation time, we
3384                    will check that B<N>::i is actually a constant.  */
3385                 return decl;
3386               }
3387             /* Check to see if DECL is a local variable in a context
3388                where that is forbidden.  */
3389             if (parser->local_variables_forbidden_p
3390                 && local_variable_p (decl))
3391               {
3392                 /* It might be that we only found DECL because we are
3393                    trying to be generous with pre-ISO scoping rules.
3394                    For example, consider:
3395
3396                      int i;
3397                      void g() {
3398                        for (int i = 0; i < 10; ++i) {}
3399                        extern void f(int j = i);
3400                      }
3401
3402                    Here, name look up will originally find the out
3403                    of scope `i'.  We need to issue a warning message,
3404                    but then use the global `i'.  */
3405                 decl = check_for_out_of_scope_variable (decl);
3406                 if (local_variable_p (decl))
3407                   {
3408                     error ("local variable %qD may not appear in this context",
3409                            decl);
3410                     return error_mark_node;
3411                   }
3412               }
3413           }
3414
3415         decl = (finish_id_expression
3416                 (id_expression, decl, parser->scope,
3417                  idk,
3418                  parser->integral_constant_expression_p,
3419                  parser->allow_non_integral_constant_expression_p,
3420                  &parser->non_integral_constant_expression_p,
3421                  template_p, done, address_p,
3422                  template_arg_p,
3423                  &error_msg));
3424         if (error_msg)
3425           cp_parser_error (parser, error_msg);
3426         return decl;
3427       }
3428
3429       /* Anything else is an error.  */
3430     default:
3431       /* ...unless we have an Objective-C++ message or string literal,
3432          that is.  */
3433       if (c_dialect_objc ()
3434           && (token->type == CPP_OPEN_SQUARE
3435               || token->type == CPP_OBJC_STRING))
3436         return cp_parser_objc_expression (parser);
3437
3438       cp_parser_error (parser, "expected primary-expression");
3439       return error_mark_node;
3440     }
3441 }
3442
3443 /* Parse an id-expression.
3444
3445    id-expression:
3446      unqualified-id
3447      qualified-id
3448
3449    qualified-id:
3450      :: [opt] nested-name-specifier template [opt] unqualified-id
3451      :: identifier
3452      :: operator-function-id
3453      :: template-id
3454
3455    Return a representation of the unqualified portion of the
3456    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3457    a `::' or nested-name-specifier.
3458
3459    Often, if the id-expression was a qualified-id, the caller will
3460    want to make a SCOPE_REF to represent the qualified-id.  This
3461    function does not do this in order to avoid wastefully creating
3462    SCOPE_REFs when they are not required.
3463
3464    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3465    `template' keyword.
3466
3467    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3468    uninstantiated templates.
3469
3470    If *TEMPLATE_P is non-NULL, it is set to true iff the
3471    `template' keyword is used to explicitly indicate that the entity
3472    named is a template.
3473
3474    If DECLARATOR_P is true, the id-expression is appearing as part of
3475    a declarator, rather than as part of an expression.  */
3476
3477 static tree
3478 cp_parser_id_expression (cp_parser *parser,
3479                          bool template_keyword_p,
3480                          bool check_dependency_p,
3481                          bool *template_p,
3482                          bool declarator_p,
3483                          bool optional_p)
3484 {
3485   bool global_scope_p;
3486   bool nested_name_specifier_p;
3487
3488   /* Assume the `template' keyword was not used.  */
3489   if (template_p)
3490     *template_p = template_keyword_p;
3491
3492   /* Look for the optional `::' operator.  */
3493   global_scope_p
3494     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3495        != NULL_TREE);
3496   /* Look for the optional nested-name-specifier.  */
3497   nested_name_specifier_p
3498     = (cp_parser_nested_name_specifier_opt (parser,
3499                                             /*typename_keyword_p=*/false,
3500                                             check_dependency_p,
3501                                             /*type_p=*/false,
3502                                             declarator_p)
3503        != NULL_TREE);
3504   /* If there is a nested-name-specifier, then we are looking at
3505      the first qualified-id production.  */
3506   if (nested_name_specifier_p)
3507     {
3508       tree saved_scope;
3509       tree saved_object_scope;
3510       tree saved_qualifying_scope;
3511       tree unqualified_id;
3512       bool is_template;
3513
3514       /* See if the next token is the `template' keyword.  */
3515       if (!template_p)
3516         template_p = &is_template;
3517       *template_p = cp_parser_optional_template_keyword (parser);
3518       /* Name lookup we do during the processing of the
3519          unqualified-id might obliterate SCOPE.  */
3520       saved_scope = parser->scope;
3521       saved_object_scope = parser->object_scope;
3522       saved_qualifying_scope = parser->qualifying_scope;
3523       /* Process the final unqualified-id.  */
3524       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3525                                                  check_dependency_p,
3526                                                  declarator_p,
3527                                                  /*optional_p=*/false);
3528       /* Restore the SAVED_SCOPE for our caller.  */
3529       parser->scope = saved_scope;
3530       parser->object_scope = saved_object_scope;
3531       parser->qualifying_scope = saved_qualifying_scope;
3532
3533       return unqualified_id;
3534     }
3535   /* Otherwise, if we are in global scope, then we are looking at one
3536      of the other qualified-id productions.  */
3537   else if (global_scope_p)
3538     {
3539       cp_token *token;
3540       tree id;
3541
3542       /* Peek at the next token.  */
3543       token = cp_lexer_peek_token (parser->lexer);
3544
3545       /* If it's an identifier, and the next token is not a "<", then
3546          we can avoid the template-id case.  This is an optimization
3547          for this common case.  */
3548       if (token->type == CPP_NAME
3549           && !cp_parser_nth_token_starts_template_argument_list_p
3550                (parser, 2))
3551         return cp_parser_identifier (parser);
3552
3553       cp_parser_parse_tentatively (parser);
3554       /* Try a template-id.  */
3555       id = cp_parser_template_id (parser,
3556                                   /*template_keyword_p=*/false,
3557                                   /*check_dependency_p=*/true,
3558                                   declarator_p);
3559       /* If that worked, we're done.  */
3560       if (cp_parser_parse_definitely (parser))
3561         return id;
3562
3563       /* Peek at the next token.  (Changes in the token buffer may
3564          have invalidated the pointer obtained above.)  */
3565       token = cp_lexer_peek_token (parser->lexer);
3566
3567       switch (token->type)
3568         {
3569         case CPP_NAME:
3570           return cp_parser_identifier (parser);
3571
3572         case CPP_KEYWORD:
3573           if (token->keyword == RID_OPERATOR)
3574             return cp_parser_operator_function_id (parser);
3575           /* Fall through.  */
3576
3577         default:
3578           cp_parser_error (parser, "expected id-expression");
3579           return error_mark_node;
3580         }
3581     }
3582   else
3583     return cp_parser_unqualified_id (parser, template_keyword_p,
3584                                      /*check_dependency_p=*/true,
3585                                      declarator_p,
3586                                      optional_p);
3587 }
3588
3589 /* Parse an unqualified-id.
3590
3591    unqualified-id:
3592      identifier
3593      operator-function-id
3594      conversion-function-id
3595      ~ class-name
3596      template-id
3597
3598    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3599    keyword, in a construct like `A::template ...'.
3600
3601    Returns a representation of unqualified-id.  For the `identifier'
3602    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3603    production a BIT_NOT_EXPR is returned; the operand of the
3604    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3605    other productions, see the documentation accompanying the
3606    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3607    names are looked up in uninstantiated templates.  If DECLARATOR_P
3608    is true, the unqualified-id is appearing as part of a declarator,
3609    rather than as part of an expression.  */
3610
3611 static tree
3612 cp_parser_unqualified_id (cp_parser* parser,
3613                           bool template_keyword_p,
3614                           bool check_dependency_p,
3615                           bool declarator_p,
3616                           bool optional_p)
3617 {
3618   cp_token *token;
3619
3620   /* Peek at the next token.  */
3621   token = cp_lexer_peek_token (parser->lexer);
3622
3623   switch (token->type)
3624     {
3625     case CPP_NAME:
3626       {
3627         tree id;
3628
3629         /* We don't know yet whether or not this will be a
3630            template-id.  */
3631         cp_parser_parse_tentatively (parser);
3632         /* Try a template-id.  */
3633         id = cp_parser_template_id (parser, template_keyword_p,
3634                                     check_dependency_p,
3635                                     declarator_p);
3636         /* If it worked, we're done.  */
3637         if (cp_parser_parse_definitely (parser))
3638           return id;
3639         /* Otherwise, it's an ordinary identifier.  */
3640         return cp_parser_identifier (parser);
3641       }
3642
3643     case CPP_TEMPLATE_ID:
3644       return cp_parser_template_id (parser, template_keyword_p,
3645                                     check_dependency_p,
3646                                     declarator_p);
3647
3648     case CPP_COMPL:
3649       {
3650         tree type_decl;
3651         tree qualifying_scope;
3652         tree object_scope;
3653         tree scope;
3654         bool done;
3655
3656         /* Consume the `~' token.  */
3657         cp_lexer_consume_token (parser->lexer);
3658         /* Parse the class-name.  The standard, as written, seems to
3659            say that:
3660
3661              template <typename T> struct S { ~S (); };
3662              template <typename T> S<T>::~S() {}
3663
3664            is invalid, since `~' must be followed by a class-name, but
3665            `S<T>' is dependent, and so not known to be a class.
3666            That's not right; we need to look in uninstantiated
3667            templates.  A further complication arises from:
3668
3669              template <typename T> void f(T t) {
3670                t.T::~T();
3671              }
3672
3673            Here, it is not possible to look up `T' in the scope of `T'
3674            itself.  We must look in both the current scope, and the
3675            scope of the containing complete expression.
3676
3677            Yet another issue is:
3678
3679              struct S {
3680                int S;
3681                ~S();
3682              };
3683
3684              S::~S() {}
3685
3686            The standard does not seem to say that the `S' in `~S'
3687            should refer to the type `S' and not the data member
3688            `S::S'.  */
3689
3690         /* DR 244 says that we look up the name after the "~" in the
3691            same scope as we looked up the qualifying name.  That idea
3692            isn't fully worked out; it's more complicated than that.  */
3693         scope = parser->scope;
3694         object_scope = parser->object_scope;
3695         qualifying_scope = parser->qualifying_scope;
3696
3697         /* Check for invalid scopes.  */
3698         if (scope == error_mark_node)
3699           {
3700             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3701               cp_lexer_consume_token (parser->lexer);
3702             return error_mark_node;
3703           }
3704         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3705           {
3706             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3707               error ("scope %qT before %<~%> is not a class-name", scope);
3708             cp_parser_simulate_error (parser);
3709             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3710               cp_lexer_consume_token (parser->lexer);
3711             return error_mark_node;
3712           }
3713         gcc_assert (!scope || TYPE_P (scope));
3714
3715         /* If the name is of the form "X::~X" it's OK.  */
3716         token = cp_lexer_peek_token (parser->lexer);
3717         if (scope
3718             && token->type == CPP_NAME
3719             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3720                 == CPP_OPEN_PAREN)
3721             && constructor_name_p (token->u.value, scope))
3722           {
3723             cp_lexer_consume_token (parser->lexer);
3724             return build_nt (BIT_NOT_EXPR, scope);
3725           }
3726
3727         /* If there was an explicit qualification (S::~T), first look
3728            in the scope given by the qualification (i.e., S).  */
3729         done = false;
3730         type_decl = NULL_TREE;
3731         if (scope)
3732           {
3733             cp_parser_parse_tentatively (parser);
3734             type_decl = cp_parser_class_name (parser,
3735                                               /*typename_keyword_p=*/false,
3736                                               /*template_keyword_p=*/false,
3737                                               none_type,
3738                                               /*check_dependency=*/false,
3739                                               /*class_head_p=*/false,
3740                                               declarator_p);
3741             if (cp_parser_parse_definitely (parser))
3742               done = true;
3743           }
3744         /* In "N::S::~S", look in "N" as well.  */
3745         if (!done && scope && qualifying_scope)
3746           {
3747             cp_parser_parse_tentatively (parser);
3748             parser->scope = qualifying_scope;
3749             parser->object_scope = NULL_TREE;
3750             parser->qualifying_scope = NULL_TREE;
3751             type_decl
3752               = cp_parser_class_name (parser,
3753                                       /*typename_keyword_p=*/false,
3754                                       /*template_keyword_p=*/false,
3755                                       none_type,
3756                                       /*check_dependency=*/false,
3757                                       /*class_head_p=*/false,
3758                                       declarator_p);
3759             if (cp_parser_parse_definitely (parser))
3760               done = true;
3761           }
3762         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3763         else if (!done && object_scope)
3764           {
3765             cp_parser_parse_tentatively (parser);
3766             parser->scope = object_scope;
3767             parser->object_scope = NULL_TREE;
3768             parser->qualifying_scope = NULL_TREE;
3769             type_decl
3770               = cp_parser_class_name (parser,
3771                                       /*typename_keyword_p=*/false,
3772                                       /*template_keyword_p=*/false,
3773                                       none_type,
3774                                       /*check_dependency=*/false,
3775                                       /*class_head_p=*/false,
3776                                       declarator_p);
3777             if (cp_parser_parse_definitely (parser))
3778               done = true;
3779           }
3780         /* Look in the surrounding context.  */
3781         if (!done)
3782           {
3783             parser->scope = NULL_TREE;
3784             parser->object_scope = NULL_TREE;
3785             parser->qualifying_scope = NULL_TREE;
3786             type_decl
3787               = cp_parser_class_name (parser,
3788                                       /*typename_keyword_p=*/false,
3789                                       /*template_keyword_p=*/false,
3790                                       none_type,
3791                                       /*check_dependency=*/false,
3792                                       /*class_head_p=*/false,
3793                                       declarator_p);
3794           }
3795         /* If an error occurred, assume that the name of the
3796            destructor is the same as the name of the qualifying
3797            class.  That allows us to keep parsing after running
3798            into ill-formed destructor names.  */
3799         if (type_decl == error_mark_node && scope)
3800           return build_nt (BIT_NOT_EXPR, scope);
3801         else if (type_decl == error_mark_node)
3802           return error_mark_node;
3803
3804         /* Check that destructor name and scope match.  */
3805         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3806           {
3807             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3808               error ("declaration of %<~%T%> as member of %qT",
3809                      type_decl, scope);
3810             cp_parser_simulate_error (parser);
3811             return error_mark_node;
3812           }
3813
3814         /* [class.dtor]
3815
3816            A typedef-name that names a class shall not be used as the
3817            identifier in the declarator for a destructor declaration.  */
3818         if (declarator_p
3819             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3820             && !DECL_SELF_REFERENCE_P (type_decl)
3821             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3822           error ("typedef-name %qD used as destructor declarator",
3823                  type_decl);
3824
3825         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3826       }
3827
3828     case CPP_KEYWORD:
3829       if (token->keyword == RID_OPERATOR)
3830         {
3831           tree id;
3832
3833           /* This could be a template-id, so we try that first.  */
3834           cp_parser_parse_tentatively (parser);
3835           /* Try a template-id.  */
3836           id = cp_parser_template_id (parser, template_keyword_p,
3837                                       /*check_dependency_p=*/true,
3838                                       declarator_p);
3839           /* If that worked, we're done.  */
3840           if (cp_parser_parse_definitely (parser))
3841             return id;
3842           /* We still don't know whether we're looking at an
3843              operator-function-id or a conversion-function-id.  */
3844           cp_parser_parse_tentatively (parser);
3845           /* Try an operator-function-id.  */
3846           id = cp_parser_operator_function_id (parser);
3847           /* If that didn't work, try a conversion-function-id.  */
3848           if (!cp_parser_parse_definitely (parser))
3849             id = cp_parser_conversion_function_id (parser);
3850
3851           return id;
3852         }
3853       /* Fall through.  */
3854
3855     default:
3856       if (optional_p)
3857         return NULL_TREE;
3858       cp_parser_error (parser, "expected unqualified-id");
3859       return error_mark_node;
3860     }
3861 }
3862
3863 /* Parse an (optional) nested-name-specifier.
3864
3865    nested-name-specifier:
3866      class-or-namespace-name :: nested-name-specifier [opt]
3867      class-or-namespace-name :: template nested-name-specifier [opt]
3868
3869    PARSER->SCOPE should be set appropriately before this function is
3870    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3871    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3872    in name lookups.
3873
3874    Sets PARSER->SCOPE to the class (TYPE) or namespace
3875    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3876    it unchanged if there is no nested-name-specifier.  Returns the new
3877    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3878
3879    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3880    part of a declaration and/or decl-specifier.  */
3881
3882 static tree
3883 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3884                                      bool typename_keyword_p,
3885                                      bool check_dependency_p,
3886                                      bool type_p,
3887                                      bool is_declaration)
3888 {
3889   bool success = false;
3890   cp_token_position start = 0;
3891   cp_token *token;
3892
3893   /* Remember where the nested-name-specifier starts.  */
3894   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3895     {
3896       start = cp_lexer_token_position (parser->lexer, false);
3897       push_deferring_access_checks (dk_deferred);
3898     }
3899
3900   while (true)
3901     {
3902       tree new_scope;
3903       tree old_scope;
3904       tree saved_qualifying_scope;
3905       bool template_keyword_p;
3906
3907       /* Spot cases that cannot be the beginning of a
3908          nested-name-specifier.  */
3909       token = cp_lexer_peek_token (parser->lexer);
3910
3911       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3912          the already parsed nested-name-specifier.  */
3913       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3914         {
3915           /* Grab the nested-name-specifier and continue the loop.  */
3916           cp_parser_pre_parsed_nested_name_specifier (parser);
3917           /* If we originally encountered this nested-name-specifier
3918              with IS_DECLARATION set to false, we will not have
3919              resolved TYPENAME_TYPEs, so we must do so here.  */
3920           if (is_declaration
3921               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3922             {
3923               new_scope = resolve_typename_type (parser->scope,
3924                                                  /*only_current_p=*/false);
3925               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3926                 parser->scope = new_scope;
3927             }
3928           success = true;
3929           continue;
3930         }
3931
3932       /* Spot cases that cannot be the beginning of a
3933          nested-name-specifier.  On the second and subsequent times
3934          through the loop, we look for the `template' keyword.  */
3935       if (success && token->keyword == RID_TEMPLATE)
3936         ;
3937       /* A template-id can start a nested-name-specifier.  */
3938       else if (token->type == CPP_TEMPLATE_ID)
3939         ;
3940       else
3941         {
3942           /* If the next token is not an identifier, then it is
3943              definitely not a class-or-namespace-name.  */
3944           if (token->type != CPP_NAME)
3945             break;
3946           /* If the following token is neither a `<' (to begin a
3947              template-id), nor a `::', then we are not looking at a
3948              nested-name-specifier.  */
3949           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3950           if (token->type != CPP_SCOPE
3951               && !cp_parser_nth_token_starts_template_argument_list_p
3952                   (parser, 2))
3953             break;
3954         }
3955
3956       /* The nested-name-specifier is optional, so we parse
3957          tentatively.  */
3958       cp_parser_parse_tentatively (parser);
3959
3960       /* Look for the optional `template' keyword, if this isn't the
3961          first time through the loop.  */
3962       if (success)
3963         template_keyword_p = cp_parser_optional_template_keyword (parser);
3964       else
3965         template_keyword_p = false;
3966
3967       /* Save the old scope since the name lookup we are about to do
3968          might destroy it.  */
3969       old_scope = parser->scope;
3970       saved_qualifying_scope = parser->qualifying_scope;
3971       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3972          look up names in "X<T>::I" in order to determine that "Y" is
3973          a template.  So, if we have a typename at this point, we make
3974          an effort to look through it.  */
3975       if (is_declaration
3976           && !typename_keyword_p
3977           && parser->scope
3978           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3979         parser->scope = resolve_typename_type (parser->scope,
3980                                                /*only_current_p=*/false);
3981       /* Parse the qualifying entity.  */
3982       new_scope
3983         = cp_parser_class_or_namespace_name (parser,
3984                                              typename_keyword_p,
3985                                              template_keyword_p,
3986                                              check_dependency_p,
3987                                              type_p,
3988                                              is_declaration);
3989       /* Look for the `::' token.  */
3990       cp_parser_require (parser, CPP_SCOPE, "`::'");
3991
3992       /* If we found what we wanted, we keep going; otherwise, we're
3993          done.  */
3994       if (!cp_parser_parse_definitely (parser))
3995         {
3996           bool error_p = false;
3997
3998           /* Restore the OLD_SCOPE since it was valid before the
3999              failed attempt at finding the last
4000              class-or-namespace-name.  */
4001           parser->scope = old_scope;
4002           parser->qualifying_scope = saved_qualifying_scope;
4003           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4004             break;
4005           /* If the next token is an identifier, and the one after
4006              that is a `::', then any valid interpretation would have
4007              found a class-or-namespace-name.  */
4008           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4009                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4010                      == CPP_SCOPE)
4011                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4012                      != CPP_COMPL))
4013             {
4014               token = cp_lexer_consume_token (parser->lexer);
4015               if (!error_p)
4016                 {
4017                   if (!token->ambiguous_p)
4018                     {
4019                       tree decl;
4020                       tree ambiguous_decls;
4021
4022                       decl = cp_parser_lookup_name (parser, token->u.value,
4023                                                     none_type,
4024                                                     /*is_template=*/false,
4025                                                     /*is_namespace=*/false,
4026                                                     /*check_dependency=*/true,
4027                                                     &ambiguous_decls);
4028                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4029                         error ("%qD used without template parameters", decl);
4030                       else if (ambiguous_decls)
4031                         {
4032                           error ("reference to %qD is ambiguous",
4033                                  token->u.value);
4034                           print_candidates (ambiguous_decls);
4035                           decl = error_mark_node;
4036                         }
4037                       else
4038                         cp_parser_name_lookup_error
4039                           (parser, token->u.value, decl,
4040                            "is not a class or namespace");
4041                     }
4042                   parser->scope = error_mark_node;
4043                   error_p = true;
4044                   /* Treat this as a successful nested-name-specifier
4045                      due to:
4046
4047                      [basic.lookup.qual]
4048
4049                      If the name found is not a class-name (clause
4050                      _class_) or namespace-name (_namespace.def_), the
4051                      program is ill-formed.  */
4052                   success = true;
4053                 }
4054               cp_lexer_consume_token (parser->lexer);
4055             }
4056           break;
4057         }
4058       /* We've found one valid nested-name-specifier.  */
4059       success = true;
4060       /* Name lookup always gives us a DECL.  */
4061       if (TREE_CODE (new_scope) == TYPE_DECL)
4062         new_scope = TREE_TYPE (new_scope);
4063       /* Uses of "template" must be followed by actual templates.  */
4064       if (template_keyword_p
4065           && !(CLASS_TYPE_P (new_scope)
4066                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4067                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4068                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4069           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4070                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4071                    == TEMPLATE_ID_EXPR)))
4072         pedwarn (TYPE_P (new_scope)
4073                  ? "%qT is not a template"
4074                  : "%qD is not a template",
4075                  new_scope);
4076       /* If it is a class scope, try to complete it; we are about to
4077          be looking up names inside the class.  */
4078       if (TYPE_P (new_scope)
4079           /* Since checking types for dependency can be expensive,
4080              avoid doing it if the type is already complete.  */
4081           && !COMPLETE_TYPE_P (new_scope)
4082           /* Do not try to complete dependent types.  */
4083           && !dependent_type_p (new_scope))
4084         {
4085           new_scope = complete_type (new_scope);
4086           /* If it is a typedef to current class, use the current
4087              class instead, as the typedef won't have any names inside
4088              it yet.  */
4089           if (!COMPLETE_TYPE_P (new_scope)
4090               && currently_open_class (new_scope))
4091             new_scope = TYPE_MAIN_VARIANT (new_scope);
4092         }
4093       /* Make sure we look in the right scope the next time through
4094          the loop.  */
4095       parser->scope = new_scope;
4096     }
4097
4098   /* If parsing tentatively, replace the sequence of tokens that makes
4099      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4100      token.  That way, should we re-parse the token stream, we will
4101      not have to repeat the effort required to do the parse, nor will
4102      we issue duplicate error messages.  */
4103   if (success && start)
4104     {
4105       cp_token *token;
4106
4107       token = cp_lexer_token_at (parser->lexer, start);
4108       /* Reset the contents of the START token.  */
4109       token->type = CPP_NESTED_NAME_SPECIFIER;
4110       /* Retrieve any deferred checks.  Do not pop this access checks yet
4111          so the memory will not be reclaimed during token replacing below.  */
4112       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4113       token->u.tree_check_value->value = parser->scope;
4114       token->u.tree_check_value->checks = get_deferred_access_checks ();
4115       token->u.tree_check_value->qualifying_scope =
4116         parser->qualifying_scope;
4117       token->keyword = RID_MAX;
4118
4119       /* Purge all subsequent tokens.  */
4120       cp_lexer_purge_tokens_after (parser->lexer, start);
4121     }
4122
4123   if (start)
4124     pop_to_parent_deferring_access_checks ();
4125
4126   return success ? parser->scope : NULL_TREE;
4127 }
4128
4129 /* Parse a nested-name-specifier.  See
4130    cp_parser_nested_name_specifier_opt for details.  This function
4131    behaves identically, except that it will an issue an error if no
4132    nested-name-specifier is present.  */
4133
4134 static tree
4135 cp_parser_nested_name_specifier (cp_parser *parser,
4136                                  bool typename_keyword_p,
4137                                  bool check_dependency_p,
4138                                  bool type_p,
4139                                  bool is_declaration)
4140 {
4141   tree scope;
4142
4143   /* Look for the nested-name-specifier.  */
4144   scope = cp_parser_nested_name_specifier_opt (parser,
4145                                                typename_keyword_p,
4146                                                check_dependency_p,
4147                                                type_p,
4148                                                is_declaration);
4149   /* If it was not present, issue an error message.  */
4150   if (!scope)
4151     {
4152       cp_parser_error (parser, "expected nested-name-specifier");
4153       parser->scope = NULL_TREE;
4154     }
4155
4156   return scope;
4157 }
4158
4159 /* Parse a class-or-namespace-name.
4160
4161    class-or-namespace-name:
4162      class-name
4163      namespace-name
4164
4165    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4166    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4167    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4168    TYPE_P is TRUE iff the next name should be taken as a class-name,
4169    even the same name is declared to be another entity in the same
4170    scope.
4171
4172    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4173    specified by the class-or-namespace-name.  If neither is found the
4174    ERROR_MARK_NODE is returned.  */
4175
4176 static tree
4177 cp_parser_class_or_namespace_name (cp_parser *parser,
4178                                    bool typename_keyword_p,
4179                                    bool template_keyword_p,
4180                                    bool check_dependency_p,
4181                                    bool type_p,
4182                                    bool is_declaration)
4183 {
4184   tree saved_scope;
4185   tree saved_qualifying_scope;
4186   tree saved_object_scope;
4187   tree scope;
4188   bool only_class_p;
4189
4190   /* Before we try to parse the class-name, we must save away the
4191      current PARSER->SCOPE since cp_parser_class_name will destroy
4192      it.  */
4193   saved_scope = parser->scope;
4194   saved_qualifying_scope = parser->qualifying_scope;
4195   saved_object_scope = parser->object_scope;
4196   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4197      there is no need to look for a namespace-name.  */
4198   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4199   if (!only_class_p)
4200     cp_parser_parse_tentatively (parser);
4201   scope = cp_parser_class_name (parser,
4202                                 typename_keyword_p,
4203                                 template_keyword_p,
4204                                 type_p ? class_type : none_type,
4205                                 check_dependency_p,
4206                                 /*class_head_p=*/false,
4207                                 is_declaration);
4208   /* If that didn't work, try for a namespace-name.  */
4209   if (!only_class_p && !cp_parser_parse_definitely (parser))
4210     {
4211       /* Restore the saved scope.  */
4212       parser->scope = saved_scope;
4213       parser->qualifying_scope = saved_qualifying_scope;
4214       parser->object_scope = saved_object_scope;
4215       /* If we are not looking at an identifier followed by the scope
4216          resolution operator, then this is not part of a
4217          nested-name-specifier.  (Note that this function is only used
4218          to parse the components of a nested-name-specifier.)  */
4219       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4220           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4221         return error_mark_node;
4222       scope = cp_parser_namespace_name (parser);
4223     }
4224
4225   return scope;
4226 }
4227
4228 /* Parse a postfix-expression.
4229
4230    postfix-expression:
4231      primary-expression
4232      postfix-expression [ expression ]
4233      postfix-expression ( expression-list [opt] )
4234      simple-type-specifier ( expression-list [opt] )
4235      typename :: [opt] nested-name-specifier identifier
4236        ( expression-list [opt] )
4237      typename :: [opt] nested-name-specifier template [opt] template-id
4238        ( expression-list [opt] )
4239      postfix-expression . template [opt] id-expression
4240      postfix-expression -> template [opt] id-expression
4241      postfix-expression . pseudo-destructor-name
4242      postfix-expression -> pseudo-destructor-name
4243      postfix-expression ++
4244      postfix-expression --
4245      dynamic_cast < type-id > ( expression )
4246      static_cast < type-id > ( expression )
4247      reinterpret_cast < type-id > ( expression )
4248      const_cast < type-id > ( expression )
4249      typeid ( expression )
4250      typeid ( type-id )
4251
4252    GNU Extension:
4253
4254    postfix-expression:
4255      ( type-id ) { initializer-list , [opt] }
4256
4257    This extension is a GNU version of the C99 compound-literal
4258    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4259    but they are essentially the same concept.)
4260
4261    If ADDRESS_P is true, the postfix expression is the operand of the
4262    `&' operator.  CAST_P is true if this expression is the target of a
4263    cast.
4264
4265    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4266    class member access expressions [expr.ref].
4267
4268    Returns a representation of the expression.  */
4269
4270 static tree
4271 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4272                               bool member_access_only_p)
4273 {
4274   cp_token *token;
4275   enum rid keyword;
4276   cp_id_kind idk = CP_ID_KIND_NONE;
4277   tree postfix_expression = NULL_TREE;
4278   bool is_member_access = false;
4279
4280   /* Peek at the next token.  */
4281   token = cp_lexer_peek_token (parser->lexer);
4282   /* Some of the productions are determined by keywords.  */
4283   keyword = token->keyword;
4284   switch (keyword)
4285     {
4286     case RID_DYNCAST:
4287     case RID_STATCAST:
4288     case RID_REINTCAST:
4289     case RID_CONSTCAST:
4290       {
4291         tree type;
4292         tree expression;
4293         const char *saved_message;
4294
4295         /* All of these can be handled in the same way from the point
4296            of view of parsing.  Begin by consuming the token
4297            identifying the cast.  */
4298         cp_lexer_consume_token (parser->lexer);
4299
4300         /* New types cannot be defined in the cast.  */
4301         saved_message = parser->type_definition_forbidden_message;
4302         parser->type_definition_forbidden_message
4303           = "types may not be defined in casts";
4304
4305         /* Look for the opening `<'.  */
4306         cp_parser_require (parser, CPP_LESS, "`<'");
4307         /* Parse the type to which we are casting.  */
4308         type = cp_parser_type_id (parser);
4309         /* Look for the closing `>'.  */
4310         cp_parser_require (parser, CPP_GREATER, "`>'");
4311         /* Restore the old message.  */
4312         parser->type_definition_forbidden_message = saved_message;
4313
4314         /* And the expression which is being cast.  */
4315         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4316         expression = cp_parser_expression (parser, /*cast_p=*/true);
4317         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4318
4319         /* Only type conversions to integral or enumeration types
4320            can be used in constant-expressions.  */
4321         if (!cast_valid_in_integral_constant_expression_p (type)
4322             && (cp_parser_non_integral_constant_expression
4323                 (parser,
4324                  "a cast to a type other than an integral or "
4325                  "enumeration type")))
4326           return error_mark_node;
4327
4328         switch (keyword)
4329           {
4330           case RID_DYNCAST:
4331             postfix_expression
4332               = build_dynamic_cast (type, expression);
4333             break;
4334           case RID_STATCAST:
4335             postfix_expression
4336               = build_static_cast (type, expression);
4337             break;
4338           case RID_REINTCAST:
4339             postfix_expression
4340               = build_reinterpret_cast (type, expression);
4341             break;
4342           case RID_CONSTCAST:
4343             postfix_expression
4344               = build_const_cast (type, expression);
4345             break;
4346           default:
4347             gcc_unreachable ();
4348           }
4349       }
4350       break;
4351
4352     case RID_TYPEID:
4353       {
4354         tree type;
4355         const char *saved_message;
4356         bool saved_in_type_id_in_expr_p;
4357
4358         /* Consume the `typeid' token.  */
4359         cp_lexer_consume_token (parser->lexer);
4360         /* Look for the `(' token.  */
4361         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4362         /* Types cannot be defined in a `typeid' expression.  */
4363         saved_message = parser->type_definition_forbidden_message;
4364         parser->type_definition_forbidden_message
4365           = "types may not be defined in a `typeid\' expression";
4366         /* We can't be sure yet whether we're looking at a type-id or an
4367            expression.  */
4368         cp_parser_parse_tentatively (parser);
4369         /* Try a type-id first.  */
4370         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4371         parser->in_type_id_in_expr_p = true;
4372         type = cp_parser_type_id (parser);
4373         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4374         /* Look for the `)' token.  Otherwise, we can't be sure that
4375            we're not looking at an expression: consider `typeid (int
4376            (3))', for example.  */
4377         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4378         /* If all went well, simply lookup the type-id.  */
4379         if (cp_parser_parse_definitely (parser))
4380           postfix_expression = get_typeid (type);
4381         /* Otherwise, fall back to the expression variant.  */
4382         else
4383           {
4384             tree expression;
4385
4386             /* Look for an expression.  */
4387             expression = cp_parser_expression (parser, /*cast_p=*/false);
4388             /* Compute its typeid.  */
4389             postfix_expression = build_typeid (expression);
4390             /* Look for the `)' token.  */
4391             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4392           }
4393         /* Restore the saved message.  */
4394         parser->type_definition_forbidden_message = saved_message;
4395         /* `typeid' may not appear in an integral constant expression.  */
4396         if (cp_parser_non_integral_constant_expression(parser,
4397                                                        "`typeid' operator"))
4398           return error_mark_node;
4399       }
4400       break;
4401
4402     case RID_TYPENAME:
4403       {
4404         tree type;
4405         /* The syntax permitted here is the same permitted for an
4406            elaborated-type-specifier.  */
4407         type = cp_parser_elaborated_type_specifier (parser,
4408                                                     /*is_friend=*/false,
4409                                                     /*is_declaration=*/false);
4410         postfix_expression = cp_parser_functional_cast (parser, type);
4411       }
4412       break;
4413
4414     default:
4415       {
4416         tree type;
4417
4418         /* If the next thing is a simple-type-specifier, we may be
4419            looking at a functional cast.  We could also be looking at
4420            an id-expression.  So, we try the functional cast, and if
4421            that doesn't work we fall back to the primary-expression.  */
4422         cp_parser_parse_tentatively (parser);
4423         /* Look for the simple-type-specifier.  */
4424         type = cp_parser_simple_type_specifier (parser,
4425                                                 /*decl_specs=*/NULL,
4426                                                 CP_PARSER_FLAGS_NONE);
4427         /* Parse the cast itself.  */
4428         if (!cp_parser_error_occurred (parser))
4429           postfix_expression
4430             = cp_parser_functional_cast (parser, type);
4431         /* If that worked, we're done.  */
4432         if (cp_parser_parse_definitely (parser))
4433           break;
4434
4435         /* If the functional-cast didn't work out, try a
4436            compound-literal.  */
4437         if (cp_parser_allow_gnu_extensions_p (parser)
4438             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4439           {
4440             VEC(constructor_elt,gc) *initializer_list = NULL;
4441             bool saved_in_type_id_in_expr_p;
4442
4443             cp_parser_parse_tentatively (parser);
4444             /* Consume the `('.  */
4445             cp_lexer_consume_token (parser->lexer);
4446             /* Parse the type.  */
4447             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4448             parser->in_type_id_in_expr_p = true;
4449             type = cp_parser_type_id (parser);
4450             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4451             /* Look for the `)'.  */
4452             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4453             /* Look for the `{'.  */
4454             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4455             /* If things aren't going well, there's no need to
4456                keep going.  */
4457             if (!cp_parser_error_occurred (parser))
4458               {
4459                 bool non_constant_p;
4460                 /* Parse the initializer-list.  */
4461                 initializer_list
4462                   = cp_parser_initializer_list (parser, &non_constant_p);
4463                 /* Allow a trailing `,'.  */
4464                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4465                   cp_lexer_consume_token (parser->lexer);
4466                 /* Look for the final `}'.  */
4467                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4468               }
4469             /* If that worked, we're definitely looking at a
4470                compound-literal expression.  */
4471             if (cp_parser_parse_definitely (parser))
4472               {
4473                 /* Warn the user that a compound literal is not
4474                    allowed in standard C++.  */
4475                 if (pedantic)
4476                   pedwarn ("ISO C++ forbids compound-literals");
4477                 /* For simplicity, we disallow compound literals in
4478                    constant-expressions.  We could
4479                    allow compound literals of integer type, whose
4480                    initializer was a constant, in constant
4481                    expressions.  Permitting that usage, as a further
4482                    extension, would not change the meaning of any
4483                    currently accepted programs.  (Of course, as
4484                    compound literals are not part of ISO C++, the
4485                    standard has nothing to say.)  */
4486                 if (cp_parser_non_integral_constant_expression 
4487                     (parser, "non-constant compound literals"))
4488                   {
4489                     postfix_expression = error_mark_node;
4490                     break;
4491                   }
4492                 /* Form the representation of the compound-literal.  */
4493                 postfix_expression
4494                   = finish_compound_literal (type, initializer_list);
4495                 break;
4496               }
4497           }
4498
4499         /* It must be a primary-expression.  */
4500         postfix_expression
4501           = cp_parser_primary_expression (parser, address_p, cast_p,
4502                                           /*template_arg_p=*/false,
4503                                           &idk);
4504       }
4505       break;
4506     }
4507
4508   /* Keep looping until the postfix-expression is complete.  */
4509   while (true)
4510     {
4511       if (idk == CP_ID_KIND_UNQUALIFIED
4512           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4513           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4514         /* It is not a Koenig lookup function call.  */
4515         postfix_expression
4516           = unqualified_name_lookup_error (postfix_expression);
4517
4518       /* Peek at the next token.  */
4519       token = cp_lexer_peek_token (parser->lexer);
4520
4521       switch (token->type)
4522         {
4523         case CPP_OPEN_SQUARE:
4524           postfix_expression
4525             = cp_parser_postfix_open_square_expression (parser,
4526                                                         postfix_expression,
4527                                                         false);
4528           idk = CP_ID_KIND_NONE;
4529           is_member_access = false;
4530           break;
4531
4532         case CPP_OPEN_PAREN:
4533           /* postfix-expression ( expression-list [opt] ) */
4534           {
4535             bool koenig_p;
4536             bool is_builtin_constant_p;
4537             bool saved_integral_constant_expression_p = false;
4538             bool saved_non_integral_constant_expression_p = false;
4539             tree args;
4540
4541             is_member_access = false;
4542
4543             is_builtin_constant_p
4544               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4545             if (is_builtin_constant_p)
4546               {
4547                 /* The whole point of __builtin_constant_p is to allow
4548                    non-constant expressions to appear as arguments.  */
4549                 saved_integral_constant_expression_p
4550                   = parser->integral_constant_expression_p;
4551                 saved_non_integral_constant_expression_p
4552                   = parser->non_integral_constant_expression_p;
4553                 parser->integral_constant_expression_p = false;
4554               }
4555             args = (cp_parser_parenthesized_expression_list
4556                     (parser, /*is_attribute_list=*/false,
4557                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4558                      /*non_constant_p=*/NULL));
4559             if (is_builtin_constant_p)
4560               {
4561                 parser->integral_constant_expression_p
4562                   = saved_integral_constant_expression_p;
4563                 parser->non_integral_constant_expression_p
4564                   = saved_non_integral_constant_expression_p;
4565               }
4566
4567             if (args == error_mark_node)
4568               {
4569                 postfix_expression = error_mark_node;
4570                 break;
4571               }
4572
4573             /* Function calls are not permitted in
4574                constant-expressions.  */
4575             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4576                 && cp_parser_non_integral_constant_expression (parser,
4577                                                                "a function call"))
4578               {
4579                 postfix_expression = error_mark_node;
4580                 break;
4581               }
4582
4583             koenig_p = false;
4584             if (idk == CP_ID_KIND_UNQUALIFIED)
4585               {
4586                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4587                   {
4588                     if (args)
4589                       {
4590                         koenig_p = true;
4591                         postfix_expression
4592                           = perform_koenig_lookup (postfix_expression, args);
4593                       }
4594                     else
4595                       postfix_expression
4596                         = unqualified_fn_lookup_error (postfix_expression);
4597                   }
4598                 /* We do not perform argument-dependent lookup if
4599                    normal lookup finds a non-function, in accordance
4600                    with the expected resolution of DR 218.  */
4601                 else if (args && is_overloaded_fn (postfix_expression))
4602                   {
4603                     tree fn = get_first_fn (postfix_expression);
4604
4605                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4606                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4607
4608                     /* Only do argument dependent lookup if regular
4609                        lookup does not find a set of member functions.
4610                        [basic.lookup.koenig]/2a  */
4611                     if (!DECL_FUNCTION_MEMBER_P (fn))
4612                       {
4613                         koenig_p = true;
4614                         postfix_expression
4615                           = perform_koenig_lookup (postfix_expression, args);
4616                       }
4617                   }
4618               }
4619
4620             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4621               {
4622                 tree instance = TREE_OPERAND (postfix_expression, 0);
4623                 tree fn = TREE_OPERAND (postfix_expression, 1);
4624
4625                 if (processing_template_decl
4626                     && (type_dependent_expression_p (instance)
4627                         || (!BASELINK_P (fn)
4628                             && TREE_CODE (fn) != FIELD_DECL)
4629                         || type_dependent_expression_p (fn)
4630                         || any_type_dependent_arguments_p (args)))
4631                   {
4632                     postfix_expression
4633                       = build_nt_call_list (postfix_expression, args);
4634                     break;
4635                   }
4636
4637                 if (BASELINK_P (fn))
4638                   postfix_expression
4639                     = (build_new_method_call
4640                        (instance, fn, args, NULL_TREE,
4641                         (idk == CP_ID_KIND_QUALIFIED
4642                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4643                         /*fn_p=*/NULL));
4644                 else
4645                   postfix_expression
4646                     = finish_call_expr (postfix_expression, args,
4647                                         /*disallow_virtual=*/false,
4648                                         /*koenig_p=*/false);
4649               }
4650             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4651                      || TREE_CODE (postfix_expression) == MEMBER_REF
4652                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4653               postfix_expression = (build_offset_ref_call_from_tree
4654                                     (postfix_expression, args));
4655             else if (idk == CP_ID_KIND_QUALIFIED)
4656               /* A call to a static class member, or a namespace-scope
4657                  function.  */
4658               postfix_expression
4659                 = finish_call_expr (postfix_expression, args,
4660                                     /*disallow_virtual=*/true,
4661                                     koenig_p);
4662             else
4663               /* All other function calls.  */
4664               postfix_expression
4665                 = finish_call_expr (postfix_expression, args,
4666                                     /*disallow_virtual=*/false,
4667                                     koenig_p);
4668
4669             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4670             idk = CP_ID_KIND_NONE;
4671           }
4672           break;
4673
4674         case CPP_DOT:
4675         case CPP_DEREF:
4676           /* postfix-expression . template [opt] id-expression
4677              postfix-expression . pseudo-destructor-name
4678              postfix-expression -> template [opt] id-expression
4679              postfix-expression -> pseudo-destructor-name */
4680
4681           /* Consume the `.' or `->' operator.  */
4682           cp_lexer_consume_token (parser->lexer);
4683
4684           postfix_expression
4685             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4686                                                       postfix_expression,
4687                                                       false, &idk);
4688
4689           is_member_access = true;
4690           break;
4691
4692         case CPP_PLUS_PLUS:
4693           /* postfix-expression ++  */
4694           /* Consume the `++' token.  */
4695           cp_lexer_consume_token (parser->lexer);
4696           /* Generate a representation for the complete expression.  */
4697           postfix_expression
4698             = finish_increment_expr (postfix_expression,
4699                                      POSTINCREMENT_EXPR);
4700           /* Increments may not appear in constant-expressions.  */
4701           if (cp_parser_non_integral_constant_expression (parser,
4702                                                           "an increment"))
4703             postfix_expression = error_mark_node;
4704           idk = CP_ID_KIND_NONE;
4705           is_member_access = false;
4706           break;
4707
4708         case CPP_MINUS_MINUS:
4709           /* postfix-expression -- */
4710           /* Consume the `--' token.  */
4711           cp_lexer_consume_token (parser->lexer);
4712           /* Generate a representation for the complete expression.  */
4713           postfix_expression
4714             = finish_increment_expr (postfix_expression,
4715                                      POSTDECREMENT_EXPR);
4716           /* Decrements may not appear in constant-expressions.  */
4717           if (cp_parser_non_integral_constant_expression (parser,
4718                                                           "a decrement"))
4719             postfix_expression = error_mark_node;
4720           idk = CP_ID_KIND_NONE;
4721           is_member_access = false;
4722           break;
4723
4724         default:
4725           if (member_access_only_p)
4726             return is_member_access? postfix_expression : error_mark_node;
4727           else
4728             return postfix_expression;
4729         }
4730     }
4731
4732   /* We should never get here.  */
4733   gcc_unreachable ();
4734   return error_mark_node;
4735 }
4736
4737 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4738    by cp_parser_builtin_offsetof.  We're looking for
4739
4740      postfix-expression [ expression ]
4741
4742    FOR_OFFSETOF is set if we're being called in that context, which
4743    changes how we deal with integer constant expressions.  */
4744
4745 static tree
4746 cp_parser_postfix_open_square_expression (cp_parser *parser,
4747                                           tree postfix_expression,
4748                                           bool for_offsetof)
4749 {
4750   tree index;
4751
4752   /* Consume the `[' token.  */
4753   cp_lexer_consume_token (parser->lexer);
4754
4755   /* Parse the index expression.  */
4756   /* ??? For offsetof, there is a question of what to allow here.  If
4757      offsetof is not being used in an integral constant expression context,
4758      then we *could* get the right answer by computing the value at runtime.
4759      If we are in an integral constant expression context, then we might
4760      could accept any constant expression; hard to say without analysis.
4761      Rather than open the barn door too wide right away, allow only integer
4762      constant expressions here.  */
4763   if (for_offsetof)
4764     index = cp_parser_constant_expression (parser, false, NULL);
4765   else
4766     index = cp_parser_expression (parser, /*cast_p=*/false);
4767
4768   /* Look for the closing `]'.  */
4769   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4770
4771   /* Build the ARRAY_REF.  */
4772   postfix_expression = grok_array_decl (postfix_expression, index);
4773
4774   /* When not doing offsetof, array references are not permitted in
4775      constant-expressions.  */
4776   if (!for_offsetof
4777       && (cp_parser_non_integral_constant_expression
4778           (parser, "an array reference")))
4779     postfix_expression = error_mark_node;
4780
4781   return postfix_expression;
4782 }
4783
4784 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4785    by cp_parser_builtin_offsetof.  We're looking for
4786
4787      postfix-expression . template [opt] id-expression
4788      postfix-expression . pseudo-destructor-name
4789      postfix-expression -> template [opt] id-expression
4790      postfix-expression -> pseudo-destructor-name
4791
4792    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4793    limits what of the above we'll actually accept, but nevermind.
4794    TOKEN_TYPE is the "." or "->" token, which will already have been
4795    removed from the stream.  */
4796
4797 static tree
4798 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4799                                         enum cpp_ttype token_type,
4800                                         tree postfix_expression,
4801                                         bool for_offsetof, cp_id_kind *idk)
4802 {
4803   tree name;
4804   bool dependent_p;
4805   bool pseudo_destructor_p;
4806   tree scope = NULL_TREE;
4807
4808   /* If this is a `->' operator, dereference the pointer.  */
4809   if (token_type == CPP_DEREF)
4810     postfix_expression = build_x_arrow (postfix_expression);
4811   /* Check to see whether or not the expression is type-dependent.  */
4812   dependent_p = type_dependent_expression_p (postfix_expression);
4813   /* The identifier following the `->' or `.' is not qualified.  */
4814   parser->scope = NULL_TREE;
4815   parser->qualifying_scope = NULL_TREE;
4816   parser->object_scope = NULL_TREE;
4817   *idk = CP_ID_KIND_NONE;
4818   /* Enter the scope corresponding to the type of the object
4819      given by the POSTFIX_EXPRESSION.  */
4820   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4821     {
4822       scope = TREE_TYPE (postfix_expression);
4823       /* According to the standard, no expression should ever have
4824          reference type.  Unfortunately, we do not currently match
4825          the standard in this respect in that our internal representation
4826          of an expression may have reference type even when the standard
4827          says it does not.  Therefore, we have to manually obtain the
4828          underlying type here.  */
4829       scope = non_reference (scope);
4830       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4831       if (scope == unknown_type_node)
4832         {
4833           error ("%qE does not have class type", postfix_expression);
4834           scope = NULL_TREE;
4835         }
4836       else
4837         scope = complete_type_or_else (scope, NULL_TREE);
4838       /* Let the name lookup machinery know that we are processing a
4839          class member access expression.  */
4840       parser->context->object_type = scope;
4841       /* If something went wrong, we want to be able to discern that case,
4842          as opposed to the case where there was no SCOPE due to the type
4843          of expression being dependent.  */
4844       if (!scope)
4845         scope = error_mark_node;
4846       /* If the SCOPE was erroneous, make the various semantic analysis
4847          functions exit quickly -- and without issuing additional error
4848          messages.  */
4849       if (scope == error_mark_node)
4850         postfix_expression = error_mark_node;
4851     }
4852
4853   /* Assume this expression is not a pseudo-destructor access.  */
4854   pseudo_destructor_p = false;
4855
4856   /* If the SCOPE is a scalar type, then, if this is a valid program,
4857      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
4858      is type dependent, it can be pseudo-destructor-name or something else.
4859      Try to parse it as pseudo-destructor-name first.  */
4860   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4861     {
4862       tree s;
4863       tree type;
4864
4865       cp_parser_parse_tentatively (parser);
4866       /* Parse the pseudo-destructor-name.  */
4867       s = NULL_TREE;
4868       cp_parser_pseudo_destructor_name (parser, &s, &type);
4869       if (dependent_p
4870           && (cp_parser_error_occurred (parser)
4871               || TREE_CODE (type) != TYPE_DECL
4872               || !SCALAR_TYPE_P (TREE_TYPE (type))))
4873         cp_parser_abort_tentative_parse (parser);
4874       else if (cp_parser_parse_definitely (parser))
4875         {
4876           pseudo_destructor_p = true;
4877           postfix_expression
4878             = finish_pseudo_destructor_expr (postfix_expression,
4879                                              s, TREE_TYPE (type));
4880         }
4881     }
4882
4883   if (!pseudo_destructor_p)
4884     {
4885       /* If the SCOPE is not a scalar type, we are looking at an
4886          ordinary class member access expression, rather than a
4887          pseudo-destructor-name.  */
4888       bool template_p;
4889       /* Parse the id-expression.  */
4890       name = (cp_parser_id_expression
4891               (parser,
4892                cp_parser_optional_template_keyword (parser),
4893                /*check_dependency_p=*/true,
4894                &template_p,
4895                /*declarator_p=*/false,
4896                /*optional_p=*/false));
4897       /* In general, build a SCOPE_REF if the member name is qualified.
4898          However, if the name was not dependent and has already been
4899          resolved; there is no need to build the SCOPE_REF.  For example;
4900
4901              struct X { void f(); };
4902              template <typename T> void f(T* t) { t->X::f(); }
4903
4904          Even though "t" is dependent, "X::f" is not and has been resolved
4905          to a BASELINK; there is no need to include scope information.  */
4906
4907       /* But we do need to remember that there was an explicit scope for
4908          virtual function calls.  */
4909       if (parser->scope)
4910         *idk = CP_ID_KIND_QUALIFIED;
4911
4912       /* If the name is a template-id that names a type, we will get a
4913          TYPE_DECL here.  That is invalid code.  */
4914       if (TREE_CODE (name) == TYPE_DECL)
4915         {
4916           error ("invalid use of %qD", name);
4917           postfix_expression = error_mark_node;
4918         }
4919       else
4920         {
4921           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4922             {
4923               name = build_qualified_name (/*type=*/NULL_TREE,
4924                                            parser->scope,
4925                                            name,
4926                                            template_p);
4927               parser->scope = NULL_TREE;
4928               parser->qualifying_scope = NULL_TREE;
4929               parser->object_scope = NULL_TREE;
4930             }
4931           if (scope && name && BASELINK_P (name))
4932             adjust_result_of_qualified_name_lookup
4933               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4934           postfix_expression
4935             = finish_class_member_access_expr (postfix_expression, name,
4936                                                template_p);
4937         }
4938     }
4939
4940   /* We no longer need to look up names in the scope of the object on
4941      the left-hand side of the `.' or `->' operator.  */
4942   parser->context->object_type = NULL_TREE;
4943
4944   /* Outside of offsetof, these operators may not appear in
4945      constant-expressions.  */
4946   if (!for_offsetof
4947       && (cp_parser_non_integral_constant_expression
4948           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4949     postfix_expression = error_mark_node;
4950
4951   return postfix_expression;
4952 }
4953
4954 /* Parse a parenthesized expression-list.
4955
4956    expression-list:
4957      assignment-expression
4958      expression-list, assignment-expression
4959
4960    attribute-list:
4961      expression-list
4962      identifier
4963      identifier, expression-list
4964
4965    CAST_P is true if this expression is the target of a cast.
4966
4967    ALLOW_EXPANSION_P is true if this expression allows expansion of an
4968    argument pack.
4969
4970    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4971    representation of an assignment-expression.  Note that a TREE_LIST
4972    is returned even if there is only a single expression in the list.
4973    error_mark_node is returned if the ( and or ) are
4974    missing. NULL_TREE is returned on no expressions. The parentheses
4975    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4976    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4977    indicates whether or not all of the expressions in the list were
4978    constant.  */
4979
4980 static tree
4981 cp_parser_parenthesized_expression_list (cp_parser* parser,
4982                                          bool is_attribute_list,
4983                                          bool cast_p,
4984                                          bool allow_expansion_p,
4985                                          bool *non_constant_p)
4986 {
4987   tree expression_list = NULL_TREE;
4988   bool fold_expr_p = is_attribute_list;
4989   tree identifier = NULL_TREE;
4990   bool saved_greater_than_is_operator_p;
4991
4992   /* Assume all the expressions will be constant.  */
4993   if (non_constant_p)
4994     *non_constant_p = false;
4995
4996   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4997     return error_mark_node;
4998
4999   /* Within a parenthesized expression, a `>' token is always
5000      the greater-than operator.  */
5001   saved_greater_than_is_operator_p
5002     = parser->greater_than_is_operator_p;
5003   parser->greater_than_is_operator_p = true;
5004
5005   /* Consume expressions until there are no more.  */
5006   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5007     while (true)
5008       {
5009         tree expr;
5010
5011         /* At the beginning of attribute lists, check to see if the
5012            next token is an identifier.  */
5013         if (is_attribute_list
5014             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5015           {
5016             cp_token *token;
5017
5018             /* Consume the identifier.  */
5019             token = cp_lexer_consume_token (parser->lexer);
5020             /* Save the identifier.  */
5021             identifier = token->u.value;
5022           }
5023         else
5024           {
5025             /* Parse the next assignment-expression.  */
5026             if (non_constant_p)
5027               {
5028                 bool expr_non_constant_p;
5029                 expr = (cp_parser_constant_expression
5030                         (parser, /*allow_non_constant_p=*/true,
5031                          &expr_non_constant_p));
5032                 if (expr_non_constant_p)
5033                   *non_constant_p = true;
5034               }
5035             else
5036               expr = cp_parser_assignment_expression (parser, cast_p);
5037
5038             if (fold_expr_p)
5039               expr = fold_non_dependent_expr (expr);
5040
5041             /* If we have an ellipsis, then this is an expression
5042                expansion.  */
5043             if (allow_expansion_p
5044                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5045               {
5046                 /* Consume the `...'.  */
5047                 cp_lexer_consume_token (parser->lexer);
5048
5049                 /* Build the argument pack.  */
5050                 expr = make_pack_expansion (expr);
5051               }
5052
5053              /* Add it to the list.  We add error_mark_node
5054                 expressions to the list, so that we can still tell if
5055                 the correct form for a parenthesized expression-list
5056                 is found. That gives better errors.  */
5057             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5058
5059             if (expr == error_mark_node)
5060               goto skip_comma;
5061           }
5062
5063         /* After the first item, attribute lists look the same as
5064            expression lists.  */
5065         is_attribute_list = false;
5066
5067       get_comma:;
5068         /* If the next token isn't a `,', then we are done.  */
5069         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5070           break;
5071
5072         /* Otherwise, consume the `,' and keep going.  */
5073         cp_lexer_consume_token (parser->lexer);
5074       }
5075
5076   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5077     {
5078       int ending;
5079
5080     skip_comma:;
5081       /* We try and resync to an unnested comma, as that will give the
5082          user better diagnostics.  */
5083       ending = cp_parser_skip_to_closing_parenthesis (parser,
5084                                                       /*recovering=*/true,
5085                                                       /*or_comma=*/true,
5086                                                       /*consume_paren=*/true);
5087       if (ending < 0)
5088         goto get_comma;
5089       if (!ending)
5090         {
5091           parser->greater_than_is_operator_p
5092             = saved_greater_than_is_operator_p;
5093           return error_mark_node;
5094         }
5095     }
5096
5097   parser->greater_than_is_operator_p
5098     = saved_greater_than_is_operator_p;
5099
5100   /* We built up the list in reverse order so we must reverse it now.  */
5101   expression_list = nreverse (expression_list);
5102   if (identifier)
5103     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5104
5105   return expression_list;
5106 }
5107
5108 /* Parse a pseudo-destructor-name.
5109
5110    pseudo-destructor-name:
5111      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5112      :: [opt] nested-name-specifier template template-id :: ~ type-name
5113      :: [opt] nested-name-specifier [opt] ~ type-name
5114
5115    If either of the first two productions is used, sets *SCOPE to the
5116    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5117    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5118    or ERROR_MARK_NODE if the parse fails.  */
5119
5120 static void
5121 cp_parser_pseudo_destructor_name (cp_parser* parser,
5122                                   tree* scope,
5123                                   tree* type)
5124 {
5125   bool nested_name_specifier_p;
5126
5127   /* Assume that things will not work out.  */
5128   *type = error_mark_node;
5129
5130   /* Look for the optional `::' operator.  */
5131   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5132   /* Look for the optional nested-name-specifier.  */
5133   nested_name_specifier_p
5134     = (cp_parser_nested_name_specifier_opt (parser,
5135                                             /*typename_keyword_p=*/false,
5136                                             /*check_dependency_p=*/true,
5137                                             /*type_p=*/false,
5138                                             /*is_declaration=*/true)
5139        != NULL_TREE);
5140   /* Now, if we saw a nested-name-specifier, we might be doing the
5141      second production.  */
5142   if (nested_name_specifier_p
5143       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5144     {
5145       /* Consume the `template' keyword.  */
5146       cp_lexer_consume_token (parser->lexer);
5147       /* Parse the template-id.  */
5148       cp_parser_template_id (parser,
5149                              /*template_keyword_p=*/true,
5150                              /*check_dependency_p=*/false,
5151                              /*is_declaration=*/true);
5152       /* Look for the `::' token.  */
5153       cp_parser_require (parser, CPP_SCOPE, "`::'");
5154     }
5155   /* If the next token is not a `~', then there might be some
5156      additional qualification.  */
5157   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5158     {
5159       /* At this point, we're looking for "type-name :: ~".  The type-name
5160          must not be a class-name, since this is a pseudo-destructor.  So,
5161          it must be either an enum-name, or a typedef-name -- both of which
5162          are just identifiers.  So, we peek ahead to check that the "::"
5163          and "~" tokens are present; if they are not, then we can avoid
5164          calling type_name.  */
5165       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5166           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5167           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5168         {
5169           cp_parser_error (parser, "non-scalar type");
5170           return;
5171         }
5172
5173       /* Look for the type-name.  */
5174       *scope = TREE_TYPE (cp_parser_type_name (parser));
5175
5176       if (*scope == error_mark_node)
5177         return;
5178
5179       /* Look for the `::' token.  */
5180       cp_parser_require (parser, CPP_SCOPE, "`::'");
5181     }
5182   else
5183     *scope = NULL_TREE;
5184
5185   /* Look for the `~'.  */
5186   cp_parser_require (parser, CPP_COMPL, "`~'");
5187   /* Look for the type-name again.  We are not responsible for
5188      checking that it matches the first type-name.  */
5189   *type = cp_parser_type_name (parser);
5190 }
5191
5192 /* Parse a unary-expression.
5193
5194    unary-expression:
5195      postfix-expression
5196      ++ cast-expression
5197      -- cast-expression
5198      unary-operator cast-expression
5199      sizeof unary-expression
5200      sizeof ( type-id )
5201      new-expression
5202      delete-expression
5203
5204    GNU Extensions:
5205
5206    unary-expression:
5207      __extension__ cast-expression
5208      __alignof__ unary-expression
5209      __alignof__ ( type-id )
5210      __real__ cast-expression
5211      __imag__ cast-expression
5212      && identifier
5213
5214    ADDRESS_P is true iff the unary-expression is appearing as the
5215    operand of the `&' operator.   CAST_P is true if this expression is
5216    the target of a cast.
5217
5218    Returns a representation of the expression.  */
5219
5220 static tree
5221 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5222 {
5223   cp_token *token;
5224   enum tree_code unary_operator;
5225
5226   /* Peek at the next token.  */
5227   token = cp_lexer_peek_token (parser->lexer);
5228   /* Some keywords give away the kind of expression.  */
5229   if (token->type == CPP_KEYWORD)
5230     {
5231       enum rid keyword = token->keyword;
5232
5233       switch (keyword)
5234         {
5235         case RID_ALIGNOF:
5236         case RID_SIZEOF:
5237           {
5238             tree operand;
5239             enum tree_code op;
5240
5241             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5242             /* Consume the token.  */
5243             cp_lexer_consume_token (parser->lexer);
5244             /* Parse the operand.  */
5245             operand = cp_parser_sizeof_operand (parser, keyword);
5246
5247             if (TYPE_P (operand))
5248               return cxx_sizeof_or_alignof_type (operand, op, true);
5249             else
5250               return cxx_sizeof_or_alignof_expr (operand, op);
5251           }
5252
5253         case RID_NEW:
5254           return cp_parser_new_expression (parser);
5255
5256         case RID_DELETE:
5257           return cp_parser_delete_expression (parser);
5258
5259         case RID_EXTENSION:
5260           {
5261             /* The saved value of the PEDANTIC flag.  */
5262             int saved_pedantic;
5263             tree expr;
5264
5265             /* Save away the PEDANTIC flag.  */
5266             cp_parser_extension_opt (parser, &saved_pedantic);
5267             /* Parse the cast-expression.  */
5268             expr = cp_parser_simple_cast_expression (parser);
5269             /* Restore the PEDANTIC flag.  */
5270             pedantic = saved_pedantic;
5271
5272             return expr;
5273           }
5274
5275         case RID_REALPART:
5276         case RID_IMAGPART:
5277           {
5278             tree expression;
5279
5280             /* Consume the `__real__' or `__imag__' token.  */
5281             cp_lexer_consume_token (parser->lexer);
5282             /* Parse the cast-expression.  */
5283             expression = cp_parser_simple_cast_expression (parser);
5284             /* Create the complete representation.  */
5285             return build_x_unary_op ((keyword == RID_REALPART
5286                                       ? REALPART_EXPR : IMAGPART_EXPR),
5287                                      expression);
5288           }
5289           break;
5290
5291         default:
5292           break;
5293         }
5294     }
5295
5296   /* Look for the `:: new' and `:: delete', which also signal the
5297      beginning of a new-expression, or delete-expression,
5298      respectively.  If the next token is `::', then it might be one of
5299      these.  */
5300   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5301     {
5302       enum rid keyword;
5303
5304       /* See if the token after the `::' is one of the keywords in
5305          which we're interested.  */
5306       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5307       /* If it's `new', we have a new-expression.  */
5308       if (keyword == RID_NEW)
5309         return cp_parser_new_expression (parser);
5310       /* Similarly, for `delete'.  */
5311       else if (keyword == RID_DELETE)
5312         return cp_parser_delete_expression (parser);
5313     }
5314
5315   /* Look for a unary operator.  */
5316   unary_operator = cp_parser_unary_operator (token);
5317   /* The `++' and `--' operators can be handled similarly, even though
5318      they are not technically unary-operators in the grammar.  */
5319   if (unary_operator == ERROR_MARK)
5320     {
5321       if (token->type == CPP_PLUS_PLUS)
5322         unary_operator = PREINCREMENT_EXPR;
5323       else if (token->type == CPP_MINUS_MINUS)
5324         unary_operator = PREDECREMENT_EXPR;
5325       /* Handle the GNU address-of-label extension.  */
5326       else if (cp_parser_allow_gnu_extensions_p (parser)
5327                && token->type == CPP_AND_AND)
5328         {
5329           tree identifier;
5330           tree expression;
5331
5332           /* Consume the '&&' token.  */
5333           cp_lexer_consume_token (parser->lexer);
5334           /* Look for the identifier.  */
5335           identifier = cp_parser_identifier (parser);
5336           /* Create an expression representing the address.  */
5337           expression = finish_label_address_expr (identifier);
5338           if (cp_parser_non_integral_constant_expression (parser,
5339                                                 "the address of a label"))
5340             expression = error_mark_node;
5341           return expression;
5342         }
5343     }
5344   if (unary_operator != ERROR_MARK)
5345     {
5346       tree cast_expression;
5347       tree expression = error_mark_node;
5348       const char *non_constant_p = NULL;
5349
5350       /* Consume the operator token.  */
5351       token = cp_lexer_consume_token (parser->lexer);
5352       /* Parse the cast-expression.  */
5353       cast_expression
5354         = cp_parser_cast_expression (parser,
5355                                      unary_operator == ADDR_EXPR,
5356                                      /*cast_p=*/false);
5357       /* Now, build an appropriate representation.  */
5358       switch (unary_operator)
5359         {
5360         case INDIRECT_REF:
5361           non_constant_p = "`*'";
5362           expression = build_x_indirect_ref (cast_expression, "unary *");
5363           break;
5364
5365         case ADDR_EXPR:
5366           non_constant_p = "`&'";
5367           /* Fall through.  */
5368         case BIT_NOT_EXPR:
5369           expression = build_x_unary_op (unary_operator, cast_expression);
5370           break;
5371
5372         case PREINCREMENT_EXPR:
5373         case PREDECREMENT_EXPR:
5374           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5375                             ? "`++'" : "`--'");
5376           /* Fall through.  */
5377         case UNARY_PLUS_EXPR:
5378         case NEGATE_EXPR:
5379         case TRUTH_NOT_EXPR:
5380           expression = finish_unary_op_expr (unary_operator, cast_expression);
5381           break;
5382
5383         default:
5384           gcc_unreachable ();
5385         }
5386
5387       if (non_constant_p
5388           && cp_parser_non_integral_constant_expression (parser,
5389                                                          non_constant_p))
5390         expression = error_mark_node;
5391
5392       return expression;
5393     }
5394
5395   return cp_parser_postfix_expression (parser, address_p, cast_p,
5396                                        /*member_access_only_p=*/false);
5397 }
5398
5399 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5400    unary-operator, the corresponding tree code is returned.  */
5401
5402 static enum tree_code
5403 cp_parser_unary_operator (cp_token* token)
5404 {
5405   switch (token->type)
5406     {
5407     case CPP_MULT:
5408       return INDIRECT_REF;
5409
5410     case CPP_AND:
5411       return ADDR_EXPR;
5412
5413     case CPP_PLUS:
5414       return UNARY_PLUS_EXPR;
5415
5416     case CPP_MINUS:
5417       return NEGATE_EXPR;
5418
5419     case CPP_NOT:
5420       return TRUTH_NOT_EXPR;
5421
5422     case CPP_COMPL:
5423       return BIT_NOT_EXPR;
5424
5425     default:
5426       return ERROR_MARK;
5427     }
5428 }
5429
5430 /* Parse a new-expression.
5431
5432    new-expression:
5433      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5434      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5435
5436    Returns a representation of the expression.  */
5437
5438 static tree
5439 cp_parser_new_expression (cp_parser* parser)
5440 {
5441   bool global_scope_p;
5442   tree placement;
5443   tree type;
5444   tree initializer;
5445   tree nelts;
5446
5447   /* Look for the optional `::' operator.  */
5448   global_scope_p
5449     = (cp_parser_global_scope_opt (parser,
5450                                    /*current_scope_valid_p=*/false)
5451        != NULL_TREE);
5452   /* Look for the `new' operator.  */
5453   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5454   /* There's no easy way to tell a new-placement from the
5455      `( type-id )' construct.  */
5456   cp_parser_parse_tentatively (parser);
5457   /* Look for a new-placement.  */
5458   placement = cp_parser_new_placement (parser);
5459   /* If that didn't work out, there's no new-placement.  */
5460   if (!cp_parser_parse_definitely (parser))
5461     placement = NULL_TREE;
5462
5463   /* If the next token is a `(', then we have a parenthesized
5464      type-id.  */
5465   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5466     {
5467       /* Consume the `('.  */
5468       cp_lexer_consume_token (parser->lexer);
5469       /* Parse the type-id.  */
5470       type = cp_parser_type_id (parser);
5471       /* Look for the closing `)'.  */
5472       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5473       /* There should not be a direct-new-declarator in this production,
5474          but GCC used to allowed this, so we check and emit a sensible error
5475          message for this case.  */
5476       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5477         {
5478           error ("array bound forbidden after parenthesized type-id");
5479           inform ("try removing the parentheses around the type-id");
5480           cp_parser_direct_new_declarator (parser);
5481         }
5482       nelts = NULL_TREE;
5483     }
5484   /* Otherwise, there must be a new-type-id.  */
5485   else
5486     type = cp_parser_new_type_id (parser, &nelts);
5487
5488   /* If the next token is a `(', then we have a new-initializer.  */
5489   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5490     initializer = cp_parser_new_initializer (parser);
5491   else
5492     initializer = NULL_TREE;
5493
5494   /* A new-expression may not appear in an integral constant
5495      expression.  */
5496   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5497     return error_mark_node;
5498
5499   /* Create a representation of the new-expression.  */
5500   return build_new (placement, type, nelts, initializer, global_scope_p);
5501 }
5502
5503 /* Parse a new-placement.
5504
5505    new-placement:
5506      ( expression-list )
5507
5508    Returns the same representation as for an expression-list.  */
5509
5510 static tree
5511 cp_parser_new_placement (cp_parser* parser)
5512 {
5513   tree expression_list;
5514
5515   /* Parse the expression-list.  */
5516   expression_list = (cp_parser_parenthesized_expression_list
5517                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5518                       /*non_constant_p=*/NULL));
5519
5520   return expression_list;
5521 }
5522
5523 /* Parse a new-type-id.
5524
5525    new-type-id:
5526      type-specifier-seq new-declarator [opt]
5527
5528    Returns the TYPE allocated.  If the new-type-id indicates an array
5529    type, *NELTS is set to the number of elements in the last array
5530    bound; the TYPE will not include the last array bound.  */
5531
5532 static tree
5533 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5534 {
5535   cp_decl_specifier_seq type_specifier_seq;
5536   cp_declarator *new_declarator;
5537   cp_declarator *declarator;
5538   cp_declarator *outer_declarator;
5539   const char *saved_message;
5540   tree type;
5541
5542   /* The type-specifier sequence must not contain type definitions.
5543      (It cannot contain declarations of new types either, but if they
5544      are not definitions we will catch that because they are not
5545      complete.)  */
5546   saved_message = parser->type_definition_forbidden_message;
5547   parser->type_definition_forbidden_message
5548     = "types may not be defined in a new-type-id";
5549   /* Parse the type-specifier-seq.  */
5550   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5551                                 &type_specifier_seq);
5552   /* Restore the old message.  */
5553   parser->type_definition_forbidden_message = saved_message;
5554   /* Parse the new-declarator.  */
5555   new_declarator = cp_parser_new_declarator_opt (parser);
5556
5557   /* Determine the number of elements in the last array dimension, if
5558      any.  */
5559   *nelts = NULL_TREE;
5560   /* Skip down to the last array dimension.  */
5561   declarator = new_declarator;
5562   outer_declarator = NULL;
5563   while (declarator && (declarator->kind == cdk_pointer
5564                         || declarator->kind == cdk_ptrmem))
5565     {
5566       outer_declarator = declarator;
5567       declarator = declarator->declarator;
5568     }
5569   while (declarator
5570          && declarator->kind == cdk_array
5571          && declarator->declarator
5572          && declarator->declarator->kind == cdk_array)
5573     {
5574       outer_declarator = declarator;
5575       declarator = declarator->declarator;
5576     }
5577
5578   if (declarator && declarator->kind == cdk_array)
5579     {
5580       *nelts = declarator->u.array.bounds;
5581       if (*nelts == error_mark_node)
5582         *nelts = integer_one_node;
5583
5584       if (outer_declarator)
5585         outer_declarator->declarator = declarator->declarator;
5586       else
5587         new_declarator = NULL;
5588     }
5589
5590   type = groktypename (&type_specifier_seq, new_declarator);
5591   return type;
5592 }
5593
5594 /* Parse an (optional) new-declarator.
5595
5596    new-declarator:
5597      ptr-operator new-declarator [opt]
5598      direct-new-declarator
5599
5600    Returns the declarator.  */
5601
5602 static cp_declarator *
5603 cp_parser_new_declarator_opt (cp_parser* parser)
5604 {
5605   enum tree_code code;
5606   tree type;
5607   cp_cv_quals cv_quals;
5608
5609   /* We don't know if there's a ptr-operator next, or not.  */
5610   cp_parser_parse_tentatively (parser);
5611   /* Look for a ptr-operator.  */
5612   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5613   /* If that worked, look for more new-declarators.  */
5614   if (cp_parser_parse_definitely (parser))
5615     {
5616       cp_declarator *declarator;
5617
5618       /* Parse another optional declarator.  */
5619       declarator = cp_parser_new_declarator_opt (parser);
5620
5621       return cp_parser_make_indirect_declarator
5622         (code, type, cv_quals, declarator);
5623     }
5624
5625   /* If the next token is a `[', there is a direct-new-declarator.  */
5626   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5627     return cp_parser_direct_new_declarator (parser);
5628
5629   return NULL;
5630 }
5631
5632 /* Parse a direct-new-declarator.
5633
5634    direct-new-declarator:
5635      [ expression ]
5636      direct-new-declarator [constant-expression]
5637
5638    */
5639
5640 static cp_declarator *
5641 cp_parser_direct_new_declarator (cp_parser* parser)
5642 {
5643   cp_declarator *declarator = NULL;
5644
5645   while (true)
5646     {
5647       tree expression;
5648
5649       /* Look for the opening `['.  */
5650       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5651       /* The first expression is not required to be constant.  */
5652       if (!declarator)
5653         {
5654           expression = cp_parser_expression (parser, /*cast_p=*/false);
5655           /* The standard requires that the expression have integral
5656              type.  DR 74 adds enumeration types.  We believe that the
5657              real intent is that these expressions be handled like the
5658              expression in a `switch' condition, which also allows
5659              classes with a single conversion to integral or
5660              enumeration type.  */
5661           if (!processing_template_decl)
5662             {
5663               expression
5664                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5665                                               expression,
5666                                               /*complain=*/true);
5667               if (!expression)
5668                 {
5669                   error ("expression in new-declarator must have integral "
5670                          "or enumeration type");
5671                   expression = error_mark_node;
5672                 }
5673             }
5674         }
5675       /* But all the other expressions must be.  */
5676       else
5677         expression
5678           = cp_parser_constant_expression (parser,
5679                                            /*allow_non_constant=*/false,
5680                                            NULL);
5681       /* Look for the closing `]'.  */
5682       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5683
5684       /* Add this bound to the declarator.  */
5685       declarator = make_array_declarator (declarator, expression);
5686
5687       /* If the next token is not a `[', then there are no more
5688          bounds.  */
5689       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5690         break;
5691     }
5692
5693   return declarator;
5694 }
5695
5696 /* Parse a new-initializer.
5697
5698    new-initializer:
5699      ( expression-list [opt] )
5700
5701    Returns a representation of the expression-list.  If there is no
5702    expression-list, VOID_ZERO_NODE is returned.  */
5703
5704 static tree
5705 cp_parser_new_initializer (cp_parser* parser)
5706 {
5707   tree expression_list;
5708
5709   expression_list = (cp_parser_parenthesized_expression_list
5710                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5711                       /*non_constant_p=*/NULL));
5712   if (!expression_list)
5713     expression_list = void_zero_node;
5714
5715   return expression_list;
5716 }
5717
5718 /* Parse a delete-expression.
5719
5720    delete-expression:
5721      :: [opt] delete cast-expression
5722      :: [opt] delete [ ] cast-expression
5723
5724    Returns a representation of the expression.  */
5725
5726 static tree
5727 cp_parser_delete_expression (cp_parser* parser)
5728 {
5729   bool global_scope_p;
5730   bool array_p;
5731   tree expression;
5732
5733   /* Look for the optional `::' operator.  */
5734   global_scope_p
5735     = (cp_parser_global_scope_opt (parser,
5736                                    /*current_scope_valid_p=*/false)
5737        != NULL_TREE);
5738   /* Look for the `delete' keyword.  */
5739   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5740   /* See if the array syntax is in use.  */
5741   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5742     {
5743       /* Consume the `[' token.  */
5744       cp_lexer_consume_token (parser->lexer);
5745       /* Look for the `]' token.  */
5746       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5747       /* Remember that this is the `[]' construct.  */
5748       array_p = true;
5749     }
5750   else
5751     array_p = false;
5752
5753   /* Parse the cast-expression.  */
5754   expression = cp_parser_simple_cast_expression (parser);
5755
5756   /* A delete-expression may not appear in an integral constant
5757      expression.  */
5758   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5759     return error_mark_node;
5760
5761   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5762 }
5763
5764 /* Parse a cast-expression.
5765
5766    cast-expression:
5767      unary-expression
5768      ( type-id ) cast-expression
5769
5770    ADDRESS_P is true iff the unary-expression is appearing as the
5771    operand of the `&' operator.   CAST_P is true if this expression is
5772    the target of a cast.
5773
5774    Returns a representation of the expression.  */
5775
5776 static tree
5777 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5778 {
5779   /* If it's a `(', then we might be looking at a cast.  */
5780   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5781     {
5782       tree type = NULL_TREE;
5783       tree expr = NULL_TREE;
5784       bool compound_literal_p;
5785       const char *saved_message;
5786
5787       /* There's no way to know yet whether or not this is a cast.
5788          For example, `(int (3))' is a unary-expression, while `(int)
5789          3' is a cast.  So, we resort to parsing tentatively.  */
5790       cp_parser_parse_tentatively (parser);
5791       /* Types may not be defined in a cast.  */
5792       saved_message = parser->type_definition_forbidden_message;
5793       parser->type_definition_forbidden_message
5794         = "types may not be defined in casts";
5795       /* Consume the `('.  */
5796       cp_lexer_consume_token (parser->lexer);
5797       /* A very tricky bit is that `(struct S) { 3 }' is a
5798          compound-literal (which we permit in C++ as an extension).
5799          But, that construct is not a cast-expression -- it is a
5800          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5801          is legal; if the compound-literal were a cast-expression,
5802          you'd need an extra set of parentheses.)  But, if we parse
5803          the type-id, and it happens to be a class-specifier, then we
5804          will commit to the parse at that point, because we cannot
5805          undo the action that is done when creating a new class.  So,
5806          then we cannot back up and do a postfix-expression.
5807
5808          Therefore, we scan ahead to the closing `)', and check to see
5809          if the token after the `)' is a `{'.  If so, we are not
5810          looking at a cast-expression.
5811
5812          Save tokens so that we can put them back.  */
5813       cp_lexer_save_tokens (parser->lexer);
5814       /* Skip tokens until the next token is a closing parenthesis.
5815          If we find the closing `)', and the next token is a `{', then
5816          we are looking at a compound-literal.  */
5817       compound_literal_p
5818         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5819                                                   /*consume_paren=*/true)
5820            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5821       /* Roll back the tokens we skipped.  */
5822       cp_lexer_rollback_tokens (parser->lexer);
5823       /* If we were looking at a compound-literal, simulate an error
5824          so that the call to cp_parser_parse_definitely below will
5825          fail.  */
5826       if (compound_literal_p)
5827         cp_parser_simulate_error (parser);
5828       else
5829         {
5830           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5831           parser->in_type_id_in_expr_p = true;
5832           /* Look for the type-id.  */
5833           type = cp_parser_type_id (parser);
5834           /* Look for the closing `)'.  */
5835           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5836           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5837         }
5838
5839       /* Restore the saved message.  */
5840       parser->type_definition_forbidden_message = saved_message;
5841
5842       /* If ok so far, parse the dependent expression. We cannot be
5843          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5844          ctor of T, but looks like a cast to function returning T
5845          without a dependent expression.  */
5846       if (!cp_parser_error_occurred (parser))
5847         expr = cp_parser_cast_expression (parser,
5848                                           /*address_p=*/false,
5849                                           /*cast_p=*/true);
5850
5851       if (cp_parser_parse_definitely (parser))
5852         {
5853           /* Warn about old-style casts, if so requested.  */
5854           if (warn_old_style_cast
5855               && !in_system_header
5856               && !VOID_TYPE_P (type)
5857               && current_lang_name != lang_name_c)
5858             warning (OPT_Wold_style_cast, "use of old-style cast");
5859
5860           /* Only type conversions to integral or enumeration types
5861              can be used in constant-expressions.  */
5862           if (!cast_valid_in_integral_constant_expression_p (type)
5863               && (cp_parser_non_integral_constant_expression
5864                   (parser,
5865                    "a cast to a type other than an integral or "
5866                    "enumeration type")))
5867             return error_mark_node;
5868
5869           /* Perform the cast.  */
5870           expr = build_c_cast (type, expr);
5871           return expr;
5872         }
5873     }
5874
5875   /* If we get here, then it's not a cast, so it must be a
5876      unary-expression.  */
5877   return cp_parser_unary_expression (parser, address_p, cast_p);
5878 }
5879
5880 /* Parse a binary expression of the general form:
5881
5882    pm-expression:
5883      cast-expression
5884      pm-expression .* cast-expression
5885      pm-expression ->* cast-expression
5886
5887    multiplicative-expression:
5888      pm-expression
5889      multiplicative-expression * pm-expression
5890      multiplicative-expression / pm-expression
5891      multiplicative-expression % pm-expression
5892
5893    additive-expression:
5894      multiplicative-expression
5895      additive-expression + multiplicative-expression
5896      additive-expression - multiplicative-expression
5897
5898    shift-expression:
5899      additive-expression
5900      shift-expression << additive-expression
5901      shift-expression >> additive-expression
5902
5903    relational-expression:
5904      shift-expression
5905      relational-expression < shift-expression
5906      relational-expression > shift-expression
5907      relational-expression <= shift-expression
5908      relational-expression >= shift-expression
5909
5910   GNU Extension:
5911
5912    relational-expression:
5913      relational-expression <? shift-expression
5914      relational-expression >? shift-expression
5915
5916    equality-expression:
5917      relational-expression
5918      equality-expression == relational-expression
5919      equality-expression != relational-expression
5920
5921    and-expression:
5922      equality-expression
5923      and-expression & equality-expression
5924
5925    exclusive-or-expression:
5926      and-expression
5927      exclusive-or-expression ^ and-expression
5928
5929    inclusive-or-expression:
5930      exclusive-or-expression
5931      inclusive-or-expression | exclusive-or-expression
5932
5933    logical-and-expression:
5934      inclusive-or-expression
5935      logical-and-expression && inclusive-or-expression
5936
5937    logical-or-expression:
5938      logical-and-expression
5939      logical-or-expression || logical-and-expression
5940
5941    All these are implemented with a single function like:
5942
5943    binary-expression:
5944      simple-cast-expression
5945      binary-expression <token> binary-expression
5946
5947    CAST_P is true if this expression is the target of a cast.
5948
5949    The binops_by_token map is used to get the tree codes for each <token> type.
5950    binary-expressions are associated according to a precedence table.  */
5951
5952 #define TOKEN_PRECEDENCE(token)                              \
5953 (((token->type == CPP_GREATER                                \
5954    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5955   && !parser->greater_than_is_operator_p)                    \
5956  ? PREC_NOT_OPERATOR                                         \
5957  : binops_by_token[token->type].prec)
5958
5959 static tree
5960 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5961 {
5962   cp_parser_expression_stack stack;
5963   cp_parser_expression_stack_entry *sp = &stack[0];
5964   tree lhs, rhs;
5965   cp_token *token;
5966   enum tree_code tree_type, lhs_type, rhs_type;
5967   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5968   bool overloaded_p;
5969
5970   /* Parse the first expression.  */
5971   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5972   lhs_type = ERROR_MARK;
5973
5974   for (;;)
5975     {
5976       /* Get an operator token.  */
5977       token = cp_lexer_peek_token (parser->lexer);
5978
5979       if (warn_cxx0x_compat
5980           && token->type == CPP_RSHIFT
5981           && !parser->greater_than_is_operator_p)
5982         {
5983           warning (OPT_Wc__0x_compat, 
5984                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
5985                    &token->location);
5986           warning (OPT_Wc__0x_compat, 
5987                    "suggest parentheses around %<>>%> expression");
5988         }
5989
5990       new_prec = TOKEN_PRECEDENCE (token);
5991
5992       /* Popping an entry off the stack means we completed a subexpression:
5993          - either we found a token which is not an operator (`>' where it is not
5994            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5995            will happen repeatedly;
5996          - or, we found an operator which has lower priority.  This is the case
5997            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5998            parsing `3 * 4'.  */
5999       if (new_prec <= prec)
6000         {
6001           if (sp == stack)
6002             break;
6003           else
6004             goto pop;
6005         }
6006
6007      get_rhs:
6008       tree_type = binops_by_token[token->type].tree_type;
6009
6010       /* We used the operator token.  */
6011       cp_lexer_consume_token (parser->lexer);
6012
6013       /* Extract another operand.  It may be the RHS of this expression
6014          or the LHS of a new, higher priority expression.  */
6015       rhs = cp_parser_simple_cast_expression (parser);
6016       rhs_type = ERROR_MARK;
6017
6018       /* Get another operator token.  Look up its precedence to avoid
6019          building a useless (immediately popped) stack entry for common
6020          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6021       token = cp_lexer_peek_token (parser->lexer);
6022       lookahead_prec = TOKEN_PRECEDENCE (token);
6023       if (lookahead_prec > new_prec)
6024         {
6025           /* ... and prepare to parse the RHS of the new, higher priority
6026              expression.  Since precedence levels on the stack are
6027              monotonically increasing, we do not have to care about
6028              stack overflows.  */
6029           sp->prec = prec;
6030           sp->tree_type = tree_type;
6031           sp->lhs = lhs;
6032           sp->lhs_type = lhs_type;
6033           sp++;
6034           lhs = rhs;
6035           lhs_type = rhs_type;
6036           prec = new_prec;
6037           new_prec = lookahead_prec;
6038           goto get_rhs;
6039
6040          pop:
6041           /* If the stack is not empty, we have parsed into LHS the right side
6042              (`4' in the example above) of an expression we had suspended.
6043              We can use the information on the stack to recover the LHS (`3')
6044              from the stack together with the tree code (`MULT_EXPR'), and
6045              the precedence of the higher level subexpression
6046              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6047              which will be used to actually build the additive expression.  */
6048           --sp;
6049           prec = sp->prec;
6050           tree_type = sp->tree_type;
6051           rhs = lhs;
6052           rhs_type = lhs_type;
6053           lhs = sp->lhs;
6054           lhs_type = sp->lhs_type;
6055         }
6056
6057       overloaded_p = false;
6058       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6059                                &overloaded_p);
6060       lhs_type = tree_type;
6061
6062       /* If the binary operator required the use of an overloaded operator,
6063          then this expression cannot be an integral constant-expression.
6064          An overloaded operator can be used even if both operands are
6065          otherwise permissible in an integral constant-expression if at
6066          least one of the operands is of enumeration type.  */
6067
6068       if (overloaded_p
6069           && (cp_parser_non_integral_constant_expression
6070               (parser, "calls to overloaded operators")))
6071         return error_mark_node;
6072     }
6073
6074   return lhs;
6075 }
6076
6077
6078 /* Parse the `? expression : assignment-expression' part of a
6079    conditional-expression.  The LOGICAL_OR_EXPR is the
6080    logical-or-expression that started the conditional-expression.
6081    Returns a representation of the entire conditional-expression.
6082
6083    This routine is used by cp_parser_assignment_expression.
6084
6085      ? expression : assignment-expression
6086
6087    GNU Extensions:
6088
6089      ? : assignment-expression */
6090
6091 static tree
6092 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6093 {
6094   tree expr;
6095   tree assignment_expr;
6096
6097   /* Consume the `?' token.  */
6098   cp_lexer_consume_token (parser->lexer);
6099   if (cp_parser_allow_gnu_extensions_p (parser)
6100       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6101     /* Implicit true clause.  */
6102     expr = NULL_TREE;
6103   else
6104     /* Parse the expression.  */
6105     expr = cp_parser_expression (parser, /*cast_p=*/false);
6106
6107   /* The next token should be a `:'.  */
6108   cp_parser_require (parser, CPP_COLON, "`:'");
6109   /* Parse the assignment-expression.  */
6110   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6111
6112   /* Build the conditional-expression.  */
6113   return build_x_conditional_expr (logical_or_expr,
6114                                    expr,
6115                                    assignment_expr);
6116 }
6117
6118 /* Parse an assignment-expression.
6119
6120    assignment-expression:
6121      conditional-expression
6122      logical-or-expression assignment-operator assignment_expression
6123      throw-expression
6124
6125    CAST_P is true if this expression is the target of a cast.
6126
6127    Returns a representation for the expression.  */
6128
6129 static tree
6130 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6131 {
6132   tree expr;
6133
6134   /* If the next token is the `throw' keyword, then we're looking at
6135      a throw-expression.  */
6136   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6137     expr = cp_parser_throw_expression (parser);
6138   /* Otherwise, it must be that we are looking at a
6139      logical-or-expression.  */
6140   else
6141     {
6142       /* Parse the binary expressions (logical-or-expression).  */
6143       expr = cp_parser_binary_expression (parser, cast_p);
6144       /* If the next token is a `?' then we're actually looking at a
6145          conditional-expression.  */
6146       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6147         return cp_parser_question_colon_clause (parser, expr);
6148       else
6149         {
6150           enum tree_code assignment_operator;
6151
6152           /* If it's an assignment-operator, we're using the second
6153              production.  */
6154           assignment_operator
6155             = cp_parser_assignment_operator_opt (parser);
6156           if (assignment_operator != ERROR_MARK)
6157             {
6158               tree rhs;
6159
6160               /* Parse the right-hand side of the assignment.  */
6161               rhs = cp_parser_assignment_expression (parser, cast_p);
6162               /* An assignment may not appear in a
6163                  constant-expression.  */
6164               if (cp_parser_non_integral_constant_expression (parser,
6165                                                               "an assignment"))
6166                 return error_mark_node;
6167               /* Build the assignment expression.  */
6168               expr = build_x_modify_expr (expr,
6169                                           assignment_operator,
6170                                           rhs);
6171             }
6172         }
6173     }
6174
6175   return expr;
6176 }
6177
6178 /* Parse an (optional) assignment-operator.
6179
6180    assignment-operator: one of
6181      = *= /= %= += -= >>= <<= &= ^= |=
6182
6183    GNU Extension:
6184
6185    assignment-operator: one of
6186      <?= >?=
6187
6188    If the next token is an assignment operator, the corresponding tree
6189    code is returned, and the token is consumed.  For example, for
6190    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6191    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6192    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6193    operator, ERROR_MARK is returned.  */
6194
6195 static enum tree_code
6196 cp_parser_assignment_operator_opt (cp_parser* parser)
6197 {
6198   enum tree_code op;
6199   cp_token *token;
6200
6201   /* Peek at the next toen.  */
6202   token = cp_lexer_peek_token (parser->lexer);
6203
6204   switch (token->type)
6205     {
6206     case CPP_EQ:
6207       op = NOP_EXPR;
6208       break;
6209
6210     case CPP_MULT_EQ:
6211       op = MULT_EXPR;
6212       break;
6213
6214     case CPP_DIV_EQ:
6215       op = TRUNC_DIV_EXPR;
6216       break;
6217
6218     case CPP_MOD_EQ:
6219       op = TRUNC_MOD_EXPR;
6220       break;
6221
6222     case CPP_PLUS_EQ:
6223       op = PLUS_EXPR;
6224       break;
6225
6226     case CPP_MINUS_EQ:
6227       op = MINUS_EXPR;
6228       break;
6229
6230     case CPP_RSHIFT_EQ:
6231       op = RSHIFT_EXPR;
6232       break;
6233
6234     case CPP_LSHIFT_EQ:
6235       op = LSHIFT_EXPR;
6236       break;
6237
6238     case CPP_AND_EQ:
6239       op = BIT_AND_EXPR;
6240       break;
6241
6242     case CPP_XOR_EQ:
6243       op = BIT_XOR_EXPR;
6244       break;
6245
6246     case CPP_OR_EQ:
6247       op = BIT_IOR_EXPR;
6248       break;
6249
6250     default:
6251       /* Nothing else is an assignment operator.  */
6252       op = ERROR_MARK;
6253     }
6254
6255   /* If it was an assignment operator, consume it.  */
6256   if (op != ERROR_MARK)
6257     cp_lexer_consume_token (parser->lexer);
6258
6259   return op;
6260 }
6261
6262 /* Parse an expression.
6263
6264    expression:
6265      assignment-expression
6266      expression , assignment-expression
6267
6268    CAST_P is true if this expression is the target of a cast.
6269
6270    Returns a representation of the expression.  */
6271
6272 static tree
6273 cp_parser_expression (cp_parser* parser, bool cast_p)
6274 {
6275   tree expression = NULL_TREE;
6276
6277   while (true)
6278     {
6279       tree assignment_expression;
6280
6281       /* Parse the next assignment-expression.  */
6282       assignment_expression
6283         = cp_parser_assignment_expression (parser, cast_p);
6284       /* If this is the first assignment-expression, we can just
6285          save it away.  */
6286       if (!expression)
6287         expression = assignment_expression;
6288       else
6289         expression = build_x_compound_expr (expression,
6290                                             assignment_expression);
6291       /* If the next token is not a comma, then we are done with the
6292          expression.  */
6293       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6294         break;
6295       /* Consume the `,'.  */
6296       cp_lexer_consume_token (parser->lexer);
6297       /* A comma operator cannot appear in a constant-expression.  */
6298       if (cp_parser_non_integral_constant_expression (parser,
6299                                                       "a comma operator"))
6300         expression = error_mark_node;
6301     }
6302
6303   return expression;
6304 }
6305
6306 /* Parse a constant-expression.
6307
6308    constant-expression:
6309      conditional-expression
6310
6311   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6312   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6313   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6314   is false, NON_CONSTANT_P should be NULL.  */
6315
6316 static tree
6317 cp_parser_constant_expression (cp_parser* parser,
6318                                bool allow_non_constant_p,
6319                                bool *non_constant_p)
6320 {
6321   bool saved_integral_constant_expression_p;
6322   bool saved_allow_non_integral_constant_expression_p;
6323   bool saved_non_integral_constant_expression_p;
6324   tree expression;
6325
6326   /* It might seem that we could simply parse the
6327      conditional-expression, and then check to see if it were
6328      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6329      one that the compiler can figure out is constant, possibly after
6330      doing some simplifications or optimizations.  The standard has a
6331      precise definition of constant-expression, and we must honor
6332      that, even though it is somewhat more restrictive.
6333
6334      For example:
6335
6336        int i[(2, 3)];
6337
6338      is not a legal declaration, because `(2, 3)' is not a
6339      constant-expression.  The `,' operator is forbidden in a
6340      constant-expression.  However, GCC's constant-folding machinery
6341      will fold this operation to an INTEGER_CST for `3'.  */
6342
6343   /* Save the old settings.  */
6344   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6345   saved_allow_non_integral_constant_expression_p
6346     = parser->allow_non_integral_constant_expression_p;
6347   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6348   /* We are now parsing a constant-expression.  */
6349   parser->integral_constant_expression_p = true;
6350   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6351   parser->non_integral_constant_expression_p = false;
6352   /* Although the grammar says "conditional-expression", we parse an
6353      "assignment-expression", which also permits "throw-expression"
6354      and the use of assignment operators.  In the case that
6355      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6356      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6357      actually essential that we look for an assignment-expression.
6358      For example, cp_parser_initializer_clauses uses this function to
6359      determine whether a particular assignment-expression is in fact
6360      constant.  */
6361   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6362   /* Restore the old settings.  */
6363   parser->integral_constant_expression_p
6364     = saved_integral_constant_expression_p;
6365   parser->allow_non_integral_constant_expression_p
6366     = saved_allow_non_integral_constant_expression_p;
6367   if (allow_non_constant_p)
6368     *non_constant_p = parser->non_integral_constant_expression_p;
6369   else if (parser->non_integral_constant_expression_p)
6370     expression = error_mark_node;
6371   parser->non_integral_constant_expression_p
6372     = saved_non_integral_constant_expression_p;
6373
6374   return expression;
6375 }
6376
6377 /* Parse __builtin_offsetof.
6378
6379    offsetof-expression:
6380      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6381
6382    offsetof-member-designator:
6383      id-expression
6384      | offsetof-member-designator "." id-expression
6385      | offsetof-member-designator "[" expression "]"  */
6386
6387 static tree
6388 cp_parser_builtin_offsetof (cp_parser *parser)
6389 {
6390   int save_ice_p, save_non_ice_p;
6391   tree type, expr;
6392   cp_id_kind dummy;
6393
6394   /* We're about to accept non-integral-constant things, but will
6395      definitely yield an integral constant expression.  Save and
6396      restore these values around our local parsing.  */
6397   save_ice_p = parser->integral_constant_expression_p;
6398   save_non_ice_p = parser->non_integral_constant_expression_p;
6399
6400   /* Consume the "__builtin_offsetof" token.  */
6401   cp_lexer_consume_token (parser->lexer);
6402   /* Consume the opening `('.  */
6403   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6404   /* Parse the type-id.  */
6405   type = cp_parser_type_id (parser);
6406   /* Look for the `,'.  */
6407   cp_parser_require (parser, CPP_COMMA, "`,'");
6408
6409   /* Build the (type *)null that begins the traditional offsetof macro.  */
6410   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6411
6412   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6413   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6414                                                  true, &dummy);
6415   while (true)
6416     {
6417       cp_token *token = cp_lexer_peek_token (parser->lexer);
6418       switch (token->type)
6419         {
6420         case CPP_OPEN_SQUARE:
6421           /* offsetof-member-designator "[" expression "]" */
6422           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6423           break;
6424
6425         case CPP_DOT:
6426           /* offsetof-member-designator "." identifier */
6427           cp_lexer_consume_token (parser->lexer);
6428           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6429                                                          true, &dummy);
6430           break;
6431
6432         case CPP_CLOSE_PAREN:
6433           /* Consume the ")" token.  */
6434           cp_lexer_consume_token (parser->lexer);
6435           goto success;
6436
6437         default:
6438           /* Error.  We know the following require will fail, but
6439              that gives the proper error message.  */
6440           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6441           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6442           expr = error_mark_node;
6443           goto failure;
6444         }
6445     }
6446
6447  success:
6448   /* If we're processing a template, we can't finish the semantics yet.
6449      Otherwise we can fold the entire expression now.  */
6450   if (processing_template_decl)
6451     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6452   else
6453     expr = finish_offsetof (expr);
6454
6455  failure:
6456   parser->integral_constant_expression_p = save_ice_p;
6457   parser->non_integral_constant_expression_p = save_non_ice_p;
6458
6459   return expr;
6460 }
6461
6462 /* Parse a trait expression.  */
6463
6464 static tree
6465 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6466 {
6467   cp_trait_kind kind;
6468   tree type1, type2 = NULL_TREE;
6469   bool binary = false;
6470   cp_decl_specifier_seq decl_specs;
6471
6472   switch (keyword)
6473     {
6474     case RID_HAS_NOTHROW_ASSIGN:
6475       kind = CPTK_HAS_NOTHROW_ASSIGN;
6476       break;
6477     case RID_HAS_NOTHROW_CONSTRUCTOR:
6478       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6479       break;
6480     case RID_HAS_NOTHROW_COPY:
6481       kind = CPTK_HAS_NOTHROW_COPY;
6482       break;
6483     case RID_HAS_TRIVIAL_ASSIGN:
6484       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6485       break;
6486     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6487       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6488       break;
6489     case RID_HAS_TRIVIAL_COPY:
6490       kind = CPTK_HAS_TRIVIAL_COPY;
6491       break;
6492     case RID_HAS_TRIVIAL_DESTRUCTOR:
6493       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6494       break;
6495     case RID_HAS_VIRTUAL_DESTRUCTOR:
6496       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6497       break;
6498     case RID_IS_ABSTRACT:
6499       kind = CPTK_IS_ABSTRACT;
6500       break;
6501     case RID_IS_BASE_OF:
6502       kind = CPTK_IS_BASE_OF;
6503       binary = true;
6504       break;
6505     case RID_IS_CLASS:
6506       kind = CPTK_IS_CLASS;
6507       break;
6508     case RID_IS_CONVERTIBLE_TO:
6509       kind = CPTK_IS_CONVERTIBLE_TO;
6510       binary = true;
6511       break;
6512     case RID_IS_EMPTY:
6513       kind = CPTK_IS_EMPTY;
6514       break;
6515     case RID_IS_ENUM:
6516       kind = CPTK_IS_ENUM;
6517       break;
6518     case RID_IS_POD:
6519       kind = CPTK_IS_POD;
6520       break;
6521     case RID_IS_POLYMORPHIC:
6522       kind = CPTK_IS_POLYMORPHIC;
6523       break;
6524     case RID_IS_UNION:
6525       kind = CPTK_IS_UNION;
6526       break;
6527     default:
6528       gcc_unreachable ();
6529     }
6530
6531   /* Consume the token.  */
6532   cp_lexer_consume_token (parser->lexer);
6533
6534   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6535
6536   type1 = cp_parser_type_id (parser);
6537
6538   if (type1 == error_mark_node)
6539     return error_mark_node;
6540
6541   /* Build a trivial decl-specifier-seq.  */
6542   clear_decl_specs (&decl_specs);
6543   decl_specs.type = type1;
6544
6545   /* Call grokdeclarator to figure out what type this is.  */
6546   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6547                           /*initialized=*/0, /*attrlist=*/NULL);
6548
6549   if (binary)
6550     {
6551       cp_parser_require (parser, CPP_COMMA, "`,'");
6552  
6553       type2 = cp_parser_type_id (parser);
6554
6555       if (type2 == error_mark_node)
6556         return error_mark_node;
6557
6558       /* Build a trivial decl-specifier-seq.  */
6559       clear_decl_specs (&decl_specs);
6560       decl_specs.type = type2;
6561
6562       /* Call grokdeclarator to figure out what type this is.  */
6563       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6564                               /*initialized=*/0, /*attrlist=*/NULL);
6565     }
6566
6567   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6568
6569   /* Complete the trait expression, which may mean either processing
6570      the trait expr now or saving it for template instantiation.  */
6571   return finish_trait_expr (kind, type1, type2);
6572 }
6573
6574 /* Statements [gram.stmt.stmt]  */
6575
6576 /* Parse a statement.
6577
6578    statement:
6579      labeled-statement
6580      expression-statement
6581      compound-statement
6582      selection-statement
6583      iteration-statement
6584      jump-statement
6585      declaration-statement
6586      try-block
6587
6588   IN_COMPOUND is true when the statement is nested inside a
6589   cp_parser_compound_statement; this matters for certain pragmas.
6590
6591   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6592   is a (possibly labeled) if statement which is not enclosed in braces
6593   and has an else clause.  This is used to implement -Wparentheses.  */
6594
6595 static void
6596 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6597                      bool in_compound, bool *if_p)
6598 {
6599   tree statement;
6600   cp_token *token;
6601   location_t statement_location;
6602
6603  restart:
6604   if (if_p != NULL)
6605     *if_p = false;
6606   /* There is no statement yet.  */
6607   statement = NULL_TREE;
6608   /* Peek at the next token.  */
6609   token = cp_lexer_peek_token (parser->lexer);
6610   /* Remember the location of the first token in the statement.  */
6611   statement_location = token->location;
6612   /* If this is a keyword, then that will often determine what kind of
6613      statement we have.  */
6614   if (token->type == CPP_KEYWORD)
6615     {
6616       enum rid keyword = token->keyword;
6617
6618       switch (keyword)
6619         {
6620         case RID_CASE:
6621         case RID_DEFAULT:
6622           /* Looks like a labeled-statement with a case label.
6623              Parse the label, and then use tail recursion to parse
6624              the statement.  */
6625           cp_parser_label_for_labeled_statement (parser);
6626           goto restart;
6627
6628         case RID_IF:
6629         case RID_SWITCH:
6630           statement = cp_parser_selection_statement (parser, if_p);
6631           break;
6632
6633         case RID_WHILE:
6634         case RID_DO:
6635         case RID_FOR:
6636           statement = cp_parser_iteration_statement (parser);
6637           break;
6638
6639         case RID_BREAK:
6640         case RID_CONTINUE:
6641         case RID_RETURN:
6642         case RID_GOTO:
6643           statement = cp_parser_jump_statement (parser);
6644           break;
6645
6646           /* Objective-C++ exception-handling constructs.  */
6647         case RID_AT_TRY:
6648         case RID_AT_CATCH:
6649         case RID_AT_FINALLY:
6650         case RID_AT_SYNCHRONIZED:
6651         case RID_AT_THROW:
6652           statement = cp_parser_objc_statement (parser);
6653           break;
6654
6655         case RID_TRY:
6656           statement = cp_parser_try_block (parser);
6657           break;
6658
6659         case RID_NAMESPACE:
6660           /* This must be a namespace alias definition.  */
6661           cp_parser_declaration_statement (parser);
6662           return;
6663           
6664         default:
6665           /* It might be a keyword like `int' that can start a
6666              declaration-statement.  */
6667           break;
6668         }
6669     }
6670   else if (token->type == CPP_NAME)
6671     {
6672       /* If the next token is a `:', then we are looking at a
6673          labeled-statement.  */
6674       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6675       if (token->type == CPP_COLON)
6676         {
6677           /* Looks like a labeled-statement with an ordinary label.
6678              Parse the label, and then use tail recursion to parse
6679              the statement.  */
6680           cp_parser_label_for_labeled_statement (parser);
6681           goto restart;
6682         }
6683     }
6684   /* Anything that starts with a `{' must be a compound-statement.  */
6685   else if (token->type == CPP_OPEN_BRACE)
6686     statement = cp_parser_compound_statement (parser, NULL, false);
6687   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6688      a statement all its own.  */
6689   else if (token->type == CPP_PRAGMA)
6690     {
6691       /* Only certain OpenMP pragmas are attached to statements, and thus
6692          are considered statements themselves.  All others are not.  In
6693          the context of a compound, accept the pragma as a "statement" and
6694          return so that we can check for a close brace.  Otherwise we
6695          require a real statement and must go back and read one.  */
6696       if (in_compound)
6697         cp_parser_pragma (parser, pragma_compound);
6698       else if (!cp_parser_pragma (parser, pragma_stmt))
6699         goto restart;
6700       return;
6701     }
6702   else if (token->type == CPP_EOF)
6703     {
6704       cp_parser_error (parser, "expected statement");
6705       return;
6706     }
6707
6708   /* Everything else must be a declaration-statement or an
6709      expression-statement.  Try for the declaration-statement
6710      first, unless we are looking at a `;', in which case we know that
6711      we have an expression-statement.  */
6712   if (!statement)
6713     {
6714       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6715         {
6716           cp_parser_parse_tentatively (parser);
6717           /* Try to parse the declaration-statement.  */
6718           cp_parser_declaration_statement (parser);
6719           /* If that worked, we're done.  */
6720           if (cp_parser_parse_definitely (parser))
6721             return;
6722         }
6723       /* Look for an expression-statement instead.  */
6724       statement = cp_parser_expression_statement (parser, in_statement_expr);
6725     }
6726
6727   /* Set the line number for the statement.  */
6728   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6729     SET_EXPR_LOCATION (statement, statement_location);
6730 }
6731
6732 /* Parse the label for a labeled-statement, i.e.
6733
6734    identifier :
6735    case constant-expression :
6736    default :
6737
6738    GNU Extension:
6739    case constant-expression ... constant-expression : statement
6740
6741    When a label is parsed without errors, the label is added to the
6742    parse tree by the finish_* functions, so this function doesn't
6743    have to return the label.  */
6744
6745 static void
6746 cp_parser_label_for_labeled_statement (cp_parser* parser)
6747 {
6748   cp_token *token;
6749
6750   /* The next token should be an identifier.  */
6751   token = cp_lexer_peek_token (parser->lexer);
6752   if (token->type != CPP_NAME
6753       && token->type != CPP_KEYWORD)
6754     {
6755       cp_parser_error (parser, "expected labeled-statement");
6756       return;
6757     }
6758
6759   switch (token->keyword)
6760     {
6761     case RID_CASE:
6762       {
6763         tree expr, expr_hi;
6764         cp_token *ellipsis;
6765
6766         /* Consume the `case' token.  */
6767         cp_lexer_consume_token (parser->lexer);
6768         /* Parse the constant-expression.  */
6769         expr = cp_parser_constant_expression (parser,
6770                                               /*allow_non_constant_p=*/false,
6771                                               NULL);
6772
6773         ellipsis = cp_lexer_peek_token (parser->lexer);
6774         if (ellipsis->type == CPP_ELLIPSIS)
6775           {
6776             /* Consume the `...' token.  */
6777             cp_lexer_consume_token (parser->lexer);
6778             expr_hi =
6779               cp_parser_constant_expression (parser,
6780                                              /*allow_non_constant_p=*/false,
6781                                              NULL);
6782             /* We don't need to emit warnings here, as the common code
6783                will do this for us.  */
6784           }
6785         else
6786           expr_hi = NULL_TREE;
6787
6788         if (parser->in_switch_statement_p)
6789           finish_case_label (expr, expr_hi);
6790         else
6791           error ("case label %qE not within a switch statement", expr);
6792       }
6793       break;
6794
6795     case RID_DEFAULT:
6796       /* Consume the `default' token.  */
6797       cp_lexer_consume_token (parser->lexer);
6798
6799       if (parser->in_switch_statement_p)
6800         finish_case_label (NULL_TREE, NULL_TREE);
6801       else
6802         error ("case label not within a switch statement");
6803       break;
6804
6805     default:
6806       /* Anything else must be an ordinary label.  */
6807       finish_label_stmt (cp_parser_identifier (parser));
6808       break;
6809     }
6810
6811   /* Require the `:' token.  */
6812   cp_parser_require (parser, CPP_COLON, "`:'");
6813 }
6814
6815 /* Parse an expression-statement.
6816
6817    expression-statement:
6818      expression [opt] ;
6819
6820    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6821    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6822    indicates whether this expression-statement is part of an
6823    expression statement.  */
6824
6825 static tree
6826 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6827 {
6828   tree statement = NULL_TREE;
6829
6830   /* If the next token is a ';', then there is no expression
6831      statement.  */
6832   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6833     statement = cp_parser_expression (parser, /*cast_p=*/false);
6834
6835   /* Consume the final `;'.  */
6836   cp_parser_consume_semicolon_at_end_of_statement (parser);
6837
6838   if (in_statement_expr
6839       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6840     /* This is the final expression statement of a statement
6841        expression.  */
6842     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6843   else if (statement)
6844     statement = finish_expr_stmt (statement);
6845   else
6846     finish_stmt ();
6847
6848   return statement;
6849 }
6850
6851 /* Parse a compound-statement.
6852
6853    compound-statement:
6854      { statement-seq [opt] }
6855
6856    GNU extension:
6857
6858    compound-statement:
6859      { label-declaration-seq [opt] statement-seq [opt] }
6860
6861    label-declaration-seq:
6862      label-declaration
6863      label-declaration-seq label-declaration
6864
6865    Returns a tree representing the statement.  */
6866
6867 static tree
6868 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6869                               bool in_try)
6870 {
6871   tree compound_stmt;
6872
6873   /* Consume the `{'.  */
6874   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6875     return error_mark_node;
6876   /* Begin the compound-statement.  */
6877   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6878   /* If the next keyword is `__label__' we have a label declaration.  */
6879   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
6880     cp_parser_label_declaration (parser);
6881   /* Parse an (optional) statement-seq.  */
6882   cp_parser_statement_seq_opt (parser, in_statement_expr);
6883   /* Finish the compound-statement.  */
6884   finish_compound_stmt (compound_stmt);
6885   /* Consume the `}'.  */
6886   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6887
6888   return compound_stmt;
6889 }
6890
6891 /* Parse an (optional) statement-seq.
6892
6893    statement-seq:
6894      statement
6895      statement-seq [opt] statement  */
6896
6897 static void
6898 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6899 {
6900   /* Scan statements until there aren't any more.  */
6901   while (true)
6902     {
6903       cp_token *token = cp_lexer_peek_token (parser->lexer);
6904
6905       /* If we're looking at a `}', then we've run out of statements.  */
6906       if (token->type == CPP_CLOSE_BRACE
6907           || token->type == CPP_EOF
6908           || token->type == CPP_PRAGMA_EOL)
6909         break;
6910       
6911       /* If we are in a compound statement and find 'else' then
6912          something went wrong.  */
6913       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6914         {
6915           if (parser->in_statement & IN_IF_STMT) 
6916             break;
6917           else
6918             {
6919               token = cp_lexer_consume_token (parser->lexer);
6920               error ("%<else%> without a previous %<if%>");
6921             }
6922         }
6923
6924       /* Parse the statement.  */
6925       cp_parser_statement (parser, in_statement_expr, true, NULL);
6926     }
6927 }
6928
6929 /* Parse a selection-statement.
6930
6931    selection-statement:
6932      if ( condition ) statement
6933      if ( condition ) statement else statement
6934      switch ( condition ) statement
6935
6936    Returns the new IF_STMT or SWITCH_STMT.
6937
6938    If IF_P is not NULL, *IF_P is set to indicate whether the statement
6939    is a (possibly labeled) if statement which is not enclosed in
6940    braces and has an else clause.  This is used to implement
6941    -Wparentheses.  */
6942
6943 static tree
6944 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6945 {
6946   cp_token *token;
6947   enum rid keyword;
6948
6949   if (if_p != NULL)
6950     *if_p = false;
6951
6952   /* Peek at the next token.  */
6953   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6954
6955   /* See what kind of keyword it is.  */
6956   keyword = token->keyword;
6957   switch (keyword)
6958     {
6959     case RID_IF:
6960     case RID_SWITCH:
6961       {
6962         tree statement;
6963         tree condition;
6964
6965         /* Look for the `('.  */
6966         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6967           {
6968             cp_parser_skip_to_end_of_statement (parser);
6969             return error_mark_node;
6970           }
6971
6972         /* Begin the selection-statement.  */
6973         if (keyword == RID_IF)
6974           statement = begin_if_stmt ();
6975         else
6976           statement = begin_switch_stmt ();
6977
6978         /* Parse the condition.  */
6979         condition = cp_parser_condition (parser);
6980         /* Look for the `)'.  */
6981         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6982           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6983                                                  /*consume_paren=*/true);
6984
6985         if (keyword == RID_IF)
6986           {
6987             bool nested_if;
6988             unsigned char in_statement;
6989
6990             /* Add the condition.  */
6991             finish_if_stmt_cond (condition, statement);
6992
6993             /* Parse the then-clause.  */
6994             in_statement = parser->in_statement;
6995             parser->in_statement |= IN_IF_STMT;
6996             cp_parser_implicitly_scoped_statement (parser, &nested_if);
6997             parser->in_statement = in_statement;
6998
6999             finish_then_clause (statement);
7000
7001             /* If the next token is `else', parse the else-clause.  */
7002             if (cp_lexer_next_token_is_keyword (parser->lexer,
7003                                                 RID_ELSE))
7004               {
7005                 /* Consume the `else' keyword.  */
7006                 cp_lexer_consume_token (parser->lexer);
7007                 begin_else_clause (statement);
7008                 /* Parse the else-clause.  */
7009                 cp_parser_implicitly_scoped_statement (parser, NULL);
7010                 finish_else_clause (statement);
7011
7012                 /* If we are currently parsing a then-clause, then
7013                    IF_P will not be NULL.  We set it to true to
7014                    indicate that this if statement has an else clause.
7015                    This may trigger the Wparentheses warning below
7016                    when we get back up to the parent if statement.  */
7017                 if (if_p != NULL)
7018                   *if_p = true;
7019               }
7020             else
7021               {
7022                 /* This if statement does not have an else clause.  If
7023                    NESTED_IF is true, then the then-clause is an if
7024                    statement which does have an else clause.  We warn
7025                    about the potential ambiguity.  */
7026                 if (nested_if)
7027                   warning (OPT_Wparentheses,
7028                            ("%Hsuggest explicit braces "
7029                             "to avoid ambiguous %<else%>"),
7030                            EXPR_LOCUS (statement));
7031               }
7032
7033             /* Now we're all done with the if-statement.  */
7034             finish_if_stmt (statement);
7035           }
7036         else
7037           {
7038             bool in_switch_statement_p;
7039             unsigned char in_statement;
7040
7041             /* Add the condition.  */
7042             finish_switch_cond (condition, statement);
7043
7044             /* Parse the body of the switch-statement.  */
7045             in_switch_statement_p = parser->in_switch_statement_p;
7046             in_statement = parser->in_statement;
7047             parser->in_switch_statement_p = true;
7048             parser->in_statement |= IN_SWITCH_STMT;
7049             cp_parser_implicitly_scoped_statement (parser, NULL);
7050             parser->in_switch_statement_p = in_switch_statement_p;
7051             parser->in_statement = in_statement;
7052
7053             /* Now we're all done with the switch-statement.  */
7054             finish_switch_stmt (statement);
7055           }
7056
7057         return statement;
7058       }
7059       break;
7060
7061     default:
7062       cp_parser_error (parser, "expected selection-statement");
7063       return error_mark_node;
7064     }
7065 }
7066
7067 /* Parse a condition.
7068
7069    condition:
7070      expression
7071      type-specifier-seq declarator = assignment-expression
7072
7073    GNU Extension:
7074
7075    condition:
7076      type-specifier-seq declarator asm-specification [opt]
7077        attributes [opt] = assignment-expression
7078
7079    Returns the expression that should be tested.  */
7080
7081 static tree
7082 cp_parser_condition (cp_parser* parser)
7083 {
7084   cp_decl_specifier_seq type_specifiers;
7085   const char *saved_message;
7086
7087   /* Try the declaration first.  */
7088   cp_parser_parse_tentatively (parser);
7089   /* New types are not allowed in the type-specifier-seq for a
7090      condition.  */
7091   saved_message = parser->type_definition_forbidden_message;
7092   parser->type_definition_forbidden_message
7093     = "types may not be defined in conditions";
7094   /* Parse the type-specifier-seq.  */
7095   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7096                                 &type_specifiers);
7097   /* Restore the saved message.  */
7098   parser->type_definition_forbidden_message = saved_message;
7099   /* If all is well, we might be looking at a declaration.  */
7100   if (!cp_parser_error_occurred (parser))
7101     {
7102       tree decl;
7103       tree asm_specification;
7104       tree attributes;
7105       cp_declarator *declarator;
7106       tree initializer = NULL_TREE;
7107
7108       /* Parse the declarator.  */
7109       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7110                                          /*ctor_dtor_or_conv_p=*/NULL,
7111                                          /*parenthesized_p=*/NULL,
7112                                          /*member_p=*/false);
7113       /* Parse the attributes.  */
7114       attributes = cp_parser_attributes_opt (parser);
7115       /* Parse the asm-specification.  */
7116       asm_specification = cp_parser_asm_specification_opt (parser);
7117       /* If the next token is not an `=', then we might still be
7118          looking at an expression.  For example:
7119
7120            if (A(a).x)
7121
7122          looks like a decl-specifier-seq and a declarator -- but then
7123          there is no `=', so this is an expression.  */
7124       cp_parser_require (parser, CPP_EQ, "`='");
7125       /* If we did see an `=', then we are looking at a declaration
7126          for sure.  */
7127       if (cp_parser_parse_definitely (parser))
7128         {
7129           tree pushed_scope;
7130           bool non_constant_p;
7131
7132           /* Create the declaration.  */
7133           decl = start_decl (declarator, &type_specifiers,
7134                              /*initialized_p=*/true,
7135                              attributes, /*prefix_attributes=*/NULL_TREE,
7136                              &pushed_scope);
7137           /* Parse the assignment-expression.  */
7138           initializer
7139             = cp_parser_constant_expression (parser,
7140                                              /*allow_non_constant_p=*/true,
7141                                              &non_constant_p);
7142           if (!non_constant_p)
7143             initializer = fold_non_dependent_expr (initializer);
7144
7145           /* Process the initializer.  */
7146           cp_finish_decl (decl,
7147                           initializer, !non_constant_p,
7148                           asm_specification,
7149                           LOOKUP_ONLYCONVERTING);
7150
7151           if (pushed_scope)
7152             pop_scope (pushed_scope);
7153
7154           return convert_from_reference (decl);
7155         }
7156     }
7157   /* If we didn't even get past the declarator successfully, we are
7158      definitely not looking at a declaration.  */
7159   else
7160     cp_parser_abort_tentative_parse (parser);
7161
7162   /* Otherwise, we are looking at an expression.  */
7163   return cp_parser_expression (parser, /*cast_p=*/false);
7164 }
7165
7166 /* We check for a ) immediately followed by ; with no whitespacing
7167    between.  This is used to issue a warning for:
7168
7169      while (...);
7170
7171    and:
7172
7173      for (...);
7174
7175    as the semicolon is probably extraneous.
7176
7177    On parse errors, the next token might not be a ), so do nothing in
7178    that case. */
7179
7180 static void
7181 check_empty_body (cp_parser* parser, const char* type)
7182 {
7183   cp_token *token;
7184   cp_token *close_paren;
7185   expanded_location close_loc;
7186   expanded_location semi_loc;
7187   
7188   close_paren = cp_lexer_peek_token (parser->lexer);
7189   if (close_paren->type != CPP_CLOSE_PAREN)
7190     return;
7191
7192   close_loc = expand_location (close_paren->location);
7193   token = cp_lexer_peek_nth_token (parser->lexer, 2);
7194
7195   if (token->type != CPP_SEMICOLON
7196       || (token->flags & PREV_WHITE))
7197     return;
7198
7199   semi_loc =  expand_location (token->location);
7200   if (close_loc.line == semi_loc.line
7201       && close_loc.column+1 == semi_loc.column)
7202     warning (OPT_Wempty_body,
7203              "suggest a space before %<;%> or explicit braces around empty "
7204              "body in %<%s%> statement",
7205              type);
7206 }
7207
7208 /* Parse an iteration-statement.
7209
7210    iteration-statement:
7211      while ( condition ) statement
7212      do statement while ( expression ) ;
7213      for ( for-init-statement condition [opt] ; expression [opt] )
7214        statement
7215
7216    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7217
7218 static tree
7219 cp_parser_iteration_statement (cp_parser* parser)
7220 {
7221   cp_token *token;
7222   enum rid keyword;
7223   tree statement;
7224   unsigned char in_statement;
7225
7226   /* Peek at the next token.  */
7227   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7228   if (!token)
7229     return error_mark_node;
7230
7231   /* Remember whether or not we are already within an iteration
7232      statement.  */
7233   in_statement = parser->in_statement;
7234
7235   /* See what kind of keyword it is.  */
7236   keyword = token->keyword;
7237   switch (keyword)
7238     {
7239     case RID_WHILE:
7240       {
7241         tree condition;
7242
7243         /* Begin the while-statement.  */
7244         statement = begin_while_stmt ();
7245         /* Look for the `('.  */
7246         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7247         /* Parse the condition.  */
7248         condition = cp_parser_condition (parser);
7249         finish_while_stmt_cond (condition, statement);
7250         check_empty_body (parser, "while");
7251         /* Look for the `)'.  */
7252         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7253         /* Parse the dependent statement.  */
7254         parser->in_statement = IN_ITERATION_STMT;
7255         cp_parser_already_scoped_statement (parser);
7256         parser->in_statement = in_statement;
7257         /* We're done with the while-statement.  */
7258         finish_while_stmt (statement);
7259       }
7260       break;
7261
7262     case RID_DO:
7263       {
7264         tree expression;
7265
7266         /* Begin the do-statement.  */
7267         statement = begin_do_stmt ();
7268         /* Parse the body of the do-statement.  */
7269         parser->in_statement = IN_ITERATION_STMT;
7270         cp_parser_implicitly_scoped_statement (parser, NULL);
7271         parser->in_statement = in_statement;
7272         finish_do_body (statement);
7273         /* Look for the `while' keyword.  */
7274         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7275         /* Look for the `('.  */
7276         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7277         /* Parse the expression.  */
7278         expression = cp_parser_expression (parser, /*cast_p=*/false);
7279         /* We're done with the do-statement.  */
7280         finish_do_stmt (expression, statement);
7281         /* Look for the `)'.  */
7282         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7283         /* Look for the `;'.  */
7284         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7285       }
7286       break;
7287
7288     case RID_FOR:
7289       {
7290         tree condition = NULL_TREE;
7291         tree expression = NULL_TREE;
7292
7293         /* Begin the for-statement.  */
7294         statement = begin_for_stmt ();
7295         /* Look for the `('.  */
7296         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7297         /* Parse the initialization.  */
7298         cp_parser_for_init_statement (parser);
7299         finish_for_init_stmt (statement);
7300
7301         /* If there's a condition, process it.  */
7302         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7303           condition = cp_parser_condition (parser);
7304         finish_for_cond (condition, statement);
7305         /* Look for the `;'.  */
7306         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7307
7308         /* If there's an expression, process it.  */
7309         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7310           expression = cp_parser_expression (parser, /*cast_p=*/false);
7311         finish_for_expr (expression, statement);
7312         check_empty_body (parser, "for");
7313         /* Look for the `)'.  */
7314         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7315
7316         /* Parse the body of the for-statement.  */
7317         parser->in_statement = IN_ITERATION_STMT;
7318         cp_parser_already_scoped_statement (parser);
7319         parser->in_statement = in_statement;
7320
7321         /* We're done with the for-statement.  */
7322         finish_for_stmt (statement);
7323       }
7324       break;
7325
7326     default:
7327       cp_parser_error (parser, "expected iteration-statement");
7328       statement = error_mark_node;
7329       break;
7330     }
7331
7332   return statement;
7333 }
7334
7335 /* Parse a for-init-statement.
7336
7337    for-init-statement:
7338      expression-statement
7339      simple-declaration  */
7340
7341 static void
7342 cp_parser_for_init_statement (cp_parser* parser)
7343 {
7344   /* If the next token is a `;', then we have an empty
7345      expression-statement.  Grammatically, this is also a
7346      simple-declaration, but an invalid one, because it does not
7347      declare anything.  Therefore, if we did not handle this case
7348      specially, we would issue an error message about an invalid
7349      declaration.  */
7350   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7351     {
7352       /* We're going to speculatively look for a declaration, falling back
7353          to an expression, if necessary.  */
7354       cp_parser_parse_tentatively (parser);
7355       /* Parse the declaration.  */
7356       cp_parser_simple_declaration (parser,
7357                                     /*function_definition_allowed_p=*/false);
7358       /* If the tentative parse failed, then we shall need to look for an
7359          expression-statement.  */
7360       if (cp_parser_parse_definitely (parser))
7361         return;
7362     }
7363
7364   cp_parser_expression_statement (parser, false);
7365 }
7366
7367 /* Parse a jump-statement.
7368
7369    jump-statement:
7370      break ;
7371      continue ;
7372      return expression [opt] ;
7373      goto identifier ;
7374
7375    GNU extension:
7376
7377    jump-statement:
7378      goto * expression ;
7379
7380    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7381
7382 static tree
7383 cp_parser_jump_statement (cp_parser* parser)
7384 {
7385   tree statement = error_mark_node;
7386   cp_token *token;
7387   enum rid keyword;
7388   unsigned char in_statement;
7389
7390   /* Peek at the next token.  */
7391   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7392   if (!token)
7393     return error_mark_node;
7394
7395   /* See what kind of keyword it is.  */
7396   keyword = token->keyword;
7397   switch (keyword)
7398     {
7399     case RID_BREAK:
7400       in_statement = parser->in_statement & ~IN_IF_STMT;      
7401       switch (in_statement)
7402         {
7403         case 0:
7404           error ("break statement not within loop or switch");
7405           break;
7406         default:
7407           gcc_assert ((in_statement & IN_SWITCH_STMT)
7408                       || in_statement == IN_ITERATION_STMT);
7409           statement = finish_break_stmt ();
7410           break;
7411         case IN_OMP_BLOCK:
7412           error ("invalid exit from OpenMP structured block");
7413           break;
7414         case IN_OMP_FOR:
7415           error ("break statement used with OpenMP for loop");
7416           break;
7417         }
7418       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7419       break;
7420
7421     case RID_CONTINUE:
7422       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7423         {
7424         case 0:
7425           error ("continue statement not within a loop");
7426           break;
7427         case IN_ITERATION_STMT:
7428         case IN_OMP_FOR:
7429           statement = finish_continue_stmt ();
7430           break;
7431         case IN_OMP_BLOCK:
7432           error ("invalid exit from OpenMP structured block");
7433           break;
7434         default:
7435           gcc_unreachable ();
7436         }
7437       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7438       break;
7439
7440     case RID_RETURN:
7441       {
7442         tree expr;
7443
7444         /* If the next token is a `;', then there is no
7445            expression.  */
7446         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7447           expr = cp_parser_expression (parser, /*cast_p=*/false);
7448         else
7449           expr = NULL_TREE;
7450         /* Build the return-statement.  */
7451         statement = finish_return_stmt (expr);
7452         /* Look for the final `;'.  */
7453         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7454       }
7455       break;
7456
7457     case RID_GOTO:
7458       /* Create the goto-statement.  */
7459       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7460         {
7461           /* Issue a warning about this use of a GNU extension.  */
7462           if (pedantic)
7463             pedwarn ("ISO C++ forbids computed gotos");
7464           /* Consume the '*' token.  */
7465           cp_lexer_consume_token (parser->lexer);
7466           /* Parse the dependent expression.  */
7467           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7468         }
7469       else
7470         finish_goto_stmt (cp_parser_identifier (parser));
7471       /* Look for the final `;'.  */
7472       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7473       break;
7474
7475     default:
7476       cp_parser_error (parser, "expected jump-statement");
7477       break;
7478     }
7479
7480   return statement;
7481 }
7482
7483 /* Parse a declaration-statement.
7484
7485    declaration-statement:
7486      block-declaration  */
7487
7488 static void
7489 cp_parser_declaration_statement (cp_parser* parser)
7490 {
7491   void *p;
7492
7493   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7494   p = obstack_alloc (&declarator_obstack, 0);
7495
7496  /* Parse the block-declaration.  */
7497   cp_parser_block_declaration (parser, /*statement_p=*/true);
7498
7499   /* Free any declarators allocated.  */
7500   obstack_free (&declarator_obstack, p);
7501
7502   /* Finish off the statement.  */
7503   finish_stmt ();
7504 }
7505
7506 /* Some dependent statements (like `if (cond) statement'), are
7507    implicitly in their own scope.  In other words, if the statement is
7508    a single statement (as opposed to a compound-statement), it is
7509    none-the-less treated as if it were enclosed in braces.  Any
7510    declarations appearing in the dependent statement are out of scope
7511    after control passes that point.  This function parses a statement,
7512    but ensures that is in its own scope, even if it is not a
7513    compound-statement.
7514
7515    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7516    is a (possibly labeled) if statement which is not enclosed in
7517    braces and has an else clause.  This is used to implement
7518    -Wparentheses.
7519
7520    Returns the new statement.  */
7521
7522 static tree
7523 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7524 {
7525   tree statement;
7526
7527   if (if_p != NULL)
7528     *if_p = false;
7529
7530   /* Mark if () ; with a special NOP_EXPR.  */
7531   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7532     {
7533       cp_lexer_consume_token (parser->lexer);
7534       statement = add_stmt (build_empty_stmt ());
7535     }
7536   /* if a compound is opened, we simply parse the statement directly.  */
7537   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7538     statement = cp_parser_compound_statement (parser, NULL, false);
7539   /* If the token is not a `{', then we must take special action.  */
7540   else
7541     {
7542       /* Create a compound-statement.  */
7543       statement = begin_compound_stmt (0);
7544       /* Parse the dependent-statement.  */
7545       cp_parser_statement (parser, NULL_TREE, false, if_p);
7546       /* Finish the dummy compound-statement.  */
7547       finish_compound_stmt (statement);
7548     }
7549
7550   /* Return the statement.  */
7551   return statement;
7552 }
7553
7554 /* For some dependent statements (like `while (cond) statement'), we
7555    have already created a scope.  Therefore, even if the dependent
7556    statement is a compound-statement, we do not want to create another
7557    scope.  */
7558
7559 static void
7560 cp_parser_already_scoped_statement (cp_parser* parser)
7561 {
7562   /* If the token is a `{', then we must take special action.  */
7563   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7564     cp_parser_statement (parser, NULL_TREE, false, NULL);
7565   else
7566     {
7567       /* Avoid calling cp_parser_compound_statement, so that we
7568          don't create a new scope.  Do everything else by hand.  */
7569       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7570       cp_parser_statement_seq_opt (parser, NULL_TREE);
7571       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7572     }
7573 }
7574
7575 /* Declarations [gram.dcl.dcl] */
7576
7577 /* Parse an optional declaration-sequence.
7578
7579    declaration-seq:
7580      declaration
7581      declaration-seq declaration  */
7582
7583 static void
7584 cp_parser_declaration_seq_opt (cp_parser* parser)
7585 {
7586   while (true)
7587     {
7588       cp_token *token;
7589
7590       token = cp_lexer_peek_token (parser->lexer);
7591
7592       if (token->type == CPP_CLOSE_BRACE
7593           || token->type == CPP_EOF
7594           || token->type == CPP_PRAGMA_EOL)
7595         break;
7596
7597       if (token->type == CPP_SEMICOLON)
7598         {
7599           /* A declaration consisting of a single semicolon is
7600              invalid.  Allow it unless we're being pedantic.  */
7601           cp_lexer_consume_token (parser->lexer);
7602           if (pedantic && !in_system_header)
7603             pedwarn ("extra %<;%>");
7604           continue;
7605         }
7606
7607       /* If we're entering or exiting a region that's implicitly
7608          extern "C", modify the lang context appropriately.  */
7609       if (!parser->implicit_extern_c && token->implicit_extern_c)
7610         {
7611           push_lang_context (lang_name_c);
7612           parser->implicit_extern_c = true;
7613         }
7614       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7615         {
7616           pop_lang_context ();
7617           parser->implicit_extern_c = false;
7618         }
7619
7620       if (token->type == CPP_PRAGMA)
7621         {
7622           /* A top-level declaration can consist solely of a #pragma.
7623              A nested declaration cannot, so this is done here and not
7624              in cp_parser_declaration.  (A #pragma at block scope is
7625              handled in cp_parser_statement.)  */
7626           cp_parser_pragma (parser, pragma_external);
7627           continue;
7628         }
7629
7630       /* Parse the declaration itself.  */
7631       cp_parser_declaration (parser);
7632     }
7633 }
7634
7635 /* Parse a declaration.
7636
7637    declaration:
7638      block-declaration
7639      function-definition
7640      template-declaration
7641      explicit-instantiation
7642      explicit-specialization
7643      linkage-specification
7644      namespace-definition
7645
7646    GNU extension:
7647
7648    declaration:
7649       __extension__ declaration */
7650
7651 static void
7652 cp_parser_declaration (cp_parser* parser)
7653 {
7654   cp_token token1;
7655   cp_token token2;
7656   int saved_pedantic;
7657   void *p;
7658
7659   /* Check for the `__extension__' keyword.  */
7660   if (cp_parser_extension_opt (parser, &saved_pedantic))
7661     {
7662       /* Parse the qualified declaration.  */
7663       cp_parser_declaration (parser);
7664       /* Restore the PEDANTIC flag.  */
7665       pedantic = saved_pedantic;
7666
7667       return;
7668     }
7669
7670   /* Try to figure out what kind of declaration is present.  */
7671   token1 = *cp_lexer_peek_token (parser->lexer);
7672
7673   if (token1.type != CPP_EOF)
7674     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7675   else
7676     {
7677       token2.type = CPP_EOF;
7678       token2.keyword = RID_MAX;
7679     }
7680
7681   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7682   p = obstack_alloc (&declarator_obstack, 0);
7683
7684   /* If the next token is `extern' and the following token is a string
7685      literal, then we have a linkage specification.  */
7686   if (token1.keyword == RID_EXTERN
7687       && cp_parser_is_string_literal (&token2))
7688     cp_parser_linkage_specification (parser);
7689   /* If the next token is `template', then we have either a template
7690      declaration, an explicit instantiation, or an explicit
7691      specialization.  */
7692   else if (token1.keyword == RID_TEMPLATE)
7693     {
7694       /* `template <>' indicates a template specialization.  */
7695       if (token2.type == CPP_LESS
7696           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7697         cp_parser_explicit_specialization (parser);
7698       /* `template <' indicates a template declaration.  */
7699       else if (token2.type == CPP_LESS)
7700         cp_parser_template_declaration (parser, /*member_p=*/false);
7701       /* Anything else must be an explicit instantiation.  */
7702       else
7703         cp_parser_explicit_instantiation (parser);
7704     }
7705   /* If the next token is `export', then we have a template
7706      declaration.  */
7707   else if (token1.keyword == RID_EXPORT)
7708     cp_parser_template_declaration (parser, /*member_p=*/false);
7709   /* If the next token is `extern', 'static' or 'inline' and the one
7710      after that is `template', we have a GNU extended explicit
7711      instantiation directive.  */
7712   else if (cp_parser_allow_gnu_extensions_p (parser)
7713            && (token1.keyword == RID_EXTERN
7714                || token1.keyword == RID_STATIC
7715                || token1.keyword == RID_INLINE)
7716            && token2.keyword == RID_TEMPLATE)
7717     cp_parser_explicit_instantiation (parser);
7718   /* If the next token is `namespace', check for a named or unnamed
7719      namespace definition.  */
7720   else if (token1.keyword == RID_NAMESPACE
7721            && (/* A named namespace definition.  */
7722                (token2.type == CPP_NAME
7723                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7724                     != CPP_EQ))
7725                /* An unnamed namespace definition.  */
7726                || token2.type == CPP_OPEN_BRACE
7727                || token2.keyword == RID_ATTRIBUTE))
7728     cp_parser_namespace_definition (parser);
7729   /* An inline (associated) namespace definition.  */
7730   else if (token1.keyword == RID_INLINE
7731            && token2.keyword == RID_NAMESPACE)
7732     cp_parser_namespace_definition (parser);
7733   /* Objective-C++ declaration/definition.  */
7734   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7735     cp_parser_objc_declaration (parser);
7736   /* We must have either a block declaration or a function
7737      definition.  */
7738   else
7739     /* Try to parse a block-declaration, or a function-definition.  */
7740     cp_parser_block_declaration (parser, /*statement_p=*/false);
7741
7742   /* Free any declarators allocated.  */
7743   obstack_free (&declarator_obstack, p);
7744 }
7745
7746 /* Parse a block-declaration.
7747
7748    block-declaration:
7749      simple-declaration
7750      asm-definition
7751      namespace-alias-definition
7752      using-declaration
7753      using-directive
7754
7755    GNU Extension:
7756
7757    block-declaration:
7758      __extension__ block-declaration
7759
7760    C++0x Extension:
7761
7762    block-declaration:
7763      static_assert-declaration
7764
7765    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7766    part of a declaration-statement.  */
7767
7768 static void
7769 cp_parser_block_declaration (cp_parser *parser,
7770                              bool      statement_p)
7771 {
7772   cp_token *token1;
7773   int saved_pedantic;
7774
7775   /* Check for the `__extension__' keyword.  */
7776   if (cp_parser_extension_opt (parser, &saved_pedantic))
7777     {
7778       /* Parse the qualified declaration.  */
7779       cp_parser_block_declaration (parser, statement_p);
7780       /* Restore the PEDANTIC flag.  */
7781       pedantic = saved_pedantic;
7782
7783       return;
7784     }
7785
7786   /* Peek at the next token to figure out which kind of declaration is
7787      present.  */
7788   token1 = cp_lexer_peek_token (parser->lexer);
7789
7790   /* If the next keyword is `asm', we have an asm-definition.  */
7791   if (token1->keyword == RID_ASM)
7792     {
7793       if (statement_p)
7794         cp_parser_commit_to_tentative_parse (parser);
7795       cp_parser_asm_definition (parser);
7796     }
7797   /* If the next keyword is `namespace', we have a
7798      namespace-alias-definition.  */
7799   else if (token1->keyword == RID_NAMESPACE)
7800     cp_parser_namespace_alias_definition (parser);
7801   /* If the next keyword is `using', we have either a
7802      using-declaration or a using-directive.  */
7803   else if (token1->keyword == RID_USING)
7804     {
7805       cp_token *token2;
7806
7807       if (statement_p)
7808         cp_parser_commit_to_tentative_parse (parser);
7809       /* If the token after `using' is `namespace', then we have a
7810          using-directive.  */
7811       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7812       if (token2->keyword == RID_NAMESPACE)
7813         cp_parser_using_directive (parser);
7814       /* Otherwise, it's a using-declaration.  */
7815       else
7816         cp_parser_using_declaration (parser,
7817                                      /*access_declaration_p=*/false);
7818     }
7819   /* If the next keyword is `__label__' we have a misplaced label
7820      declaration.  */
7821   else if (token1->keyword == RID_LABEL)
7822     {
7823       cp_lexer_consume_token (parser->lexer);
7824       error ("%<__label__%> not at the beginning of a block");
7825       cp_parser_skip_to_end_of_statement (parser);
7826       /* If the next token is now a `;', consume it.  */
7827       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7828         cp_lexer_consume_token (parser->lexer);
7829     }
7830   /* If the next token is `static_assert' we have a static assertion.  */
7831   else if (token1->keyword == RID_STATIC_ASSERT)
7832     cp_parser_static_assert (parser, /*member_p=*/false);
7833   /* Anything else must be a simple-declaration.  */
7834   else
7835     cp_parser_simple_declaration (parser, !statement_p);
7836 }
7837
7838 /* Parse a simple-declaration.
7839
7840    simple-declaration:
7841      decl-specifier-seq [opt] init-declarator-list [opt] ;
7842
7843    init-declarator-list:
7844      init-declarator
7845      init-declarator-list , init-declarator
7846
7847    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7848    function-definition as a simple-declaration.  */
7849
7850 static void
7851 cp_parser_simple_declaration (cp_parser* parser,
7852                               bool function_definition_allowed_p)
7853 {
7854   cp_decl_specifier_seq decl_specifiers;
7855   int declares_class_or_enum;
7856   bool saw_declarator;
7857
7858   /* Defer access checks until we know what is being declared; the
7859      checks for names appearing in the decl-specifier-seq should be
7860      done as if we were in the scope of the thing being declared.  */
7861   push_deferring_access_checks (dk_deferred);
7862
7863   /* Parse the decl-specifier-seq.  We have to keep track of whether
7864      or not the decl-specifier-seq declares a named class or
7865      enumeration type, since that is the only case in which the
7866      init-declarator-list is allowed to be empty.
7867
7868      [dcl.dcl]
7869
7870      In a simple-declaration, the optional init-declarator-list can be
7871      omitted only when declaring a class or enumeration, that is when
7872      the decl-specifier-seq contains either a class-specifier, an
7873      elaborated-type-specifier, or an enum-specifier.  */
7874   cp_parser_decl_specifier_seq (parser,
7875                                 CP_PARSER_FLAGS_OPTIONAL,
7876                                 &decl_specifiers,
7877                                 &declares_class_or_enum);
7878   /* We no longer need to defer access checks.  */
7879   stop_deferring_access_checks ();
7880
7881   /* In a block scope, a valid declaration must always have a
7882      decl-specifier-seq.  By not trying to parse declarators, we can
7883      resolve the declaration/expression ambiguity more quickly.  */
7884   if (!function_definition_allowed_p
7885       && !decl_specifiers.any_specifiers_p)
7886     {
7887       cp_parser_error (parser, "expected declaration");
7888       goto done;
7889     }
7890
7891   /* If the next two tokens are both identifiers, the code is
7892      erroneous. The usual cause of this situation is code like:
7893
7894        T t;
7895
7896      where "T" should name a type -- but does not.  */
7897   if (!decl_specifiers.type
7898       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7899     {
7900       /* If parsing tentatively, we should commit; we really are
7901          looking at a declaration.  */
7902       cp_parser_commit_to_tentative_parse (parser);
7903       /* Give up.  */
7904       goto done;
7905     }
7906
7907   /* If we have seen at least one decl-specifier, and the next token
7908      is not a parenthesis, then we must be looking at a declaration.
7909      (After "int (" we might be looking at a functional cast.)  */
7910   if (decl_specifiers.any_specifiers_p
7911       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7912     cp_parser_commit_to_tentative_parse (parser);
7913
7914   /* Keep going until we hit the `;' at the end of the simple
7915      declaration.  */
7916   saw_declarator = false;
7917   while (cp_lexer_next_token_is_not (parser->lexer,
7918                                      CPP_SEMICOLON))
7919     {
7920       cp_token *token;
7921       bool function_definition_p;
7922       tree decl;
7923
7924       if (saw_declarator)
7925         {
7926           /* If we are processing next declarator, coma is expected */
7927           token = cp_lexer_peek_token (parser->lexer);
7928           gcc_assert (token->type == CPP_COMMA);
7929           cp_lexer_consume_token (parser->lexer);
7930         }
7931       else
7932         saw_declarator = true;
7933
7934       /* Parse the init-declarator.  */
7935       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7936                                         /*checks=*/NULL,
7937                                         function_definition_allowed_p,
7938                                         /*member_p=*/false,
7939                                         declares_class_or_enum,
7940                                         &function_definition_p);
7941       /* If an error occurred while parsing tentatively, exit quickly.
7942          (That usually happens when in the body of a function; each
7943          statement is treated as a declaration-statement until proven
7944          otherwise.)  */
7945       if (cp_parser_error_occurred (parser))
7946         goto done;
7947       /* Handle function definitions specially.  */
7948       if (function_definition_p)
7949         {
7950           /* If the next token is a `,', then we are probably
7951              processing something like:
7952
7953                void f() {}, *p;
7954
7955              which is erroneous.  */
7956           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7957             error ("mixing declarations and function-definitions is forbidden");
7958           /* Otherwise, we're done with the list of declarators.  */
7959           else
7960             {
7961               pop_deferring_access_checks ();
7962               return;
7963             }
7964         }
7965       /* The next token should be either a `,' or a `;'.  */
7966       token = cp_lexer_peek_token (parser->lexer);
7967       /* If it's a `,', there are more declarators to come.  */
7968       if (token->type == CPP_COMMA)
7969         /* will be consumed next time around */;
7970       /* If it's a `;', we are done.  */
7971       else if (token->type == CPP_SEMICOLON)
7972         break;
7973       /* Anything else is an error.  */
7974       else
7975         {
7976           /* If we have already issued an error message we don't need
7977              to issue another one.  */
7978           if (decl != error_mark_node
7979               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7980             cp_parser_error (parser, "expected %<,%> or %<;%>");
7981           /* Skip tokens until we reach the end of the statement.  */
7982           cp_parser_skip_to_end_of_statement (parser);
7983           /* If the next token is now a `;', consume it.  */
7984           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7985             cp_lexer_consume_token (parser->lexer);
7986           goto done;
7987         }
7988       /* After the first time around, a function-definition is not
7989          allowed -- even if it was OK at first.  For example:
7990
7991            int i, f() {}
7992
7993          is not valid.  */
7994       function_definition_allowed_p = false;
7995     }
7996
7997   /* Issue an error message if no declarators are present, and the
7998      decl-specifier-seq does not itself declare a class or
7999      enumeration.  */
8000   if (!saw_declarator)
8001     {
8002       if (cp_parser_declares_only_class_p (parser))
8003         shadow_tag (&decl_specifiers);
8004       /* Perform any deferred access checks.  */
8005       perform_deferred_access_checks ();
8006     }
8007
8008   /* Consume the `;'.  */
8009   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8010
8011  done:
8012   pop_deferring_access_checks ();
8013 }
8014
8015 /* Parse a decl-specifier-seq.
8016
8017    decl-specifier-seq:
8018      decl-specifier-seq [opt] decl-specifier
8019
8020    decl-specifier:
8021      storage-class-specifier
8022      type-specifier
8023      function-specifier
8024      friend
8025      typedef
8026
8027    GNU Extension:
8028
8029    decl-specifier:
8030      attributes
8031
8032    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8033
8034    The parser flags FLAGS is used to control type-specifier parsing.
8035
8036    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8037    flags:
8038
8039      1: one of the decl-specifiers is an elaborated-type-specifier
8040         (i.e., a type declaration)
8041      2: one of the decl-specifiers is an enum-specifier or a
8042         class-specifier (i.e., a type definition)
8043
8044    */
8045
8046 static void
8047 cp_parser_decl_specifier_seq (cp_parser* parser,
8048                               cp_parser_flags flags,
8049                               cp_decl_specifier_seq *decl_specs,
8050                               int* declares_class_or_enum)
8051 {
8052   bool constructor_possible_p = !parser->in_declarator_p;
8053
8054   /* Clear DECL_SPECS.  */
8055   clear_decl_specs (decl_specs);
8056
8057   /* Assume no class or enumeration type is declared.  */
8058   *declares_class_or_enum = 0;
8059
8060   /* Keep reading specifiers until there are no more to read.  */
8061   while (true)
8062     {
8063       bool constructor_p;
8064       bool found_decl_spec;
8065       cp_token *token;
8066
8067       /* Peek at the next token.  */
8068       token = cp_lexer_peek_token (parser->lexer);
8069       /* Handle attributes.  */
8070       if (token->keyword == RID_ATTRIBUTE)
8071         {
8072           /* Parse the attributes.  */
8073           decl_specs->attributes
8074             = chainon (decl_specs->attributes,
8075                        cp_parser_attributes_opt (parser));
8076           continue;
8077         }
8078       /* Assume we will find a decl-specifier keyword.  */
8079       found_decl_spec = true;
8080       /* If the next token is an appropriate keyword, we can simply
8081          add it to the list.  */
8082       switch (token->keyword)
8083         {
8084           /* decl-specifier:
8085                friend  */
8086         case RID_FRIEND:
8087           if (!at_class_scope_p ())
8088             {
8089               error ("%<friend%> used outside of class");
8090               cp_lexer_purge_token (parser->lexer);
8091             }
8092           else
8093             {
8094               ++decl_specs->specs[(int) ds_friend];
8095               /* Consume the token.  */
8096               cp_lexer_consume_token (parser->lexer);
8097             }
8098           break;
8099
8100           /* function-specifier:
8101                inline
8102                virtual
8103                explicit  */
8104         case RID_INLINE:
8105         case RID_VIRTUAL:
8106         case RID_EXPLICIT:
8107           cp_parser_function_specifier_opt (parser, decl_specs);
8108           break;
8109
8110           /* decl-specifier:
8111                typedef  */
8112         case RID_TYPEDEF:
8113           ++decl_specs->specs[(int) ds_typedef];
8114           /* Consume the token.  */
8115           cp_lexer_consume_token (parser->lexer);
8116           /* A constructor declarator cannot appear in a typedef.  */
8117           constructor_possible_p = false;
8118           /* The "typedef" keyword can only occur in a declaration; we
8119              may as well commit at this point.  */
8120           cp_parser_commit_to_tentative_parse (parser);
8121
8122           if (decl_specs->storage_class != sc_none)
8123             decl_specs->conflicting_specifiers_p = true;
8124           break;
8125
8126           /* storage-class-specifier:
8127                auto
8128                register
8129                static
8130                extern
8131                mutable
8132
8133              GNU Extension:
8134                thread  */
8135         case RID_AUTO:
8136         case RID_REGISTER:
8137         case RID_STATIC:
8138         case RID_EXTERN:
8139         case RID_MUTABLE:
8140           /* Consume the token.  */
8141           cp_lexer_consume_token (parser->lexer);
8142           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8143           break;
8144         case RID_THREAD:
8145           /* Consume the token.  */
8146           cp_lexer_consume_token (parser->lexer);
8147           ++decl_specs->specs[(int) ds_thread];
8148           break;
8149
8150         default:
8151           /* We did not yet find a decl-specifier yet.  */
8152           found_decl_spec = false;
8153           break;
8154         }
8155
8156       /* Constructors are a special case.  The `S' in `S()' is not a
8157          decl-specifier; it is the beginning of the declarator.  */
8158       constructor_p
8159         = (!found_decl_spec
8160            && constructor_possible_p
8161            && (cp_parser_constructor_declarator_p
8162                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8163
8164       /* If we don't have a DECL_SPEC yet, then we must be looking at
8165          a type-specifier.  */
8166       if (!found_decl_spec && !constructor_p)
8167         {
8168           int decl_spec_declares_class_or_enum;
8169           bool is_cv_qualifier;
8170           tree type_spec;
8171
8172           type_spec
8173             = cp_parser_type_specifier (parser, flags,
8174                                         decl_specs,
8175                                         /*is_declaration=*/true,
8176                                         &decl_spec_declares_class_or_enum,
8177                                         &is_cv_qualifier);
8178
8179           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8180
8181           /* If this type-specifier referenced a user-defined type
8182              (a typedef, class-name, etc.), then we can't allow any
8183              more such type-specifiers henceforth.
8184
8185              [dcl.spec]
8186
8187              The longest sequence of decl-specifiers that could
8188              possibly be a type name is taken as the
8189              decl-specifier-seq of a declaration.  The sequence shall
8190              be self-consistent as described below.
8191
8192              [dcl.type]
8193
8194              As a general rule, at most one type-specifier is allowed
8195              in the complete decl-specifier-seq of a declaration.  The
8196              only exceptions are the following:
8197
8198              -- const or volatile can be combined with any other
8199                 type-specifier.
8200
8201              -- signed or unsigned can be combined with char, long,
8202                 short, or int.
8203
8204              -- ..
8205
8206              Example:
8207
8208                typedef char* Pc;
8209                void g (const int Pc);
8210
8211              Here, Pc is *not* part of the decl-specifier seq; it's
8212              the declarator.  Therefore, once we see a type-specifier
8213              (other than a cv-qualifier), we forbid any additional
8214              user-defined types.  We *do* still allow things like `int
8215              int' to be considered a decl-specifier-seq, and issue the
8216              error message later.  */
8217           if (type_spec && !is_cv_qualifier)
8218             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8219           /* A constructor declarator cannot follow a type-specifier.  */
8220           if (type_spec)
8221             {
8222               constructor_possible_p = false;
8223               found_decl_spec = true;
8224             }
8225         }
8226
8227       /* If we still do not have a DECL_SPEC, then there are no more
8228          decl-specifiers.  */
8229       if (!found_decl_spec)
8230         break;
8231
8232       decl_specs->any_specifiers_p = true;
8233       /* After we see one decl-specifier, further decl-specifiers are
8234          always optional.  */
8235       flags |= CP_PARSER_FLAGS_OPTIONAL;
8236     }
8237
8238   cp_parser_check_decl_spec (decl_specs);
8239
8240   /* Don't allow a friend specifier with a class definition.  */
8241   if (decl_specs->specs[(int) ds_friend] != 0
8242       && (*declares_class_or_enum & 2))
8243     error ("class definition may not be declared a friend");
8244 }
8245
8246 /* Parse an (optional) storage-class-specifier.
8247
8248    storage-class-specifier:
8249      auto
8250      register
8251      static
8252      extern
8253      mutable
8254
8255    GNU Extension:
8256
8257    storage-class-specifier:
8258      thread
8259
8260    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8261
8262 static tree
8263 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8264 {
8265   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8266     {
8267     case RID_AUTO:
8268     case RID_REGISTER:
8269     case RID_STATIC:
8270     case RID_EXTERN:
8271     case RID_MUTABLE:
8272     case RID_THREAD:
8273       /* Consume the token.  */
8274       return cp_lexer_consume_token (parser->lexer)->u.value;
8275
8276     default:
8277       return NULL_TREE;
8278     }
8279 }
8280
8281 /* Parse an (optional) function-specifier.
8282
8283    function-specifier:
8284      inline
8285      virtual
8286      explicit
8287
8288    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8289    Updates DECL_SPECS, if it is non-NULL.  */
8290
8291 static tree
8292 cp_parser_function_specifier_opt (cp_parser* parser,
8293                                   cp_decl_specifier_seq *decl_specs)
8294 {
8295   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8296     {
8297     case RID_INLINE:
8298       if (decl_specs)
8299         ++decl_specs->specs[(int) ds_inline];
8300       break;
8301
8302     case RID_VIRTUAL:
8303       /* 14.5.2.3 [temp.mem]
8304
8305          A member function template shall not be virtual.  */
8306       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8307         error ("templates may not be %<virtual%>");
8308       else if (decl_specs)
8309         ++decl_specs->specs[(int) ds_virtual];
8310       break;
8311
8312     case RID_EXPLICIT:
8313       if (decl_specs)
8314         ++decl_specs->specs[(int) ds_explicit];
8315       break;
8316
8317     default:
8318       return NULL_TREE;
8319     }
8320
8321   /* Consume the token.  */
8322   return cp_lexer_consume_token (parser->lexer)->u.value;
8323 }
8324
8325 /* Parse a linkage-specification.
8326
8327    linkage-specification:
8328      extern string-literal { declaration-seq [opt] }
8329      extern string-literal declaration  */
8330
8331 static void
8332 cp_parser_linkage_specification (cp_parser* parser)
8333 {
8334   tree linkage;
8335
8336   /* Look for the `extern' keyword.  */
8337   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8338
8339   /* Look for the string-literal.  */
8340   linkage = cp_parser_string_literal (parser, false, false);
8341
8342   /* Transform the literal into an identifier.  If the literal is a
8343      wide-character string, or contains embedded NULs, then we can't
8344      handle it as the user wants.  */
8345   if (strlen (TREE_STRING_POINTER (linkage))
8346       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8347     {
8348       cp_parser_error (parser, "invalid linkage-specification");
8349       /* Assume C++ linkage.  */
8350       linkage = lang_name_cplusplus;
8351     }
8352   else
8353     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8354
8355   /* We're now using the new linkage.  */
8356   push_lang_context (linkage);
8357
8358   /* If the next token is a `{', then we're using the first
8359      production.  */
8360   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8361     {
8362       /* Consume the `{' token.  */
8363       cp_lexer_consume_token (parser->lexer);
8364       /* Parse the declarations.  */
8365       cp_parser_declaration_seq_opt (parser);
8366       /* Look for the closing `}'.  */
8367       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8368     }
8369   /* Otherwise, there's just one declaration.  */
8370   else
8371     {
8372       bool saved_in_unbraced_linkage_specification_p;
8373
8374       saved_in_unbraced_linkage_specification_p
8375         = parser->in_unbraced_linkage_specification_p;
8376       parser->in_unbraced_linkage_specification_p = true;
8377       cp_parser_declaration (parser);
8378       parser->in_unbraced_linkage_specification_p
8379         = saved_in_unbraced_linkage_specification_p;
8380     }
8381
8382   /* We're done with the linkage-specification.  */
8383   pop_lang_context ();
8384 }
8385
8386 /* Parse a static_assert-declaration.
8387
8388    static_assert-declaration:
8389      static_assert ( constant-expression , string-literal ) ; 
8390
8391    If MEMBER_P, this static_assert is a class member.  */
8392
8393 static void 
8394 cp_parser_static_assert(cp_parser *parser, bool member_p)
8395 {
8396   tree condition;
8397   tree message;
8398   cp_token *token;
8399   location_t saved_loc;
8400
8401   /* Peek at the `static_assert' token so we can keep track of exactly
8402      where the static assertion started.  */
8403   token = cp_lexer_peek_token (parser->lexer);
8404   saved_loc = token->location;
8405
8406   /* Look for the `static_assert' keyword.  */
8407   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8408                                   "`static_assert'"))
8409     return;
8410
8411   /*  We know we are in a static assertion; commit to any tentative
8412       parse.  */
8413   if (cp_parser_parsing_tentatively (parser))
8414     cp_parser_commit_to_tentative_parse (parser);
8415
8416   /* Parse the `(' starting the static assertion condition.  */
8417   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8418
8419   /* Parse the constant-expression.  */
8420   condition = 
8421     cp_parser_constant_expression (parser,
8422                                    /*allow_non_constant_p=*/false,
8423                                    /*non_constant_p=*/NULL);
8424
8425   /* Parse the separating `,'.  */
8426   cp_parser_require (parser, CPP_COMMA, "`,'");
8427
8428   /* Parse the string-literal message.  */
8429   message = cp_parser_string_literal (parser, 
8430                                       /*translate=*/false,
8431                                       /*wide_ok=*/true);
8432
8433   /* A `)' completes the static assertion.  */
8434   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8435     cp_parser_skip_to_closing_parenthesis (parser, 
8436                                            /*recovering=*/true, 
8437                                            /*or_comma=*/false,
8438                                            /*consume_paren=*/true);
8439
8440   /* A semicolon terminates the declaration.  */
8441   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8442
8443   /* Complete the static assertion, which may mean either processing 
8444      the static assert now or saving it for template instantiation.  */
8445   finish_static_assert (condition, message, saved_loc, member_p);
8446 }
8447
8448 /* Parse a `decltype' type. Returns the type. 
8449
8450    simple-type-specifier:
8451      decltype ( expression )  */
8452
8453 static tree
8454 cp_parser_decltype (cp_parser *parser)
8455 {
8456   tree expr;
8457   bool id_expression_or_member_access_p = false;
8458   const char *saved_message;
8459   bool saved_integral_constant_expression_p;
8460   bool saved_non_integral_constant_expression_p;
8461
8462   /* Look for the `decltype' token.  */
8463   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "`decltype'"))
8464     return error_mark_node;
8465
8466   /* Types cannot be defined in a `decltype' expression.  Save away the
8467      old message.  */
8468   saved_message = parser->type_definition_forbidden_message;
8469
8470   /* And create the new one.  */
8471   parser->type_definition_forbidden_message
8472     = "types may not be defined in `decltype' expressions";
8473
8474   /* The restrictions on constant-expressions do not apply inside
8475      decltype expressions.  */
8476   saved_integral_constant_expression_p
8477     = parser->integral_constant_expression_p;
8478   saved_non_integral_constant_expression_p
8479     = parser->non_integral_constant_expression_p;
8480   parser->integral_constant_expression_p = false;
8481
8482   /* Do not actually evaluate the expression.  */
8483   ++skip_evaluation;
8484
8485   /* Parse the opening `('.  */
8486   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
8487     return error_mark_node;
8488   
8489   /* First, try parsing an id-expression.  */
8490   cp_parser_parse_tentatively (parser);
8491   expr = cp_parser_id_expression (parser,
8492                                   /*template_keyword_p=*/false,
8493                                   /*check_dependency_p=*/true,
8494                                   /*template_p=*/NULL,
8495                                   /*declarator_p=*/false,
8496                                   /*optional_p=*/false);
8497
8498   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8499     {
8500       bool non_integral_constant_expression_p = false;
8501       tree id_expression = expr;
8502       cp_id_kind idk;
8503       const char *error_msg;
8504
8505       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8506         /* Lookup the name we got back from the id-expression.  */
8507         expr = cp_parser_lookup_name (parser, expr,
8508                                       none_type,
8509                                       /*is_template=*/false,
8510                                       /*is_namespace=*/false,
8511                                       /*check_dependency=*/true,
8512                                       /*ambiguous_decls=*/NULL);
8513
8514       if (expr
8515           && expr != error_mark_node
8516           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8517           && TREE_CODE (expr) != TYPE_DECL
8518           && (TREE_CODE (expr) != BIT_NOT_EXPR
8519               || !TYPE_P (TREE_OPERAND (expr, 0)))
8520           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8521         {
8522           /* Complete lookup of the id-expression.  */
8523           expr = (finish_id_expression
8524                   (id_expression, expr, parser->scope, &idk,
8525                    /*integral_constant_expression_p=*/false,
8526                    /*allow_non_integral_constant_expression_p=*/true,
8527                    &non_integral_constant_expression_p,
8528                    /*template_p=*/false,
8529                    /*done=*/true,
8530                    /*address_p=*/false,
8531                    /*template_arg_p=*/false,
8532                    &error_msg));
8533
8534           if (expr == error_mark_node)
8535             /* We found an id-expression, but it was something that we
8536                should not have found. This is an error, not something
8537                we can recover from, so note that we found an
8538                id-expression and we'll recover as gracefully as
8539                possible.  */
8540             id_expression_or_member_access_p = true;
8541         }
8542
8543       if (expr 
8544           && expr != error_mark_node
8545           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8546         /* We have an id-expression.  */
8547         id_expression_or_member_access_p = true;
8548     }
8549
8550   if (!id_expression_or_member_access_p)
8551     {
8552       /* Abort the id-expression parse.  */
8553       cp_parser_abort_tentative_parse (parser);
8554
8555       /* Parsing tentatively, again.  */
8556       cp_parser_parse_tentatively (parser);
8557
8558       /* Parse a class member access.  */
8559       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8560                                            /*cast_p=*/false,
8561                                            /*member_access_only_p=*/true);
8562
8563       if (expr 
8564           && expr != error_mark_node
8565           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8566         /* We have an id-expression.  */
8567         id_expression_or_member_access_p = true;
8568     }
8569
8570   if (id_expression_or_member_access_p)
8571     /* We have parsed the complete id-expression or member access.  */
8572     cp_parser_parse_definitely (parser);
8573   else
8574     {
8575       /* Abort our attempt to parse an id-expression or member access
8576          expression.  */
8577       cp_parser_abort_tentative_parse (parser);
8578
8579       /* Parse a full expression.  */
8580       expr = cp_parser_expression (parser, /*cast_p=*/false);
8581     }
8582
8583   /* Go back to evaluating expressions.  */
8584   --skip_evaluation;
8585
8586   /* Restore the old message and the integral constant expression
8587      flags.  */
8588   parser->type_definition_forbidden_message = saved_message;
8589   parser->integral_constant_expression_p
8590     = saved_integral_constant_expression_p;
8591   parser->non_integral_constant_expression_p
8592     = saved_non_integral_constant_expression_p;
8593
8594   if (expr == error_mark_node)
8595     {
8596       /* Skip everything up to the closing `)'.  */
8597       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8598                                              /*consume_paren=*/true);
8599       return error_mark_node;
8600     }
8601   
8602   /* Parse to the closing `)'.  */
8603   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8604     {
8605       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8606                                              /*consume_paren=*/true);
8607       return error_mark_node;
8608     }
8609
8610   return finish_decltype_type (expr, id_expression_or_member_access_p);
8611 }
8612
8613 /* Special member functions [gram.special] */
8614
8615 /* Parse a conversion-function-id.
8616
8617    conversion-function-id:
8618      operator conversion-type-id
8619
8620    Returns an IDENTIFIER_NODE representing the operator.  */
8621
8622 static tree
8623 cp_parser_conversion_function_id (cp_parser* parser)
8624 {
8625   tree type;
8626   tree saved_scope;
8627   tree saved_qualifying_scope;
8628   tree saved_object_scope;
8629   tree pushed_scope = NULL_TREE;
8630
8631   /* Look for the `operator' token.  */
8632   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8633     return error_mark_node;
8634   /* When we parse the conversion-type-id, the current scope will be
8635      reset.  However, we need that information in able to look up the
8636      conversion function later, so we save it here.  */
8637   saved_scope = parser->scope;
8638   saved_qualifying_scope = parser->qualifying_scope;
8639   saved_object_scope = parser->object_scope;
8640   /* We must enter the scope of the class so that the names of
8641      entities declared within the class are available in the
8642      conversion-type-id.  For example, consider:
8643
8644        struct S {
8645          typedef int I;
8646          operator I();
8647        };
8648
8649        S::operator I() { ... }
8650
8651      In order to see that `I' is a type-name in the definition, we
8652      must be in the scope of `S'.  */
8653   if (saved_scope)
8654     pushed_scope = push_scope (saved_scope);
8655   /* Parse the conversion-type-id.  */
8656   type = cp_parser_conversion_type_id (parser);
8657   /* Leave the scope of the class, if any.  */
8658   if (pushed_scope)
8659     pop_scope (pushed_scope);
8660   /* Restore the saved scope.  */
8661   parser->scope = saved_scope;
8662   parser->qualifying_scope = saved_qualifying_scope;
8663   parser->object_scope = saved_object_scope;
8664   /* If the TYPE is invalid, indicate failure.  */
8665   if (type == error_mark_node)
8666     return error_mark_node;
8667   return mangle_conv_op_name_for_type (type);
8668 }
8669
8670 /* Parse a conversion-type-id:
8671
8672    conversion-type-id:
8673      type-specifier-seq conversion-declarator [opt]
8674
8675    Returns the TYPE specified.  */
8676
8677 static tree
8678 cp_parser_conversion_type_id (cp_parser* parser)
8679 {
8680   tree attributes;
8681   cp_decl_specifier_seq type_specifiers;
8682   cp_declarator *declarator;
8683   tree type_specified;
8684
8685   /* Parse the attributes.  */
8686   attributes = cp_parser_attributes_opt (parser);
8687   /* Parse the type-specifiers.  */
8688   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8689                                 &type_specifiers);
8690   /* If that didn't work, stop.  */
8691   if (type_specifiers.type == error_mark_node)
8692     return error_mark_node;
8693   /* Parse the conversion-declarator.  */
8694   declarator = cp_parser_conversion_declarator_opt (parser);
8695
8696   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8697                                     /*initialized=*/0, &attributes);
8698   if (attributes)
8699     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8700   return type_specified;
8701 }
8702
8703 /* Parse an (optional) conversion-declarator.
8704
8705    conversion-declarator:
8706      ptr-operator conversion-declarator [opt]
8707
8708    */
8709
8710 static cp_declarator *
8711 cp_parser_conversion_declarator_opt (cp_parser* parser)
8712 {
8713   enum tree_code code;
8714   tree class_type;
8715   cp_cv_quals cv_quals;
8716
8717   /* We don't know if there's a ptr-operator next, or not.  */
8718   cp_parser_parse_tentatively (parser);
8719   /* Try the ptr-operator.  */
8720   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8721   /* If it worked, look for more conversion-declarators.  */
8722   if (cp_parser_parse_definitely (parser))
8723     {
8724       cp_declarator *declarator;
8725
8726       /* Parse another optional declarator.  */
8727       declarator = cp_parser_conversion_declarator_opt (parser);
8728
8729       return cp_parser_make_indirect_declarator
8730         (code, class_type, cv_quals, declarator);
8731    }
8732
8733   return NULL;
8734 }
8735
8736 /* Parse an (optional) ctor-initializer.
8737
8738    ctor-initializer:
8739      : mem-initializer-list
8740
8741    Returns TRUE iff the ctor-initializer was actually present.  */
8742
8743 static bool
8744 cp_parser_ctor_initializer_opt (cp_parser* parser)
8745 {
8746   /* If the next token is not a `:', then there is no
8747      ctor-initializer.  */
8748   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8749     {
8750       /* Do default initialization of any bases and members.  */
8751       if (DECL_CONSTRUCTOR_P (current_function_decl))
8752         finish_mem_initializers (NULL_TREE);
8753
8754       return false;
8755     }
8756
8757   /* Consume the `:' token.  */
8758   cp_lexer_consume_token (parser->lexer);
8759   /* And the mem-initializer-list.  */
8760   cp_parser_mem_initializer_list (parser);
8761
8762   return true;
8763 }
8764
8765 /* Parse a mem-initializer-list.
8766
8767    mem-initializer-list:
8768      mem-initializer ... [opt]
8769      mem-initializer ... [opt] , mem-initializer-list  */
8770
8771 static void
8772 cp_parser_mem_initializer_list (cp_parser* parser)
8773 {
8774   tree mem_initializer_list = NULL_TREE;
8775
8776   /* Let the semantic analysis code know that we are starting the
8777      mem-initializer-list.  */
8778   if (!DECL_CONSTRUCTOR_P (current_function_decl))
8779     error ("only constructors take base initializers");
8780
8781   /* Loop through the list.  */
8782   while (true)
8783     {
8784       tree mem_initializer;
8785
8786       /* Parse the mem-initializer.  */
8787       mem_initializer = cp_parser_mem_initializer (parser);
8788       /* If the next token is a `...', we're expanding member initializers. */
8789       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8790         {
8791           /* Consume the `...'. */
8792           cp_lexer_consume_token (parser->lexer);
8793
8794           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8795              can be expanded but members cannot. */
8796           if (mem_initializer != error_mark_node
8797               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8798             {
8799               error ("cannot expand initializer for member %<%D%>", 
8800                      TREE_PURPOSE (mem_initializer));
8801               mem_initializer = error_mark_node;
8802             }
8803
8804           /* Construct the pack expansion type. */
8805           if (mem_initializer != error_mark_node)
8806             mem_initializer = make_pack_expansion (mem_initializer);
8807         }
8808       /* Add it to the list, unless it was erroneous.  */
8809       if (mem_initializer != error_mark_node)
8810         {
8811           TREE_CHAIN (mem_initializer) = mem_initializer_list;
8812           mem_initializer_list = mem_initializer;
8813         }
8814       /* If the next token is not a `,', we're done.  */
8815       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8816         break;
8817       /* Consume the `,' token.  */
8818       cp_lexer_consume_token (parser->lexer);
8819     }
8820
8821   /* Perform semantic analysis.  */
8822   if (DECL_CONSTRUCTOR_P (current_function_decl))
8823     finish_mem_initializers (mem_initializer_list);
8824 }
8825
8826 /* Parse a mem-initializer.
8827
8828    mem-initializer:
8829      mem-initializer-id ( expression-list [opt] )
8830
8831    GNU extension:
8832
8833    mem-initializer:
8834      ( expression-list [opt] )
8835
8836    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
8837    class) or FIELD_DECL (for a non-static data member) to initialize;
8838    the TREE_VALUE is the expression-list.  An empty initialization
8839    list is represented by void_list_node.  */
8840
8841 static tree
8842 cp_parser_mem_initializer (cp_parser* parser)
8843 {
8844   tree mem_initializer_id;
8845   tree expression_list;
8846   tree member;
8847
8848   /* Find out what is being initialized.  */
8849   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8850     {
8851       pedwarn ("anachronistic old-style base class initializer");
8852       mem_initializer_id = NULL_TREE;
8853     }
8854   else
8855     mem_initializer_id = cp_parser_mem_initializer_id (parser);
8856   member = expand_member_init (mem_initializer_id);
8857   if (member && !DECL_P (member))
8858     in_base_initializer = 1;
8859
8860   expression_list
8861     = cp_parser_parenthesized_expression_list (parser, false,
8862                                                /*cast_p=*/false,
8863                                                /*allow_expansion_p=*/true,
8864                                                /*non_constant_p=*/NULL);
8865   if (expression_list == error_mark_node)
8866     return error_mark_node;
8867   if (!expression_list)
8868     expression_list = void_type_node;
8869
8870   in_base_initializer = 0;
8871
8872   return member ? build_tree_list (member, expression_list) : error_mark_node;
8873 }
8874
8875 /* Parse a mem-initializer-id.
8876
8877    mem-initializer-id:
8878      :: [opt] nested-name-specifier [opt] class-name
8879      identifier
8880
8881    Returns a TYPE indicating the class to be initializer for the first
8882    production.  Returns an IDENTIFIER_NODE indicating the data member
8883    to be initialized for the second production.  */
8884
8885 static tree
8886 cp_parser_mem_initializer_id (cp_parser* parser)
8887 {
8888   bool global_scope_p;
8889   bool nested_name_specifier_p;
8890   bool template_p = false;
8891   tree id;
8892
8893   /* `typename' is not allowed in this context ([temp.res]).  */
8894   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8895     {
8896       error ("keyword %<typename%> not allowed in this context (a qualified "
8897              "member initializer is implicitly a type)");
8898       cp_lexer_consume_token (parser->lexer);
8899     }
8900   /* Look for the optional `::' operator.  */
8901   global_scope_p
8902     = (cp_parser_global_scope_opt (parser,
8903                                    /*current_scope_valid_p=*/false)
8904        != NULL_TREE);
8905   /* Look for the optional nested-name-specifier.  The simplest way to
8906      implement:
8907
8908        [temp.res]
8909
8910        The keyword `typename' is not permitted in a base-specifier or
8911        mem-initializer; in these contexts a qualified name that
8912        depends on a template-parameter is implicitly assumed to be a
8913        type name.
8914
8915      is to assume that we have seen the `typename' keyword at this
8916      point.  */
8917   nested_name_specifier_p
8918     = (cp_parser_nested_name_specifier_opt (parser,
8919                                             /*typename_keyword_p=*/true,
8920                                             /*check_dependency_p=*/true,
8921                                             /*type_p=*/true,
8922                                             /*is_declaration=*/true)
8923        != NULL_TREE);
8924   if (nested_name_specifier_p)
8925     template_p = cp_parser_optional_template_keyword (parser);
8926   /* If there is a `::' operator or a nested-name-specifier, then we
8927      are definitely looking for a class-name.  */
8928   if (global_scope_p || nested_name_specifier_p)
8929     return cp_parser_class_name (parser,
8930                                  /*typename_keyword_p=*/true,
8931                                  /*template_keyword_p=*/template_p,
8932                                  none_type,
8933                                  /*check_dependency_p=*/true,
8934                                  /*class_head_p=*/false,
8935                                  /*is_declaration=*/true);
8936   /* Otherwise, we could also be looking for an ordinary identifier.  */
8937   cp_parser_parse_tentatively (parser);
8938   /* Try a class-name.  */
8939   id = cp_parser_class_name (parser,
8940                              /*typename_keyword_p=*/true,
8941                              /*template_keyword_p=*/false,
8942                              none_type,
8943                              /*check_dependency_p=*/true,
8944                              /*class_head_p=*/false,
8945                              /*is_declaration=*/true);
8946   /* If we found one, we're done.  */
8947   if (cp_parser_parse_definitely (parser))
8948     return id;
8949   /* Otherwise, look for an ordinary identifier.  */
8950   return cp_parser_identifier (parser);
8951 }
8952
8953 /* Overloading [gram.over] */
8954
8955 /* Parse an operator-function-id.
8956
8957    operator-function-id:
8958      operator operator
8959
8960    Returns an IDENTIFIER_NODE for the operator which is a
8961    human-readable spelling of the identifier, e.g., `operator +'.  */
8962
8963 static tree
8964 cp_parser_operator_function_id (cp_parser* parser)
8965 {
8966   /* Look for the `operator' keyword.  */
8967   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8968     return error_mark_node;
8969   /* And then the name of the operator itself.  */
8970   return cp_parser_operator (parser);
8971 }
8972
8973 /* Parse an operator.
8974
8975    operator:
8976      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8977      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8978      || ++ -- , ->* -> () []
8979
8980    GNU Extensions:
8981
8982    operator:
8983      <? >? <?= >?=
8984
8985    Returns an IDENTIFIER_NODE for the operator which is a
8986    human-readable spelling of the identifier, e.g., `operator +'.  */
8987
8988 static tree
8989 cp_parser_operator (cp_parser* parser)
8990 {
8991   tree id = NULL_TREE;
8992   cp_token *token;
8993
8994   /* Peek at the next token.  */
8995   token = cp_lexer_peek_token (parser->lexer);
8996   /* Figure out which operator we have.  */
8997   switch (token->type)
8998     {
8999     case CPP_KEYWORD:
9000       {
9001         enum tree_code op;
9002
9003         /* The keyword should be either `new' or `delete'.  */
9004         if (token->keyword == RID_NEW)
9005           op = NEW_EXPR;
9006         else if (token->keyword == RID_DELETE)
9007           op = DELETE_EXPR;
9008         else
9009           break;
9010
9011         /* Consume the `new' or `delete' token.  */
9012         cp_lexer_consume_token (parser->lexer);
9013
9014         /* Peek at the next token.  */
9015         token = cp_lexer_peek_token (parser->lexer);
9016         /* If it's a `[' token then this is the array variant of the
9017            operator.  */
9018         if (token->type == CPP_OPEN_SQUARE)
9019           {
9020             /* Consume the `[' token.  */
9021             cp_lexer_consume_token (parser->lexer);
9022             /* Look for the `]' token.  */
9023             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9024             id = ansi_opname (op == NEW_EXPR
9025                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9026           }
9027         /* Otherwise, we have the non-array variant.  */
9028         else
9029           id = ansi_opname (op);
9030
9031         return id;
9032       }
9033
9034     case CPP_PLUS:
9035       id = ansi_opname (PLUS_EXPR);
9036       break;
9037
9038     case CPP_MINUS:
9039       id = ansi_opname (MINUS_EXPR);
9040       break;
9041
9042     case CPP_MULT:
9043       id = ansi_opname (MULT_EXPR);
9044       break;
9045
9046     case CPP_DIV:
9047       id = ansi_opname (TRUNC_DIV_EXPR);
9048       break;
9049
9050     case CPP_MOD:
9051       id = ansi_opname (TRUNC_MOD_EXPR);
9052       break;
9053
9054     case CPP_XOR:
9055       id = ansi_opname (BIT_XOR_EXPR);
9056       break;
9057
9058     case CPP_AND:
9059       id = ansi_opname (BIT_AND_EXPR);
9060       break;
9061
9062     case CPP_OR:
9063       id = ansi_opname (BIT_IOR_EXPR);
9064       break;
9065
9066     case CPP_COMPL:
9067       id = ansi_opname (BIT_NOT_EXPR);
9068       break;
9069
9070     case CPP_NOT:
9071       id = ansi_opname (TRUTH_NOT_EXPR);
9072       break;
9073
9074     case CPP_EQ:
9075       id = ansi_assopname (NOP_EXPR);
9076       break;
9077
9078     case CPP_LESS:
9079       id = ansi_opname (LT_EXPR);
9080       break;
9081
9082     case CPP_GREATER:
9083       id = ansi_opname (GT_EXPR);
9084       break;
9085
9086     case CPP_PLUS_EQ:
9087       id = ansi_assopname (PLUS_EXPR);
9088       break;
9089
9090     case CPP_MINUS_EQ:
9091       id = ansi_assopname (MINUS_EXPR);
9092       break;
9093
9094     case CPP_MULT_EQ:
9095       id = ansi_assopname (MULT_EXPR);
9096       break;
9097
9098     case CPP_DIV_EQ:
9099       id = ansi_assopname (TRUNC_DIV_EXPR);
9100       break;
9101
9102     case CPP_MOD_EQ:
9103       id = ansi_assopname (TRUNC_MOD_EXPR);
9104       break;
9105
9106     case CPP_XOR_EQ:
9107       id = ansi_assopname (BIT_XOR_EXPR);
9108       break;
9109
9110     case CPP_AND_EQ:
9111       id = ansi_assopname (BIT_AND_EXPR);
9112       break;
9113
9114     case CPP_OR_EQ:
9115       id = ansi_assopname (BIT_IOR_EXPR);
9116       break;
9117
9118     case CPP_LSHIFT:
9119       id = ansi_opname (LSHIFT_EXPR);
9120       break;
9121
9122     case CPP_RSHIFT:
9123       id = ansi_opname (RSHIFT_EXPR);
9124       break;
9125
9126     case CPP_LSHIFT_EQ:
9127       id = ansi_assopname (LSHIFT_EXPR);
9128       break;
9129
9130     case CPP_RSHIFT_EQ:
9131       id = ansi_assopname (RSHIFT_EXPR);
9132       break;
9133
9134     case CPP_EQ_EQ:
9135       id = ansi_opname (EQ_EXPR);
9136       break;
9137
9138     case CPP_NOT_EQ:
9139       id = ansi_opname (NE_EXPR);
9140       break;
9141
9142     case CPP_LESS_EQ:
9143       id = ansi_opname (LE_EXPR);
9144       break;
9145
9146     case CPP_GREATER_EQ:
9147       id = ansi_opname (GE_EXPR);
9148       break;
9149
9150     case CPP_AND_AND:
9151       id = ansi_opname (TRUTH_ANDIF_EXPR);
9152       break;
9153
9154     case CPP_OR_OR:
9155       id = ansi_opname (TRUTH_ORIF_EXPR);
9156       break;
9157
9158     case CPP_PLUS_PLUS:
9159       id = ansi_opname (POSTINCREMENT_EXPR);
9160       break;
9161
9162     case CPP_MINUS_MINUS:
9163       id = ansi_opname (PREDECREMENT_EXPR);
9164       break;
9165
9166     case CPP_COMMA:
9167       id = ansi_opname (COMPOUND_EXPR);
9168       break;
9169
9170     case CPP_DEREF_STAR:
9171       id = ansi_opname (MEMBER_REF);
9172       break;
9173
9174     case CPP_DEREF:
9175       id = ansi_opname (COMPONENT_REF);
9176       break;
9177
9178     case CPP_OPEN_PAREN:
9179       /* Consume the `('.  */
9180       cp_lexer_consume_token (parser->lexer);
9181       /* Look for the matching `)'.  */
9182       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9183       return ansi_opname (CALL_EXPR);
9184
9185     case CPP_OPEN_SQUARE:
9186       /* Consume the `['.  */
9187       cp_lexer_consume_token (parser->lexer);
9188       /* Look for the matching `]'.  */
9189       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9190       return ansi_opname (ARRAY_REF);
9191
9192     default:
9193       /* Anything else is an error.  */
9194       break;
9195     }
9196
9197   /* If we have selected an identifier, we need to consume the
9198      operator token.  */
9199   if (id)
9200     cp_lexer_consume_token (parser->lexer);
9201   /* Otherwise, no valid operator name was present.  */
9202   else
9203     {
9204       cp_parser_error (parser, "expected operator");
9205       id = error_mark_node;
9206     }
9207
9208   return id;
9209 }
9210
9211 /* Parse a template-declaration.
9212
9213    template-declaration:
9214      export [opt] template < template-parameter-list > declaration
9215
9216    If MEMBER_P is TRUE, this template-declaration occurs within a
9217    class-specifier.
9218
9219    The grammar rule given by the standard isn't correct.  What
9220    is really meant is:
9221
9222    template-declaration:
9223      export [opt] template-parameter-list-seq
9224        decl-specifier-seq [opt] init-declarator [opt] ;
9225      export [opt] template-parameter-list-seq
9226        function-definition
9227
9228    template-parameter-list-seq:
9229      template-parameter-list-seq [opt]
9230      template < template-parameter-list >  */
9231
9232 static void
9233 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9234 {
9235   /* Check for `export'.  */
9236   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9237     {
9238       /* Consume the `export' token.  */
9239       cp_lexer_consume_token (parser->lexer);
9240       /* Warn that we do not support `export'.  */
9241       warning (0, "keyword %<export%> not implemented, and will be ignored");
9242     }
9243
9244   cp_parser_template_declaration_after_export (parser, member_p);
9245 }
9246
9247 /* Parse a template-parameter-list.
9248
9249    template-parameter-list:
9250      template-parameter
9251      template-parameter-list , template-parameter
9252
9253    Returns a TREE_LIST.  Each node represents a template parameter.
9254    The nodes are connected via their TREE_CHAINs.  */
9255
9256 static tree
9257 cp_parser_template_parameter_list (cp_parser* parser)
9258 {
9259   tree parameter_list = NULL_TREE;
9260
9261   begin_template_parm_list ();
9262   while (true)
9263     {
9264       tree parameter;
9265       cp_token *token;
9266       bool is_non_type;
9267       bool is_parameter_pack;
9268
9269       /* Parse the template-parameter.  */
9270       parameter = cp_parser_template_parameter (parser, 
9271                                                 &is_non_type,
9272                                                 &is_parameter_pack);
9273       /* Add it to the list.  */
9274       if (parameter != error_mark_node)
9275         parameter_list = process_template_parm (parameter_list,
9276                                                 parameter,
9277                                                 is_non_type,
9278                                                 is_parameter_pack);
9279       else
9280        {
9281          tree err_parm = build_tree_list (parameter, parameter);
9282          TREE_VALUE (err_parm) = error_mark_node;
9283          parameter_list = chainon (parameter_list, err_parm);
9284        }
9285
9286       /* Peek at the next token.  */
9287       token = cp_lexer_peek_token (parser->lexer);
9288       /* If it's not a `,', we're done.  */
9289       if (token->type != CPP_COMMA)
9290         break;
9291       /* Otherwise, consume the `,' token.  */
9292       cp_lexer_consume_token (parser->lexer);
9293     }
9294
9295   return end_template_parm_list (parameter_list);
9296 }
9297
9298 /* Parse a template-parameter.
9299
9300    template-parameter:
9301      type-parameter
9302      parameter-declaration
9303
9304    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9305    the parameter.  The TREE_PURPOSE is the default value, if any.
9306    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9307    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9308    set to true iff this parameter is a parameter pack. */
9309
9310 static tree
9311 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9312                               bool *is_parameter_pack)
9313 {
9314   cp_token *token;
9315   cp_parameter_declarator *parameter_declarator;
9316   cp_declarator *id_declarator;
9317   tree parm;
9318
9319   /* Assume it is a type parameter or a template parameter.  */
9320   *is_non_type = false;
9321   /* Assume it not a parameter pack. */
9322   *is_parameter_pack = false;
9323   /* Peek at the next token.  */
9324   token = cp_lexer_peek_token (parser->lexer);
9325   /* If it is `class' or `template', we have a type-parameter.  */
9326   if (token->keyword == RID_TEMPLATE)
9327     return cp_parser_type_parameter (parser, is_parameter_pack);
9328   /* If it is `class' or `typename' we do not know yet whether it is a
9329      type parameter or a non-type parameter.  Consider:
9330
9331        template <typename T, typename T::X X> ...
9332
9333      or:
9334
9335        template <class C, class D*> ...
9336
9337      Here, the first parameter is a type parameter, and the second is
9338      a non-type parameter.  We can tell by looking at the token after
9339      the identifier -- if it is a `,', `=', or `>' then we have a type
9340      parameter.  */
9341   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9342     {
9343       /* Peek at the token after `class' or `typename'.  */
9344       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9345       /* If it's an ellipsis, we have a template type parameter
9346          pack. */
9347       if (token->type == CPP_ELLIPSIS)
9348         return cp_parser_type_parameter (parser, is_parameter_pack);
9349       /* If it's an identifier, skip it.  */
9350       if (token->type == CPP_NAME)
9351         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9352       /* Now, see if the token looks like the end of a template
9353          parameter.  */
9354       if (token->type == CPP_COMMA
9355           || token->type == CPP_EQ
9356           || token->type == CPP_GREATER)
9357         return cp_parser_type_parameter (parser, is_parameter_pack);
9358     }
9359
9360   /* Otherwise, it is a non-type parameter.
9361
9362      [temp.param]
9363
9364      When parsing a default template-argument for a non-type
9365      template-parameter, the first non-nested `>' is taken as the end
9366      of the template parameter-list rather than a greater-than
9367      operator.  */
9368   *is_non_type = true;
9369   parameter_declarator
9370      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9371                                         /*parenthesized_p=*/NULL);
9372
9373   /* If the parameter declaration is marked as a parameter pack, set
9374      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9375      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9376      grokdeclarator. */
9377   if (parameter_declarator
9378       && parameter_declarator->declarator
9379       && parameter_declarator->declarator->parameter_pack_p)
9380     {
9381       *is_parameter_pack = true;
9382       parameter_declarator->declarator->parameter_pack_p = false;
9383     }
9384
9385   /* If the next token is an ellipsis, and we don't already have it
9386      marked as a parameter pack, then we have a parameter pack (that
9387      has no declarator).  */
9388   if (!*is_parameter_pack
9389       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9390       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9391     {
9392       /* Consume the `...'.  */
9393       cp_lexer_consume_token (parser->lexer);
9394       maybe_warn_variadic_templates ();
9395       
9396       *is_parameter_pack = true;
9397
9398       /* Parameter packs cannot have default arguments.  However, a
9399          user may try to do so, so we'll parse them and give an
9400          appropriate diagnostic here.  */
9401       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9402         {
9403           /* Consume the `='.  */
9404           cp_lexer_consume_token (parser->lexer);
9405
9406           /* Find the name of the parameter pack.  */     
9407           id_declarator = parameter_declarator->declarator;
9408           while (id_declarator && id_declarator->kind != cdk_id)
9409             id_declarator = id_declarator->declarator;
9410           
9411           if (id_declarator && id_declarator->kind == cdk_id)
9412             error ("template parameter pack %qD cannot have a default argument",
9413                    id_declarator->u.id.unqualified_name);
9414           else
9415             error ("template parameter pack cannot have a default argument");
9416
9417           /* Parse the default argument, but throw away the result.  */
9418           cp_parser_default_argument (parser, /*template_parm_p=*/true);
9419         }
9420     }
9421
9422   parm = grokdeclarator (parameter_declarator->declarator,
9423                          &parameter_declarator->decl_specifiers,
9424                          PARM, /*initialized=*/0,
9425                          /*attrlist=*/NULL);
9426   if (parm == error_mark_node)
9427     return error_mark_node;
9428
9429   return build_tree_list (parameter_declarator->default_argument, parm);
9430 }
9431
9432 /* Parse a type-parameter.
9433
9434    type-parameter:
9435      class identifier [opt]
9436      class identifier [opt] = type-id
9437      typename identifier [opt]
9438      typename identifier [opt] = type-id
9439      template < template-parameter-list > class identifier [opt]
9440      template < template-parameter-list > class identifier [opt]
9441        = id-expression
9442
9443    GNU Extension (variadic templates):
9444
9445    type-parameter:
9446      class ... identifier [opt]
9447      typename ... identifier [opt]
9448
9449    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9450    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9451    the declaration of the parameter.
9452
9453    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9454
9455 static tree
9456 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9457 {
9458   cp_token *token;
9459   tree parameter;
9460
9461   /* Look for a keyword to tell us what kind of parameter this is.  */
9462   token = cp_parser_require (parser, CPP_KEYWORD,
9463                              "`class', `typename', or `template'");
9464   if (!token)
9465     return error_mark_node;
9466
9467   switch (token->keyword)
9468     {
9469     case RID_CLASS:
9470     case RID_TYPENAME:
9471       {
9472         tree identifier;
9473         tree default_argument;
9474
9475         /* If the next token is an ellipsis, we have a template
9476            argument pack. */
9477         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9478           {
9479             /* Consume the `...' token. */
9480             cp_lexer_consume_token (parser->lexer);
9481             maybe_warn_variadic_templates ();
9482
9483             *is_parameter_pack = true;
9484           }
9485
9486         /* If the next token is an identifier, then it names the
9487            parameter.  */
9488         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9489           identifier = cp_parser_identifier (parser);
9490         else
9491           identifier = NULL_TREE;
9492
9493         /* Create the parameter.  */
9494         parameter = finish_template_type_parm (class_type_node, identifier);
9495
9496         /* If the next token is an `=', we have a default argument.  */
9497         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9498           {
9499             /* Consume the `=' token.  */
9500             cp_lexer_consume_token (parser->lexer);
9501             /* Parse the default-argument.  */
9502             push_deferring_access_checks (dk_no_deferred);
9503             default_argument = cp_parser_type_id (parser);
9504
9505             /* Template parameter packs cannot have default
9506                arguments. */
9507             if (*is_parameter_pack)
9508               {
9509                 if (identifier)
9510                   error ("template parameter pack %qD cannot have a default argument", 
9511                          identifier);
9512                 else
9513                   error ("template parameter packs cannot have default arguments");
9514                 default_argument = NULL_TREE;
9515               }
9516             pop_deferring_access_checks ();
9517           }
9518         else
9519           default_argument = NULL_TREE;
9520
9521         /* Create the combined representation of the parameter and the
9522            default argument.  */
9523         parameter = build_tree_list (default_argument, parameter);
9524       }
9525       break;
9526
9527     case RID_TEMPLATE:
9528       {
9529         tree parameter_list;
9530         tree identifier;
9531         tree default_argument;
9532
9533         /* Look for the `<'.  */
9534         cp_parser_require (parser, CPP_LESS, "`<'");
9535         /* Parse the template-parameter-list.  */
9536         parameter_list = cp_parser_template_parameter_list (parser);
9537         /* Look for the `>'.  */
9538         cp_parser_require (parser, CPP_GREATER, "`>'");
9539         /* Look for the `class' keyword.  */
9540         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9541         /* If the next token is an ellipsis, we have a template
9542            argument pack. */
9543         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9544           {
9545             /* Consume the `...' token. */
9546             cp_lexer_consume_token (parser->lexer);
9547             maybe_warn_variadic_templates ();
9548
9549             *is_parameter_pack = true;
9550           }
9551         /* If the next token is an `=', then there is a
9552            default-argument.  If the next token is a `>', we are at
9553            the end of the parameter-list.  If the next token is a `,',
9554            then we are at the end of this parameter.  */
9555         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9556             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9557             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9558           {
9559             identifier = cp_parser_identifier (parser);
9560             /* Treat invalid names as if the parameter were nameless.  */
9561             if (identifier == error_mark_node)
9562               identifier = NULL_TREE;
9563           }
9564         else
9565           identifier = NULL_TREE;
9566
9567         /* Create the template parameter.  */
9568         parameter = finish_template_template_parm (class_type_node,
9569                                                    identifier);
9570
9571         /* If the next token is an `=', then there is a
9572            default-argument.  */
9573         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9574           {
9575             bool is_template;
9576
9577             /* Consume the `='.  */
9578             cp_lexer_consume_token (parser->lexer);
9579             /* Parse the id-expression.  */
9580             push_deferring_access_checks (dk_no_deferred);
9581             default_argument
9582               = cp_parser_id_expression (parser,
9583                                          /*template_keyword_p=*/false,
9584                                          /*check_dependency_p=*/true,
9585                                          /*template_p=*/&is_template,
9586                                          /*declarator_p=*/false,
9587                                          /*optional_p=*/false);
9588             if (TREE_CODE (default_argument) == TYPE_DECL)
9589               /* If the id-expression was a template-id that refers to
9590                  a template-class, we already have the declaration here,
9591                  so no further lookup is needed.  */
9592                  ;
9593             else
9594               /* Look up the name.  */
9595               default_argument
9596                 = cp_parser_lookup_name (parser, default_argument,
9597                                          none_type,
9598                                          /*is_template=*/is_template,
9599                                          /*is_namespace=*/false,
9600                                          /*check_dependency=*/true,
9601                                          /*ambiguous_decls=*/NULL);
9602             /* See if the default argument is valid.  */
9603             default_argument
9604               = check_template_template_default_arg (default_argument);
9605
9606             /* Template parameter packs cannot have default
9607                arguments. */
9608             if (*is_parameter_pack)
9609               {
9610                 if (identifier)
9611                   error ("template parameter pack %qD cannot have a default argument", 
9612                          identifier);
9613                 else
9614                   error ("template parameter packs cannot have default arguments");
9615                 default_argument = NULL_TREE;
9616               }
9617             pop_deferring_access_checks ();
9618           }
9619         else
9620           default_argument = NULL_TREE;
9621
9622         /* Create the combined representation of the parameter and the
9623            default argument.  */
9624         parameter = build_tree_list (default_argument, parameter);
9625       }
9626       break;
9627
9628     default:
9629       gcc_unreachable ();
9630       break;
9631     }
9632
9633   return parameter;
9634 }
9635
9636 /* Parse a template-id.
9637
9638    template-id:
9639      template-name < template-argument-list [opt] >
9640
9641    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9642    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9643    returned.  Otherwise, if the template-name names a function, or set
9644    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9645    names a class, returns a TYPE_DECL for the specialization.
9646
9647    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9648    uninstantiated templates.  */
9649
9650 static tree
9651 cp_parser_template_id (cp_parser *parser,
9652                        bool template_keyword_p,
9653                        bool check_dependency_p,
9654                        bool is_declaration)
9655 {
9656   int i;
9657   tree template;
9658   tree arguments;
9659   tree template_id;
9660   cp_token_position start_of_id = 0;
9661   deferred_access_check *chk;
9662   VEC (deferred_access_check,gc) *access_check;
9663   cp_token *next_token, *next_token_2;
9664   bool is_identifier;
9665
9666   /* If the next token corresponds to a template-id, there is no need
9667      to reparse it.  */
9668   next_token = cp_lexer_peek_token (parser->lexer);
9669   if (next_token->type == CPP_TEMPLATE_ID)
9670     {
9671       struct tree_check *check_value;
9672
9673       /* Get the stored value.  */
9674       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9675       /* Perform any access checks that were deferred.  */
9676       access_check = check_value->checks;
9677       if (access_check)
9678         {
9679           for (i = 0 ;
9680                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9681                ++i)
9682             {
9683               perform_or_defer_access_check (chk->binfo,
9684                                              chk->decl,
9685                                              chk->diag_decl);
9686             }
9687         }
9688       /* Return the stored value.  */
9689       return check_value->value;
9690     }
9691
9692   /* Avoid performing name lookup if there is no possibility of
9693      finding a template-id.  */
9694   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9695       || (next_token->type == CPP_NAME
9696           && !cp_parser_nth_token_starts_template_argument_list_p
9697                (parser, 2)))
9698     {
9699       cp_parser_error (parser, "expected template-id");
9700       return error_mark_node;
9701     }
9702
9703   /* Remember where the template-id starts.  */
9704   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9705     start_of_id = cp_lexer_token_position (parser->lexer, false);
9706
9707   push_deferring_access_checks (dk_deferred);
9708
9709   /* Parse the template-name.  */
9710   is_identifier = false;
9711   template = cp_parser_template_name (parser, template_keyword_p,
9712                                       check_dependency_p,
9713                                       is_declaration,
9714                                       &is_identifier);
9715   if (template == error_mark_node || is_identifier)
9716     {
9717       pop_deferring_access_checks ();
9718       return template;
9719     }
9720
9721   /* If we find the sequence `[:' after a template-name, it's probably
9722      a digraph-typo for `< ::'. Substitute the tokens and check if we can
9723      parse correctly the argument list.  */
9724   next_token = cp_lexer_peek_token (parser->lexer);
9725   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9726   if (next_token->type == CPP_OPEN_SQUARE
9727       && next_token->flags & DIGRAPH
9728       && next_token_2->type == CPP_COLON
9729       && !(next_token_2->flags & PREV_WHITE))
9730     {
9731       cp_parser_parse_tentatively (parser);
9732       /* Change `:' into `::'.  */
9733       next_token_2->type = CPP_SCOPE;
9734       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9735          CPP_LESS.  */
9736       cp_lexer_consume_token (parser->lexer);
9737       /* Parse the arguments.  */
9738       arguments = cp_parser_enclosed_template_argument_list (parser);
9739       if (!cp_parser_parse_definitely (parser))
9740         {
9741           /* If we couldn't parse an argument list, then we revert our changes
9742              and return simply an error. Maybe this is not a template-id
9743              after all.  */
9744           next_token_2->type = CPP_COLON;
9745           cp_parser_error (parser, "expected %<<%>");
9746           pop_deferring_access_checks ();
9747           return error_mark_node;
9748         }
9749       /* Otherwise, emit an error about the invalid digraph, but continue
9750          parsing because we got our argument list.  */
9751       pedwarn ("%<<::%> cannot begin a template-argument list");
9752       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9753               "between %<<%> and %<::%>");
9754       if (!flag_permissive)
9755         {
9756           static bool hint;
9757           if (!hint)
9758             {
9759               inform ("(if you use -fpermissive G++ will accept your code)");
9760               hint = true;
9761             }
9762         }
9763     }
9764   else
9765     {
9766       /* Look for the `<' that starts the template-argument-list.  */
9767       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9768         {
9769           pop_deferring_access_checks ();
9770           return error_mark_node;
9771         }
9772       /* Parse the arguments.  */
9773       arguments = cp_parser_enclosed_template_argument_list (parser);
9774     }
9775
9776   /* Build a representation of the specialization.  */
9777   if (TREE_CODE (template) == IDENTIFIER_NODE)
9778     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9779   else if (DECL_CLASS_TEMPLATE_P (template)
9780            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9781     {
9782       bool entering_scope;
9783       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9784          template (rather than some instantiation thereof) only if
9785          is not nested within some other construct.  For example, in
9786          "template <typename T> void f(T) { A<T>::", A<T> is just an
9787          instantiation of A.  */
9788       entering_scope = (template_parm_scope_p ()
9789                         && cp_lexer_next_token_is (parser->lexer,
9790                                                    CPP_SCOPE));
9791       template_id
9792         = finish_template_type (template, arguments, entering_scope);
9793     }
9794   else
9795     {
9796       /* If it's not a class-template or a template-template, it should be
9797          a function-template.  */
9798       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9799                    || TREE_CODE (template) == OVERLOAD
9800                    || BASELINK_P (template)));
9801
9802       template_id = lookup_template_function (template, arguments);
9803     }
9804
9805   /* If parsing tentatively, replace the sequence of tokens that makes
9806      up the template-id with a CPP_TEMPLATE_ID token.  That way,
9807      should we re-parse the token stream, we will not have to repeat
9808      the effort required to do the parse, nor will we issue duplicate
9809      error messages about problems during instantiation of the
9810      template.  */
9811   if (start_of_id)
9812     {
9813       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9814
9815       /* Reset the contents of the START_OF_ID token.  */
9816       token->type = CPP_TEMPLATE_ID;
9817       /* Retrieve any deferred checks.  Do not pop this access checks yet
9818          so the memory will not be reclaimed during token replacing below.  */
9819       token->u.tree_check_value = GGC_CNEW (struct tree_check);
9820       token->u.tree_check_value->value = template_id;
9821       token->u.tree_check_value->checks = get_deferred_access_checks ();
9822       token->keyword = RID_MAX;
9823
9824       /* Purge all subsequent tokens.  */
9825       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9826
9827       /* ??? Can we actually assume that, if template_id ==
9828          error_mark_node, we will have issued a diagnostic to the
9829          user, as opposed to simply marking the tentative parse as
9830          failed?  */
9831       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9832         error ("parse error in template argument list");
9833     }
9834
9835   pop_deferring_access_checks ();
9836   return template_id;
9837 }
9838
9839 /* Parse a template-name.
9840
9841    template-name:
9842      identifier
9843
9844    The standard should actually say:
9845
9846    template-name:
9847      identifier
9848      operator-function-id
9849
9850    A defect report has been filed about this issue.
9851
9852    A conversion-function-id cannot be a template name because they cannot
9853    be part of a template-id. In fact, looking at this code:
9854
9855    a.operator K<int>()
9856
9857    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9858    It is impossible to call a templated conversion-function-id with an
9859    explicit argument list, since the only allowed template parameter is
9860    the type to which it is converting.
9861
9862    If TEMPLATE_KEYWORD_P is true, then we have just seen the
9863    `template' keyword, in a construction like:
9864
9865      T::template f<3>()
9866
9867    In that case `f' is taken to be a template-name, even though there
9868    is no way of knowing for sure.
9869
9870    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9871    name refers to a set of overloaded functions, at least one of which
9872    is a template, or an IDENTIFIER_NODE with the name of the template,
9873    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
9874    names are looked up inside uninstantiated templates.  */
9875
9876 static tree
9877 cp_parser_template_name (cp_parser* parser,
9878                          bool template_keyword_p,
9879                          bool check_dependency_p,
9880                          bool is_declaration,
9881                          bool *is_identifier)
9882 {
9883   tree identifier;
9884   tree decl;
9885   tree fns;
9886
9887   /* If the next token is `operator', then we have either an
9888      operator-function-id or a conversion-function-id.  */
9889   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9890     {
9891       /* We don't know whether we're looking at an
9892          operator-function-id or a conversion-function-id.  */
9893       cp_parser_parse_tentatively (parser);
9894       /* Try an operator-function-id.  */
9895       identifier = cp_parser_operator_function_id (parser);
9896       /* If that didn't work, try a conversion-function-id.  */
9897       if (!cp_parser_parse_definitely (parser))
9898         {
9899           cp_parser_error (parser, "expected template-name");
9900           return error_mark_node;
9901         }
9902     }
9903   /* Look for the identifier.  */
9904   else
9905     identifier = cp_parser_identifier (parser);
9906
9907   /* If we didn't find an identifier, we don't have a template-id.  */
9908   if (identifier == error_mark_node)
9909     return error_mark_node;
9910
9911   /* If the name immediately followed the `template' keyword, then it
9912      is a template-name.  However, if the next token is not `<', then
9913      we do not treat it as a template-name, since it is not being used
9914      as part of a template-id.  This enables us to handle constructs
9915      like:
9916
9917        template <typename T> struct S { S(); };
9918        template <typename T> S<T>::S();
9919
9920      correctly.  We would treat `S' as a template -- if it were `S<T>'
9921      -- but we do not if there is no `<'.  */
9922
9923   if (processing_template_decl
9924       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9925     {
9926       /* In a declaration, in a dependent context, we pretend that the
9927          "template" keyword was present in order to improve error
9928          recovery.  For example, given:
9929
9930            template <typename T> void f(T::X<int>);
9931
9932          we want to treat "X<int>" as a template-id.  */
9933       if (is_declaration
9934           && !template_keyword_p
9935           && parser->scope && TYPE_P (parser->scope)
9936           && check_dependency_p
9937           && dependent_type_p (parser->scope)
9938           /* Do not do this for dtors (or ctors), since they never
9939              need the template keyword before their name.  */
9940           && !constructor_name_p (identifier, parser->scope))
9941         {
9942           cp_token_position start = 0;
9943
9944           /* Explain what went wrong.  */
9945           error ("non-template %qD used as template", identifier);
9946           inform ("use %<%T::template %D%> to indicate that it is a template",
9947                   parser->scope, identifier);
9948           /* If parsing tentatively, find the location of the "<" token.  */
9949           if (cp_parser_simulate_error (parser))
9950             start = cp_lexer_token_position (parser->lexer, true);
9951           /* Parse the template arguments so that we can issue error
9952              messages about them.  */
9953           cp_lexer_consume_token (parser->lexer);
9954           cp_parser_enclosed_template_argument_list (parser);
9955           /* Skip tokens until we find a good place from which to
9956              continue parsing.  */
9957           cp_parser_skip_to_closing_parenthesis (parser,
9958                                                  /*recovering=*/true,
9959                                                  /*or_comma=*/true,
9960                                                  /*consume_paren=*/false);
9961           /* If parsing tentatively, permanently remove the
9962              template argument list.  That will prevent duplicate
9963              error messages from being issued about the missing
9964              "template" keyword.  */
9965           if (start)
9966             cp_lexer_purge_tokens_after (parser->lexer, start);
9967           if (is_identifier)
9968             *is_identifier = true;
9969           return identifier;
9970         }
9971
9972       /* If the "template" keyword is present, then there is generally
9973          no point in doing name-lookup, so we just return IDENTIFIER.
9974          But, if the qualifying scope is non-dependent then we can
9975          (and must) do name-lookup normally.  */
9976       if (template_keyword_p
9977           && (!parser->scope
9978               || (TYPE_P (parser->scope)
9979                   && dependent_type_p (parser->scope))))
9980         return identifier;
9981     }
9982
9983   /* Look up the name.  */
9984   decl = cp_parser_lookup_name (parser, identifier,
9985                                 none_type,
9986                                 /*is_template=*/false,
9987                                 /*is_namespace=*/false,
9988                                 check_dependency_p,
9989                                 /*ambiguous_decls=*/NULL);
9990   decl = maybe_get_template_decl_from_type_decl (decl);
9991
9992   /* If DECL is a template, then the name was a template-name.  */
9993   if (TREE_CODE (decl) == TEMPLATE_DECL)
9994     ;
9995   else
9996     {
9997       tree fn = NULL_TREE;
9998
9999       /* The standard does not explicitly indicate whether a name that
10000          names a set of overloaded declarations, some of which are
10001          templates, is a template-name.  However, such a name should
10002          be a template-name; otherwise, there is no way to form a
10003          template-id for the overloaded templates.  */
10004       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10005       if (TREE_CODE (fns) == OVERLOAD)
10006         for (fn = fns; fn; fn = OVL_NEXT (fn))
10007           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10008             break;
10009
10010       if (!fn)
10011         {
10012           /* The name does not name a template.  */
10013           cp_parser_error (parser, "expected template-name");
10014           return error_mark_node;
10015         }
10016     }
10017
10018   /* If DECL is dependent, and refers to a function, then just return
10019      its name; we will look it up again during template instantiation.  */
10020   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10021     {
10022       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10023       if (TYPE_P (scope) && dependent_type_p (scope))
10024         return identifier;
10025     }
10026
10027   return decl;
10028 }
10029
10030 /* Parse a template-argument-list.
10031
10032    template-argument-list:
10033      template-argument ... [opt]
10034      template-argument-list , template-argument ... [opt]
10035
10036    Returns a TREE_VEC containing the arguments.  */
10037
10038 static tree
10039 cp_parser_template_argument_list (cp_parser* parser)
10040 {
10041   tree fixed_args[10];
10042   unsigned n_args = 0;
10043   unsigned alloced = 10;
10044   tree *arg_ary = fixed_args;
10045   tree vec;
10046   bool saved_in_template_argument_list_p;
10047   bool saved_ice_p;
10048   bool saved_non_ice_p;
10049
10050   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10051   parser->in_template_argument_list_p = true;
10052   /* Even if the template-id appears in an integral
10053      constant-expression, the contents of the argument list do
10054      not.  */
10055   saved_ice_p = parser->integral_constant_expression_p;
10056   parser->integral_constant_expression_p = false;
10057   saved_non_ice_p = parser->non_integral_constant_expression_p;
10058   parser->non_integral_constant_expression_p = false;
10059   /* Parse the arguments.  */
10060   do
10061     {
10062       tree argument;
10063
10064       if (n_args)
10065         /* Consume the comma.  */
10066         cp_lexer_consume_token (parser->lexer);
10067
10068       /* Parse the template-argument.  */
10069       argument = cp_parser_template_argument (parser);
10070
10071       /* If the next token is an ellipsis, we're expanding a template
10072          argument pack. */
10073       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10074         {
10075           /* Consume the `...' token. */
10076           cp_lexer_consume_token (parser->lexer);
10077
10078           /* Make the argument into a TYPE_PACK_EXPANSION or
10079              EXPR_PACK_EXPANSION. */
10080           argument = make_pack_expansion (argument);
10081         }
10082
10083       if (n_args == alloced)
10084         {
10085           alloced *= 2;
10086
10087           if (arg_ary == fixed_args)
10088             {
10089               arg_ary = XNEWVEC (tree, alloced);
10090               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10091             }
10092           else
10093             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10094         }
10095       arg_ary[n_args++] = argument;
10096     }
10097   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10098
10099   vec = make_tree_vec (n_args);
10100
10101   while (n_args--)
10102     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10103
10104   if (arg_ary != fixed_args)
10105     free (arg_ary);
10106   parser->non_integral_constant_expression_p = saved_non_ice_p;
10107   parser->integral_constant_expression_p = saved_ice_p;
10108   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10109   return vec;
10110 }
10111
10112 /* Parse a template-argument.
10113
10114    template-argument:
10115      assignment-expression
10116      type-id
10117      id-expression
10118
10119    The representation is that of an assignment-expression, type-id, or
10120    id-expression -- except that the qualified id-expression is
10121    evaluated, so that the value returned is either a DECL or an
10122    OVERLOAD.
10123
10124    Although the standard says "assignment-expression", it forbids
10125    throw-expressions or assignments in the template argument.
10126    Therefore, we use "conditional-expression" instead.  */
10127
10128 static tree
10129 cp_parser_template_argument (cp_parser* parser)
10130 {
10131   tree argument;
10132   bool template_p;
10133   bool address_p;
10134   bool maybe_type_id = false;
10135   cp_token *token;
10136   cp_id_kind idk;
10137
10138   /* There's really no way to know what we're looking at, so we just
10139      try each alternative in order.
10140
10141        [temp.arg]
10142
10143        In a template-argument, an ambiguity between a type-id and an
10144        expression is resolved to a type-id, regardless of the form of
10145        the corresponding template-parameter.
10146
10147      Therefore, we try a type-id first.  */
10148   cp_parser_parse_tentatively (parser);
10149   argument = cp_parser_type_id (parser);
10150   /* If there was no error parsing the type-id but the next token is a '>>',
10151      we probably found a typo for '> >'. But there are type-id which are
10152      also valid expressions. For instance:
10153
10154      struct X { int operator >> (int); };
10155      template <int V> struct Foo {};
10156      Foo<X () >> 5> r;
10157
10158      Here 'X()' is a valid type-id of a function type, but the user just
10159      wanted to write the expression "X() >> 5". Thus, we remember that we
10160      found a valid type-id, but we still try to parse the argument as an
10161      expression to see what happens.  */
10162   if (!cp_parser_error_occurred (parser)
10163       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10164     {
10165       maybe_type_id = true;
10166       cp_parser_abort_tentative_parse (parser);
10167     }
10168   else
10169     {
10170       /* If the next token isn't a `,' or a `>', then this argument wasn't
10171       really finished. This means that the argument is not a valid
10172       type-id.  */
10173       if (!cp_parser_next_token_ends_template_argument_p (parser))
10174         cp_parser_error (parser, "expected template-argument");
10175       /* If that worked, we're done.  */
10176       if (cp_parser_parse_definitely (parser))
10177         return argument;
10178     }
10179   /* We're still not sure what the argument will be.  */
10180   cp_parser_parse_tentatively (parser);
10181   /* Try a template.  */
10182   argument = cp_parser_id_expression (parser,
10183                                       /*template_keyword_p=*/false,
10184                                       /*check_dependency_p=*/true,
10185                                       &template_p,
10186                                       /*declarator_p=*/false,
10187                                       /*optional_p=*/false);
10188   /* If the next token isn't a `,' or a `>', then this argument wasn't
10189      really finished.  */
10190   if (!cp_parser_next_token_ends_template_argument_p (parser))
10191     cp_parser_error (parser, "expected template-argument");
10192   if (!cp_parser_error_occurred (parser))
10193     {
10194       /* Figure out what is being referred to.  If the id-expression
10195          was for a class template specialization, then we will have a
10196          TYPE_DECL at this point.  There is no need to do name lookup
10197          at this point in that case.  */
10198       if (TREE_CODE (argument) != TYPE_DECL)
10199         argument = cp_parser_lookup_name (parser, argument,
10200                                           none_type,
10201                                           /*is_template=*/template_p,
10202                                           /*is_namespace=*/false,
10203                                           /*check_dependency=*/true,
10204                                           /*ambiguous_decls=*/NULL);
10205       if (TREE_CODE (argument) != TEMPLATE_DECL
10206           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10207         cp_parser_error (parser, "expected template-name");
10208     }
10209   if (cp_parser_parse_definitely (parser))
10210     return argument;
10211   /* It must be a non-type argument.  There permitted cases are given
10212      in [temp.arg.nontype]:
10213
10214      -- an integral constant-expression of integral or enumeration
10215         type; or
10216
10217      -- the name of a non-type template-parameter; or
10218
10219      -- the name of an object or function with external linkage...
10220
10221      -- the address of an object or function with external linkage...
10222
10223      -- a pointer to member...  */
10224   /* Look for a non-type template parameter.  */
10225   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10226     {
10227       cp_parser_parse_tentatively (parser);
10228       argument = cp_parser_primary_expression (parser,
10229                                                /*adress_p=*/false,
10230                                                /*cast_p=*/false,
10231                                                /*template_arg_p=*/true,
10232                                                &idk);
10233       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10234           || !cp_parser_next_token_ends_template_argument_p (parser))
10235         cp_parser_simulate_error (parser);
10236       if (cp_parser_parse_definitely (parser))
10237         return argument;
10238     }
10239
10240   /* If the next token is "&", the argument must be the address of an
10241      object or function with external linkage.  */
10242   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10243   if (address_p)
10244     cp_lexer_consume_token (parser->lexer);
10245   /* See if we might have an id-expression.  */
10246   token = cp_lexer_peek_token (parser->lexer);
10247   if (token->type == CPP_NAME
10248       || token->keyword == RID_OPERATOR
10249       || token->type == CPP_SCOPE
10250       || token->type == CPP_TEMPLATE_ID
10251       || token->type == CPP_NESTED_NAME_SPECIFIER)
10252     {
10253       cp_parser_parse_tentatively (parser);
10254       argument = cp_parser_primary_expression (parser,
10255                                                address_p,
10256                                                /*cast_p=*/false,
10257                                                /*template_arg_p=*/true,
10258                                                &idk);
10259       if (cp_parser_error_occurred (parser)
10260           || !cp_parser_next_token_ends_template_argument_p (parser))
10261         cp_parser_abort_tentative_parse (parser);
10262       else
10263         {
10264           if (TREE_CODE (argument) == INDIRECT_REF)
10265             {
10266               gcc_assert (REFERENCE_REF_P (argument));
10267               argument = TREE_OPERAND (argument, 0);
10268             }
10269
10270           if (TREE_CODE (argument) == VAR_DECL)
10271             {
10272               /* A variable without external linkage might still be a
10273                  valid constant-expression, so no error is issued here
10274                  if the external-linkage check fails.  */
10275               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10276                 cp_parser_simulate_error (parser);
10277             }
10278           else if (is_overloaded_fn (argument))
10279             /* All overloaded functions are allowed; if the external
10280                linkage test does not pass, an error will be issued
10281                later.  */
10282             ;
10283           else if (address_p
10284                    && (TREE_CODE (argument) == OFFSET_REF
10285                        || TREE_CODE (argument) == SCOPE_REF))
10286             /* A pointer-to-member.  */
10287             ;
10288           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10289             ;
10290           else
10291             cp_parser_simulate_error (parser);
10292
10293           if (cp_parser_parse_definitely (parser))
10294             {
10295               if (address_p)
10296                 argument = build_x_unary_op (ADDR_EXPR, argument);
10297               return argument;
10298             }
10299         }
10300     }
10301   /* If the argument started with "&", there are no other valid
10302      alternatives at this point.  */
10303   if (address_p)
10304     {
10305       cp_parser_error (parser, "invalid non-type template argument");
10306       return error_mark_node;
10307     }
10308
10309   /* If the argument wasn't successfully parsed as a type-id followed
10310      by '>>', the argument can only be a constant expression now.
10311      Otherwise, we try parsing the constant-expression tentatively,
10312      because the argument could really be a type-id.  */
10313   if (maybe_type_id)
10314     cp_parser_parse_tentatively (parser);
10315   argument = cp_parser_constant_expression (parser,
10316                                             /*allow_non_constant_p=*/false,
10317                                             /*non_constant_p=*/NULL);
10318   argument = fold_non_dependent_expr (argument);
10319   if (!maybe_type_id)
10320     return argument;
10321   if (!cp_parser_next_token_ends_template_argument_p (parser))
10322     cp_parser_error (parser, "expected template-argument");
10323   if (cp_parser_parse_definitely (parser))
10324     return argument;
10325   /* We did our best to parse the argument as a non type-id, but that
10326      was the only alternative that matched (albeit with a '>' after
10327      it). We can assume it's just a typo from the user, and a
10328      diagnostic will then be issued.  */
10329   return cp_parser_type_id (parser);
10330 }
10331
10332 /* Parse an explicit-instantiation.
10333
10334    explicit-instantiation:
10335      template declaration
10336
10337    Although the standard says `declaration', what it really means is:
10338
10339    explicit-instantiation:
10340      template decl-specifier-seq [opt] declarator [opt] ;
10341
10342    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10343    supposed to be allowed.  A defect report has been filed about this
10344    issue.
10345
10346    GNU Extension:
10347
10348    explicit-instantiation:
10349      storage-class-specifier template
10350        decl-specifier-seq [opt] declarator [opt] ;
10351      function-specifier template
10352        decl-specifier-seq [opt] declarator [opt] ;  */
10353
10354 static void
10355 cp_parser_explicit_instantiation (cp_parser* parser)
10356 {
10357   int declares_class_or_enum;
10358   cp_decl_specifier_seq decl_specifiers;
10359   tree extension_specifier = NULL_TREE;
10360
10361   /* Look for an (optional) storage-class-specifier or
10362      function-specifier.  */
10363   if (cp_parser_allow_gnu_extensions_p (parser))
10364     {
10365       extension_specifier
10366         = cp_parser_storage_class_specifier_opt (parser);
10367       if (!extension_specifier)
10368         extension_specifier
10369           = cp_parser_function_specifier_opt (parser,
10370                                               /*decl_specs=*/NULL);
10371     }
10372
10373   /* Look for the `template' keyword.  */
10374   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10375   /* Let the front end know that we are processing an explicit
10376      instantiation.  */
10377   begin_explicit_instantiation ();
10378   /* [temp.explicit] says that we are supposed to ignore access
10379      control while processing explicit instantiation directives.  */
10380   push_deferring_access_checks (dk_no_check);
10381   /* Parse a decl-specifier-seq.  */
10382   cp_parser_decl_specifier_seq (parser,
10383                                 CP_PARSER_FLAGS_OPTIONAL,
10384                                 &decl_specifiers,
10385                                 &declares_class_or_enum);
10386   /* If there was exactly one decl-specifier, and it declared a class,
10387      and there's no declarator, then we have an explicit type
10388      instantiation.  */
10389   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10390     {
10391       tree type;
10392
10393       type = check_tag_decl (&decl_specifiers);
10394       /* Turn access control back on for names used during
10395          template instantiation.  */
10396       pop_deferring_access_checks ();
10397       if (type)
10398         do_type_instantiation (type, extension_specifier,
10399                                /*complain=*/tf_error);
10400     }
10401   else
10402     {
10403       cp_declarator *declarator;
10404       tree decl;
10405
10406       /* Parse the declarator.  */
10407       declarator
10408         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10409                                 /*ctor_dtor_or_conv_p=*/NULL,
10410                                 /*parenthesized_p=*/NULL,
10411                                 /*member_p=*/false);
10412       if (declares_class_or_enum & 2)
10413         cp_parser_check_for_definition_in_return_type (declarator,
10414                                                        decl_specifiers.type);
10415       if (declarator != cp_error_declarator)
10416         {
10417           decl = grokdeclarator (declarator, &decl_specifiers,
10418                                  NORMAL, 0, &decl_specifiers.attributes);
10419           /* Turn access control back on for names used during
10420              template instantiation.  */
10421           pop_deferring_access_checks ();
10422           /* Do the explicit instantiation.  */
10423           do_decl_instantiation (decl, extension_specifier);
10424         }
10425       else
10426         {
10427           pop_deferring_access_checks ();
10428           /* Skip the body of the explicit instantiation.  */
10429           cp_parser_skip_to_end_of_statement (parser);
10430         }
10431     }
10432   /* We're done with the instantiation.  */
10433   end_explicit_instantiation ();
10434
10435   cp_parser_consume_semicolon_at_end_of_statement (parser);
10436 }
10437
10438 /* Parse an explicit-specialization.
10439
10440    explicit-specialization:
10441      template < > declaration
10442
10443    Although the standard says `declaration', what it really means is:
10444
10445    explicit-specialization:
10446      template <> decl-specifier [opt] init-declarator [opt] ;
10447      template <> function-definition
10448      template <> explicit-specialization
10449      template <> template-declaration  */
10450
10451 static void
10452 cp_parser_explicit_specialization (cp_parser* parser)
10453 {
10454   bool need_lang_pop;
10455   /* Look for the `template' keyword.  */
10456   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10457   /* Look for the `<'.  */
10458   cp_parser_require (parser, CPP_LESS, "`<'");
10459   /* Look for the `>'.  */
10460   cp_parser_require (parser, CPP_GREATER, "`>'");
10461   /* We have processed another parameter list.  */
10462   ++parser->num_template_parameter_lists;
10463   /* [temp]
10464
10465      A template ... explicit specialization ... shall not have C
10466      linkage.  */
10467   if (current_lang_name == lang_name_c)
10468     {
10469       error ("template specialization with C linkage");
10470       /* Give it C++ linkage to avoid confusing other parts of the
10471          front end.  */
10472       push_lang_context (lang_name_cplusplus);
10473       need_lang_pop = true;
10474     }
10475   else
10476     need_lang_pop = false;
10477   /* Let the front end know that we are beginning a specialization.  */
10478   if (!begin_specialization ())
10479     {
10480       end_specialization ();
10481       cp_parser_skip_to_end_of_block_or_statement (parser);
10482       return;
10483     }
10484
10485   /* If the next keyword is `template', we need to figure out whether
10486      or not we're looking a template-declaration.  */
10487   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10488     {
10489       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10490           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10491         cp_parser_template_declaration_after_export (parser,
10492                                                      /*member_p=*/false);
10493       else
10494         cp_parser_explicit_specialization (parser);
10495     }
10496   else
10497     /* Parse the dependent declaration.  */
10498     cp_parser_single_declaration (parser,
10499                                   /*checks=*/NULL,
10500                                   /*member_p=*/false,
10501                                   /*explicit_specialization_p=*/true,
10502                                   /*friend_p=*/NULL);
10503   /* We're done with the specialization.  */
10504   end_specialization ();
10505   /* For the erroneous case of a template with C linkage, we pushed an
10506      implicit C++ linkage scope; exit that scope now.  */
10507   if (need_lang_pop)
10508     pop_lang_context ();
10509   /* We're done with this parameter list.  */
10510   --parser->num_template_parameter_lists;
10511 }
10512
10513 /* Parse a type-specifier.
10514
10515    type-specifier:
10516      simple-type-specifier
10517      class-specifier
10518      enum-specifier
10519      elaborated-type-specifier
10520      cv-qualifier
10521
10522    GNU Extension:
10523
10524    type-specifier:
10525      __complex__
10526
10527    Returns a representation of the type-specifier.  For a
10528    class-specifier, enum-specifier, or elaborated-type-specifier, a
10529    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10530
10531    The parser flags FLAGS is used to control type-specifier parsing.
10532
10533    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10534    in a decl-specifier-seq.
10535
10536    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10537    class-specifier, enum-specifier, or elaborated-type-specifier, then
10538    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10539    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10540    zero.
10541
10542    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10543    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10544    is set to FALSE.  */
10545
10546 static tree
10547 cp_parser_type_specifier (cp_parser* parser,
10548                           cp_parser_flags flags,
10549                           cp_decl_specifier_seq *decl_specs,
10550                           bool is_declaration,
10551                           int* declares_class_or_enum,
10552                           bool* is_cv_qualifier)
10553 {
10554   tree type_spec = NULL_TREE;
10555   cp_token *token;
10556   enum rid keyword;
10557   cp_decl_spec ds = ds_last;
10558
10559   /* Assume this type-specifier does not declare a new type.  */
10560   if (declares_class_or_enum)
10561     *declares_class_or_enum = 0;
10562   /* And that it does not specify a cv-qualifier.  */
10563   if (is_cv_qualifier)
10564     *is_cv_qualifier = false;
10565   /* Peek at the next token.  */
10566   token = cp_lexer_peek_token (parser->lexer);
10567
10568   /* If we're looking at a keyword, we can use that to guide the
10569      production we choose.  */
10570   keyword = token->keyword;
10571   switch (keyword)
10572     {
10573     case RID_ENUM:
10574       /* Look for the enum-specifier.  */
10575       type_spec = cp_parser_enum_specifier (parser);
10576       /* If that worked, we're done.  */
10577       if (type_spec)
10578         {
10579           if (declares_class_or_enum)
10580             *declares_class_or_enum = 2;
10581           if (decl_specs)
10582             cp_parser_set_decl_spec_type (decl_specs,
10583                                           type_spec,
10584                                           /*user_defined_p=*/true);
10585           return type_spec;
10586         }
10587       else
10588         goto elaborated_type_specifier;
10589
10590       /* Any of these indicate either a class-specifier, or an
10591          elaborated-type-specifier.  */
10592     case RID_CLASS:
10593     case RID_STRUCT:
10594     case RID_UNION:
10595       /* Parse tentatively so that we can back up if we don't find a
10596          class-specifier.  */
10597       cp_parser_parse_tentatively (parser);
10598       /* Look for the class-specifier.  */
10599       type_spec = cp_parser_class_specifier (parser);
10600       /* If that worked, we're done.  */
10601       if (cp_parser_parse_definitely (parser))
10602         {
10603           if (declares_class_or_enum)
10604             *declares_class_or_enum = 2;
10605           if (decl_specs)
10606             cp_parser_set_decl_spec_type (decl_specs,
10607                                           type_spec,
10608                                           /*user_defined_p=*/true);
10609           return type_spec;
10610         }
10611
10612       /* Fall through.  */
10613     elaborated_type_specifier:
10614       /* We're declaring (not defining) a class or enum.  */
10615       if (declares_class_or_enum)
10616         *declares_class_or_enum = 1;
10617
10618       /* Fall through.  */
10619     case RID_TYPENAME:
10620       /* Look for an elaborated-type-specifier.  */
10621       type_spec
10622         = (cp_parser_elaborated_type_specifier
10623            (parser,
10624             decl_specs && decl_specs->specs[(int) ds_friend],
10625             is_declaration));
10626       if (decl_specs)
10627         cp_parser_set_decl_spec_type (decl_specs,
10628                                       type_spec,
10629                                       /*user_defined_p=*/true);
10630       return type_spec;
10631
10632     case RID_CONST:
10633       ds = ds_const;
10634       if (is_cv_qualifier)
10635         *is_cv_qualifier = true;
10636       break;
10637
10638     case RID_VOLATILE:
10639       ds = ds_volatile;
10640       if (is_cv_qualifier)
10641         *is_cv_qualifier = true;
10642       break;
10643
10644     case RID_RESTRICT:
10645       ds = ds_restrict;
10646       if (is_cv_qualifier)
10647         *is_cv_qualifier = true;
10648       break;
10649
10650     case RID_COMPLEX:
10651       /* The `__complex__' keyword is a GNU extension.  */
10652       ds = ds_complex;
10653       break;
10654
10655     default:
10656       break;
10657     }
10658
10659   /* Handle simple keywords.  */
10660   if (ds != ds_last)
10661     {
10662       if (decl_specs)
10663         {
10664           ++decl_specs->specs[(int)ds];
10665           decl_specs->any_specifiers_p = true;
10666         }
10667       return cp_lexer_consume_token (parser->lexer)->u.value;
10668     }
10669
10670   /* If we do not already have a type-specifier, assume we are looking
10671      at a simple-type-specifier.  */
10672   type_spec = cp_parser_simple_type_specifier (parser,
10673                                                decl_specs,
10674                                                flags);
10675
10676   /* If we didn't find a type-specifier, and a type-specifier was not
10677      optional in this context, issue an error message.  */
10678   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10679     {
10680       cp_parser_error (parser, "expected type specifier");
10681       return error_mark_node;
10682     }
10683
10684   return type_spec;
10685 }
10686
10687 /* Parse a simple-type-specifier.
10688
10689    simple-type-specifier:
10690      :: [opt] nested-name-specifier [opt] type-name
10691      :: [opt] nested-name-specifier template template-id
10692      char
10693      wchar_t
10694      bool
10695      short
10696      int
10697      long
10698      signed
10699      unsigned
10700      float
10701      double
10702      void
10703
10704    C++0x Extension:
10705
10706    simple-type-specifier:
10707      decltype ( expression )   
10708
10709    GNU Extension:
10710
10711    simple-type-specifier:
10712      __typeof__ unary-expression
10713      __typeof__ ( type-id )
10714
10715    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
10716    appropriately updated.  */
10717
10718 static tree
10719 cp_parser_simple_type_specifier (cp_parser* parser,
10720                                  cp_decl_specifier_seq *decl_specs,
10721                                  cp_parser_flags flags)
10722 {
10723   tree type = NULL_TREE;
10724   cp_token *token;
10725
10726   /* Peek at the next token.  */
10727   token = cp_lexer_peek_token (parser->lexer);
10728
10729   /* If we're looking at a keyword, things are easy.  */
10730   switch (token->keyword)
10731     {
10732     case RID_CHAR:
10733       if (decl_specs)
10734         decl_specs->explicit_char_p = true;
10735       type = char_type_node;
10736       break;
10737     case RID_WCHAR:
10738       type = wchar_type_node;
10739       break;
10740     case RID_BOOL:
10741       type = boolean_type_node;
10742       break;
10743     case RID_SHORT:
10744       if (decl_specs)
10745         ++decl_specs->specs[(int) ds_short];
10746       type = short_integer_type_node;
10747       break;
10748     case RID_INT:
10749       if (decl_specs)
10750         decl_specs->explicit_int_p = true;
10751       type = integer_type_node;
10752       break;
10753     case RID_LONG:
10754       if (decl_specs)
10755         ++decl_specs->specs[(int) ds_long];
10756       type = long_integer_type_node;
10757       break;
10758     case RID_SIGNED:
10759       if (decl_specs)
10760         ++decl_specs->specs[(int) ds_signed];
10761       type = integer_type_node;
10762       break;
10763     case RID_UNSIGNED:
10764       if (decl_specs)
10765         ++decl_specs->specs[(int) ds_unsigned];
10766       type = unsigned_type_node;
10767       break;
10768     case RID_FLOAT:
10769       type = float_type_node;
10770       break;
10771     case RID_DOUBLE:
10772       type = double_type_node;
10773       break;
10774     case RID_VOID:
10775       type = void_type_node;
10776       break;
10777
10778     case RID_DECLTYPE:
10779       /* Parse the `decltype' type.  */
10780       type = cp_parser_decltype (parser);
10781
10782       if (decl_specs)
10783         cp_parser_set_decl_spec_type (decl_specs, type,
10784                                       /*user_defined_p=*/true);
10785
10786       return type;
10787
10788     case RID_TYPEOF:
10789       /* Consume the `typeof' token.  */
10790       cp_lexer_consume_token (parser->lexer);
10791       /* Parse the operand to `typeof'.  */
10792       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10793       /* If it is not already a TYPE, take its type.  */
10794       if (!TYPE_P (type))
10795         type = finish_typeof (type);
10796
10797       if (decl_specs)
10798         cp_parser_set_decl_spec_type (decl_specs, type,
10799                                       /*user_defined_p=*/true);
10800
10801       return type;
10802
10803     default:
10804       break;
10805     }
10806
10807   /* If the type-specifier was for a built-in type, we're done.  */
10808   if (type)
10809     {
10810       tree id;
10811
10812       /* Record the type.  */
10813       if (decl_specs
10814           && (token->keyword != RID_SIGNED
10815               && token->keyword != RID_UNSIGNED
10816               && token->keyword != RID_SHORT
10817               && token->keyword != RID_LONG))
10818         cp_parser_set_decl_spec_type (decl_specs,
10819                                       type,
10820                                       /*user_defined=*/false);
10821       if (decl_specs)
10822         decl_specs->any_specifiers_p = true;
10823
10824       /* Consume the token.  */
10825       id = cp_lexer_consume_token (parser->lexer)->u.value;
10826
10827       /* There is no valid C++ program where a non-template type is
10828          followed by a "<".  That usually indicates that the user thought
10829          that the type was a template.  */
10830       cp_parser_check_for_invalid_template_id (parser, type);
10831
10832       return TYPE_NAME (type);
10833     }
10834
10835   /* The type-specifier must be a user-defined type.  */
10836   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10837     {
10838       bool qualified_p;
10839       bool global_p;
10840
10841       /* Don't gobble tokens or issue error messages if this is an
10842          optional type-specifier.  */
10843       if (flags & CP_PARSER_FLAGS_OPTIONAL)
10844         cp_parser_parse_tentatively (parser);
10845
10846       /* Look for the optional `::' operator.  */
10847       global_p
10848         = (cp_parser_global_scope_opt (parser,
10849                                        /*current_scope_valid_p=*/false)
10850            != NULL_TREE);
10851       /* Look for the nested-name specifier.  */
10852       qualified_p
10853         = (cp_parser_nested_name_specifier_opt (parser,
10854                                                 /*typename_keyword_p=*/false,
10855                                                 /*check_dependency_p=*/true,
10856                                                 /*type_p=*/false,
10857                                                 /*is_declaration=*/false)
10858            != NULL_TREE);
10859       /* If we have seen a nested-name-specifier, and the next token
10860          is `template', then we are using the template-id production.  */
10861       if (parser->scope
10862           && cp_parser_optional_template_keyword (parser))
10863         {
10864           /* Look for the template-id.  */
10865           type = cp_parser_template_id (parser,
10866                                         /*template_keyword_p=*/true,
10867                                         /*check_dependency_p=*/true,
10868                                         /*is_declaration=*/false);
10869           /* If the template-id did not name a type, we are out of
10870              luck.  */
10871           if (TREE_CODE (type) != TYPE_DECL)
10872             {
10873               cp_parser_error (parser, "expected template-id for type");
10874               type = NULL_TREE;
10875             }
10876         }
10877       /* Otherwise, look for a type-name.  */
10878       else
10879         type = cp_parser_type_name (parser);
10880       /* Keep track of all name-lookups performed in class scopes.  */
10881       if (type
10882           && !global_p
10883           && !qualified_p
10884           && TREE_CODE (type) == TYPE_DECL
10885           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10886         maybe_note_name_used_in_class (DECL_NAME (type), type);
10887       /* If it didn't work out, we don't have a TYPE.  */
10888       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10889           && !cp_parser_parse_definitely (parser))
10890         type = NULL_TREE;
10891       if (type && decl_specs)
10892         cp_parser_set_decl_spec_type (decl_specs, type,
10893                                       /*user_defined=*/true);
10894     }
10895
10896   /* If we didn't get a type-name, issue an error message.  */
10897   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10898     {
10899       cp_parser_error (parser, "expected type-name");
10900       return error_mark_node;
10901     }
10902
10903   /* There is no valid C++ program where a non-template type is
10904      followed by a "<".  That usually indicates that the user thought
10905      that the type was a template.  */
10906   if (type && type != error_mark_node)
10907     {
10908       /* As a last-ditch effort, see if TYPE is an Objective-C type.
10909          If it is, then the '<'...'>' enclose protocol names rather than
10910          template arguments, and so everything is fine.  */
10911       if (c_dialect_objc ()
10912           && (objc_is_id (type) || objc_is_class_name (type)))
10913         {
10914           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10915           tree qual_type = objc_get_protocol_qualified_type (type, protos);
10916
10917           /* Clobber the "unqualified" type previously entered into
10918              DECL_SPECS with the new, improved protocol-qualified version.  */
10919           if (decl_specs)
10920             decl_specs->type = qual_type;
10921
10922           return qual_type;
10923         }
10924
10925       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10926     }
10927
10928   return type;
10929 }
10930
10931 /* Parse a type-name.
10932
10933    type-name:
10934      class-name
10935      enum-name
10936      typedef-name
10937
10938    enum-name:
10939      identifier
10940
10941    typedef-name:
10942      identifier
10943
10944    Returns a TYPE_DECL for the type.  */
10945
10946 static tree
10947 cp_parser_type_name (cp_parser* parser)
10948 {
10949   tree type_decl;
10950   tree identifier;
10951
10952   /* We can't know yet whether it is a class-name or not.  */
10953   cp_parser_parse_tentatively (parser);
10954   /* Try a class-name.  */
10955   type_decl = cp_parser_class_name (parser,
10956                                     /*typename_keyword_p=*/false,
10957                                     /*template_keyword_p=*/false,
10958                                     none_type,
10959                                     /*check_dependency_p=*/true,
10960                                     /*class_head_p=*/false,
10961                                     /*is_declaration=*/false);
10962   /* If it's not a class-name, keep looking.  */
10963   if (!cp_parser_parse_definitely (parser))
10964     {
10965       /* It must be a typedef-name or an enum-name.  */
10966       identifier = cp_parser_identifier (parser);
10967       if (identifier == error_mark_node)
10968         return error_mark_node;
10969
10970       /* Look up the type-name.  */
10971       type_decl = cp_parser_lookup_name_simple (parser, identifier);
10972
10973       if (TREE_CODE (type_decl) != TYPE_DECL
10974           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10975         {
10976           /* See if this is an Objective-C type.  */
10977           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10978           tree type = objc_get_protocol_qualified_type (identifier, protos);
10979           if (type)
10980             type_decl = TYPE_NAME (type);
10981         }
10982
10983       /* Issue an error if we did not find a type-name.  */
10984       if (TREE_CODE (type_decl) != TYPE_DECL)
10985         {
10986           if (!cp_parser_simulate_error (parser))
10987             cp_parser_name_lookup_error (parser, identifier, type_decl,
10988                                          "is not a type");
10989           type_decl = error_mark_node;
10990         }
10991       /* Remember that the name was used in the definition of the
10992          current class so that we can check later to see if the
10993          meaning would have been different after the class was
10994          entirely defined.  */
10995       else if (type_decl != error_mark_node
10996                && !parser->scope)
10997         maybe_note_name_used_in_class (identifier, type_decl);
10998     }
10999
11000   return type_decl;
11001 }
11002
11003
11004 /* Parse an elaborated-type-specifier.  Note that the grammar given
11005    here incorporates the resolution to DR68.
11006
11007    elaborated-type-specifier:
11008      class-key :: [opt] nested-name-specifier [opt] identifier
11009      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11010      enum :: [opt] nested-name-specifier [opt] identifier
11011      typename :: [opt] nested-name-specifier identifier
11012      typename :: [opt] nested-name-specifier template [opt]
11013        template-id
11014
11015    GNU extension:
11016
11017    elaborated-type-specifier:
11018      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11019      class-key attributes :: [opt] nested-name-specifier [opt]
11020                template [opt] template-id
11021      enum attributes :: [opt] nested-name-specifier [opt] identifier
11022
11023    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11024    declared `friend'.  If IS_DECLARATION is TRUE, then this
11025    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11026    something is being declared.
11027
11028    Returns the TYPE specified.  */
11029
11030 static tree
11031 cp_parser_elaborated_type_specifier (cp_parser* parser,
11032                                      bool is_friend,
11033                                      bool is_declaration)
11034 {
11035   enum tag_types tag_type;
11036   tree identifier;
11037   tree type = NULL_TREE;
11038   tree attributes = NULL_TREE;
11039
11040   /* See if we're looking at the `enum' keyword.  */
11041   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11042     {
11043       /* Consume the `enum' token.  */
11044       cp_lexer_consume_token (parser->lexer);
11045       /* Remember that it's an enumeration type.  */
11046       tag_type = enum_type;
11047       /* Parse the attributes.  */
11048       attributes = cp_parser_attributes_opt (parser);
11049     }
11050   /* Or, it might be `typename'.  */
11051   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11052                                            RID_TYPENAME))
11053     {
11054       /* Consume the `typename' token.  */
11055       cp_lexer_consume_token (parser->lexer);
11056       /* Remember that it's a `typename' type.  */
11057       tag_type = typename_type;
11058       /* The `typename' keyword is only allowed in templates.  */
11059       if (!processing_template_decl)
11060         pedwarn ("using %<typename%> outside of template");
11061     }
11062   /* Otherwise it must be a class-key.  */
11063   else
11064     {
11065       tag_type = cp_parser_class_key (parser);
11066       if (tag_type == none_type)
11067         return error_mark_node;
11068       /* Parse the attributes.  */
11069       attributes = cp_parser_attributes_opt (parser);
11070     }
11071
11072   /* Look for the `::' operator.  */
11073   cp_parser_global_scope_opt (parser,
11074                               /*current_scope_valid_p=*/false);
11075   /* Look for the nested-name-specifier.  */
11076   if (tag_type == typename_type)
11077     {
11078       if (!cp_parser_nested_name_specifier (parser,
11079                                            /*typename_keyword_p=*/true,
11080                                            /*check_dependency_p=*/true,
11081                                            /*type_p=*/true,
11082                                             is_declaration))
11083         return error_mark_node;
11084     }
11085   else
11086     /* Even though `typename' is not present, the proposed resolution
11087        to Core Issue 180 says that in `class A<T>::B', `B' should be
11088        considered a type-name, even if `A<T>' is dependent.  */
11089     cp_parser_nested_name_specifier_opt (parser,
11090                                          /*typename_keyword_p=*/true,
11091                                          /*check_dependency_p=*/true,
11092                                          /*type_p=*/true,
11093                                          is_declaration);
11094  /* For everything but enumeration types, consider a template-id.
11095     For an enumeration type, consider only a plain identifier.  */
11096   if (tag_type != enum_type)
11097     {
11098       bool template_p = false;
11099       tree decl;
11100
11101       /* Allow the `template' keyword.  */
11102       template_p = cp_parser_optional_template_keyword (parser);
11103       /* If we didn't see `template', we don't know if there's a
11104          template-id or not.  */
11105       if (!template_p)
11106         cp_parser_parse_tentatively (parser);
11107       /* Parse the template-id.  */
11108       decl = cp_parser_template_id (parser, template_p,
11109                                     /*check_dependency_p=*/true,
11110                                     is_declaration);
11111       /* If we didn't find a template-id, look for an ordinary
11112          identifier.  */
11113       if (!template_p && !cp_parser_parse_definitely (parser))
11114         ;
11115       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11116          in effect, then we must assume that, upon instantiation, the
11117          template will correspond to a class.  */
11118       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11119                && tag_type == typename_type)
11120         type = make_typename_type (parser->scope, decl,
11121                                    typename_type,
11122                                    /*complain=*/tf_error);
11123       else
11124         type = TREE_TYPE (decl);
11125     }
11126
11127   if (!type)
11128     {
11129       identifier = cp_parser_identifier (parser);
11130
11131       if (identifier == error_mark_node)
11132         {
11133           parser->scope = NULL_TREE;
11134           return error_mark_node;
11135         }
11136
11137       /* For a `typename', we needn't call xref_tag.  */
11138       if (tag_type == typename_type
11139           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11140         return cp_parser_make_typename_type (parser, parser->scope,
11141                                              identifier);
11142       /* Look up a qualified name in the usual way.  */
11143       if (parser->scope)
11144         {
11145           tree decl;
11146           tree ambiguous_decls;
11147
11148           decl = cp_parser_lookup_name (parser, identifier,
11149                                         tag_type,
11150                                         /*is_template=*/false,
11151                                         /*is_namespace=*/false,
11152                                         /*check_dependency=*/true,
11153                                         &ambiguous_decls);
11154
11155           /* If the lookup was ambiguous, an error will already have been
11156              issued.  */
11157           if (ambiguous_decls)
11158             return error_mark_node;
11159
11160           /* If we are parsing friend declaration, DECL may be a
11161              TEMPLATE_DECL tree node here.  However, we need to check
11162              whether this TEMPLATE_DECL results in valid code.  Consider
11163              the following example:
11164
11165                namespace N {
11166                  template <class T> class C {};
11167                }
11168                class X {
11169                  template <class T> friend class N::C; // #1, valid code
11170                };
11171                template <class T> class Y {
11172                  friend class N::C;                    // #2, invalid code
11173                };
11174
11175              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11176              name lookup of `N::C'.  We see that friend declaration must
11177              be template for the code to be valid.  Note that
11178              processing_template_decl does not work here since it is
11179              always 1 for the above two cases.  */
11180
11181           decl = (cp_parser_maybe_treat_template_as_class
11182                   (decl, /*tag_name_p=*/is_friend
11183                          && parser->num_template_parameter_lists));
11184
11185           if (TREE_CODE (decl) != TYPE_DECL)
11186             {
11187               cp_parser_diagnose_invalid_type_name (parser,
11188                                                     parser->scope,
11189                                                     identifier);
11190               return error_mark_node;
11191             }
11192
11193           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11194             {
11195               bool allow_template = (parser->num_template_parameter_lists
11196                                       || DECL_SELF_REFERENCE_P (decl));
11197               type = check_elaborated_type_specifier (tag_type, decl, 
11198                                                       allow_template);
11199
11200               if (type == error_mark_node)
11201                 return error_mark_node;
11202             }
11203
11204           /* Forward declarations of nested types, such as
11205
11206                class C1::C2;
11207                class C1::C2::C3;
11208
11209              are invalid unless all components preceding the final '::'
11210              are complete.  If all enclosing types are complete, these
11211              declarations become merely pointless.
11212
11213              Invalid forward declarations of nested types are errors
11214              caught elsewhere in parsing.  Those that are pointless arrive
11215              here.  */
11216
11217           if (cp_parser_declares_only_class_p (parser)
11218               && !is_friend && !processing_explicit_instantiation)
11219             warning (0, "declaration %qD does not declare anything", decl);
11220
11221           type = TREE_TYPE (decl);
11222         }
11223       else
11224         {
11225           /* An elaborated-type-specifier sometimes introduces a new type and
11226              sometimes names an existing type.  Normally, the rule is that it
11227              introduces a new type only if there is not an existing type of
11228              the same name already in scope.  For example, given:
11229
11230                struct S {};
11231                void f() { struct S s; }
11232
11233              the `struct S' in the body of `f' is the same `struct S' as in
11234              the global scope; the existing definition is used.  However, if
11235              there were no global declaration, this would introduce a new
11236              local class named `S'.
11237
11238              An exception to this rule applies to the following code:
11239
11240                namespace N { struct S; }
11241
11242              Here, the elaborated-type-specifier names a new type
11243              unconditionally; even if there is already an `S' in the
11244              containing scope this declaration names a new type.
11245              This exception only applies if the elaborated-type-specifier
11246              forms the complete declaration:
11247
11248                [class.name]
11249
11250                A declaration consisting solely of `class-key identifier ;' is
11251                either a redeclaration of the name in the current scope or a
11252                forward declaration of the identifier as a class name.  It
11253                introduces the name into the current scope.
11254
11255              We are in this situation precisely when the next token is a `;'.
11256
11257              An exception to the exception is that a `friend' declaration does
11258              *not* name a new type; i.e., given:
11259
11260                struct S { friend struct T; };
11261
11262              `T' is not a new type in the scope of `S'.
11263
11264              Also, `new struct S' or `sizeof (struct S)' never results in the
11265              definition of a new type; a new type can only be declared in a
11266              declaration context.  */
11267
11268           tag_scope ts;
11269           bool template_p;
11270
11271           if (is_friend)
11272             /* Friends have special name lookup rules.  */
11273             ts = ts_within_enclosing_non_class;
11274           else if (is_declaration
11275                    && cp_lexer_next_token_is (parser->lexer,
11276                                               CPP_SEMICOLON))
11277             /* This is a `class-key identifier ;' */
11278             ts = ts_current;
11279           else
11280             ts = ts_global;
11281
11282           template_p =
11283             (parser->num_template_parameter_lists
11284              && (cp_parser_next_token_starts_class_definition_p (parser)
11285                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11286           /* An unqualified name was used to reference this type, so
11287              there were no qualifying templates.  */
11288           if (!cp_parser_check_template_parameters (parser,
11289                                                     /*num_templates=*/0))
11290             return error_mark_node;
11291           type = xref_tag (tag_type, identifier, ts, template_p);
11292         }
11293     }
11294
11295   if (type == error_mark_node)
11296     return error_mark_node;
11297
11298   /* Allow attributes on forward declarations of classes.  */
11299   if (attributes)
11300     {
11301       if (TREE_CODE (type) == TYPENAME_TYPE)
11302         warning (OPT_Wattributes,
11303                  "attributes ignored on uninstantiated type");
11304       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11305                && ! processing_explicit_instantiation)
11306         warning (OPT_Wattributes,
11307                  "attributes ignored on template instantiation");
11308       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11309         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11310       else
11311         warning (OPT_Wattributes,
11312                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11313     }
11314
11315   if (tag_type != enum_type)
11316     cp_parser_check_class_key (tag_type, type);
11317
11318   /* A "<" cannot follow an elaborated type specifier.  If that
11319      happens, the user was probably trying to form a template-id.  */
11320   cp_parser_check_for_invalid_template_id (parser, type);
11321
11322   return type;
11323 }
11324
11325 /* Parse an enum-specifier.
11326
11327    enum-specifier:
11328      enum identifier [opt] { enumerator-list [opt] }
11329
11330    GNU Extensions:
11331      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11332        attributes[opt]
11333
11334    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11335    if the token stream isn't an enum-specifier after all.  */
11336
11337 static tree
11338 cp_parser_enum_specifier (cp_parser* parser)
11339 {
11340   tree identifier;
11341   tree type;
11342   tree attributes;
11343
11344   /* Parse tentatively so that we can back up if we don't find a
11345      enum-specifier.  */
11346   cp_parser_parse_tentatively (parser);
11347
11348   /* Caller guarantees that the current token is 'enum', an identifier
11349      possibly follows, and the token after that is an opening brace.
11350      If we don't have an identifier, fabricate an anonymous name for
11351      the enumeration being defined.  */
11352   cp_lexer_consume_token (parser->lexer);
11353
11354   attributes = cp_parser_attributes_opt (parser);
11355
11356   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11357     identifier = cp_parser_identifier (parser);
11358   else
11359     identifier = make_anon_name ();
11360
11361   /* Look for the `{' but don't consume it yet.  */
11362   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11363     cp_parser_simulate_error (parser);
11364
11365   if (!cp_parser_parse_definitely (parser))
11366     return NULL_TREE;
11367
11368   /* Issue an error message if type-definitions are forbidden here.  */
11369   if (!cp_parser_check_type_definition (parser))
11370     type = error_mark_node;
11371   else
11372     /* Create the new type.  We do this before consuming the opening
11373        brace so the enum will be recorded as being on the line of its
11374        tag (or the 'enum' keyword, if there is no tag).  */
11375     type = start_enum (identifier);
11376   
11377   /* Consume the opening brace.  */
11378   cp_lexer_consume_token (parser->lexer);
11379
11380   if (type == error_mark_node)
11381     {
11382       cp_parser_skip_to_end_of_block_or_statement (parser);
11383       return error_mark_node;
11384     }
11385
11386   /* If the next token is not '}', then there are some enumerators.  */
11387   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11388     cp_parser_enumerator_list (parser, type);
11389
11390   /* Consume the final '}'.  */
11391   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11392
11393   /* Look for trailing attributes to apply to this enumeration, and
11394      apply them if appropriate.  */
11395   if (cp_parser_allow_gnu_extensions_p (parser))
11396     {
11397       tree trailing_attr = cp_parser_attributes_opt (parser);
11398       cplus_decl_attributes (&type,
11399                              trailing_attr,
11400                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11401     }
11402
11403   /* Finish up the enumeration.  */
11404   finish_enum (type);
11405
11406   return type;
11407 }
11408
11409 /* Parse an enumerator-list.  The enumerators all have the indicated
11410    TYPE.
11411
11412    enumerator-list:
11413      enumerator-definition
11414      enumerator-list , enumerator-definition  */
11415
11416 static void
11417 cp_parser_enumerator_list (cp_parser* parser, tree type)
11418 {
11419   while (true)
11420     {
11421       /* Parse an enumerator-definition.  */
11422       cp_parser_enumerator_definition (parser, type);
11423
11424       /* If the next token is not a ',', we've reached the end of
11425          the list.  */
11426       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11427         break;
11428       /* Otherwise, consume the `,' and keep going.  */
11429       cp_lexer_consume_token (parser->lexer);
11430       /* If the next token is a `}', there is a trailing comma.  */
11431       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11432         {
11433           if (pedantic && !in_system_header)
11434             pedwarn ("comma at end of enumerator list");
11435           break;
11436         }
11437     }
11438 }
11439
11440 /* Parse an enumerator-definition.  The enumerator has the indicated
11441    TYPE.
11442
11443    enumerator-definition:
11444      enumerator
11445      enumerator = constant-expression
11446
11447    enumerator:
11448      identifier  */
11449
11450 static void
11451 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11452 {
11453   tree identifier;
11454   tree value;
11455
11456   /* Look for the identifier.  */
11457   identifier = cp_parser_identifier (parser);
11458   if (identifier == error_mark_node)
11459     return;
11460
11461   /* If the next token is an '=', then there is an explicit value.  */
11462   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11463     {
11464       /* Consume the `=' token.  */
11465       cp_lexer_consume_token (parser->lexer);
11466       /* Parse the value.  */
11467       value = cp_parser_constant_expression (parser,
11468                                              /*allow_non_constant_p=*/false,
11469                                              NULL);
11470     }
11471   else
11472     value = NULL_TREE;
11473
11474   /* Create the enumerator.  */
11475   build_enumerator (identifier, value, type);
11476 }
11477
11478 /* Parse a namespace-name.
11479
11480    namespace-name:
11481      original-namespace-name
11482      namespace-alias
11483
11484    Returns the NAMESPACE_DECL for the namespace.  */
11485
11486 static tree
11487 cp_parser_namespace_name (cp_parser* parser)
11488 {
11489   tree identifier;
11490   tree namespace_decl;
11491
11492   /* Get the name of the namespace.  */
11493   identifier = cp_parser_identifier (parser);
11494   if (identifier == error_mark_node)
11495     return error_mark_node;
11496
11497   /* Look up the identifier in the currently active scope.  Look only
11498      for namespaces, due to:
11499
11500        [basic.lookup.udir]
11501
11502        When looking up a namespace-name in a using-directive or alias
11503        definition, only namespace names are considered.
11504
11505      And:
11506
11507        [basic.lookup.qual]
11508
11509        During the lookup of a name preceding the :: scope resolution
11510        operator, object, function, and enumerator names are ignored.
11511
11512      (Note that cp_parser_class_or_namespace_name only calls this
11513      function if the token after the name is the scope resolution
11514      operator.)  */
11515   namespace_decl = cp_parser_lookup_name (parser, identifier,
11516                                           none_type,
11517                                           /*is_template=*/false,
11518                                           /*is_namespace=*/true,
11519                                           /*check_dependency=*/true,
11520                                           /*ambiguous_decls=*/NULL);
11521   /* If it's not a namespace, issue an error.  */
11522   if (namespace_decl == error_mark_node
11523       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11524     {
11525       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11526         error ("%qD is not a namespace-name", identifier);
11527       cp_parser_error (parser, "expected namespace-name");
11528       namespace_decl = error_mark_node;
11529     }
11530
11531   return namespace_decl;
11532 }
11533
11534 /* Parse a namespace-definition.
11535
11536    namespace-definition:
11537      named-namespace-definition
11538      unnamed-namespace-definition
11539
11540    named-namespace-definition:
11541      original-namespace-definition
11542      extension-namespace-definition
11543
11544    original-namespace-definition:
11545      namespace identifier { namespace-body }
11546
11547    extension-namespace-definition:
11548      namespace original-namespace-name { namespace-body }
11549
11550    unnamed-namespace-definition:
11551      namespace { namespace-body } */
11552
11553 static void
11554 cp_parser_namespace_definition (cp_parser* parser)
11555 {
11556   tree identifier, attribs;
11557   bool has_visibility;
11558   bool is_inline;
11559
11560   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11561     {
11562       is_inline = true;
11563       cp_lexer_consume_token (parser->lexer);
11564     }
11565   else
11566     is_inline = false;
11567
11568   /* Look for the `namespace' keyword.  */
11569   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11570
11571   /* Get the name of the namespace.  We do not attempt to distinguish
11572      between an original-namespace-definition and an
11573      extension-namespace-definition at this point.  The semantic
11574      analysis routines are responsible for that.  */
11575   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11576     identifier = cp_parser_identifier (parser);
11577   else
11578     identifier = NULL_TREE;
11579
11580   /* Parse any specified attributes.  */
11581   attribs = cp_parser_attributes_opt (parser);
11582
11583   /* Look for the `{' to start the namespace.  */
11584   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11585   /* Start the namespace.  */
11586   push_namespace (identifier);
11587
11588   /* "inline namespace" is equivalent to a stub namespace definition
11589      followed by a strong using directive.  */
11590   if (is_inline)
11591     {
11592       tree namespace = current_namespace;
11593       /* Set up namespace association.  */
11594       DECL_NAMESPACE_ASSOCIATIONS (namespace)
11595         = tree_cons (CP_DECL_CONTEXT (namespace), NULL_TREE,
11596                      DECL_NAMESPACE_ASSOCIATIONS (namespace));
11597       /* Import the contents of the inline namespace.  */
11598       pop_namespace ();
11599       do_using_directive (namespace);
11600       push_namespace (identifier);
11601     }
11602
11603   has_visibility = handle_namespace_attrs (current_namespace, attribs);
11604
11605   /* Parse the body of the namespace.  */
11606   cp_parser_namespace_body (parser);
11607
11608 #ifdef HANDLE_PRAGMA_VISIBILITY
11609   if (has_visibility)
11610     pop_visibility ();
11611 #endif
11612
11613   /* Finish the namespace.  */
11614   pop_namespace ();
11615   /* Look for the final `}'.  */
11616   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11617 }
11618
11619 /* Parse a namespace-body.
11620
11621    namespace-body:
11622      declaration-seq [opt]  */
11623
11624 static void
11625 cp_parser_namespace_body (cp_parser* parser)
11626 {
11627   cp_parser_declaration_seq_opt (parser);
11628 }
11629
11630 /* Parse a namespace-alias-definition.
11631
11632    namespace-alias-definition:
11633      namespace identifier = qualified-namespace-specifier ;  */
11634
11635 static void
11636 cp_parser_namespace_alias_definition (cp_parser* parser)
11637 {
11638   tree identifier;
11639   tree namespace_specifier;
11640
11641   /* Look for the `namespace' keyword.  */
11642   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11643   /* Look for the identifier.  */
11644   identifier = cp_parser_identifier (parser);
11645   if (identifier == error_mark_node)
11646     return;
11647   /* Look for the `=' token.  */
11648   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11649       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
11650     {
11651       error ("%<namespace%> definition is not allowed here");
11652       /* Skip the definition.  */
11653       cp_lexer_consume_token (parser->lexer);
11654       if (cp_parser_skip_to_closing_brace (parser))
11655         cp_lexer_consume_token (parser->lexer);
11656       return;
11657     }
11658   cp_parser_require (parser, CPP_EQ, "`='");
11659   /* Look for the qualified-namespace-specifier.  */
11660   namespace_specifier
11661     = cp_parser_qualified_namespace_specifier (parser);
11662   /* Look for the `;' token.  */
11663   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11664
11665   /* Register the alias in the symbol table.  */
11666   do_namespace_alias (identifier, namespace_specifier);
11667 }
11668
11669 /* Parse a qualified-namespace-specifier.
11670
11671    qualified-namespace-specifier:
11672      :: [opt] nested-name-specifier [opt] namespace-name
11673
11674    Returns a NAMESPACE_DECL corresponding to the specified
11675    namespace.  */
11676
11677 static tree
11678 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11679 {
11680   /* Look for the optional `::'.  */
11681   cp_parser_global_scope_opt (parser,
11682                               /*current_scope_valid_p=*/false);
11683
11684   /* Look for the optional nested-name-specifier.  */
11685   cp_parser_nested_name_specifier_opt (parser,
11686                                        /*typename_keyword_p=*/false,
11687                                        /*check_dependency_p=*/true,
11688                                        /*type_p=*/false,
11689                                        /*is_declaration=*/true);
11690
11691   return cp_parser_namespace_name (parser);
11692 }
11693
11694 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11695    access declaration.
11696
11697    using-declaration:
11698      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11699      using :: unqualified-id ;  
11700
11701    access-declaration:
11702      qualified-id ;  
11703
11704    */
11705
11706 static bool
11707 cp_parser_using_declaration (cp_parser* parser, 
11708                              bool access_declaration_p)
11709 {
11710   cp_token *token;
11711   bool typename_p = false;
11712   bool global_scope_p;
11713   tree decl;
11714   tree identifier;
11715   tree qscope;
11716
11717   if (access_declaration_p)
11718     cp_parser_parse_tentatively (parser);
11719   else
11720     {
11721       /* Look for the `using' keyword.  */
11722       cp_parser_require_keyword (parser, RID_USING, "`using'");
11723       
11724       /* Peek at the next token.  */
11725       token = cp_lexer_peek_token (parser->lexer);
11726       /* See if it's `typename'.  */
11727       if (token->keyword == RID_TYPENAME)
11728         {
11729           /* Remember that we've seen it.  */
11730           typename_p = true;
11731           /* Consume the `typename' token.  */
11732           cp_lexer_consume_token (parser->lexer);
11733         }
11734     }
11735
11736   /* Look for the optional global scope qualification.  */
11737   global_scope_p
11738     = (cp_parser_global_scope_opt (parser,
11739                                    /*current_scope_valid_p=*/false)
11740        != NULL_TREE);
11741
11742   /* If we saw `typename', or didn't see `::', then there must be a
11743      nested-name-specifier present.  */
11744   if (typename_p || !global_scope_p)
11745     qscope = cp_parser_nested_name_specifier (parser, typename_p,
11746                                               /*check_dependency_p=*/true,
11747                                               /*type_p=*/false,
11748                                               /*is_declaration=*/true);
11749   /* Otherwise, we could be in either of the two productions.  In that
11750      case, treat the nested-name-specifier as optional.  */
11751   else
11752     qscope = cp_parser_nested_name_specifier_opt (parser,
11753                                                   /*typename_keyword_p=*/false,
11754                                                   /*check_dependency_p=*/true,
11755                                                   /*type_p=*/false,
11756                                                   /*is_declaration=*/true);
11757   if (!qscope)
11758     qscope = global_namespace;
11759
11760   if (access_declaration_p && cp_parser_error_occurred (parser))
11761     /* Something has already gone wrong; there's no need to parse
11762        further.  Since an error has occurred, the return value of
11763        cp_parser_parse_definitely will be false, as required.  */
11764     return cp_parser_parse_definitely (parser);
11765
11766   /* Parse the unqualified-id.  */
11767   identifier = cp_parser_unqualified_id (parser,
11768                                          /*template_keyword_p=*/false,
11769                                          /*check_dependency_p=*/true,
11770                                          /*declarator_p=*/true,
11771                                          /*optional_p=*/false);
11772
11773   if (access_declaration_p)
11774     {
11775       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11776         cp_parser_simulate_error (parser);
11777       if (!cp_parser_parse_definitely (parser))
11778         return false;
11779     }
11780
11781   /* The function we call to handle a using-declaration is different
11782      depending on what scope we are in.  */
11783   if (qscope == error_mark_node || identifier == error_mark_node)
11784     ;
11785   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11786            && TREE_CODE (identifier) != BIT_NOT_EXPR)
11787     /* [namespace.udecl]
11788
11789        A using declaration shall not name a template-id.  */
11790     error ("a template-id may not appear in a using-declaration");
11791   else
11792     {
11793       if (at_class_scope_p ())
11794         {
11795           /* Create the USING_DECL.  */
11796           decl = do_class_using_decl (parser->scope, identifier);
11797
11798           if (check_for_bare_parameter_packs (decl))
11799             return false;
11800           else
11801             /* Add it to the list of members in this class.  */
11802             finish_member_declaration (decl);
11803         }
11804       else
11805         {
11806           decl = cp_parser_lookup_name_simple (parser, identifier);
11807           if (decl == error_mark_node)
11808             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11809           else if (check_for_bare_parameter_packs (decl))
11810             return false;
11811           else if (!at_namespace_scope_p ())
11812             do_local_using_decl (decl, qscope, identifier);
11813           else
11814             do_toplevel_using_decl (decl, qscope, identifier);
11815         }
11816     }
11817
11818   /* Look for the final `;'.  */
11819   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11820   
11821   return true;
11822 }
11823
11824 /* Parse a using-directive.
11825
11826    using-directive:
11827      using namespace :: [opt] nested-name-specifier [opt]
11828        namespace-name ;  */
11829
11830 static void
11831 cp_parser_using_directive (cp_parser* parser)
11832 {
11833   tree namespace_decl;
11834   tree attribs;
11835
11836   /* Look for the `using' keyword.  */
11837   cp_parser_require_keyword (parser, RID_USING, "`using'");
11838   /* And the `namespace' keyword.  */
11839   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11840   /* Look for the optional `::' operator.  */
11841   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11842   /* And the optional nested-name-specifier.  */
11843   cp_parser_nested_name_specifier_opt (parser,
11844                                        /*typename_keyword_p=*/false,
11845                                        /*check_dependency_p=*/true,
11846                                        /*type_p=*/false,
11847                                        /*is_declaration=*/true);
11848   /* Get the namespace being used.  */
11849   namespace_decl = cp_parser_namespace_name (parser);
11850   /* And any specified attributes.  */
11851   attribs = cp_parser_attributes_opt (parser);
11852   /* Update the symbol table.  */
11853   parse_using_directive (namespace_decl, attribs);
11854   /* Look for the final `;'.  */
11855   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11856 }
11857
11858 /* Parse an asm-definition.
11859
11860    asm-definition:
11861      asm ( string-literal ) ;
11862
11863    GNU Extension:
11864
11865    asm-definition:
11866      asm volatile [opt] ( string-literal ) ;
11867      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11868      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11869                           : asm-operand-list [opt] ) ;
11870      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11871                           : asm-operand-list [opt]
11872                           : asm-operand-list [opt] ) ;  */
11873
11874 static void
11875 cp_parser_asm_definition (cp_parser* parser)
11876 {
11877   tree string;
11878   tree outputs = NULL_TREE;
11879   tree inputs = NULL_TREE;
11880   tree clobbers = NULL_TREE;
11881   tree asm_stmt;
11882   bool volatile_p = false;
11883   bool extended_p = false;
11884   bool invalid_inputs_p = false;
11885   bool invalid_outputs_p = false;
11886
11887   /* Look for the `asm' keyword.  */
11888   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11889   /* See if the next token is `volatile'.  */
11890   if (cp_parser_allow_gnu_extensions_p (parser)
11891       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11892     {
11893       /* Remember that we saw the `volatile' keyword.  */
11894       volatile_p = true;
11895       /* Consume the token.  */
11896       cp_lexer_consume_token (parser->lexer);
11897     }
11898   /* Look for the opening `('.  */
11899   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11900     return;
11901   /* Look for the string.  */
11902   string = cp_parser_string_literal (parser, false, false);
11903   if (string == error_mark_node)
11904     {
11905       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11906                                              /*consume_paren=*/true);
11907       return;
11908     }
11909
11910   /* If we're allowing GNU extensions, check for the extended assembly
11911      syntax.  Unfortunately, the `:' tokens need not be separated by
11912      a space in C, and so, for compatibility, we tolerate that here
11913      too.  Doing that means that we have to treat the `::' operator as
11914      two `:' tokens.  */
11915   if (cp_parser_allow_gnu_extensions_p (parser)
11916       && parser->in_function_body
11917       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11918           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11919     {
11920       bool inputs_p = false;
11921       bool clobbers_p = false;
11922
11923       /* The extended syntax was used.  */
11924       extended_p = true;
11925
11926       /* Look for outputs.  */
11927       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11928         {
11929           /* Consume the `:'.  */
11930           cp_lexer_consume_token (parser->lexer);
11931           /* Parse the output-operands.  */
11932           if (cp_lexer_next_token_is_not (parser->lexer,
11933                                           CPP_COLON)
11934               && cp_lexer_next_token_is_not (parser->lexer,
11935                                              CPP_SCOPE)
11936               && cp_lexer_next_token_is_not (parser->lexer,
11937                                              CPP_CLOSE_PAREN))
11938             outputs = cp_parser_asm_operand_list (parser);
11939
11940             if (outputs == error_mark_node)
11941               invalid_outputs_p = true;
11942         }
11943       /* If the next token is `::', there are no outputs, and the
11944          next token is the beginning of the inputs.  */
11945       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11946         /* The inputs are coming next.  */
11947         inputs_p = true;
11948
11949       /* Look for inputs.  */
11950       if (inputs_p
11951           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11952         {
11953           /* Consume the `:' or `::'.  */
11954           cp_lexer_consume_token (parser->lexer);
11955           /* Parse the output-operands.  */
11956           if (cp_lexer_next_token_is_not (parser->lexer,
11957                                           CPP_COLON)
11958               && cp_lexer_next_token_is_not (parser->lexer,
11959                                              CPP_CLOSE_PAREN))
11960             inputs = cp_parser_asm_operand_list (parser);
11961
11962             if (inputs == error_mark_node)
11963               invalid_inputs_p = true;
11964         }
11965       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11966         /* The clobbers are coming next.  */
11967         clobbers_p = true;
11968
11969       /* Look for clobbers.  */
11970       if (clobbers_p
11971           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11972         {
11973           /* Consume the `:' or `::'.  */
11974           cp_lexer_consume_token (parser->lexer);
11975           /* Parse the clobbers.  */
11976           if (cp_lexer_next_token_is_not (parser->lexer,
11977                                           CPP_CLOSE_PAREN))
11978             clobbers = cp_parser_asm_clobber_list (parser);
11979         }
11980     }
11981   /* Look for the closing `)'.  */
11982   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11983     cp_parser_skip_to_closing_parenthesis (parser, true, false,
11984                                            /*consume_paren=*/true);
11985   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11986
11987   if (!invalid_inputs_p && !invalid_outputs_p)
11988     {
11989       /* Create the ASM_EXPR.  */
11990       if (parser->in_function_body)
11991         {
11992           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11993                                       inputs, clobbers);
11994           /* If the extended syntax was not used, mark the ASM_EXPR.  */
11995           if (!extended_p)
11996             {
11997               tree temp = asm_stmt;
11998               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11999                 temp = TREE_OPERAND (temp, 0);
12000
12001               ASM_INPUT_P (temp) = 1;
12002             }
12003         }
12004       else
12005         cgraph_add_asm_node (string);
12006     }
12007 }
12008
12009 /* Declarators [gram.dcl.decl] */
12010
12011 /* Parse an init-declarator.
12012
12013    init-declarator:
12014      declarator initializer [opt]
12015
12016    GNU Extension:
12017
12018    init-declarator:
12019      declarator asm-specification [opt] attributes [opt] initializer [opt]
12020
12021    function-definition:
12022      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12023        function-body
12024      decl-specifier-seq [opt] declarator function-try-block
12025
12026    GNU Extension:
12027
12028    function-definition:
12029      __extension__ function-definition
12030
12031    The DECL_SPECIFIERS apply to this declarator.  Returns a
12032    representation of the entity declared.  If MEMBER_P is TRUE, then
12033    this declarator appears in a class scope.  The new DECL created by
12034    this declarator is returned.
12035
12036    The CHECKS are access checks that should be performed once we know
12037    what entity is being declared (and, therefore, what classes have
12038    befriended it).
12039
12040    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12041    for a function-definition here as well.  If the declarator is a
12042    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12043    be TRUE upon return.  By that point, the function-definition will
12044    have been completely parsed.
12045
12046    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12047    is FALSE.  */
12048
12049 static tree
12050 cp_parser_init_declarator (cp_parser* parser,
12051                            cp_decl_specifier_seq *decl_specifiers,
12052                            VEC (deferred_access_check,gc)* checks,
12053                            bool function_definition_allowed_p,
12054                            bool member_p,
12055                            int declares_class_or_enum,
12056                            bool* function_definition_p)
12057 {
12058   cp_token *token;
12059   cp_declarator *declarator;
12060   tree prefix_attributes;
12061   tree attributes;
12062   tree asm_specification;
12063   tree initializer;
12064   tree decl = NULL_TREE;
12065   tree scope;
12066   bool is_initialized;
12067   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12068      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12069      "(...)".  */
12070   enum cpp_ttype initialization_kind;
12071   bool is_parenthesized_init = false;
12072   bool is_non_constant_init;
12073   int ctor_dtor_or_conv_p;
12074   bool friend_p;
12075   tree pushed_scope = NULL;
12076
12077   /* Gather the attributes that were provided with the
12078      decl-specifiers.  */
12079   prefix_attributes = decl_specifiers->attributes;
12080
12081   /* Assume that this is not the declarator for a function
12082      definition.  */
12083   if (function_definition_p)
12084     *function_definition_p = false;
12085
12086   /* Defer access checks while parsing the declarator; we cannot know
12087      what names are accessible until we know what is being
12088      declared.  */
12089   resume_deferring_access_checks ();
12090
12091   /* Parse the declarator.  */
12092   declarator
12093     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12094                             &ctor_dtor_or_conv_p,
12095                             /*parenthesized_p=*/NULL,
12096                             /*member_p=*/false);
12097   /* Gather up the deferred checks.  */
12098   stop_deferring_access_checks ();
12099
12100   /* If the DECLARATOR was erroneous, there's no need to go
12101      further.  */
12102   if (declarator == cp_error_declarator)
12103     return error_mark_node;
12104
12105   /* Check that the number of template-parameter-lists is OK.  */
12106   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
12107     return error_mark_node;
12108
12109   if (declares_class_or_enum & 2)
12110     cp_parser_check_for_definition_in_return_type (declarator,
12111                                                    decl_specifiers->type);
12112
12113   /* Figure out what scope the entity declared by the DECLARATOR is
12114      located in.  `grokdeclarator' sometimes changes the scope, so
12115      we compute it now.  */
12116   scope = get_scope_of_declarator (declarator);
12117
12118   /* If we're allowing GNU extensions, look for an asm-specification
12119      and attributes.  */
12120   if (cp_parser_allow_gnu_extensions_p (parser))
12121     {
12122       /* Look for an asm-specification.  */
12123       asm_specification = cp_parser_asm_specification_opt (parser);
12124       /* And attributes.  */
12125       attributes = cp_parser_attributes_opt (parser);
12126     }
12127   else
12128     {
12129       asm_specification = NULL_TREE;
12130       attributes = NULL_TREE;
12131     }
12132
12133   /* Peek at the next token.  */
12134   token = cp_lexer_peek_token (parser->lexer);
12135   /* Check to see if the token indicates the start of a
12136      function-definition.  */
12137   if (cp_parser_token_starts_function_definition_p (token))
12138     {
12139       if (!function_definition_allowed_p)
12140         {
12141           /* If a function-definition should not appear here, issue an
12142              error message.  */
12143           cp_parser_error (parser,
12144                            "a function-definition is not allowed here");
12145           return error_mark_node;
12146         }
12147       else
12148         {
12149           /* Neither attributes nor an asm-specification are allowed
12150              on a function-definition.  */
12151           if (asm_specification)
12152             error ("an asm-specification is not allowed on a function-definition");
12153           if (attributes)
12154             error ("attributes are not allowed on a function-definition");
12155           /* This is a function-definition.  */
12156           *function_definition_p = true;
12157
12158           /* Parse the function definition.  */
12159           if (member_p)
12160             decl = cp_parser_save_member_function_body (parser,
12161                                                         decl_specifiers,
12162                                                         declarator,
12163                                                         prefix_attributes);
12164           else
12165             decl
12166               = (cp_parser_function_definition_from_specifiers_and_declarator
12167                  (parser, decl_specifiers, prefix_attributes, declarator));
12168
12169           return decl;
12170         }
12171     }
12172
12173   /* [dcl.dcl]
12174
12175      Only in function declarations for constructors, destructors, and
12176      type conversions can the decl-specifier-seq be omitted.
12177
12178      We explicitly postpone this check past the point where we handle
12179      function-definitions because we tolerate function-definitions
12180      that are missing their return types in some modes.  */
12181   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12182     {
12183       cp_parser_error (parser,
12184                        "expected constructor, destructor, or type conversion");
12185       return error_mark_node;
12186     }
12187
12188   /* An `=' or an `(' indicates an initializer.  */
12189   if (token->type == CPP_EQ
12190       || token->type == CPP_OPEN_PAREN)
12191     {
12192       is_initialized = true;
12193       initialization_kind = token->type;
12194     }
12195   else
12196     {
12197       /* If the init-declarator isn't initialized and isn't followed by a
12198          `,' or `;', it's not a valid init-declarator.  */
12199       if (token->type != CPP_COMMA
12200           && token->type != CPP_SEMICOLON)
12201         {
12202           cp_parser_error (parser, "expected initializer");
12203           return error_mark_node;
12204         }
12205       is_initialized = false;
12206       initialization_kind = CPP_EOF;
12207     }
12208
12209   /* Because start_decl has side-effects, we should only call it if we
12210      know we're going ahead.  By this point, we know that we cannot
12211      possibly be looking at any other construct.  */
12212   cp_parser_commit_to_tentative_parse (parser);
12213
12214   /* If the decl specifiers were bad, issue an error now that we're
12215      sure this was intended to be a declarator.  Then continue
12216      declaring the variable(s), as int, to try to cut down on further
12217      errors.  */
12218   if (decl_specifiers->any_specifiers_p
12219       && decl_specifiers->type == error_mark_node)
12220     {
12221       cp_parser_error (parser, "invalid type in declaration");
12222       decl_specifiers->type = integer_type_node;
12223     }
12224
12225   /* Check to see whether or not this declaration is a friend.  */
12226   friend_p = cp_parser_friend_p (decl_specifiers);
12227
12228   /* Enter the newly declared entry in the symbol table.  If we're
12229      processing a declaration in a class-specifier, we wait until
12230      after processing the initializer.  */
12231   if (!member_p)
12232     {
12233       if (parser->in_unbraced_linkage_specification_p)
12234         decl_specifiers->storage_class = sc_extern;
12235       decl = start_decl (declarator, decl_specifiers,
12236                          is_initialized, attributes, prefix_attributes,
12237                          &pushed_scope);
12238     }
12239   else if (scope)
12240     /* Enter the SCOPE.  That way unqualified names appearing in the
12241        initializer will be looked up in SCOPE.  */
12242     pushed_scope = push_scope (scope);
12243
12244   /* Perform deferred access control checks, now that we know in which
12245      SCOPE the declared entity resides.  */
12246   if (!member_p && decl)
12247     {
12248       tree saved_current_function_decl = NULL_TREE;
12249
12250       /* If the entity being declared is a function, pretend that we
12251          are in its scope.  If it is a `friend', it may have access to
12252          things that would not otherwise be accessible.  */
12253       if (TREE_CODE (decl) == FUNCTION_DECL)
12254         {
12255           saved_current_function_decl = current_function_decl;
12256           current_function_decl = decl;
12257         }
12258
12259       /* Perform access checks for template parameters.  */
12260       cp_parser_perform_template_parameter_access_checks (checks);
12261
12262       /* Perform the access control checks for the declarator and the
12263          the decl-specifiers.  */
12264       perform_deferred_access_checks ();
12265
12266       /* Restore the saved value.  */
12267       if (TREE_CODE (decl) == FUNCTION_DECL)
12268         current_function_decl = saved_current_function_decl;
12269     }
12270
12271   /* Parse the initializer.  */
12272   initializer = NULL_TREE;
12273   is_parenthesized_init = false;
12274   is_non_constant_init = true;
12275   if (is_initialized)
12276     {
12277       if (function_declarator_p (declarator))
12278         {
12279            if (initialization_kind == CPP_EQ)
12280              initializer = cp_parser_pure_specifier (parser);
12281            else
12282              {
12283                /* If the declaration was erroneous, we don't really
12284                   know what the user intended, so just silently
12285                   consume the initializer.  */
12286                if (decl != error_mark_node)
12287                  error ("initializer provided for function");
12288                cp_parser_skip_to_closing_parenthesis (parser,
12289                                                       /*recovering=*/true,
12290                                                       /*or_comma=*/false,
12291                                                       /*consume_paren=*/true);
12292              }
12293         }
12294       else
12295         initializer = cp_parser_initializer (parser,
12296                                              &is_parenthesized_init,
12297                                              &is_non_constant_init);
12298     }
12299
12300   /* The old parser allows attributes to appear after a parenthesized
12301      initializer.  Mark Mitchell proposed removing this functionality
12302      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12303      attributes -- but ignores them.  */
12304   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
12305     if (cp_parser_attributes_opt (parser))
12306       warning (OPT_Wattributes,
12307                "attributes after parenthesized initializer ignored");
12308
12309   /* For an in-class declaration, use `grokfield' to create the
12310      declaration.  */
12311   if (member_p)
12312     {
12313       if (pushed_scope)
12314         {
12315           pop_scope (pushed_scope);
12316           pushed_scope = false;
12317         }
12318       decl = grokfield (declarator, decl_specifiers,
12319                         initializer, !is_non_constant_init,
12320                         /*asmspec=*/NULL_TREE,
12321                         prefix_attributes);
12322       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12323         cp_parser_save_default_args (parser, decl);
12324     }
12325
12326   /* Finish processing the declaration.  But, skip friend
12327      declarations.  */
12328   if (!friend_p && decl && decl != error_mark_node)
12329     {
12330       cp_finish_decl (decl,
12331                       initializer, !is_non_constant_init,
12332                       asm_specification,
12333                       /* If the initializer is in parentheses, then this is
12334                          a direct-initialization, which means that an
12335                          `explicit' constructor is OK.  Otherwise, an
12336                          `explicit' constructor cannot be used.  */
12337                       ((is_parenthesized_init || !is_initialized)
12338                      ? 0 : LOOKUP_ONLYCONVERTING));
12339     }
12340   else if ((cxx_dialect != cxx98) && friend_p
12341            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12342     /* Core issue #226 (C++0x only): A default template-argument
12343        shall not be specified in a friend class template
12344        declaration. */
12345     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12346                              /*is_partial=*/0, /*is_friend_decl=*/1);
12347
12348   if (!friend_p && pushed_scope)
12349     pop_scope (pushed_scope);
12350
12351   return decl;
12352 }
12353
12354 /* Parse a declarator.
12355
12356    declarator:
12357      direct-declarator
12358      ptr-operator declarator
12359
12360    abstract-declarator:
12361      ptr-operator abstract-declarator [opt]
12362      direct-abstract-declarator
12363
12364    GNU Extensions:
12365
12366    declarator:
12367      attributes [opt] direct-declarator
12368      attributes [opt] ptr-operator declarator
12369
12370    abstract-declarator:
12371      attributes [opt] ptr-operator abstract-declarator [opt]
12372      attributes [opt] direct-abstract-declarator
12373
12374    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12375    detect constructor, destructor or conversion operators. It is set
12376    to -1 if the declarator is a name, and +1 if it is a
12377    function. Otherwise it is set to zero. Usually you just want to
12378    test for >0, but internally the negative value is used.
12379
12380    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12381    a decl-specifier-seq unless it declares a constructor, destructor,
12382    or conversion.  It might seem that we could check this condition in
12383    semantic analysis, rather than parsing, but that makes it difficult
12384    to handle something like `f()'.  We want to notice that there are
12385    no decl-specifiers, and therefore realize that this is an
12386    expression, not a declaration.)
12387
12388    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12389    the declarator is a direct-declarator of the form "(...)".
12390
12391    MEMBER_P is true iff this declarator is a member-declarator.  */
12392
12393 static cp_declarator *
12394 cp_parser_declarator (cp_parser* parser,
12395                       cp_parser_declarator_kind dcl_kind,
12396                       int* ctor_dtor_or_conv_p,
12397                       bool* parenthesized_p,
12398                       bool member_p)
12399 {
12400   cp_token *token;
12401   cp_declarator *declarator;
12402   enum tree_code code;
12403   cp_cv_quals cv_quals;
12404   tree class_type;
12405   tree attributes = NULL_TREE;
12406
12407   /* Assume this is not a constructor, destructor, or type-conversion
12408      operator.  */
12409   if (ctor_dtor_or_conv_p)
12410     *ctor_dtor_or_conv_p = 0;
12411
12412   if (cp_parser_allow_gnu_extensions_p (parser))
12413     attributes = cp_parser_attributes_opt (parser);
12414
12415   /* Peek at the next token.  */
12416   token = cp_lexer_peek_token (parser->lexer);
12417
12418   /* Check for the ptr-operator production.  */
12419   cp_parser_parse_tentatively (parser);
12420   /* Parse the ptr-operator.  */
12421   code = cp_parser_ptr_operator (parser,
12422                                  &class_type,
12423                                  &cv_quals);
12424   /* If that worked, then we have a ptr-operator.  */
12425   if (cp_parser_parse_definitely (parser))
12426     {
12427       /* If a ptr-operator was found, then this declarator was not
12428          parenthesized.  */
12429       if (parenthesized_p)
12430         *parenthesized_p = true;
12431       /* The dependent declarator is optional if we are parsing an
12432          abstract-declarator.  */
12433       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12434         cp_parser_parse_tentatively (parser);
12435
12436       /* Parse the dependent declarator.  */
12437       declarator = cp_parser_declarator (parser, dcl_kind,
12438                                          /*ctor_dtor_or_conv_p=*/NULL,
12439                                          /*parenthesized_p=*/NULL,
12440                                          /*member_p=*/false);
12441
12442       /* If we are parsing an abstract-declarator, we must handle the
12443          case where the dependent declarator is absent.  */
12444       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12445           && !cp_parser_parse_definitely (parser))
12446         declarator = NULL;
12447
12448       declarator = cp_parser_make_indirect_declarator
12449         (code, class_type, cv_quals, declarator);
12450     }
12451   /* Everything else is a direct-declarator.  */
12452   else
12453     {
12454       if (parenthesized_p)
12455         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12456                                                    CPP_OPEN_PAREN);
12457       declarator = cp_parser_direct_declarator (parser, dcl_kind,
12458                                                 ctor_dtor_or_conv_p,
12459                                                 member_p);
12460     }
12461
12462   if (attributes && declarator && declarator != cp_error_declarator)
12463     declarator->attributes = attributes;
12464
12465   return declarator;
12466 }
12467
12468 /* Parse a direct-declarator or direct-abstract-declarator.
12469
12470    direct-declarator:
12471      declarator-id
12472      direct-declarator ( parameter-declaration-clause )
12473        cv-qualifier-seq [opt]
12474        exception-specification [opt]
12475      direct-declarator [ constant-expression [opt] ]
12476      ( declarator )
12477
12478    direct-abstract-declarator:
12479      direct-abstract-declarator [opt]
12480        ( parameter-declaration-clause )
12481        cv-qualifier-seq [opt]
12482        exception-specification [opt]
12483      direct-abstract-declarator [opt] [ constant-expression [opt] ]
12484      ( abstract-declarator )
12485
12486    Returns a representation of the declarator.  DCL_KIND is
12487    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12488    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
12489    we are parsing a direct-declarator.  It is
12490    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12491    of ambiguity we prefer an abstract declarator, as per
12492    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12493    cp_parser_declarator.  */
12494
12495 static cp_declarator *
12496 cp_parser_direct_declarator (cp_parser* parser,
12497                              cp_parser_declarator_kind dcl_kind,
12498                              int* ctor_dtor_or_conv_p,
12499                              bool member_p)
12500 {
12501   cp_token *token;
12502   cp_declarator *declarator = NULL;
12503   tree scope = NULL_TREE;
12504   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12505   bool saved_in_declarator_p = parser->in_declarator_p;
12506   bool first = true;
12507   tree pushed_scope = NULL_TREE;
12508
12509   while (true)
12510     {
12511       /* Peek at the next token.  */
12512       token = cp_lexer_peek_token (parser->lexer);
12513       if (token->type == CPP_OPEN_PAREN)
12514         {
12515           /* This is either a parameter-declaration-clause, or a
12516              parenthesized declarator. When we know we are parsing a
12517              named declarator, it must be a parenthesized declarator
12518              if FIRST is true. For instance, `(int)' is a
12519              parameter-declaration-clause, with an omitted
12520              direct-abstract-declarator. But `((*))', is a
12521              parenthesized abstract declarator. Finally, when T is a
12522              template parameter `(T)' is a
12523              parameter-declaration-clause, and not a parenthesized
12524              named declarator.
12525
12526              We first try and parse a parameter-declaration-clause,
12527              and then try a nested declarator (if FIRST is true).
12528
12529              It is not an error for it not to be a
12530              parameter-declaration-clause, even when FIRST is
12531              false. Consider,
12532
12533                int i (int);
12534                int i (3);
12535
12536              The first is the declaration of a function while the
12537              second is a the definition of a variable, including its
12538              initializer.
12539
12540              Having seen only the parenthesis, we cannot know which of
12541              these two alternatives should be selected.  Even more
12542              complex are examples like:
12543
12544                int i (int (a));
12545                int i (int (3));
12546
12547              The former is a function-declaration; the latter is a
12548              variable initialization.
12549
12550              Thus again, we try a parameter-declaration-clause, and if
12551              that fails, we back out and return.  */
12552
12553           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12554             {
12555               cp_parameter_declarator *params;
12556               unsigned saved_num_template_parameter_lists;
12557
12558               /* In a member-declarator, the only valid interpretation
12559                  of a parenthesis is the start of a
12560                  parameter-declaration-clause.  (It is invalid to
12561                  initialize a static data member with a parenthesized
12562                  initializer; only the "=" form of initialization is
12563                  permitted.)  */
12564               if (!member_p)
12565                 cp_parser_parse_tentatively (parser);
12566
12567               /* Consume the `('.  */
12568               cp_lexer_consume_token (parser->lexer);
12569               if (first)
12570                 {
12571                   /* If this is going to be an abstract declarator, we're
12572                      in a declarator and we can't have default args.  */
12573                   parser->default_arg_ok_p = false;
12574                   parser->in_declarator_p = true;
12575                 }
12576
12577               /* Inside the function parameter list, surrounding
12578                  template-parameter-lists do not apply.  */
12579               saved_num_template_parameter_lists
12580                 = parser->num_template_parameter_lists;
12581               parser->num_template_parameter_lists = 0;
12582
12583               /* Parse the parameter-declaration-clause.  */
12584               params = cp_parser_parameter_declaration_clause (parser);
12585
12586               parser->num_template_parameter_lists
12587                 = saved_num_template_parameter_lists;
12588
12589               /* If all went well, parse the cv-qualifier-seq and the
12590                  exception-specification.  */
12591               if (member_p || cp_parser_parse_definitely (parser))
12592                 {
12593                   cp_cv_quals cv_quals;
12594                   tree exception_specification;
12595
12596                   if (ctor_dtor_or_conv_p)
12597                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12598                   first = false;
12599                   /* Consume the `)'.  */
12600                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12601
12602                   /* Parse the cv-qualifier-seq.  */
12603                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12604                   /* And the exception-specification.  */
12605                   exception_specification
12606                     = cp_parser_exception_specification_opt (parser);
12607
12608                   /* Create the function-declarator.  */
12609                   declarator = make_call_declarator (declarator,
12610                                                      params,
12611                                                      cv_quals,
12612                                                      exception_specification);
12613                   /* Any subsequent parameter lists are to do with
12614                      return type, so are not those of the declared
12615                      function.  */
12616                   parser->default_arg_ok_p = false;
12617
12618                   /* Repeat the main loop.  */
12619                   continue;
12620                 }
12621             }
12622
12623           /* If this is the first, we can try a parenthesized
12624              declarator.  */
12625           if (first)
12626             {
12627               bool saved_in_type_id_in_expr_p;
12628
12629               parser->default_arg_ok_p = saved_default_arg_ok_p;
12630               parser->in_declarator_p = saved_in_declarator_p;
12631
12632               /* Consume the `('.  */
12633               cp_lexer_consume_token (parser->lexer);
12634               /* Parse the nested declarator.  */
12635               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12636               parser->in_type_id_in_expr_p = true;
12637               declarator
12638                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12639                                         /*parenthesized_p=*/NULL,
12640                                         member_p);
12641               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12642               first = false;
12643               /* Expect a `)'.  */
12644               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12645                 declarator = cp_error_declarator;
12646               if (declarator == cp_error_declarator)
12647                 break;
12648
12649               goto handle_declarator;
12650             }
12651           /* Otherwise, we must be done.  */
12652           else
12653             break;
12654         }
12655       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12656                && token->type == CPP_OPEN_SQUARE)
12657         {
12658           /* Parse an array-declarator.  */
12659           tree bounds;
12660
12661           if (ctor_dtor_or_conv_p)
12662             *ctor_dtor_or_conv_p = 0;
12663
12664           first = false;
12665           parser->default_arg_ok_p = false;
12666           parser->in_declarator_p = true;
12667           /* Consume the `['.  */
12668           cp_lexer_consume_token (parser->lexer);
12669           /* Peek at the next token.  */
12670           token = cp_lexer_peek_token (parser->lexer);
12671           /* If the next token is `]', then there is no
12672              constant-expression.  */
12673           if (token->type != CPP_CLOSE_SQUARE)
12674             {
12675               bool non_constant_p;
12676
12677               bounds
12678                 = cp_parser_constant_expression (parser,
12679                                                  /*allow_non_constant=*/true,
12680                                                  &non_constant_p);
12681               if (!non_constant_p)
12682                 bounds = fold_non_dependent_expr (bounds);
12683               /* Normally, the array bound must be an integral constant
12684                  expression.  However, as an extension, we allow VLAs
12685                  in function scopes.  */
12686               else if (!parser->in_function_body)
12687                 {
12688                   error ("array bound is not an integer constant");
12689                   bounds = error_mark_node;
12690                 }
12691             }
12692           else
12693             bounds = NULL_TREE;
12694           /* Look for the closing `]'.  */
12695           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12696             {
12697               declarator = cp_error_declarator;
12698               break;
12699             }
12700
12701           declarator = make_array_declarator (declarator, bounds);
12702         }
12703       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12704         {
12705           tree qualifying_scope;
12706           tree unqualified_name;
12707           special_function_kind sfk;
12708           bool abstract_ok;
12709           bool pack_expansion_p = false;
12710
12711           /* Parse a declarator-id */
12712           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12713           if (abstract_ok)
12714             {
12715               cp_parser_parse_tentatively (parser);
12716
12717               /* If we see an ellipsis, we should be looking at a
12718                  parameter pack. */
12719               if (token->type == CPP_ELLIPSIS)
12720                 {
12721                   /* Consume the `...' */
12722                   cp_lexer_consume_token (parser->lexer);
12723
12724                   pack_expansion_p = true;
12725                 }
12726             }
12727
12728           unqualified_name
12729             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12730           qualifying_scope = parser->scope;
12731           if (abstract_ok)
12732             {
12733               bool okay = false;
12734
12735               if (!unqualified_name && pack_expansion_p)
12736                 {
12737                   /* Check whether an error occurred. */
12738                   okay = !cp_parser_error_occurred (parser);
12739
12740                   /* We already consumed the ellipsis to mark a
12741                      parameter pack, but we have no way to report it,
12742                      so abort the tentative parse. We will be exiting
12743                      immediately anyway. */
12744                   cp_parser_abort_tentative_parse (parser);
12745                 }
12746               else
12747                 okay = cp_parser_parse_definitely (parser);
12748
12749               if (!okay)
12750                 unqualified_name = error_mark_node;
12751               else if (unqualified_name
12752                        && (qualifying_scope
12753                            || (TREE_CODE (unqualified_name)
12754                                != IDENTIFIER_NODE)))
12755                 {
12756                   cp_parser_error (parser, "expected unqualified-id");
12757                   unqualified_name = error_mark_node;
12758                 }
12759             }
12760
12761           if (!unqualified_name)
12762             return NULL;
12763           if (unqualified_name == error_mark_node)
12764             {
12765               declarator = cp_error_declarator;
12766               pack_expansion_p = false;
12767               declarator->parameter_pack_p = false;
12768               break;
12769             }
12770
12771           if (qualifying_scope && at_namespace_scope_p ()
12772               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12773             {
12774               /* In the declaration of a member of a template class
12775                  outside of the class itself, the SCOPE will sometimes
12776                  be a TYPENAME_TYPE.  For example, given:
12777
12778                  template <typename T>
12779                  int S<T>::R::i = 3;
12780
12781                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
12782                  this context, we must resolve S<T>::R to an ordinary
12783                  type, rather than a typename type.
12784
12785                  The reason we normally avoid resolving TYPENAME_TYPEs
12786                  is that a specialization of `S' might render
12787                  `S<T>::R' not a type.  However, if `S' is
12788                  specialized, then this `i' will not be used, so there
12789                  is no harm in resolving the types here.  */
12790               tree type;
12791
12792               /* Resolve the TYPENAME_TYPE.  */
12793               type = resolve_typename_type (qualifying_scope,
12794                                             /*only_current_p=*/false);
12795               /* If that failed, the declarator is invalid.  */
12796               if (TREE_CODE (type) == TYPENAME_TYPE)
12797                 error ("%<%T::%E%> is not a type",
12798                        TYPE_CONTEXT (qualifying_scope),
12799                        TYPE_IDENTIFIER (qualifying_scope));
12800               qualifying_scope = type;
12801             }
12802
12803           sfk = sfk_none;
12804
12805           if (unqualified_name)
12806             {
12807               tree class_type;
12808
12809               if (qualifying_scope
12810                   && CLASS_TYPE_P (qualifying_scope))
12811                 class_type = qualifying_scope;
12812               else
12813                 class_type = current_class_type;
12814
12815               if (TREE_CODE (unqualified_name) == TYPE_DECL)
12816                 {
12817                   tree name_type = TREE_TYPE (unqualified_name);
12818                   if (class_type && same_type_p (name_type, class_type))
12819                     {
12820                       if (qualifying_scope
12821                           && CLASSTYPE_USE_TEMPLATE (name_type))
12822                         {
12823                           error ("invalid use of constructor as a template");
12824                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12825                                   "name the constructor in a qualified name",
12826                                   class_type,
12827                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12828                                   class_type, name_type);
12829                           declarator = cp_error_declarator;
12830                           break;
12831                         }
12832                       else
12833                         unqualified_name = constructor_name (class_type);
12834                     }
12835                   else
12836                     {
12837                       /* We do not attempt to print the declarator
12838                          here because we do not have enough
12839                          information about its original syntactic
12840                          form.  */
12841                       cp_parser_error (parser, "invalid declarator");
12842                       declarator = cp_error_declarator;
12843                       break;
12844                     }
12845                 }
12846
12847               if (class_type)
12848                 {
12849                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12850                     sfk = sfk_destructor;
12851                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12852                     sfk = sfk_conversion;
12853                   else if (/* There's no way to declare a constructor
12854                               for an anonymous type, even if the type
12855                               got a name for linkage purposes.  */
12856                            !TYPE_WAS_ANONYMOUS (class_type)
12857                            && constructor_name_p (unqualified_name,
12858                                                   class_type))
12859                     {
12860                       unqualified_name = constructor_name (class_type);
12861                       sfk = sfk_constructor;
12862                     }
12863
12864                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
12865                     *ctor_dtor_or_conv_p = -1;
12866                 }
12867             }
12868           declarator = make_id_declarator (qualifying_scope,
12869                                            unqualified_name,
12870                                            sfk);
12871           declarator->id_loc = token->location;
12872           declarator->parameter_pack_p = pack_expansion_p;
12873
12874           if (pack_expansion_p)
12875             maybe_warn_variadic_templates ();
12876
12877         handle_declarator:;
12878           scope = get_scope_of_declarator (declarator);
12879           if (scope)
12880             /* Any names that appear after the declarator-id for a
12881                member are looked up in the containing scope.  */
12882             pushed_scope = push_scope (scope);
12883           parser->in_declarator_p = true;
12884           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12885               || (declarator && declarator->kind == cdk_id))
12886             /* Default args are only allowed on function
12887                declarations.  */
12888             parser->default_arg_ok_p = saved_default_arg_ok_p;
12889           else
12890             parser->default_arg_ok_p = false;
12891
12892           first = false;
12893         }
12894       /* We're done.  */
12895       else
12896         break;
12897     }
12898
12899   /* For an abstract declarator, we might wind up with nothing at this
12900      point.  That's an error; the declarator is not optional.  */
12901   if (!declarator)
12902     cp_parser_error (parser, "expected declarator");
12903
12904   /* If we entered a scope, we must exit it now.  */
12905   if (pushed_scope)
12906     pop_scope (pushed_scope);
12907
12908   parser->default_arg_ok_p = saved_default_arg_ok_p;
12909   parser->in_declarator_p = saved_in_declarator_p;
12910
12911   return declarator;
12912 }
12913
12914 /* Parse a ptr-operator.
12915
12916    ptr-operator:
12917      * cv-qualifier-seq [opt]
12918      &
12919      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12920
12921    GNU Extension:
12922
12923    ptr-operator:
12924      & cv-qualifier-seq [opt]
12925
12926    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12927    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
12928    an rvalue reference. In the case of a pointer-to-member, *TYPE is
12929    filled in with the TYPE containing the member.  *CV_QUALS is
12930    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
12931    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
12932    Note that the tree codes returned by this function have nothing
12933    to do with the types of trees that will be eventually be created
12934    to represent the pointer or reference type being parsed. They are
12935    just constants with suggestive names. */
12936 static enum tree_code
12937 cp_parser_ptr_operator (cp_parser* parser,
12938                         tree* type,
12939                         cp_cv_quals *cv_quals)
12940 {
12941   enum tree_code code = ERROR_MARK;
12942   cp_token *token;
12943
12944   /* Assume that it's not a pointer-to-member.  */
12945   *type = NULL_TREE;
12946   /* And that there are no cv-qualifiers.  */
12947   *cv_quals = TYPE_UNQUALIFIED;
12948
12949   /* Peek at the next token.  */
12950   token = cp_lexer_peek_token (parser->lexer);
12951
12952   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
12953   if (token->type == CPP_MULT)
12954     code = INDIRECT_REF;
12955   else if (token->type == CPP_AND)
12956     code = ADDR_EXPR;
12957   else if ((cxx_dialect != cxx98) &&
12958            token->type == CPP_AND_AND) /* C++0x only */
12959     code = NON_LVALUE_EXPR;
12960
12961   if (code != ERROR_MARK)
12962     {
12963       /* Consume the `*', `&' or `&&'.  */
12964       cp_lexer_consume_token (parser->lexer);
12965
12966       /* A `*' can be followed by a cv-qualifier-seq, and so can a
12967          `&', if we are allowing GNU extensions.  (The only qualifier
12968          that can legally appear after `&' is `restrict', but that is
12969          enforced during semantic analysis.  */
12970       if (code == INDIRECT_REF
12971           || cp_parser_allow_gnu_extensions_p (parser))
12972         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12973     }
12974   else
12975     {
12976       /* Try the pointer-to-member case.  */
12977       cp_parser_parse_tentatively (parser);
12978       /* Look for the optional `::' operator.  */
12979       cp_parser_global_scope_opt (parser,
12980                                   /*current_scope_valid_p=*/false);
12981       /* Look for the nested-name specifier.  */
12982       cp_parser_nested_name_specifier (parser,
12983                                        /*typename_keyword_p=*/false,
12984                                        /*check_dependency_p=*/true,
12985                                        /*type_p=*/false,
12986                                        /*is_declaration=*/false);
12987       /* If we found it, and the next token is a `*', then we are
12988          indeed looking at a pointer-to-member operator.  */
12989       if (!cp_parser_error_occurred (parser)
12990           && cp_parser_require (parser, CPP_MULT, "`*'"))
12991         {
12992           /* Indicate that the `*' operator was used.  */
12993           code = INDIRECT_REF;
12994
12995           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12996             error ("%qD is a namespace", parser->scope);
12997           else
12998             {
12999               /* The type of which the member is a member is given by the
13000                  current SCOPE.  */
13001               *type = parser->scope;
13002               /* The next name will not be qualified.  */
13003               parser->scope = NULL_TREE;
13004               parser->qualifying_scope = NULL_TREE;
13005               parser->object_scope = NULL_TREE;
13006               /* Look for the optional cv-qualifier-seq.  */
13007               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13008             }
13009         }
13010       /* If that didn't work we don't have a ptr-operator.  */
13011       if (!cp_parser_parse_definitely (parser))
13012         cp_parser_error (parser, "expected ptr-operator");
13013     }
13014
13015   return code;
13016 }
13017
13018 /* Parse an (optional) cv-qualifier-seq.
13019
13020    cv-qualifier-seq:
13021      cv-qualifier cv-qualifier-seq [opt]
13022
13023    cv-qualifier:
13024      const
13025      volatile
13026
13027    GNU Extension:
13028
13029    cv-qualifier:
13030      __restrict__
13031
13032    Returns a bitmask representing the cv-qualifiers.  */
13033
13034 static cp_cv_quals
13035 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13036 {
13037   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13038
13039   while (true)
13040     {
13041       cp_token *token;
13042       cp_cv_quals cv_qualifier;
13043
13044       /* Peek at the next token.  */
13045       token = cp_lexer_peek_token (parser->lexer);
13046       /* See if it's a cv-qualifier.  */
13047       switch (token->keyword)
13048         {
13049         case RID_CONST:
13050           cv_qualifier = TYPE_QUAL_CONST;
13051           break;
13052
13053         case RID_VOLATILE:
13054           cv_qualifier = TYPE_QUAL_VOLATILE;
13055           break;
13056
13057         case RID_RESTRICT:
13058           cv_qualifier = TYPE_QUAL_RESTRICT;
13059           break;
13060
13061         default:
13062           cv_qualifier = TYPE_UNQUALIFIED;
13063           break;
13064         }
13065
13066       if (!cv_qualifier)
13067         break;
13068
13069       if (cv_quals & cv_qualifier)
13070         {
13071           error ("duplicate cv-qualifier");
13072           cp_lexer_purge_token (parser->lexer);
13073         }
13074       else
13075         {
13076           cp_lexer_consume_token (parser->lexer);
13077           cv_quals |= cv_qualifier;
13078         }
13079     }
13080
13081   return cv_quals;
13082 }
13083
13084 /* Parse a declarator-id.
13085
13086    declarator-id:
13087      id-expression
13088      :: [opt] nested-name-specifier [opt] type-name
13089
13090    In the `id-expression' case, the value returned is as for
13091    cp_parser_id_expression if the id-expression was an unqualified-id.
13092    If the id-expression was a qualified-id, then a SCOPE_REF is
13093    returned.  The first operand is the scope (either a NAMESPACE_DECL
13094    or TREE_TYPE), but the second is still just a representation of an
13095    unqualified-id.  */
13096
13097 static tree
13098 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13099 {
13100   tree id;
13101   /* The expression must be an id-expression.  Assume that qualified
13102      names are the names of types so that:
13103
13104        template <class T>
13105        int S<T>::R::i = 3;
13106
13107      will work; we must treat `S<T>::R' as the name of a type.
13108      Similarly, assume that qualified names are templates, where
13109      required, so that:
13110
13111        template <class T>
13112        int S<T>::R<T>::i = 3;
13113
13114      will work, too.  */
13115   id = cp_parser_id_expression (parser,
13116                                 /*template_keyword_p=*/false,
13117                                 /*check_dependency_p=*/false,
13118                                 /*template_p=*/NULL,
13119                                 /*declarator_p=*/true,
13120                                 optional_p);
13121   if (id && BASELINK_P (id))
13122     id = BASELINK_FUNCTIONS (id);
13123   return id;
13124 }
13125
13126 /* Parse a type-id.
13127
13128    type-id:
13129      type-specifier-seq abstract-declarator [opt]
13130
13131    Returns the TYPE specified.  */
13132
13133 static tree
13134 cp_parser_type_id (cp_parser* parser)
13135 {
13136   cp_decl_specifier_seq type_specifier_seq;
13137   cp_declarator *abstract_declarator;
13138
13139   /* Parse the type-specifier-seq.  */
13140   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13141                                 &type_specifier_seq);
13142   if (type_specifier_seq.type == error_mark_node)
13143     return error_mark_node;
13144
13145   /* There might or might not be an abstract declarator.  */
13146   cp_parser_parse_tentatively (parser);
13147   /* Look for the declarator.  */
13148   abstract_declarator
13149     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13150                             /*parenthesized_p=*/NULL,
13151                             /*member_p=*/false);
13152   /* Check to see if there really was a declarator.  */
13153   if (!cp_parser_parse_definitely (parser))
13154     abstract_declarator = NULL;
13155
13156   return groktypename (&type_specifier_seq, abstract_declarator);
13157 }
13158
13159 /* Parse a type-specifier-seq.
13160
13161    type-specifier-seq:
13162      type-specifier type-specifier-seq [opt]
13163
13164    GNU extension:
13165
13166    type-specifier-seq:
13167      attributes type-specifier-seq [opt]
13168
13169    If IS_CONDITION is true, we are at the start of a "condition",
13170    e.g., we've just seen "if (".
13171
13172    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13173
13174 static void
13175 cp_parser_type_specifier_seq (cp_parser* parser,
13176                               bool is_condition,
13177                               cp_decl_specifier_seq *type_specifier_seq)
13178 {
13179   bool seen_type_specifier = false;
13180   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13181
13182   /* Clear the TYPE_SPECIFIER_SEQ.  */
13183   clear_decl_specs (type_specifier_seq);
13184
13185   /* Parse the type-specifiers and attributes.  */
13186   while (true)
13187     {
13188       tree type_specifier;
13189       bool is_cv_qualifier;
13190
13191       /* Check for attributes first.  */
13192       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13193         {
13194           type_specifier_seq->attributes =
13195             chainon (type_specifier_seq->attributes,
13196                      cp_parser_attributes_opt (parser));
13197           continue;
13198         }
13199
13200       /* Look for the type-specifier.  */
13201       type_specifier = cp_parser_type_specifier (parser,
13202                                                  flags,
13203                                                  type_specifier_seq,
13204                                                  /*is_declaration=*/false,
13205                                                  NULL,
13206                                                  &is_cv_qualifier);
13207       if (!type_specifier)
13208         {
13209           /* If the first type-specifier could not be found, this is not a
13210              type-specifier-seq at all.  */
13211           if (!seen_type_specifier)
13212             {
13213               cp_parser_error (parser, "expected type-specifier");
13214               type_specifier_seq->type = error_mark_node;
13215               return;
13216             }
13217           /* If subsequent type-specifiers could not be found, the
13218              type-specifier-seq is complete.  */
13219           break;
13220         }
13221
13222       seen_type_specifier = true;
13223       /* The standard says that a condition can be:
13224
13225             type-specifier-seq declarator = assignment-expression
13226
13227          However, given:
13228
13229            struct S {};
13230            if (int S = ...)
13231
13232          we should treat the "S" as a declarator, not as a
13233          type-specifier.  The standard doesn't say that explicitly for
13234          type-specifier-seq, but it does say that for
13235          decl-specifier-seq in an ordinary declaration.  Perhaps it
13236          would be clearer just to allow a decl-specifier-seq here, and
13237          then add a semantic restriction that if any decl-specifiers
13238          that are not type-specifiers appear, the program is invalid.  */
13239       if (is_condition && !is_cv_qualifier)
13240         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13241     }
13242
13243   cp_parser_check_decl_spec (type_specifier_seq);
13244 }
13245
13246 /* Parse a parameter-declaration-clause.
13247
13248    parameter-declaration-clause:
13249      parameter-declaration-list [opt] ... [opt]
13250      parameter-declaration-list , ...
13251
13252    Returns a representation for the parameter declarations.  A return
13253    value of NULL indicates a parameter-declaration-clause consisting
13254    only of an ellipsis.  */
13255
13256 static cp_parameter_declarator *
13257 cp_parser_parameter_declaration_clause (cp_parser* parser)
13258 {
13259   cp_parameter_declarator *parameters;
13260   cp_token *token;
13261   bool ellipsis_p;
13262   bool is_error;
13263
13264   /* Peek at the next token.  */
13265   token = cp_lexer_peek_token (parser->lexer);
13266   /* Check for trivial parameter-declaration-clauses.  */
13267   if (token->type == CPP_ELLIPSIS)
13268     {
13269       /* Consume the `...' token.  */
13270       cp_lexer_consume_token (parser->lexer);
13271       return NULL;
13272     }
13273   else if (token->type == CPP_CLOSE_PAREN)
13274     /* There are no parameters.  */
13275     {
13276 #ifndef NO_IMPLICIT_EXTERN_C
13277       if (in_system_header && current_class_type == NULL
13278           && current_lang_name == lang_name_c)
13279         return NULL;
13280       else
13281 #endif
13282         return no_parameters;
13283     }
13284   /* Check for `(void)', too, which is a special case.  */
13285   else if (token->keyword == RID_VOID
13286            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13287                == CPP_CLOSE_PAREN))
13288     {
13289       /* Consume the `void' token.  */
13290       cp_lexer_consume_token (parser->lexer);
13291       /* There are no parameters.  */
13292       return no_parameters;
13293     }
13294
13295   /* Parse the parameter-declaration-list.  */
13296   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13297   /* If a parse error occurred while parsing the
13298      parameter-declaration-list, then the entire
13299      parameter-declaration-clause is erroneous.  */
13300   if (is_error)
13301     return NULL;
13302
13303   /* Peek at the next token.  */
13304   token = cp_lexer_peek_token (parser->lexer);
13305   /* If it's a `,', the clause should terminate with an ellipsis.  */
13306   if (token->type == CPP_COMMA)
13307     {
13308       /* Consume the `,'.  */
13309       cp_lexer_consume_token (parser->lexer);
13310       /* Expect an ellipsis.  */
13311       ellipsis_p
13312         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
13313     }
13314   /* It might also be `...' if the optional trailing `,' was
13315      omitted.  */
13316   else if (token->type == CPP_ELLIPSIS)
13317     {
13318       /* Consume the `...' token.  */
13319       cp_lexer_consume_token (parser->lexer);
13320       /* And remember that we saw it.  */
13321       ellipsis_p = true;
13322     }
13323   else
13324     ellipsis_p = false;
13325
13326   /* Finish the parameter list.  */
13327   if (parameters && ellipsis_p)
13328     parameters->ellipsis_p = true;
13329
13330   return parameters;
13331 }
13332
13333 /* Parse a parameter-declaration-list.
13334
13335    parameter-declaration-list:
13336      parameter-declaration
13337      parameter-declaration-list , parameter-declaration
13338
13339    Returns a representation of the parameter-declaration-list, as for
13340    cp_parser_parameter_declaration_clause.  However, the
13341    `void_list_node' is never appended to the list.  Upon return,
13342    *IS_ERROR will be true iff an error occurred.  */
13343
13344 static cp_parameter_declarator *
13345 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13346 {
13347   cp_parameter_declarator *parameters = NULL;
13348   cp_parameter_declarator **tail = &parameters;
13349   bool saved_in_unbraced_linkage_specification_p;
13350
13351   /* Assume all will go well.  */
13352   *is_error = false;
13353   /* The special considerations that apply to a function within an
13354      unbraced linkage specifications do not apply to the parameters
13355      to the function.  */
13356   saved_in_unbraced_linkage_specification_p 
13357     = parser->in_unbraced_linkage_specification_p;
13358   parser->in_unbraced_linkage_specification_p = false;
13359
13360   /* Look for more parameters.  */
13361   while (true)
13362     {
13363       cp_parameter_declarator *parameter;
13364       bool parenthesized_p;
13365       /* Parse the parameter.  */
13366       parameter
13367         = cp_parser_parameter_declaration (parser,
13368                                            /*template_parm_p=*/false,
13369                                            &parenthesized_p);
13370
13371       /* If a parse error occurred parsing the parameter declaration,
13372          then the entire parameter-declaration-list is erroneous.  */
13373       if (!parameter)
13374         {
13375           *is_error = true;
13376           parameters = NULL;
13377           break;
13378         }
13379       /* Add the new parameter to the list.  */
13380       *tail = parameter;
13381       tail = &parameter->next;
13382
13383       /* Peek at the next token.  */
13384       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13385           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13386           /* These are for Objective-C++ */
13387           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13388           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13389         /* The parameter-declaration-list is complete.  */
13390         break;
13391       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13392         {
13393           cp_token *token;
13394
13395           /* Peek at the next token.  */
13396           token = cp_lexer_peek_nth_token (parser->lexer, 2);
13397           /* If it's an ellipsis, then the list is complete.  */
13398           if (token->type == CPP_ELLIPSIS)
13399             break;
13400           /* Otherwise, there must be more parameters.  Consume the
13401              `,'.  */
13402           cp_lexer_consume_token (parser->lexer);
13403           /* When parsing something like:
13404
13405                 int i(float f, double d)
13406
13407              we can tell after seeing the declaration for "f" that we
13408              are not looking at an initialization of a variable "i",
13409              but rather at the declaration of a function "i".
13410
13411              Due to the fact that the parsing of template arguments
13412              (as specified to a template-id) requires backtracking we
13413              cannot use this technique when inside a template argument
13414              list.  */
13415           if (!parser->in_template_argument_list_p
13416               && !parser->in_type_id_in_expr_p
13417               && cp_parser_uncommitted_to_tentative_parse_p (parser)
13418               /* However, a parameter-declaration of the form
13419                  "foat(f)" (which is a valid declaration of a
13420                  parameter "f") can also be interpreted as an
13421                  expression (the conversion of "f" to "float").  */
13422               && !parenthesized_p)
13423             cp_parser_commit_to_tentative_parse (parser);
13424         }
13425       else
13426         {
13427           cp_parser_error (parser, "expected %<,%> or %<...%>");
13428           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13429             cp_parser_skip_to_closing_parenthesis (parser,
13430                                                    /*recovering=*/true,
13431                                                    /*or_comma=*/false,
13432                                                    /*consume_paren=*/false);
13433           break;
13434         }
13435     }
13436
13437   parser->in_unbraced_linkage_specification_p
13438     = saved_in_unbraced_linkage_specification_p;
13439
13440   return parameters;
13441 }
13442
13443 /* Parse a parameter declaration.
13444
13445    parameter-declaration:
13446      decl-specifier-seq ... [opt] declarator
13447      decl-specifier-seq declarator = assignment-expression
13448      decl-specifier-seq ... [opt] abstract-declarator [opt]
13449      decl-specifier-seq abstract-declarator [opt] = assignment-expression
13450
13451    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13452    declares a template parameter.  (In that case, a non-nested `>'
13453    token encountered during the parsing of the assignment-expression
13454    is not interpreted as a greater-than operator.)
13455
13456    Returns a representation of the parameter, or NULL if an error
13457    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13458    true iff the declarator is of the form "(p)".  */
13459
13460 static cp_parameter_declarator *
13461 cp_parser_parameter_declaration (cp_parser *parser,
13462                                  bool template_parm_p,
13463                                  bool *parenthesized_p)
13464 {
13465   int declares_class_or_enum;
13466   bool greater_than_is_operator_p;
13467   cp_decl_specifier_seq decl_specifiers;
13468   cp_declarator *declarator;
13469   tree default_argument;
13470   cp_token *token;
13471   const char *saved_message;
13472
13473   /* In a template parameter, `>' is not an operator.
13474
13475      [temp.param]
13476
13477      When parsing a default template-argument for a non-type
13478      template-parameter, the first non-nested `>' is taken as the end
13479      of the template parameter-list rather than a greater-than
13480      operator.  */
13481   greater_than_is_operator_p = !template_parm_p;
13482
13483   /* Type definitions may not appear in parameter types.  */
13484   saved_message = parser->type_definition_forbidden_message;
13485   parser->type_definition_forbidden_message
13486     = "types may not be defined in parameter types";
13487
13488   /* Parse the declaration-specifiers.  */
13489   cp_parser_decl_specifier_seq (parser,
13490                                 CP_PARSER_FLAGS_NONE,
13491                                 &decl_specifiers,
13492                                 &declares_class_or_enum);
13493   /* If an error occurred, there's no reason to attempt to parse the
13494      rest of the declaration.  */
13495   if (cp_parser_error_occurred (parser))
13496     {
13497       parser->type_definition_forbidden_message = saved_message;
13498       return NULL;
13499     }
13500
13501   /* Peek at the next token.  */
13502   token = cp_lexer_peek_token (parser->lexer);
13503
13504   /* If the next token is a `)', `,', `=', `>', or `...', then there
13505      is no declarator. However, when variadic templates are enabled,
13506      there may be a declarator following `...'.  */
13507   if (token->type == CPP_CLOSE_PAREN
13508       || token->type == CPP_COMMA
13509       || token->type == CPP_EQ
13510       || token->type == CPP_GREATER)
13511     {
13512       declarator = NULL;
13513       if (parenthesized_p)
13514         *parenthesized_p = false;
13515     }
13516   /* Otherwise, there should be a declarator.  */
13517   else
13518     {
13519       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13520       parser->default_arg_ok_p = false;
13521
13522       /* After seeing a decl-specifier-seq, if the next token is not a
13523          "(", there is no possibility that the code is a valid
13524          expression.  Therefore, if parsing tentatively, we commit at
13525          this point.  */
13526       if (!parser->in_template_argument_list_p
13527           /* In an expression context, having seen:
13528
13529                (int((char ...
13530
13531              we cannot be sure whether we are looking at a
13532              function-type (taking a "char" as a parameter) or a cast
13533              of some object of type "char" to "int".  */
13534           && !parser->in_type_id_in_expr_p
13535           && cp_parser_uncommitted_to_tentative_parse_p (parser)
13536           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13537         cp_parser_commit_to_tentative_parse (parser);
13538       /* Parse the declarator.  */
13539       declarator = cp_parser_declarator (parser,
13540                                          CP_PARSER_DECLARATOR_EITHER,
13541                                          /*ctor_dtor_or_conv_p=*/NULL,
13542                                          parenthesized_p,
13543                                          /*member_p=*/false);
13544       parser->default_arg_ok_p = saved_default_arg_ok_p;
13545       /* After the declarator, allow more attributes.  */
13546       decl_specifiers.attributes
13547         = chainon (decl_specifiers.attributes,
13548                    cp_parser_attributes_opt (parser));
13549     }
13550
13551   /* If the next token is an ellipsis, and we have not seen a
13552      declarator name, and the type of the declarator contains parameter
13553      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13554      a parameter pack expansion expression. Otherwise, leave the
13555      ellipsis for a C-style variadic function. */
13556   token = cp_lexer_peek_token (parser->lexer);
13557   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13558     {
13559       tree type = decl_specifiers.type;
13560
13561       if (type && DECL_P (type))
13562         type = TREE_TYPE (type);
13563
13564       if (type
13565           && TREE_CODE (type) != TYPE_PACK_EXPANSION
13566           && declarator_can_be_parameter_pack (declarator)
13567           && (!declarator || !declarator->parameter_pack_p)
13568           && uses_parameter_packs (type))
13569         {
13570           /* Consume the `...'. */
13571           cp_lexer_consume_token (parser->lexer);
13572           maybe_warn_variadic_templates ();
13573           
13574           /* Build a pack expansion type */
13575           if (declarator)
13576             declarator->parameter_pack_p = true;
13577           else
13578             decl_specifiers.type = make_pack_expansion (type);
13579         }
13580     }
13581
13582   /* The restriction on defining new types applies only to the type
13583      of the parameter, not to the default argument.  */
13584   parser->type_definition_forbidden_message = saved_message;
13585
13586   /* If the next token is `=', then process a default argument.  */
13587   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13588     {
13589       /* Consume the `='.  */
13590       cp_lexer_consume_token (parser->lexer);
13591
13592       /* If we are defining a class, then the tokens that make up the
13593          default argument must be saved and processed later.  */
13594       if (!template_parm_p && at_class_scope_p ()
13595           && TYPE_BEING_DEFINED (current_class_type))
13596         {
13597           unsigned depth = 0;
13598           cp_token *first_token;
13599           cp_token *token;
13600
13601           /* Add tokens until we have processed the entire default
13602              argument.  We add the range [first_token, token).  */
13603           first_token = cp_lexer_peek_token (parser->lexer);
13604           while (true)
13605             {
13606               bool done = false;
13607
13608               /* Peek at the next token.  */
13609               token = cp_lexer_peek_token (parser->lexer);
13610               /* What we do depends on what token we have.  */
13611               switch (token->type)
13612                 {
13613                   /* In valid code, a default argument must be
13614                      immediately followed by a `,' `)', or `...'.  */
13615                 case CPP_COMMA:
13616                 case CPP_CLOSE_PAREN:
13617                 case CPP_ELLIPSIS:
13618                   /* If we run into a non-nested `;', `}', or `]',
13619                      then the code is invalid -- but the default
13620                      argument is certainly over.  */
13621                 case CPP_SEMICOLON:
13622                 case CPP_CLOSE_BRACE:
13623                 case CPP_CLOSE_SQUARE:
13624                   if (depth == 0)
13625                     done = true;
13626                   /* Update DEPTH, if necessary.  */
13627                   else if (token->type == CPP_CLOSE_PAREN
13628                            || token->type == CPP_CLOSE_BRACE
13629                            || token->type == CPP_CLOSE_SQUARE)
13630                     --depth;
13631                   break;
13632
13633                 case CPP_OPEN_PAREN:
13634                 case CPP_OPEN_SQUARE:
13635                 case CPP_OPEN_BRACE:
13636                   ++depth;
13637                   break;
13638
13639                 case CPP_RSHIFT:
13640                   if (cxx_dialect == cxx98)
13641                     break;
13642                   /* Fall through for C++0x, which treats the `>>'
13643                      operator like two `>' tokens in certain
13644                      cases.  */
13645
13646                 case CPP_GREATER:
13647                   /* If we see a non-nested `>', and `>' is not an
13648                      operator, then it marks the end of the default
13649                      argument.  */
13650                   if (!depth && !greater_than_is_operator_p)
13651                     done = true;
13652                   break;
13653
13654                   /* If we run out of tokens, issue an error message.  */
13655                 case CPP_EOF:
13656                 case CPP_PRAGMA_EOL:
13657                   error ("file ends in default argument");
13658                   done = true;
13659                   break;
13660
13661                 case CPP_NAME:
13662                 case CPP_SCOPE:
13663                   /* In these cases, we should look for template-ids.
13664                      For example, if the default argument is
13665                      `X<int, double>()', we need to do name lookup to
13666                      figure out whether or not `X' is a template; if
13667                      so, the `,' does not end the default argument.
13668
13669                      That is not yet done.  */
13670                   break;
13671
13672                 default:
13673                   break;
13674                 }
13675
13676               /* If we've reached the end, stop.  */
13677               if (done)
13678                 break;
13679
13680               /* Add the token to the token block.  */
13681               token = cp_lexer_consume_token (parser->lexer);
13682             }
13683
13684           /* Create a DEFAULT_ARG to represent the unparsed default
13685              argument.  */
13686           default_argument = make_node (DEFAULT_ARG);
13687           DEFARG_TOKENS (default_argument)
13688             = cp_token_cache_new (first_token, token);
13689           DEFARG_INSTANTIATIONS (default_argument) = NULL;
13690         }
13691       /* Outside of a class definition, we can just parse the
13692          assignment-expression.  */
13693       else
13694         default_argument 
13695           = cp_parser_default_argument (parser, template_parm_p);
13696
13697       if (!parser->default_arg_ok_p)
13698         {
13699           if (!flag_pedantic_errors)
13700             warning (0, "deprecated use of default argument for parameter of non-function");
13701           else
13702             {
13703               error ("default arguments are only permitted for function parameters");
13704               default_argument = NULL_TREE;
13705             }
13706         }
13707       else if ((declarator && declarator->parameter_pack_p)
13708                || (decl_specifiers.type
13709                    && PACK_EXPANSION_P (decl_specifiers.type)))
13710         {
13711           const char* kind = template_parm_p? "template " : "";
13712           
13713           /* Find the name of the parameter pack.  */     
13714           cp_declarator *id_declarator = declarator;
13715           while (id_declarator && id_declarator->kind != cdk_id)
13716             id_declarator = id_declarator->declarator;
13717           
13718           if (id_declarator && id_declarator->kind == cdk_id)
13719             error ("%sparameter pack %qD cannot have a default argument",
13720                    kind, id_declarator->u.id.unqualified_name);
13721           else
13722             error ("%sparameter pack cannot have a default argument",
13723                    kind);
13724           
13725           default_argument = NULL_TREE;
13726         }
13727     }
13728   else
13729     default_argument = NULL_TREE;
13730
13731   return make_parameter_declarator (&decl_specifiers,
13732                                     declarator,
13733                                     default_argument);
13734 }
13735
13736 /* Parse a default argument and return it.
13737
13738    TEMPLATE_PARM_P is true if this is a default argument for a
13739    non-type template parameter.  */
13740 static tree
13741 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
13742 {
13743   tree default_argument = NULL_TREE;
13744   bool saved_greater_than_is_operator_p;
13745   bool saved_local_variables_forbidden_p;
13746
13747   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13748      set correctly.  */
13749   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
13750   parser->greater_than_is_operator_p = !template_parm_p;
13751   /* Local variable names (and the `this' keyword) may not
13752      appear in a default argument.  */
13753   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13754   parser->local_variables_forbidden_p = true;
13755   /* The default argument expression may cause implicitly
13756      defined member functions to be synthesized, which will
13757      result in garbage collection.  We must treat this
13758      situation as if we were within the body of function so as
13759      to avoid collecting live data on the stack.  */
13760   ++function_depth;
13761   /* Parse the assignment-expression.  */
13762   if (template_parm_p)
13763     push_deferring_access_checks (dk_no_deferred);
13764   default_argument
13765     = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13766   if (template_parm_p)
13767     pop_deferring_access_checks ();
13768   /* Restore saved state.  */
13769   --function_depth;
13770   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
13771   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
13772
13773   return default_argument;
13774 }
13775
13776 /* Parse a function-body.
13777
13778    function-body:
13779      compound_statement  */
13780
13781 static void
13782 cp_parser_function_body (cp_parser *parser)
13783 {
13784   cp_parser_compound_statement (parser, NULL, false);
13785 }
13786
13787 /* Parse a ctor-initializer-opt followed by a function-body.  Return
13788    true if a ctor-initializer was present.  */
13789
13790 static bool
13791 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13792 {
13793   tree body;
13794   bool ctor_initializer_p;
13795
13796   /* Begin the function body.  */
13797   body = begin_function_body ();
13798   /* Parse the optional ctor-initializer.  */
13799   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13800   /* Parse the function-body.  */
13801   cp_parser_function_body (parser);
13802   /* Finish the function body.  */
13803   finish_function_body (body);
13804
13805   return ctor_initializer_p;
13806 }
13807
13808 /* Parse an initializer.
13809
13810    initializer:
13811      = initializer-clause
13812      ( expression-list )
13813
13814    Returns an expression representing the initializer.  If no
13815    initializer is present, NULL_TREE is returned.
13816
13817    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13818    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
13819    set to FALSE if there is no initializer present.  If there is an
13820    initializer, and it is not a constant-expression, *NON_CONSTANT_P
13821    is set to true; otherwise it is set to false.  */
13822
13823 static tree
13824 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13825                        bool* non_constant_p)
13826 {
13827   cp_token *token;
13828   tree init;
13829
13830   /* Peek at the next token.  */
13831   token = cp_lexer_peek_token (parser->lexer);
13832
13833   /* Let our caller know whether or not this initializer was
13834      parenthesized.  */
13835   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13836   /* Assume that the initializer is constant.  */
13837   *non_constant_p = false;
13838
13839   if (token->type == CPP_EQ)
13840     {
13841       /* Consume the `='.  */
13842       cp_lexer_consume_token (parser->lexer);
13843       /* Parse the initializer-clause.  */
13844       init = cp_parser_initializer_clause (parser, non_constant_p);
13845     }
13846   else if (token->type == CPP_OPEN_PAREN)
13847     init = cp_parser_parenthesized_expression_list (parser, false,
13848                                                     /*cast_p=*/false,
13849                                                     /*allow_expansion_p=*/true,
13850                                                     non_constant_p);
13851   else
13852     {
13853       /* Anything else is an error.  */
13854       cp_parser_error (parser, "expected initializer");
13855       init = error_mark_node;
13856     }
13857
13858   return init;
13859 }
13860
13861 /* Parse an initializer-clause.
13862
13863    initializer-clause:
13864      assignment-expression
13865      { initializer-list , [opt] }
13866      { }
13867
13868    Returns an expression representing the initializer.
13869
13870    If the `assignment-expression' production is used the value
13871    returned is simply a representation for the expression.
13872
13873    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
13874    the elements of the initializer-list (or NULL, if the last
13875    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
13876    NULL_TREE.  There is no way to detect whether or not the optional
13877    trailing `,' was provided.  NON_CONSTANT_P is as for
13878    cp_parser_initializer.  */
13879
13880 static tree
13881 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13882 {
13883   tree initializer;
13884
13885   /* Assume the expression is constant.  */
13886   *non_constant_p = false;
13887
13888   /* If it is not a `{', then we are looking at an
13889      assignment-expression.  */
13890   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13891     {
13892       initializer
13893         = cp_parser_constant_expression (parser,
13894                                         /*allow_non_constant_p=*/true,
13895                                         non_constant_p);
13896       if (!*non_constant_p)
13897         initializer = fold_non_dependent_expr (initializer);
13898     }
13899   else
13900     {
13901       /* Consume the `{' token.  */
13902       cp_lexer_consume_token (parser->lexer);
13903       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
13904       initializer = make_node (CONSTRUCTOR);
13905       /* If it's not a `}', then there is a non-trivial initializer.  */
13906       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13907         {
13908           /* Parse the initializer list.  */
13909           CONSTRUCTOR_ELTS (initializer)
13910             = cp_parser_initializer_list (parser, non_constant_p);
13911           /* A trailing `,' token is allowed.  */
13912           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13913             cp_lexer_consume_token (parser->lexer);
13914         }
13915       /* Now, there should be a trailing `}'.  */
13916       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13917     }
13918
13919   return initializer;
13920 }
13921
13922 /* Parse an initializer-list.
13923
13924    initializer-list:
13925      initializer-clause ... [opt]
13926      initializer-list , initializer-clause ... [opt]
13927
13928    GNU Extension:
13929
13930    initializer-list:
13931      identifier : initializer-clause
13932      initializer-list, identifier : initializer-clause
13933
13934    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
13935    for the initializer.  If the INDEX of the elt is non-NULL, it is the
13936    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
13937    as for cp_parser_initializer.  */
13938
13939 static VEC(constructor_elt,gc) *
13940 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13941 {
13942   VEC(constructor_elt,gc) *v = NULL;
13943
13944   /* Assume all of the expressions are constant.  */
13945   *non_constant_p = false;
13946
13947   /* Parse the rest of the list.  */
13948   while (true)
13949     {
13950       cp_token *token;
13951       tree identifier;
13952       tree initializer;
13953       bool clause_non_constant_p;
13954
13955       /* If the next token is an identifier and the following one is a
13956          colon, we are looking at the GNU designated-initializer
13957          syntax.  */
13958       if (cp_parser_allow_gnu_extensions_p (parser)
13959           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13960           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13961         {
13962           /* Warn the user that they are using an extension.  */
13963           if (pedantic)
13964             pedwarn ("ISO C++ does not allow designated initializers");
13965           /* Consume the identifier.  */
13966           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13967           /* Consume the `:'.  */
13968           cp_lexer_consume_token (parser->lexer);
13969         }
13970       else
13971         identifier = NULL_TREE;
13972
13973       /* Parse the initializer.  */
13974       initializer = cp_parser_initializer_clause (parser,
13975                                                   &clause_non_constant_p);
13976       /* If any clause is non-constant, so is the entire initializer.  */
13977       if (clause_non_constant_p)
13978         *non_constant_p = true;
13979
13980       /* If we have an ellipsis, this is an initializer pack
13981          expansion.  */
13982       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13983         {
13984           /* Consume the `...'.  */
13985           cp_lexer_consume_token (parser->lexer);
13986
13987           /* Turn the initializer into an initializer expansion.  */
13988           initializer = make_pack_expansion (initializer);
13989         }
13990
13991       /* Add it to the vector.  */
13992       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13993
13994       /* If the next token is not a comma, we have reached the end of
13995          the list.  */
13996       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13997         break;
13998
13999       /* Peek at the next token.  */
14000       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14001       /* If the next token is a `}', then we're still done.  An
14002          initializer-clause can have a trailing `,' after the
14003          initializer-list and before the closing `}'.  */
14004       if (token->type == CPP_CLOSE_BRACE)
14005         break;
14006
14007       /* Consume the `,' token.  */
14008       cp_lexer_consume_token (parser->lexer);
14009     }
14010
14011   return v;
14012 }
14013
14014 /* Classes [gram.class] */
14015
14016 /* Parse a class-name.
14017
14018    class-name:
14019      identifier
14020      template-id
14021
14022    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14023    to indicate that names looked up in dependent types should be
14024    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14025    keyword has been used to indicate that the name that appears next
14026    is a template.  TAG_TYPE indicates the explicit tag given before
14027    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14028    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14029    is the class being defined in a class-head.
14030
14031    Returns the TYPE_DECL representing the class.  */
14032
14033 static tree
14034 cp_parser_class_name (cp_parser *parser,
14035                       bool typename_keyword_p,
14036                       bool template_keyword_p,
14037                       enum tag_types tag_type,
14038                       bool check_dependency_p,
14039                       bool class_head_p,
14040                       bool is_declaration)
14041 {
14042   tree decl;
14043   tree scope;
14044   bool typename_p;
14045   cp_token *token;
14046
14047   /* All class-names start with an identifier.  */
14048   token = cp_lexer_peek_token (parser->lexer);
14049   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14050     {
14051       cp_parser_error (parser, "expected class-name");
14052       return error_mark_node;
14053     }
14054
14055   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14056      to a template-id, so we save it here.  */
14057   scope = parser->scope;
14058   if (scope == error_mark_node)
14059     return error_mark_node;
14060
14061   /* Any name names a type if we're following the `typename' keyword
14062      in a qualified name where the enclosing scope is type-dependent.  */
14063   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14064                 && dependent_type_p (scope));
14065   /* Handle the common case (an identifier, but not a template-id)
14066      efficiently.  */
14067   if (token->type == CPP_NAME
14068       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14069     {
14070       cp_token *identifier_token;
14071       tree identifier;
14072       bool ambiguous_p;
14073
14074       /* Look for the identifier.  */
14075       identifier_token = cp_lexer_peek_token (parser->lexer);
14076       ambiguous_p = identifier_token->ambiguous_p;
14077       identifier = cp_parser_identifier (parser);
14078       /* If the next token isn't an identifier, we are certainly not
14079          looking at a class-name.  */
14080       if (identifier == error_mark_node)
14081         decl = error_mark_node;
14082       /* If we know this is a type-name, there's no need to look it
14083          up.  */
14084       else if (typename_p)
14085         decl = identifier;
14086       else
14087         {
14088           tree ambiguous_decls;
14089           /* If we already know that this lookup is ambiguous, then
14090              we've already issued an error message; there's no reason
14091              to check again.  */
14092           if (ambiguous_p)
14093             {
14094               cp_parser_simulate_error (parser);
14095               return error_mark_node;
14096             }
14097           /* If the next token is a `::', then the name must be a type
14098              name.
14099
14100              [basic.lookup.qual]
14101
14102              During the lookup for a name preceding the :: scope
14103              resolution operator, object, function, and enumerator
14104              names are ignored.  */
14105           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14106             tag_type = typename_type;
14107           /* Look up the name.  */
14108           decl = cp_parser_lookup_name (parser, identifier,
14109                                         tag_type,
14110                                         /*is_template=*/false,
14111                                         /*is_namespace=*/false,
14112                                         check_dependency_p,
14113                                         &ambiguous_decls);
14114           if (ambiguous_decls)
14115             {
14116               error ("reference to %qD is ambiguous", identifier);
14117               print_candidates (ambiguous_decls);
14118               if (cp_parser_parsing_tentatively (parser))
14119                 {
14120                   identifier_token->ambiguous_p = true;
14121                   cp_parser_simulate_error (parser);
14122                 }
14123               return error_mark_node;
14124             }
14125         }
14126     }
14127   else
14128     {
14129       /* Try a template-id.  */
14130       decl = cp_parser_template_id (parser, template_keyword_p,
14131                                     check_dependency_p,
14132                                     is_declaration);
14133       if (decl == error_mark_node)
14134         return error_mark_node;
14135     }
14136
14137   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14138
14139   /* If this is a typename, create a TYPENAME_TYPE.  */
14140   if (typename_p && decl != error_mark_node)
14141     {
14142       decl = make_typename_type (scope, decl, typename_type,
14143                                  /*complain=*/tf_error);
14144       if (decl != error_mark_node)
14145         decl = TYPE_NAME (decl);
14146     }
14147
14148   /* Check to see that it is really the name of a class.  */
14149   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14150       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14151       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14152     /* Situations like this:
14153
14154          template <typename T> struct A {
14155            typename T::template X<int>::I i;
14156          };
14157
14158        are problematic.  Is `T::template X<int>' a class-name?  The
14159        standard does not seem to be definitive, but there is no other
14160        valid interpretation of the following `::'.  Therefore, those
14161        names are considered class-names.  */
14162     {
14163       decl = make_typename_type (scope, decl, tag_type, tf_error);
14164       if (decl != error_mark_node)
14165         decl = TYPE_NAME (decl);
14166     }
14167   else if (TREE_CODE (decl) != TYPE_DECL
14168            || TREE_TYPE (decl) == error_mark_node
14169            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
14170     decl = error_mark_node;
14171
14172   if (decl == error_mark_node)
14173     cp_parser_error (parser, "expected class-name");
14174
14175   return decl;
14176 }
14177
14178 /* Parse a class-specifier.
14179
14180    class-specifier:
14181      class-head { member-specification [opt] }
14182
14183    Returns the TREE_TYPE representing the class.  */
14184
14185 static tree
14186 cp_parser_class_specifier (cp_parser* parser)
14187 {
14188   cp_token *token;
14189   tree type;
14190   tree attributes = NULL_TREE;
14191   int has_trailing_semicolon;
14192   bool nested_name_specifier_p;
14193   unsigned saved_num_template_parameter_lists;
14194   bool saved_in_function_body;
14195   tree old_scope = NULL_TREE;
14196   tree scope = NULL_TREE;
14197   tree bases;
14198
14199   push_deferring_access_checks (dk_no_deferred);
14200
14201   /* Parse the class-head.  */
14202   type = cp_parser_class_head (parser,
14203                                &nested_name_specifier_p,
14204                                &attributes,
14205                                &bases);
14206   /* If the class-head was a semantic disaster, skip the entire body
14207      of the class.  */
14208   if (!type)
14209     {
14210       cp_parser_skip_to_end_of_block_or_statement (parser);
14211       pop_deferring_access_checks ();
14212       return error_mark_node;
14213     }
14214
14215   /* Look for the `{'.  */
14216   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
14217     {
14218       pop_deferring_access_checks ();
14219       return error_mark_node;
14220     }
14221
14222   /* Process the base classes. If they're invalid, skip the 
14223      entire class body.  */
14224   if (!xref_basetypes (type, bases))
14225     {
14226       /* Consuming the closing brace yields better error messages
14227          later on.  */
14228       if (cp_parser_skip_to_closing_brace (parser))
14229         cp_lexer_consume_token (parser->lexer);
14230       pop_deferring_access_checks ();
14231       return error_mark_node;
14232     }
14233
14234   /* Issue an error message if type-definitions are forbidden here.  */
14235   cp_parser_check_type_definition (parser);
14236   /* Remember that we are defining one more class.  */
14237   ++parser->num_classes_being_defined;
14238   /* Inside the class, surrounding template-parameter-lists do not
14239      apply.  */
14240   saved_num_template_parameter_lists
14241     = parser->num_template_parameter_lists;
14242   parser->num_template_parameter_lists = 0;
14243   /* We are not in a function body.  */
14244   saved_in_function_body = parser->in_function_body;
14245   parser->in_function_body = false;
14246
14247   /* Start the class.  */
14248   if (nested_name_specifier_p)
14249     {
14250       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14251       old_scope = push_inner_scope (scope);
14252     }
14253   type = begin_class_definition (type, attributes);
14254
14255   if (type == error_mark_node)
14256     /* If the type is erroneous, skip the entire body of the class.  */
14257     cp_parser_skip_to_closing_brace (parser);
14258   else
14259     /* Parse the member-specification.  */
14260     cp_parser_member_specification_opt (parser);
14261
14262   /* Look for the trailing `}'.  */
14263   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14264   /* We get better error messages by noticing a common problem: a
14265      missing trailing `;'.  */
14266   token = cp_lexer_peek_token (parser->lexer);
14267   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14268   /* Look for trailing attributes to apply to this class.  */
14269   if (cp_parser_allow_gnu_extensions_p (parser))
14270     attributes = cp_parser_attributes_opt (parser);
14271   if (type != error_mark_node)
14272     type = finish_struct (type, attributes);
14273   if (nested_name_specifier_p)
14274     pop_inner_scope (old_scope, scope);
14275   /* If this class is not itself within the scope of another class,
14276      then we need to parse the bodies of all of the queued function
14277      definitions.  Note that the queued functions defined in a class
14278      are not always processed immediately following the
14279      class-specifier for that class.  Consider:
14280
14281        struct A {
14282          struct B { void f() { sizeof (A); } };
14283        };
14284
14285      If `f' were processed before the processing of `A' were
14286      completed, there would be no way to compute the size of `A'.
14287      Note that the nesting we are interested in here is lexical --
14288      not the semantic nesting given by TYPE_CONTEXT.  In particular,
14289      for:
14290
14291        struct A { struct B; };
14292        struct A::B { void f() { } };
14293
14294      there is no need to delay the parsing of `A::B::f'.  */
14295   if (--parser->num_classes_being_defined == 0)
14296     {
14297       tree queue_entry;
14298       tree fn;
14299       tree class_type = NULL_TREE;
14300       tree pushed_scope = NULL_TREE;
14301
14302       /* In a first pass, parse default arguments to the functions.
14303          Then, in a second pass, parse the bodies of the functions.
14304          This two-phased approach handles cases like:
14305
14306             struct S {
14307               void f() { g(); }
14308               void g(int i = 3);
14309             };
14310
14311          */
14312       for (TREE_PURPOSE (parser->unparsed_functions_queues)
14313              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14314            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14315            TREE_PURPOSE (parser->unparsed_functions_queues)
14316              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14317         {
14318           fn = TREE_VALUE (queue_entry);
14319           /* If there are default arguments that have not yet been processed,
14320              take care of them now.  */
14321           if (class_type != TREE_PURPOSE (queue_entry))
14322             {
14323               if (pushed_scope)
14324                 pop_scope (pushed_scope);
14325               class_type = TREE_PURPOSE (queue_entry);
14326               pushed_scope = push_scope (class_type);
14327             }
14328           /* Make sure that any template parameters are in scope.  */
14329           maybe_begin_member_template_processing (fn);
14330           /* Parse the default argument expressions.  */
14331           cp_parser_late_parsing_default_args (parser, fn);
14332           /* Remove any template parameters from the symbol table.  */
14333           maybe_end_member_template_processing ();
14334         }
14335       if (pushed_scope)
14336         pop_scope (pushed_scope);
14337       /* Now parse the body of the functions.  */
14338       for (TREE_VALUE (parser->unparsed_functions_queues)
14339              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14340            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14341            TREE_VALUE (parser->unparsed_functions_queues)
14342              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14343         {
14344           /* Figure out which function we need to process.  */
14345           fn = TREE_VALUE (queue_entry);
14346           /* Parse the function.  */
14347           cp_parser_late_parsing_for_member (parser, fn);
14348         }
14349     }
14350
14351   /* Put back any saved access checks.  */
14352   pop_deferring_access_checks ();
14353
14354   /* Restore saved state.  */
14355   parser->in_function_body = saved_in_function_body;
14356   parser->num_template_parameter_lists
14357     = saved_num_template_parameter_lists;
14358
14359   return type;
14360 }
14361
14362 /* Parse a class-head.
14363
14364    class-head:
14365      class-key identifier [opt] base-clause [opt]
14366      class-key nested-name-specifier identifier base-clause [opt]
14367      class-key nested-name-specifier [opt] template-id
14368        base-clause [opt]
14369
14370    GNU Extensions:
14371      class-key attributes identifier [opt] base-clause [opt]
14372      class-key attributes nested-name-specifier identifier base-clause [opt]
14373      class-key attributes nested-name-specifier [opt] template-id
14374        base-clause [opt]
14375
14376    Upon return BASES is initialized to the list of base classes (or
14377    NULL, if there are none) in the same form returned by
14378    cp_parser_base_clause.
14379
14380    Returns the TYPE of the indicated class.  Sets
14381    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14382    involving a nested-name-specifier was used, and FALSE otherwise.
14383
14384    Returns error_mark_node if this is not a class-head.
14385
14386    Returns NULL_TREE if the class-head is syntactically valid, but
14387    semantically invalid in a way that means we should skip the entire
14388    body of the class.  */
14389
14390 static tree
14391 cp_parser_class_head (cp_parser* parser,
14392                       bool* nested_name_specifier_p,
14393                       tree *attributes_p,
14394                       tree *bases)
14395 {
14396   tree nested_name_specifier;
14397   enum tag_types class_key;
14398   tree id = NULL_TREE;
14399   tree type = NULL_TREE;
14400   tree attributes;
14401   bool template_id_p = false;
14402   bool qualified_p = false;
14403   bool invalid_nested_name_p = false;
14404   bool invalid_explicit_specialization_p = false;
14405   tree pushed_scope = NULL_TREE;
14406   unsigned num_templates;
14407
14408   /* Assume no nested-name-specifier will be present.  */
14409   *nested_name_specifier_p = false;
14410   /* Assume no template parameter lists will be used in defining the
14411      type.  */
14412   num_templates = 0;
14413
14414   *bases = NULL_TREE;
14415
14416   /* Look for the class-key.  */
14417   class_key = cp_parser_class_key (parser);
14418   if (class_key == none_type)
14419     return error_mark_node;
14420
14421   /* Parse the attributes.  */
14422   attributes = cp_parser_attributes_opt (parser);
14423
14424   /* If the next token is `::', that is invalid -- but sometimes
14425      people do try to write:
14426
14427        struct ::S {};
14428
14429      Handle this gracefully by accepting the extra qualifier, and then
14430      issuing an error about it later if this really is a
14431      class-head.  If it turns out just to be an elaborated type
14432      specifier, remain silent.  */
14433   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14434     qualified_p = true;
14435
14436   push_deferring_access_checks (dk_no_check);
14437
14438   /* Determine the name of the class.  Begin by looking for an
14439      optional nested-name-specifier.  */
14440   nested_name_specifier
14441     = cp_parser_nested_name_specifier_opt (parser,
14442                                            /*typename_keyword_p=*/false,
14443                                            /*check_dependency_p=*/false,
14444                                            /*type_p=*/false,
14445                                            /*is_declaration=*/false);
14446   /* If there was a nested-name-specifier, then there *must* be an
14447      identifier.  */
14448   if (nested_name_specifier)
14449     {
14450       /* Although the grammar says `identifier', it really means
14451          `class-name' or `template-name'.  You are only allowed to
14452          define a class that has already been declared with this
14453          syntax.
14454
14455          The proposed resolution for Core Issue 180 says that wherever
14456          you see `class T::X' you should treat `X' as a type-name.
14457
14458          It is OK to define an inaccessible class; for example:
14459
14460            class A { class B; };
14461            class A::B {};
14462
14463          We do not know if we will see a class-name, or a
14464          template-name.  We look for a class-name first, in case the
14465          class-name is a template-id; if we looked for the
14466          template-name first we would stop after the template-name.  */
14467       cp_parser_parse_tentatively (parser);
14468       type = cp_parser_class_name (parser,
14469                                    /*typename_keyword_p=*/false,
14470                                    /*template_keyword_p=*/false,
14471                                    class_type,
14472                                    /*check_dependency_p=*/false,
14473                                    /*class_head_p=*/true,
14474                                    /*is_declaration=*/false);
14475       /* If that didn't work, ignore the nested-name-specifier.  */
14476       if (!cp_parser_parse_definitely (parser))
14477         {
14478           invalid_nested_name_p = true;
14479           id = cp_parser_identifier (parser);
14480           if (id == error_mark_node)
14481             id = NULL_TREE;
14482         }
14483       /* If we could not find a corresponding TYPE, treat this
14484          declaration like an unqualified declaration.  */
14485       if (type == error_mark_node)
14486         nested_name_specifier = NULL_TREE;
14487       /* Otherwise, count the number of templates used in TYPE and its
14488          containing scopes.  */
14489       else
14490         {
14491           tree scope;
14492
14493           for (scope = TREE_TYPE (type);
14494                scope && TREE_CODE (scope) != NAMESPACE_DECL;
14495                scope = (TYPE_P (scope)
14496                         ? TYPE_CONTEXT (scope)
14497                         : DECL_CONTEXT (scope)))
14498             if (TYPE_P (scope)
14499                 && CLASS_TYPE_P (scope)
14500                 && CLASSTYPE_TEMPLATE_INFO (scope)
14501                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14502                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14503               ++num_templates;
14504         }
14505     }
14506   /* Otherwise, the identifier is optional.  */
14507   else
14508     {
14509       /* We don't know whether what comes next is a template-id,
14510          an identifier, or nothing at all.  */
14511       cp_parser_parse_tentatively (parser);
14512       /* Check for a template-id.  */
14513       id = cp_parser_template_id (parser,
14514                                   /*template_keyword_p=*/false,
14515                                   /*check_dependency_p=*/true,
14516                                   /*is_declaration=*/true);
14517       /* If that didn't work, it could still be an identifier.  */
14518       if (!cp_parser_parse_definitely (parser))
14519         {
14520           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14521             id = cp_parser_identifier (parser);
14522           else
14523             id = NULL_TREE;
14524         }
14525       else
14526         {
14527           template_id_p = true;
14528           ++num_templates;
14529         }
14530     }
14531
14532   pop_deferring_access_checks ();
14533
14534   if (id)
14535     cp_parser_check_for_invalid_template_id (parser, id);
14536
14537   /* If it's not a `:' or a `{' then we can't really be looking at a
14538      class-head, since a class-head only appears as part of a
14539      class-specifier.  We have to detect this situation before calling
14540      xref_tag, since that has irreversible side-effects.  */
14541   if (!cp_parser_next_token_starts_class_definition_p (parser))
14542     {
14543       cp_parser_error (parser, "expected %<{%> or %<:%>");
14544       return error_mark_node;
14545     }
14546
14547   /* At this point, we're going ahead with the class-specifier, even
14548      if some other problem occurs.  */
14549   cp_parser_commit_to_tentative_parse (parser);
14550   /* Issue the error about the overly-qualified name now.  */
14551   if (qualified_p)
14552     cp_parser_error (parser,
14553                      "global qualification of class name is invalid");
14554   else if (invalid_nested_name_p)
14555     cp_parser_error (parser,
14556                      "qualified name does not name a class");
14557   else if (nested_name_specifier)
14558     {
14559       tree scope;
14560
14561       /* Reject typedef-names in class heads.  */
14562       if (!DECL_IMPLICIT_TYPEDEF_P (type))
14563         {
14564           error ("invalid class name in declaration of %qD", type);
14565           type = NULL_TREE;
14566           goto done;
14567         }
14568
14569       /* Figure out in what scope the declaration is being placed.  */
14570       scope = current_scope ();
14571       /* If that scope does not contain the scope in which the
14572          class was originally declared, the program is invalid.  */
14573       if (scope && !is_ancestor (scope, nested_name_specifier))
14574         {
14575           if (at_namespace_scope_p ())
14576             error ("declaration of %qD in namespace %qD which does not "
14577                    "enclose %qD", type, scope, nested_name_specifier);
14578           else
14579             error ("declaration of %qD in %qD which does not enclose %qD",
14580                    type, scope, nested_name_specifier);
14581           type = NULL_TREE;
14582           goto done;
14583         }
14584       /* [dcl.meaning]
14585
14586          A declarator-id shall not be qualified exception of the
14587          definition of a ... nested class outside of its class
14588          ... [or] a the definition or explicit instantiation of a
14589          class member of a namespace outside of its namespace.  */
14590       if (scope == nested_name_specifier)
14591         {
14592           pedwarn ("extra qualification ignored");
14593           nested_name_specifier = NULL_TREE;
14594           num_templates = 0;
14595         }
14596     }
14597   /* An explicit-specialization must be preceded by "template <>".  If
14598      it is not, try to recover gracefully.  */
14599   if (at_namespace_scope_p ()
14600       && parser->num_template_parameter_lists == 0
14601       && template_id_p)
14602     {
14603       error ("an explicit specialization must be preceded by %<template <>%>");
14604       invalid_explicit_specialization_p = true;
14605       /* Take the same action that would have been taken by
14606          cp_parser_explicit_specialization.  */
14607       ++parser->num_template_parameter_lists;
14608       begin_specialization ();
14609     }
14610   /* There must be no "return" statements between this point and the
14611      end of this function; set "type "to the correct return value and
14612      use "goto done;" to return.  */
14613   /* Make sure that the right number of template parameters were
14614      present.  */
14615   if (!cp_parser_check_template_parameters (parser, num_templates))
14616     {
14617       /* If something went wrong, there is no point in even trying to
14618          process the class-definition.  */
14619       type = NULL_TREE;
14620       goto done;
14621     }
14622
14623   /* Look up the type.  */
14624   if (template_id_p)
14625     {
14626       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
14627           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
14628               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
14629         {
14630           error ("function template %qD redeclared as a class template", id);
14631           type = error_mark_node;
14632         }
14633       else
14634         {
14635           type = TREE_TYPE (id);
14636           type = maybe_process_partial_specialization (type);
14637         }
14638       if (nested_name_specifier)
14639         pushed_scope = push_scope (nested_name_specifier);
14640     }
14641   else if (nested_name_specifier)
14642     {
14643       tree class_type;
14644
14645       /* Given:
14646
14647             template <typename T> struct S { struct T };
14648             template <typename T> struct S<T>::T { };
14649
14650          we will get a TYPENAME_TYPE when processing the definition of
14651          `S::T'.  We need to resolve it to the actual type before we
14652          try to define it.  */
14653       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14654         {
14655           class_type = resolve_typename_type (TREE_TYPE (type),
14656                                               /*only_current_p=*/false);
14657           if (TREE_CODE (class_type) != TYPENAME_TYPE)
14658             type = TYPE_NAME (class_type);
14659           else
14660             {
14661               cp_parser_error (parser, "could not resolve typename type");
14662               type = error_mark_node;
14663             }
14664         }
14665
14666       maybe_process_partial_specialization (TREE_TYPE (type));
14667       class_type = current_class_type;
14668       /* Enter the scope indicated by the nested-name-specifier.  */
14669       pushed_scope = push_scope (nested_name_specifier);
14670       /* Get the canonical version of this type.  */
14671       type = TYPE_MAIN_DECL (TREE_TYPE (type));
14672       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14673           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14674         {
14675           type = push_template_decl (type);
14676           if (type == error_mark_node)
14677             {
14678               type = NULL_TREE;
14679               goto done;
14680             }
14681         }
14682
14683       type = TREE_TYPE (type);
14684       *nested_name_specifier_p = true;
14685     }
14686   else      /* The name is not a nested name.  */
14687     {
14688       /* If the class was unnamed, create a dummy name.  */
14689       if (!id)
14690         id = make_anon_name ();
14691       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14692                        parser->num_template_parameter_lists);
14693     }
14694
14695   /* Indicate whether this class was declared as a `class' or as a
14696      `struct'.  */
14697   if (TREE_CODE (type) == RECORD_TYPE)
14698     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14699   cp_parser_check_class_key (class_key, type);
14700
14701   /* If this type was already complete, and we see another definition,
14702      that's an error.  */
14703   if (type != error_mark_node && COMPLETE_TYPE_P (type))
14704     {
14705       error ("redefinition of %q#T", type);
14706       error ("previous definition of %q+#T", type);
14707       type = NULL_TREE;
14708       goto done;
14709     }
14710   else if (type == error_mark_node)
14711     type = NULL_TREE;
14712
14713   /* We will have entered the scope containing the class; the names of
14714      base classes should be looked up in that context.  For example:
14715
14716        struct A { struct B {}; struct C; };
14717        struct A::C : B {};
14718
14719      is valid.  */
14720
14721   /* Get the list of base-classes, if there is one.  */
14722   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14723     *bases = cp_parser_base_clause (parser);
14724
14725  done:
14726   /* Leave the scope given by the nested-name-specifier.  We will
14727      enter the class scope itself while processing the members.  */
14728   if (pushed_scope)
14729     pop_scope (pushed_scope);
14730
14731   if (invalid_explicit_specialization_p)
14732     {
14733       end_specialization ();
14734       --parser->num_template_parameter_lists;
14735     }
14736   *attributes_p = attributes;
14737   return type;
14738 }
14739
14740 /* Parse a class-key.
14741
14742    class-key:
14743      class
14744      struct
14745      union
14746
14747    Returns the kind of class-key specified, or none_type to indicate
14748    error.  */
14749
14750 static enum tag_types
14751 cp_parser_class_key (cp_parser* parser)
14752 {
14753   cp_token *token;
14754   enum tag_types tag_type;
14755
14756   /* Look for the class-key.  */
14757   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14758   if (!token)
14759     return none_type;
14760
14761   /* Check to see if the TOKEN is a class-key.  */
14762   tag_type = cp_parser_token_is_class_key (token);
14763   if (!tag_type)
14764     cp_parser_error (parser, "expected class-key");
14765   return tag_type;
14766 }
14767
14768 /* Parse an (optional) member-specification.
14769
14770    member-specification:
14771      member-declaration member-specification [opt]
14772      access-specifier : member-specification [opt]  */
14773
14774 static void
14775 cp_parser_member_specification_opt (cp_parser* parser)
14776 {
14777   while (true)
14778     {
14779       cp_token *token;
14780       enum rid keyword;
14781
14782       /* Peek at the next token.  */
14783       token = cp_lexer_peek_token (parser->lexer);
14784       /* If it's a `}', or EOF then we've seen all the members.  */
14785       if (token->type == CPP_CLOSE_BRACE
14786           || token->type == CPP_EOF
14787           || token->type == CPP_PRAGMA_EOL)
14788         break;
14789
14790       /* See if this token is a keyword.  */
14791       keyword = token->keyword;
14792       switch (keyword)
14793         {
14794         case RID_PUBLIC:
14795         case RID_PROTECTED:
14796         case RID_PRIVATE:
14797           /* Consume the access-specifier.  */
14798           cp_lexer_consume_token (parser->lexer);
14799           /* Remember which access-specifier is active.  */
14800           current_access_specifier = token->u.value;
14801           /* Look for the `:'.  */
14802           cp_parser_require (parser, CPP_COLON, "`:'");
14803           break;
14804
14805         default:
14806           /* Accept #pragmas at class scope.  */
14807           if (token->type == CPP_PRAGMA)
14808             {
14809               cp_parser_pragma (parser, pragma_external);
14810               break;
14811             }
14812
14813           /* Otherwise, the next construction must be a
14814              member-declaration.  */
14815           cp_parser_member_declaration (parser);
14816         }
14817     }
14818 }
14819
14820 /* Parse a member-declaration.
14821
14822    member-declaration:
14823      decl-specifier-seq [opt] member-declarator-list [opt] ;
14824      function-definition ; [opt]
14825      :: [opt] nested-name-specifier template [opt] unqualified-id ;
14826      using-declaration
14827      template-declaration
14828
14829    member-declarator-list:
14830      member-declarator
14831      member-declarator-list , member-declarator
14832
14833    member-declarator:
14834      declarator pure-specifier [opt]
14835      declarator constant-initializer [opt]
14836      identifier [opt] : constant-expression
14837
14838    GNU Extensions:
14839
14840    member-declaration:
14841      __extension__ member-declaration
14842
14843    member-declarator:
14844      declarator attributes [opt] pure-specifier [opt]
14845      declarator attributes [opt] constant-initializer [opt]
14846      identifier [opt] attributes [opt] : constant-expression  
14847
14848    C++0x Extensions:
14849
14850    member-declaration:
14851      static_assert-declaration  */
14852
14853 static void
14854 cp_parser_member_declaration (cp_parser* parser)
14855 {
14856   cp_decl_specifier_seq decl_specifiers;
14857   tree prefix_attributes;
14858   tree decl;
14859   int declares_class_or_enum;
14860   bool friend_p;
14861   cp_token *token;
14862   int saved_pedantic;
14863
14864   /* Check for the `__extension__' keyword.  */
14865   if (cp_parser_extension_opt (parser, &saved_pedantic))
14866     {
14867       /* Recurse.  */
14868       cp_parser_member_declaration (parser);
14869       /* Restore the old value of the PEDANTIC flag.  */
14870       pedantic = saved_pedantic;
14871
14872       return;
14873     }
14874
14875   /* Check for a template-declaration.  */
14876   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14877     {
14878       /* An explicit specialization here is an error condition, and we
14879          expect the specialization handler to detect and report this.  */
14880       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14881           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14882         cp_parser_explicit_specialization (parser);
14883       else
14884         cp_parser_template_declaration (parser, /*member_p=*/true);
14885
14886       return;
14887     }
14888
14889   /* Check for a using-declaration.  */
14890   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14891     {
14892       /* Parse the using-declaration.  */
14893       cp_parser_using_declaration (parser,
14894                                    /*access_declaration_p=*/false);
14895       return;
14896     }
14897
14898   /* Check for @defs.  */
14899   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14900     {
14901       tree ivar, member;
14902       tree ivar_chains = cp_parser_objc_defs_expression (parser);
14903       ivar = ivar_chains;
14904       while (ivar)
14905         {
14906           member = ivar;
14907           ivar = TREE_CHAIN (member);
14908           TREE_CHAIN (member) = NULL_TREE;
14909           finish_member_declaration (member);
14910         }
14911       return;
14912     }
14913
14914   /* If the next token is `static_assert' we have a static assertion.  */
14915   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14916     {
14917       cp_parser_static_assert (parser, /*member_p=*/true);
14918       return;
14919     }
14920
14921   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14922     return;
14923
14924   /* Parse the decl-specifier-seq.  */
14925   cp_parser_decl_specifier_seq (parser,
14926                                 CP_PARSER_FLAGS_OPTIONAL,
14927                                 &decl_specifiers,
14928                                 &declares_class_or_enum);
14929   prefix_attributes = decl_specifiers.attributes;
14930   decl_specifiers.attributes = NULL_TREE;
14931   /* Check for an invalid type-name.  */
14932   if (!decl_specifiers.type
14933       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14934     return;
14935   /* If there is no declarator, then the decl-specifier-seq should
14936      specify a type.  */
14937   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14938     {
14939       /* If there was no decl-specifier-seq, and the next token is a
14940          `;', then we have something like:
14941
14942            struct S { ; };
14943
14944          [class.mem]
14945
14946          Each member-declaration shall declare at least one member
14947          name of the class.  */
14948       if (!decl_specifiers.any_specifiers_p)
14949         {
14950           cp_token *token = cp_lexer_peek_token (parser->lexer);
14951           if (pedantic && !token->in_system_header)
14952             pedwarn ("%Hextra %<;%>", &token->location);
14953         }
14954       else
14955         {
14956           tree type;
14957
14958           /* See if this declaration is a friend.  */
14959           friend_p = cp_parser_friend_p (&decl_specifiers);
14960           /* If there were decl-specifiers, check to see if there was
14961              a class-declaration.  */
14962           type = check_tag_decl (&decl_specifiers);
14963           /* Nested classes have already been added to the class, but
14964              a `friend' needs to be explicitly registered.  */
14965           if (friend_p)
14966             {
14967               /* If the `friend' keyword was present, the friend must
14968                  be introduced with a class-key.  */
14969                if (!declares_class_or_enum)
14970                  error ("a class-key must be used when declaring a friend");
14971                /* In this case:
14972
14973                     template <typename T> struct A {
14974                       friend struct A<T>::B;
14975                     };
14976
14977                   A<T>::B will be represented by a TYPENAME_TYPE, and
14978                   therefore not recognized by check_tag_decl.  */
14979                if (!type
14980                    && decl_specifiers.type
14981                    && TYPE_P (decl_specifiers.type))
14982                  type = decl_specifiers.type;
14983                if (!type || !TYPE_P (type))
14984                  error ("friend declaration does not name a class or "
14985                         "function");
14986                else
14987                  make_friend_class (current_class_type, type,
14988                                     /*complain=*/true);
14989             }
14990           /* If there is no TYPE, an error message will already have
14991              been issued.  */
14992           else if (!type || type == error_mark_node)
14993             ;
14994           /* An anonymous aggregate has to be handled specially; such
14995              a declaration really declares a data member (with a
14996              particular type), as opposed to a nested class.  */
14997           else if (ANON_AGGR_TYPE_P (type))
14998             {
14999               /* Remove constructors and such from TYPE, now that we
15000                  know it is an anonymous aggregate.  */
15001               fixup_anonymous_aggr (type);
15002               /* And make the corresponding data member.  */
15003               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15004               /* Add it to the class.  */
15005               finish_member_declaration (decl);
15006             }
15007           else
15008             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
15009         }
15010     }
15011   else
15012     {
15013       /* See if these declarations will be friends.  */
15014       friend_p = cp_parser_friend_p (&decl_specifiers);
15015
15016       /* Keep going until we hit the `;' at the end of the
15017          declaration.  */
15018       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15019         {
15020           tree attributes = NULL_TREE;
15021           tree first_attribute;
15022
15023           /* Peek at the next token.  */
15024           token = cp_lexer_peek_token (parser->lexer);
15025
15026           /* Check for a bitfield declaration.  */
15027           if (token->type == CPP_COLON
15028               || (token->type == CPP_NAME
15029                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15030                   == CPP_COLON))
15031             {
15032               tree identifier;
15033               tree width;
15034
15035               /* Get the name of the bitfield.  Note that we cannot just
15036                  check TOKEN here because it may have been invalidated by
15037                  the call to cp_lexer_peek_nth_token above.  */
15038               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15039                 identifier = cp_parser_identifier (parser);
15040               else
15041                 identifier = NULL_TREE;
15042
15043               /* Consume the `:' token.  */
15044               cp_lexer_consume_token (parser->lexer);
15045               /* Get the width of the bitfield.  */
15046               width
15047                 = cp_parser_constant_expression (parser,
15048                                                  /*allow_non_constant=*/false,
15049                                                  NULL);
15050
15051               /* Look for attributes that apply to the bitfield.  */
15052               attributes = cp_parser_attributes_opt (parser);
15053               /* Remember which attributes are prefix attributes and
15054                  which are not.  */
15055               first_attribute = attributes;
15056               /* Combine the attributes.  */
15057               attributes = chainon (prefix_attributes, attributes);
15058
15059               /* Create the bitfield declaration.  */
15060               decl = grokbitfield (identifier
15061                                    ? make_id_declarator (NULL_TREE,
15062                                                          identifier,
15063                                                          sfk_none)
15064                                    : NULL,
15065                                    &decl_specifiers,
15066                                    width);
15067               /* Apply the attributes.  */
15068               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
15069             }
15070           else
15071             {
15072               cp_declarator *declarator;
15073               tree initializer;
15074               tree asm_specification;
15075               int ctor_dtor_or_conv_p;
15076
15077               /* Parse the declarator.  */
15078               declarator
15079                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15080                                         &ctor_dtor_or_conv_p,
15081                                         /*parenthesized_p=*/NULL,
15082                                         /*member_p=*/true);
15083
15084               /* If something went wrong parsing the declarator, make sure
15085                  that we at least consume some tokens.  */
15086               if (declarator == cp_error_declarator)
15087                 {
15088                   /* Skip to the end of the statement.  */
15089                   cp_parser_skip_to_end_of_statement (parser);
15090                   /* If the next token is not a semicolon, that is
15091                      probably because we just skipped over the body of
15092                      a function.  So, we consume a semicolon if
15093                      present, but do not issue an error message if it
15094                      is not present.  */
15095                   if (cp_lexer_next_token_is (parser->lexer,
15096                                               CPP_SEMICOLON))
15097                     cp_lexer_consume_token (parser->lexer);
15098                   return;
15099                 }
15100
15101               if (declares_class_or_enum & 2)
15102                 cp_parser_check_for_definition_in_return_type
15103                   (declarator, decl_specifiers.type);
15104
15105               /* Look for an asm-specification.  */
15106               asm_specification = cp_parser_asm_specification_opt (parser);
15107               /* Look for attributes that apply to the declaration.  */
15108               attributes = cp_parser_attributes_opt (parser);
15109               /* Remember which attributes are prefix attributes and
15110                  which are not.  */
15111               first_attribute = attributes;
15112               /* Combine the attributes.  */
15113               attributes = chainon (prefix_attributes, attributes);
15114
15115               /* If it's an `=', then we have a constant-initializer or a
15116                  pure-specifier.  It is not correct to parse the
15117                  initializer before registering the member declaration
15118                  since the member declaration should be in scope while
15119                  its initializer is processed.  However, the rest of the
15120                  front end does not yet provide an interface that allows
15121                  us to handle this correctly.  */
15122               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15123                 {
15124                   /* In [class.mem]:
15125
15126                      A pure-specifier shall be used only in the declaration of
15127                      a virtual function.
15128
15129                      A member-declarator can contain a constant-initializer
15130                      only if it declares a static member of integral or
15131                      enumeration type.
15132
15133                      Therefore, if the DECLARATOR is for a function, we look
15134                      for a pure-specifier; otherwise, we look for a
15135                      constant-initializer.  When we call `grokfield', it will
15136                      perform more stringent semantics checks.  */
15137                   if (function_declarator_p (declarator))
15138                     initializer = cp_parser_pure_specifier (parser);
15139                   else
15140                     /* Parse the initializer.  */
15141                     initializer = cp_parser_constant_initializer (parser);
15142                 }
15143               /* Otherwise, there is no initializer.  */
15144               else
15145                 initializer = NULL_TREE;
15146
15147               /* See if we are probably looking at a function
15148                  definition.  We are certainly not looking at a
15149                  member-declarator.  Calling `grokfield' has
15150                  side-effects, so we must not do it unless we are sure
15151                  that we are looking at a member-declarator.  */
15152               if (cp_parser_token_starts_function_definition_p
15153                   (cp_lexer_peek_token (parser->lexer)))
15154                 {
15155                   /* The grammar does not allow a pure-specifier to be
15156                      used when a member function is defined.  (It is
15157                      possible that this fact is an oversight in the
15158                      standard, since a pure function may be defined
15159                      outside of the class-specifier.  */
15160                   if (initializer)
15161                     error ("pure-specifier on function-definition");
15162                   decl = cp_parser_save_member_function_body (parser,
15163                                                               &decl_specifiers,
15164                                                               declarator,
15165                                                               attributes);
15166                   /* If the member was not a friend, declare it here.  */
15167                   if (!friend_p)
15168                     finish_member_declaration (decl);
15169                   /* Peek at the next token.  */
15170                   token = cp_lexer_peek_token (parser->lexer);
15171                   /* If the next token is a semicolon, consume it.  */
15172                   if (token->type == CPP_SEMICOLON)
15173                     cp_lexer_consume_token (parser->lexer);
15174                   return;
15175                 }
15176               else
15177                 /* Create the declaration.  */
15178                 decl = grokfield (declarator, &decl_specifiers,
15179                                   initializer, /*init_const_expr_p=*/true,
15180                                   asm_specification,
15181                                   attributes);
15182             }
15183
15184           /* Reset PREFIX_ATTRIBUTES.  */
15185           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15186             attributes = TREE_CHAIN (attributes);
15187           if (attributes)
15188             TREE_CHAIN (attributes) = NULL_TREE;
15189
15190           /* If there is any qualification still in effect, clear it
15191              now; we will be starting fresh with the next declarator.  */
15192           parser->scope = NULL_TREE;
15193           parser->qualifying_scope = NULL_TREE;
15194           parser->object_scope = NULL_TREE;
15195           /* If it's a `,', then there are more declarators.  */
15196           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15197             cp_lexer_consume_token (parser->lexer);
15198           /* If the next token isn't a `;', then we have a parse error.  */
15199           else if (cp_lexer_next_token_is_not (parser->lexer,
15200                                                CPP_SEMICOLON))
15201             {
15202               cp_parser_error (parser, "expected %<;%>");
15203               /* Skip tokens until we find a `;'.  */
15204               cp_parser_skip_to_end_of_statement (parser);
15205
15206               break;
15207             }
15208
15209           if (decl)
15210             {
15211               /* Add DECL to the list of members.  */
15212               if (!friend_p)
15213                 finish_member_declaration (decl);
15214
15215               if (TREE_CODE (decl) == FUNCTION_DECL)
15216                 cp_parser_save_default_args (parser, decl);
15217             }
15218         }
15219     }
15220
15221   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15222 }
15223
15224 /* Parse a pure-specifier.
15225
15226    pure-specifier:
15227      = 0
15228
15229    Returns INTEGER_ZERO_NODE if a pure specifier is found.
15230    Otherwise, ERROR_MARK_NODE is returned.  */
15231
15232 static tree
15233 cp_parser_pure_specifier (cp_parser* parser)
15234 {
15235   cp_token *token;
15236
15237   /* Look for the `=' token.  */
15238   if (!cp_parser_require (parser, CPP_EQ, "`='"))
15239     return error_mark_node;
15240   /* Look for the `0' token.  */
15241   token = cp_lexer_consume_token (parser->lexer);
15242   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
15243   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15244     {
15245       cp_parser_error (parser,
15246                        "invalid pure specifier (only `= 0' is allowed)");
15247       cp_parser_skip_to_end_of_statement (parser);
15248       return error_mark_node;
15249     }
15250   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15251     {
15252       error ("templates may not be %<virtual%>");
15253       return error_mark_node;
15254     }
15255
15256   return integer_zero_node;
15257 }
15258
15259 /* Parse a constant-initializer.
15260
15261    constant-initializer:
15262      = constant-expression
15263
15264    Returns a representation of the constant-expression.  */
15265
15266 static tree
15267 cp_parser_constant_initializer (cp_parser* parser)
15268 {
15269   /* Look for the `=' token.  */
15270   if (!cp_parser_require (parser, CPP_EQ, "`='"))
15271     return error_mark_node;
15272
15273   /* It is invalid to write:
15274
15275        struct S { static const int i = { 7 }; };
15276
15277      */
15278   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15279     {
15280       cp_parser_error (parser,
15281                        "a brace-enclosed initializer is not allowed here");
15282       /* Consume the opening brace.  */
15283       cp_lexer_consume_token (parser->lexer);
15284       /* Skip the initializer.  */
15285       cp_parser_skip_to_closing_brace (parser);
15286       /* Look for the trailing `}'.  */
15287       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
15288
15289       return error_mark_node;
15290     }
15291
15292   return cp_parser_constant_expression (parser,
15293                                         /*allow_non_constant=*/false,
15294                                         NULL);
15295 }
15296
15297 /* Derived classes [gram.class.derived] */
15298
15299 /* Parse a base-clause.
15300
15301    base-clause:
15302      : base-specifier-list
15303
15304    base-specifier-list:
15305      base-specifier ... [opt]
15306      base-specifier-list , base-specifier ... [opt]
15307
15308    Returns a TREE_LIST representing the base-classes, in the order in
15309    which they were declared.  The representation of each node is as
15310    described by cp_parser_base_specifier.
15311
15312    In the case that no bases are specified, this function will return
15313    NULL_TREE, not ERROR_MARK_NODE.  */
15314
15315 static tree
15316 cp_parser_base_clause (cp_parser* parser)
15317 {
15318   tree bases = NULL_TREE;
15319
15320   /* Look for the `:' that begins the list.  */
15321   cp_parser_require (parser, CPP_COLON, "`:'");
15322
15323   /* Scan the base-specifier-list.  */
15324   while (true)
15325     {
15326       cp_token *token;
15327       tree base;
15328       bool pack_expansion_p = false;
15329
15330       /* Look for the base-specifier.  */
15331       base = cp_parser_base_specifier (parser);
15332       /* Look for the (optional) ellipsis. */
15333       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15334         {
15335           /* Consume the `...'. */
15336           cp_lexer_consume_token (parser->lexer);
15337
15338           pack_expansion_p = true;
15339         }
15340
15341       /* Add BASE to the front of the list.  */
15342       if (base != error_mark_node)
15343         {
15344           if (pack_expansion_p)
15345             /* Make this a pack expansion type. */
15346             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15347           
15348
15349           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15350             {
15351               TREE_CHAIN (base) = bases;
15352               bases = base;
15353             }
15354         }
15355       /* Peek at the next token.  */
15356       token = cp_lexer_peek_token (parser->lexer);
15357       /* If it's not a comma, then the list is complete.  */
15358       if (token->type != CPP_COMMA)
15359         break;
15360       /* Consume the `,'.  */
15361       cp_lexer_consume_token (parser->lexer);
15362     }
15363
15364   /* PARSER->SCOPE may still be non-NULL at this point, if the last
15365      base class had a qualified name.  However, the next name that
15366      appears is certainly not qualified.  */
15367   parser->scope = NULL_TREE;
15368   parser->qualifying_scope = NULL_TREE;
15369   parser->object_scope = NULL_TREE;
15370
15371   return nreverse (bases);
15372 }
15373
15374 /* Parse a base-specifier.
15375
15376    base-specifier:
15377      :: [opt] nested-name-specifier [opt] class-name
15378      virtual access-specifier [opt] :: [opt] nested-name-specifier
15379        [opt] class-name
15380      access-specifier virtual [opt] :: [opt] nested-name-specifier
15381        [opt] class-name
15382
15383    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
15384    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15385    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
15386    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
15387
15388 static tree
15389 cp_parser_base_specifier (cp_parser* parser)
15390 {
15391   cp_token *token;
15392   bool done = false;
15393   bool virtual_p = false;
15394   bool duplicate_virtual_error_issued_p = false;
15395   bool duplicate_access_error_issued_p = false;
15396   bool class_scope_p, template_p;
15397   tree access = access_default_node;
15398   tree type;
15399
15400   /* Process the optional `virtual' and `access-specifier'.  */
15401   while (!done)
15402     {
15403       /* Peek at the next token.  */
15404       token = cp_lexer_peek_token (parser->lexer);
15405       /* Process `virtual'.  */
15406       switch (token->keyword)
15407         {
15408         case RID_VIRTUAL:
15409           /* If `virtual' appears more than once, issue an error.  */
15410           if (virtual_p && !duplicate_virtual_error_issued_p)
15411             {
15412               cp_parser_error (parser,
15413                                "%<virtual%> specified more than once in base-specified");
15414               duplicate_virtual_error_issued_p = true;
15415             }
15416
15417           virtual_p = true;
15418
15419           /* Consume the `virtual' token.  */
15420           cp_lexer_consume_token (parser->lexer);
15421
15422           break;
15423
15424         case RID_PUBLIC:
15425         case RID_PROTECTED:
15426         case RID_PRIVATE:
15427           /* If more than one access specifier appears, issue an
15428              error.  */
15429           if (access != access_default_node
15430               && !duplicate_access_error_issued_p)
15431             {
15432               cp_parser_error (parser,
15433                                "more than one access specifier in base-specified");
15434               duplicate_access_error_issued_p = true;
15435             }
15436
15437           access = ridpointers[(int) token->keyword];
15438
15439           /* Consume the access-specifier.  */
15440           cp_lexer_consume_token (parser->lexer);
15441
15442           break;
15443
15444         default:
15445           done = true;
15446           break;
15447         }
15448     }
15449   /* It is not uncommon to see programs mechanically, erroneously, use
15450      the 'typename' keyword to denote (dependent) qualified types
15451      as base classes.  */
15452   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15453     {
15454       if (!processing_template_decl)
15455         error ("keyword %<typename%> not allowed outside of templates");
15456       else
15457         error ("keyword %<typename%> not allowed in this context "
15458                "(the base class is implicitly a type)");
15459       cp_lexer_consume_token (parser->lexer);
15460     }
15461
15462   /* Look for the optional `::' operator.  */
15463   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15464   /* Look for the nested-name-specifier.  The simplest way to
15465      implement:
15466
15467        [temp.res]
15468
15469        The keyword `typename' is not permitted in a base-specifier or
15470        mem-initializer; in these contexts a qualified name that
15471        depends on a template-parameter is implicitly assumed to be a
15472        type name.
15473
15474      is to pretend that we have seen the `typename' keyword at this
15475      point.  */
15476   cp_parser_nested_name_specifier_opt (parser,
15477                                        /*typename_keyword_p=*/true,
15478                                        /*check_dependency_p=*/true,
15479                                        typename_type,
15480                                        /*is_declaration=*/true);
15481   /* If the base class is given by a qualified name, assume that names
15482      we see are type names or templates, as appropriate.  */
15483   class_scope_p = (parser->scope && TYPE_P (parser->scope));
15484   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15485
15486   /* Finally, look for the class-name.  */
15487   type = cp_parser_class_name (parser,
15488                                class_scope_p,
15489                                template_p,
15490                                typename_type,
15491                                /*check_dependency_p=*/true,
15492                                /*class_head_p=*/false,
15493                                /*is_declaration=*/true);
15494
15495   if (type == error_mark_node)
15496     return error_mark_node;
15497
15498   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15499 }
15500
15501 /* Exception handling [gram.exception] */
15502
15503 /* Parse an (optional) exception-specification.
15504
15505    exception-specification:
15506      throw ( type-id-list [opt] )
15507
15508    Returns a TREE_LIST representing the exception-specification.  The
15509    TREE_VALUE of each node is a type.  */
15510
15511 static tree
15512 cp_parser_exception_specification_opt (cp_parser* parser)
15513 {
15514   cp_token *token;
15515   tree type_id_list;
15516
15517   /* Peek at the next token.  */
15518   token = cp_lexer_peek_token (parser->lexer);
15519   /* If it's not `throw', then there's no exception-specification.  */
15520   if (!cp_parser_is_keyword (token, RID_THROW))
15521     return NULL_TREE;
15522
15523   /* Consume the `throw'.  */
15524   cp_lexer_consume_token (parser->lexer);
15525
15526   /* Look for the `('.  */
15527   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15528
15529   /* Peek at the next token.  */
15530   token = cp_lexer_peek_token (parser->lexer);
15531   /* If it's not a `)', then there is a type-id-list.  */
15532   if (token->type != CPP_CLOSE_PAREN)
15533     {
15534       const char *saved_message;
15535
15536       /* Types may not be defined in an exception-specification.  */
15537       saved_message = parser->type_definition_forbidden_message;
15538       parser->type_definition_forbidden_message
15539         = "types may not be defined in an exception-specification";
15540       /* Parse the type-id-list.  */
15541       type_id_list = cp_parser_type_id_list (parser);
15542       /* Restore the saved message.  */
15543       parser->type_definition_forbidden_message = saved_message;
15544     }
15545   else
15546     type_id_list = empty_except_spec;
15547
15548   /* Look for the `)'.  */
15549   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15550
15551   return type_id_list;
15552 }
15553
15554 /* Parse an (optional) type-id-list.
15555
15556    type-id-list:
15557      type-id ... [opt]
15558      type-id-list , type-id ... [opt]
15559
15560    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
15561    in the order that the types were presented.  */
15562
15563 static tree
15564 cp_parser_type_id_list (cp_parser* parser)
15565 {
15566   tree types = NULL_TREE;
15567
15568   while (true)
15569     {
15570       cp_token *token;
15571       tree type;
15572
15573       /* Get the next type-id.  */
15574       type = cp_parser_type_id (parser);
15575       /* Parse the optional ellipsis. */
15576       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15577         {
15578           /* Consume the `...'. */
15579           cp_lexer_consume_token (parser->lexer);
15580
15581           /* Turn the type into a pack expansion expression. */
15582           type = make_pack_expansion (type);
15583         }
15584       /* Add it to the list.  */
15585       types = add_exception_specifier (types, type, /*complain=*/1);
15586       /* Peek at the next token.  */
15587       token = cp_lexer_peek_token (parser->lexer);
15588       /* If it is not a `,', we are done.  */
15589       if (token->type != CPP_COMMA)
15590         break;
15591       /* Consume the `,'.  */
15592       cp_lexer_consume_token (parser->lexer);
15593     }
15594
15595   return nreverse (types);
15596 }
15597
15598 /* Parse a try-block.
15599
15600    try-block:
15601      try compound-statement handler-seq  */
15602
15603 static tree
15604 cp_parser_try_block (cp_parser* parser)
15605 {
15606   tree try_block;
15607
15608   cp_parser_require_keyword (parser, RID_TRY, "`try'");
15609   try_block = begin_try_block ();
15610   cp_parser_compound_statement (parser, NULL, true);
15611   finish_try_block (try_block);
15612   cp_parser_handler_seq (parser);
15613   finish_handler_sequence (try_block);
15614
15615   return try_block;
15616 }
15617
15618 /* Parse a function-try-block.
15619
15620    function-try-block:
15621      try ctor-initializer [opt] function-body handler-seq  */
15622
15623 static bool
15624 cp_parser_function_try_block (cp_parser* parser)
15625 {
15626   tree compound_stmt;
15627   tree try_block;
15628   bool ctor_initializer_p;
15629
15630   /* Look for the `try' keyword.  */
15631   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15632     return false;
15633   /* Let the rest of the front end know where we are.  */
15634   try_block = begin_function_try_block (&compound_stmt);
15635   /* Parse the function-body.  */
15636   ctor_initializer_p
15637     = cp_parser_ctor_initializer_opt_and_function_body (parser);
15638   /* We're done with the `try' part.  */
15639   finish_function_try_block (try_block);
15640   /* Parse the handlers.  */
15641   cp_parser_handler_seq (parser);
15642   /* We're done with the handlers.  */
15643   finish_function_handler_sequence (try_block, compound_stmt);
15644
15645   return ctor_initializer_p;
15646 }
15647
15648 /* Parse a handler-seq.
15649
15650    handler-seq:
15651      handler handler-seq [opt]  */
15652
15653 static void
15654 cp_parser_handler_seq (cp_parser* parser)
15655 {
15656   while (true)
15657     {
15658       cp_token *token;
15659
15660       /* Parse the handler.  */
15661       cp_parser_handler (parser);
15662       /* Peek at the next token.  */
15663       token = cp_lexer_peek_token (parser->lexer);
15664       /* If it's not `catch' then there are no more handlers.  */
15665       if (!cp_parser_is_keyword (token, RID_CATCH))
15666         break;
15667     }
15668 }
15669
15670 /* Parse a handler.
15671
15672    handler:
15673      catch ( exception-declaration ) compound-statement  */
15674
15675 static void
15676 cp_parser_handler (cp_parser* parser)
15677 {
15678   tree handler;
15679   tree declaration;
15680
15681   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15682   handler = begin_handler ();
15683   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15684   declaration = cp_parser_exception_declaration (parser);
15685   finish_handler_parms (declaration, handler);
15686   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15687   cp_parser_compound_statement (parser, NULL, false);
15688   finish_handler (handler);
15689 }
15690
15691 /* Parse an exception-declaration.
15692
15693    exception-declaration:
15694      type-specifier-seq declarator
15695      type-specifier-seq abstract-declarator
15696      type-specifier-seq
15697      ...
15698
15699    Returns a VAR_DECL for the declaration, or NULL_TREE if the
15700    ellipsis variant is used.  */
15701
15702 static tree
15703 cp_parser_exception_declaration (cp_parser* parser)
15704 {
15705   cp_decl_specifier_seq type_specifiers;
15706   cp_declarator *declarator;
15707   const char *saved_message;
15708
15709   /* If it's an ellipsis, it's easy to handle.  */
15710   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15711     {
15712       /* Consume the `...' token.  */
15713       cp_lexer_consume_token (parser->lexer);
15714       return NULL_TREE;
15715     }
15716
15717   /* Types may not be defined in exception-declarations.  */
15718   saved_message = parser->type_definition_forbidden_message;
15719   parser->type_definition_forbidden_message
15720     = "types may not be defined in exception-declarations";
15721
15722   /* Parse the type-specifier-seq.  */
15723   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15724                                 &type_specifiers);
15725   /* If it's a `)', then there is no declarator.  */
15726   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15727     declarator = NULL;
15728   else
15729     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15730                                        /*ctor_dtor_or_conv_p=*/NULL,
15731                                        /*parenthesized_p=*/NULL,
15732                                        /*member_p=*/false);
15733
15734   /* Restore the saved message.  */
15735   parser->type_definition_forbidden_message = saved_message;
15736
15737   if (!type_specifiers.any_specifiers_p)
15738     return error_mark_node;
15739
15740   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15741 }
15742
15743 /* Parse a throw-expression.
15744
15745    throw-expression:
15746      throw assignment-expression [opt]
15747
15748    Returns a THROW_EXPR representing the throw-expression.  */
15749
15750 static tree
15751 cp_parser_throw_expression (cp_parser* parser)
15752 {
15753   tree expression;
15754   cp_token* token;
15755
15756   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15757   token = cp_lexer_peek_token (parser->lexer);
15758   /* Figure out whether or not there is an assignment-expression
15759      following the "throw" keyword.  */
15760   if (token->type == CPP_COMMA
15761       || token->type == CPP_SEMICOLON
15762       || token->type == CPP_CLOSE_PAREN
15763       || token->type == CPP_CLOSE_SQUARE
15764       || token->type == CPP_CLOSE_BRACE
15765       || token->type == CPP_COLON)
15766     expression = NULL_TREE;
15767   else
15768     expression = cp_parser_assignment_expression (parser,
15769                                                   /*cast_p=*/false);
15770
15771   return build_throw (expression);
15772 }
15773
15774 /* GNU Extensions */
15775
15776 /* Parse an (optional) asm-specification.
15777
15778    asm-specification:
15779      asm ( string-literal )
15780
15781    If the asm-specification is present, returns a STRING_CST
15782    corresponding to the string-literal.  Otherwise, returns
15783    NULL_TREE.  */
15784
15785 static tree
15786 cp_parser_asm_specification_opt (cp_parser* parser)
15787 {
15788   cp_token *token;
15789   tree asm_specification;
15790
15791   /* Peek at the next token.  */
15792   token = cp_lexer_peek_token (parser->lexer);
15793   /* If the next token isn't the `asm' keyword, then there's no
15794      asm-specification.  */
15795   if (!cp_parser_is_keyword (token, RID_ASM))
15796     return NULL_TREE;
15797
15798   /* Consume the `asm' token.  */
15799   cp_lexer_consume_token (parser->lexer);
15800   /* Look for the `('.  */
15801   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15802
15803   /* Look for the string-literal.  */
15804   asm_specification = cp_parser_string_literal (parser, false, false);
15805
15806   /* Look for the `)'.  */
15807   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15808
15809   return asm_specification;
15810 }
15811
15812 /* Parse an asm-operand-list.
15813
15814    asm-operand-list:
15815      asm-operand
15816      asm-operand-list , asm-operand
15817
15818    asm-operand:
15819      string-literal ( expression )
15820      [ string-literal ] string-literal ( expression )
15821
15822    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
15823    each node is the expression.  The TREE_PURPOSE is itself a
15824    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15825    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15826    is a STRING_CST for the string literal before the parenthesis. Returns
15827    ERROR_MARK_NODE if any of the operands are invalid.  */
15828
15829 static tree
15830 cp_parser_asm_operand_list (cp_parser* parser)
15831 {
15832   tree asm_operands = NULL_TREE;
15833   bool invalid_operands = false;
15834
15835   while (true)
15836     {
15837       tree string_literal;
15838       tree expression;
15839       tree name;
15840
15841       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15842         {
15843           /* Consume the `[' token.  */
15844           cp_lexer_consume_token (parser->lexer);
15845           /* Read the operand name.  */
15846           name = cp_parser_identifier (parser);
15847           if (name != error_mark_node)
15848             name = build_string (IDENTIFIER_LENGTH (name),
15849                                  IDENTIFIER_POINTER (name));
15850           /* Look for the closing `]'.  */
15851           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15852         }
15853       else
15854         name = NULL_TREE;
15855       /* Look for the string-literal.  */
15856       string_literal = cp_parser_string_literal (parser, false, false);
15857
15858       /* Look for the `('.  */
15859       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15860       /* Parse the expression.  */
15861       expression = cp_parser_expression (parser, /*cast_p=*/false);
15862       /* Look for the `)'.  */
15863       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15864
15865       if (name == error_mark_node 
15866           || string_literal == error_mark_node 
15867           || expression == error_mark_node)
15868         invalid_operands = true;
15869
15870       /* Add this operand to the list.  */
15871       asm_operands = tree_cons (build_tree_list (name, string_literal),
15872                                 expression,
15873                                 asm_operands);
15874       /* If the next token is not a `,', there are no more
15875          operands.  */
15876       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15877         break;
15878       /* Consume the `,'.  */
15879       cp_lexer_consume_token (parser->lexer);
15880     }
15881
15882   return invalid_operands ? error_mark_node : nreverse (asm_operands);
15883 }
15884
15885 /* Parse an asm-clobber-list.
15886
15887    asm-clobber-list:
15888      string-literal
15889      asm-clobber-list , string-literal
15890
15891    Returns a TREE_LIST, indicating the clobbers in the order that they
15892    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
15893
15894 static tree
15895 cp_parser_asm_clobber_list (cp_parser* parser)
15896 {
15897   tree clobbers = NULL_TREE;
15898
15899   while (true)
15900     {
15901       tree string_literal;
15902
15903       /* Look for the string literal.  */
15904       string_literal = cp_parser_string_literal (parser, false, false);
15905       /* Add it to the list.  */
15906       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15907       /* If the next token is not a `,', then the list is
15908          complete.  */
15909       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15910         break;
15911       /* Consume the `,' token.  */
15912       cp_lexer_consume_token (parser->lexer);
15913     }
15914
15915   return clobbers;
15916 }
15917
15918 /* Parse an (optional) series of attributes.
15919
15920    attributes:
15921      attributes attribute
15922
15923    attribute:
15924      __attribute__ (( attribute-list [opt] ))
15925
15926    The return value is as for cp_parser_attribute_list.  */
15927
15928 static tree
15929 cp_parser_attributes_opt (cp_parser* parser)
15930 {
15931   tree attributes = NULL_TREE;
15932
15933   while (true)
15934     {
15935       cp_token *token;
15936       tree attribute_list;
15937
15938       /* Peek at the next token.  */
15939       token = cp_lexer_peek_token (parser->lexer);
15940       /* If it's not `__attribute__', then we're done.  */
15941       if (token->keyword != RID_ATTRIBUTE)
15942         break;
15943
15944       /* Consume the `__attribute__' keyword.  */
15945       cp_lexer_consume_token (parser->lexer);
15946       /* Look for the two `(' tokens.  */
15947       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15948       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15949
15950       /* Peek at the next token.  */
15951       token = cp_lexer_peek_token (parser->lexer);
15952       if (token->type != CPP_CLOSE_PAREN)
15953         /* Parse the attribute-list.  */
15954         attribute_list = cp_parser_attribute_list (parser);
15955       else
15956         /* If the next token is a `)', then there is no attribute
15957            list.  */
15958         attribute_list = NULL;
15959
15960       /* Look for the two `)' tokens.  */
15961       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15962       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15963
15964       /* Add these new attributes to the list.  */
15965       attributes = chainon (attributes, attribute_list);
15966     }
15967
15968   return attributes;
15969 }
15970
15971 /* Parse an attribute-list.
15972
15973    attribute-list:
15974      attribute
15975      attribute-list , attribute
15976
15977    attribute:
15978      identifier
15979      identifier ( identifier )
15980      identifier ( identifier , expression-list )
15981      identifier ( expression-list )
15982
15983    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
15984    to an attribute.  The TREE_PURPOSE of each node is the identifier
15985    indicating which attribute is in use.  The TREE_VALUE represents
15986    the arguments, if any.  */
15987
15988 static tree
15989 cp_parser_attribute_list (cp_parser* parser)
15990 {
15991   tree attribute_list = NULL_TREE;
15992   bool save_translate_strings_p = parser->translate_strings_p;
15993
15994   parser->translate_strings_p = false;
15995   while (true)
15996     {
15997       cp_token *token;
15998       tree identifier;
15999       tree attribute;
16000
16001       /* Look for the identifier.  We also allow keywords here; for
16002          example `__attribute__ ((const))' is legal.  */
16003       token = cp_lexer_peek_token (parser->lexer);
16004       if (token->type == CPP_NAME
16005           || token->type == CPP_KEYWORD)
16006         {
16007           tree arguments = NULL_TREE;
16008
16009           /* Consume the token.  */
16010           token = cp_lexer_consume_token (parser->lexer);
16011
16012           /* Save away the identifier that indicates which attribute
16013              this is.  */
16014           identifier = token->u.value;
16015           attribute = build_tree_list (identifier, NULL_TREE);
16016
16017           /* Peek at the next token.  */
16018           token = cp_lexer_peek_token (parser->lexer);
16019           /* If it's an `(', then parse the attribute arguments.  */
16020           if (token->type == CPP_OPEN_PAREN)
16021             {
16022               arguments = cp_parser_parenthesized_expression_list
16023                           (parser, true, /*cast_p=*/false,
16024                            /*allow_expansion_p=*/false,
16025                            /*non_constant_p=*/NULL);
16026               /* Save the arguments away.  */
16027               TREE_VALUE (attribute) = arguments;
16028             }
16029
16030           if (arguments != error_mark_node)
16031             {
16032               /* Add this attribute to the list.  */
16033               TREE_CHAIN (attribute) = attribute_list;
16034               attribute_list = attribute;
16035             }
16036
16037           token = cp_lexer_peek_token (parser->lexer);
16038         }
16039       /* Now, look for more attributes.  If the next token isn't a
16040          `,', we're done.  */
16041       if (token->type != CPP_COMMA)
16042         break;
16043
16044       /* Consume the comma and keep going.  */
16045       cp_lexer_consume_token (parser->lexer);
16046     }
16047   parser->translate_strings_p = save_translate_strings_p;
16048
16049   /* We built up the list in reverse order.  */
16050   return nreverse (attribute_list);
16051 }
16052
16053 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16054    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16055    current value of the PEDANTIC flag, regardless of whether or not
16056    the `__extension__' keyword is present.  The caller is responsible
16057    for restoring the value of the PEDANTIC flag.  */
16058
16059 static bool
16060 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16061 {
16062   /* Save the old value of the PEDANTIC flag.  */
16063   *saved_pedantic = pedantic;
16064
16065   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16066     {
16067       /* Consume the `__extension__' token.  */
16068       cp_lexer_consume_token (parser->lexer);
16069       /* We're not being pedantic while the `__extension__' keyword is
16070          in effect.  */
16071       pedantic = 0;
16072
16073       return true;
16074     }
16075
16076   return false;
16077 }
16078
16079 /* Parse a label declaration.
16080
16081    label-declaration:
16082      __label__ label-declarator-seq ;
16083
16084    label-declarator-seq:
16085      identifier , label-declarator-seq
16086      identifier  */
16087
16088 static void
16089 cp_parser_label_declaration (cp_parser* parser)
16090 {
16091   /* Look for the `__label__' keyword.  */
16092   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
16093
16094   while (true)
16095     {
16096       tree identifier;
16097
16098       /* Look for an identifier.  */
16099       identifier = cp_parser_identifier (parser);
16100       /* If we failed, stop.  */
16101       if (identifier == error_mark_node)
16102         break;
16103       /* Declare it as a label.  */
16104       finish_label_decl (identifier);
16105       /* If the next token is a `;', stop.  */
16106       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16107         break;
16108       /* Look for the `,' separating the label declarations.  */
16109       cp_parser_require (parser, CPP_COMMA, "`,'");
16110     }
16111
16112   /* Look for the final `;'.  */
16113   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
16114 }
16115
16116 /* Support Functions */
16117
16118 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16119    NAME should have one of the representations used for an
16120    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16121    is returned.  If PARSER->SCOPE is a dependent type, then a
16122    SCOPE_REF is returned.
16123
16124    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16125    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16126    was formed.  Abstractly, such entities should not be passed to this
16127    function, because they do not need to be looked up, but it is
16128    simpler to check for this special case here, rather than at the
16129    call-sites.
16130
16131    In cases not explicitly covered above, this function returns a
16132    DECL, OVERLOAD, or baselink representing the result of the lookup.
16133    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16134    is returned.
16135
16136    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16137    (e.g., "struct") that was used.  In that case bindings that do not
16138    refer to types are ignored.
16139
16140    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16141    ignored.
16142
16143    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16144    are ignored.
16145
16146    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16147    types.
16148
16149    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16150    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16151    NULL_TREE otherwise.  */
16152
16153 static tree
16154 cp_parser_lookup_name (cp_parser *parser, tree name,
16155                        enum tag_types tag_type,
16156                        bool is_template,
16157                        bool is_namespace,
16158                        bool check_dependency,
16159                        tree *ambiguous_decls)
16160 {
16161   int flags = 0;
16162   tree decl;
16163   tree object_type = parser->context->object_type;
16164
16165   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16166     flags |= LOOKUP_COMPLAIN;
16167
16168   /* Assume that the lookup will be unambiguous.  */
16169   if (ambiguous_decls)
16170     *ambiguous_decls = NULL_TREE;
16171
16172   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16173      no longer valid.  Note that if we are parsing tentatively, and
16174      the parse fails, OBJECT_TYPE will be automatically restored.  */
16175   parser->context->object_type = NULL_TREE;
16176
16177   if (name == error_mark_node)
16178     return error_mark_node;
16179
16180   /* A template-id has already been resolved; there is no lookup to
16181      do.  */
16182   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16183     return name;
16184   if (BASELINK_P (name))
16185     {
16186       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16187                   == TEMPLATE_ID_EXPR);
16188       return name;
16189     }
16190
16191   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
16192      it should already have been checked to make sure that the name
16193      used matches the type being destroyed.  */
16194   if (TREE_CODE (name) == BIT_NOT_EXPR)
16195     {
16196       tree type;
16197
16198       /* Figure out to which type this destructor applies.  */
16199       if (parser->scope)
16200         type = parser->scope;
16201       else if (object_type)
16202         type = object_type;
16203       else
16204         type = current_class_type;
16205       /* If that's not a class type, there is no destructor.  */
16206       if (!type || !CLASS_TYPE_P (type))
16207         return error_mark_node;
16208       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16209         lazily_declare_fn (sfk_destructor, type);
16210       if (!CLASSTYPE_DESTRUCTORS (type))
16211           return error_mark_node;
16212       /* If it was a class type, return the destructor.  */
16213       return CLASSTYPE_DESTRUCTORS (type);
16214     }
16215
16216   /* By this point, the NAME should be an ordinary identifier.  If
16217      the id-expression was a qualified name, the qualifying scope is
16218      stored in PARSER->SCOPE at this point.  */
16219   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16220
16221   /* Perform the lookup.  */
16222   if (parser->scope)
16223     {
16224       bool dependent_p;
16225
16226       if (parser->scope == error_mark_node)
16227         return error_mark_node;
16228
16229       /* If the SCOPE is dependent, the lookup must be deferred until
16230          the template is instantiated -- unless we are explicitly
16231          looking up names in uninstantiated templates.  Even then, we
16232          cannot look up the name if the scope is not a class type; it
16233          might, for example, be a template type parameter.  */
16234       dependent_p = (TYPE_P (parser->scope)
16235                      && !(parser->in_declarator_p
16236                           && currently_open_class (parser->scope))
16237                      && dependent_type_p (parser->scope));
16238       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16239            && dependent_p)
16240         {
16241           if (tag_type)
16242             {
16243               tree type;
16244
16245               /* The resolution to Core Issue 180 says that `struct
16246                  A::B' should be considered a type-name, even if `A'
16247                  is dependent.  */
16248               type = make_typename_type (parser->scope, name, tag_type,
16249                                          /*complain=*/tf_error);
16250               decl = TYPE_NAME (type);
16251             }
16252           else if (is_template
16253                    && (cp_parser_next_token_ends_template_argument_p (parser)
16254                        || cp_lexer_next_token_is (parser->lexer,
16255                                                   CPP_CLOSE_PAREN)))
16256             decl = make_unbound_class_template (parser->scope,
16257                                                 name, NULL_TREE,
16258                                                 /*complain=*/tf_error);
16259           else
16260             decl = build_qualified_name (/*type=*/NULL_TREE,
16261                                          parser->scope, name,
16262                                          is_template);
16263         }
16264       else
16265         {
16266           tree pushed_scope = NULL_TREE;
16267
16268           /* If PARSER->SCOPE is a dependent type, then it must be a
16269              class type, and we must not be checking dependencies;
16270              otherwise, we would have processed this lookup above.  So
16271              that PARSER->SCOPE is not considered a dependent base by
16272              lookup_member, we must enter the scope here.  */
16273           if (dependent_p)
16274             pushed_scope = push_scope (parser->scope);
16275           /* If the PARSER->SCOPE is a template specialization, it
16276              may be instantiated during name lookup.  In that case,
16277              errors may be issued.  Even if we rollback the current
16278              tentative parse, those errors are valid.  */
16279           decl = lookup_qualified_name (parser->scope, name,
16280                                         tag_type != none_type,
16281                                         /*complain=*/true);
16282           if (pushed_scope)
16283             pop_scope (pushed_scope);
16284         }
16285       parser->qualifying_scope = parser->scope;
16286       parser->object_scope = NULL_TREE;
16287     }
16288   else if (object_type)
16289     {
16290       tree object_decl = NULL_TREE;
16291       /* Look up the name in the scope of the OBJECT_TYPE, unless the
16292          OBJECT_TYPE is not a class.  */
16293       if (CLASS_TYPE_P (object_type))
16294         /* If the OBJECT_TYPE is a template specialization, it may
16295            be instantiated during name lookup.  In that case, errors
16296            may be issued.  Even if we rollback the current tentative
16297            parse, those errors are valid.  */
16298         object_decl = lookup_member (object_type,
16299                                      name,
16300                                      /*protect=*/0,
16301                                      tag_type != none_type);
16302       /* Look it up in the enclosing context, too.  */
16303       decl = lookup_name_real (name, tag_type != none_type,
16304                                /*nonclass=*/0,
16305                                /*block_p=*/true, is_namespace, flags);
16306       parser->object_scope = object_type;
16307       parser->qualifying_scope = NULL_TREE;
16308       if (object_decl)
16309         decl = object_decl;
16310     }
16311   else
16312     {
16313       decl = lookup_name_real (name, tag_type != none_type,
16314                                /*nonclass=*/0,
16315                                /*block_p=*/true, is_namespace, flags);
16316       parser->qualifying_scope = NULL_TREE;
16317       parser->object_scope = NULL_TREE;
16318     }
16319
16320   /* If the lookup failed, let our caller know.  */
16321   if (!decl || decl == error_mark_node)
16322     return error_mark_node;
16323
16324   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
16325   if (TREE_CODE (decl) == TREE_LIST)
16326     {
16327       if (ambiguous_decls)
16328         *ambiguous_decls = decl;
16329       /* The error message we have to print is too complicated for
16330          cp_parser_error, so we incorporate its actions directly.  */
16331       if (!cp_parser_simulate_error (parser))
16332         {
16333           error ("reference to %qD is ambiguous", name);
16334           print_candidates (decl);
16335         }
16336       return error_mark_node;
16337     }
16338
16339   gcc_assert (DECL_P (decl)
16340               || TREE_CODE (decl) == OVERLOAD
16341               || TREE_CODE (decl) == SCOPE_REF
16342               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16343               || BASELINK_P (decl));
16344
16345   /* If we have resolved the name of a member declaration, check to
16346      see if the declaration is accessible.  When the name resolves to
16347      set of overloaded functions, accessibility is checked when
16348      overload resolution is done.
16349
16350      During an explicit instantiation, access is not checked at all,
16351      as per [temp.explicit].  */
16352   if (DECL_P (decl))
16353     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16354
16355   return decl;
16356 }
16357
16358 /* Like cp_parser_lookup_name, but for use in the typical case where
16359    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16360    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
16361
16362 static tree
16363 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
16364 {
16365   return cp_parser_lookup_name (parser, name,
16366                                 none_type,
16367                                 /*is_template=*/false,
16368                                 /*is_namespace=*/false,
16369                                 /*check_dependency=*/true,
16370                                 /*ambiguous_decls=*/NULL);
16371 }
16372
16373 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16374    the current context, return the TYPE_DECL.  If TAG_NAME_P is
16375    true, the DECL indicates the class being defined in a class-head,
16376    or declared in an elaborated-type-specifier.
16377
16378    Otherwise, return DECL.  */
16379
16380 static tree
16381 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16382 {
16383   /* If the TEMPLATE_DECL is being declared as part of a class-head,
16384      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16385
16386        struct A {
16387          template <typename T> struct B;
16388        };
16389
16390        template <typename T> struct A::B {};
16391
16392      Similarly, in an elaborated-type-specifier:
16393
16394        namespace N { struct X{}; }
16395
16396        struct A {
16397          template <typename T> friend struct N::X;
16398        };
16399
16400      However, if the DECL refers to a class type, and we are in
16401      the scope of the class, then the name lookup automatically
16402      finds the TYPE_DECL created by build_self_reference rather
16403      than a TEMPLATE_DECL.  For example, in:
16404
16405        template <class T> struct S {
16406          S s;
16407        };
16408
16409      there is no need to handle such case.  */
16410
16411   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16412     return DECL_TEMPLATE_RESULT (decl);
16413
16414   return decl;
16415 }
16416
16417 /* If too many, or too few, template-parameter lists apply to the
16418    declarator, issue an error message.  Returns TRUE if all went well,
16419    and FALSE otherwise.  */
16420
16421 static bool
16422 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16423                                                 cp_declarator *declarator)
16424 {
16425   unsigned num_templates;
16426
16427   /* We haven't seen any classes that involve template parameters yet.  */
16428   num_templates = 0;
16429
16430   switch (declarator->kind)
16431     {
16432     case cdk_id:
16433       if (declarator->u.id.qualifying_scope)
16434         {
16435           tree scope;
16436           tree member;
16437
16438           scope = declarator->u.id.qualifying_scope;
16439           member = declarator->u.id.unqualified_name;
16440
16441           while (scope && CLASS_TYPE_P (scope))
16442             {
16443               /* You're supposed to have one `template <...>'
16444                  for every template class, but you don't need one
16445                  for a full specialization.  For example:
16446
16447                  template <class T> struct S{};
16448                  template <> struct S<int> { void f(); };
16449                  void S<int>::f () {}
16450
16451                  is correct; there shouldn't be a `template <>' for
16452                  the definition of `S<int>::f'.  */
16453               if (!CLASSTYPE_TEMPLATE_INFO (scope))
16454                 /* If SCOPE does not have template information of any
16455                    kind, then it is not a template, nor is it nested
16456                    within a template.  */
16457                 break;
16458               if (explicit_class_specialization_p (scope))
16459                 break;
16460               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16461                 ++num_templates;
16462
16463               scope = TYPE_CONTEXT (scope);
16464             }
16465         }
16466       else if (TREE_CODE (declarator->u.id.unqualified_name)
16467                == TEMPLATE_ID_EXPR)
16468         /* If the DECLARATOR has the form `X<y>' then it uses one
16469            additional level of template parameters.  */
16470         ++num_templates;
16471
16472       return cp_parser_check_template_parameters (parser,
16473                                                   num_templates);
16474
16475     case cdk_function:
16476     case cdk_array:
16477     case cdk_pointer:
16478     case cdk_reference:
16479     case cdk_ptrmem:
16480       return (cp_parser_check_declarator_template_parameters
16481               (parser, declarator->declarator));
16482
16483     case cdk_error:
16484       return true;
16485
16486     default:
16487       gcc_unreachable ();
16488     }
16489   return false;
16490 }
16491
16492 /* NUM_TEMPLATES were used in the current declaration.  If that is
16493    invalid, return FALSE and issue an error messages.  Otherwise,
16494    return TRUE.  */
16495
16496 static bool
16497 cp_parser_check_template_parameters (cp_parser* parser,
16498                                      unsigned num_templates)
16499 {
16500   /* If there are more template classes than parameter lists, we have
16501      something like:
16502
16503        template <class T> void S<T>::R<T>::f ();  */
16504   if (parser->num_template_parameter_lists < num_templates)
16505     {
16506       error ("too few template-parameter-lists");
16507       return false;
16508     }
16509   /* If there are the same number of template classes and parameter
16510      lists, that's OK.  */
16511   if (parser->num_template_parameter_lists == num_templates)
16512     return true;
16513   /* If there are more, but only one more, then we are referring to a
16514      member template.  That's OK too.  */
16515   if (parser->num_template_parameter_lists == num_templates + 1)
16516       return true;
16517   /* Otherwise, there are too many template parameter lists.  We have
16518      something like:
16519
16520      template <class T> template <class U> void S::f();  */
16521   error ("too many template-parameter-lists");
16522   return false;
16523 }
16524
16525 /* Parse an optional `::' token indicating that the following name is
16526    from the global namespace.  If so, PARSER->SCOPE is set to the
16527    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16528    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16529    Returns the new value of PARSER->SCOPE, if the `::' token is
16530    present, and NULL_TREE otherwise.  */
16531
16532 static tree
16533 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16534 {
16535   cp_token *token;
16536
16537   /* Peek at the next token.  */
16538   token = cp_lexer_peek_token (parser->lexer);
16539   /* If we're looking at a `::' token then we're starting from the
16540      global namespace, not our current location.  */
16541   if (token->type == CPP_SCOPE)
16542     {
16543       /* Consume the `::' token.  */
16544       cp_lexer_consume_token (parser->lexer);
16545       /* Set the SCOPE so that we know where to start the lookup.  */
16546       parser->scope = global_namespace;
16547       parser->qualifying_scope = global_namespace;
16548       parser->object_scope = NULL_TREE;
16549
16550       return parser->scope;
16551     }
16552   else if (!current_scope_valid_p)
16553     {
16554       parser->scope = NULL_TREE;
16555       parser->qualifying_scope = NULL_TREE;
16556       parser->object_scope = NULL_TREE;
16557     }
16558
16559   return NULL_TREE;
16560 }
16561
16562 /* Returns TRUE if the upcoming token sequence is the start of a
16563    constructor declarator.  If FRIEND_P is true, the declarator is
16564    preceded by the `friend' specifier.  */
16565
16566 static bool
16567 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16568 {
16569   bool constructor_p;
16570   tree type_decl = NULL_TREE;
16571   bool nested_name_p;
16572   cp_token *next_token;
16573
16574   /* The common case is that this is not a constructor declarator, so
16575      try to avoid doing lots of work if at all possible.  It's not
16576      valid declare a constructor at function scope.  */
16577   if (parser->in_function_body)
16578     return false;
16579   /* And only certain tokens can begin a constructor declarator.  */
16580   next_token = cp_lexer_peek_token (parser->lexer);
16581   if (next_token->type != CPP_NAME
16582       && next_token->type != CPP_SCOPE
16583       && next_token->type != CPP_NESTED_NAME_SPECIFIER
16584       && next_token->type != CPP_TEMPLATE_ID)
16585     return false;
16586
16587   /* Parse tentatively; we are going to roll back all of the tokens
16588      consumed here.  */
16589   cp_parser_parse_tentatively (parser);
16590   /* Assume that we are looking at a constructor declarator.  */
16591   constructor_p = true;
16592
16593   /* Look for the optional `::' operator.  */
16594   cp_parser_global_scope_opt (parser,
16595                               /*current_scope_valid_p=*/false);
16596   /* Look for the nested-name-specifier.  */
16597   nested_name_p
16598     = (cp_parser_nested_name_specifier_opt (parser,
16599                                             /*typename_keyword_p=*/false,
16600                                             /*check_dependency_p=*/false,
16601                                             /*type_p=*/false,
16602                                             /*is_declaration=*/false)
16603        != NULL_TREE);
16604   /* Outside of a class-specifier, there must be a
16605      nested-name-specifier.  */
16606   if (!nested_name_p &&
16607       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16608        || friend_p))
16609     constructor_p = false;
16610   /* If we still think that this might be a constructor-declarator,
16611      look for a class-name.  */
16612   if (constructor_p)
16613     {
16614       /* If we have:
16615
16616            template <typename T> struct S { S(); };
16617            template <typename T> S<T>::S ();
16618
16619          we must recognize that the nested `S' names a class.
16620          Similarly, for:
16621
16622            template <typename T> S<T>::S<T> ();
16623
16624          we must recognize that the nested `S' names a template.  */
16625       type_decl = cp_parser_class_name (parser,
16626                                         /*typename_keyword_p=*/false,
16627                                         /*template_keyword_p=*/false,
16628                                         none_type,
16629                                         /*check_dependency_p=*/false,
16630                                         /*class_head_p=*/false,
16631                                         /*is_declaration=*/false);
16632       /* If there was no class-name, then this is not a constructor.  */
16633       constructor_p = !cp_parser_error_occurred (parser);
16634     }
16635
16636   /* If we're still considering a constructor, we have to see a `(',
16637      to begin the parameter-declaration-clause, followed by either a
16638      `)', an `...', or a decl-specifier.  We need to check for a
16639      type-specifier to avoid being fooled into thinking that:
16640
16641        S::S (f) (int);
16642
16643      is a constructor.  (It is actually a function named `f' that
16644      takes one parameter (of type `int') and returns a value of type
16645      `S::S'.  */
16646   if (constructor_p
16647       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16648     {
16649       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16650           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16651           /* A parameter declaration begins with a decl-specifier,
16652              which is either the "attribute" keyword, a storage class
16653              specifier, or (usually) a type-specifier.  */
16654           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16655         {
16656           tree type;
16657           tree pushed_scope = NULL_TREE;
16658           unsigned saved_num_template_parameter_lists;
16659
16660           /* Names appearing in the type-specifier should be looked up
16661              in the scope of the class.  */
16662           if (current_class_type)
16663             type = NULL_TREE;
16664           else
16665             {
16666               type = TREE_TYPE (type_decl);
16667               if (TREE_CODE (type) == TYPENAME_TYPE)
16668                 {
16669                   type = resolve_typename_type (type,
16670                                                 /*only_current_p=*/false);
16671                   if (TREE_CODE (type) == TYPENAME_TYPE)
16672                     {
16673                       cp_parser_abort_tentative_parse (parser);
16674                       return false;
16675                     }
16676                 }
16677               pushed_scope = push_scope (type);
16678             }
16679
16680           /* Inside the constructor parameter list, surrounding
16681              template-parameter-lists do not apply.  */
16682           saved_num_template_parameter_lists
16683             = parser->num_template_parameter_lists;
16684           parser->num_template_parameter_lists = 0;
16685
16686           /* Look for the type-specifier.  */
16687           cp_parser_type_specifier (parser,
16688                                     CP_PARSER_FLAGS_NONE,
16689                                     /*decl_specs=*/NULL,
16690                                     /*is_declarator=*/true,
16691                                     /*declares_class_or_enum=*/NULL,
16692                                     /*is_cv_qualifier=*/NULL);
16693
16694           parser->num_template_parameter_lists
16695             = saved_num_template_parameter_lists;
16696
16697           /* Leave the scope of the class.  */
16698           if (pushed_scope)
16699             pop_scope (pushed_scope);
16700
16701           constructor_p = !cp_parser_error_occurred (parser);
16702         }
16703     }
16704   else
16705     constructor_p = false;
16706   /* We did not really want to consume any tokens.  */
16707   cp_parser_abort_tentative_parse (parser);
16708
16709   return constructor_p;
16710 }
16711
16712 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16713    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
16714    they must be performed once we are in the scope of the function.
16715
16716    Returns the function defined.  */
16717
16718 static tree
16719 cp_parser_function_definition_from_specifiers_and_declarator
16720   (cp_parser* parser,
16721    cp_decl_specifier_seq *decl_specifiers,
16722    tree attributes,
16723    const cp_declarator *declarator)
16724 {
16725   tree fn;
16726   bool success_p;
16727
16728   /* Begin the function-definition.  */
16729   success_p = start_function (decl_specifiers, declarator, attributes);
16730
16731   /* The things we're about to see are not directly qualified by any
16732      template headers we've seen thus far.  */
16733   reset_specialization ();
16734
16735   /* If there were names looked up in the decl-specifier-seq that we
16736      did not check, check them now.  We must wait until we are in the
16737      scope of the function to perform the checks, since the function
16738      might be a friend.  */
16739   perform_deferred_access_checks ();
16740
16741   if (!success_p)
16742     {
16743       /* Skip the entire function.  */
16744       cp_parser_skip_to_end_of_block_or_statement (parser);
16745       fn = error_mark_node;
16746     }
16747   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16748     {
16749       /* Seen already, skip it.  An error message has already been output.  */
16750       cp_parser_skip_to_end_of_block_or_statement (parser);
16751       fn = current_function_decl;
16752       current_function_decl = NULL_TREE;
16753       /* If this is a function from a class, pop the nested class.  */
16754       if (current_class_name)
16755         pop_nested_class ();
16756     }
16757   else
16758     fn = cp_parser_function_definition_after_declarator (parser,
16759                                                          /*inline_p=*/false);
16760
16761   return fn;
16762 }
16763
16764 /* Parse the part of a function-definition that follows the
16765    declarator.  INLINE_P is TRUE iff this function is an inline
16766    function defined with a class-specifier.
16767
16768    Returns the function defined.  */
16769
16770 static tree
16771 cp_parser_function_definition_after_declarator (cp_parser* parser,
16772                                                 bool inline_p)
16773 {
16774   tree fn;
16775   bool ctor_initializer_p = false;
16776   bool saved_in_unbraced_linkage_specification_p;
16777   bool saved_in_function_body;
16778   unsigned saved_num_template_parameter_lists;
16779
16780   saved_in_function_body = parser->in_function_body;
16781   parser->in_function_body = true;
16782   /* If the next token is `return', then the code may be trying to
16783      make use of the "named return value" extension that G++ used to
16784      support.  */
16785   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16786     {
16787       /* Consume the `return' keyword.  */
16788       cp_lexer_consume_token (parser->lexer);
16789       /* Look for the identifier that indicates what value is to be
16790          returned.  */
16791       cp_parser_identifier (parser);
16792       /* Issue an error message.  */
16793       error ("named return values are no longer supported");
16794       /* Skip tokens until we reach the start of the function body.  */
16795       while (true)
16796         {
16797           cp_token *token = cp_lexer_peek_token (parser->lexer);
16798           if (token->type == CPP_OPEN_BRACE
16799               || token->type == CPP_EOF
16800               || token->type == CPP_PRAGMA_EOL)
16801             break;
16802           cp_lexer_consume_token (parser->lexer);
16803         }
16804     }
16805   /* The `extern' in `extern "C" void f () { ... }' does not apply to
16806      anything declared inside `f'.  */
16807   saved_in_unbraced_linkage_specification_p
16808     = parser->in_unbraced_linkage_specification_p;
16809   parser->in_unbraced_linkage_specification_p = false;
16810   /* Inside the function, surrounding template-parameter-lists do not
16811      apply.  */
16812   saved_num_template_parameter_lists
16813     = parser->num_template_parameter_lists;
16814   parser->num_template_parameter_lists = 0;
16815   /* If the next token is `try', then we are looking at a
16816      function-try-block.  */
16817   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16818     ctor_initializer_p = cp_parser_function_try_block (parser);
16819   /* A function-try-block includes the function-body, so we only do
16820      this next part if we're not processing a function-try-block.  */
16821   else
16822     ctor_initializer_p
16823       = cp_parser_ctor_initializer_opt_and_function_body (parser);
16824
16825   /* Finish the function.  */
16826   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16827                         (inline_p ? 2 : 0));
16828   /* Generate code for it, if necessary.  */
16829   expand_or_defer_fn (fn);
16830   /* Restore the saved values.  */
16831   parser->in_unbraced_linkage_specification_p
16832     = saved_in_unbraced_linkage_specification_p;
16833   parser->num_template_parameter_lists
16834     = saved_num_template_parameter_lists;
16835   parser->in_function_body = saved_in_function_body;
16836
16837   return fn;
16838 }
16839
16840 /* Parse a template-declaration, assuming that the `export' (and
16841    `extern') keywords, if present, has already been scanned.  MEMBER_P
16842    is as for cp_parser_template_declaration.  */
16843
16844 static void
16845 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16846 {
16847   tree decl = NULL_TREE;
16848   VEC (deferred_access_check,gc) *checks;
16849   tree parameter_list;
16850   bool friend_p = false;
16851   bool need_lang_pop;
16852
16853   /* Look for the `template' keyword.  */
16854   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16855     return;
16856
16857   /* And the `<'.  */
16858   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16859     return;
16860   if (at_class_scope_p () && current_function_decl)
16861     {
16862       /* 14.5.2.2 [temp.mem]
16863
16864          A local class shall not have member templates.  */
16865       error ("invalid declaration of member template in local class");
16866       cp_parser_skip_to_end_of_block_or_statement (parser);
16867       return;
16868     }
16869   /* [temp]
16870
16871      A template ... shall not have C linkage.  */
16872   if (current_lang_name == lang_name_c)
16873     {
16874       error ("template with C linkage");
16875       /* Give it C++ linkage to avoid confusing other parts of the
16876          front end.  */
16877       push_lang_context (lang_name_cplusplus);
16878       need_lang_pop = true;
16879     }
16880   else
16881     need_lang_pop = false;
16882
16883   /* We cannot perform access checks on the template parameter
16884      declarations until we know what is being declared, just as we
16885      cannot check the decl-specifier list.  */
16886   push_deferring_access_checks (dk_deferred);
16887
16888   /* If the next token is `>', then we have an invalid
16889      specialization.  Rather than complain about an invalid template
16890      parameter, issue an error message here.  */
16891   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16892     {
16893       cp_parser_error (parser, "invalid explicit specialization");
16894       begin_specialization ();
16895       parameter_list = NULL_TREE;
16896     }
16897   else
16898     /* Parse the template parameters.  */
16899     parameter_list = cp_parser_template_parameter_list (parser);
16900
16901   /* Get the deferred access checks from the parameter list.  These
16902      will be checked once we know what is being declared, as for a
16903      member template the checks must be performed in the scope of the
16904      class containing the member.  */
16905   checks = get_deferred_access_checks ();
16906
16907   /* Look for the `>'.  */
16908   cp_parser_skip_to_end_of_template_parameter_list (parser);
16909   /* We just processed one more parameter list.  */
16910   ++parser->num_template_parameter_lists;
16911   /* If the next token is `template', there are more template
16912      parameters.  */
16913   if (cp_lexer_next_token_is_keyword (parser->lexer,
16914                                       RID_TEMPLATE))
16915     cp_parser_template_declaration_after_export (parser, member_p);
16916   else
16917     {
16918       /* There are no access checks when parsing a template, as we do not
16919          know if a specialization will be a friend.  */
16920       push_deferring_access_checks (dk_no_check);
16921       decl = cp_parser_single_declaration (parser,
16922                                            checks,
16923                                            member_p,
16924                                            /*explicit_specialization_p=*/false,
16925                                            &friend_p);
16926       pop_deferring_access_checks ();
16927
16928       /* If this is a member template declaration, let the front
16929          end know.  */
16930       if (member_p && !friend_p && decl)
16931         {
16932           if (TREE_CODE (decl) == TYPE_DECL)
16933             cp_parser_check_access_in_redeclaration (decl);
16934
16935           decl = finish_member_template_decl (decl);
16936         }
16937       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16938         make_friend_class (current_class_type, TREE_TYPE (decl),
16939                            /*complain=*/true);
16940     }
16941   /* We are done with the current parameter list.  */
16942   --parser->num_template_parameter_lists;
16943
16944   pop_deferring_access_checks ();
16945
16946   /* Finish up.  */
16947   finish_template_decl (parameter_list);
16948
16949   /* Register member declarations.  */
16950   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16951     finish_member_declaration (decl);
16952   /* For the erroneous case of a template with C linkage, we pushed an
16953      implicit C++ linkage scope; exit that scope now.  */
16954   if (need_lang_pop)
16955     pop_lang_context ();
16956   /* If DECL is a function template, we must return to parse it later.
16957      (Even though there is no definition, there might be default
16958      arguments that need handling.)  */
16959   if (member_p && decl
16960       && (TREE_CODE (decl) == FUNCTION_DECL
16961           || DECL_FUNCTION_TEMPLATE_P (decl)))
16962     TREE_VALUE (parser->unparsed_functions_queues)
16963       = tree_cons (NULL_TREE, decl,
16964                    TREE_VALUE (parser->unparsed_functions_queues));
16965 }
16966
16967 /* Perform the deferred access checks from a template-parameter-list.
16968    CHECKS is a TREE_LIST of access checks, as returned by
16969    get_deferred_access_checks.  */
16970
16971 static void
16972 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16973 {
16974   ++processing_template_parmlist;
16975   perform_access_checks (checks);
16976   --processing_template_parmlist;
16977 }
16978
16979 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16980    `function-definition' sequence.  MEMBER_P is true, this declaration
16981    appears in a class scope.
16982
16983    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
16984    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
16985
16986 static tree
16987 cp_parser_single_declaration (cp_parser* parser,
16988                               VEC (deferred_access_check,gc)* checks,
16989                               bool member_p,
16990                               bool explicit_specialization_p,
16991                               bool* friend_p)
16992 {
16993   int declares_class_or_enum;
16994   tree decl = NULL_TREE;
16995   cp_decl_specifier_seq decl_specifiers;
16996   bool function_definition_p = false;
16997
16998   /* This function is only used when processing a template
16999      declaration.  */
17000   gcc_assert (innermost_scope_kind () == sk_template_parms
17001               || innermost_scope_kind () == sk_template_spec);
17002
17003   /* Defer access checks until we know what is being declared.  */
17004   push_deferring_access_checks (dk_deferred);
17005
17006   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17007      alternative.  */
17008   cp_parser_decl_specifier_seq (parser,
17009                                 CP_PARSER_FLAGS_OPTIONAL,
17010                                 &decl_specifiers,
17011                                 &declares_class_or_enum);
17012   if (friend_p)
17013     *friend_p = cp_parser_friend_p (&decl_specifiers);
17014
17015   /* There are no template typedefs.  */
17016   if (decl_specifiers.specs[(int) ds_typedef])
17017     {
17018       error ("template declaration of %qs", "typedef");
17019       decl = error_mark_node;
17020     }
17021
17022   /* Gather up the access checks that occurred the
17023      decl-specifier-seq.  */
17024   stop_deferring_access_checks ();
17025
17026   /* Check for the declaration of a template class.  */
17027   if (declares_class_or_enum)
17028     {
17029       if (cp_parser_declares_only_class_p (parser))
17030         {
17031           decl = shadow_tag (&decl_specifiers);
17032
17033           /* In this case:
17034
17035                struct C {
17036                  friend template <typename T> struct A<T>::B;
17037                };
17038
17039              A<T>::B will be represented by a TYPENAME_TYPE, and
17040              therefore not recognized by shadow_tag.  */
17041           if (friend_p && *friend_p
17042               && !decl
17043               && decl_specifiers.type
17044               && TYPE_P (decl_specifiers.type))
17045             decl = decl_specifiers.type;
17046
17047           if (decl && decl != error_mark_node)
17048             decl = TYPE_NAME (decl);
17049           else
17050             decl = error_mark_node;
17051
17052           /* Perform access checks for template parameters.  */
17053           cp_parser_perform_template_parameter_access_checks (checks);
17054         }
17055     }
17056   /* If it's not a template class, try for a template function.  If
17057      the next token is a `;', then this declaration does not declare
17058      anything.  But, if there were errors in the decl-specifiers, then
17059      the error might well have come from an attempted class-specifier.
17060      In that case, there's no need to warn about a missing declarator.  */
17061   if (!decl
17062       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17063           || decl_specifiers.type != error_mark_node))
17064     {
17065       decl = cp_parser_init_declarator (parser,
17066                                         &decl_specifiers,
17067                                         checks,
17068                                         /*function_definition_allowed_p=*/true,
17069                                         member_p,
17070                                         declares_class_or_enum,
17071                                         &function_definition_p);
17072
17073     /* 7.1.1-1 [dcl.stc]
17074
17075        A storage-class-specifier shall not be specified in an explicit
17076        specialization...  */
17077     if (decl
17078         && explicit_specialization_p
17079         && decl_specifiers.storage_class != sc_none)
17080       {
17081         error ("explicit template specialization cannot have a storage class");
17082         decl = error_mark_node;
17083       }
17084     }
17085
17086   pop_deferring_access_checks ();
17087
17088   /* Clear any current qualification; whatever comes next is the start
17089      of something new.  */
17090   parser->scope = NULL_TREE;
17091   parser->qualifying_scope = NULL_TREE;
17092   parser->object_scope = NULL_TREE;
17093   /* Look for a trailing `;' after the declaration.  */
17094   if (!function_definition_p
17095       && (decl == error_mark_node
17096           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
17097     cp_parser_skip_to_end_of_block_or_statement (parser);
17098
17099   return decl;
17100 }
17101
17102 /* Parse a cast-expression that is not the operand of a unary "&".  */
17103
17104 static tree
17105 cp_parser_simple_cast_expression (cp_parser *parser)
17106 {
17107   return cp_parser_cast_expression (parser, /*address_p=*/false,
17108                                     /*cast_p=*/false);
17109 }
17110
17111 /* Parse a functional cast to TYPE.  Returns an expression
17112    representing the cast.  */
17113
17114 static tree
17115 cp_parser_functional_cast (cp_parser* parser, tree type)
17116 {
17117   tree expression_list;
17118   tree cast;
17119
17120   expression_list
17121     = cp_parser_parenthesized_expression_list (parser, false,
17122                                                /*cast_p=*/true,
17123                                                /*allow_expansion_p=*/true,
17124                                                /*non_constant_p=*/NULL);
17125
17126   cast = build_functional_cast (type, expression_list);
17127   /* [expr.const]/1: In an integral constant expression "only type
17128      conversions to integral or enumeration type can be used".  */
17129   if (TREE_CODE (type) == TYPE_DECL)
17130     type = TREE_TYPE (type);
17131   if (cast != error_mark_node
17132       && !cast_valid_in_integral_constant_expression_p (type)
17133       && (cp_parser_non_integral_constant_expression
17134           (parser, "a call to a constructor")))
17135     return error_mark_node;
17136   return cast;
17137 }
17138
17139 /* Save the tokens that make up the body of a member function defined
17140    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
17141    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
17142    specifiers applied to the declaration.  Returns the FUNCTION_DECL
17143    for the member function.  */
17144
17145 static tree
17146 cp_parser_save_member_function_body (cp_parser* parser,
17147                                      cp_decl_specifier_seq *decl_specifiers,
17148                                      cp_declarator *declarator,
17149                                      tree attributes)
17150 {
17151   cp_token *first;
17152   cp_token *last;
17153   tree fn;
17154
17155   /* Create the function-declaration.  */
17156   fn = start_method (decl_specifiers, declarator, attributes);
17157   /* If something went badly wrong, bail out now.  */
17158   if (fn == error_mark_node)
17159     {
17160       /* If there's a function-body, skip it.  */
17161       if (cp_parser_token_starts_function_definition_p
17162           (cp_lexer_peek_token (parser->lexer)))
17163         cp_parser_skip_to_end_of_block_or_statement (parser);
17164       return error_mark_node;
17165     }
17166
17167   /* Remember it, if there default args to post process.  */
17168   cp_parser_save_default_args (parser, fn);
17169
17170   /* Save away the tokens that make up the body of the
17171      function.  */
17172   first = parser->lexer->next_token;
17173   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17174   /* Handle function try blocks.  */
17175   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17176     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17177   last = parser->lexer->next_token;
17178
17179   /* Save away the inline definition; we will process it when the
17180      class is complete.  */
17181   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17182   DECL_PENDING_INLINE_P (fn) = 1;
17183
17184   /* We need to know that this was defined in the class, so that
17185      friend templates are handled correctly.  */
17186   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17187
17188   /* We're done with the inline definition.  */
17189   finish_method (fn);
17190
17191   /* Add FN to the queue of functions to be parsed later.  */
17192   TREE_VALUE (parser->unparsed_functions_queues)
17193     = tree_cons (NULL_TREE, fn,
17194                  TREE_VALUE (parser->unparsed_functions_queues));
17195
17196   return fn;
17197 }
17198
17199 /* Parse a template-argument-list, as well as the trailing ">" (but
17200    not the opening ">").  See cp_parser_template_argument_list for the
17201    return value.  */
17202
17203 static tree
17204 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17205 {
17206   tree arguments;
17207   tree saved_scope;
17208   tree saved_qualifying_scope;
17209   tree saved_object_scope;
17210   bool saved_greater_than_is_operator_p;
17211   bool saved_skip_evaluation;
17212
17213   /* [temp.names]
17214
17215      When parsing a template-id, the first non-nested `>' is taken as
17216      the end of the template-argument-list rather than a greater-than
17217      operator.  */
17218   saved_greater_than_is_operator_p
17219     = parser->greater_than_is_operator_p;
17220   parser->greater_than_is_operator_p = false;
17221   /* Parsing the argument list may modify SCOPE, so we save it
17222      here.  */
17223   saved_scope = parser->scope;
17224   saved_qualifying_scope = parser->qualifying_scope;
17225   saved_object_scope = parser->object_scope;
17226   /* We need to evaluate the template arguments, even though this
17227      template-id may be nested within a "sizeof".  */
17228   saved_skip_evaluation = skip_evaluation;
17229   skip_evaluation = false;
17230   /* Parse the template-argument-list itself.  */
17231   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17232       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17233     arguments = NULL_TREE;
17234   else
17235     arguments = cp_parser_template_argument_list (parser);
17236   /* Look for the `>' that ends the template-argument-list. If we find
17237      a '>>' instead, it's probably just a typo.  */
17238   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17239     {
17240       if (cxx_dialect != cxx98)
17241         {
17242           /* In C++0x, a `>>' in a template argument list or cast
17243              expression is considered to be two separate `>'
17244              tokens. So, change the current token to a `>', but don't
17245              consume it: it will be consumed later when the outer
17246              template argument list (or cast expression) is parsed.
17247              Note that this replacement of `>' for `>>' is necessary
17248              even if we are parsing tentatively: in the tentative
17249              case, after calling
17250              cp_parser_enclosed_template_argument_list we will always
17251              throw away all of the template arguments and the first
17252              closing `>', either because the template argument list
17253              was erroneous or because we are replacing those tokens
17254              with a CPP_TEMPLATE_ID token.  The second `>' (which will
17255              not have been thrown away) is needed either to close an
17256              outer template argument list or to complete a new-style
17257              cast.  */
17258           cp_token *token = cp_lexer_peek_token (parser->lexer);
17259           token->type = CPP_GREATER;
17260         }
17261       else if (!saved_greater_than_is_operator_p)
17262         {
17263           /* If we're in a nested template argument list, the '>>' has
17264             to be a typo for '> >'. We emit the error message, but we
17265             continue parsing and we push a '>' as next token, so that
17266             the argument list will be parsed correctly.  Note that the
17267             global source location is still on the token before the
17268             '>>', so we need to say explicitly where we want it.  */
17269           cp_token *token = cp_lexer_peek_token (parser->lexer);
17270           error ("%H%<>>%> should be %<> >%> "
17271                  "within a nested template argument list",
17272                  &token->location);
17273
17274           token->type = CPP_GREATER;
17275         }
17276       else
17277         {
17278           /* If this is not a nested template argument list, the '>>'
17279             is a typo for '>'. Emit an error message and continue.
17280             Same deal about the token location, but here we can get it
17281             right by consuming the '>>' before issuing the diagnostic.  */
17282           cp_lexer_consume_token (parser->lexer);
17283           error ("spurious %<>>%>, use %<>%> to terminate "
17284                  "a template argument list");
17285         }
17286     }
17287   else
17288     cp_parser_skip_to_end_of_template_parameter_list (parser);
17289   /* The `>' token might be a greater-than operator again now.  */
17290   parser->greater_than_is_operator_p
17291     = saved_greater_than_is_operator_p;
17292   /* Restore the SAVED_SCOPE.  */
17293   parser->scope = saved_scope;
17294   parser->qualifying_scope = saved_qualifying_scope;
17295   parser->object_scope = saved_object_scope;
17296   skip_evaluation = saved_skip_evaluation;
17297
17298   return arguments;
17299 }
17300
17301 /* MEMBER_FUNCTION is a member function, or a friend.  If default
17302    arguments, or the body of the function have not yet been parsed,
17303    parse them now.  */
17304
17305 static void
17306 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17307 {
17308   /* If this member is a template, get the underlying
17309      FUNCTION_DECL.  */
17310   if (DECL_FUNCTION_TEMPLATE_P (member_function))
17311     member_function = DECL_TEMPLATE_RESULT (member_function);
17312
17313   /* There should not be any class definitions in progress at this
17314      point; the bodies of members are only parsed outside of all class
17315      definitions.  */
17316   gcc_assert (parser->num_classes_being_defined == 0);
17317   /* While we're parsing the member functions we might encounter more
17318      classes.  We want to handle them right away, but we don't want
17319      them getting mixed up with functions that are currently in the
17320      queue.  */
17321   parser->unparsed_functions_queues
17322     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17323
17324   /* Make sure that any template parameters are in scope.  */
17325   maybe_begin_member_template_processing (member_function);
17326
17327   /* If the body of the function has not yet been parsed, parse it
17328      now.  */
17329   if (DECL_PENDING_INLINE_P (member_function))
17330     {
17331       tree function_scope;
17332       cp_token_cache *tokens;
17333
17334       /* The function is no longer pending; we are processing it.  */
17335       tokens = DECL_PENDING_INLINE_INFO (member_function);
17336       DECL_PENDING_INLINE_INFO (member_function) = NULL;
17337       DECL_PENDING_INLINE_P (member_function) = 0;
17338
17339       /* If this is a local class, enter the scope of the containing
17340          function.  */
17341       function_scope = current_function_decl;
17342       if (function_scope)
17343         push_function_context_to (function_scope);
17344
17345
17346       /* Push the body of the function onto the lexer stack.  */
17347       cp_parser_push_lexer_for_tokens (parser, tokens);
17348
17349       /* Let the front end know that we going to be defining this
17350          function.  */
17351       start_preparsed_function (member_function, NULL_TREE,
17352                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
17353
17354       /* Don't do access checking if it is a templated function.  */
17355       if (processing_template_decl)
17356         push_deferring_access_checks (dk_no_check);
17357
17358       /* Now, parse the body of the function.  */
17359       cp_parser_function_definition_after_declarator (parser,
17360                                                       /*inline_p=*/true);
17361
17362       if (processing_template_decl)
17363         pop_deferring_access_checks ();
17364
17365       /* Leave the scope of the containing function.  */
17366       if (function_scope)
17367         pop_function_context_from (function_scope);
17368       cp_parser_pop_lexer (parser);
17369     }
17370
17371   /* Remove any template parameters from the symbol table.  */
17372   maybe_end_member_template_processing ();
17373
17374   /* Restore the queue.  */
17375   parser->unparsed_functions_queues
17376     = TREE_CHAIN (parser->unparsed_functions_queues);
17377 }
17378
17379 /* If DECL contains any default args, remember it on the unparsed
17380    functions queue.  */
17381
17382 static void
17383 cp_parser_save_default_args (cp_parser* parser, tree decl)
17384 {
17385   tree probe;
17386
17387   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17388        probe;
17389        probe = TREE_CHAIN (probe))
17390     if (TREE_PURPOSE (probe))
17391       {
17392         TREE_PURPOSE (parser->unparsed_functions_queues)
17393           = tree_cons (current_class_type, decl,
17394                        TREE_PURPOSE (parser->unparsed_functions_queues));
17395         break;
17396       }
17397 }
17398
17399 /* FN is a FUNCTION_DECL which may contains a parameter with an
17400    unparsed DEFAULT_ARG.  Parse the default args now.  This function
17401    assumes that the current scope is the scope in which the default
17402    argument should be processed.  */
17403
17404 static void
17405 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17406 {
17407   bool saved_local_variables_forbidden_p;
17408   tree parm;
17409
17410   /* While we're parsing the default args, we might (due to the
17411      statement expression extension) encounter more classes.  We want
17412      to handle them right away, but we don't want them getting mixed
17413      up with default args that are currently in the queue.  */
17414   parser->unparsed_functions_queues
17415     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17416
17417   /* Local variable names (and the `this' keyword) may not appear
17418      in a default argument.  */
17419   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17420   parser->local_variables_forbidden_p = true;
17421
17422   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17423        parm;
17424        parm = TREE_CHAIN (parm))
17425     {
17426       cp_token_cache *tokens;
17427       tree default_arg = TREE_PURPOSE (parm);
17428       tree parsed_arg;
17429       VEC(tree,gc) *insts;
17430       tree copy;
17431       unsigned ix;
17432
17433       if (!default_arg)
17434         continue;
17435
17436       if (TREE_CODE (default_arg) != DEFAULT_ARG)
17437         /* This can happen for a friend declaration for a function
17438            already declared with default arguments.  */
17439         continue;
17440
17441        /* Push the saved tokens for the default argument onto the parser's
17442           lexer stack.  */
17443       tokens = DEFARG_TOKENS (default_arg);
17444       cp_parser_push_lexer_for_tokens (parser, tokens);
17445
17446       /* Parse the assignment-expression.  */
17447       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17448
17449       if (!processing_template_decl)
17450         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17451
17452       TREE_PURPOSE (parm) = parsed_arg;
17453
17454       /* Update any instantiations we've already created.  */
17455       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17456            VEC_iterate (tree, insts, ix, copy); ix++)
17457         TREE_PURPOSE (copy) = parsed_arg;
17458
17459       /* If the token stream has not been completely used up, then
17460          there was extra junk after the end of the default
17461          argument.  */
17462       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17463         cp_parser_error (parser, "expected %<,%>");
17464
17465       /* Revert to the main lexer.  */
17466       cp_parser_pop_lexer (parser);
17467     }
17468
17469   /* Make sure no default arg is missing.  */
17470   check_default_args (fn);
17471
17472   /* Restore the state of local_variables_forbidden_p.  */
17473   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17474
17475   /* Restore the queue.  */
17476   parser->unparsed_functions_queues
17477     = TREE_CHAIN (parser->unparsed_functions_queues);
17478 }
17479
17480 /* Parse the operand of `sizeof' (or a similar operator).  Returns
17481    either a TYPE or an expression, depending on the form of the
17482    input.  The KEYWORD indicates which kind of expression we have
17483    encountered.  */
17484
17485 static tree
17486 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17487 {
17488   static const char *format;
17489   tree expr = NULL_TREE;
17490   const char *saved_message;
17491   char *tmp;
17492   bool saved_integral_constant_expression_p;
17493   bool saved_non_integral_constant_expression_p;
17494   bool pack_expansion_p = false;
17495
17496   /* Initialize FORMAT the first time we get here.  */
17497   if (!format)
17498     format = "types may not be defined in '%s' expressions";
17499
17500   /* Types cannot be defined in a `sizeof' expression.  Save away the
17501      old message.  */
17502   saved_message = parser->type_definition_forbidden_message;
17503   /* And create the new one.  */
17504   parser->type_definition_forbidden_message = tmp
17505     = XNEWVEC (char, strlen (format)
17506                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17507                + 1 /* `\0' */);
17508   sprintf (tmp, format, IDENTIFIER_POINTER (ridpointers[keyword]));
17509
17510   /* The restrictions on constant-expressions do not apply inside
17511      sizeof expressions.  */
17512   saved_integral_constant_expression_p
17513     = parser->integral_constant_expression_p;
17514   saved_non_integral_constant_expression_p
17515     = parser->non_integral_constant_expression_p;
17516   parser->integral_constant_expression_p = false;
17517
17518   /* If it's a `...', then we are computing the length of a parameter
17519      pack.  */
17520   if (keyword == RID_SIZEOF
17521       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17522     {
17523       /* Consume the `...'.  */
17524       cp_lexer_consume_token (parser->lexer);
17525       maybe_warn_variadic_templates ();
17526
17527       /* Note that this is an expansion.  */
17528       pack_expansion_p = true;
17529     }
17530
17531   /* Do not actually evaluate the expression.  */
17532   ++skip_evaluation;
17533   /* If it's a `(', then we might be looking at the type-id
17534      construction.  */
17535   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17536     {
17537       tree type;
17538       bool saved_in_type_id_in_expr_p;
17539
17540       /* We can't be sure yet whether we're looking at a type-id or an
17541          expression.  */
17542       cp_parser_parse_tentatively (parser);
17543       /* Consume the `('.  */
17544       cp_lexer_consume_token (parser->lexer);
17545       /* Parse the type-id.  */
17546       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17547       parser->in_type_id_in_expr_p = true;
17548       type = cp_parser_type_id (parser);
17549       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17550       /* Now, look for the trailing `)'.  */
17551       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17552       /* If all went well, then we're done.  */
17553       if (cp_parser_parse_definitely (parser))
17554         {
17555           cp_decl_specifier_seq decl_specs;
17556
17557           /* Build a trivial decl-specifier-seq.  */
17558           clear_decl_specs (&decl_specs);
17559           decl_specs.type = type;
17560
17561           /* Call grokdeclarator to figure out what type this is.  */
17562           expr = grokdeclarator (NULL,
17563                                  &decl_specs,
17564                                  TYPENAME,
17565                                  /*initialized=*/0,
17566                                  /*attrlist=*/NULL);
17567         }
17568     }
17569
17570   /* If the type-id production did not work out, then we must be
17571      looking at the unary-expression production.  */
17572   if (!expr)
17573     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17574                                        /*cast_p=*/false);
17575
17576   if (pack_expansion_p)
17577     /* Build a pack expansion. */
17578     expr = make_pack_expansion (expr);
17579
17580   /* Go back to evaluating expressions.  */
17581   --skip_evaluation;
17582
17583   /* Free the message we created.  */
17584   free (tmp);
17585   /* And restore the old one.  */
17586   parser->type_definition_forbidden_message = saved_message;
17587   parser->integral_constant_expression_p
17588     = saved_integral_constant_expression_p;
17589   parser->non_integral_constant_expression_p
17590     = saved_non_integral_constant_expression_p;
17591
17592   return expr;
17593 }
17594
17595 /* If the current declaration has no declarator, return true.  */
17596
17597 static bool
17598 cp_parser_declares_only_class_p (cp_parser *parser)
17599 {
17600   /* If the next token is a `;' or a `,' then there is no
17601      declarator.  */
17602   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17603           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17604 }
17605
17606 /* Update the DECL_SPECS to reflect the storage class indicated by
17607    KEYWORD.  */
17608
17609 static void
17610 cp_parser_set_storage_class (cp_parser *parser,
17611                              cp_decl_specifier_seq *decl_specs,
17612                              enum rid keyword)
17613 {
17614   cp_storage_class storage_class;
17615
17616   if (parser->in_unbraced_linkage_specification_p)
17617     {
17618       error ("invalid use of %qD in linkage specification",
17619              ridpointers[keyword]);
17620       return;
17621     }
17622   else if (decl_specs->storage_class != sc_none)
17623     {
17624       decl_specs->conflicting_specifiers_p = true;
17625       return;
17626     }
17627
17628   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17629       && decl_specs->specs[(int) ds_thread])
17630     {
17631       error ("%<__thread%> before %qD", ridpointers[keyword]);
17632       decl_specs->specs[(int) ds_thread] = 0;
17633     }
17634
17635   switch (keyword)
17636     {
17637     case RID_AUTO:
17638       storage_class = sc_auto;
17639       break;
17640     case RID_REGISTER:
17641       storage_class = sc_register;
17642       break;
17643     case RID_STATIC:
17644       storage_class = sc_static;
17645       break;
17646     case RID_EXTERN:
17647       storage_class = sc_extern;
17648       break;
17649     case RID_MUTABLE:
17650       storage_class = sc_mutable;
17651       break;
17652     default:
17653       gcc_unreachable ();
17654     }
17655   decl_specs->storage_class = storage_class;
17656
17657   /* A storage class specifier cannot be applied alongside a typedef 
17658      specifier. If there is a typedef specifier present then set 
17659      conflicting_specifiers_p which will trigger an error later
17660      on in grokdeclarator. */
17661   if (decl_specs->specs[(int)ds_typedef])
17662     decl_specs->conflicting_specifiers_p = true;
17663 }
17664
17665 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
17666    is true, the type is a user-defined type; otherwise it is a
17667    built-in type specified by a keyword.  */
17668
17669 static void
17670 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17671                               tree type_spec,
17672                               bool user_defined_p)
17673 {
17674   decl_specs->any_specifiers_p = true;
17675
17676   /* If the user tries to redeclare bool or wchar_t (with, for
17677      example, in "typedef int wchar_t;") we remember that this is what
17678      happened.  In system headers, we ignore these declarations so
17679      that G++ can work with system headers that are not C++-safe.  */
17680   if (decl_specs->specs[(int) ds_typedef]
17681       && !user_defined_p
17682       && (type_spec == boolean_type_node
17683           || type_spec == wchar_type_node)
17684       && (decl_specs->type
17685           || decl_specs->specs[(int) ds_long]
17686           || decl_specs->specs[(int) ds_short]
17687           || decl_specs->specs[(int) ds_unsigned]
17688           || decl_specs->specs[(int) ds_signed]))
17689     {
17690       decl_specs->redefined_builtin_type = type_spec;
17691       if (!decl_specs->type)
17692         {
17693           decl_specs->type = type_spec;
17694           decl_specs->user_defined_type_p = false;
17695         }
17696     }
17697   else if (decl_specs->type)
17698     decl_specs->multiple_types_p = true;
17699   else
17700     {
17701       decl_specs->type = type_spec;
17702       decl_specs->user_defined_type_p = user_defined_p;
17703       decl_specs->redefined_builtin_type = NULL_TREE;
17704     }
17705 }
17706
17707 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17708    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
17709
17710 static bool
17711 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17712 {
17713   return decl_specifiers->specs[(int) ds_friend] != 0;
17714 }
17715
17716 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
17717    issue an error message indicating that TOKEN_DESC was expected.
17718
17719    Returns the token consumed, if the token had the appropriate type.
17720    Otherwise, returns NULL.  */
17721
17722 static cp_token *
17723 cp_parser_require (cp_parser* parser,
17724                    enum cpp_ttype type,
17725                    const char* token_desc)
17726 {
17727   if (cp_lexer_next_token_is (parser->lexer, type))
17728     return cp_lexer_consume_token (parser->lexer);
17729   else
17730     {
17731       /* Output the MESSAGE -- unless we're parsing tentatively.  */
17732       if (!cp_parser_simulate_error (parser))
17733         {
17734           char *message = concat ("expected ", token_desc, NULL);
17735           cp_parser_error (parser, message);
17736           free (message);
17737         }
17738       return NULL;
17739     }
17740 }
17741
17742 /* An error message is produced if the next token is not '>'.
17743    All further tokens are skipped until the desired token is
17744    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
17745
17746 static void
17747 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17748 {
17749   /* Current level of '< ... >'.  */
17750   unsigned level = 0;
17751   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
17752   unsigned nesting_depth = 0;
17753
17754   /* Are we ready, yet?  If not, issue error message.  */
17755   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17756     return;
17757
17758   /* Skip tokens until the desired token is found.  */
17759   while (true)
17760     {
17761       /* Peek at the next token.  */
17762       switch (cp_lexer_peek_token (parser->lexer)->type)
17763         {
17764         case CPP_LESS:
17765           if (!nesting_depth)
17766             ++level;
17767           break;
17768
17769         case CPP_RSHIFT:
17770           if (cxx_dialect == cxx98)
17771             /* C++0x views the `>>' operator as two `>' tokens, but
17772                C++98 does not. */
17773             break;
17774           else if (!nesting_depth && level-- == 0)
17775             {
17776               /* We've hit a `>>' where the first `>' closes the
17777                  template argument list, and the second `>' is
17778                  spurious.  Just consume the `>>' and stop; we've
17779                  already produced at least one error.  */
17780               cp_lexer_consume_token (parser->lexer);
17781               return;
17782             }
17783           /* Fall through for C++0x, so we handle the second `>' in
17784              the `>>'.  */
17785
17786         case CPP_GREATER:
17787           if (!nesting_depth && level-- == 0)
17788             {
17789               /* We've reached the token we want, consume it and stop.  */
17790               cp_lexer_consume_token (parser->lexer);
17791               return;
17792             }
17793           break;
17794
17795         case CPP_OPEN_PAREN:
17796         case CPP_OPEN_SQUARE:
17797           ++nesting_depth;
17798           break;
17799
17800         case CPP_CLOSE_PAREN:
17801         case CPP_CLOSE_SQUARE:
17802           if (nesting_depth-- == 0)
17803             return;
17804           break;
17805
17806         case CPP_EOF:
17807         case CPP_PRAGMA_EOL:
17808         case CPP_SEMICOLON:
17809         case CPP_OPEN_BRACE:
17810         case CPP_CLOSE_BRACE:
17811           /* The '>' was probably forgotten, don't look further.  */
17812           return;
17813
17814         default:
17815           break;
17816         }
17817
17818       /* Consume this token.  */
17819       cp_lexer_consume_token (parser->lexer);
17820     }
17821 }
17822
17823 /* If the next token is the indicated keyword, consume it.  Otherwise,
17824    issue an error message indicating that TOKEN_DESC was expected.
17825
17826    Returns the token consumed, if the token had the appropriate type.
17827    Otherwise, returns NULL.  */
17828
17829 static cp_token *
17830 cp_parser_require_keyword (cp_parser* parser,
17831                            enum rid keyword,
17832                            const char* token_desc)
17833 {
17834   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17835
17836   if (token && token->keyword != keyword)
17837     {
17838       dyn_string_t error_msg;
17839
17840       /* Format the error message.  */
17841       error_msg = dyn_string_new (0);
17842       dyn_string_append_cstr (error_msg, "expected ");
17843       dyn_string_append_cstr (error_msg, token_desc);
17844       cp_parser_error (parser, error_msg->s);
17845       dyn_string_delete (error_msg);
17846       return NULL;
17847     }
17848
17849   return token;
17850 }
17851
17852 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17853    function-definition.  */
17854
17855 static bool
17856 cp_parser_token_starts_function_definition_p (cp_token* token)
17857 {
17858   return (/* An ordinary function-body begins with an `{'.  */
17859           token->type == CPP_OPEN_BRACE
17860           /* A ctor-initializer begins with a `:'.  */
17861           || token->type == CPP_COLON
17862           /* A function-try-block begins with `try'.  */
17863           || token->keyword == RID_TRY
17864           /* The named return value extension begins with `return'.  */
17865           || token->keyword == RID_RETURN);
17866 }
17867
17868 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17869    definition.  */
17870
17871 static bool
17872 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17873 {
17874   cp_token *token;
17875
17876   token = cp_lexer_peek_token (parser->lexer);
17877   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17878 }
17879
17880 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17881    C++0x) ending a template-argument.  */
17882
17883 static bool
17884 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17885 {
17886   cp_token *token;
17887
17888   token = cp_lexer_peek_token (parser->lexer);
17889   return (token->type == CPP_COMMA 
17890           || token->type == CPP_GREATER
17891           || token->type == CPP_ELLIPSIS
17892           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
17893 }
17894
17895 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17896    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
17897
17898 static bool
17899 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17900                                                      size_t n)
17901 {
17902   cp_token *token;
17903
17904   token = cp_lexer_peek_nth_token (parser->lexer, n);
17905   if (token->type == CPP_LESS)
17906     return true;
17907   /* Check for the sequence `<::' in the original code. It would be lexed as
17908      `[:', where `[' is a digraph, and there is no whitespace before
17909      `:'.  */
17910   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17911     {
17912       cp_token *token2;
17913       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17914       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17915         return true;
17916     }
17917   return false;
17918 }
17919
17920 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17921    or none_type otherwise.  */
17922
17923 static enum tag_types
17924 cp_parser_token_is_class_key (cp_token* token)
17925 {
17926   switch (token->keyword)
17927     {
17928     case RID_CLASS:
17929       return class_type;
17930     case RID_STRUCT:
17931       return record_type;
17932     case RID_UNION:
17933       return union_type;
17934
17935     default:
17936       return none_type;
17937     }
17938 }
17939
17940 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
17941
17942 static void
17943 cp_parser_check_class_key (enum tag_types class_key, tree type)
17944 {
17945   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17946     pedwarn ("%qs tag used in naming %q#T",
17947             class_key == union_type ? "union"
17948              : class_key == record_type ? "struct" : "class",
17949              type);
17950 }
17951
17952 /* Issue an error message if DECL is redeclared with different
17953    access than its original declaration [class.access.spec/3].
17954    This applies to nested classes and nested class templates.
17955    [class.mem/1].  */
17956
17957 static void
17958 cp_parser_check_access_in_redeclaration (tree decl)
17959 {
17960   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
17961     return;
17962
17963   if ((TREE_PRIVATE (decl)
17964        != (current_access_specifier == access_private_node))
17965       || (TREE_PROTECTED (decl)
17966           != (current_access_specifier == access_protected_node)))
17967     error ("%qD redeclared with different access", decl);
17968 }
17969
17970 /* Look for the `template' keyword, as a syntactic disambiguator.
17971    Return TRUE iff it is present, in which case it will be
17972    consumed.  */
17973
17974 static bool
17975 cp_parser_optional_template_keyword (cp_parser *parser)
17976 {
17977   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17978     {
17979       /* The `template' keyword can only be used within templates;
17980          outside templates the parser can always figure out what is a
17981          template and what is not.  */
17982       if (!processing_template_decl)
17983         {
17984           error ("%<template%> (as a disambiguator) is only allowed "
17985                  "within templates");
17986           /* If this part of the token stream is rescanned, the same
17987              error message would be generated.  So, we purge the token
17988              from the stream.  */
17989           cp_lexer_purge_token (parser->lexer);
17990           return false;
17991         }
17992       else
17993         {
17994           /* Consume the `template' keyword.  */
17995           cp_lexer_consume_token (parser->lexer);
17996           return true;
17997         }
17998     }
17999
18000   return false;
18001 }
18002
18003 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18004    set PARSER->SCOPE, and perform other related actions.  */
18005
18006 static void
18007 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18008 {
18009   int i;
18010   struct tree_check *check_value;
18011   deferred_access_check *chk;
18012   VEC (deferred_access_check,gc) *checks;
18013
18014   /* Get the stored value.  */
18015   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18016   /* Perform any access checks that were deferred.  */
18017   checks = check_value->checks;
18018   if (checks)
18019     {
18020       for (i = 0 ;
18021            VEC_iterate (deferred_access_check, checks, i, chk) ;
18022            ++i)
18023         {
18024           perform_or_defer_access_check (chk->binfo,
18025                                          chk->decl,
18026                                          chk->diag_decl);
18027         }
18028     }
18029   /* Set the scope from the stored value.  */
18030   parser->scope = check_value->value;
18031   parser->qualifying_scope = check_value->qualifying_scope;
18032   parser->object_scope = NULL_TREE;
18033 }
18034
18035 /* Consume tokens up through a non-nested END token.  */
18036
18037 static void
18038 cp_parser_cache_group (cp_parser *parser,
18039                        enum cpp_ttype end,
18040                        unsigned depth)
18041 {
18042   while (true)
18043     {
18044       cp_token *token;
18045
18046       /* Abort a parenthesized expression if we encounter a brace.  */
18047       if ((end == CPP_CLOSE_PAREN || depth == 0)
18048           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18049         return;
18050       /* If we've reached the end of the file, stop.  */
18051       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
18052           || (end != CPP_PRAGMA_EOL
18053               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
18054         return;
18055       /* Consume the next token.  */
18056       token = cp_lexer_consume_token (parser->lexer);
18057       /* See if it starts a new group.  */
18058       if (token->type == CPP_OPEN_BRACE)
18059         {
18060           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18061           if (depth == 0)
18062             return;
18063         }
18064       else if (token->type == CPP_OPEN_PAREN)
18065         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18066       else if (token->type == CPP_PRAGMA)
18067         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18068       else if (token->type == end)
18069         return;
18070     }
18071 }
18072
18073 /* Begin parsing tentatively.  We always save tokens while parsing
18074    tentatively so that if the tentative parsing fails we can restore the
18075    tokens.  */
18076
18077 static void
18078 cp_parser_parse_tentatively (cp_parser* parser)
18079 {
18080   /* Enter a new parsing context.  */
18081   parser->context = cp_parser_context_new (parser->context);
18082   /* Begin saving tokens.  */
18083   cp_lexer_save_tokens (parser->lexer);
18084   /* In order to avoid repetitive access control error messages,
18085      access checks are queued up until we are no longer parsing
18086      tentatively.  */
18087   push_deferring_access_checks (dk_deferred);
18088 }
18089
18090 /* Commit to the currently active tentative parse.  */
18091
18092 static void
18093 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18094 {
18095   cp_parser_context *context;
18096   cp_lexer *lexer;
18097
18098   /* Mark all of the levels as committed.  */
18099   lexer = parser->lexer;
18100   for (context = parser->context; context->next; context = context->next)
18101     {
18102       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18103         break;
18104       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18105       while (!cp_lexer_saving_tokens (lexer))
18106         lexer = lexer->next;
18107       cp_lexer_commit_tokens (lexer);
18108     }
18109 }
18110
18111 /* Abort the currently active tentative parse.  All consumed tokens
18112    will be rolled back, and no diagnostics will be issued.  */
18113
18114 static void
18115 cp_parser_abort_tentative_parse (cp_parser* parser)
18116 {
18117   cp_parser_simulate_error (parser);
18118   /* Now, pretend that we want to see if the construct was
18119      successfully parsed.  */
18120   cp_parser_parse_definitely (parser);
18121 }
18122
18123 /* Stop parsing tentatively.  If a parse error has occurred, restore the
18124    token stream.  Otherwise, commit to the tokens we have consumed.
18125    Returns true if no error occurred; false otherwise.  */
18126
18127 static bool
18128 cp_parser_parse_definitely (cp_parser* parser)
18129 {
18130   bool error_occurred;
18131   cp_parser_context *context;
18132
18133   /* Remember whether or not an error occurred, since we are about to
18134      destroy that information.  */
18135   error_occurred = cp_parser_error_occurred (parser);
18136   /* Remove the topmost context from the stack.  */
18137   context = parser->context;
18138   parser->context = context->next;
18139   /* If no parse errors occurred, commit to the tentative parse.  */
18140   if (!error_occurred)
18141     {
18142       /* Commit to the tokens read tentatively, unless that was
18143          already done.  */
18144       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18145         cp_lexer_commit_tokens (parser->lexer);
18146
18147       pop_to_parent_deferring_access_checks ();
18148     }
18149   /* Otherwise, if errors occurred, roll back our state so that things
18150      are just as they were before we began the tentative parse.  */
18151   else
18152     {
18153       cp_lexer_rollback_tokens (parser->lexer);
18154       pop_deferring_access_checks ();
18155     }
18156   /* Add the context to the front of the free list.  */
18157   context->next = cp_parser_context_free_list;
18158   cp_parser_context_free_list = context;
18159
18160   return !error_occurred;
18161 }
18162
18163 /* Returns true if we are parsing tentatively and are not committed to
18164    this tentative parse.  */
18165
18166 static bool
18167 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18168 {
18169   return (cp_parser_parsing_tentatively (parser)
18170           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18171 }
18172
18173 /* Returns nonzero iff an error has occurred during the most recent
18174    tentative parse.  */
18175
18176 static bool
18177 cp_parser_error_occurred (cp_parser* parser)
18178 {
18179   return (cp_parser_parsing_tentatively (parser)
18180           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18181 }
18182
18183 /* Returns nonzero if GNU extensions are allowed.  */
18184
18185 static bool
18186 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18187 {
18188   return parser->allow_gnu_extensions_p;
18189 }
18190 \f
18191 /* Objective-C++ Productions */
18192
18193
18194 /* Parse an Objective-C expression, which feeds into a primary-expression
18195    above.
18196
18197    objc-expression:
18198      objc-message-expression
18199      objc-string-literal
18200      objc-encode-expression
18201      objc-protocol-expression
18202      objc-selector-expression
18203
18204   Returns a tree representation of the expression.  */
18205
18206 static tree
18207 cp_parser_objc_expression (cp_parser* parser)
18208 {
18209   /* Try to figure out what kind of declaration is present.  */
18210   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18211
18212   switch (kwd->type)
18213     {
18214     case CPP_OPEN_SQUARE:
18215       return cp_parser_objc_message_expression (parser);
18216
18217     case CPP_OBJC_STRING:
18218       kwd = cp_lexer_consume_token (parser->lexer);
18219       return objc_build_string_object (kwd->u.value);
18220
18221     case CPP_KEYWORD:
18222       switch (kwd->keyword)
18223         {
18224         case RID_AT_ENCODE:
18225           return cp_parser_objc_encode_expression (parser);
18226
18227         case RID_AT_PROTOCOL:
18228           return cp_parser_objc_protocol_expression (parser);
18229
18230         case RID_AT_SELECTOR:
18231           return cp_parser_objc_selector_expression (parser);
18232
18233         default:
18234           break;
18235         }
18236     default:
18237       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18238       cp_parser_skip_to_end_of_block_or_statement (parser);
18239     }
18240
18241   return error_mark_node;
18242 }
18243
18244 /* Parse an Objective-C message expression.
18245
18246    objc-message-expression:
18247      [ objc-message-receiver objc-message-args ]
18248
18249    Returns a representation of an Objective-C message.  */
18250
18251 static tree
18252 cp_parser_objc_message_expression (cp_parser* parser)
18253 {
18254   tree receiver, messageargs;
18255
18256   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
18257   receiver = cp_parser_objc_message_receiver (parser);
18258   messageargs = cp_parser_objc_message_args (parser);
18259   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
18260
18261   return objc_build_message_expr (build_tree_list (receiver, messageargs));
18262 }
18263
18264 /* Parse an objc-message-receiver.
18265
18266    objc-message-receiver:
18267      expression
18268      simple-type-specifier
18269
18270   Returns a representation of the type or expression.  */
18271
18272 static tree
18273 cp_parser_objc_message_receiver (cp_parser* parser)
18274 {
18275   tree rcv;
18276
18277   /* An Objective-C message receiver may be either (1) a type
18278      or (2) an expression.  */
18279   cp_parser_parse_tentatively (parser);
18280   rcv = cp_parser_expression (parser, false);
18281
18282   if (cp_parser_parse_definitely (parser))
18283     return rcv;
18284
18285   rcv = cp_parser_simple_type_specifier (parser,
18286                                          /*decl_specs=*/NULL,
18287                                          CP_PARSER_FLAGS_NONE);
18288
18289   return objc_get_class_reference (rcv);
18290 }
18291
18292 /* Parse the arguments and selectors comprising an Objective-C message.
18293
18294    objc-message-args:
18295      objc-selector
18296      objc-selector-args
18297      objc-selector-args , objc-comma-args
18298
18299    objc-selector-args:
18300      objc-selector [opt] : assignment-expression
18301      objc-selector-args objc-selector [opt] : assignment-expression
18302
18303    objc-comma-args:
18304      assignment-expression
18305      objc-comma-args , assignment-expression
18306
18307    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18308    selector arguments and TREE_VALUE containing a list of comma
18309    arguments.  */
18310
18311 static tree
18312 cp_parser_objc_message_args (cp_parser* parser)
18313 {
18314   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18315   bool maybe_unary_selector_p = true;
18316   cp_token *token = cp_lexer_peek_token (parser->lexer);
18317
18318   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18319     {
18320       tree selector = NULL_TREE, arg;
18321
18322       if (token->type != CPP_COLON)
18323         selector = cp_parser_objc_selector (parser);
18324
18325       /* Detect if we have a unary selector.  */
18326       if (maybe_unary_selector_p
18327           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18328         return build_tree_list (selector, NULL_TREE);
18329
18330       maybe_unary_selector_p = false;
18331       cp_parser_require (parser, CPP_COLON, "`:'");
18332       arg = cp_parser_assignment_expression (parser, false);
18333
18334       sel_args
18335         = chainon (sel_args,
18336                    build_tree_list (selector, arg));
18337
18338       token = cp_lexer_peek_token (parser->lexer);
18339     }
18340
18341   /* Handle non-selector arguments, if any. */
18342   while (token->type == CPP_COMMA)
18343     {
18344       tree arg;
18345
18346       cp_lexer_consume_token (parser->lexer);
18347       arg = cp_parser_assignment_expression (parser, false);
18348
18349       addl_args
18350         = chainon (addl_args,
18351                    build_tree_list (NULL_TREE, arg));
18352
18353       token = cp_lexer_peek_token (parser->lexer);
18354     }
18355
18356   return build_tree_list (sel_args, addl_args);
18357 }
18358
18359 /* Parse an Objective-C encode expression.
18360
18361    objc-encode-expression:
18362      @encode objc-typename
18363
18364    Returns an encoded representation of the type argument.  */
18365
18366 static tree
18367 cp_parser_objc_encode_expression (cp_parser* parser)
18368 {
18369   tree type;
18370
18371   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
18372   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18373   type = complete_type (cp_parser_type_id (parser));
18374   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18375
18376   if (!type)
18377     {
18378       error ("%<@encode%> must specify a type as an argument");
18379       return error_mark_node;
18380     }
18381
18382   return objc_build_encode_expr (type);
18383 }
18384
18385 /* Parse an Objective-C @defs expression.  */
18386
18387 static tree
18388 cp_parser_objc_defs_expression (cp_parser *parser)
18389 {
18390   tree name;
18391
18392   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
18393   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18394   name = cp_parser_identifier (parser);
18395   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18396
18397   return objc_get_class_ivars (name);
18398 }
18399
18400 /* Parse an Objective-C protocol expression.
18401
18402   objc-protocol-expression:
18403     @protocol ( identifier )
18404
18405   Returns a representation of the protocol expression.  */
18406
18407 static tree
18408 cp_parser_objc_protocol_expression (cp_parser* parser)
18409 {
18410   tree proto;
18411
18412   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
18413   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18414   proto = cp_parser_identifier (parser);
18415   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18416
18417   return objc_build_protocol_expr (proto);
18418 }
18419
18420 /* Parse an Objective-C selector expression.
18421
18422    objc-selector-expression:
18423      @selector ( objc-method-signature )
18424
18425    objc-method-signature:
18426      objc-selector
18427      objc-selector-seq
18428
18429    objc-selector-seq:
18430      objc-selector :
18431      objc-selector-seq objc-selector :
18432
18433   Returns a representation of the method selector.  */
18434
18435 static tree
18436 cp_parser_objc_selector_expression (cp_parser* parser)
18437 {
18438   tree sel_seq = NULL_TREE;
18439   bool maybe_unary_selector_p = true;
18440   cp_token *token;
18441
18442   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
18443   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18444   token = cp_lexer_peek_token (parser->lexer);
18445
18446   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18447          || token->type == CPP_SCOPE)
18448     {
18449       tree selector = NULL_TREE;
18450
18451       if (token->type != CPP_COLON
18452           || token->type == CPP_SCOPE)
18453         selector = cp_parser_objc_selector (parser);
18454
18455       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18456           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18457         {
18458           /* Detect if we have a unary selector.  */
18459           if (maybe_unary_selector_p)
18460             {
18461               sel_seq = selector;
18462               goto finish_selector;
18463             }
18464           else
18465             {
18466               cp_parser_error (parser, "expected %<:%>");
18467             }
18468         }
18469       maybe_unary_selector_p = false;
18470       token = cp_lexer_consume_token (parser->lexer);
18471
18472       if (token->type == CPP_SCOPE)
18473         {
18474           sel_seq
18475             = chainon (sel_seq,
18476                        build_tree_list (selector, NULL_TREE));
18477           sel_seq
18478             = chainon (sel_seq,
18479                        build_tree_list (NULL_TREE, NULL_TREE));
18480         }
18481       else
18482         sel_seq
18483           = chainon (sel_seq,
18484                      build_tree_list (selector, NULL_TREE));
18485
18486       token = cp_lexer_peek_token (parser->lexer);
18487     }
18488
18489  finish_selector:
18490   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18491
18492   return objc_build_selector_expr (sel_seq);
18493 }
18494
18495 /* Parse a list of identifiers.
18496
18497    objc-identifier-list:
18498      identifier
18499      objc-identifier-list , identifier
18500
18501    Returns a TREE_LIST of identifier nodes.  */
18502
18503 static tree
18504 cp_parser_objc_identifier_list (cp_parser* parser)
18505 {
18506   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18507   cp_token *sep = cp_lexer_peek_token (parser->lexer);
18508
18509   while (sep->type == CPP_COMMA)
18510     {
18511       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18512       list = chainon (list,
18513                       build_tree_list (NULL_TREE,
18514                                        cp_parser_identifier (parser)));
18515       sep = cp_lexer_peek_token (parser->lexer);
18516     }
18517
18518   return list;
18519 }
18520
18521 /* Parse an Objective-C alias declaration.
18522
18523    objc-alias-declaration:
18524      @compatibility_alias identifier identifier ;
18525
18526    This function registers the alias mapping with the Objective-C front end.
18527    It returns nothing.  */
18528
18529 static void
18530 cp_parser_objc_alias_declaration (cp_parser* parser)
18531 {
18532   tree alias, orig;
18533
18534   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
18535   alias = cp_parser_identifier (parser);
18536   orig = cp_parser_identifier (parser);
18537   objc_declare_alias (alias, orig);
18538   cp_parser_consume_semicolon_at_end_of_statement (parser);
18539 }
18540
18541 /* Parse an Objective-C class forward-declaration.
18542
18543    objc-class-declaration:
18544      @class objc-identifier-list ;
18545
18546    The function registers the forward declarations with the Objective-C
18547    front end.  It returns nothing.  */
18548
18549 static void
18550 cp_parser_objc_class_declaration (cp_parser* parser)
18551 {
18552   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
18553   objc_declare_class (cp_parser_objc_identifier_list (parser));
18554   cp_parser_consume_semicolon_at_end_of_statement (parser);
18555 }
18556
18557 /* Parse a list of Objective-C protocol references.
18558
18559    objc-protocol-refs-opt:
18560      objc-protocol-refs [opt]
18561
18562    objc-protocol-refs:
18563      < objc-identifier-list >
18564
18565    Returns a TREE_LIST of identifiers, if any.  */
18566
18567 static tree
18568 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18569 {
18570   tree protorefs = NULL_TREE;
18571
18572   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18573     {
18574       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
18575       protorefs = cp_parser_objc_identifier_list (parser);
18576       cp_parser_require (parser, CPP_GREATER, "`>'");
18577     }
18578
18579   return protorefs;
18580 }
18581
18582 /* Parse a Objective-C visibility specification.  */
18583
18584 static void
18585 cp_parser_objc_visibility_spec (cp_parser* parser)
18586 {
18587   cp_token *vis = cp_lexer_peek_token (parser->lexer);
18588
18589   switch (vis->keyword)
18590     {
18591     case RID_AT_PRIVATE:
18592       objc_set_visibility (2);
18593       break;
18594     case RID_AT_PROTECTED:
18595       objc_set_visibility (0);
18596       break;
18597     case RID_AT_PUBLIC:
18598       objc_set_visibility (1);
18599       break;
18600     default:
18601       return;
18602     }
18603
18604   /* Eat '@private'/'@protected'/'@public'.  */
18605   cp_lexer_consume_token (parser->lexer);
18606 }
18607
18608 /* Parse an Objective-C method type.  */
18609
18610 static void
18611 cp_parser_objc_method_type (cp_parser* parser)
18612 {
18613   objc_set_method_type
18614    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18615     ? PLUS_EXPR
18616     : MINUS_EXPR);
18617 }
18618
18619 /* Parse an Objective-C protocol qualifier.  */
18620
18621 static tree
18622 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18623 {
18624   tree quals = NULL_TREE, node;
18625   cp_token *token = cp_lexer_peek_token (parser->lexer);
18626
18627   node = token->u.value;
18628
18629   while (node && TREE_CODE (node) == IDENTIFIER_NODE
18630          && (node == ridpointers [(int) RID_IN]
18631              || node == ridpointers [(int) RID_OUT]
18632              || node == ridpointers [(int) RID_INOUT]
18633              || node == ridpointers [(int) RID_BYCOPY]
18634              || node == ridpointers [(int) RID_BYREF]
18635              || node == ridpointers [(int) RID_ONEWAY]))
18636     {
18637       quals = tree_cons (NULL_TREE, node, quals);
18638       cp_lexer_consume_token (parser->lexer);
18639       token = cp_lexer_peek_token (parser->lexer);
18640       node = token->u.value;
18641     }
18642
18643   return quals;
18644 }
18645
18646 /* Parse an Objective-C typename.  */
18647
18648 static tree
18649 cp_parser_objc_typename (cp_parser* parser)
18650 {
18651   tree typename = NULL_TREE;
18652
18653   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18654     {
18655       tree proto_quals, cp_type = NULL_TREE;
18656
18657       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
18658       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18659
18660       /* An ObjC type name may consist of just protocol qualifiers, in which
18661          case the type shall default to 'id'.  */
18662       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18663         cp_type = cp_parser_type_id (parser);
18664
18665       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18666       typename = build_tree_list (proto_quals, cp_type);
18667     }
18668
18669   return typename;
18670 }
18671
18672 /* Check to see if TYPE refers to an Objective-C selector name.  */
18673
18674 static bool
18675 cp_parser_objc_selector_p (enum cpp_ttype type)
18676 {
18677   return (type == CPP_NAME || type == CPP_KEYWORD
18678           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18679           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18680           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18681           || type == CPP_XOR || type == CPP_XOR_EQ);
18682 }
18683
18684 /* Parse an Objective-C selector.  */
18685
18686 static tree
18687 cp_parser_objc_selector (cp_parser* parser)
18688 {
18689   cp_token *token = cp_lexer_consume_token (parser->lexer);
18690
18691   if (!cp_parser_objc_selector_p (token->type))
18692     {
18693       error ("invalid Objective-C++ selector name");
18694       return error_mark_node;
18695     }
18696
18697   /* C++ operator names are allowed to appear in ObjC selectors.  */
18698   switch (token->type)
18699     {
18700     case CPP_AND_AND: return get_identifier ("and");
18701     case CPP_AND_EQ: return get_identifier ("and_eq");
18702     case CPP_AND: return get_identifier ("bitand");
18703     case CPP_OR: return get_identifier ("bitor");
18704     case CPP_COMPL: return get_identifier ("compl");
18705     case CPP_NOT: return get_identifier ("not");
18706     case CPP_NOT_EQ: return get_identifier ("not_eq");
18707     case CPP_OR_OR: return get_identifier ("or");
18708     case CPP_OR_EQ: return get_identifier ("or_eq");
18709     case CPP_XOR: return get_identifier ("xor");
18710     case CPP_XOR_EQ: return get_identifier ("xor_eq");
18711     default: return token->u.value;
18712     }
18713 }
18714
18715 /* Parse an Objective-C params list.  */
18716
18717 static tree
18718 cp_parser_objc_method_keyword_params (cp_parser* parser)
18719 {
18720   tree params = NULL_TREE;
18721   bool maybe_unary_selector_p = true;
18722   cp_token *token = cp_lexer_peek_token (parser->lexer);
18723
18724   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18725     {
18726       tree selector = NULL_TREE, typename, identifier;
18727
18728       if (token->type != CPP_COLON)
18729         selector = cp_parser_objc_selector (parser);
18730
18731       /* Detect if we have a unary selector.  */
18732       if (maybe_unary_selector_p
18733           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18734         return selector;
18735
18736       maybe_unary_selector_p = false;
18737       cp_parser_require (parser, CPP_COLON, "`:'");
18738       typename = cp_parser_objc_typename (parser);
18739       identifier = cp_parser_identifier (parser);
18740
18741       params
18742         = chainon (params,
18743                    objc_build_keyword_decl (selector,
18744                                             typename,
18745                                             identifier));
18746
18747       token = cp_lexer_peek_token (parser->lexer);
18748     }
18749
18750   return params;
18751 }
18752
18753 /* Parse the non-keyword Objective-C params.  */
18754
18755 static tree
18756 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18757 {
18758   tree params = make_node (TREE_LIST);
18759   cp_token *token = cp_lexer_peek_token (parser->lexer);
18760   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
18761
18762   while (token->type == CPP_COMMA)
18763     {
18764       cp_parameter_declarator *parmdecl;
18765       tree parm;
18766
18767       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18768       token = cp_lexer_peek_token (parser->lexer);
18769
18770       if (token->type == CPP_ELLIPSIS)
18771         {
18772           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
18773           *ellipsisp = true;
18774           break;
18775         }
18776
18777       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18778       parm = grokdeclarator (parmdecl->declarator,
18779                              &parmdecl->decl_specifiers,
18780                              PARM, /*initialized=*/0,
18781                              /*attrlist=*/NULL);
18782
18783       chainon (params, build_tree_list (NULL_TREE, parm));
18784       token = cp_lexer_peek_token (parser->lexer);
18785     }
18786
18787   return params;
18788 }
18789
18790 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
18791
18792 static void
18793 cp_parser_objc_interstitial_code (cp_parser* parser)
18794 {
18795   cp_token *token = cp_lexer_peek_token (parser->lexer);
18796
18797   /* If the next token is `extern' and the following token is a string
18798      literal, then we have a linkage specification.  */
18799   if (token->keyword == RID_EXTERN
18800       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18801     cp_parser_linkage_specification (parser);
18802   /* Handle #pragma, if any.  */
18803   else if (token->type == CPP_PRAGMA)
18804     cp_parser_pragma (parser, pragma_external);
18805   /* Allow stray semicolons.  */
18806   else if (token->type == CPP_SEMICOLON)
18807     cp_lexer_consume_token (parser->lexer);
18808   /* Finally, try to parse a block-declaration, or a function-definition.  */
18809   else
18810     cp_parser_block_declaration (parser, /*statement_p=*/false);
18811 }
18812
18813 /* Parse a method signature.  */
18814
18815 static tree
18816 cp_parser_objc_method_signature (cp_parser* parser)
18817 {
18818   tree rettype, kwdparms, optparms;
18819   bool ellipsis = false;
18820
18821   cp_parser_objc_method_type (parser);
18822   rettype = cp_parser_objc_typename (parser);
18823   kwdparms = cp_parser_objc_method_keyword_params (parser);
18824   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18825
18826   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18827 }
18828
18829 /* Pars an Objective-C method prototype list.  */
18830
18831 static void
18832 cp_parser_objc_method_prototype_list (cp_parser* parser)
18833 {
18834   cp_token *token = cp_lexer_peek_token (parser->lexer);
18835
18836   while (token->keyword != RID_AT_END)
18837     {
18838       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18839         {
18840           objc_add_method_declaration
18841            (cp_parser_objc_method_signature (parser));
18842           cp_parser_consume_semicolon_at_end_of_statement (parser);
18843         }
18844       else
18845         /* Allow for interspersed non-ObjC++ code.  */
18846         cp_parser_objc_interstitial_code (parser);
18847
18848       token = cp_lexer_peek_token (parser->lexer);
18849     }
18850
18851   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18852   objc_finish_interface ();
18853 }
18854
18855 /* Parse an Objective-C method definition list.  */
18856
18857 static void
18858 cp_parser_objc_method_definition_list (cp_parser* parser)
18859 {
18860   cp_token *token = cp_lexer_peek_token (parser->lexer);
18861
18862   while (token->keyword != RID_AT_END)
18863     {
18864       tree meth;
18865
18866       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18867         {
18868           push_deferring_access_checks (dk_deferred);
18869           objc_start_method_definition
18870            (cp_parser_objc_method_signature (parser));
18871
18872           /* For historical reasons, we accept an optional semicolon.  */
18873           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18874             cp_lexer_consume_token (parser->lexer);
18875
18876           perform_deferred_access_checks ();
18877           stop_deferring_access_checks ();
18878           meth = cp_parser_function_definition_after_declarator (parser,
18879                                                                  false);
18880           pop_deferring_access_checks ();
18881           objc_finish_method_definition (meth);
18882         }
18883       else
18884         /* Allow for interspersed non-ObjC++ code.  */
18885         cp_parser_objc_interstitial_code (parser);
18886
18887       token = cp_lexer_peek_token (parser->lexer);
18888     }
18889
18890   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18891   objc_finish_implementation ();
18892 }
18893
18894 /* Parse Objective-C ivars.  */
18895
18896 static void
18897 cp_parser_objc_class_ivars (cp_parser* parser)
18898 {
18899   cp_token *token = cp_lexer_peek_token (parser->lexer);
18900
18901   if (token->type != CPP_OPEN_BRACE)
18902     return;     /* No ivars specified.  */
18903
18904   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
18905   token = cp_lexer_peek_token (parser->lexer);
18906
18907   while (token->type != CPP_CLOSE_BRACE)
18908     {
18909       cp_decl_specifier_seq declspecs;
18910       int decl_class_or_enum_p;
18911       tree prefix_attributes;
18912
18913       cp_parser_objc_visibility_spec (parser);
18914
18915       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18916         break;
18917
18918       cp_parser_decl_specifier_seq (parser,
18919                                     CP_PARSER_FLAGS_OPTIONAL,
18920                                     &declspecs,
18921                                     &decl_class_or_enum_p);
18922       prefix_attributes = declspecs.attributes;
18923       declspecs.attributes = NULL_TREE;
18924
18925       /* Keep going until we hit the `;' at the end of the
18926          declaration.  */
18927       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18928         {
18929           tree width = NULL_TREE, attributes, first_attribute, decl;
18930           cp_declarator *declarator = NULL;
18931           int ctor_dtor_or_conv_p;
18932
18933           /* Check for a (possibly unnamed) bitfield declaration.  */
18934           token = cp_lexer_peek_token (parser->lexer);
18935           if (token->type == CPP_COLON)
18936             goto eat_colon;
18937
18938           if (token->type == CPP_NAME
18939               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18940                   == CPP_COLON))
18941             {
18942               /* Get the name of the bitfield.  */
18943               declarator = make_id_declarator (NULL_TREE,
18944                                                cp_parser_identifier (parser),
18945                                                sfk_none);
18946
18947              eat_colon:
18948               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
18949               /* Get the width of the bitfield.  */
18950               width
18951                 = cp_parser_constant_expression (parser,
18952                                                  /*allow_non_constant=*/false,
18953                                                  NULL);
18954             }
18955           else
18956             {
18957               /* Parse the declarator.  */
18958               declarator
18959                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18960                                         &ctor_dtor_or_conv_p,
18961                                         /*parenthesized_p=*/NULL,
18962                                         /*member_p=*/false);
18963             }
18964
18965           /* Look for attributes that apply to the ivar.  */
18966           attributes = cp_parser_attributes_opt (parser);
18967           /* Remember which attributes are prefix attributes and
18968              which are not.  */
18969           first_attribute = attributes;
18970           /* Combine the attributes.  */
18971           attributes = chainon (prefix_attributes, attributes);
18972
18973           if (width)
18974             {
18975               /* Create the bitfield declaration.  */
18976               decl = grokbitfield (declarator, &declspecs, width);
18977               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18978             }
18979           else
18980             decl = grokfield (declarator, &declspecs,
18981                               NULL_TREE, /*init_const_expr_p=*/false,
18982                               NULL_TREE, attributes);
18983
18984           /* Add the instance variable.  */
18985           objc_add_instance_variable (decl);
18986
18987           /* Reset PREFIX_ATTRIBUTES.  */
18988           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18989             attributes = TREE_CHAIN (attributes);
18990           if (attributes)
18991             TREE_CHAIN (attributes) = NULL_TREE;
18992
18993           token = cp_lexer_peek_token (parser->lexer);
18994
18995           if (token->type == CPP_COMMA)
18996             {
18997               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18998               continue;
18999             }
19000           break;
19001         }
19002
19003       cp_parser_consume_semicolon_at_end_of_statement (parser);
19004       token = cp_lexer_peek_token (parser->lexer);
19005     }
19006
19007   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19008   /* For historical reasons, we accept an optional semicolon.  */
19009   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19010     cp_lexer_consume_token (parser->lexer);
19011 }
19012
19013 /* Parse an Objective-C protocol declaration.  */
19014
19015 static void
19016 cp_parser_objc_protocol_declaration (cp_parser* parser)
19017 {
19018   tree proto, protorefs;
19019   cp_token *tok;
19020
19021   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19022   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19023     {
19024       error ("identifier expected after %<@protocol%>");
19025       goto finish;
19026     }
19027
19028   /* See if we have a forward declaration or a definition.  */
19029   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19030
19031   /* Try a forward declaration first.  */
19032   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19033     {
19034       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19035      finish:
19036       cp_parser_consume_semicolon_at_end_of_statement (parser);
19037     }
19038
19039   /* Ok, we got a full-fledged definition (or at least should).  */
19040   else
19041     {
19042       proto = cp_parser_identifier (parser);
19043       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19044       objc_start_protocol (proto, protorefs);
19045       cp_parser_objc_method_prototype_list (parser);
19046     }
19047 }
19048
19049 /* Parse an Objective-C superclass or category.  */
19050
19051 static void
19052 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19053                                                           tree *categ)
19054 {
19055   cp_token *next = cp_lexer_peek_token (parser->lexer);
19056
19057   *super = *categ = NULL_TREE;
19058   if (next->type == CPP_COLON)
19059     {
19060       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19061       *super = cp_parser_identifier (parser);
19062     }
19063   else if (next->type == CPP_OPEN_PAREN)
19064     {
19065       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19066       *categ = cp_parser_identifier (parser);
19067       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19068     }
19069 }
19070
19071 /* Parse an Objective-C class interface.  */
19072
19073 static void
19074 cp_parser_objc_class_interface (cp_parser* parser)
19075 {
19076   tree name, super, categ, protos;
19077
19078   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19079   name = cp_parser_identifier (parser);
19080   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19081   protos = cp_parser_objc_protocol_refs_opt (parser);
19082
19083   /* We have either a class or a category on our hands.  */
19084   if (categ)
19085     objc_start_category_interface (name, categ, protos);
19086   else
19087     {
19088       objc_start_class_interface (name, super, protos);
19089       /* Handle instance variable declarations, if any.  */
19090       cp_parser_objc_class_ivars (parser);
19091       objc_continue_interface ();
19092     }
19093
19094   cp_parser_objc_method_prototype_list (parser);
19095 }
19096
19097 /* Parse an Objective-C class implementation.  */
19098
19099 static void
19100 cp_parser_objc_class_implementation (cp_parser* parser)
19101 {
19102   tree name, super, categ;
19103
19104   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
19105   name = cp_parser_identifier (parser);
19106   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19107
19108   /* We have either a class or a category on our hands.  */
19109   if (categ)
19110     objc_start_category_implementation (name, categ);
19111   else
19112     {
19113       objc_start_class_implementation (name, super);
19114       /* Handle instance variable declarations, if any.  */
19115       cp_parser_objc_class_ivars (parser);
19116       objc_continue_implementation ();
19117     }
19118
19119   cp_parser_objc_method_definition_list (parser);
19120 }
19121
19122 /* Consume the @end token and finish off the implementation.  */
19123
19124 static void
19125 cp_parser_objc_end_implementation (cp_parser* parser)
19126 {
19127   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19128   objc_finish_implementation ();
19129 }
19130
19131 /* Parse an Objective-C declaration.  */
19132
19133 static void
19134 cp_parser_objc_declaration (cp_parser* parser)
19135 {
19136   /* Try to figure out what kind of declaration is present.  */
19137   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19138
19139   switch (kwd->keyword)
19140     {
19141     case RID_AT_ALIAS:
19142       cp_parser_objc_alias_declaration (parser);
19143       break;
19144     case RID_AT_CLASS:
19145       cp_parser_objc_class_declaration (parser);
19146       break;
19147     case RID_AT_PROTOCOL:
19148       cp_parser_objc_protocol_declaration (parser);
19149       break;
19150     case RID_AT_INTERFACE:
19151       cp_parser_objc_class_interface (parser);
19152       break;
19153     case RID_AT_IMPLEMENTATION:
19154       cp_parser_objc_class_implementation (parser);
19155       break;
19156     case RID_AT_END:
19157       cp_parser_objc_end_implementation (parser);
19158       break;
19159     default:
19160       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19161       cp_parser_skip_to_end_of_block_or_statement (parser);
19162     }
19163 }
19164
19165 /* Parse an Objective-C try-catch-finally statement.
19166
19167    objc-try-catch-finally-stmt:
19168      @try compound-statement objc-catch-clause-seq [opt]
19169        objc-finally-clause [opt]
19170
19171    objc-catch-clause-seq:
19172      objc-catch-clause objc-catch-clause-seq [opt]
19173
19174    objc-catch-clause:
19175      @catch ( exception-declaration ) compound-statement
19176
19177    objc-finally-clause
19178      @finally compound-statement
19179
19180    Returns NULL_TREE.  */
19181
19182 static tree
19183 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19184   location_t location;
19185   tree stmt;
19186
19187   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
19188   location = cp_lexer_peek_token (parser->lexer)->location;
19189   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19190      node, lest it get absorbed into the surrounding block.  */
19191   stmt = push_stmt_list ();
19192   cp_parser_compound_statement (parser, NULL, false);
19193   objc_begin_try_stmt (location, pop_stmt_list (stmt));
19194
19195   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19196     {
19197       cp_parameter_declarator *parmdecl;
19198       tree parm;
19199
19200       cp_lexer_consume_token (parser->lexer);
19201       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19202       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19203       parm = grokdeclarator (parmdecl->declarator,
19204                              &parmdecl->decl_specifiers,
19205                              PARM, /*initialized=*/0,
19206                              /*attrlist=*/NULL);
19207       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19208       objc_begin_catch_clause (parm);
19209       cp_parser_compound_statement (parser, NULL, false);
19210       objc_finish_catch_clause ();
19211     }
19212
19213   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19214     {
19215       cp_lexer_consume_token (parser->lexer);
19216       location = cp_lexer_peek_token (parser->lexer)->location;
19217       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19218          node, lest it get absorbed into the surrounding block.  */
19219       stmt = push_stmt_list ();
19220       cp_parser_compound_statement (parser, NULL, false);
19221       objc_build_finally_clause (location, pop_stmt_list (stmt));
19222     }
19223
19224   return objc_finish_try_stmt ();
19225 }
19226
19227 /* Parse an Objective-C synchronized statement.
19228
19229    objc-synchronized-stmt:
19230      @synchronized ( expression ) compound-statement
19231
19232    Returns NULL_TREE.  */
19233
19234 static tree
19235 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19236   location_t location;
19237   tree lock, stmt;
19238
19239   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
19240
19241   location = cp_lexer_peek_token (parser->lexer)->location;
19242   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19243   lock = cp_parser_expression (parser, false);
19244   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19245
19246   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19247      node, lest it get absorbed into the surrounding block.  */
19248   stmt = push_stmt_list ();
19249   cp_parser_compound_statement (parser, NULL, false);
19250
19251   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19252 }
19253
19254 /* Parse an Objective-C throw statement.
19255
19256    objc-throw-stmt:
19257      @throw assignment-expression [opt] ;
19258
19259    Returns a constructed '@throw' statement.  */
19260
19261 static tree
19262 cp_parser_objc_throw_statement (cp_parser *parser) {
19263   tree expr = NULL_TREE;
19264
19265   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
19266
19267   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19268     expr = cp_parser_assignment_expression (parser, false);
19269
19270   cp_parser_consume_semicolon_at_end_of_statement (parser);
19271
19272   return objc_build_throw_stmt (expr);
19273 }
19274
19275 /* Parse an Objective-C statement.  */
19276
19277 static tree
19278 cp_parser_objc_statement (cp_parser * parser) {
19279   /* Try to figure out what kind of declaration is present.  */
19280   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19281
19282   switch (kwd->keyword)
19283     {
19284     case RID_AT_TRY:
19285       return cp_parser_objc_try_catch_finally_statement (parser);
19286     case RID_AT_SYNCHRONIZED:
19287       return cp_parser_objc_synchronized_statement (parser);
19288     case RID_AT_THROW:
19289       return cp_parser_objc_throw_statement (parser);
19290     default:
19291       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19292       cp_parser_skip_to_end_of_block_or_statement (parser);
19293     }
19294
19295   return error_mark_node;
19296 }
19297 \f
19298 /* OpenMP 2.5 parsing routines.  */
19299
19300 /* Returns name of the next clause.
19301    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19302    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
19303    returned and the token is consumed.  */
19304
19305 static pragma_omp_clause
19306 cp_parser_omp_clause_name (cp_parser *parser)
19307 {
19308   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19309
19310   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19311     result = PRAGMA_OMP_CLAUSE_IF;
19312   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19313     result = PRAGMA_OMP_CLAUSE_DEFAULT;
19314   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19315     result = PRAGMA_OMP_CLAUSE_PRIVATE;
19316   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19317     {
19318       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19319       const char *p = IDENTIFIER_POINTER (id);
19320
19321       switch (p[0])
19322         {
19323         case 'c':
19324           if (!strcmp ("copyin", p))
19325             result = PRAGMA_OMP_CLAUSE_COPYIN;
19326           else if (!strcmp ("copyprivate", p))
19327             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19328           break;
19329         case 'f':
19330           if (!strcmp ("firstprivate", p))
19331             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19332           break;
19333         case 'l':
19334           if (!strcmp ("lastprivate", p))
19335             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19336           break;
19337         case 'n':
19338           if (!strcmp ("nowait", p))
19339             result = PRAGMA_OMP_CLAUSE_NOWAIT;
19340           else if (!strcmp ("num_threads", p))
19341             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19342           break;
19343         case 'o':
19344           if (!strcmp ("ordered", p))
19345             result = PRAGMA_OMP_CLAUSE_ORDERED;
19346           break;
19347         case 'r':
19348           if (!strcmp ("reduction", p))
19349             result = PRAGMA_OMP_CLAUSE_REDUCTION;
19350           break;
19351         case 's':
19352           if (!strcmp ("schedule", p))
19353             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19354           else if (!strcmp ("shared", p))
19355             result = PRAGMA_OMP_CLAUSE_SHARED;
19356           break;
19357         }
19358     }
19359
19360   if (result != PRAGMA_OMP_CLAUSE_NONE)
19361     cp_lexer_consume_token (parser->lexer);
19362
19363   return result;
19364 }
19365
19366 /* Validate that a clause of the given type does not already exist.  */
19367
19368 static void
19369 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
19370 {
19371   tree c;
19372
19373   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19374     if (OMP_CLAUSE_CODE (c) == code)
19375       {
19376         error ("too many %qs clauses", name);
19377         break;
19378       }
19379 }
19380
19381 /* OpenMP 2.5:
19382    variable-list:
19383      identifier
19384      variable-list , identifier
19385
19386    In addition, we match a closing parenthesis.  An opening parenthesis
19387    will have been consumed by the caller.
19388
19389    If KIND is nonzero, create the appropriate node and install the decl
19390    in OMP_CLAUSE_DECL and add the node to the head of the list.
19391
19392    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19393    return the list created.  */
19394
19395 static tree
19396 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19397                                 tree list)
19398 {
19399   while (1)
19400     {
19401       tree name, decl;
19402
19403       name = cp_parser_id_expression (parser, /*template_p=*/false,
19404                                       /*check_dependency_p=*/true,
19405                                       /*template_p=*/NULL,
19406                                       /*declarator_p=*/false,
19407                                       /*optional_p=*/false);
19408       if (name == error_mark_node)
19409         goto skip_comma;
19410
19411       decl = cp_parser_lookup_name_simple (parser, name);
19412       if (decl == error_mark_node)
19413         cp_parser_name_lookup_error (parser, name, decl, NULL);
19414       else if (kind != 0)
19415         {
19416           tree u = build_omp_clause (kind);
19417           OMP_CLAUSE_DECL (u) = decl;
19418           OMP_CLAUSE_CHAIN (u) = list;
19419           list = u;
19420         }
19421       else
19422         list = tree_cons (decl, NULL_TREE, list);
19423
19424     get_comma:
19425       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19426         break;
19427       cp_lexer_consume_token (parser->lexer);
19428     }
19429
19430   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19431     {
19432       int ending;
19433
19434       /* Try to resync to an unnested comma.  Copied from
19435          cp_parser_parenthesized_expression_list.  */
19436     skip_comma:
19437       ending = cp_parser_skip_to_closing_parenthesis (parser,
19438                                                       /*recovering=*/true,
19439                                                       /*or_comma=*/true,
19440                                                       /*consume_paren=*/true);
19441       if (ending < 0)
19442         goto get_comma;
19443     }
19444
19445   return list;
19446 }
19447
19448 /* Similarly, but expect leading and trailing parenthesis.  This is a very
19449    common case for omp clauses.  */
19450
19451 static tree
19452 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19453 {
19454   if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19455     return cp_parser_omp_var_list_no_open (parser, kind, list);
19456   return list;
19457 }
19458
19459 /* OpenMP 2.5:
19460    default ( shared | none ) */
19461
19462 static tree
19463 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19464 {
19465   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19466   tree c;
19467
19468   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19469     return list;
19470   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19471     {
19472       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19473       const char *p = IDENTIFIER_POINTER (id);
19474
19475       switch (p[0])
19476         {
19477         case 'n':
19478           if (strcmp ("none", p) != 0)
19479             goto invalid_kind;
19480           kind = OMP_CLAUSE_DEFAULT_NONE;
19481           break;
19482
19483         case 's':
19484           if (strcmp ("shared", p) != 0)
19485             goto invalid_kind;
19486           kind = OMP_CLAUSE_DEFAULT_SHARED;
19487           break;
19488
19489         default:
19490           goto invalid_kind;
19491         }
19492
19493       cp_lexer_consume_token (parser->lexer);
19494     }
19495   else
19496     {
19497     invalid_kind:
19498       cp_parser_error (parser, "expected %<none%> or %<shared%>");
19499     }
19500
19501   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19502     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19503                                            /*or_comma=*/false,
19504                                            /*consume_paren=*/true);
19505
19506   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19507     return list;
19508
19509   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19510   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19511   OMP_CLAUSE_CHAIN (c) = list;
19512   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19513
19514   return c;
19515 }
19516
19517 /* OpenMP 2.5:
19518    if ( expression ) */
19519
19520 static tree
19521 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19522 {
19523   tree t, c;
19524
19525   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19526     return list;
19527
19528   t = cp_parser_condition (parser);
19529
19530   if (t == error_mark_node
19531       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19532     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19533                                            /*or_comma=*/false,
19534                                            /*consume_paren=*/true);
19535
19536   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19537
19538   c = build_omp_clause (OMP_CLAUSE_IF);
19539   OMP_CLAUSE_IF_EXPR (c) = t;
19540   OMP_CLAUSE_CHAIN (c) = list;
19541
19542   return c;
19543 }
19544
19545 /* OpenMP 2.5:
19546    nowait */
19547
19548 static tree
19549 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19550 {
19551   tree c;
19552
19553   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19554
19555   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19556   OMP_CLAUSE_CHAIN (c) = list;
19557   return c;
19558 }
19559
19560 /* OpenMP 2.5:
19561    num_threads ( expression ) */
19562
19563 static tree
19564 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19565 {
19566   tree t, c;
19567
19568   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19569     return list;
19570
19571   t = cp_parser_expression (parser, false);
19572
19573   if (t == error_mark_node
19574       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19575     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19576                                            /*or_comma=*/false,
19577                                            /*consume_paren=*/true);
19578
19579   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19580
19581   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19582   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19583   OMP_CLAUSE_CHAIN (c) = list;
19584
19585   return c;
19586 }
19587
19588 /* OpenMP 2.5:
19589    ordered */
19590
19591 static tree
19592 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19593 {
19594   tree c;
19595
19596   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19597
19598   c = build_omp_clause (OMP_CLAUSE_ORDERED);
19599   OMP_CLAUSE_CHAIN (c) = list;
19600   return c;
19601 }
19602
19603 /* OpenMP 2.5:
19604    reduction ( reduction-operator : variable-list )
19605
19606    reduction-operator:
19607      One of: + * - & ^ | && || */
19608
19609 static tree
19610 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19611 {
19612   enum tree_code code;
19613   tree nlist, c;
19614
19615   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19616     return list;
19617
19618   switch (cp_lexer_peek_token (parser->lexer)->type)
19619     {
19620     case CPP_PLUS:
19621       code = PLUS_EXPR;
19622       break;
19623     case CPP_MULT:
19624       code = MULT_EXPR;
19625       break;
19626     case CPP_MINUS:
19627       code = MINUS_EXPR;
19628       break;
19629     case CPP_AND:
19630       code = BIT_AND_EXPR;
19631       break;
19632     case CPP_XOR:
19633       code = BIT_XOR_EXPR;
19634       break;
19635     case CPP_OR:
19636       code = BIT_IOR_EXPR;
19637       break;
19638     case CPP_AND_AND:
19639       code = TRUTH_ANDIF_EXPR;
19640       break;
19641     case CPP_OR_OR:
19642       code = TRUTH_ORIF_EXPR;
19643       break;
19644     default:
19645       cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19646     resync_fail:
19647       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19648                                              /*or_comma=*/false,
19649                                              /*consume_paren=*/true);
19650       return list;
19651     }
19652   cp_lexer_consume_token (parser->lexer);
19653
19654   if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19655     goto resync_fail;
19656
19657   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19658   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19659     OMP_CLAUSE_REDUCTION_CODE (c) = code;
19660
19661   return nlist;
19662 }
19663
19664 /* OpenMP 2.5:
19665    schedule ( schedule-kind )
19666    schedule ( schedule-kind , expression )
19667
19668    schedule-kind:
19669      static | dynamic | guided | runtime  */
19670
19671 static tree
19672 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19673 {
19674   tree c, t;
19675
19676   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19677     return list;
19678
19679   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19680
19681   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19682     {
19683       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19684       const char *p = IDENTIFIER_POINTER (id);
19685
19686       switch (p[0])
19687         {
19688         case 'd':
19689           if (strcmp ("dynamic", p) != 0)
19690             goto invalid_kind;
19691           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19692           break;
19693
19694         case 'g':
19695           if (strcmp ("guided", p) != 0)
19696             goto invalid_kind;
19697           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19698           break;
19699
19700         case 'r':
19701           if (strcmp ("runtime", p) != 0)
19702             goto invalid_kind;
19703           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19704           break;
19705
19706         default:
19707           goto invalid_kind;
19708         }
19709     }
19710   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19711     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19712   else
19713     goto invalid_kind;
19714   cp_lexer_consume_token (parser->lexer);
19715
19716   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19717     {
19718       cp_lexer_consume_token (parser->lexer);
19719
19720       t = cp_parser_assignment_expression (parser, false);
19721
19722       if (t == error_mark_node)
19723         goto resync_fail;
19724       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19725         error ("schedule %<runtime%> does not take "
19726                "a %<chunk_size%> parameter");
19727       else
19728         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19729
19730       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19731         goto resync_fail;
19732     }
19733   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19734     goto resync_fail;
19735
19736   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19737   OMP_CLAUSE_CHAIN (c) = list;
19738   return c;
19739
19740  invalid_kind:
19741   cp_parser_error (parser, "invalid schedule kind");
19742  resync_fail:
19743   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19744                                          /*or_comma=*/false,
19745                                          /*consume_paren=*/true);
19746   return list;
19747 }
19748
19749 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
19750    is a bitmask in MASK.  Return the list of clauses found; the result
19751    of clause default goes in *pdefault.  */
19752
19753 static tree
19754 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19755                            const char *where, cp_token *pragma_tok)
19756 {
19757   tree clauses = NULL;
19758   bool first = true;
19759
19760   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19761     {
19762       pragma_omp_clause c_kind;
19763       const char *c_name;
19764       tree prev = clauses;
19765
19766       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19767         cp_lexer_consume_token (parser->lexer);
19768
19769       c_kind = cp_parser_omp_clause_name (parser);
19770       first = false;
19771
19772       switch (c_kind)
19773         {
19774         case PRAGMA_OMP_CLAUSE_COPYIN:
19775           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19776           c_name = "copyin";
19777           break;
19778         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19779           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19780                                             clauses);
19781           c_name = "copyprivate";
19782           break;
19783         case PRAGMA_OMP_CLAUSE_DEFAULT:
19784           clauses = cp_parser_omp_clause_default (parser, clauses);
19785           c_name = "default";
19786           break;
19787         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19788           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19789                                             clauses);
19790           c_name = "firstprivate";
19791           break;
19792         case PRAGMA_OMP_CLAUSE_IF:
19793           clauses = cp_parser_omp_clause_if (parser, clauses);
19794           c_name = "if";
19795           break;
19796         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19797           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19798                                             clauses);
19799           c_name = "lastprivate";
19800           break;
19801         case PRAGMA_OMP_CLAUSE_NOWAIT:
19802           clauses = cp_parser_omp_clause_nowait (parser, clauses);
19803           c_name = "nowait";
19804           break;
19805         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19806           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19807           c_name = "num_threads";
19808           break;
19809         case PRAGMA_OMP_CLAUSE_ORDERED:
19810           clauses = cp_parser_omp_clause_ordered (parser, clauses);
19811           c_name = "ordered";
19812           break;
19813         case PRAGMA_OMP_CLAUSE_PRIVATE:
19814           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19815                                             clauses);
19816           c_name = "private";
19817           break;
19818         case PRAGMA_OMP_CLAUSE_REDUCTION:
19819           clauses = cp_parser_omp_clause_reduction (parser, clauses);
19820           c_name = "reduction";
19821           break;
19822         case PRAGMA_OMP_CLAUSE_SCHEDULE:
19823           clauses = cp_parser_omp_clause_schedule (parser, clauses);
19824           c_name = "schedule";
19825           break;
19826         case PRAGMA_OMP_CLAUSE_SHARED:
19827           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19828                                             clauses);
19829           c_name = "shared";
19830           break;
19831         default:
19832           cp_parser_error (parser, "expected %<#pragma omp%> clause");
19833           goto saw_error;
19834         }
19835
19836       if (((mask >> c_kind) & 1) == 0)
19837         {
19838           /* Remove the invalid clause(s) from the list to avoid
19839              confusing the rest of the compiler.  */
19840           clauses = prev;
19841           error ("%qs is not valid for %qs", c_name, where);
19842         }
19843     }
19844  saw_error:
19845   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19846   return finish_omp_clauses (clauses);
19847 }
19848
19849 /* OpenMP 2.5:
19850    structured-block:
19851      statement
19852
19853    In practice, we're also interested in adding the statement to an
19854    outer node.  So it is convenient if we work around the fact that
19855    cp_parser_statement calls add_stmt.  */
19856
19857 static unsigned
19858 cp_parser_begin_omp_structured_block (cp_parser *parser)
19859 {
19860   unsigned save = parser->in_statement;
19861
19862   /* Only move the values to IN_OMP_BLOCK if they weren't false.
19863      This preserves the "not within loop or switch" style error messages
19864      for nonsense cases like
19865         void foo() {
19866         #pragma omp single
19867           break;
19868         }
19869   */
19870   if (parser->in_statement)
19871     parser->in_statement = IN_OMP_BLOCK;
19872
19873   return save;
19874 }
19875
19876 static void
19877 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19878 {
19879   parser->in_statement = save;
19880 }
19881
19882 static tree
19883 cp_parser_omp_structured_block (cp_parser *parser)
19884 {
19885   tree stmt = begin_omp_structured_block ();
19886   unsigned int save = cp_parser_begin_omp_structured_block (parser);
19887
19888   cp_parser_statement (parser, NULL_TREE, false, NULL);
19889
19890   cp_parser_end_omp_structured_block (parser, save);
19891   return finish_omp_structured_block (stmt);
19892 }
19893
19894 /* OpenMP 2.5:
19895    # pragma omp atomic new-line
19896      expression-stmt
19897
19898    expression-stmt:
19899      x binop= expr | x++ | ++x | x-- | --x
19900    binop:
19901      +, *, -, /, &, ^, |, <<, >>
19902
19903   where x is an lvalue expression with scalar type.  */
19904
19905 static void
19906 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19907 {
19908   tree lhs, rhs;
19909   enum tree_code code;
19910
19911   cp_parser_require_pragma_eol (parser, pragma_tok);
19912
19913   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19914                                     /*cast_p=*/false);
19915   switch (TREE_CODE (lhs))
19916     {
19917     case ERROR_MARK:
19918       goto saw_error;
19919
19920     case PREINCREMENT_EXPR:
19921     case POSTINCREMENT_EXPR:
19922       lhs = TREE_OPERAND (lhs, 0);
19923       code = PLUS_EXPR;
19924       rhs = integer_one_node;
19925       break;
19926
19927     case PREDECREMENT_EXPR:
19928     case POSTDECREMENT_EXPR:
19929       lhs = TREE_OPERAND (lhs, 0);
19930       code = MINUS_EXPR;
19931       rhs = integer_one_node;
19932       break;
19933
19934     default:
19935       switch (cp_lexer_peek_token (parser->lexer)->type)
19936         {
19937         case CPP_MULT_EQ:
19938           code = MULT_EXPR;
19939           break;
19940         case CPP_DIV_EQ:
19941           code = TRUNC_DIV_EXPR;
19942           break;
19943         case CPP_PLUS_EQ:
19944           code = PLUS_EXPR;
19945           break;
19946         case CPP_MINUS_EQ:
19947           code = MINUS_EXPR;
19948           break;
19949         case CPP_LSHIFT_EQ:
19950           code = LSHIFT_EXPR;
19951           break;
19952         case CPP_RSHIFT_EQ:
19953           code = RSHIFT_EXPR;
19954           break;
19955         case CPP_AND_EQ:
19956           code = BIT_AND_EXPR;
19957           break;
19958         case CPP_OR_EQ:
19959           code = BIT_IOR_EXPR;
19960           break;
19961         case CPP_XOR_EQ:
19962           code = BIT_XOR_EXPR;
19963           break;
19964         default:
19965           cp_parser_error (parser,
19966                            "invalid operator for %<#pragma omp atomic%>");
19967           goto saw_error;
19968         }
19969       cp_lexer_consume_token (parser->lexer);
19970
19971       rhs = cp_parser_expression (parser, false);
19972       if (rhs == error_mark_node)
19973         goto saw_error;
19974       break;
19975     }
19976   finish_omp_atomic (code, lhs, rhs);
19977   cp_parser_consume_semicolon_at_end_of_statement (parser);
19978   return;
19979
19980  saw_error:
19981   cp_parser_skip_to_end_of_block_or_statement (parser);
19982 }
19983
19984
19985 /* OpenMP 2.5:
19986    # pragma omp barrier new-line  */
19987
19988 static void
19989 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19990 {
19991   cp_parser_require_pragma_eol (parser, pragma_tok);
19992   finish_omp_barrier ();
19993 }
19994
19995 /* OpenMP 2.5:
19996    # pragma omp critical [(name)] new-line
19997      structured-block  */
19998
19999 static tree
20000 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20001 {
20002   tree stmt, name = NULL;
20003
20004   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20005     {
20006       cp_lexer_consume_token (parser->lexer);
20007
20008       name = cp_parser_identifier (parser);
20009
20010       if (name == error_mark_node
20011           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20012         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20013                                                /*or_comma=*/false,
20014                                                /*consume_paren=*/true);
20015       if (name == error_mark_node)
20016         name = NULL;
20017     }
20018   cp_parser_require_pragma_eol (parser, pragma_tok);
20019
20020   stmt = cp_parser_omp_structured_block (parser);
20021   return c_finish_omp_critical (stmt, name);
20022 }
20023
20024 /* OpenMP 2.5:
20025    # pragma omp flush flush-vars[opt] new-line
20026
20027    flush-vars:
20028      ( variable-list ) */
20029
20030 static void
20031 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20032 {
20033   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20034     (void) cp_parser_omp_var_list (parser, 0, NULL);
20035   cp_parser_require_pragma_eol (parser, pragma_tok);
20036
20037   finish_omp_flush ();
20038 }
20039
20040 /* Parse the restricted form of the for statment allowed by OpenMP.  */
20041
20042 static tree
20043 cp_parser_omp_for_loop (cp_parser *parser)
20044 {
20045   tree init, cond, incr, body, decl, pre_body;
20046   location_t loc;
20047
20048   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20049     {
20050       cp_parser_error (parser, "for statement expected");
20051       return NULL;
20052     }
20053   loc = cp_lexer_consume_token (parser->lexer)->location;
20054   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
20055     return NULL;
20056
20057   init = decl = NULL;
20058   pre_body = push_stmt_list ();
20059   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20060     {
20061       cp_decl_specifier_seq type_specifiers;
20062
20063       /* First, try to parse as an initialized declaration.  See
20064          cp_parser_condition, from whence the bulk of this is copied.  */
20065
20066       cp_parser_parse_tentatively (parser);
20067       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20068                                     &type_specifiers);
20069       if (!cp_parser_error_occurred (parser))
20070         {
20071           tree asm_specification, attributes;
20072           cp_declarator *declarator;
20073
20074           declarator = cp_parser_declarator (parser,
20075                                              CP_PARSER_DECLARATOR_NAMED,
20076                                              /*ctor_dtor_or_conv_p=*/NULL,
20077                                              /*parenthesized_p=*/NULL,
20078                                              /*member_p=*/false);
20079           attributes = cp_parser_attributes_opt (parser);
20080           asm_specification = cp_parser_asm_specification_opt (parser);
20081
20082           cp_parser_require (parser, CPP_EQ, "`='");
20083           if (cp_parser_parse_definitely (parser))
20084             {
20085               tree pushed_scope;
20086
20087               decl = start_decl (declarator, &type_specifiers,
20088                                  /*initialized_p=*/false, attributes,
20089                                  /*prefix_attributes=*/NULL_TREE,
20090                                  &pushed_scope);
20091
20092               init = cp_parser_assignment_expression (parser, false);
20093
20094               if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
20095                 init = error_mark_node;
20096               else
20097                 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
20098                                 asm_specification, LOOKUP_ONLYCONVERTING);
20099
20100               if (pushed_scope)
20101                 pop_scope (pushed_scope);
20102             }
20103         }
20104       else
20105         cp_parser_abort_tentative_parse (parser);
20106
20107       /* If parsing as an initialized declaration failed, try again as
20108          a simple expression.  */
20109       if (decl == NULL)
20110         init = cp_parser_expression (parser, false);
20111     }
20112   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20113   pre_body = pop_stmt_list (pre_body);
20114
20115   cond = NULL;
20116   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20117     cond = cp_parser_condition (parser);
20118   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20119
20120   incr = NULL;
20121   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20122     incr = cp_parser_expression (parser, false);
20123
20124   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20125     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20126                                            /*or_comma=*/false,
20127                                            /*consume_paren=*/true);
20128
20129   /* Note that we saved the original contents of this flag when we entered
20130      the structured block, and so we don't need to re-save it here.  */
20131   parser->in_statement = IN_OMP_FOR;
20132
20133   /* Note that the grammar doesn't call for a structured block here,
20134      though the loop as a whole is a structured block.  */
20135   body = push_stmt_list ();
20136   cp_parser_statement (parser, NULL_TREE, false, NULL);
20137   body = pop_stmt_list (body);
20138
20139   return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
20140 }
20141
20142 /* OpenMP 2.5:
20143    #pragma omp for for-clause[optseq] new-line
20144      for-loop  */
20145
20146 #define OMP_FOR_CLAUSE_MASK                             \
20147         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20148         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20149         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
20150         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
20151         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
20152         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
20153         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20154
20155 static tree
20156 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
20157 {
20158   tree clauses, sb, ret;
20159   unsigned int save;
20160
20161   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
20162                                        "#pragma omp for", pragma_tok);
20163
20164   sb = begin_omp_structured_block ();
20165   save = cp_parser_begin_omp_structured_block (parser);
20166
20167   ret = cp_parser_omp_for_loop (parser);
20168   if (ret)
20169     OMP_FOR_CLAUSES (ret) = clauses;
20170
20171   cp_parser_end_omp_structured_block (parser, save);
20172   add_stmt (finish_omp_structured_block (sb));
20173
20174   return ret;
20175 }
20176
20177 /* OpenMP 2.5:
20178    # pragma omp master new-line
20179      structured-block  */
20180
20181 static tree
20182 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
20183 {
20184   cp_parser_require_pragma_eol (parser, pragma_tok);
20185   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
20186 }
20187
20188 /* OpenMP 2.5:
20189    # pragma omp ordered new-line
20190      structured-block  */
20191
20192 static tree
20193 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
20194 {
20195   cp_parser_require_pragma_eol (parser, pragma_tok);
20196   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
20197 }
20198
20199 /* OpenMP 2.5:
20200
20201    section-scope:
20202      { section-sequence }
20203
20204    section-sequence:
20205      section-directive[opt] structured-block
20206      section-sequence section-directive structured-block  */
20207
20208 static tree
20209 cp_parser_omp_sections_scope (cp_parser *parser)
20210 {
20211   tree stmt, substmt;
20212   bool error_suppress = false;
20213   cp_token *tok;
20214
20215   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
20216     return NULL_TREE;
20217
20218   stmt = push_stmt_list ();
20219
20220   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
20221     {
20222       unsigned save;
20223
20224       substmt = begin_omp_structured_block ();
20225       save = cp_parser_begin_omp_structured_block (parser);
20226
20227       while (1)
20228         {
20229           cp_parser_statement (parser, NULL_TREE, false, NULL);
20230
20231           tok = cp_lexer_peek_token (parser->lexer);
20232           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20233             break;
20234           if (tok->type == CPP_CLOSE_BRACE)
20235             break;
20236           if (tok->type == CPP_EOF)
20237             break;
20238         }
20239
20240       cp_parser_end_omp_structured_block (parser, save);
20241       substmt = finish_omp_structured_block (substmt);
20242       substmt = build1 (OMP_SECTION, void_type_node, substmt);
20243       add_stmt (substmt);
20244     }
20245
20246   while (1)
20247     {
20248       tok = cp_lexer_peek_token (parser->lexer);
20249       if (tok->type == CPP_CLOSE_BRACE)
20250         break;
20251       if (tok->type == CPP_EOF)
20252         break;
20253
20254       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20255         {
20256           cp_lexer_consume_token (parser->lexer);
20257           cp_parser_require_pragma_eol (parser, tok);
20258           error_suppress = false;
20259         }
20260       else if (!error_suppress)
20261         {
20262           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
20263           error_suppress = true;
20264         }
20265
20266       substmt = cp_parser_omp_structured_block (parser);
20267       substmt = build1 (OMP_SECTION, void_type_node, substmt);
20268       add_stmt (substmt);
20269     }
20270   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
20271
20272   substmt = pop_stmt_list (stmt);
20273
20274   stmt = make_node (OMP_SECTIONS);
20275   TREE_TYPE (stmt) = void_type_node;
20276   OMP_SECTIONS_BODY (stmt) = substmt;
20277
20278   add_stmt (stmt);
20279   return stmt;
20280 }
20281
20282 /* OpenMP 2.5:
20283    # pragma omp sections sections-clause[optseq] newline
20284      sections-scope  */
20285
20286 #define OMP_SECTIONS_CLAUSE_MASK                        \
20287         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20288         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20289         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
20290         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
20291         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20292
20293 static tree
20294 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
20295 {
20296   tree clauses, ret;
20297
20298   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
20299                                        "#pragma omp sections", pragma_tok);
20300
20301   ret = cp_parser_omp_sections_scope (parser);
20302   if (ret)
20303     OMP_SECTIONS_CLAUSES (ret) = clauses;
20304
20305   return ret;
20306 }
20307
20308 /* OpenMP 2.5:
20309    # pragma parallel parallel-clause new-line
20310    # pragma parallel for parallel-for-clause new-line
20311    # pragma parallel sections parallel-sections-clause new-line  */
20312
20313 #define OMP_PARALLEL_CLAUSE_MASK                        \
20314         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
20315         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20316         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20317         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
20318         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
20319         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
20320         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
20321         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
20322
20323 static tree
20324 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
20325 {
20326   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
20327   const char *p_name = "#pragma omp parallel";
20328   tree stmt, clauses, par_clause, ws_clause, block;
20329   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
20330   unsigned int save;
20331
20332   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20333     {
20334       cp_lexer_consume_token (parser->lexer);
20335       p_kind = PRAGMA_OMP_PARALLEL_FOR;
20336       p_name = "#pragma omp parallel for";
20337       mask |= OMP_FOR_CLAUSE_MASK;
20338       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20339     }
20340   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20341     {
20342       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20343       const char *p = IDENTIFIER_POINTER (id);
20344       if (strcmp (p, "sections") == 0)
20345         {
20346           cp_lexer_consume_token (parser->lexer);
20347           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
20348           p_name = "#pragma omp parallel sections";
20349           mask |= OMP_SECTIONS_CLAUSE_MASK;
20350           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20351         }
20352     }
20353
20354   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
20355   block = begin_omp_parallel ();
20356   save = cp_parser_begin_omp_structured_block (parser);
20357
20358   switch (p_kind)
20359     {
20360     case PRAGMA_OMP_PARALLEL:
20361       cp_parser_statement (parser, NULL_TREE, false, NULL);
20362       par_clause = clauses;
20363       break;
20364
20365     case PRAGMA_OMP_PARALLEL_FOR:
20366       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20367       stmt = cp_parser_omp_for_loop (parser);
20368       if (stmt)
20369         OMP_FOR_CLAUSES (stmt) = ws_clause;
20370       break;
20371
20372     case PRAGMA_OMP_PARALLEL_SECTIONS:
20373       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20374       stmt = cp_parser_omp_sections_scope (parser);
20375       if (stmt)
20376         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
20377       break;
20378
20379     default:
20380       gcc_unreachable ();
20381     }
20382
20383   cp_parser_end_omp_structured_block (parser, save);
20384   stmt = finish_omp_parallel (par_clause, block);
20385   if (p_kind != PRAGMA_OMP_PARALLEL)
20386     OMP_PARALLEL_COMBINED (stmt) = 1;
20387   return stmt;
20388 }
20389
20390 /* OpenMP 2.5:
20391    # pragma omp single single-clause[optseq] new-line
20392      structured-block  */
20393
20394 #define OMP_SINGLE_CLAUSE_MASK                          \
20395         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20396         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20397         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
20398         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20399
20400 static tree
20401 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
20402 {
20403   tree stmt = make_node (OMP_SINGLE);
20404   TREE_TYPE (stmt) = void_type_node;
20405
20406   OMP_SINGLE_CLAUSES (stmt)
20407     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
20408                                  "#pragma omp single", pragma_tok);
20409   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
20410
20411   return add_stmt (stmt);
20412 }
20413
20414 /* OpenMP 2.5:
20415    # pragma omp threadprivate (variable-list) */
20416
20417 static void
20418 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20419 {
20420   tree vars;
20421
20422   vars = cp_parser_omp_var_list (parser, 0, NULL);
20423   cp_parser_require_pragma_eol (parser, pragma_tok);
20424
20425   finish_omp_threadprivate (vars);
20426 }
20427
20428 /* Main entry point to OpenMP statement pragmas.  */
20429
20430 static void
20431 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20432 {
20433   tree stmt;
20434
20435   switch (pragma_tok->pragma_kind)
20436     {
20437     case PRAGMA_OMP_ATOMIC:
20438       cp_parser_omp_atomic (parser, pragma_tok);
20439       return;
20440     case PRAGMA_OMP_CRITICAL:
20441       stmt = cp_parser_omp_critical (parser, pragma_tok);
20442       break;
20443     case PRAGMA_OMP_FOR:
20444       stmt = cp_parser_omp_for (parser, pragma_tok);
20445       break;
20446     case PRAGMA_OMP_MASTER:
20447       stmt = cp_parser_omp_master (parser, pragma_tok);
20448       break;
20449     case PRAGMA_OMP_ORDERED:
20450       stmt = cp_parser_omp_ordered (parser, pragma_tok);
20451       break;
20452     case PRAGMA_OMP_PARALLEL:
20453       stmt = cp_parser_omp_parallel (parser, pragma_tok);
20454       break;
20455     case PRAGMA_OMP_SECTIONS:
20456       stmt = cp_parser_omp_sections (parser, pragma_tok);
20457       break;
20458     case PRAGMA_OMP_SINGLE:
20459       stmt = cp_parser_omp_single (parser, pragma_tok);
20460       break;
20461     default:
20462       gcc_unreachable ();
20463     }
20464
20465   if (stmt)
20466     SET_EXPR_LOCATION (stmt, pragma_tok->location);
20467 }
20468 \f
20469 /* The parser.  */
20470
20471 static GTY (()) cp_parser *the_parser;
20472
20473 \f
20474 /* Special handling for the first token or line in the file.  The first
20475    thing in the file might be #pragma GCC pch_preprocess, which loads a
20476    PCH file, which is a GC collection point.  So we need to handle this
20477    first pragma without benefit of an existing lexer structure.
20478
20479    Always returns one token to the caller in *FIRST_TOKEN.  This is
20480    either the true first token of the file, or the first token after
20481    the initial pragma.  */
20482
20483 static void
20484 cp_parser_initial_pragma (cp_token *first_token)
20485 {
20486   tree name = NULL;
20487
20488   cp_lexer_get_preprocessor_token (NULL, first_token);
20489   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20490     return;
20491
20492   cp_lexer_get_preprocessor_token (NULL, first_token);
20493   if (first_token->type == CPP_STRING)
20494     {
20495       name = first_token->u.value;
20496
20497       cp_lexer_get_preprocessor_token (NULL, first_token);
20498       if (first_token->type != CPP_PRAGMA_EOL)
20499         error ("junk at end of %<#pragma GCC pch_preprocess%>");
20500     }
20501   else
20502     error ("expected string literal");
20503
20504   /* Skip to the end of the pragma.  */
20505   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20506     cp_lexer_get_preprocessor_token (NULL, first_token);
20507
20508   /* Now actually load the PCH file.  */
20509   if (name)
20510     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20511
20512   /* Read one more token to return to our caller.  We have to do this
20513      after reading the PCH file in, since its pointers have to be
20514      live.  */
20515   cp_lexer_get_preprocessor_token (NULL, first_token);
20516 }
20517
20518 /* Normal parsing of a pragma token.  Here we can (and must) use the
20519    regular lexer.  */
20520
20521 static bool
20522 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20523 {
20524   cp_token *pragma_tok;
20525   unsigned int id;
20526
20527   pragma_tok = cp_lexer_consume_token (parser->lexer);
20528   gcc_assert (pragma_tok->type == CPP_PRAGMA);
20529   parser->lexer->in_pragma = true;
20530
20531   id = pragma_tok->pragma_kind;
20532   switch (id)
20533     {
20534     case PRAGMA_GCC_PCH_PREPROCESS:
20535       error ("%<#pragma GCC pch_preprocess%> must be first");
20536       break;
20537
20538     case PRAGMA_OMP_BARRIER:
20539       switch (context)
20540         {
20541         case pragma_compound:
20542           cp_parser_omp_barrier (parser, pragma_tok);
20543           return false;
20544         case pragma_stmt:
20545           error ("%<#pragma omp barrier%> may only be "
20546                  "used in compound statements");
20547           break;
20548         default:
20549           goto bad_stmt;
20550         }
20551       break;
20552
20553     case PRAGMA_OMP_FLUSH:
20554       switch (context)
20555         {
20556         case pragma_compound:
20557           cp_parser_omp_flush (parser, pragma_tok);
20558           return false;
20559         case pragma_stmt:
20560           error ("%<#pragma omp flush%> may only be "
20561                  "used in compound statements");
20562           break;
20563         default:
20564           goto bad_stmt;
20565         }
20566       break;
20567
20568     case PRAGMA_OMP_THREADPRIVATE:
20569       cp_parser_omp_threadprivate (parser, pragma_tok);
20570       return false;
20571
20572     case PRAGMA_OMP_ATOMIC:
20573     case PRAGMA_OMP_CRITICAL:
20574     case PRAGMA_OMP_FOR:
20575     case PRAGMA_OMP_MASTER:
20576     case PRAGMA_OMP_ORDERED:
20577     case PRAGMA_OMP_PARALLEL:
20578     case PRAGMA_OMP_SECTIONS:
20579     case PRAGMA_OMP_SINGLE:
20580       if (context == pragma_external)
20581         goto bad_stmt;
20582       cp_parser_omp_construct (parser, pragma_tok);
20583       return true;
20584
20585     case PRAGMA_OMP_SECTION:
20586       error ("%<#pragma omp section%> may only be used in "
20587              "%<#pragma omp sections%> construct");
20588       break;
20589
20590     default:
20591       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20592       c_invoke_pragma_handler (id);
20593       break;
20594
20595     bad_stmt:
20596       cp_parser_error (parser, "expected declaration specifiers");
20597       break;
20598     }
20599
20600   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20601   return false;
20602 }
20603
20604 /* The interface the pragma parsers have to the lexer.  */
20605
20606 enum cpp_ttype
20607 pragma_lex (tree *value)
20608 {
20609   cp_token *tok;
20610   enum cpp_ttype ret;
20611
20612   tok = cp_lexer_peek_token (the_parser->lexer);
20613
20614   ret = tok->type;
20615   *value = tok->u.value;
20616
20617   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20618     ret = CPP_EOF;
20619   else if (ret == CPP_STRING)
20620     *value = cp_parser_string_literal (the_parser, false, false);
20621   else
20622     {
20623       cp_lexer_consume_token (the_parser->lexer);
20624       if (ret == CPP_KEYWORD)
20625         ret = CPP_NAME;
20626     }
20627
20628   return ret;
20629 }
20630
20631 \f
20632 /* External interface.  */
20633
20634 /* Parse one entire translation unit.  */
20635
20636 void
20637 c_parse_file (void)
20638 {
20639   bool error_occurred;
20640   static bool already_called = false;
20641
20642   if (already_called)
20643     {
20644       sorry ("inter-module optimizations not implemented for C++");
20645       return;
20646     }
20647   already_called = true;
20648
20649   the_parser = cp_parser_new ();
20650   push_deferring_access_checks (flag_access_control
20651                                 ? dk_no_deferred : dk_no_check);
20652   error_occurred = cp_parser_translation_unit (the_parser);
20653   the_parser = NULL;
20654 }
20655
20656 #include "gt-cp-parser.h"