c-common.c (fname_as_string): Update.
[platform/upstream/gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007  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 input file stack index at which this token was found.  */
83   unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
84   /* The value associated with this token, if any.  */
85   union cp_token_value {
86     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
87     struct tree_check* GTY((tag ("1"))) tree_check_value;
88     /* Use for all other tokens.  */
89     tree GTY((tag ("0"))) value;
90   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
91   /* The location at which this token was found.  */
92   location_t location;
93 } cp_token;
94
95 /* We use a stack of token pointer for saving token sets.  */
96 typedef struct cp_token *cp_token_position;
97 DEF_VEC_P (cp_token_position);
98 DEF_VEC_ALLOC_P (cp_token_position,heap);
99
100 static cp_token eof_token =
101 {
102   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
103 #if USE_MAPPED_LOCATION
104   0
105 #else
106   {0, 0}
107 #endif
108 };
109
110 /* The cp_lexer structure represents the C++ lexer.  It is responsible
111    for managing the token stream from the preprocessor and supplying
112    it to the parser.  Tokens are never added to the cp_lexer after
113    it is created.  */
114
115 typedef struct cp_lexer GTY (())
116 {
117   /* The memory allocated for the buffer.  NULL if this lexer does not
118      own the token buffer.  */
119   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
120   /* If the lexer owns the buffer, this is the number of tokens in the
121      buffer.  */
122   size_t buffer_length;
123
124   /* A pointer just past the last available token.  The tokens
125      in this lexer are [buffer, last_token).  */
126   cp_token_position GTY ((skip)) last_token;
127
128   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
129      no more available tokens.  */
130   cp_token_position GTY ((skip)) next_token;
131
132   /* A stack indicating positions at which cp_lexer_save_tokens was
133      called.  The top entry is the most recent position at which we
134      began saving tokens.  If the stack is non-empty, we are saving
135      tokens.  */
136   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
137
138   /* The next lexer in a linked list of lexers.  */
139   struct cp_lexer *next;
140
141   /* True if we should output debugging information.  */
142   bool debugging_p;
143
144   /* True if we're in the context of parsing a pragma, and should not
145      increment past the end-of-line marker.  */
146   bool in_pragma;
147 } cp_lexer;
148
149 /* cp_token_cache is a range of tokens.  There is no need to represent
150    allocate heap memory for it, since tokens are never removed from the
151    lexer's array.  There is also no need for the GC to walk through
152    a cp_token_cache, since everything in here is referenced through
153    a lexer.  */
154
155 typedef struct cp_token_cache GTY(())
156 {
157   /* The beginning of the token range.  */
158   cp_token * GTY((skip)) first;
159
160   /* Points immediately after the last token in the range.  */
161   cp_token * GTY ((skip)) last;
162 } cp_token_cache;
163
164 /* Prototypes.  */
165
166 static cp_lexer *cp_lexer_new_main
167   (void);
168 static cp_lexer *cp_lexer_new_from_tokens
169   (cp_token_cache *tokens);
170 static void cp_lexer_destroy
171   (cp_lexer *);
172 static int cp_lexer_saving_tokens
173   (const cp_lexer *);
174 static cp_token_position cp_lexer_token_position
175   (cp_lexer *, bool);
176 static cp_token *cp_lexer_token_at
177   (cp_lexer *, cp_token_position);
178 static void cp_lexer_get_preprocessor_token
179   (cp_lexer *, cp_token *);
180 static inline cp_token *cp_lexer_peek_token
181   (cp_lexer *);
182 static cp_token *cp_lexer_peek_nth_token
183   (cp_lexer *, size_t);
184 static inline bool cp_lexer_next_token_is
185   (cp_lexer *, enum cpp_ttype);
186 static bool cp_lexer_next_token_is_not
187   (cp_lexer *, enum cpp_ttype);
188 static bool cp_lexer_next_token_is_keyword
189   (cp_lexer *, enum rid);
190 static cp_token *cp_lexer_consume_token
191   (cp_lexer *);
192 static void cp_lexer_purge_token
193   (cp_lexer *);
194 static void cp_lexer_purge_tokens_after
195   (cp_lexer *, cp_token_position);
196 static void cp_lexer_save_tokens
197   (cp_lexer *);
198 static void cp_lexer_commit_tokens
199   (cp_lexer *);
200 static void cp_lexer_rollback_tokens
201   (cp_lexer *);
202 #ifdef ENABLE_CHECKING
203 static void cp_lexer_print_token
204   (FILE *, cp_token *);
205 static inline bool cp_lexer_debugging_p
206   (cp_lexer *);
207 static void cp_lexer_start_debugging
208   (cp_lexer *) ATTRIBUTE_UNUSED;
209 static void cp_lexer_stop_debugging
210   (cp_lexer *) ATTRIBUTE_UNUSED;
211 #else
212 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
213    about passing NULL to functions that require non-NULL arguments
214    (fputs, fprintf).  It will never be used, so all we need is a value
215    of the right type that's guaranteed not to be NULL.  */
216 #define cp_lexer_debug_stream stdout
217 #define cp_lexer_print_token(str, tok) (void) 0
218 #define cp_lexer_debugging_p(lexer) 0
219 #endif /* ENABLE_CHECKING */
220
221 static cp_token_cache *cp_token_cache_new
222   (cp_token *, cp_token *);
223
224 static void cp_parser_initial_pragma
225   (cp_token *);
226
227 /* Manifest constants.  */
228 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
229 #define CP_SAVED_TOKEN_STACK 5
230
231 /* A token type for keywords, as opposed to ordinary identifiers.  */
232 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
233
234 /* A token type for template-ids.  If a template-id is processed while
235    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
236    the value of the CPP_TEMPLATE_ID is whatever was returned by
237    cp_parser_template_id.  */
238 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
239
240 /* A token type for nested-name-specifiers.  If a
241    nested-name-specifier is processed while parsing tentatively, it is
242    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
243    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
244    cp_parser_nested_name_specifier_opt.  */
245 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
246
247 /* A token type for tokens that are not tokens at all; these are used
248    to represent slots in the array where there used to be a token
249    that has now been deleted.  */
250 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
251
252 /* The number of token types, including C++-specific ones.  */
253 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
254
255 /* Variables.  */
256
257 #ifdef ENABLE_CHECKING
258 /* The stream to which debugging output should be written.  */
259 static FILE *cp_lexer_debug_stream;
260 #endif /* ENABLE_CHECKING */
261
262 /* Create a new main C++ lexer, the lexer that gets tokens from the
263    preprocessor.  */
264
265 static cp_lexer *
266 cp_lexer_new_main (void)
267 {
268   cp_token first_token;
269   cp_lexer *lexer;
270   cp_token *pos;
271   size_t alloc;
272   size_t space;
273   cp_token *buffer;
274
275   /* It's possible that parsing the first pragma will load a PCH file,
276      which is a GC collection point.  So we have to do that before
277      allocating any memory.  */
278   cp_parser_initial_pragma (&first_token);
279
280   c_common_no_more_pch ();
281
282   /* Allocate the memory.  */
283   lexer = GGC_CNEW (cp_lexer);
284
285 #ifdef ENABLE_CHECKING
286   /* Initially we are not debugging.  */
287   lexer->debugging_p = false;
288 #endif /* ENABLE_CHECKING */
289   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
290                                    CP_SAVED_TOKEN_STACK);
291
292   /* Create the buffer.  */
293   alloc = CP_LEXER_BUFFER_SIZE;
294   buffer = GGC_NEWVEC (cp_token, alloc);
295
296   /* Put the first token in the buffer.  */
297   space = alloc;
298   pos = buffer;
299   *pos = first_token;
300
301   /* Get the remaining tokens from the preprocessor.  */
302   while (pos->type != CPP_EOF)
303     {
304       pos++;
305       if (!--space)
306         {
307           space = alloc;
308           alloc *= 2;
309           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
310           pos = buffer + space;
311         }
312       cp_lexer_get_preprocessor_token (lexer, pos);
313     }
314   lexer->buffer = buffer;
315   lexer->buffer_length = alloc - space;
316   lexer->last_token = pos;
317   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
318
319   /* Subsequent preprocessor diagnostics should use compiler
320      diagnostic functions to get the compiler source location.  */
321   cpp_get_options (parse_in)->client_diagnostic = true;
322   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
323
324   gcc_assert (lexer->next_token->type != CPP_PURGED);
325   return lexer;
326 }
327
328 /* Create a new lexer whose token stream is primed with the tokens in
329    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
330
331 static cp_lexer *
332 cp_lexer_new_from_tokens (cp_token_cache *cache)
333 {
334   cp_token *first = cache->first;
335   cp_token *last = cache->last;
336   cp_lexer *lexer = GGC_CNEW (cp_lexer);
337
338   /* We do not own the buffer.  */
339   lexer->buffer = NULL;
340   lexer->buffer_length = 0;
341   lexer->next_token = first == last ? &eof_token : first;
342   lexer->last_token = last;
343
344   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
345                                    CP_SAVED_TOKEN_STACK);
346
347 #ifdef ENABLE_CHECKING
348   /* Initially we are not debugging.  */
349   lexer->debugging_p = false;
350 #endif
351
352   gcc_assert (lexer->next_token->type != CPP_PURGED);
353   return lexer;
354 }
355
356 /* Frees all resources associated with LEXER.  */
357
358 static void
359 cp_lexer_destroy (cp_lexer *lexer)
360 {
361   if (lexer->buffer)
362     ggc_free (lexer->buffer);
363   VEC_free (cp_token_position, heap, lexer->saved_tokens);
364   ggc_free (lexer);
365 }
366
367 /* Returns nonzero if debugging information should be output.  */
368
369 #ifdef ENABLE_CHECKING
370
371 static inline bool
372 cp_lexer_debugging_p (cp_lexer *lexer)
373 {
374   return lexer->debugging_p;
375 }
376
377 #endif /* ENABLE_CHECKING */
378
379 static inline cp_token_position
380 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
381 {
382   gcc_assert (!previous_p || lexer->next_token != &eof_token);
383
384   return lexer->next_token - previous_p;
385 }
386
387 static inline cp_token *
388 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
389 {
390   return pos;
391 }
392
393 /* nonzero if we are presently saving tokens.  */
394
395 static inline int
396 cp_lexer_saving_tokens (const cp_lexer* lexer)
397 {
398   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
399 }
400
401 /* Store the next token from the preprocessor in *TOKEN.  Return true
402    if we reach EOF.  If LEXER is NULL, assume we are handling an
403    initial #pragma pch_preprocess, and thus want the lexer to return
404    processed strings.  */
405
406 static void
407 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
408 {
409   static int is_extern_c = 0;
410
411    /* Get a new token from the preprocessor.  */
412   token->type
413     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
414                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
415   token->input_file_stack_index = input_file_stack_tick;
416   token->keyword = RID_MAX;
417   token->pragma_kind = PRAGMA_NONE;
418   token->in_system_header = in_system_header;
419
420   /* On some systems, some header files are surrounded by an
421      implicit extern "C" block.  Set a flag in the token if it
422      comes from such a header.  */
423   is_extern_c += pending_lang_change;
424   pending_lang_change = 0;
425   token->implicit_extern_c = is_extern_c > 0;
426
427   /* Check to see if this token is a keyword.  */
428   if (token->type == CPP_NAME)
429     {
430       if (C_IS_RESERVED_WORD (token->u.value))
431         {
432           /* Mark this token as a keyword.  */
433           token->type = CPP_KEYWORD;
434           /* Record which keyword.  */
435           token->keyword = C_RID_CODE (token->u.value);
436           /* Update the value.  Some keywords are mapped to particular
437              entities, rather than simply having the value of the
438              corresponding IDENTIFIER_NODE.  For example, `__const' is
439              mapped to `const'.  */
440           token->u.value = ridpointers[token->keyword];
441         }
442       else
443         {
444           if (warn_cxx0x_compat
445               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
446               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
447             {
448               /* Warn about the C++0x keyword (but still treat it as
449                  an identifier).  */
450               warning (OPT_Wc__0x_compat, 
451                        "identifier %<%s%> will become a keyword in C++0x",
452                        IDENTIFIER_POINTER (token->u.value));
453
454               /* Clear out the C_RID_CODE so we don't warn about this
455                  particular identifier-turned-keyword again.  */
456               C_RID_CODE (token->u.value) = RID_MAX;
457             }
458
459           token->ambiguous_p = false;
460           token->keyword = RID_MAX;
461         }
462     }
463   /* Handle Objective-C++ keywords.  */
464   else if (token->type == CPP_AT_NAME)
465     {
466       token->type = CPP_KEYWORD;
467       switch (C_RID_CODE (token->u.value))
468         {
469         /* Map 'class' to '@class', 'private' to '@private', etc.  */
470         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
471         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
472         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
473         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
474         case RID_THROW: token->keyword = RID_AT_THROW; break;
475         case RID_TRY: token->keyword = RID_AT_TRY; break;
476         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
477         default: token->keyword = C_RID_CODE (token->u.value);
478         }
479     }
480   else if (token->type == CPP_PRAGMA)
481     {
482       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
483       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
484       token->u.value = NULL_TREE;
485     }
486 }
487
488 /* Update the globals input_location and in_system_header and the
489    input file stack from TOKEN.  */
490 static inline void
491 cp_lexer_set_source_position_from_token (cp_token *token)
492 {
493   if (token->type != CPP_EOF)
494     {
495       input_location = token->location;
496       in_system_header = token->in_system_header;
497       restore_input_file_stack (token->input_file_stack_index);
498     }
499 }
500
501 /* Return a pointer to the next token in the token stream, but do not
502    consume it.  */
503
504 static inline cp_token *
505 cp_lexer_peek_token (cp_lexer *lexer)
506 {
507   if (cp_lexer_debugging_p (lexer))
508     {
509       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
510       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
511       putc ('\n', cp_lexer_debug_stream);
512     }
513   return lexer->next_token;
514 }
515
516 /* Return true if the next token has the indicated TYPE.  */
517
518 static inline bool
519 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
520 {
521   return cp_lexer_peek_token (lexer)->type == type;
522 }
523
524 /* Return true if the next token does not have the indicated TYPE.  */
525
526 static inline bool
527 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
528 {
529   return !cp_lexer_next_token_is (lexer, type);
530 }
531
532 /* Return true if the next token is the indicated KEYWORD.  */
533
534 static inline bool
535 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
536 {
537   return cp_lexer_peek_token (lexer)->keyword == keyword;
538 }
539
540 /* Return true if the next token is a keyword for a decl-specifier.  */
541
542 static bool
543 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
544 {
545   cp_token *token;
546
547   token = cp_lexer_peek_token (lexer);
548   switch (token->keyword) 
549     {
550       /* Storage classes.  */
551     case RID_AUTO:
552     case RID_REGISTER:
553     case RID_STATIC:
554     case RID_EXTERN:
555     case RID_MUTABLE:
556     case RID_THREAD:
557       /* Elaborated type specifiers.  */
558     case RID_ENUM:
559     case RID_CLASS:
560     case RID_STRUCT:
561     case RID_UNION:
562     case RID_TYPENAME:
563       /* Simple type specifiers.  */
564     case RID_CHAR:
565     case RID_WCHAR:
566     case RID_BOOL:
567     case RID_SHORT:
568     case RID_INT:
569     case RID_LONG:
570     case RID_SIGNED:
571     case RID_UNSIGNED:
572     case RID_FLOAT:
573     case RID_DOUBLE:
574     case RID_VOID:
575       /* GNU extensions.  */ 
576     case RID_ATTRIBUTE:
577     case RID_TYPEOF:
578       /* C++0x extensions.  */
579     case RID_DECLTYPE:
580       return true;
581
582     default:
583       return false;
584     }
585 }
586
587 /* Return a pointer to the Nth token in the token stream.  If N is 1,
588    then this is precisely equivalent to cp_lexer_peek_token (except
589    that it is not inline).  One would like to disallow that case, but
590    there is one case (cp_parser_nth_token_starts_template_id) where
591    the caller passes a variable for N and it might be 1.  */
592
593 static cp_token *
594 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
595 {
596   cp_token *token;
597
598   /* N is 1-based, not zero-based.  */
599   gcc_assert (n > 0);
600
601   if (cp_lexer_debugging_p (lexer))
602     fprintf (cp_lexer_debug_stream,
603              "cp_lexer: peeking ahead %ld at token: ", (long)n);
604
605   --n;
606   token = lexer->next_token;
607   gcc_assert (!n || token != &eof_token);
608   while (n != 0)
609     {
610       ++token;
611       if (token == lexer->last_token)
612         {
613           token = &eof_token;
614           break;
615         }
616
617       if (token->type != CPP_PURGED)
618         --n;
619     }
620
621   if (cp_lexer_debugging_p (lexer))
622     {
623       cp_lexer_print_token (cp_lexer_debug_stream, token);
624       putc ('\n', cp_lexer_debug_stream);
625     }
626
627   return token;
628 }
629
630 /* Return the next token, and advance the lexer's next_token pointer
631    to point to the next non-purged token.  */
632
633 static cp_token *
634 cp_lexer_consume_token (cp_lexer* lexer)
635 {
636   cp_token *token = lexer->next_token;
637
638   gcc_assert (token != &eof_token);
639   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
640
641   do
642     {
643       lexer->next_token++;
644       if (lexer->next_token == lexer->last_token)
645         {
646           lexer->next_token = &eof_token;
647           break;
648         }
649
650     }
651   while (lexer->next_token->type == CPP_PURGED);
652
653   cp_lexer_set_source_position_from_token (token);
654
655   /* Provide debugging output.  */
656   if (cp_lexer_debugging_p (lexer))
657     {
658       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
659       cp_lexer_print_token (cp_lexer_debug_stream, token);
660       putc ('\n', cp_lexer_debug_stream);
661     }
662
663   return token;
664 }
665
666 /* Permanently remove the next token from the token stream, and
667    advance the next_token pointer to refer to the next non-purged
668    token.  */
669
670 static void
671 cp_lexer_purge_token (cp_lexer *lexer)
672 {
673   cp_token *tok = lexer->next_token;
674
675   gcc_assert (tok != &eof_token);
676   tok->type = CPP_PURGED;
677   tok->location = UNKNOWN_LOCATION;
678   tok->u.value = NULL_TREE;
679   tok->keyword = RID_MAX;
680
681   do
682     {
683       tok++;
684       if (tok == lexer->last_token)
685         {
686           tok = &eof_token;
687           break;
688         }
689     }
690   while (tok->type == CPP_PURGED);
691   lexer->next_token = tok;
692 }
693
694 /* Permanently remove all tokens after TOK, up to, but not
695    including, the token that will be returned next by
696    cp_lexer_peek_token.  */
697
698 static void
699 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
700 {
701   cp_token *peek = lexer->next_token;
702
703   if (peek == &eof_token)
704     peek = lexer->last_token;
705
706   gcc_assert (tok < peek);
707
708   for ( tok += 1; tok != peek; tok += 1)
709     {
710       tok->type = CPP_PURGED;
711       tok->location = UNKNOWN_LOCATION;
712       tok->u.value = NULL_TREE;
713       tok->keyword = RID_MAX;
714     }
715 }
716
717 /* Begin saving tokens.  All tokens consumed after this point will be
718    preserved.  */
719
720 static void
721 cp_lexer_save_tokens (cp_lexer* lexer)
722 {
723   /* Provide debugging output.  */
724   if (cp_lexer_debugging_p (lexer))
725     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
726
727   VEC_safe_push (cp_token_position, heap,
728                  lexer->saved_tokens, lexer->next_token);
729 }
730
731 /* Commit to the portion of the token stream most recently saved.  */
732
733 static void
734 cp_lexer_commit_tokens (cp_lexer* lexer)
735 {
736   /* Provide debugging output.  */
737   if (cp_lexer_debugging_p (lexer))
738     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
739
740   VEC_pop (cp_token_position, lexer->saved_tokens);
741 }
742
743 /* Return all tokens saved since the last call to cp_lexer_save_tokens
744    to the token stream.  Stop saving tokens.  */
745
746 static void
747 cp_lexer_rollback_tokens (cp_lexer* lexer)
748 {
749   /* Provide debugging output.  */
750   if (cp_lexer_debugging_p (lexer))
751     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
752
753   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
754 }
755
756 /* Print a representation of the TOKEN on the STREAM.  */
757
758 #ifdef ENABLE_CHECKING
759
760 static void
761 cp_lexer_print_token (FILE * stream, cp_token *token)
762 {
763   /* We don't use cpp_type2name here because the parser defines
764      a few tokens of its own.  */
765   static const char *const token_names[] = {
766     /* cpplib-defined token types */
767 #define OP(e, s) #e,
768 #define TK(e, s) #e,
769     TTYPE_TABLE
770 #undef OP
771 #undef TK
772     /* C++ parser token types - see "Manifest constants", above.  */
773     "KEYWORD",
774     "TEMPLATE_ID",
775     "NESTED_NAME_SPECIFIER",
776     "PURGED"
777   };
778
779   /* If we have a name for the token, print it out.  Otherwise, we
780      simply give the numeric code.  */
781   gcc_assert (token->type < ARRAY_SIZE(token_names));
782   fputs (token_names[token->type], stream);
783
784   /* For some tokens, print the associated data.  */
785   switch (token->type)
786     {
787     case CPP_KEYWORD:
788       /* Some keywords have a value that is not an IDENTIFIER_NODE.
789          For example, `struct' is mapped to an INTEGER_CST.  */
790       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
791         break;
792       /* else fall through */
793     case CPP_NAME:
794       fputs (IDENTIFIER_POINTER (token->u.value), stream);
795       break;
796
797     case CPP_STRING:
798     case CPP_WSTRING:
799       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
800       break;
801
802     default:
803       break;
804     }
805 }
806
807 /* Start emitting debugging information.  */
808
809 static void
810 cp_lexer_start_debugging (cp_lexer* lexer)
811 {
812   lexer->debugging_p = true;
813 }
814
815 /* Stop emitting debugging information.  */
816
817 static void
818 cp_lexer_stop_debugging (cp_lexer* lexer)
819 {
820   lexer->debugging_p = false;
821 }
822
823 #endif /* ENABLE_CHECKING */
824
825 /* Create a new cp_token_cache, representing a range of tokens.  */
826
827 static cp_token_cache *
828 cp_token_cache_new (cp_token *first, cp_token *last)
829 {
830   cp_token_cache *cache = GGC_NEW (cp_token_cache);
831   cache->first = first;
832   cache->last = last;
833   return cache;
834 }
835
836 \f
837 /* Decl-specifiers.  */
838
839 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
840
841 static void
842 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
843 {
844   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
845 }
846
847 /* Declarators.  */
848
849 /* Nothing other than the parser should be creating declarators;
850    declarators are a semi-syntactic representation of C++ entities.
851    Other parts of the front end that need to create entities (like
852    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
853
854 static cp_declarator *make_call_declarator
855   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
856 static cp_declarator *make_array_declarator
857   (cp_declarator *, tree);
858 static cp_declarator *make_pointer_declarator
859   (cp_cv_quals, cp_declarator *);
860 static cp_declarator *make_reference_declarator
861   (cp_cv_quals, cp_declarator *, bool);
862 static cp_parameter_declarator *make_parameter_declarator
863   (cp_decl_specifier_seq *, cp_declarator *, tree);
864 static cp_declarator *make_ptrmem_declarator
865   (cp_cv_quals, tree, cp_declarator *);
866
867 /* An erroneous declarator.  */
868 static cp_declarator *cp_error_declarator;
869
870 /* The obstack on which declarators and related data structures are
871    allocated.  */
872 static struct obstack declarator_obstack;
873
874 /* Alloc BYTES from the declarator memory pool.  */
875
876 static inline void *
877 alloc_declarator (size_t bytes)
878 {
879   return obstack_alloc (&declarator_obstack, bytes);
880 }
881
882 /* Allocate a declarator of the indicated KIND.  Clear fields that are
883    common to all declarators.  */
884
885 static cp_declarator *
886 make_declarator (cp_declarator_kind kind)
887 {
888   cp_declarator *declarator;
889
890   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
891   declarator->kind = kind;
892   declarator->attributes = NULL_TREE;
893   declarator->declarator = NULL;
894   declarator->parameter_pack_p = false;
895
896   return declarator;
897 }
898
899 /* Make a declarator for a generalized identifier.  If
900    QUALIFYING_SCOPE is non-NULL, the identifier is
901    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
902    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
903    is, if any.   */
904
905 static cp_declarator *
906 make_id_declarator (tree qualifying_scope, tree unqualified_name,
907                     special_function_kind sfk)
908 {
909   cp_declarator *declarator;
910
911   /* It is valid to write:
912
913        class C { void f(); };
914        typedef C D;
915        void D::f();
916
917      The standard is not clear about whether `typedef const C D' is
918      legal; as of 2002-09-15 the committee is considering that
919      question.  EDG 3.0 allows that syntax.  Therefore, we do as
920      well.  */
921   if (qualifying_scope && TYPE_P (qualifying_scope))
922     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
923
924   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
925               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
926               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
927
928   declarator = make_declarator (cdk_id);
929   declarator->u.id.qualifying_scope = qualifying_scope;
930   declarator->u.id.unqualified_name = unqualified_name;
931   declarator->u.id.sfk = sfk;
932   
933   return declarator;
934 }
935
936 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
937    of modifiers such as const or volatile to apply to the pointer
938    type, represented as identifiers.  */
939
940 cp_declarator *
941 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
942 {
943   cp_declarator *declarator;
944
945   declarator = make_declarator (cdk_pointer);
946   declarator->declarator = target;
947   declarator->u.pointer.qualifiers = cv_qualifiers;
948   declarator->u.pointer.class_type = NULL_TREE;
949   if (target)
950     {
951       declarator->parameter_pack_p = target->parameter_pack_p;
952       target->parameter_pack_p = false;
953     }
954   else
955     declarator->parameter_pack_p = false;
956
957   return declarator;
958 }
959
960 /* Like make_pointer_declarator -- but for references.  */
961
962 cp_declarator *
963 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
964                            bool rvalue_ref)
965 {
966   cp_declarator *declarator;
967
968   declarator = make_declarator (cdk_reference);
969   declarator->declarator = target;
970   declarator->u.reference.qualifiers = cv_qualifiers;
971   declarator->u.reference.rvalue_ref = rvalue_ref;
972   if (target)
973     {
974       declarator->parameter_pack_p = target->parameter_pack_p;
975       target->parameter_pack_p = false;
976     }
977   else
978     declarator->parameter_pack_p = false;
979
980   return declarator;
981 }
982
983 /* Like make_pointer_declarator -- but for a pointer to a non-static
984    member of CLASS_TYPE.  */
985
986 cp_declarator *
987 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
988                         cp_declarator *pointee)
989 {
990   cp_declarator *declarator;
991
992   declarator = make_declarator (cdk_ptrmem);
993   declarator->declarator = pointee;
994   declarator->u.pointer.qualifiers = cv_qualifiers;
995   declarator->u.pointer.class_type = class_type;
996
997   if (pointee)
998     {
999       declarator->parameter_pack_p = pointee->parameter_pack_p;
1000       pointee->parameter_pack_p = false;
1001     }
1002   else
1003     declarator->parameter_pack_p = false;
1004
1005   return declarator;
1006 }
1007
1008 /* Make a declarator for the function given by TARGET, with the
1009    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1010    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1011    indicates what exceptions can be thrown.  */
1012
1013 cp_declarator *
1014 make_call_declarator (cp_declarator *target,
1015                       cp_parameter_declarator *parms,
1016                       cp_cv_quals cv_qualifiers,
1017                       tree exception_specification)
1018 {
1019   cp_declarator *declarator;
1020
1021   declarator = make_declarator (cdk_function);
1022   declarator->declarator = target;
1023   declarator->u.function.parameters = parms;
1024   declarator->u.function.qualifiers = cv_qualifiers;
1025   declarator->u.function.exception_specification = exception_specification;
1026   if (target)
1027     {
1028       declarator->parameter_pack_p = target->parameter_pack_p;
1029       target->parameter_pack_p = false;
1030     }
1031   else
1032     declarator->parameter_pack_p = false;
1033
1034   return declarator;
1035 }
1036
1037 /* Make a declarator for an array of BOUNDS elements, each of which is
1038    defined by ELEMENT.  */
1039
1040 cp_declarator *
1041 make_array_declarator (cp_declarator *element, tree bounds)
1042 {
1043   cp_declarator *declarator;
1044
1045   declarator = make_declarator (cdk_array);
1046   declarator->declarator = element;
1047   declarator->u.array.bounds = bounds;
1048   if (element)
1049     {
1050       declarator->parameter_pack_p = element->parameter_pack_p;
1051       element->parameter_pack_p = false;
1052     }
1053   else
1054     declarator->parameter_pack_p = false;
1055
1056   return declarator;
1057 }
1058
1059 /* Determine whether the declarator we've seen so far can be a
1060    parameter pack, when followed by an ellipsis.  */
1061 static bool 
1062 declarator_can_be_parameter_pack (cp_declarator *declarator)
1063 {
1064   /* Search for a declarator name, or any other declarator that goes
1065      after the point where the ellipsis could appear in a parameter
1066      pack. If we find any of these, then this declarator can not be
1067      made into a parameter pack.  */
1068   bool found = false;
1069   while (declarator && !found)
1070     {
1071       switch ((int)declarator->kind)
1072         {
1073         case cdk_id:
1074         case cdk_error:
1075         case cdk_array:
1076         case cdk_ptrmem:
1077           found = true;
1078           break;
1079           
1080         default:
1081           declarator = declarator->declarator;
1082           break;
1083         }
1084     }
1085
1086   return !found;
1087 }
1088
1089 cp_parameter_declarator *no_parameters;
1090
1091 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1092    DECLARATOR and DEFAULT_ARGUMENT.  */
1093
1094 cp_parameter_declarator *
1095 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1096                            cp_declarator *declarator,
1097                            tree default_argument)
1098 {
1099   cp_parameter_declarator *parameter;
1100
1101   parameter = ((cp_parameter_declarator *)
1102                alloc_declarator (sizeof (cp_parameter_declarator)));
1103   parameter->next = NULL;
1104   if (decl_specifiers)
1105     parameter->decl_specifiers = *decl_specifiers;
1106   else
1107     clear_decl_specs (&parameter->decl_specifiers);
1108   parameter->declarator = declarator;
1109   parameter->default_argument = default_argument;
1110   parameter->ellipsis_p = false;
1111
1112   return parameter;
1113 }
1114
1115 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1116
1117 static bool
1118 function_declarator_p (const cp_declarator *declarator)
1119 {
1120   while (declarator)
1121     {
1122       if (declarator->kind == cdk_function
1123           && declarator->declarator->kind == cdk_id)
1124         return true;
1125       if (declarator->kind == cdk_id
1126           || declarator->kind == cdk_error)
1127         return false;
1128       declarator = declarator->declarator;
1129     }
1130   return false;
1131 }
1132  
1133 /* The parser.  */
1134
1135 /* Overview
1136    --------
1137
1138    A cp_parser parses the token stream as specified by the C++
1139    grammar.  Its job is purely parsing, not semantic analysis.  For
1140    example, the parser breaks the token stream into declarators,
1141    expressions, statements, and other similar syntactic constructs.
1142    It does not check that the types of the expressions on either side
1143    of an assignment-statement are compatible, or that a function is
1144    not declared with a parameter of type `void'.
1145
1146    The parser invokes routines elsewhere in the compiler to perform
1147    semantic analysis and to build up the abstract syntax tree for the
1148    code processed.
1149
1150    The parser (and the template instantiation code, which is, in a
1151    way, a close relative of parsing) are the only parts of the
1152    compiler that should be calling push_scope and pop_scope, or
1153    related functions.  The parser (and template instantiation code)
1154    keeps track of what scope is presently active; everything else
1155    should simply honor that.  (The code that generates static
1156    initializers may also need to set the scope, in order to check
1157    access control correctly when emitting the initializers.)
1158
1159    Methodology
1160    -----------
1161
1162    The parser is of the standard recursive-descent variety.  Upcoming
1163    tokens in the token stream are examined in order to determine which
1164    production to use when parsing a non-terminal.  Some C++ constructs
1165    require arbitrary look ahead to disambiguate.  For example, it is
1166    impossible, in the general case, to tell whether a statement is an
1167    expression or declaration without scanning the entire statement.
1168    Therefore, the parser is capable of "parsing tentatively."  When the
1169    parser is not sure what construct comes next, it enters this mode.
1170    Then, while we attempt to parse the construct, the parser queues up
1171    error messages, rather than issuing them immediately, and saves the
1172    tokens it consumes.  If the construct is parsed successfully, the
1173    parser "commits", i.e., it issues any queued error messages and
1174    the tokens that were being preserved are permanently discarded.
1175    If, however, the construct is not parsed successfully, the parser
1176    rolls back its state completely so that it can resume parsing using
1177    a different alternative.
1178
1179    Future Improvements
1180    -------------------
1181
1182    The performance of the parser could probably be improved substantially.
1183    We could often eliminate the need to parse tentatively by looking ahead
1184    a little bit.  In some places, this approach might not entirely eliminate
1185    the need to parse tentatively, but it might still speed up the average
1186    case.  */
1187
1188 /* Flags that are passed to some parsing functions.  These values can
1189    be bitwise-ored together.  */
1190
1191 typedef enum cp_parser_flags
1192 {
1193   /* No flags.  */
1194   CP_PARSER_FLAGS_NONE = 0x0,
1195   /* The construct is optional.  If it is not present, then no error
1196      should be issued.  */
1197   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1198   /* When parsing a type-specifier, do not allow user-defined types.  */
1199   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1200 } cp_parser_flags;
1201
1202 /* The different kinds of declarators we want to parse.  */
1203
1204 typedef enum cp_parser_declarator_kind
1205 {
1206   /* We want an abstract declarator.  */
1207   CP_PARSER_DECLARATOR_ABSTRACT,
1208   /* We want a named declarator.  */
1209   CP_PARSER_DECLARATOR_NAMED,
1210   /* We don't mind, but the name must be an unqualified-id.  */
1211   CP_PARSER_DECLARATOR_EITHER
1212 } cp_parser_declarator_kind;
1213
1214 /* The precedence values used to parse binary expressions.  The minimum value
1215    of PREC must be 1, because zero is reserved to quickly discriminate
1216    binary operators from other tokens.  */
1217
1218 enum cp_parser_prec
1219 {
1220   PREC_NOT_OPERATOR,
1221   PREC_LOGICAL_OR_EXPRESSION,
1222   PREC_LOGICAL_AND_EXPRESSION,
1223   PREC_INCLUSIVE_OR_EXPRESSION,
1224   PREC_EXCLUSIVE_OR_EXPRESSION,
1225   PREC_AND_EXPRESSION,
1226   PREC_EQUALITY_EXPRESSION,
1227   PREC_RELATIONAL_EXPRESSION,
1228   PREC_SHIFT_EXPRESSION,
1229   PREC_ADDITIVE_EXPRESSION,
1230   PREC_MULTIPLICATIVE_EXPRESSION,
1231   PREC_PM_EXPRESSION,
1232   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1233 };
1234
1235 /* A mapping from a token type to a corresponding tree node type, with a
1236    precedence value.  */
1237
1238 typedef struct cp_parser_binary_operations_map_node
1239 {
1240   /* The token type.  */
1241   enum cpp_ttype token_type;
1242   /* The corresponding tree code.  */
1243   enum tree_code tree_type;
1244   /* The precedence of this operator.  */
1245   enum cp_parser_prec prec;
1246 } cp_parser_binary_operations_map_node;
1247
1248 /* The status of a tentative parse.  */
1249
1250 typedef enum cp_parser_status_kind
1251 {
1252   /* No errors have occurred.  */
1253   CP_PARSER_STATUS_KIND_NO_ERROR,
1254   /* An error has occurred.  */
1255   CP_PARSER_STATUS_KIND_ERROR,
1256   /* We are committed to this tentative parse, whether or not an error
1257      has occurred.  */
1258   CP_PARSER_STATUS_KIND_COMMITTED
1259 } cp_parser_status_kind;
1260
1261 typedef struct cp_parser_expression_stack_entry
1262 {
1263   /* Left hand side of the binary operation we are currently
1264      parsing.  */
1265   tree lhs;
1266   /* Original tree code for left hand side, if it was a binary
1267      expression itself (used for -Wparentheses).  */
1268   enum tree_code lhs_type;
1269   /* Tree code for the binary operation we are parsing.  */
1270   enum tree_code tree_type;
1271   /* Precedence of the binary operation we are parsing.  */
1272   int prec;
1273 } cp_parser_expression_stack_entry;
1274
1275 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1276    entries because precedence levels on the stack are monotonically
1277    increasing.  */
1278 typedef struct cp_parser_expression_stack_entry
1279   cp_parser_expression_stack[NUM_PREC_VALUES];
1280
1281 /* Context that is saved and restored when parsing tentatively.  */
1282 typedef struct cp_parser_context GTY (())
1283 {
1284   /* If this is a tentative parsing context, the status of the
1285      tentative parse.  */
1286   enum cp_parser_status_kind status;
1287   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1288      that are looked up in this context must be looked up both in the
1289      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1290      the context of the containing expression.  */
1291   tree object_type;
1292
1293   /* The next parsing context in the stack.  */
1294   struct cp_parser_context *next;
1295 } cp_parser_context;
1296
1297 /* Prototypes.  */
1298
1299 /* Constructors and destructors.  */
1300
1301 static cp_parser_context *cp_parser_context_new
1302   (cp_parser_context *);
1303
1304 /* Class variables.  */
1305
1306 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1307
1308 /* The operator-precedence table used by cp_parser_binary_expression.
1309    Transformed into an associative array (binops_by_token) by
1310    cp_parser_new.  */
1311
1312 static const cp_parser_binary_operations_map_node binops[] = {
1313   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1314   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1315
1316   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1317   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1318   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319
1320   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1321   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1322
1323   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1324   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1325
1326   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1327   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1328   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1329   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1330
1331   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1332   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1333
1334   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1335
1336   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1337
1338   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1339
1340   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1341
1342   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1343 };
1344
1345 /* The same as binops, but initialized by cp_parser_new so that
1346    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1347    for speed.  */
1348 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1349
1350 /* Constructors and destructors.  */
1351
1352 /* Construct a new context.  The context below this one on the stack
1353    is given by NEXT.  */
1354
1355 static cp_parser_context *
1356 cp_parser_context_new (cp_parser_context* next)
1357 {
1358   cp_parser_context *context;
1359
1360   /* Allocate the storage.  */
1361   if (cp_parser_context_free_list != NULL)
1362     {
1363       /* Pull the first entry from the free list.  */
1364       context = cp_parser_context_free_list;
1365       cp_parser_context_free_list = context->next;
1366       memset (context, 0, sizeof (*context));
1367     }
1368   else
1369     context = GGC_CNEW (cp_parser_context);
1370
1371   /* No errors have occurred yet in this context.  */
1372   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1373   /* If this is not the bottomost context, copy information that we
1374      need from the previous context.  */
1375   if (next)
1376     {
1377       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1378          expression, then we are parsing one in this context, too.  */
1379       context->object_type = next->object_type;
1380       /* Thread the stack.  */
1381       context->next = next;
1382     }
1383
1384   return context;
1385 }
1386
1387 /* The cp_parser structure represents the C++ parser.  */
1388
1389 typedef struct cp_parser GTY(())
1390 {
1391   /* The lexer from which we are obtaining tokens.  */
1392   cp_lexer *lexer;
1393
1394   /* The scope in which names should be looked up.  If NULL_TREE, then
1395      we look up names in the scope that is currently open in the
1396      source program.  If non-NULL, this is either a TYPE or
1397      NAMESPACE_DECL for the scope in which we should look.  It can
1398      also be ERROR_MARK, when we've parsed a bogus scope.
1399
1400      This value is not cleared automatically after a name is looked
1401      up, so we must be careful to clear it before starting a new look
1402      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1403      will look up `Z' in the scope of `X', rather than the current
1404      scope.)  Unfortunately, it is difficult to tell when name lookup
1405      is complete, because we sometimes peek at a token, look it up,
1406      and then decide not to consume it.   */
1407   tree scope;
1408
1409   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1410      last lookup took place.  OBJECT_SCOPE is used if an expression
1411      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1412      respectively.  QUALIFYING_SCOPE is used for an expression of the
1413      form "X::Y"; it refers to X.  */
1414   tree object_scope;
1415   tree qualifying_scope;
1416
1417   /* A stack of parsing contexts.  All but the bottom entry on the
1418      stack will be tentative contexts.
1419
1420      We parse tentatively in order to determine which construct is in
1421      use in some situations.  For example, in order to determine
1422      whether a statement is an expression-statement or a
1423      declaration-statement we parse it tentatively as a
1424      declaration-statement.  If that fails, we then reparse the same
1425      token stream as an expression-statement.  */
1426   cp_parser_context *context;
1427
1428   /* True if we are parsing GNU C++.  If this flag is not set, then
1429      GNU extensions are not recognized.  */
1430   bool allow_gnu_extensions_p;
1431
1432   /* TRUE if the `>' token should be interpreted as the greater-than
1433      operator.  FALSE if it is the end of a template-id or
1434      template-parameter-list. In C++0x mode, this flag also applies to
1435      `>>' tokens, which are viewed as two consecutive `>' tokens when
1436      this flag is FALSE.  */
1437   bool greater_than_is_operator_p;
1438
1439   /* TRUE if default arguments are allowed within a parameter list
1440      that starts at this point. FALSE if only a gnu extension makes
1441      them permissible.  */
1442   bool default_arg_ok_p;
1443
1444   /* TRUE if we are parsing an integral constant-expression.  See
1445      [expr.const] for a precise definition.  */
1446   bool integral_constant_expression_p;
1447
1448   /* TRUE if we are parsing an integral constant-expression -- but a
1449      non-constant expression should be permitted as well.  This flag
1450      is used when parsing an array bound so that GNU variable-length
1451      arrays are tolerated.  */
1452   bool allow_non_integral_constant_expression_p;
1453
1454   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1455      been seen that makes the expression non-constant.  */
1456   bool non_integral_constant_expression_p;
1457
1458   /* TRUE if local variable names and `this' are forbidden in the
1459      current context.  */
1460   bool local_variables_forbidden_p;
1461
1462   /* TRUE if the declaration we are parsing is part of a
1463      linkage-specification of the form `extern string-literal
1464      declaration'.  */
1465   bool in_unbraced_linkage_specification_p;
1466
1467   /* TRUE if we are presently parsing a declarator, after the
1468      direct-declarator.  */
1469   bool in_declarator_p;
1470
1471   /* TRUE if we are presently parsing a template-argument-list.  */
1472   bool in_template_argument_list_p;
1473
1474   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1475      to IN_OMP_BLOCK if parsing OpenMP structured block and
1476      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1477      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1478      iteration-statement, OpenMP block or loop within that switch.  */
1479 #define IN_SWITCH_STMT          1
1480 #define IN_ITERATION_STMT       2
1481 #define IN_OMP_BLOCK            4
1482 #define IN_OMP_FOR              8
1483 #define IN_IF_STMT             16
1484   unsigned char in_statement;
1485
1486   /* TRUE if we are presently parsing the body of a switch statement.
1487      Note that this doesn't quite overlap with in_statement above.
1488      The difference relates to giving the right sets of error messages:
1489      "case not in switch" vs "break statement used with OpenMP...".  */
1490   bool in_switch_statement_p;
1491
1492   /* TRUE if we are parsing a type-id in an expression context.  In
1493      such a situation, both "type (expr)" and "type (type)" are valid
1494      alternatives.  */
1495   bool in_type_id_in_expr_p;
1496
1497   /* TRUE if we are currently in a header file where declarations are
1498      implicitly extern "C".  */
1499   bool implicit_extern_c;
1500
1501   /* TRUE if strings in expressions should be translated to the execution
1502      character set.  */
1503   bool translate_strings_p;
1504
1505   /* TRUE if we are presently parsing the body of a function, but not
1506      a local class.  */
1507   bool in_function_body;
1508
1509   /* If non-NULL, then we are parsing a construct where new type
1510      definitions are not permitted.  The string stored here will be
1511      issued as an error message if a type is defined.  */
1512   const char *type_definition_forbidden_message;
1513
1514   /* A list of lists. The outer list is a stack, used for member
1515      functions of local classes. At each level there are two sub-list,
1516      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1517      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1518      TREE_VALUE's. The functions are chained in reverse declaration
1519      order.
1520
1521      The TREE_PURPOSE sublist contains those functions with default
1522      arguments that need post processing, and the TREE_VALUE sublist
1523      contains those functions with definitions that need post
1524      processing.
1525
1526      These lists can only be processed once the outermost class being
1527      defined is complete.  */
1528   tree unparsed_functions_queues;
1529
1530   /* The number of classes whose definitions are currently in
1531      progress.  */
1532   unsigned num_classes_being_defined;
1533
1534   /* The number of template parameter lists that apply directly to the
1535      current declaration.  */
1536   unsigned num_template_parameter_lists;
1537 } cp_parser;
1538
1539 /* Prototypes.  */
1540
1541 /* Constructors and destructors.  */
1542
1543 static cp_parser *cp_parser_new
1544   (void);
1545
1546 /* Routines to parse various constructs.
1547
1548    Those that return `tree' will return the error_mark_node (rather
1549    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1550    Sometimes, they will return an ordinary node if error-recovery was
1551    attempted, even though a parse error occurred.  So, to check
1552    whether or not a parse error occurred, you should always use
1553    cp_parser_error_occurred.  If the construct is optional (indicated
1554    either by an `_opt' in the name of the function that does the
1555    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1556    the construct is not present.  */
1557
1558 /* Lexical conventions [gram.lex]  */
1559
1560 static tree cp_parser_identifier
1561   (cp_parser *);
1562 static tree cp_parser_string_literal
1563   (cp_parser *, bool, bool);
1564
1565 /* Basic concepts [gram.basic]  */
1566
1567 static bool cp_parser_translation_unit
1568   (cp_parser *);
1569
1570 /* Expressions [gram.expr]  */
1571
1572 static tree cp_parser_primary_expression
1573   (cp_parser *, bool, bool, bool, cp_id_kind *);
1574 static tree cp_parser_id_expression
1575   (cp_parser *, bool, bool, bool *, bool, bool);
1576 static tree cp_parser_unqualified_id
1577   (cp_parser *, bool, bool, bool, bool);
1578 static tree cp_parser_nested_name_specifier_opt
1579   (cp_parser *, bool, bool, bool, bool);
1580 static tree cp_parser_nested_name_specifier
1581   (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_class_or_namespace_name
1583   (cp_parser *, bool, bool, bool, bool, bool);
1584 static tree cp_parser_postfix_expression
1585   (cp_parser *, bool, bool, bool);
1586 static tree cp_parser_postfix_open_square_expression
1587   (cp_parser *, tree, bool);
1588 static tree cp_parser_postfix_dot_deref_expression
1589   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1590 static tree cp_parser_parenthesized_expression_list
1591   (cp_parser *, bool, bool, bool, bool *);
1592 static void cp_parser_pseudo_destructor_name
1593   (cp_parser *, tree *, tree *);
1594 static tree cp_parser_unary_expression
1595   (cp_parser *, bool, bool);
1596 static enum tree_code cp_parser_unary_operator
1597   (cp_token *);
1598 static tree cp_parser_new_expression
1599   (cp_parser *);
1600 static tree cp_parser_new_placement
1601   (cp_parser *);
1602 static tree cp_parser_new_type_id
1603   (cp_parser *, tree *);
1604 static cp_declarator *cp_parser_new_declarator_opt
1605   (cp_parser *);
1606 static cp_declarator *cp_parser_direct_new_declarator
1607   (cp_parser *);
1608 static tree cp_parser_new_initializer
1609   (cp_parser *);
1610 static tree cp_parser_delete_expression
1611   (cp_parser *);
1612 static tree cp_parser_cast_expression
1613   (cp_parser *, bool, bool);
1614 static tree cp_parser_binary_expression
1615   (cp_parser *, bool);
1616 static tree cp_parser_question_colon_clause
1617   (cp_parser *, tree);
1618 static tree cp_parser_assignment_expression
1619   (cp_parser *, bool);
1620 static enum tree_code cp_parser_assignment_operator_opt
1621   (cp_parser *);
1622 static tree cp_parser_expression
1623   (cp_parser *, bool);
1624 static tree cp_parser_constant_expression
1625   (cp_parser *, bool, bool *);
1626 static tree cp_parser_builtin_offsetof
1627   (cp_parser *);
1628
1629 /* Statements [gram.stmt.stmt]  */
1630
1631 static void cp_parser_statement
1632   (cp_parser *, tree, bool, bool *);
1633 static void cp_parser_label_for_labeled_statement
1634   (cp_parser *);
1635 static tree cp_parser_expression_statement
1636   (cp_parser *, tree);
1637 static tree cp_parser_compound_statement
1638   (cp_parser *, tree, bool);
1639 static void cp_parser_statement_seq_opt
1640   (cp_parser *, tree);
1641 static tree cp_parser_selection_statement
1642   (cp_parser *, bool *);
1643 static tree cp_parser_condition
1644   (cp_parser *);
1645 static tree cp_parser_iteration_statement
1646   (cp_parser *);
1647 static void cp_parser_for_init_statement
1648   (cp_parser *);
1649 static tree cp_parser_jump_statement
1650   (cp_parser *);
1651 static void cp_parser_declaration_statement
1652   (cp_parser *);
1653
1654 static tree cp_parser_implicitly_scoped_statement
1655   (cp_parser *, bool *);
1656 static void cp_parser_already_scoped_statement
1657   (cp_parser *);
1658
1659 /* Declarations [gram.dcl.dcl] */
1660
1661 static void cp_parser_declaration_seq_opt
1662   (cp_parser *);
1663 static void cp_parser_declaration
1664   (cp_parser *);
1665 static void cp_parser_block_declaration
1666   (cp_parser *, bool);
1667 static void cp_parser_simple_declaration
1668   (cp_parser *, bool);
1669 static void cp_parser_decl_specifier_seq
1670   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1671 static tree cp_parser_storage_class_specifier_opt
1672   (cp_parser *);
1673 static tree cp_parser_function_specifier_opt
1674   (cp_parser *, cp_decl_specifier_seq *);
1675 static tree cp_parser_type_specifier
1676   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1677    int *, bool *);
1678 static tree cp_parser_simple_type_specifier
1679   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1680 static tree cp_parser_type_name
1681   (cp_parser *);
1682 static tree cp_parser_elaborated_type_specifier
1683   (cp_parser *, bool, bool);
1684 static tree cp_parser_enum_specifier
1685   (cp_parser *);
1686 static void cp_parser_enumerator_list
1687   (cp_parser *, tree);
1688 static void cp_parser_enumerator_definition
1689   (cp_parser *, tree);
1690 static tree cp_parser_namespace_name
1691   (cp_parser *);
1692 static void cp_parser_namespace_definition
1693   (cp_parser *);
1694 static void cp_parser_namespace_body
1695   (cp_parser *);
1696 static tree cp_parser_qualified_namespace_specifier
1697   (cp_parser *);
1698 static void cp_parser_namespace_alias_definition
1699   (cp_parser *);
1700 static bool cp_parser_using_declaration
1701   (cp_parser *, bool);
1702 static void cp_parser_using_directive
1703   (cp_parser *);
1704 static void cp_parser_asm_definition
1705   (cp_parser *);
1706 static void cp_parser_linkage_specification
1707   (cp_parser *);
1708 static void cp_parser_static_assert
1709   (cp_parser *, bool);
1710 static tree cp_parser_decltype
1711   (cp_parser *);
1712
1713 /* Declarators [gram.dcl.decl] */
1714
1715 static tree cp_parser_init_declarator
1716   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1717 static cp_declarator *cp_parser_declarator
1718   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1719 static cp_declarator *cp_parser_direct_declarator
1720   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1721 static enum tree_code cp_parser_ptr_operator
1722   (cp_parser *, tree *, cp_cv_quals *);
1723 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1724   (cp_parser *);
1725 static tree cp_parser_declarator_id
1726   (cp_parser *, bool);
1727 static tree cp_parser_type_id
1728   (cp_parser *);
1729 static void cp_parser_type_specifier_seq
1730   (cp_parser *, bool, cp_decl_specifier_seq *);
1731 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1732   (cp_parser *);
1733 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1734   (cp_parser *, bool *);
1735 static cp_parameter_declarator *cp_parser_parameter_declaration
1736   (cp_parser *, bool, bool *);
1737 static void cp_parser_function_body
1738   (cp_parser *);
1739 static tree cp_parser_initializer
1740   (cp_parser *, bool *, bool *);
1741 static tree cp_parser_initializer_clause
1742   (cp_parser *, bool *);
1743 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1744   (cp_parser *, bool *);
1745
1746 static bool cp_parser_ctor_initializer_opt_and_function_body
1747   (cp_parser *);
1748
1749 /* Classes [gram.class] */
1750
1751 static tree cp_parser_class_name
1752   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1753 static tree cp_parser_class_specifier
1754   (cp_parser *);
1755 static tree cp_parser_class_head
1756   (cp_parser *, bool *, tree *, tree *);
1757 static enum tag_types cp_parser_class_key
1758   (cp_parser *);
1759 static void cp_parser_member_specification_opt
1760   (cp_parser *);
1761 static void cp_parser_member_declaration
1762   (cp_parser *);
1763 static tree cp_parser_pure_specifier
1764   (cp_parser *);
1765 static tree cp_parser_constant_initializer
1766   (cp_parser *);
1767
1768 /* Derived classes [gram.class.derived] */
1769
1770 static tree cp_parser_base_clause
1771   (cp_parser *);
1772 static tree cp_parser_base_specifier
1773   (cp_parser *);
1774
1775 /* Special member functions [gram.special] */
1776
1777 static tree cp_parser_conversion_function_id
1778   (cp_parser *);
1779 static tree cp_parser_conversion_type_id
1780   (cp_parser *);
1781 static cp_declarator *cp_parser_conversion_declarator_opt
1782   (cp_parser *);
1783 static bool cp_parser_ctor_initializer_opt
1784   (cp_parser *);
1785 static void cp_parser_mem_initializer_list
1786   (cp_parser *);
1787 static tree cp_parser_mem_initializer
1788   (cp_parser *);
1789 static tree cp_parser_mem_initializer_id
1790   (cp_parser *);
1791
1792 /* Overloading [gram.over] */
1793
1794 static tree cp_parser_operator_function_id
1795   (cp_parser *);
1796 static tree cp_parser_operator
1797   (cp_parser *);
1798
1799 /* Templates [gram.temp] */
1800
1801 static void cp_parser_template_declaration
1802   (cp_parser *, bool);
1803 static tree cp_parser_template_parameter_list
1804   (cp_parser *);
1805 static tree cp_parser_template_parameter
1806   (cp_parser *, bool *, bool *);
1807 static tree cp_parser_type_parameter
1808   (cp_parser *, bool *);
1809 static tree cp_parser_template_id
1810   (cp_parser *, bool, bool, bool);
1811 static tree cp_parser_template_name
1812   (cp_parser *, bool, bool, bool, bool *);
1813 static tree cp_parser_template_argument_list
1814   (cp_parser *);
1815 static tree cp_parser_template_argument
1816   (cp_parser *);
1817 static void cp_parser_explicit_instantiation
1818   (cp_parser *);
1819 static void cp_parser_explicit_specialization
1820   (cp_parser *);
1821
1822 /* Exception handling [gram.exception] */
1823
1824 static tree cp_parser_try_block
1825   (cp_parser *);
1826 static bool cp_parser_function_try_block
1827   (cp_parser *);
1828 static void cp_parser_handler_seq
1829   (cp_parser *);
1830 static void cp_parser_handler
1831   (cp_parser *);
1832 static tree cp_parser_exception_declaration
1833   (cp_parser *);
1834 static tree cp_parser_throw_expression
1835   (cp_parser *);
1836 static tree cp_parser_exception_specification_opt
1837   (cp_parser *);
1838 static tree cp_parser_type_id_list
1839   (cp_parser *);
1840
1841 /* GNU Extensions */
1842
1843 static tree cp_parser_asm_specification_opt
1844   (cp_parser *);
1845 static tree cp_parser_asm_operand_list
1846   (cp_parser *);
1847 static tree cp_parser_asm_clobber_list
1848   (cp_parser *);
1849 static tree cp_parser_attributes_opt
1850   (cp_parser *);
1851 static tree cp_parser_attribute_list
1852   (cp_parser *);
1853 static bool cp_parser_extension_opt
1854   (cp_parser *, int *);
1855 static void cp_parser_label_declaration
1856   (cp_parser *);
1857
1858 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1859 static bool cp_parser_pragma
1860   (cp_parser *, enum pragma_context);
1861
1862 /* Objective-C++ Productions */
1863
1864 static tree cp_parser_objc_message_receiver
1865   (cp_parser *);
1866 static tree cp_parser_objc_message_args
1867   (cp_parser *);
1868 static tree cp_parser_objc_message_expression
1869   (cp_parser *);
1870 static tree cp_parser_objc_encode_expression
1871   (cp_parser *);
1872 static tree cp_parser_objc_defs_expression
1873   (cp_parser *);
1874 static tree cp_parser_objc_protocol_expression
1875   (cp_parser *);
1876 static tree cp_parser_objc_selector_expression
1877   (cp_parser *);
1878 static tree cp_parser_objc_expression
1879   (cp_parser *);
1880 static bool cp_parser_objc_selector_p
1881   (enum cpp_ttype);
1882 static tree cp_parser_objc_selector
1883   (cp_parser *);
1884 static tree cp_parser_objc_protocol_refs_opt
1885   (cp_parser *);
1886 static void cp_parser_objc_declaration
1887   (cp_parser *);
1888 static tree cp_parser_objc_statement
1889   (cp_parser *);
1890
1891 /* Utility Routines */
1892
1893 static tree cp_parser_lookup_name
1894   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1895 static tree cp_parser_lookup_name_simple
1896   (cp_parser *, tree);
1897 static tree cp_parser_maybe_treat_template_as_class
1898   (tree, bool);
1899 static bool cp_parser_check_declarator_template_parameters
1900   (cp_parser *, cp_declarator *);
1901 static bool cp_parser_check_template_parameters
1902   (cp_parser *, unsigned);
1903 static tree cp_parser_simple_cast_expression
1904   (cp_parser *);
1905 static tree cp_parser_global_scope_opt
1906   (cp_parser *, bool);
1907 static bool cp_parser_constructor_declarator_p
1908   (cp_parser *, bool);
1909 static tree cp_parser_function_definition_from_specifiers_and_declarator
1910   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1911 static tree cp_parser_function_definition_after_declarator
1912   (cp_parser *, bool);
1913 static void cp_parser_template_declaration_after_export
1914   (cp_parser *, bool);
1915 static void cp_parser_perform_template_parameter_access_checks
1916   (VEC (deferred_access_check,gc)*);
1917 static tree cp_parser_single_declaration
1918   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1919 static tree cp_parser_functional_cast
1920   (cp_parser *, tree);
1921 static tree cp_parser_save_member_function_body
1922   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1923 static tree cp_parser_enclosed_template_argument_list
1924   (cp_parser *);
1925 static void cp_parser_save_default_args
1926   (cp_parser *, tree);
1927 static void cp_parser_late_parsing_for_member
1928   (cp_parser *, tree);
1929 static void cp_parser_late_parsing_default_args
1930   (cp_parser *, tree);
1931 static tree cp_parser_sizeof_operand
1932   (cp_parser *, enum rid);
1933 static tree cp_parser_trait_expr
1934   (cp_parser *, enum rid);
1935 static bool cp_parser_declares_only_class_p
1936   (cp_parser *);
1937 static void cp_parser_set_storage_class
1938   (cp_parser *, cp_decl_specifier_seq *, enum rid);
1939 static void cp_parser_set_decl_spec_type
1940   (cp_decl_specifier_seq *, tree, bool);
1941 static bool cp_parser_friend_p
1942   (const cp_decl_specifier_seq *);
1943 static cp_token *cp_parser_require
1944   (cp_parser *, enum cpp_ttype, const char *);
1945 static cp_token *cp_parser_require_keyword
1946   (cp_parser *, enum rid, const char *);
1947 static bool cp_parser_token_starts_function_definition_p
1948   (cp_token *);
1949 static bool cp_parser_next_token_starts_class_definition_p
1950   (cp_parser *);
1951 static bool cp_parser_next_token_ends_template_argument_p
1952   (cp_parser *);
1953 static bool cp_parser_nth_token_starts_template_argument_list_p
1954   (cp_parser *, size_t);
1955 static enum tag_types cp_parser_token_is_class_key
1956   (cp_token *);
1957 static void cp_parser_check_class_key
1958   (enum tag_types, tree type);
1959 static void cp_parser_check_access_in_redeclaration
1960   (tree type);
1961 static bool cp_parser_optional_template_keyword
1962   (cp_parser *);
1963 static void cp_parser_pre_parsed_nested_name_specifier
1964   (cp_parser *);
1965 static void cp_parser_cache_group
1966   (cp_parser *, enum cpp_ttype, unsigned);
1967 static void cp_parser_parse_tentatively
1968   (cp_parser *);
1969 static void cp_parser_commit_to_tentative_parse
1970   (cp_parser *);
1971 static void cp_parser_abort_tentative_parse
1972   (cp_parser *);
1973 static bool cp_parser_parse_definitely
1974   (cp_parser *);
1975 static inline bool cp_parser_parsing_tentatively
1976   (cp_parser *);
1977 static bool cp_parser_uncommitted_to_tentative_parse_p
1978   (cp_parser *);
1979 static void cp_parser_error
1980   (cp_parser *, const char *);
1981 static void cp_parser_name_lookup_error
1982   (cp_parser *, tree, tree, const char *);
1983 static bool cp_parser_simulate_error
1984   (cp_parser *);
1985 static bool cp_parser_check_type_definition
1986   (cp_parser *);
1987 static void cp_parser_check_for_definition_in_return_type
1988   (cp_declarator *, tree);
1989 static void cp_parser_check_for_invalid_template_id
1990   (cp_parser *, tree);
1991 static bool cp_parser_non_integral_constant_expression
1992   (cp_parser *, const char *);
1993 static void cp_parser_diagnose_invalid_type_name
1994   (cp_parser *, tree, tree);
1995 static bool cp_parser_parse_and_diagnose_invalid_type_name
1996   (cp_parser *);
1997 static int cp_parser_skip_to_closing_parenthesis
1998   (cp_parser *, bool, bool, bool);
1999 static void cp_parser_skip_to_end_of_statement
2000   (cp_parser *);
2001 static void cp_parser_consume_semicolon_at_end_of_statement
2002   (cp_parser *);
2003 static void cp_parser_skip_to_end_of_block_or_statement
2004   (cp_parser *);
2005 static bool cp_parser_skip_to_closing_brace
2006   (cp_parser *);
2007 static void cp_parser_skip_to_end_of_template_parameter_list
2008   (cp_parser *);
2009 static void cp_parser_skip_to_pragma_eol
2010   (cp_parser*, cp_token *);
2011 static bool cp_parser_error_occurred
2012   (cp_parser *);
2013 static bool cp_parser_allow_gnu_extensions_p
2014   (cp_parser *);
2015 static bool cp_parser_is_string_literal
2016   (cp_token *);
2017 static bool cp_parser_is_keyword
2018   (cp_token *, enum rid);
2019 static tree cp_parser_make_typename_type
2020   (cp_parser *, tree, tree);
2021 static cp_declarator * cp_parser_make_indirect_declarator
2022   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2023
2024 /* Returns nonzero if we are parsing tentatively.  */
2025
2026 static inline bool
2027 cp_parser_parsing_tentatively (cp_parser* parser)
2028 {
2029   return parser->context->next != NULL;
2030 }
2031
2032 /* Returns nonzero if TOKEN is a string literal.  */
2033
2034 static bool
2035 cp_parser_is_string_literal (cp_token* token)
2036 {
2037   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2038 }
2039
2040 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2041
2042 static bool
2043 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2044 {
2045   return token->keyword == keyword;
2046 }
2047
2048 /* If not parsing tentatively, issue a diagnostic of the form
2049       FILE:LINE: MESSAGE before TOKEN
2050    where TOKEN is the next token in the input stream.  MESSAGE
2051    (specified by the caller) is usually of the form "expected
2052    OTHER-TOKEN".  */
2053
2054 static void
2055 cp_parser_error (cp_parser* parser, const char* message)
2056 {
2057   if (!cp_parser_simulate_error (parser))
2058     {
2059       cp_token *token = cp_lexer_peek_token (parser->lexer);
2060       /* This diagnostic makes more sense if it is tagged to the line
2061          of the token we just peeked at.  */
2062       cp_lexer_set_source_position_from_token (token);
2063
2064       if (token->type == CPP_PRAGMA)
2065         {
2066           error ("%<#pragma%> is not allowed here");
2067           cp_parser_skip_to_pragma_eol (parser, token);
2068           return;
2069         }
2070
2071       c_parse_error (message,
2072                      /* Because c_parser_error does not understand
2073                         CPP_KEYWORD, keywords are treated like
2074                         identifiers.  */
2075                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2076                      token->u.value);
2077     }
2078 }
2079
2080 /* Issue an error about name-lookup failing.  NAME is the
2081    IDENTIFIER_NODE DECL is the result of
2082    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2083    the thing that we hoped to find.  */
2084
2085 static void
2086 cp_parser_name_lookup_error (cp_parser* parser,
2087                              tree name,
2088                              tree decl,
2089                              const char* desired)
2090 {
2091   /* If name lookup completely failed, tell the user that NAME was not
2092      declared.  */
2093   if (decl == error_mark_node)
2094     {
2095       if (parser->scope && parser->scope != global_namespace)
2096         error ("%<%E::%E%> has not been declared",
2097                parser->scope, name);
2098       else if (parser->scope == global_namespace)
2099         error ("%<::%E%> has not been declared", name);
2100       else if (parser->object_scope
2101                && !CLASS_TYPE_P (parser->object_scope))
2102         error ("request for member %qE in non-class type %qT",
2103                name, parser->object_scope);
2104       else if (parser->object_scope)
2105         error ("%<%T::%E%> has not been declared",
2106                parser->object_scope, name);
2107       else
2108         error ("%qE has not been declared", name);
2109     }
2110   else if (parser->scope && parser->scope != global_namespace)
2111     error ("%<%E::%E%> %s", parser->scope, name, desired);
2112   else if (parser->scope == global_namespace)
2113     error ("%<::%E%> %s", name, desired);
2114   else
2115     error ("%qE %s", name, desired);
2116 }
2117
2118 /* If we are parsing tentatively, remember that an error has occurred
2119    during this tentative parse.  Returns true if the error was
2120    simulated; false if a message should be issued by the caller.  */
2121
2122 static bool
2123 cp_parser_simulate_error (cp_parser* parser)
2124 {
2125   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2126     {
2127       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2128       return true;
2129     }
2130   return false;
2131 }
2132
2133 /* Check for repeated decl-specifiers.  */
2134
2135 static void
2136 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2137 {
2138   cp_decl_spec ds;
2139
2140   for (ds = ds_first; ds != ds_last; ++ds)
2141     {
2142       unsigned count = decl_specs->specs[(int)ds];
2143       if (count < 2)
2144         continue;
2145       /* The "long" specifier is a special case because of "long long".  */
2146       if (ds == ds_long)
2147         {
2148           if (count > 2)
2149             error ("%<long long long%> is too long for GCC");
2150           else if (pedantic && !in_system_header && warn_long_long)
2151             pedwarn ("ISO C++ does not support %<long long%>");
2152         }
2153       else if (count > 1)
2154         {
2155           static const char *const decl_spec_names[] = {
2156             "signed",
2157             "unsigned",
2158             "short",
2159             "long",
2160             "const",
2161             "volatile",
2162             "restrict",
2163             "inline",
2164             "virtual",
2165             "explicit",
2166             "friend",
2167             "typedef",
2168             "__complex",
2169             "__thread"
2170           };
2171           error ("duplicate %qs", decl_spec_names[(int)ds]);
2172         }
2173     }
2174 }
2175
2176 /* This function is called when a type is defined.  If type
2177    definitions are forbidden at this point, an error message is
2178    issued.  */
2179
2180 static bool
2181 cp_parser_check_type_definition (cp_parser* parser)
2182 {
2183   /* If types are forbidden here, issue a message.  */
2184   if (parser->type_definition_forbidden_message)
2185     {
2186       /* Use `%s' to print the string in case there are any escape
2187          characters in the message.  */
2188       error ("%s", parser->type_definition_forbidden_message);
2189       return false;
2190     }
2191   return true;
2192 }
2193
2194 /* This function is called when the DECLARATOR is processed.  The TYPE
2195    was a type defined in the decl-specifiers.  If it is invalid to
2196    define a type in the decl-specifiers for DECLARATOR, an error is
2197    issued.  */
2198
2199 static void
2200 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2201                                                tree type)
2202 {
2203   /* [dcl.fct] forbids type definitions in return types.
2204      Unfortunately, it's not easy to know whether or not we are
2205      processing a return type until after the fact.  */
2206   while (declarator
2207          && (declarator->kind == cdk_pointer
2208              || declarator->kind == cdk_reference
2209              || declarator->kind == cdk_ptrmem))
2210     declarator = declarator->declarator;
2211   if (declarator
2212       && declarator->kind == cdk_function)
2213     {
2214       error ("new types may not be defined in a return type");
2215       inform ("(perhaps a semicolon is missing after the definition of %qT)",
2216               type);
2217     }
2218 }
2219
2220 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2221    "<" in any valid C++ program.  If the next token is indeed "<",
2222    issue a message warning the user about what appears to be an
2223    invalid attempt to form a template-id.  */
2224
2225 static void
2226 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2227                                          tree type)
2228 {
2229   cp_token_position start = 0;
2230
2231   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2232     {
2233       if (TYPE_P (type))
2234         error ("%qT is not a template", type);
2235       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2236         error ("%qE is not a template", type);
2237       else
2238         error ("invalid template-id");
2239       /* Remember the location of the invalid "<".  */
2240       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2241         start = cp_lexer_token_position (parser->lexer, true);
2242       /* Consume the "<".  */
2243       cp_lexer_consume_token (parser->lexer);
2244       /* Parse the template arguments.  */
2245       cp_parser_enclosed_template_argument_list (parser);
2246       /* Permanently remove the invalid template arguments so that
2247          this error message is not issued again.  */
2248       if (start)
2249         cp_lexer_purge_tokens_after (parser->lexer, start);
2250     }
2251 }
2252
2253 /* If parsing an integral constant-expression, issue an error message
2254    about the fact that THING appeared and return true.  Otherwise,
2255    return false.  In either case, set
2256    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2257
2258 static bool
2259 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2260                                             const char *thing)
2261 {
2262   parser->non_integral_constant_expression_p = true;
2263   if (parser->integral_constant_expression_p)
2264     {
2265       if (!parser->allow_non_integral_constant_expression_p)
2266         {
2267           error ("%s cannot appear in a constant-expression", thing);
2268           return true;
2269         }
2270     }
2271   return false;
2272 }
2273
2274 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2275    qualifying scope (or NULL, if none) for ID.  This function commits
2276    to the current active tentative parse, if any.  (Otherwise, the
2277    problematic construct might be encountered again later, resulting
2278    in duplicate error messages.)  */
2279
2280 static void
2281 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2282 {
2283   tree decl, old_scope;
2284   /* Try to lookup the identifier.  */
2285   old_scope = parser->scope;
2286   parser->scope = scope;
2287   decl = cp_parser_lookup_name_simple (parser, id);
2288   parser->scope = old_scope;
2289   /* If the lookup found a template-name, it means that the user forgot
2290   to specify an argument list. Emit a useful error message.  */
2291   if (TREE_CODE (decl) == TEMPLATE_DECL)
2292     error ("invalid use of template-name %qE without an argument list", decl);
2293   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2294     error ("invalid use of destructor %qD as a type", id);
2295   else if (TREE_CODE (decl) == TYPE_DECL)
2296     /* Something like 'unsigned A a;'  */
2297     error ("invalid combination of multiple type-specifiers");
2298   else if (!parser->scope)
2299     {
2300       /* Issue an error message.  */
2301       error ("%qE does not name a type", id);
2302       /* If we're in a template class, it's possible that the user was
2303          referring to a type from a base class.  For example:
2304
2305            template <typename T> struct A { typedef T X; };
2306            template <typename T> struct B : public A<T> { X x; };
2307
2308          The user should have said "typename A<T>::X".  */
2309       if (processing_template_decl && current_class_type
2310           && TYPE_BINFO (current_class_type))
2311         {
2312           tree b;
2313
2314           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2315                b;
2316                b = TREE_CHAIN (b))
2317             {
2318               tree base_type = BINFO_TYPE (b);
2319               if (CLASS_TYPE_P (base_type)
2320                   && dependent_type_p (base_type))
2321                 {
2322                   tree field;
2323                   /* Go from a particular instantiation of the
2324                      template (which will have an empty TYPE_FIELDs),
2325                      to the main version.  */
2326                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2327                   for (field = TYPE_FIELDS (base_type);
2328                        field;
2329                        field = TREE_CHAIN (field))
2330                     if (TREE_CODE (field) == TYPE_DECL
2331                         && DECL_NAME (field) == id)
2332                       {
2333                         inform ("(perhaps %<typename %T::%E%> was intended)",
2334                                 BINFO_TYPE (b), id);
2335                         break;
2336                       }
2337                   if (field)
2338                     break;
2339                 }
2340             }
2341         }
2342     }
2343   /* Here we diagnose qualified-ids where the scope is actually correct,
2344      but the identifier does not resolve to a valid type name.  */
2345   else if (parser->scope != error_mark_node)
2346     {
2347       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2348         error ("%qE in namespace %qE does not name a type",
2349                id, parser->scope);
2350       else if (TYPE_P (parser->scope))
2351         error ("%qE in class %qT does not name a type", id, parser->scope);
2352       else
2353         gcc_unreachable ();
2354     }
2355   cp_parser_commit_to_tentative_parse (parser);
2356 }
2357
2358 /* Check for a common situation where a type-name should be present,
2359    but is not, and issue a sensible error message.  Returns true if an
2360    invalid type-name was detected.
2361
2362    The situation handled by this function are variable declarations of the
2363    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2364    Usually, `ID' should name a type, but if we got here it means that it
2365    does not. We try to emit the best possible error message depending on
2366    how exactly the id-expression looks like.  */
2367
2368 static bool
2369 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2370 {
2371   tree id;
2372
2373   cp_parser_parse_tentatively (parser);
2374   id = cp_parser_id_expression (parser,
2375                                 /*template_keyword_p=*/false,
2376                                 /*check_dependency_p=*/true,
2377                                 /*template_p=*/NULL,
2378                                 /*declarator_p=*/true,
2379                                 /*optional_p=*/false);
2380   /* After the id-expression, there should be a plain identifier,
2381      otherwise this is not a simple variable declaration. Also, if
2382      the scope is dependent, we cannot do much.  */
2383   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2384       || (parser->scope && TYPE_P (parser->scope)
2385           && dependent_type_p (parser->scope))
2386       || TREE_CODE (id) == TYPE_DECL)
2387     {
2388       cp_parser_abort_tentative_parse (parser);
2389       return false;
2390     }
2391   if (!cp_parser_parse_definitely (parser))
2392     return false;
2393
2394   /* Emit a diagnostic for the invalid type.  */
2395   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2396   /* Skip to the end of the declaration; there's no point in
2397      trying to process it.  */
2398   cp_parser_skip_to_end_of_block_or_statement (parser);
2399   return true;
2400 }
2401
2402 /* Consume tokens up to, and including, the next non-nested closing `)'.
2403    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2404    are doing error recovery. Returns -1 if OR_COMMA is true and we
2405    found an unnested comma.  */
2406
2407 static int
2408 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2409                                        bool recovering,
2410                                        bool or_comma,
2411                                        bool consume_paren)
2412 {
2413   unsigned paren_depth = 0;
2414   unsigned brace_depth = 0;
2415
2416   if (recovering && !or_comma
2417       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2418     return 0;
2419
2420   while (true)
2421     {
2422       cp_token * token = cp_lexer_peek_token (parser->lexer);
2423
2424       switch (token->type)
2425         {
2426         case CPP_EOF:
2427         case CPP_PRAGMA_EOL:
2428           /* If we've run out of tokens, then there is no closing `)'.  */
2429           return 0;
2430
2431         case CPP_SEMICOLON:
2432           /* This matches the processing in skip_to_end_of_statement.  */
2433           if (!brace_depth)
2434             return 0;
2435           break;
2436
2437         case CPP_OPEN_BRACE:
2438           ++brace_depth;
2439           break;
2440         case CPP_CLOSE_BRACE:
2441           if (!brace_depth--)
2442             return 0;
2443           break;
2444
2445         case CPP_COMMA:
2446           if (recovering && or_comma && !brace_depth && !paren_depth)
2447             return -1;
2448           break;
2449
2450         case CPP_OPEN_PAREN:
2451           if (!brace_depth)
2452             ++paren_depth;
2453           break;
2454
2455         case CPP_CLOSE_PAREN:
2456           if (!brace_depth && !paren_depth--)
2457             {
2458               if (consume_paren)
2459                 cp_lexer_consume_token (parser->lexer);
2460               return 1;
2461             }
2462           break;
2463
2464         default:
2465           break;
2466         }
2467
2468       /* Consume the token.  */
2469       cp_lexer_consume_token (parser->lexer);
2470     }
2471 }
2472
2473 /* Consume tokens until we reach the end of the current statement.
2474    Normally, that will be just before consuming a `;'.  However, if a
2475    non-nested `}' comes first, then we stop before consuming that.  */
2476
2477 static void
2478 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2479 {
2480   unsigned nesting_depth = 0;
2481
2482   while (true)
2483     {
2484       cp_token *token = cp_lexer_peek_token (parser->lexer);
2485
2486       switch (token->type)
2487         {
2488         case CPP_EOF:
2489         case CPP_PRAGMA_EOL:
2490           /* If we've run out of tokens, stop.  */
2491           return;
2492
2493         case CPP_SEMICOLON:
2494           /* If the next token is a `;', we have reached the end of the
2495              statement.  */
2496           if (!nesting_depth)
2497             return;
2498           break;
2499
2500         case CPP_CLOSE_BRACE:
2501           /* If this is a non-nested '}', stop before consuming it.
2502              That way, when confronted with something like:
2503
2504                { 3 + }
2505
2506              we stop before consuming the closing '}', even though we
2507              have not yet reached a `;'.  */
2508           if (nesting_depth == 0)
2509             return;
2510
2511           /* If it is the closing '}' for a block that we have
2512              scanned, stop -- but only after consuming the token.
2513              That way given:
2514
2515                 void f g () { ... }
2516                 typedef int I;
2517
2518              we will stop after the body of the erroneously declared
2519              function, but before consuming the following `typedef'
2520              declaration.  */
2521           if (--nesting_depth == 0)
2522             {
2523               cp_lexer_consume_token (parser->lexer);
2524               return;
2525             }
2526
2527         case CPP_OPEN_BRACE:
2528           ++nesting_depth;
2529           break;
2530
2531         default:
2532           break;
2533         }
2534
2535       /* Consume the token.  */
2536       cp_lexer_consume_token (parser->lexer);
2537     }
2538 }
2539
2540 /* This function is called at the end of a statement or declaration.
2541    If the next token is a semicolon, it is consumed; otherwise, error
2542    recovery is attempted.  */
2543
2544 static void
2545 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2546 {
2547   /* Look for the trailing `;'.  */
2548   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2549     {
2550       /* If there is additional (erroneous) input, skip to the end of
2551          the statement.  */
2552       cp_parser_skip_to_end_of_statement (parser);
2553       /* If the next token is now a `;', consume it.  */
2554       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2555         cp_lexer_consume_token (parser->lexer);
2556     }
2557 }
2558
2559 /* Skip tokens until we have consumed an entire block, or until we
2560    have consumed a non-nested `;'.  */
2561
2562 static void
2563 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2564 {
2565   int nesting_depth = 0;
2566
2567   while (nesting_depth >= 0)
2568     {
2569       cp_token *token = cp_lexer_peek_token (parser->lexer);
2570
2571       switch (token->type)
2572         {
2573         case CPP_EOF:
2574         case CPP_PRAGMA_EOL:
2575           /* If we've run out of tokens, stop.  */
2576           return;
2577
2578         case CPP_SEMICOLON:
2579           /* Stop if this is an unnested ';'. */
2580           if (!nesting_depth)
2581             nesting_depth = -1;
2582           break;
2583
2584         case CPP_CLOSE_BRACE:
2585           /* Stop if this is an unnested '}', or closes the outermost
2586              nesting level.  */
2587           nesting_depth--;
2588           if (!nesting_depth)
2589             nesting_depth = -1;
2590           break;
2591
2592         case CPP_OPEN_BRACE:
2593           /* Nest. */
2594           nesting_depth++;
2595           break;
2596
2597         default:
2598           break;
2599         }
2600
2601       /* Consume the token.  */
2602       cp_lexer_consume_token (parser->lexer);
2603     }
2604 }
2605
2606 /* Skip tokens until a non-nested closing curly brace is the next
2607    token, or there are no more tokens. Return true in the first case,
2608    false otherwise.  */
2609
2610 static bool
2611 cp_parser_skip_to_closing_brace (cp_parser *parser)
2612 {
2613   unsigned nesting_depth = 0;
2614
2615   while (true)
2616     {
2617       cp_token *token = cp_lexer_peek_token (parser->lexer);
2618
2619       switch (token->type)
2620         {
2621         case CPP_EOF:
2622         case CPP_PRAGMA_EOL:
2623           /* If we've run out of tokens, stop.  */
2624           return false;
2625
2626         case CPP_CLOSE_BRACE:
2627           /* If the next token is a non-nested `}', then we have reached
2628              the end of the current block.  */
2629           if (nesting_depth-- == 0)
2630             return true;
2631           break;
2632
2633         case CPP_OPEN_BRACE:
2634           /* If it the next token is a `{', then we are entering a new
2635              block.  Consume the entire block.  */
2636           ++nesting_depth;
2637           break;
2638
2639         default:
2640           break;
2641         }
2642
2643       /* Consume the token.  */
2644       cp_lexer_consume_token (parser->lexer);
2645     }
2646 }
2647
2648 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2649    parameter is the PRAGMA token, allowing us to purge the entire pragma
2650    sequence.  */
2651
2652 static void
2653 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2654 {
2655   cp_token *token;
2656
2657   parser->lexer->in_pragma = false;
2658
2659   do
2660     token = cp_lexer_consume_token (parser->lexer);
2661   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2662
2663   /* Ensure that the pragma is not parsed again.  */
2664   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2665 }
2666
2667 /* Require pragma end of line, resyncing with it as necessary.  The
2668    arguments are as for cp_parser_skip_to_pragma_eol.  */
2669
2670 static void
2671 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2672 {
2673   parser->lexer->in_pragma = false;
2674   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2675     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2676 }
2677
2678 /* This is a simple wrapper around make_typename_type. When the id is
2679    an unresolved identifier node, we can provide a superior diagnostic
2680    using cp_parser_diagnose_invalid_type_name.  */
2681
2682 static tree
2683 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2684 {
2685   tree result;
2686   if (TREE_CODE (id) == IDENTIFIER_NODE)
2687     {
2688       result = make_typename_type (scope, id, typename_type,
2689                                    /*complain=*/tf_none);
2690       if (result == error_mark_node)
2691         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2692       return result;
2693     }
2694   return make_typename_type (scope, id, typename_type, tf_error);
2695 }
2696
2697 /* This is a wrapper around the
2698    make_{pointer,ptrmem,reference}_declarator functions that decides
2699    which one to call based on the CODE and CLASS_TYPE arguments. The
2700    CODE argument should be one of the values returned by
2701    cp_parser_ptr_operator. */
2702 static cp_declarator *
2703 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2704                                     cp_cv_quals cv_qualifiers,
2705                                     cp_declarator *target)
2706 {
2707   if (code == ERROR_MARK)
2708     return cp_error_declarator;
2709
2710   if (code == INDIRECT_REF)
2711     if (class_type == NULL_TREE)
2712       return make_pointer_declarator (cv_qualifiers, target);
2713     else
2714       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2715   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2716     return make_reference_declarator (cv_qualifiers, target, false);
2717   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2718     return make_reference_declarator (cv_qualifiers, target, true);
2719   gcc_unreachable ();
2720 }
2721
2722 /* Create a new C++ parser.  */
2723
2724 static cp_parser *
2725 cp_parser_new (void)
2726 {
2727   cp_parser *parser;
2728   cp_lexer *lexer;
2729   unsigned i;
2730
2731   /* cp_lexer_new_main is called before calling ggc_alloc because
2732      cp_lexer_new_main might load a PCH file.  */
2733   lexer = cp_lexer_new_main ();
2734
2735   /* Initialize the binops_by_token so that we can get the tree
2736      directly from the token.  */
2737   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2738     binops_by_token[binops[i].token_type] = binops[i];
2739
2740   parser = GGC_CNEW (cp_parser);
2741   parser->lexer = lexer;
2742   parser->context = cp_parser_context_new (NULL);
2743
2744   /* For now, we always accept GNU extensions.  */
2745   parser->allow_gnu_extensions_p = 1;
2746
2747   /* The `>' token is a greater-than operator, not the end of a
2748      template-id.  */
2749   parser->greater_than_is_operator_p = true;
2750
2751   parser->default_arg_ok_p = true;
2752
2753   /* We are not parsing a constant-expression.  */
2754   parser->integral_constant_expression_p = false;
2755   parser->allow_non_integral_constant_expression_p = false;
2756   parser->non_integral_constant_expression_p = false;
2757
2758   /* Local variable names are not forbidden.  */
2759   parser->local_variables_forbidden_p = false;
2760
2761   /* We are not processing an `extern "C"' declaration.  */
2762   parser->in_unbraced_linkage_specification_p = false;
2763
2764   /* We are not processing a declarator.  */
2765   parser->in_declarator_p = false;
2766
2767   /* We are not processing a template-argument-list.  */
2768   parser->in_template_argument_list_p = false;
2769
2770   /* We are not in an iteration statement.  */
2771   parser->in_statement = 0;
2772
2773   /* We are not in a switch statement.  */
2774   parser->in_switch_statement_p = false;
2775
2776   /* We are not parsing a type-id inside an expression.  */
2777   parser->in_type_id_in_expr_p = false;
2778
2779   /* Declarations aren't implicitly extern "C".  */
2780   parser->implicit_extern_c = false;
2781
2782   /* String literals should be translated to the execution character set.  */
2783   parser->translate_strings_p = true;
2784
2785   /* We are not parsing a function body.  */
2786   parser->in_function_body = false;
2787
2788   /* The unparsed function queue is empty.  */
2789   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2790
2791   /* There are no classes being defined.  */
2792   parser->num_classes_being_defined = 0;
2793
2794   /* No template parameters apply.  */
2795   parser->num_template_parameter_lists = 0;
2796
2797   return parser;
2798 }
2799
2800 /* Create a cp_lexer structure which will emit the tokens in CACHE
2801    and push it onto the parser's lexer stack.  This is used for delayed
2802    parsing of in-class method bodies and default arguments, and should
2803    not be confused with tentative parsing.  */
2804 static void
2805 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2806 {
2807   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2808   lexer->next = parser->lexer;
2809   parser->lexer = lexer;
2810
2811   /* Move the current source position to that of the first token in the
2812      new lexer.  */
2813   cp_lexer_set_source_position_from_token (lexer->next_token);
2814 }
2815
2816 /* Pop the top lexer off the parser stack.  This is never used for the
2817    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2818 static void
2819 cp_parser_pop_lexer (cp_parser *parser)
2820 {
2821   cp_lexer *lexer = parser->lexer;
2822   parser->lexer = lexer->next;
2823   cp_lexer_destroy (lexer);
2824
2825   /* Put the current source position back where it was before this
2826      lexer was pushed.  */
2827   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2828 }
2829
2830 /* Lexical conventions [gram.lex]  */
2831
2832 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2833    identifier.  */
2834
2835 static tree
2836 cp_parser_identifier (cp_parser* parser)
2837 {
2838   cp_token *token;
2839
2840   /* Look for the identifier.  */
2841   token = cp_parser_require (parser, CPP_NAME, "identifier");
2842   /* Return the value.  */
2843   return token ? token->u.value : error_mark_node;
2844 }
2845
2846 /* Parse a sequence of adjacent string constants.  Returns a
2847    TREE_STRING representing the combined, nul-terminated string
2848    constant.  If TRANSLATE is true, translate the string to the
2849    execution character set.  If WIDE_OK is true, a wide string is
2850    invalid here.
2851
2852    C++98 [lex.string] says that if a narrow string literal token is
2853    adjacent to a wide string literal token, the behavior is undefined.
2854    However, C99 6.4.5p4 says that this results in a wide string literal.
2855    We follow C99 here, for consistency with the C front end.
2856
2857    This code is largely lifted from lex_string() in c-lex.c.
2858
2859    FUTURE: ObjC++ will need to handle @-strings here.  */
2860 static tree
2861 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2862 {
2863   tree value;
2864   bool wide = false;
2865   size_t count;
2866   struct obstack str_ob;
2867   cpp_string str, istr, *strs;
2868   cp_token *tok;
2869
2870   tok = cp_lexer_peek_token (parser->lexer);
2871   if (!cp_parser_is_string_literal (tok))
2872     {
2873       cp_parser_error (parser, "expected string-literal");
2874       return error_mark_node;
2875     }
2876
2877   /* Try to avoid the overhead of creating and destroying an obstack
2878      for the common case of just one string.  */
2879   if (!cp_parser_is_string_literal
2880       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2881     {
2882       cp_lexer_consume_token (parser->lexer);
2883
2884       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2885       str.len = TREE_STRING_LENGTH (tok->u.value);
2886       count = 1;
2887       if (tok->type == CPP_WSTRING)
2888         wide = true;
2889
2890       strs = &str;
2891     }
2892   else
2893     {
2894       gcc_obstack_init (&str_ob);
2895       count = 0;
2896
2897       do
2898         {
2899           cp_lexer_consume_token (parser->lexer);
2900           count++;
2901           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2902           str.len = TREE_STRING_LENGTH (tok->u.value);
2903           if (tok->type == CPP_WSTRING)
2904             wide = true;
2905
2906           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2907
2908           tok = cp_lexer_peek_token (parser->lexer);
2909         }
2910       while (cp_parser_is_string_literal (tok));
2911
2912       strs = (cpp_string *) obstack_finish (&str_ob);
2913     }
2914
2915   if (wide && !wide_ok)
2916     {
2917       cp_parser_error (parser, "a wide string is invalid in this context");
2918       wide = false;
2919     }
2920
2921   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2922       (parse_in, strs, count, &istr, wide))
2923     {
2924       value = build_string (istr.len, (const char *)istr.text);
2925       free (CONST_CAST (unsigned char *, istr.text));
2926
2927       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2928       value = fix_string_type (value);
2929     }
2930   else
2931     /* cpp_interpret_string has issued an error.  */
2932     value = error_mark_node;
2933
2934   if (count > 1)
2935     obstack_free (&str_ob, 0);
2936
2937   return value;
2938 }
2939
2940
2941 /* Basic concepts [gram.basic]  */
2942
2943 /* Parse a translation-unit.
2944
2945    translation-unit:
2946      declaration-seq [opt]
2947
2948    Returns TRUE if all went well.  */
2949
2950 static bool
2951 cp_parser_translation_unit (cp_parser* parser)
2952 {
2953   /* The address of the first non-permanent object on the declarator
2954      obstack.  */
2955   static void *declarator_obstack_base;
2956
2957   bool success;
2958
2959   /* Create the declarator obstack, if necessary.  */
2960   if (!cp_error_declarator)
2961     {
2962       gcc_obstack_init (&declarator_obstack);
2963       /* Create the error declarator.  */
2964       cp_error_declarator = make_declarator (cdk_error);
2965       /* Create the empty parameter list.  */
2966       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2967       /* Remember where the base of the declarator obstack lies.  */
2968       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2969     }
2970
2971   cp_parser_declaration_seq_opt (parser);
2972
2973   /* If there are no tokens left then all went well.  */
2974   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2975     {
2976       /* Get rid of the token array; we don't need it any more.  */
2977       cp_lexer_destroy (parser->lexer);
2978       parser->lexer = NULL;
2979
2980       /* This file might have been a context that's implicitly extern
2981          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2982       if (parser->implicit_extern_c)
2983         {
2984           pop_lang_context ();
2985           parser->implicit_extern_c = false;
2986         }
2987
2988       /* Finish up.  */
2989       finish_translation_unit ();
2990
2991       success = true;
2992     }
2993   else
2994     {
2995       cp_parser_error (parser, "expected declaration");
2996       success = false;
2997     }
2998
2999   /* Make sure the declarator obstack was fully cleaned up.  */
3000   gcc_assert (obstack_next_free (&declarator_obstack)
3001               == declarator_obstack_base);
3002
3003   /* All went well.  */
3004   return success;
3005 }
3006
3007 /* Expressions [gram.expr] */
3008
3009 /* Parse a primary-expression.
3010
3011    primary-expression:
3012      literal
3013      this
3014      ( expression )
3015      id-expression
3016
3017    GNU Extensions:
3018
3019    primary-expression:
3020      ( compound-statement )
3021      __builtin_va_arg ( assignment-expression , type-id )
3022      __builtin_offsetof ( type-id , offsetof-expression )
3023
3024    C++ Extensions:
3025      __has_nothrow_assign ( type-id )   
3026      __has_nothrow_constructor ( type-id )
3027      __has_nothrow_copy ( type-id )
3028      __has_trivial_assign ( type-id )   
3029      __has_trivial_constructor ( type-id )
3030      __has_trivial_copy ( type-id )
3031      __has_trivial_destructor ( type-id )
3032      __has_virtual_destructor ( type-id )     
3033      __is_abstract ( type-id )
3034      __is_base_of ( type-id , type-id )
3035      __is_class ( type-id )
3036      __is_convertible_to ( type-id , type-id )     
3037      __is_empty ( type-id )
3038      __is_enum ( type-id )
3039      __is_pod ( type-id )
3040      __is_polymorphic ( type-id )
3041      __is_union ( type-id )
3042
3043    Objective-C++ Extension:
3044
3045    primary-expression:
3046      objc-expression
3047
3048    literal:
3049      __null
3050
3051    ADDRESS_P is true iff this expression was immediately preceded by
3052    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3053    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3054    true iff this expression is a template argument.
3055
3056    Returns a representation of the expression.  Upon return, *IDK
3057    indicates what kind of id-expression (if any) was present.  */
3058
3059 static tree
3060 cp_parser_primary_expression (cp_parser *parser,
3061                               bool address_p,
3062                               bool cast_p,
3063                               bool template_arg_p,
3064                               cp_id_kind *idk)
3065 {
3066   cp_token *token;
3067
3068   /* Assume the primary expression is not an id-expression.  */
3069   *idk = CP_ID_KIND_NONE;
3070
3071   /* Peek at the next token.  */
3072   token = cp_lexer_peek_token (parser->lexer);
3073   switch (token->type)
3074     {
3075       /* literal:
3076            integer-literal
3077            character-literal
3078            floating-literal
3079            string-literal
3080            boolean-literal  */
3081     case CPP_CHAR:
3082     case CPP_WCHAR:
3083     case CPP_NUMBER:
3084       token = cp_lexer_consume_token (parser->lexer);
3085       /* Floating-point literals are only allowed in an integral
3086          constant expression if they are cast to an integral or
3087          enumeration type.  */
3088       if (TREE_CODE (token->u.value) == REAL_CST
3089           && parser->integral_constant_expression_p
3090           && pedantic)
3091         {
3092           /* CAST_P will be set even in invalid code like "int(2.7 +
3093              ...)".   Therefore, we have to check that the next token
3094              is sure to end the cast.  */
3095           if (cast_p)
3096             {
3097               cp_token *next_token;
3098
3099               next_token = cp_lexer_peek_token (parser->lexer);
3100               if (/* The comma at the end of an
3101                      enumerator-definition.  */
3102                   next_token->type != CPP_COMMA
3103                   /* The curly brace at the end of an enum-specifier.  */
3104                   && next_token->type != CPP_CLOSE_BRACE
3105                   /* The end of a statement.  */
3106                   && next_token->type != CPP_SEMICOLON
3107                   /* The end of the cast-expression.  */
3108                   && next_token->type != CPP_CLOSE_PAREN
3109                   /* The end of an array bound.  */
3110                   && next_token->type != CPP_CLOSE_SQUARE
3111                   /* The closing ">" in a template-argument-list.  */
3112                   && (next_token->type != CPP_GREATER
3113                       || parser->greater_than_is_operator_p)
3114                   /* C++0x only: A ">>" treated like two ">" tokens,
3115                      in a template-argument-list.  */
3116                   && (next_token->type != CPP_RSHIFT
3117                       || (cxx_dialect == cxx98)
3118                       || parser->greater_than_is_operator_p))
3119                 cast_p = false;
3120             }
3121
3122           /* If we are within a cast, then the constraint that the
3123              cast is to an integral or enumeration type will be
3124              checked at that point.  If we are not within a cast, then
3125              this code is invalid.  */
3126           if (!cast_p)
3127             cp_parser_non_integral_constant_expression
3128               (parser, "floating-point literal");
3129         }
3130       return token->u.value;
3131
3132     case CPP_STRING:
3133     case CPP_WSTRING:
3134       /* ??? Should wide strings be allowed when parser->translate_strings_p
3135          is false (i.e. in attributes)?  If not, we can kill the third
3136          argument to cp_parser_string_literal.  */
3137       return cp_parser_string_literal (parser,
3138                                        parser->translate_strings_p,
3139                                        true);
3140
3141     case CPP_OPEN_PAREN:
3142       {
3143         tree expr;
3144         bool saved_greater_than_is_operator_p;
3145
3146         /* Consume the `('.  */
3147         cp_lexer_consume_token (parser->lexer);
3148         /* Within a parenthesized expression, a `>' token is always
3149            the greater-than operator.  */
3150         saved_greater_than_is_operator_p
3151           = parser->greater_than_is_operator_p;
3152         parser->greater_than_is_operator_p = true;
3153         /* If we see `( { ' then we are looking at the beginning of
3154            a GNU statement-expression.  */
3155         if (cp_parser_allow_gnu_extensions_p (parser)
3156             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3157           {
3158             /* Statement-expressions are not allowed by the standard.  */
3159             if (pedantic)
3160               pedwarn ("ISO C++ forbids braced-groups within expressions");
3161
3162             /* And they're not allowed outside of a function-body; you
3163                cannot, for example, write:
3164
3165                  int i = ({ int j = 3; j + 1; });
3166
3167                at class or namespace scope.  */
3168             if (!parser->in_function_body
3169                 || parser->in_template_argument_list_p)
3170               {
3171                 error ("statement-expressions are not allowed outside "
3172                        "functions nor in template-argument lists");
3173                 cp_parser_skip_to_end_of_block_or_statement (parser);
3174                 expr = error_mark_node;
3175               }
3176             else
3177               {
3178                 /* Start the statement-expression.  */
3179                 expr = begin_stmt_expr ();
3180                 /* Parse the compound-statement.  */
3181                 cp_parser_compound_statement (parser, expr, false);
3182                 /* Finish up.  */
3183                 expr = finish_stmt_expr (expr, false);
3184               }
3185           }
3186         else
3187           {
3188             /* Parse the parenthesized expression.  */
3189             expr = cp_parser_expression (parser, cast_p);
3190             /* Let the front end know that this expression was
3191                enclosed in parentheses. This matters in case, for
3192                example, the expression is of the form `A::B', since
3193                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3194                not.  */
3195             finish_parenthesized_expr (expr);
3196           }
3197         /* The `>' token might be the end of a template-id or
3198            template-parameter-list now.  */
3199         parser->greater_than_is_operator_p
3200           = saved_greater_than_is_operator_p;
3201         /* Consume the `)'.  */
3202         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3203           cp_parser_skip_to_end_of_statement (parser);
3204
3205         return expr;
3206       }
3207
3208     case CPP_KEYWORD:
3209       switch (token->keyword)
3210         {
3211           /* These two are the boolean literals.  */
3212         case RID_TRUE:
3213           cp_lexer_consume_token (parser->lexer);
3214           return boolean_true_node;
3215         case RID_FALSE:
3216           cp_lexer_consume_token (parser->lexer);
3217           return boolean_false_node;
3218
3219           /* The `__null' literal.  */
3220         case RID_NULL:
3221           cp_lexer_consume_token (parser->lexer);
3222           return null_node;
3223
3224           /* Recognize the `this' keyword.  */
3225         case RID_THIS:
3226           cp_lexer_consume_token (parser->lexer);
3227           if (parser->local_variables_forbidden_p)
3228             {
3229               error ("%<this%> may not be used in this context");
3230               return error_mark_node;
3231             }
3232           /* Pointers cannot appear in constant-expressions.  */
3233           if (cp_parser_non_integral_constant_expression (parser,
3234                                                           "`this'"))
3235             return error_mark_node;
3236           return finish_this_expr ();
3237
3238           /* The `operator' keyword can be the beginning of an
3239              id-expression.  */
3240         case RID_OPERATOR:
3241           goto id_expression;
3242
3243         case RID_FUNCTION_NAME:
3244         case RID_PRETTY_FUNCTION_NAME:
3245         case RID_C99_FUNCTION_NAME:
3246           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3247              __func__ are the names of variables -- but they are
3248              treated specially.  Therefore, they are handled here,
3249              rather than relying on the generic id-expression logic
3250              below.  Grammatically, these names are id-expressions.
3251
3252              Consume the token.  */
3253           token = cp_lexer_consume_token (parser->lexer);
3254           /* Look up the name.  */
3255           return finish_fname (token->u.value);
3256
3257         case RID_VA_ARG:
3258           {
3259             tree expression;
3260             tree type;
3261
3262             /* The `__builtin_va_arg' construct is used to handle
3263                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3264             cp_lexer_consume_token (parser->lexer);
3265             /* Look for the opening `('.  */
3266             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3267             /* Now, parse the assignment-expression.  */
3268             expression = cp_parser_assignment_expression (parser,
3269                                                           /*cast_p=*/false);
3270             /* Look for the `,'.  */
3271             cp_parser_require (parser, CPP_COMMA, "`,'");
3272             /* Parse the type-id.  */
3273             type = cp_parser_type_id (parser);
3274             /* Look for the closing `)'.  */
3275             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3276             /* Using `va_arg' in a constant-expression is not
3277                allowed.  */
3278             if (cp_parser_non_integral_constant_expression (parser,
3279                                                             "`va_arg'"))
3280               return error_mark_node;
3281             return build_x_va_arg (expression, type);
3282           }
3283
3284         case RID_OFFSETOF:
3285           return cp_parser_builtin_offsetof (parser);
3286
3287         case RID_HAS_NOTHROW_ASSIGN:
3288         case RID_HAS_NOTHROW_CONSTRUCTOR:
3289         case RID_HAS_NOTHROW_COPY:        
3290         case RID_HAS_TRIVIAL_ASSIGN:
3291         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3292         case RID_HAS_TRIVIAL_COPY:        
3293         case RID_HAS_TRIVIAL_DESTRUCTOR:
3294         case RID_HAS_VIRTUAL_DESTRUCTOR:
3295         case RID_IS_ABSTRACT:
3296         case RID_IS_BASE_OF:
3297         case RID_IS_CLASS:
3298         case RID_IS_CONVERTIBLE_TO:
3299         case RID_IS_EMPTY:
3300         case RID_IS_ENUM:
3301         case RID_IS_POD:
3302         case RID_IS_POLYMORPHIC:
3303         case RID_IS_UNION:
3304           return cp_parser_trait_expr (parser, token->keyword);
3305
3306         /* Objective-C++ expressions.  */
3307         case RID_AT_ENCODE:
3308         case RID_AT_PROTOCOL:
3309         case RID_AT_SELECTOR:
3310           return cp_parser_objc_expression (parser);
3311
3312         default:
3313           cp_parser_error (parser, "expected primary-expression");
3314           return error_mark_node;
3315         }
3316
3317       /* An id-expression can start with either an identifier, a
3318          `::' as the beginning of a qualified-id, or the "operator"
3319          keyword.  */
3320     case CPP_NAME:
3321     case CPP_SCOPE:
3322     case CPP_TEMPLATE_ID:
3323     case CPP_NESTED_NAME_SPECIFIER:
3324       {
3325         tree id_expression;
3326         tree decl;
3327         const char *error_msg;
3328         bool template_p;
3329         bool done;
3330
3331       id_expression:
3332         /* Parse the id-expression.  */
3333         id_expression
3334           = cp_parser_id_expression (parser,
3335                                      /*template_keyword_p=*/false,
3336                                      /*check_dependency_p=*/true,
3337                                      &template_p,
3338                                      /*declarator_p=*/false,
3339                                      /*optional_p=*/false);
3340         if (id_expression == error_mark_node)
3341           return error_mark_node;
3342         token = cp_lexer_peek_token (parser->lexer);
3343         done = (token->type != CPP_OPEN_SQUARE
3344                 && token->type != CPP_OPEN_PAREN
3345                 && token->type != CPP_DOT
3346                 && token->type != CPP_DEREF
3347                 && token->type != CPP_PLUS_PLUS
3348                 && token->type != CPP_MINUS_MINUS);
3349         /* If we have a template-id, then no further lookup is
3350            required.  If the template-id was for a template-class, we
3351            will sometimes have a TYPE_DECL at this point.  */
3352         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3353                  || TREE_CODE (id_expression) == TYPE_DECL)
3354           decl = id_expression;
3355         /* Look up the name.  */
3356         else
3357           {
3358             tree ambiguous_decls;
3359
3360             decl = cp_parser_lookup_name (parser, id_expression,
3361                                           none_type,
3362                                           template_p,
3363                                           /*is_namespace=*/false,
3364                                           /*check_dependency=*/true,
3365                                           &ambiguous_decls);
3366             /* If the lookup was ambiguous, an error will already have
3367                been issued.  */
3368             if (ambiguous_decls)
3369               return error_mark_node;
3370
3371             /* In Objective-C++, an instance variable (ivar) may be preferred
3372                to whatever cp_parser_lookup_name() found.  */
3373             decl = objc_lookup_ivar (decl, id_expression);
3374
3375             /* If name lookup gives us a SCOPE_REF, then the
3376                qualifying scope was dependent.  */
3377             if (TREE_CODE (decl) == SCOPE_REF)
3378               {
3379                 /* At this point, we do not know if DECL is a valid
3380                    integral constant expression.  We assume that it is
3381                    in fact such an expression, so that code like:
3382
3383                       template <int N> struct A {
3384                         int a[B<N>::i];
3385                       };
3386                      
3387                    is accepted.  At template-instantiation time, we
3388                    will check that B<N>::i is actually a constant.  */
3389                 return decl;
3390               }
3391             /* Check to see if DECL is a local variable in a context
3392                where that is forbidden.  */
3393             if (parser->local_variables_forbidden_p
3394                 && local_variable_p (decl))
3395               {
3396                 /* It might be that we only found DECL because we are
3397                    trying to be generous with pre-ISO scoping rules.
3398                    For example, consider:
3399
3400                      int i;
3401                      void g() {
3402                        for (int i = 0; i < 10; ++i) {}
3403                        extern void f(int j = i);
3404                      }
3405
3406                    Here, name look up will originally find the out
3407                    of scope `i'.  We need to issue a warning message,
3408                    but then use the global `i'.  */
3409                 decl = check_for_out_of_scope_variable (decl);
3410                 if (local_variable_p (decl))
3411                   {
3412                     error ("local variable %qD may not appear in this context",
3413                            decl);
3414                     return error_mark_node;
3415                   }
3416               }
3417           }
3418
3419         decl = (finish_id_expression
3420                 (id_expression, decl, parser->scope,
3421                  idk,
3422                  parser->integral_constant_expression_p,
3423                  parser->allow_non_integral_constant_expression_p,
3424                  &parser->non_integral_constant_expression_p,
3425                  template_p, done, address_p,
3426                  template_arg_p,
3427                  &error_msg));
3428         if (error_msg)
3429           cp_parser_error (parser, error_msg);
3430         return decl;
3431       }
3432
3433       /* Anything else is an error.  */
3434     default:
3435       /* ...unless we have an Objective-C++ message or string literal,
3436          that is.  */
3437       if (c_dialect_objc ()
3438           && (token->type == CPP_OPEN_SQUARE
3439               || token->type == CPP_OBJC_STRING))
3440         return cp_parser_objc_expression (parser);
3441
3442       cp_parser_error (parser, "expected primary-expression");
3443       return error_mark_node;
3444     }
3445 }
3446
3447 /* Parse an id-expression.
3448
3449    id-expression:
3450      unqualified-id
3451      qualified-id
3452
3453    qualified-id:
3454      :: [opt] nested-name-specifier template [opt] unqualified-id
3455      :: identifier
3456      :: operator-function-id
3457      :: template-id
3458
3459    Return a representation of the unqualified portion of the
3460    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3461    a `::' or nested-name-specifier.
3462
3463    Often, if the id-expression was a qualified-id, the caller will
3464    want to make a SCOPE_REF to represent the qualified-id.  This
3465    function does not do this in order to avoid wastefully creating
3466    SCOPE_REFs when they are not required.
3467
3468    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3469    `template' keyword.
3470
3471    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3472    uninstantiated templates.
3473
3474    If *TEMPLATE_P is non-NULL, it is set to true iff the
3475    `template' keyword is used to explicitly indicate that the entity
3476    named is a template.
3477
3478    If DECLARATOR_P is true, the id-expression is appearing as part of
3479    a declarator, rather than as part of an expression.  */
3480
3481 static tree
3482 cp_parser_id_expression (cp_parser *parser,
3483                          bool template_keyword_p,
3484                          bool check_dependency_p,
3485                          bool *template_p,
3486                          bool declarator_p,
3487                          bool optional_p)
3488 {
3489   bool global_scope_p;
3490   bool nested_name_specifier_p;
3491
3492   /* Assume the `template' keyword was not used.  */
3493   if (template_p)
3494     *template_p = template_keyword_p;
3495
3496   /* Look for the optional `::' operator.  */
3497   global_scope_p
3498     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3499        != NULL_TREE);
3500   /* Look for the optional nested-name-specifier.  */
3501   nested_name_specifier_p
3502     = (cp_parser_nested_name_specifier_opt (parser,
3503                                             /*typename_keyword_p=*/false,
3504                                             check_dependency_p,
3505                                             /*type_p=*/false,
3506                                             declarator_p)
3507        != NULL_TREE);
3508   /* If there is a nested-name-specifier, then we are looking at
3509      the first qualified-id production.  */
3510   if (nested_name_specifier_p)
3511     {
3512       tree saved_scope;
3513       tree saved_object_scope;
3514       tree saved_qualifying_scope;
3515       tree unqualified_id;
3516       bool is_template;
3517
3518       /* See if the next token is the `template' keyword.  */
3519       if (!template_p)
3520         template_p = &is_template;
3521       *template_p = cp_parser_optional_template_keyword (parser);
3522       /* Name lookup we do during the processing of the
3523          unqualified-id might obliterate SCOPE.  */
3524       saved_scope = parser->scope;
3525       saved_object_scope = parser->object_scope;
3526       saved_qualifying_scope = parser->qualifying_scope;
3527       /* Process the final unqualified-id.  */
3528       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3529                                                  check_dependency_p,
3530                                                  declarator_p,
3531                                                  /*optional_p=*/false);
3532       /* Restore the SAVED_SCOPE for our caller.  */
3533       parser->scope = saved_scope;
3534       parser->object_scope = saved_object_scope;
3535       parser->qualifying_scope = saved_qualifying_scope;
3536
3537       return unqualified_id;
3538     }
3539   /* Otherwise, if we are in global scope, then we are looking at one
3540      of the other qualified-id productions.  */
3541   else if (global_scope_p)
3542     {
3543       cp_token *token;
3544       tree id;
3545
3546       /* Peek at the next token.  */
3547       token = cp_lexer_peek_token (parser->lexer);
3548
3549       /* If it's an identifier, and the next token is not a "<", then
3550          we can avoid the template-id case.  This is an optimization
3551          for this common case.  */
3552       if (token->type == CPP_NAME
3553           && !cp_parser_nth_token_starts_template_argument_list_p
3554                (parser, 2))
3555         return cp_parser_identifier (parser);
3556
3557       cp_parser_parse_tentatively (parser);
3558       /* Try a template-id.  */
3559       id = cp_parser_template_id (parser,
3560                                   /*template_keyword_p=*/false,
3561                                   /*check_dependency_p=*/true,
3562                                   declarator_p);
3563       /* If that worked, we're done.  */
3564       if (cp_parser_parse_definitely (parser))
3565         return id;
3566
3567       /* Peek at the next token.  (Changes in the token buffer may
3568          have invalidated the pointer obtained above.)  */
3569       token = cp_lexer_peek_token (parser->lexer);
3570
3571       switch (token->type)
3572         {
3573         case CPP_NAME:
3574           return cp_parser_identifier (parser);
3575
3576         case CPP_KEYWORD:
3577           if (token->keyword == RID_OPERATOR)
3578             return cp_parser_operator_function_id (parser);
3579           /* Fall through.  */
3580
3581         default:
3582           cp_parser_error (parser, "expected id-expression");
3583           return error_mark_node;
3584         }
3585     }
3586   else
3587     return cp_parser_unqualified_id (parser, template_keyword_p,
3588                                      /*check_dependency_p=*/true,
3589                                      declarator_p,
3590                                      optional_p);
3591 }
3592
3593 /* Parse an unqualified-id.
3594
3595    unqualified-id:
3596      identifier
3597      operator-function-id
3598      conversion-function-id
3599      ~ class-name
3600      template-id
3601
3602    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3603    keyword, in a construct like `A::template ...'.
3604
3605    Returns a representation of unqualified-id.  For the `identifier'
3606    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3607    production a BIT_NOT_EXPR is returned; the operand of the
3608    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3609    other productions, see the documentation accompanying the
3610    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3611    names are looked up in uninstantiated templates.  If DECLARATOR_P
3612    is true, the unqualified-id is appearing as part of a declarator,
3613    rather than as part of an expression.  */
3614
3615 static tree
3616 cp_parser_unqualified_id (cp_parser* parser,
3617                           bool template_keyword_p,
3618                           bool check_dependency_p,
3619                           bool declarator_p,
3620                           bool optional_p)
3621 {
3622   cp_token *token;
3623
3624   /* Peek at the next token.  */
3625   token = cp_lexer_peek_token (parser->lexer);
3626
3627   switch (token->type)
3628     {
3629     case CPP_NAME:
3630       {
3631         tree id;
3632
3633         /* We don't know yet whether or not this will be a
3634            template-id.  */
3635         cp_parser_parse_tentatively (parser);
3636         /* Try a template-id.  */
3637         id = cp_parser_template_id (parser, template_keyword_p,
3638                                     check_dependency_p,
3639                                     declarator_p);
3640         /* If it worked, we're done.  */
3641         if (cp_parser_parse_definitely (parser))
3642           return id;
3643         /* Otherwise, it's an ordinary identifier.  */
3644         return cp_parser_identifier (parser);
3645       }
3646
3647     case CPP_TEMPLATE_ID:
3648       return cp_parser_template_id (parser, template_keyword_p,
3649                                     check_dependency_p,
3650                                     declarator_p);
3651
3652     case CPP_COMPL:
3653       {
3654         tree type_decl;
3655         tree qualifying_scope;
3656         tree object_scope;
3657         tree scope;
3658         bool done;
3659
3660         /* Consume the `~' token.  */
3661         cp_lexer_consume_token (parser->lexer);
3662         /* Parse the class-name.  The standard, as written, seems to
3663            say that:
3664
3665              template <typename T> struct S { ~S (); };
3666              template <typename T> S<T>::~S() {}
3667
3668            is invalid, since `~' must be followed by a class-name, but
3669            `S<T>' is dependent, and so not known to be a class.
3670            That's not right; we need to look in uninstantiated
3671            templates.  A further complication arises from:
3672
3673              template <typename T> void f(T t) {
3674                t.T::~T();
3675              }
3676
3677            Here, it is not possible to look up `T' in the scope of `T'
3678            itself.  We must look in both the current scope, and the
3679            scope of the containing complete expression.
3680
3681            Yet another issue is:
3682
3683              struct S {
3684                int S;
3685                ~S();
3686              };
3687
3688              S::~S() {}
3689
3690            The standard does not seem to say that the `S' in `~S'
3691            should refer to the type `S' and not the data member
3692            `S::S'.  */
3693
3694         /* DR 244 says that we look up the name after the "~" in the
3695            same scope as we looked up the qualifying name.  That idea
3696            isn't fully worked out; it's more complicated than that.  */
3697         scope = parser->scope;
3698         object_scope = parser->object_scope;
3699         qualifying_scope = parser->qualifying_scope;
3700
3701         /* Check for invalid scopes.  */
3702         if (scope == error_mark_node)
3703           {
3704             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3705               cp_lexer_consume_token (parser->lexer);
3706             return error_mark_node;
3707           }
3708         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3709           {
3710             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3711               error ("scope %qT before %<~%> is not a class-name", scope);
3712             cp_parser_simulate_error (parser);
3713             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3714               cp_lexer_consume_token (parser->lexer);
3715             return error_mark_node;
3716           }
3717         gcc_assert (!scope || TYPE_P (scope));
3718
3719         /* If the name is of the form "X::~X" it's OK.  */
3720         token = cp_lexer_peek_token (parser->lexer);
3721         if (scope
3722             && token->type == CPP_NAME
3723             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3724                 == CPP_OPEN_PAREN)
3725             && constructor_name_p (token->u.value, scope))
3726           {
3727             cp_lexer_consume_token (parser->lexer);
3728             return build_nt (BIT_NOT_EXPR, scope);
3729           }
3730
3731         /* If there was an explicit qualification (S::~T), first look
3732            in the scope given by the qualification (i.e., S).  */
3733         done = false;
3734         type_decl = NULL_TREE;
3735         if (scope)
3736           {
3737             cp_parser_parse_tentatively (parser);
3738             type_decl = cp_parser_class_name (parser,
3739                                               /*typename_keyword_p=*/false,
3740                                               /*template_keyword_p=*/false,
3741                                               none_type,
3742                                               /*check_dependency=*/false,
3743                                               /*class_head_p=*/false,
3744                                               declarator_p);
3745             if (cp_parser_parse_definitely (parser))
3746               done = true;
3747           }
3748         /* In "N::S::~S", look in "N" as well.  */
3749         if (!done && scope && qualifying_scope)
3750           {
3751             cp_parser_parse_tentatively (parser);
3752             parser->scope = qualifying_scope;
3753             parser->object_scope = NULL_TREE;
3754             parser->qualifying_scope = NULL_TREE;
3755             type_decl
3756               = cp_parser_class_name (parser,
3757                                       /*typename_keyword_p=*/false,
3758                                       /*template_keyword_p=*/false,
3759                                       none_type,
3760                                       /*check_dependency=*/false,
3761                                       /*class_head_p=*/false,
3762                                       declarator_p);
3763             if (cp_parser_parse_definitely (parser))
3764               done = true;
3765           }
3766         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3767         else if (!done && object_scope)
3768           {
3769             cp_parser_parse_tentatively (parser);
3770             parser->scope = object_scope;
3771             parser->object_scope = NULL_TREE;
3772             parser->qualifying_scope = NULL_TREE;
3773             type_decl
3774               = cp_parser_class_name (parser,
3775                                       /*typename_keyword_p=*/false,
3776                                       /*template_keyword_p=*/false,
3777                                       none_type,
3778                                       /*check_dependency=*/false,
3779                                       /*class_head_p=*/false,
3780                                       declarator_p);
3781             if (cp_parser_parse_definitely (parser))
3782               done = true;
3783           }
3784         /* Look in the surrounding context.  */
3785         if (!done)
3786           {
3787             parser->scope = NULL_TREE;
3788             parser->object_scope = NULL_TREE;
3789             parser->qualifying_scope = NULL_TREE;
3790             type_decl
3791               = cp_parser_class_name (parser,
3792                                       /*typename_keyword_p=*/false,
3793                                       /*template_keyword_p=*/false,
3794                                       none_type,
3795                                       /*check_dependency=*/false,
3796                                       /*class_head_p=*/false,
3797                                       declarator_p);
3798           }
3799         /* If an error occurred, assume that the name of the
3800            destructor is the same as the name of the qualifying
3801            class.  That allows us to keep parsing after running
3802            into ill-formed destructor names.  */
3803         if (type_decl == error_mark_node && scope)
3804           return build_nt (BIT_NOT_EXPR, scope);
3805         else if (type_decl == error_mark_node)
3806           return error_mark_node;
3807
3808         /* Check that destructor name and scope match.  */
3809         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3810           {
3811             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3812               error ("declaration of %<~%T%> as member of %qT",
3813                      type_decl, scope);
3814             cp_parser_simulate_error (parser);
3815             return error_mark_node;
3816           }
3817
3818         /* [class.dtor]
3819
3820            A typedef-name that names a class shall not be used as the
3821            identifier in the declarator for a destructor declaration.  */
3822         if (declarator_p
3823             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3824             && !DECL_SELF_REFERENCE_P (type_decl)
3825             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3826           error ("typedef-name %qD used as destructor declarator",
3827                  type_decl);
3828
3829         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3830       }
3831
3832     case CPP_KEYWORD:
3833       if (token->keyword == RID_OPERATOR)
3834         {
3835           tree id;
3836
3837           /* This could be a template-id, so we try that first.  */
3838           cp_parser_parse_tentatively (parser);
3839           /* Try a template-id.  */
3840           id = cp_parser_template_id (parser, template_keyword_p,
3841                                       /*check_dependency_p=*/true,
3842                                       declarator_p);
3843           /* If that worked, we're done.  */
3844           if (cp_parser_parse_definitely (parser))
3845             return id;
3846           /* We still don't know whether we're looking at an
3847              operator-function-id or a conversion-function-id.  */
3848           cp_parser_parse_tentatively (parser);
3849           /* Try an operator-function-id.  */
3850           id = cp_parser_operator_function_id (parser);
3851           /* If that didn't work, try a conversion-function-id.  */
3852           if (!cp_parser_parse_definitely (parser))
3853             id = cp_parser_conversion_function_id (parser);
3854
3855           return id;
3856         }
3857       /* Fall through.  */
3858
3859     default:
3860       if (optional_p)
3861         return NULL_TREE;
3862       cp_parser_error (parser, "expected unqualified-id");
3863       return error_mark_node;
3864     }
3865 }
3866
3867 /* Parse an (optional) nested-name-specifier.
3868
3869    nested-name-specifier:
3870      class-or-namespace-name :: nested-name-specifier [opt]
3871      class-or-namespace-name :: template nested-name-specifier [opt]
3872
3873    PARSER->SCOPE should be set appropriately before this function is
3874    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3875    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3876    in name lookups.
3877
3878    Sets PARSER->SCOPE to the class (TYPE) or namespace
3879    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3880    it unchanged if there is no nested-name-specifier.  Returns the new
3881    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3882
3883    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3884    part of a declaration and/or decl-specifier.  */
3885
3886 static tree
3887 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3888                                      bool typename_keyword_p,
3889                                      bool check_dependency_p,
3890                                      bool type_p,
3891                                      bool is_declaration)
3892 {
3893   bool success = false;
3894   cp_token_position start = 0;
3895   cp_token *token;
3896
3897   /* Remember where the nested-name-specifier starts.  */
3898   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3899     {
3900       start = cp_lexer_token_position (parser->lexer, false);
3901       push_deferring_access_checks (dk_deferred);
3902     }
3903
3904   while (true)
3905     {
3906       tree new_scope;
3907       tree old_scope;
3908       tree saved_qualifying_scope;
3909       bool template_keyword_p;
3910
3911       /* Spot cases that cannot be the beginning of a
3912          nested-name-specifier.  */
3913       token = cp_lexer_peek_token (parser->lexer);
3914
3915       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3916          the already parsed nested-name-specifier.  */
3917       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3918         {
3919           /* Grab the nested-name-specifier and continue the loop.  */
3920           cp_parser_pre_parsed_nested_name_specifier (parser);
3921           /* If we originally encountered this nested-name-specifier
3922              with IS_DECLARATION set to false, we will not have
3923              resolved TYPENAME_TYPEs, so we must do so here.  */
3924           if (is_declaration
3925               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3926             {
3927               new_scope = resolve_typename_type (parser->scope,
3928                                                  /*only_current_p=*/false);
3929               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3930                 parser->scope = new_scope;
3931             }
3932           success = true;
3933           continue;
3934         }
3935
3936       /* Spot cases that cannot be the beginning of a
3937          nested-name-specifier.  On the second and subsequent times
3938          through the loop, we look for the `template' keyword.  */
3939       if (success && token->keyword == RID_TEMPLATE)
3940         ;
3941       /* A template-id can start a nested-name-specifier.  */
3942       else if (token->type == CPP_TEMPLATE_ID)
3943         ;
3944       else
3945         {
3946           /* If the next token is not an identifier, then it is
3947              definitely not a class-or-namespace-name.  */
3948           if (token->type != CPP_NAME)
3949             break;
3950           /* If the following token is neither a `<' (to begin a
3951              template-id), nor a `::', then we are not looking at a
3952              nested-name-specifier.  */
3953           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3954           if (token->type != CPP_SCOPE
3955               && !cp_parser_nth_token_starts_template_argument_list_p
3956                   (parser, 2))
3957             break;
3958         }
3959
3960       /* The nested-name-specifier is optional, so we parse
3961          tentatively.  */
3962       cp_parser_parse_tentatively (parser);
3963
3964       /* Look for the optional `template' keyword, if this isn't the
3965          first time through the loop.  */
3966       if (success)
3967         template_keyword_p = cp_parser_optional_template_keyword (parser);
3968       else
3969         template_keyword_p = false;
3970
3971       /* Save the old scope since the name lookup we are about to do
3972          might destroy it.  */
3973       old_scope = parser->scope;
3974       saved_qualifying_scope = parser->qualifying_scope;
3975       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3976          look up names in "X<T>::I" in order to determine that "Y" is
3977          a template.  So, if we have a typename at this point, we make
3978          an effort to look through it.  */
3979       if (is_declaration
3980           && !typename_keyword_p
3981           && parser->scope
3982           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3983         parser->scope = resolve_typename_type (parser->scope,
3984                                                /*only_current_p=*/false);
3985       /* Parse the qualifying entity.  */
3986       new_scope
3987         = cp_parser_class_or_namespace_name (parser,
3988                                              typename_keyword_p,
3989                                              template_keyword_p,
3990                                              check_dependency_p,
3991                                              type_p,
3992                                              is_declaration);
3993       /* Look for the `::' token.  */
3994       cp_parser_require (parser, CPP_SCOPE, "`::'");
3995
3996       /* If we found what we wanted, we keep going; otherwise, we're
3997          done.  */
3998       if (!cp_parser_parse_definitely (parser))
3999         {
4000           bool error_p = false;
4001
4002           /* Restore the OLD_SCOPE since it was valid before the
4003              failed attempt at finding the last
4004              class-or-namespace-name.  */
4005           parser->scope = old_scope;
4006           parser->qualifying_scope = saved_qualifying_scope;
4007           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4008             break;
4009           /* If the next token is an identifier, and the one after
4010              that is a `::', then any valid interpretation would have
4011              found a class-or-namespace-name.  */
4012           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4013                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4014                      == CPP_SCOPE)
4015                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4016                      != CPP_COMPL))
4017             {
4018               token = cp_lexer_consume_token (parser->lexer);
4019               if (!error_p)
4020                 {
4021                   if (!token->ambiguous_p)
4022                     {
4023                       tree decl;
4024                       tree ambiguous_decls;
4025
4026                       decl = cp_parser_lookup_name (parser, token->u.value,
4027                                                     none_type,
4028                                                     /*is_template=*/false,
4029                                                     /*is_namespace=*/false,
4030                                                     /*check_dependency=*/true,
4031                                                     &ambiguous_decls);
4032                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4033                         error ("%qD used without template parameters", decl);
4034                       else if (ambiguous_decls)
4035                         {
4036                           error ("reference to %qD is ambiguous",
4037                                  token->u.value);
4038                           print_candidates (ambiguous_decls);
4039                           decl = error_mark_node;
4040                         }
4041                       else
4042                         cp_parser_name_lookup_error
4043                           (parser, token->u.value, decl,
4044                            "is not a class or namespace");
4045                     }
4046                   parser->scope = error_mark_node;
4047                   error_p = true;
4048                   /* Treat this as a successful nested-name-specifier
4049                      due to:
4050
4051                      [basic.lookup.qual]
4052
4053                      If the name found is not a class-name (clause
4054                      _class_) or namespace-name (_namespace.def_), the
4055                      program is ill-formed.  */
4056                   success = true;
4057                 }
4058               cp_lexer_consume_token (parser->lexer);
4059             }
4060           break;
4061         }
4062       /* We've found one valid nested-name-specifier.  */
4063       success = true;
4064       /* Name lookup always gives us a DECL.  */
4065       if (TREE_CODE (new_scope) == TYPE_DECL)
4066         new_scope = TREE_TYPE (new_scope);
4067       /* Uses of "template" must be followed by actual templates.  */
4068       if (template_keyword_p
4069           && !(CLASS_TYPE_P (new_scope)
4070                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4071                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4072                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4073           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4074                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4075                    == TEMPLATE_ID_EXPR)))
4076         pedwarn (TYPE_P (new_scope)
4077                  ? "%qT is not a template"
4078                  : "%qD is not a template",
4079                  new_scope);
4080       /* If it is a class scope, try to complete it; we are about to
4081          be looking up names inside the class.  */
4082       if (TYPE_P (new_scope)
4083           /* Since checking types for dependency can be expensive,
4084              avoid doing it if the type is already complete.  */
4085           && !COMPLETE_TYPE_P (new_scope)
4086           /* Do not try to complete dependent types.  */
4087           && !dependent_type_p (new_scope))
4088         new_scope = complete_type (new_scope);
4089       /* Make sure we look in the right scope the next time through
4090          the loop.  */
4091       parser->scope = new_scope;
4092     }
4093
4094   /* If parsing tentatively, replace the sequence of tokens that makes
4095      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4096      token.  That way, should we re-parse the token stream, we will
4097      not have to repeat the effort required to do the parse, nor will
4098      we issue duplicate error messages.  */
4099   if (success && start)
4100     {
4101       cp_token *token;
4102
4103       token = cp_lexer_token_at (parser->lexer, start);
4104       /* Reset the contents of the START token.  */
4105       token->type = CPP_NESTED_NAME_SPECIFIER;
4106       /* Retrieve any deferred checks.  Do not pop this access checks yet
4107          so the memory will not be reclaimed during token replacing below.  */
4108       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4109       token->u.tree_check_value->value = parser->scope;
4110       token->u.tree_check_value->checks = get_deferred_access_checks ();
4111       token->u.tree_check_value->qualifying_scope =
4112         parser->qualifying_scope;
4113       token->keyword = RID_MAX;
4114
4115       /* Purge all subsequent tokens.  */
4116       cp_lexer_purge_tokens_after (parser->lexer, start);
4117     }
4118
4119   if (start)
4120     pop_to_parent_deferring_access_checks ();
4121
4122   return success ? parser->scope : NULL_TREE;
4123 }
4124
4125 /* Parse a nested-name-specifier.  See
4126    cp_parser_nested_name_specifier_opt for details.  This function
4127    behaves identically, except that it will an issue an error if no
4128    nested-name-specifier is present.  */
4129
4130 static tree
4131 cp_parser_nested_name_specifier (cp_parser *parser,
4132                                  bool typename_keyword_p,
4133                                  bool check_dependency_p,
4134                                  bool type_p,
4135                                  bool is_declaration)
4136 {
4137   tree scope;
4138
4139   /* Look for the nested-name-specifier.  */
4140   scope = cp_parser_nested_name_specifier_opt (parser,
4141                                                typename_keyword_p,
4142                                                check_dependency_p,
4143                                                type_p,
4144                                                is_declaration);
4145   /* If it was not present, issue an error message.  */
4146   if (!scope)
4147     {
4148       cp_parser_error (parser, "expected nested-name-specifier");
4149       parser->scope = NULL_TREE;
4150     }
4151
4152   return scope;
4153 }
4154
4155 /* Parse a class-or-namespace-name.
4156
4157    class-or-namespace-name:
4158      class-name
4159      namespace-name
4160
4161    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4162    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4163    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4164    TYPE_P is TRUE iff the next name should be taken as a class-name,
4165    even the same name is declared to be another entity in the same
4166    scope.
4167
4168    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4169    specified by the class-or-namespace-name.  If neither is found the
4170    ERROR_MARK_NODE is returned.  */
4171
4172 static tree
4173 cp_parser_class_or_namespace_name (cp_parser *parser,
4174                                    bool typename_keyword_p,
4175                                    bool template_keyword_p,
4176                                    bool check_dependency_p,
4177                                    bool type_p,
4178                                    bool is_declaration)
4179 {
4180   tree saved_scope;
4181   tree saved_qualifying_scope;
4182   tree saved_object_scope;
4183   tree scope;
4184   bool only_class_p;
4185
4186   /* Before we try to parse the class-name, we must save away the
4187      current PARSER->SCOPE since cp_parser_class_name will destroy
4188      it.  */
4189   saved_scope = parser->scope;
4190   saved_qualifying_scope = parser->qualifying_scope;
4191   saved_object_scope = parser->object_scope;
4192   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4193      there is no need to look for a namespace-name.  */
4194   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4195   if (!only_class_p)
4196     cp_parser_parse_tentatively (parser);
4197   scope = cp_parser_class_name (parser,
4198                                 typename_keyword_p,
4199                                 template_keyword_p,
4200                                 type_p ? class_type : none_type,
4201                                 check_dependency_p,
4202                                 /*class_head_p=*/false,
4203                                 is_declaration);
4204   /* If that didn't work, try for a namespace-name.  */
4205   if (!only_class_p && !cp_parser_parse_definitely (parser))
4206     {
4207       /* Restore the saved scope.  */
4208       parser->scope = saved_scope;
4209       parser->qualifying_scope = saved_qualifying_scope;
4210       parser->object_scope = saved_object_scope;
4211       /* If we are not looking at an identifier followed by the scope
4212          resolution operator, then this is not part of a
4213          nested-name-specifier.  (Note that this function is only used
4214          to parse the components of a nested-name-specifier.)  */
4215       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4216           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4217         return error_mark_node;
4218       scope = cp_parser_namespace_name (parser);
4219     }
4220
4221   return scope;
4222 }
4223
4224 /* Parse a postfix-expression.
4225
4226    postfix-expression:
4227      primary-expression
4228      postfix-expression [ expression ]
4229      postfix-expression ( expression-list [opt] )
4230      simple-type-specifier ( expression-list [opt] )
4231      typename :: [opt] nested-name-specifier identifier
4232        ( expression-list [opt] )
4233      typename :: [opt] nested-name-specifier template [opt] template-id
4234        ( expression-list [opt] )
4235      postfix-expression . template [opt] id-expression
4236      postfix-expression -> template [opt] id-expression
4237      postfix-expression . pseudo-destructor-name
4238      postfix-expression -> pseudo-destructor-name
4239      postfix-expression ++
4240      postfix-expression --
4241      dynamic_cast < type-id > ( expression )
4242      static_cast < type-id > ( expression )
4243      reinterpret_cast < type-id > ( expression )
4244      const_cast < type-id > ( expression )
4245      typeid ( expression )
4246      typeid ( type-id )
4247
4248    GNU Extension:
4249
4250    postfix-expression:
4251      ( type-id ) { initializer-list , [opt] }
4252
4253    This extension is a GNU version of the C99 compound-literal
4254    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4255    but they are essentially the same concept.)
4256
4257    If ADDRESS_P is true, the postfix expression is the operand of the
4258    `&' operator.  CAST_P is true if this expression is the target of a
4259    cast.
4260
4261    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4262    class member access expressions [expr.ref].
4263
4264    Returns a representation of the expression.  */
4265
4266 static tree
4267 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4268                               bool member_access_only_p)
4269 {
4270   cp_token *token;
4271   enum rid keyword;
4272   cp_id_kind idk = CP_ID_KIND_NONE;
4273   tree postfix_expression = NULL_TREE;
4274   bool is_member_access = false;
4275
4276   /* Peek at the next token.  */
4277   token = cp_lexer_peek_token (parser->lexer);
4278   /* Some of the productions are determined by keywords.  */
4279   keyword = token->keyword;
4280   switch (keyword)
4281     {
4282     case RID_DYNCAST:
4283     case RID_STATCAST:
4284     case RID_REINTCAST:
4285     case RID_CONSTCAST:
4286       {
4287         tree type;
4288         tree expression;
4289         const char *saved_message;
4290
4291         /* All of these can be handled in the same way from the point
4292            of view of parsing.  Begin by consuming the token
4293            identifying the cast.  */
4294         cp_lexer_consume_token (parser->lexer);
4295
4296         /* New types cannot be defined in the cast.  */
4297         saved_message = parser->type_definition_forbidden_message;
4298         parser->type_definition_forbidden_message
4299           = "types may not be defined in casts";
4300
4301         /* Look for the opening `<'.  */
4302         cp_parser_require (parser, CPP_LESS, "`<'");
4303         /* Parse the type to which we are casting.  */
4304         type = cp_parser_type_id (parser);
4305         /* Look for the closing `>'.  */
4306         cp_parser_require (parser, CPP_GREATER, "`>'");
4307         /* Restore the old message.  */
4308         parser->type_definition_forbidden_message = saved_message;
4309
4310         /* And the expression which is being cast.  */
4311         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4312         expression = cp_parser_expression (parser, /*cast_p=*/true);
4313         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4314
4315         /* Only type conversions to integral or enumeration types
4316            can be used in constant-expressions.  */
4317         if (!cast_valid_in_integral_constant_expression_p (type)
4318             && (cp_parser_non_integral_constant_expression
4319                 (parser,
4320                  "a cast to a type other than an integral or "
4321                  "enumeration type")))
4322           return error_mark_node;
4323
4324         switch (keyword)
4325           {
4326           case RID_DYNCAST:
4327             postfix_expression
4328               = build_dynamic_cast (type, expression);
4329             break;
4330           case RID_STATCAST:
4331             postfix_expression
4332               = build_static_cast (type, expression);
4333             break;
4334           case RID_REINTCAST:
4335             postfix_expression
4336               = build_reinterpret_cast (type, expression);
4337             break;
4338           case RID_CONSTCAST:
4339             postfix_expression
4340               = build_const_cast (type, expression);
4341             break;
4342           default:
4343             gcc_unreachable ();
4344           }
4345       }
4346       break;
4347
4348     case RID_TYPEID:
4349       {
4350         tree type;
4351         const char *saved_message;
4352         bool saved_in_type_id_in_expr_p;
4353
4354         /* Consume the `typeid' token.  */
4355         cp_lexer_consume_token (parser->lexer);
4356         /* Look for the `(' token.  */
4357         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4358         /* Types cannot be defined in a `typeid' expression.  */
4359         saved_message = parser->type_definition_forbidden_message;
4360         parser->type_definition_forbidden_message
4361           = "types may not be defined in a `typeid\' expression";
4362         /* We can't be sure yet whether we're looking at a type-id or an
4363            expression.  */
4364         cp_parser_parse_tentatively (parser);
4365         /* Try a type-id first.  */
4366         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4367         parser->in_type_id_in_expr_p = true;
4368         type = cp_parser_type_id (parser);
4369         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4370         /* Look for the `)' token.  Otherwise, we can't be sure that
4371            we're not looking at an expression: consider `typeid (int
4372            (3))', for example.  */
4373         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4374         /* If all went well, simply lookup the type-id.  */
4375         if (cp_parser_parse_definitely (parser))
4376           postfix_expression = get_typeid (type);
4377         /* Otherwise, fall back to the expression variant.  */
4378         else
4379           {
4380             tree expression;
4381
4382             /* Look for an expression.  */
4383             expression = cp_parser_expression (parser, /*cast_p=*/false);
4384             /* Compute its typeid.  */
4385             postfix_expression = build_typeid (expression);
4386             /* Look for the `)' token.  */
4387             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4388           }
4389         /* Restore the saved message.  */
4390         parser->type_definition_forbidden_message = saved_message;
4391         /* `typeid' may not appear in an integral constant expression.  */
4392         if (cp_parser_non_integral_constant_expression(parser,
4393                                                        "`typeid' operator"))
4394           return error_mark_node;
4395       }
4396       break;
4397
4398     case RID_TYPENAME:
4399       {
4400         tree type;
4401         /* The syntax permitted here is the same permitted for an
4402            elaborated-type-specifier.  */
4403         type = cp_parser_elaborated_type_specifier (parser,
4404                                                     /*is_friend=*/false,
4405                                                     /*is_declaration=*/false);
4406         postfix_expression = cp_parser_functional_cast (parser, type);
4407       }
4408       break;
4409
4410     default:
4411       {
4412         tree type;
4413
4414         /* If the next thing is a simple-type-specifier, we may be
4415            looking at a functional cast.  We could also be looking at
4416            an id-expression.  So, we try the functional cast, and if
4417            that doesn't work we fall back to the primary-expression.  */
4418         cp_parser_parse_tentatively (parser);
4419         /* Look for the simple-type-specifier.  */
4420         type = cp_parser_simple_type_specifier (parser,
4421                                                 /*decl_specs=*/NULL,
4422                                                 CP_PARSER_FLAGS_NONE);
4423         /* Parse the cast itself.  */
4424         if (!cp_parser_error_occurred (parser))
4425           postfix_expression
4426             = cp_parser_functional_cast (parser, type);
4427         /* If that worked, we're done.  */
4428         if (cp_parser_parse_definitely (parser))
4429           break;
4430
4431         /* If the functional-cast didn't work out, try a
4432            compound-literal.  */
4433         if (cp_parser_allow_gnu_extensions_p (parser)
4434             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4435           {
4436             VEC(constructor_elt,gc) *initializer_list = NULL;
4437             bool saved_in_type_id_in_expr_p;
4438
4439             cp_parser_parse_tentatively (parser);
4440             /* Consume the `('.  */
4441             cp_lexer_consume_token (parser->lexer);
4442             /* Parse the type.  */
4443             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4444             parser->in_type_id_in_expr_p = true;
4445             type = cp_parser_type_id (parser);
4446             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4447             /* Look for the `)'.  */
4448             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4449             /* Look for the `{'.  */
4450             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4451             /* If things aren't going well, there's no need to
4452                keep going.  */
4453             if (!cp_parser_error_occurred (parser))
4454               {
4455                 bool non_constant_p;
4456                 /* Parse the initializer-list.  */
4457                 initializer_list
4458                   = cp_parser_initializer_list (parser, &non_constant_p);
4459                 /* Allow a trailing `,'.  */
4460                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4461                   cp_lexer_consume_token (parser->lexer);
4462                 /* Look for the final `}'.  */
4463                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4464               }
4465             /* If that worked, we're definitely looking at a
4466                compound-literal expression.  */
4467             if (cp_parser_parse_definitely (parser))
4468               {
4469                 /* Warn the user that a compound literal is not
4470                    allowed in standard C++.  */
4471                 if (pedantic)
4472                   pedwarn ("ISO C++ forbids compound-literals");
4473                 /* For simplicity, we disallow compound literals in
4474                    constant-expressions.  We could
4475                    allow compound literals of integer type, whose
4476                    initializer was a constant, in constant
4477                    expressions.  Permitting that usage, as a further
4478                    extension, would not change the meaning of any
4479                    currently accepted programs.  (Of course, as
4480                    compound literals are not part of ISO C++, the
4481                    standard has nothing to say.)  */
4482                 if (cp_parser_non_integral_constant_expression 
4483                     (parser, "non-constant compound literals"))
4484                   {
4485                     postfix_expression = error_mark_node;
4486                     break;
4487                   }
4488                 /* Form the representation of the compound-literal.  */
4489                 postfix_expression
4490                   = finish_compound_literal (type, initializer_list);
4491                 break;
4492               }
4493           }
4494
4495         /* It must be a primary-expression.  */
4496         postfix_expression
4497           = cp_parser_primary_expression (parser, address_p, cast_p,
4498                                           /*template_arg_p=*/false,
4499                                           &idk);
4500       }
4501       break;
4502     }
4503
4504   /* Keep looping until the postfix-expression is complete.  */
4505   while (true)
4506     {
4507       if (idk == CP_ID_KIND_UNQUALIFIED
4508           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4509           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4510         /* It is not a Koenig lookup function call.  */
4511         postfix_expression
4512           = unqualified_name_lookup_error (postfix_expression);
4513
4514       /* Peek at the next token.  */
4515       token = cp_lexer_peek_token (parser->lexer);
4516
4517       switch (token->type)
4518         {
4519         case CPP_OPEN_SQUARE:
4520           postfix_expression
4521             = cp_parser_postfix_open_square_expression (parser,
4522                                                         postfix_expression,
4523                                                         false);
4524           idk = CP_ID_KIND_NONE;
4525           is_member_access = false;
4526           break;
4527
4528         case CPP_OPEN_PAREN:
4529           /* postfix-expression ( expression-list [opt] ) */
4530           {
4531             bool koenig_p;
4532             bool is_builtin_constant_p;
4533             bool saved_integral_constant_expression_p = false;
4534             bool saved_non_integral_constant_expression_p = false;
4535             tree args;
4536
4537             is_member_access = false;
4538
4539             is_builtin_constant_p
4540               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4541             if (is_builtin_constant_p)
4542               {
4543                 /* The whole point of __builtin_constant_p is to allow
4544                    non-constant expressions to appear as arguments.  */
4545                 saved_integral_constant_expression_p
4546                   = parser->integral_constant_expression_p;
4547                 saved_non_integral_constant_expression_p
4548                   = parser->non_integral_constant_expression_p;
4549                 parser->integral_constant_expression_p = false;
4550               }
4551             args = (cp_parser_parenthesized_expression_list
4552                     (parser, /*is_attribute_list=*/false,
4553                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4554                      /*non_constant_p=*/NULL));
4555             if (is_builtin_constant_p)
4556               {
4557                 parser->integral_constant_expression_p
4558                   = saved_integral_constant_expression_p;
4559                 parser->non_integral_constant_expression_p
4560                   = saved_non_integral_constant_expression_p;
4561               }
4562
4563             if (args == error_mark_node)
4564               {
4565                 postfix_expression = error_mark_node;
4566                 break;
4567               }
4568
4569             /* Function calls are not permitted in
4570                constant-expressions.  */
4571             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4572                 && cp_parser_non_integral_constant_expression (parser,
4573                                                                "a function call"))
4574               {
4575                 postfix_expression = error_mark_node;
4576                 break;
4577               }
4578
4579             koenig_p = false;
4580             if (idk == CP_ID_KIND_UNQUALIFIED)
4581               {
4582                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4583                   {
4584                     if (args)
4585                       {
4586                         koenig_p = true;
4587                         postfix_expression
4588                           = perform_koenig_lookup (postfix_expression, args);
4589                       }
4590                     else
4591                       postfix_expression
4592                         = unqualified_fn_lookup_error (postfix_expression);
4593                   }
4594                 /* We do not perform argument-dependent lookup if
4595                    normal lookup finds a non-function, in accordance
4596                    with the expected resolution of DR 218.  */
4597                 else if (args && is_overloaded_fn (postfix_expression))
4598                   {
4599                     tree fn = get_first_fn (postfix_expression);
4600
4601                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4602                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4603
4604                     /* Only do argument dependent lookup if regular
4605                        lookup does not find a set of member functions.
4606                        [basic.lookup.koenig]/2a  */
4607                     if (!DECL_FUNCTION_MEMBER_P (fn))
4608                       {
4609                         koenig_p = true;
4610                         postfix_expression
4611                           = perform_koenig_lookup (postfix_expression, args);
4612                       }
4613                   }
4614               }
4615
4616             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4617               {
4618                 tree instance = TREE_OPERAND (postfix_expression, 0);
4619                 tree fn = TREE_OPERAND (postfix_expression, 1);
4620
4621                 if (processing_template_decl
4622                     && (type_dependent_expression_p (instance)
4623                         || (!BASELINK_P (fn)
4624                             && TREE_CODE (fn) != FIELD_DECL)
4625                         || type_dependent_expression_p (fn)
4626                         || any_type_dependent_arguments_p (args)))
4627                   {
4628                     postfix_expression
4629                       = build_nt_call_list (postfix_expression, args);
4630                     break;
4631                   }
4632
4633                 if (BASELINK_P (fn))
4634                   postfix_expression
4635                     = (build_new_method_call
4636                        (instance, fn, args, NULL_TREE,
4637                         (idk == CP_ID_KIND_QUALIFIED
4638                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4639                         /*fn_p=*/NULL));
4640                 else
4641                   postfix_expression
4642                     = finish_call_expr (postfix_expression, args,
4643                                         /*disallow_virtual=*/false,
4644                                         /*koenig_p=*/false);
4645               }
4646             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4647                      || TREE_CODE (postfix_expression) == MEMBER_REF
4648                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4649               postfix_expression = (build_offset_ref_call_from_tree
4650                                     (postfix_expression, args));
4651             else if (idk == CP_ID_KIND_QUALIFIED)
4652               /* A call to a static class member, or a namespace-scope
4653                  function.  */
4654               postfix_expression
4655                 = finish_call_expr (postfix_expression, args,
4656                                     /*disallow_virtual=*/true,
4657                                     koenig_p);
4658             else
4659               /* All other function calls.  */
4660               postfix_expression
4661                 = finish_call_expr (postfix_expression, args,
4662                                     /*disallow_virtual=*/false,
4663                                     koenig_p);
4664
4665             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4666             idk = CP_ID_KIND_NONE;
4667           }
4668           break;
4669
4670         case CPP_DOT:
4671         case CPP_DEREF:
4672           /* postfix-expression . template [opt] id-expression
4673              postfix-expression . pseudo-destructor-name
4674              postfix-expression -> template [opt] id-expression
4675              postfix-expression -> pseudo-destructor-name */
4676
4677           /* Consume the `.' or `->' operator.  */
4678           cp_lexer_consume_token (parser->lexer);
4679
4680           postfix_expression
4681             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4682                                                       postfix_expression,
4683                                                       false, &idk);
4684
4685           is_member_access = true;
4686           break;
4687
4688         case CPP_PLUS_PLUS:
4689           /* postfix-expression ++  */
4690           /* Consume the `++' token.  */
4691           cp_lexer_consume_token (parser->lexer);
4692           /* Generate a representation for the complete expression.  */
4693           postfix_expression
4694             = finish_increment_expr (postfix_expression,
4695                                      POSTINCREMENT_EXPR);
4696           /* Increments may not appear in constant-expressions.  */
4697           if (cp_parser_non_integral_constant_expression (parser,
4698                                                           "an increment"))
4699             postfix_expression = error_mark_node;
4700           idk = CP_ID_KIND_NONE;
4701           is_member_access = false;
4702           break;
4703
4704         case CPP_MINUS_MINUS:
4705           /* postfix-expression -- */
4706           /* Consume the `--' token.  */
4707           cp_lexer_consume_token (parser->lexer);
4708           /* Generate a representation for the complete expression.  */
4709           postfix_expression
4710             = finish_increment_expr (postfix_expression,
4711                                      POSTDECREMENT_EXPR);
4712           /* Decrements may not appear in constant-expressions.  */
4713           if (cp_parser_non_integral_constant_expression (parser,
4714                                                           "a decrement"))
4715             postfix_expression = error_mark_node;
4716           idk = CP_ID_KIND_NONE;
4717           is_member_access = false;
4718           break;
4719
4720         default:
4721           if (member_access_only_p)
4722             return is_member_access? postfix_expression : error_mark_node;
4723           else
4724             return postfix_expression;
4725         }
4726     }
4727
4728   /* We should never get here.  */
4729   gcc_unreachable ();
4730   return error_mark_node;
4731 }
4732
4733 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4734    by cp_parser_builtin_offsetof.  We're looking for
4735
4736      postfix-expression [ expression ]
4737
4738    FOR_OFFSETOF is set if we're being called in that context, which
4739    changes how we deal with integer constant expressions.  */
4740
4741 static tree
4742 cp_parser_postfix_open_square_expression (cp_parser *parser,
4743                                           tree postfix_expression,
4744                                           bool for_offsetof)
4745 {
4746   tree index;
4747
4748   /* Consume the `[' token.  */
4749   cp_lexer_consume_token (parser->lexer);
4750
4751   /* Parse the index expression.  */
4752   /* ??? For offsetof, there is a question of what to allow here.  If
4753      offsetof is not being used in an integral constant expression context,
4754      then we *could* get the right answer by computing the value at runtime.
4755      If we are in an integral constant expression context, then we might
4756      could accept any constant expression; hard to say without analysis.
4757      Rather than open the barn door too wide right away, allow only integer
4758      constant expressions here.  */
4759   if (for_offsetof)
4760     index = cp_parser_constant_expression (parser, false, NULL);
4761   else
4762     index = cp_parser_expression (parser, /*cast_p=*/false);
4763
4764   /* Look for the closing `]'.  */
4765   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4766
4767   /* Build the ARRAY_REF.  */
4768   postfix_expression = grok_array_decl (postfix_expression, index);
4769
4770   /* When not doing offsetof, array references are not permitted in
4771      constant-expressions.  */
4772   if (!for_offsetof
4773       && (cp_parser_non_integral_constant_expression
4774           (parser, "an array reference")))
4775     postfix_expression = error_mark_node;
4776
4777   return postfix_expression;
4778 }
4779
4780 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4781    by cp_parser_builtin_offsetof.  We're looking for
4782
4783      postfix-expression . template [opt] id-expression
4784      postfix-expression . pseudo-destructor-name
4785      postfix-expression -> template [opt] id-expression
4786      postfix-expression -> pseudo-destructor-name
4787
4788    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4789    limits what of the above we'll actually accept, but nevermind.
4790    TOKEN_TYPE is the "." or "->" token, which will already have been
4791    removed from the stream.  */
4792
4793 static tree
4794 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4795                                         enum cpp_ttype token_type,
4796                                         tree postfix_expression,
4797                                         bool for_offsetof, cp_id_kind *idk)
4798 {
4799   tree name;
4800   bool dependent_p;
4801   bool pseudo_destructor_p;
4802   tree scope = NULL_TREE;
4803
4804   /* If this is a `->' operator, dereference the pointer.  */
4805   if (token_type == CPP_DEREF)
4806     postfix_expression = build_x_arrow (postfix_expression);
4807   /* Check to see whether or not the expression is type-dependent.  */
4808   dependent_p = type_dependent_expression_p (postfix_expression);
4809   /* The identifier following the `->' or `.' is not qualified.  */
4810   parser->scope = NULL_TREE;
4811   parser->qualifying_scope = NULL_TREE;
4812   parser->object_scope = NULL_TREE;
4813   *idk = CP_ID_KIND_NONE;
4814   /* Enter the scope corresponding to the type of the object
4815      given by the POSTFIX_EXPRESSION.  */
4816   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4817     {
4818       scope = TREE_TYPE (postfix_expression);
4819       /* According to the standard, no expression should ever have
4820          reference type.  Unfortunately, we do not currently match
4821          the standard in this respect in that our internal representation
4822          of an expression may have reference type even when the standard
4823          says it does not.  Therefore, we have to manually obtain the
4824          underlying type here.  */
4825       scope = non_reference (scope);
4826       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4827       if (scope == unknown_type_node)
4828         {
4829           error ("%qE does not have class type", postfix_expression);
4830           scope = NULL_TREE;
4831         }
4832       else
4833         scope = complete_type_or_else (scope, NULL_TREE);
4834       /* Let the name lookup machinery know that we are processing a
4835          class member access expression.  */
4836       parser->context->object_type = scope;
4837       /* If something went wrong, we want to be able to discern that case,
4838          as opposed to the case where there was no SCOPE due to the type
4839          of expression being dependent.  */
4840       if (!scope)
4841         scope = error_mark_node;
4842       /* If the SCOPE was erroneous, make the various semantic analysis
4843          functions exit quickly -- and without issuing additional error
4844          messages.  */
4845       if (scope == error_mark_node)
4846         postfix_expression = error_mark_node;
4847     }
4848
4849   /* Assume this expression is not a pseudo-destructor access.  */
4850   pseudo_destructor_p = false;
4851
4852   /* If the SCOPE is a scalar type, then, if this is a valid program,
4853      we must be looking at a pseudo-destructor-name.  */
4854   if (scope && SCALAR_TYPE_P (scope))
4855     {
4856       tree s;
4857       tree type;
4858
4859       cp_parser_parse_tentatively (parser);
4860       /* Parse the pseudo-destructor-name.  */
4861       s = NULL_TREE;
4862       cp_parser_pseudo_destructor_name (parser, &s, &type);
4863       if (cp_parser_parse_definitely (parser))
4864         {
4865           pseudo_destructor_p = true;
4866           postfix_expression
4867             = finish_pseudo_destructor_expr (postfix_expression,
4868                                              s, TREE_TYPE (type));
4869         }
4870     }
4871
4872   if (!pseudo_destructor_p)
4873     {
4874       /* If the SCOPE is not a scalar type, we are looking at an
4875          ordinary class member access expression, rather than a
4876          pseudo-destructor-name.  */
4877       bool template_p;
4878       /* Parse the id-expression.  */
4879       name = (cp_parser_id_expression
4880               (parser,
4881                cp_parser_optional_template_keyword (parser),
4882                /*check_dependency_p=*/true,
4883                &template_p,
4884                /*declarator_p=*/false,
4885                /*optional_p=*/false));
4886       /* In general, build a SCOPE_REF if the member name is qualified.
4887          However, if the name was not dependent and has already been
4888          resolved; there is no need to build the SCOPE_REF.  For example;
4889
4890              struct X { void f(); };
4891              template <typename T> void f(T* t) { t->X::f(); }
4892
4893          Even though "t" is dependent, "X::f" is not and has been resolved
4894          to a BASELINK; there is no need to include scope information.  */
4895
4896       /* But we do need to remember that there was an explicit scope for
4897          virtual function calls.  */
4898       if (parser->scope)
4899         *idk = CP_ID_KIND_QUALIFIED;
4900
4901       /* If the name is a template-id that names a type, we will get a
4902          TYPE_DECL here.  That is invalid code.  */
4903       if (TREE_CODE (name) == TYPE_DECL)
4904         {
4905           error ("invalid use of %qD", name);
4906           postfix_expression = error_mark_node;
4907         }
4908       else
4909         {
4910           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4911             {
4912               name = build_qualified_name (/*type=*/NULL_TREE,
4913                                            parser->scope,
4914                                            name,
4915                                            template_p);
4916               parser->scope = NULL_TREE;
4917               parser->qualifying_scope = NULL_TREE;
4918               parser->object_scope = NULL_TREE;
4919             }
4920           if (scope && name && BASELINK_P (name))
4921             adjust_result_of_qualified_name_lookup
4922               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4923           postfix_expression
4924             = finish_class_member_access_expr (postfix_expression, name,
4925                                                template_p);
4926         }
4927     }
4928
4929   /* We no longer need to look up names in the scope of the object on
4930      the left-hand side of the `.' or `->' operator.  */
4931   parser->context->object_type = NULL_TREE;
4932
4933   /* Outside of offsetof, these operators may not appear in
4934      constant-expressions.  */
4935   if (!for_offsetof
4936       && (cp_parser_non_integral_constant_expression
4937           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4938     postfix_expression = error_mark_node;
4939
4940   return postfix_expression;
4941 }
4942
4943 /* Parse a parenthesized expression-list.
4944
4945    expression-list:
4946      assignment-expression
4947      expression-list, assignment-expression
4948
4949    attribute-list:
4950      expression-list
4951      identifier
4952      identifier, expression-list
4953
4954    CAST_P is true if this expression is the target of a cast.
4955
4956    ALLOW_EXPANSION_P is true if this expression allows expansion of an
4957    argument pack.
4958
4959    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4960    representation of an assignment-expression.  Note that a TREE_LIST
4961    is returned even if there is only a single expression in the list.
4962    error_mark_node is returned if the ( and or ) are
4963    missing. NULL_TREE is returned on no expressions. The parentheses
4964    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4965    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4966    indicates whether or not all of the expressions in the list were
4967    constant.  */
4968
4969 static tree
4970 cp_parser_parenthesized_expression_list (cp_parser* parser,
4971                                          bool is_attribute_list,
4972                                          bool cast_p,
4973                                          bool allow_expansion_p,
4974                                          bool *non_constant_p)
4975 {
4976   tree expression_list = NULL_TREE;
4977   bool fold_expr_p = is_attribute_list;
4978   tree identifier = NULL_TREE;
4979
4980   /* Assume all the expressions will be constant.  */
4981   if (non_constant_p)
4982     *non_constant_p = false;
4983
4984   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4985     return error_mark_node;
4986
4987   /* Consume expressions until there are no more.  */
4988   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4989     while (true)
4990       {
4991         tree expr;
4992
4993         /* At the beginning of attribute lists, check to see if the
4994            next token is an identifier.  */
4995         if (is_attribute_list
4996             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4997           {
4998             cp_token *token;
4999
5000             /* Consume the identifier.  */
5001             token = cp_lexer_consume_token (parser->lexer);
5002             /* Save the identifier.  */
5003             identifier = token->u.value;
5004           }
5005         else
5006           {
5007             /* Parse the next assignment-expression.  */
5008             if (non_constant_p)
5009               {
5010                 bool expr_non_constant_p;
5011                 expr = (cp_parser_constant_expression
5012                         (parser, /*allow_non_constant_p=*/true,
5013                          &expr_non_constant_p));
5014                 if (expr_non_constant_p)
5015                   *non_constant_p = true;
5016               }
5017             else
5018               expr = cp_parser_assignment_expression (parser, cast_p);
5019
5020             if (fold_expr_p)
5021               expr = fold_non_dependent_expr (expr);
5022
5023             /* If we have an ellipsis, then this is an expression
5024                expansion.  */
5025             if (allow_expansion_p
5026                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5027               {
5028                 /* Consume the `...'.  */
5029                 cp_lexer_consume_token (parser->lexer);
5030
5031                 /* Build the argument pack.  */
5032                 expr = make_pack_expansion (expr);
5033               }
5034
5035              /* Add it to the list.  We add error_mark_node
5036                 expressions to the list, so that we can still tell if
5037                 the correct form for a parenthesized expression-list
5038                 is found. That gives better errors.  */
5039             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5040
5041             if (expr == error_mark_node)
5042               goto skip_comma;
5043           }
5044
5045         /* After the first item, attribute lists look the same as
5046            expression lists.  */
5047         is_attribute_list = false;
5048
5049       get_comma:;
5050         /* If the next token isn't a `,', then we are done.  */
5051         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5052           break;
5053
5054         /* Otherwise, consume the `,' and keep going.  */
5055         cp_lexer_consume_token (parser->lexer);
5056       }
5057
5058   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5059     {
5060       int ending;
5061
5062     skip_comma:;
5063       /* We try and resync to an unnested comma, as that will give the
5064          user better diagnostics.  */
5065       ending = cp_parser_skip_to_closing_parenthesis (parser,
5066                                                       /*recovering=*/true,
5067                                                       /*or_comma=*/true,
5068                                                       /*consume_paren=*/true);
5069       if (ending < 0)
5070         goto get_comma;
5071       if (!ending)
5072         return error_mark_node;
5073     }
5074
5075   /* We built up the list in reverse order so we must reverse it now.  */
5076   expression_list = nreverse (expression_list);
5077   if (identifier)
5078     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5079
5080   return expression_list;
5081 }
5082
5083 /* Parse a pseudo-destructor-name.
5084
5085    pseudo-destructor-name:
5086      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5087      :: [opt] nested-name-specifier template template-id :: ~ type-name
5088      :: [opt] nested-name-specifier [opt] ~ type-name
5089
5090    If either of the first two productions is used, sets *SCOPE to the
5091    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5092    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5093    or ERROR_MARK_NODE if the parse fails.  */
5094
5095 static void
5096 cp_parser_pseudo_destructor_name (cp_parser* parser,
5097                                   tree* scope,
5098                                   tree* type)
5099 {
5100   bool nested_name_specifier_p;
5101
5102   /* Assume that things will not work out.  */
5103   *type = error_mark_node;
5104
5105   /* Look for the optional `::' operator.  */
5106   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5107   /* Look for the optional nested-name-specifier.  */
5108   nested_name_specifier_p
5109     = (cp_parser_nested_name_specifier_opt (parser,
5110                                             /*typename_keyword_p=*/false,
5111                                             /*check_dependency_p=*/true,
5112                                             /*type_p=*/false,
5113                                             /*is_declaration=*/true)
5114        != NULL_TREE);
5115   /* Now, if we saw a nested-name-specifier, we might be doing the
5116      second production.  */
5117   if (nested_name_specifier_p
5118       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5119     {
5120       /* Consume the `template' keyword.  */
5121       cp_lexer_consume_token (parser->lexer);
5122       /* Parse the template-id.  */
5123       cp_parser_template_id (parser,
5124                              /*template_keyword_p=*/true,
5125                              /*check_dependency_p=*/false,
5126                              /*is_declaration=*/true);
5127       /* Look for the `::' token.  */
5128       cp_parser_require (parser, CPP_SCOPE, "`::'");
5129     }
5130   /* If the next token is not a `~', then there might be some
5131      additional qualification.  */
5132   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5133     {
5134       /* Look for the type-name.  */
5135       *scope = TREE_TYPE (cp_parser_type_name (parser));
5136
5137       if (*scope == error_mark_node)
5138         return;
5139
5140       /* If we don't have ::~, then something has gone wrong.  Since
5141          the only caller of this function is looking for something
5142          after `.' or `->' after a scalar type, most likely the
5143          program is trying to get a member of a non-aggregate
5144          type.  */
5145       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5146           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5147         {
5148           cp_parser_error (parser, "request for member of non-aggregate type");
5149           return;
5150         }
5151
5152       /* Look for the `::' token.  */
5153       cp_parser_require (parser, CPP_SCOPE, "`::'");
5154     }
5155   else
5156     *scope = NULL_TREE;
5157
5158   /* Look for the `~'.  */
5159   cp_parser_require (parser, CPP_COMPL, "`~'");
5160   /* Look for the type-name again.  We are not responsible for
5161      checking that it matches the first type-name.  */
5162   *type = cp_parser_type_name (parser);
5163 }
5164
5165 /* Parse a unary-expression.
5166
5167    unary-expression:
5168      postfix-expression
5169      ++ cast-expression
5170      -- cast-expression
5171      unary-operator cast-expression
5172      sizeof unary-expression
5173      sizeof ( type-id )
5174      new-expression
5175      delete-expression
5176
5177    GNU Extensions:
5178
5179    unary-expression:
5180      __extension__ cast-expression
5181      __alignof__ unary-expression
5182      __alignof__ ( type-id )
5183      __real__ cast-expression
5184      __imag__ cast-expression
5185      && identifier
5186
5187    ADDRESS_P is true iff the unary-expression is appearing as the
5188    operand of the `&' operator.   CAST_P is true if this expression is
5189    the target of a cast.
5190
5191    Returns a representation of the expression.  */
5192
5193 static tree
5194 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5195 {
5196   cp_token *token;
5197   enum tree_code unary_operator;
5198
5199   /* Peek at the next token.  */
5200   token = cp_lexer_peek_token (parser->lexer);
5201   /* Some keywords give away the kind of expression.  */
5202   if (token->type == CPP_KEYWORD)
5203     {
5204       enum rid keyword = token->keyword;
5205
5206       switch (keyword)
5207         {
5208         case RID_ALIGNOF:
5209         case RID_SIZEOF:
5210           {
5211             tree operand;
5212             enum tree_code op;
5213
5214             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5215             /* Consume the token.  */
5216             cp_lexer_consume_token (parser->lexer);
5217             /* Parse the operand.  */
5218             operand = cp_parser_sizeof_operand (parser, keyword);
5219
5220             if (TYPE_P (operand))
5221               return cxx_sizeof_or_alignof_type (operand, op, true);
5222             else
5223               return cxx_sizeof_or_alignof_expr (operand, op);
5224           }
5225
5226         case RID_NEW:
5227           return cp_parser_new_expression (parser);
5228
5229         case RID_DELETE:
5230           return cp_parser_delete_expression (parser);
5231
5232         case RID_EXTENSION:
5233           {
5234             /* The saved value of the PEDANTIC flag.  */
5235             int saved_pedantic;
5236             tree expr;
5237
5238             /* Save away the PEDANTIC flag.  */
5239             cp_parser_extension_opt (parser, &saved_pedantic);
5240             /* Parse the cast-expression.  */
5241             expr = cp_parser_simple_cast_expression (parser);
5242             /* Restore the PEDANTIC flag.  */
5243             pedantic = saved_pedantic;
5244
5245             return expr;
5246           }
5247
5248         case RID_REALPART:
5249         case RID_IMAGPART:
5250           {
5251             tree expression;
5252
5253             /* Consume the `__real__' or `__imag__' token.  */
5254             cp_lexer_consume_token (parser->lexer);
5255             /* Parse the cast-expression.  */
5256             expression = cp_parser_simple_cast_expression (parser);
5257             /* Create the complete representation.  */
5258             return build_x_unary_op ((keyword == RID_REALPART
5259                                       ? REALPART_EXPR : IMAGPART_EXPR),
5260                                      expression);
5261           }
5262           break;
5263
5264         default:
5265           break;
5266         }
5267     }
5268
5269   /* Look for the `:: new' and `:: delete', which also signal the
5270      beginning of a new-expression, or delete-expression,
5271      respectively.  If the next token is `::', then it might be one of
5272      these.  */
5273   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5274     {
5275       enum rid keyword;
5276
5277       /* See if the token after the `::' is one of the keywords in
5278          which we're interested.  */
5279       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5280       /* If it's `new', we have a new-expression.  */
5281       if (keyword == RID_NEW)
5282         return cp_parser_new_expression (parser);
5283       /* Similarly, for `delete'.  */
5284       else if (keyword == RID_DELETE)
5285         return cp_parser_delete_expression (parser);
5286     }
5287
5288   /* Look for a unary operator.  */
5289   unary_operator = cp_parser_unary_operator (token);
5290   /* The `++' and `--' operators can be handled similarly, even though
5291      they are not technically unary-operators in the grammar.  */
5292   if (unary_operator == ERROR_MARK)
5293     {
5294       if (token->type == CPP_PLUS_PLUS)
5295         unary_operator = PREINCREMENT_EXPR;
5296       else if (token->type == CPP_MINUS_MINUS)
5297         unary_operator = PREDECREMENT_EXPR;
5298       /* Handle the GNU address-of-label extension.  */
5299       else if (cp_parser_allow_gnu_extensions_p (parser)
5300                && token->type == CPP_AND_AND)
5301         {
5302           tree identifier;
5303
5304           /* Consume the '&&' token.  */
5305           cp_lexer_consume_token (parser->lexer);
5306           /* Look for the identifier.  */
5307           identifier = cp_parser_identifier (parser);
5308           /* Create an expression representing the address.  */
5309           return finish_label_address_expr (identifier);
5310         }
5311     }
5312   if (unary_operator != ERROR_MARK)
5313     {
5314       tree cast_expression;
5315       tree expression = error_mark_node;
5316       const char *non_constant_p = NULL;
5317
5318       /* Consume the operator token.  */
5319       token = cp_lexer_consume_token (parser->lexer);
5320       /* Parse the cast-expression.  */
5321       cast_expression
5322         = cp_parser_cast_expression (parser,
5323                                      unary_operator == ADDR_EXPR,
5324                                      /*cast_p=*/false);
5325       /* Now, build an appropriate representation.  */
5326       switch (unary_operator)
5327         {
5328         case INDIRECT_REF:
5329           non_constant_p = "`*'";
5330           expression = build_x_indirect_ref (cast_expression, "unary *");
5331           break;
5332
5333         case ADDR_EXPR:
5334           non_constant_p = "`&'";
5335           /* Fall through.  */
5336         case BIT_NOT_EXPR:
5337           expression = build_x_unary_op (unary_operator, cast_expression);
5338           break;
5339
5340         case PREINCREMENT_EXPR:
5341         case PREDECREMENT_EXPR:
5342           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5343                             ? "`++'" : "`--'");
5344           /* Fall through.  */
5345         case UNARY_PLUS_EXPR:
5346         case NEGATE_EXPR:
5347         case TRUTH_NOT_EXPR:
5348           expression = finish_unary_op_expr (unary_operator, cast_expression);
5349           break;
5350
5351         default:
5352           gcc_unreachable ();
5353         }
5354
5355       if (non_constant_p
5356           && cp_parser_non_integral_constant_expression (parser,
5357                                                          non_constant_p))
5358         expression = error_mark_node;
5359
5360       return expression;
5361     }
5362
5363   return cp_parser_postfix_expression (parser, address_p, cast_p,
5364                                        /*member_access_only_p=*/false);
5365 }
5366
5367 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5368    unary-operator, the corresponding tree code is returned.  */
5369
5370 static enum tree_code
5371 cp_parser_unary_operator (cp_token* token)
5372 {
5373   switch (token->type)
5374     {
5375     case CPP_MULT:
5376       return INDIRECT_REF;
5377
5378     case CPP_AND:
5379       return ADDR_EXPR;
5380
5381     case CPP_PLUS:
5382       return UNARY_PLUS_EXPR;
5383
5384     case CPP_MINUS:
5385       return NEGATE_EXPR;
5386
5387     case CPP_NOT:
5388       return TRUTH_NOT_EXPR;
5389
5390     case CPP_COMPL:
5391       return BIT_NOT_EXPR;
5392
5393     default:
5394       return ERROR_MARK;
5395     }
5396 }
5397
5398 /* Parse a new-expression.
5399
5400    new-expression:
5401      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5402      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5403
5404    Returns a representation of the expression.  */
5405
5406 static tree
5407 cp_parser_new_expression (cp_parser* parser)
5408 {
5409   bool global_scope_p;
5410   tree placement;
5411   tree type;
5412   tree initializer;
5413   tree nelts;
5414
5415   /* Look for the optional `::' operator.  */
5416   global_scope_p
5417     = (cp_parser_global_scope_opt (parser,
5418                                    /*current_scope_valid_p=*/false)
5419        != NULL_TREE);
5420   /* Look for the `new' operator.  */
5421   cp_parser_require_keyword (parser, RID_NEW, "`new'");
5422   /* There's no easy way to tell a new-placement from the
5423      `( type-id )' construct.  */
5424   cp_parser_parse_tentatively (parser);
5425   /* Look for a new-placement.  */
5426   placement = cp_parser_new_placement (parser);
5427   /* If that didn't work out, there's no new-placement.  */
5428   if (!cp_parser_parse_definitely (parser))
5429     placement = NULL_TREE;
5430
5431   /* If the next token is a `(', then we have a parenthesized
5432      type-id.  */
5433   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5434     {
5435       /* Consume the `('.  */
5436       cp_lexer_consume_token (parser->lexer);
5437       /* Parse the type-id.  */
5438       type = cp_parser_type_id (parser);
5439       /* Look for the closing `)'.  */
5440       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5441       /* There should not be a direct-new-declarator in this production,
5442          but GCC used to allowed this, so we check and emit a sensible error
5443          message for this case.  */
5444       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5445         {
5446           error ("array bound forbidden after parenthesized type-id");
5447           inform ("try removing the parentheses around the type-id");
5448           cp_parser_direct_new_declarator (parser);
5449         }
5450       nelts = NULL_TREE;
5451     }
5452   /* Otherwise, there must be a new-type-id.  */
5453   else
5454     type = cp_parser_new_type_id (parser, &nelts);
5455
5456   /* If the next token is a `(', then we have a new-initializer.  */
5457   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5458     initializer = cp_parser_new_initializer (parser);
5459   else
5460     initializer = NULL_TREE;
5461
5462   /* A new-expression may not appear in an integral constant
5463      expression.  */
5464   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5465     return error_mark_node;
5466
5467   /* Create a representation of the new-expression.  */
5468   return build_new (placement, type, nelts, initializer, global_scope_p);
5469 }
5470
5471 /* Parse a new-placement.
5472
5473    new-placement:
5474      ( expression-list )
5475
5476    Returns the same representation as for an expression-list.  */
5477
5478 static tree
5479 cp_parser_new_placement (cp_parser* parser)
5480 {
5481   tree expression_list;
5482
5483   /* Parse the expression-list.  */
5484   expression_list = (cp_parser_parenthesized_expression_list
5485                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5486                       /*non_constant_p=*/NULL));
5487
5488   return expression_list;
5489 }
5490
5491 /* Parse a new-type-id.
5492
5493    new-type-id:
5494      type-specifier-seq new-declarator [opt]
5495
5496    Returns the TYPE allocated.  If the new-type-id indicates an array
5497    type, *NELTS is set to the number of elements in the last array
5498    bound; the TYPE will not include the last array bound.  */
5499
5500 static tree
5501 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5502 {
5503   cp_decl_specifier_seq type_specifier_seq;
5504   cp_declarator *new_declarator;
5505   cp_declarator *declarator;
5506   cp_declarator *outer_declarator;
5507   const char *saved_message;
5508   tree type;
5509
5510   /* The type-specifier sequence must not contain type definitions.
5511      (It cannot contain declarations of new types either, but if they
5512      are not definitions we will catch that because they are not
5513      complete.)  */
5514   saved_message = parser->type_definition_forbidden_message;
5515   parser->type_definition_forbidden_message
5516     = "types may not be defined in a new-type-id";
5517   /* Parse the type-specifier-seq.  */
5518   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5519                                 &type_specifier_seq);
5520   /* Restore the old message.  */
5521   parser->type_definition_forbidden_message = saved_message;
5522   /* Parse the new-declarator.  */
5523   new_declarator = cp_parser_new_declarator_opt (parser);
5524
5525   /* Determine the number of elements in the last array dimension, if
5526      any.  */
5527   *nelts = NULL_TREE;
5528   /* Skip down to the last array dimension.  */
5529   declarator = new_declarator;
5530   outer_declarator = NULL;
5531   while (declarator && (declarator->kind == cdk_pointer
5532                         || declarator->kind == cdk_ptrmem))
5533     {
5534       outer_declarator = declarator;
5535       declarator = declarator->declarator;
5536     }
5537   while (declarator
5538          && declarator->kind == cdk_array
5539          && declarator->declarator
5540          && declarator->declarator->kind == cdk_array)
5541     {
5542       outer_declarator = declarator;
5543       declarator = declarator->declarator;
5544     }
5545
5546   if (declarator && declarator->kind == cdk_array)
5547     {
5548       *nelts = declarator->u.array.bounds;
5549       if (*nelts == error_mark_node)
5550         *nelts = integer_one_node;
5551
5552       if (outer_declarator)
5553         outer_declarator->declarator = declarator->declarator;
5554       else
5555         new_declarator = NULL;
5556     }
5557
5558   type = groktypename (&type_specifier_seq, new_declarator);
5559   return type;
5560 }
5561
5562 /* Parse an (optional) new-declarator.
5563
5564    new-declarator:
5565      ptr-operator new-declarator [opt]
5566      direct-new-declarator
5567
5568    Returns the declarator.  */
5569
5570 static cp_declarator *
5571 cp_parser_new_declarator_opt (cp_parser* parser)
5572 {
5573   enum tree_code code;
5574   tree type;
5575   cp_cv_quals cv_quals;
5576
5577   /* We don't know if there's a ptr-operator next, or not.  */
5578   cp_parser_parse_tentatively (parser);
5579   /* Look for a ptr-operator.  */
5580   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5581   /* If that worked, look for more new-declarators.  */
5582   if (cp_parser_parse_definitely (parser))
5583     {
5584       cp_declarator *declarator;
5585
5586       /* Parse another optional declarator.  */
5587       declarator = cp_parser_new_declarator_opt (parser);
5588
5589       return cp_parser_make_indirect_declarator
5590         (code, type, cv_quals, declarator);
5591     }
5592
5593   /* If the next token is a `[', there is a direct-new-declarator.  */
5594   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5595     return cp_parser_direct_new_declarator (parser);
5596
5597   return NULL;
5598 }
5599
5600 /* Parse a direct-new-declarator.
5601
5602    direct-new-declarator:
5603      [ expression ]
5604      direct-new-declarator [constant-expression]
5605
5606    */
5607
5608 static cp_declarator *
5609 cp_parser_direct_new_declarator (cp_parser* parser)
5610 {
5611   cp_declarator *declarator = NULL;
5612
5613   while (true)
5614     {
5615       tree expression;
5616
5617       /* Look for the opening `['.  */
5618       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5619       /* The first expression is not required to be constant.  */
5620       if (!declarator)
5621         {
5622           expression = cp_parser_expression (parser, /*cast_p=*/false);
5623           /* The standard requires that the expression have integral
5624              type.  DR 74 adds enumeration types.  We believe that the
5625              real intent is that these expressions be handled like the
5626              expression in a `switch' condition, which also allows
5627              classes with a single conversion to integral or
5628              enumeration type.  */
5629           if (!processing_template_decl)
5630             {
5631               expression
5632                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5633                                               expression,
5634                                               /*complain=*/true);
5635               if (!expression)
5636                 {
5637                   error ("expression in new-declarator must have integral "
5638                          "or enumeration type");
5639                   expression = error_mark_node;
5640                 }
5641             }
5642         }
5643       /* But all the other expressions must be.  */
5644       else
5645         expression
5646           = cp_parser_constant_expression (parser,
5647                                            /*allow_non_constant=*/false,
5648                                            NULL);
5649       /* Look for the closing `]'.  */
5650       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5651
5652       /* Add this bound to the declarator.  */
5653       declarator = make_array_declarator (declarator, expression);
5654
5655       /* If the next token is not a `[', then there are no more
5656          bounds.  */
5657       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5658         break;
5659     }
5660
5661   return declarator;
5662 }
5663
5664 /* Parse a new-initializer.
5665
5666    new-initializer:
5667      ( expression-list [opt] )
5668
5669    Returns a representation of the expression-list.  If there is no
5670    expression-list, VOID_ZERO_NODE is returned.  */
5671
5672 static tree
5673 cp_parser_new_initializer (cp_parser* parser)
5674 {
5675   tree expression_list;
5676
5677   expression_list = (cp_parser_parenthesized_expression_list
5678                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5679                       /*non_constant_p=*/NULL));
5680   if (!expression_list)
5681     expression_list = void_zero_node;
5682
5683   return expression_list;
5684 }
5685
5686 /* Parse a delete-expression.
5687
5688    delete-expression:
5689      :: [opt] delete cast-expression
5690      :: [opt] delete [ ] cast-expression
5691
5692    Returns a representation of the expression.  */
5693
5694 static tree
5695 cp_parser_delete_expression (cp_parser* parser)
5696 {
5697   bool global_scope_p;
5698   bool array_p;
5699   tree expression;
5700
5701   /* Look for the optional `::' operator.  */
5702   global_scope_p
5703     = (cp_parser_global_scope_opt (parser,
5704                                    /*current_scope_valid_p=*/false)
5705        != NULL_TREE);
5706   /* Look for the `delete' keyword.  */
5707   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5708   /* See if the array syntax is in use.  */
5709   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5710     {
5711       /* Consume the `[' token.  */
5712       cp_lexer_consume_token (parser->lexer);
5713       /* Look for the `]' token.  */
5714       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5715       /* Remember that this is the `[]' construct.  */
5716       array_p = true;
5717     }
5718   else
5719     array_p = false;
5720
5721   /* Parse the cast-expression.  */
5722   expression = cp_parser_simple_cast_expression (parser);
5723
5724   /* A delete-expression may not appear in an integral constant
5725      expression.  */
5726   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5727     return error_mark_node;
5728
5729   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5730 }
5731
5732 /* Parse a cast-expression.
5733
5734    cast-expression:
5735      unary-expression
5736      ( type-id ) cast-expression
5737
5738    ADDRESS_P is true iff the unary-expression is appearing as the
5739    operand of the `&' operator.   CAST_P is true if this expression is
5740    the target of a cast.
5741
5742    Returns a representation of the expression.  */
5743
5744 static tree
5745 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5746 {
5747   /* If it's a `(', then we might be looking at a cast.  */
5748   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5749     {
5750       tree type = NULL_TREE;
5751       tree expr = NULL_TREE;
5752       bool compound_literal_p;
5753       const char *saved_message;
5754
5755       /* There's no way to know yet whether or not this is a cast.
5756          For example, `(int (3))' is a unary-expression, while `(int)
5757          3' is a cast.  So, we resort to parsing tentatively.  */
5758       cp_parser_parse_tentatively (parser);
5759       /* Types may not be defined in a cast.  */
5760       saved_message = parser->type_definition_forbidden_message;
5761       parser->type_definition_forbidden_message
5762         = "types may not be defined in casts";
5763       /* Consume the `('.  */
5764       cp_lexer_consume_token (parser->lexer);
5765       /* A very tricky bit is that `(struct S) { 3 }' is a
5766          compound-literal (which we permit in C++ as an extension).
5767          But, that construct is not a cast-expression -- it is a
5768          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5769          is legal; if the compound-literal were a cast-expression,
5770          you'd need an extra set of parentheses.)  But, if we parse
5771          the type-id, and it happens to be a class-specifier, then we
5772          will commit to the parse at that point, because we cannot
5773          undo the action that is done when creating a new class.  So,
5774          then we cannot back up and do a postfix-expression.
5775
5776          Therefore, we scan ahead to the closing `)', and check to see
5777          if the token after the `)' is a `{'.  If so, we are not
5778          looking at a cast-expression.
5779
5780          Save tokens so that we can put them back.  */
5781       cp_lexer_save_tokens (parser->lexer);
5782       /* Skip tokens until the next token is a closing parenthesis.
5783          If we find the closing `)', and the next token is a `{', then
5784          we are looking at a compound-literal.  */
5785       compound_literal_p
5786         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5787                                                   /*consume_paren=*/true)
5788            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5789       /* Roll back the tokens we skipped.  */
5790       cp_lexer_rollback_tokens (parser->lexer);
5791       /* If we were looking at a compound-literal, simulate an error
5792          so that the call to cp_parser_parse_definitely below will
5793          fail.  */
5794       if (compound_literal_p)
5795         cp_parser_simulate_error (parser);
5796       else
5797         {
5798           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5799           parser->in_type_id_in_expr_p = true;
5800           /* Look for the type-id.  */
5801           type = cp_parser_type_id (parser);
5802           /* Look for the closing `)'.  */
5803           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5804           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5805         }
5806
5807       /* Restore the saved message.  */
5808       parser->type_definition_forbidden_message = saved_message;
5809
5810       /* If ok so far, parse the dependent expression. We cannot be
5811          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5812          ctor of T, but looks like a cast to function returning T
5813          without a dependent expression.  */
5814       if (!cp_parser_error_occurred (parser))
5815         expr = cp_parser_cast_expression (parser,
5816                                           /*address_p=*/false,
5817                                           /*cast_p=*/true);
5818
5819       if (cp_parser_parse_definitely (parser))
5820         {
5821           /* Warn about old-style casts, if so requested.  */
5822           if (warn_old_style_cast
5823               && !in_system_header
5824               && !VOID_TYPE_P (type)
5825               && current_lang_name != lang_name_c)
5826             warning (OPT_Wold_style_cast, "use of old-style cast");
5827
5828           /* Only type conversions to integral or enumeration types
5829              can be used in constant-expressions.  */
5830           if (!cast_valid_in_integral_constant_expression_p (type)
5831               && (cp_parser_non_integral_constant_expression
5832                   (parser,
5833                    "a cast to a type other than an integral or "
5834                    "enumeration type")))
5835             return error_mark_node;
5836
5837           /* Perform the cast.  */
5838           expr = build_c_cast (type, expr);
5839           return expr;
5840         }
5841     }
5842
5843   /* If we get here, then it's not a cast, so it must be a
5844      unary-expression.  */
5845   return cp_parser_unary_expression (parser, address_p, cast_p);
5846 }
5847
5848 /* Parse a binary expression of the general form:
5849
5850    pm-expression:
5851      cast-expression
5852      pm-expression .* cast-expression
5853      pm-expression ->* cast-expression
5854
5855    multiplicative-expression:
5856      pm-expression
5857      multiplicative-expression * pm-expression
5858      multiplicative-expression / pm-expression
5859      multiplicative-expression % pm-expression
5860
5861    additive-expression:
5862      multiplicative-expression
5863      additive-expression + multiplicative-expression
5864      additive-expression - multiplicative-expression
5865
5866    shift-expression:
5867      additive-expression
5868      shift-expression << additive-expression
5869      shift-expression >> additive-expression
5870
5871    relational-expression:
5872      shift-expression
5873      relational-expression < shift-expression
5874      relational-expression > shift-expression
5875      relational-expression <= shift-expression
5876      relational-expression >= shift-expression
5877
5878   GNU Extension:
5879
5880    relational-expression:
5881      relational-expression <? shift-expression
5882      relational-expression >? shift-expression
5883
5884    equality-expression:
5885      relational-expression
5886      equality-expression == relational-expression
5887      equality-expression != relational-expression
5888
5889    and-expression:
5890      equality-expression
5891      and-expression & equality-expression
5892
5893    exclusive-or-expression:
5894      and-expression
5895      exclusive-or-expression ^ and-expression
5896
5897    inclusive-or-expression:
5898      exclusive-or-expression
5899      inclusive-or-expression | exclusive-or-expression
5900
5901    logical-and-expression:
5902      inclusive-or-expression
5903      logical-and-expression && inclusive-or-expression
5904
5905    logical-or-expression:
5906      logical-and-expression
5907      logical-or-expression || logical-and-expression
5908
5909    All these are implemented with a single function like:
5910
5911    binary-expression:
5912      simple-cast-expression
5913      binary-expression <token> binary-expression
5914
5915    CAST_P is true if this expression is the target of a cast.
5916
5917    The binops_by_token map is used to get the tree codes for each <token> type.
5918    binary-expressions are associated according to a precedence table.  */
5919
5920 #define TOKEN_PRECEDENCE(token)                              \
5921 (((token->type == CPP_GREATER                                \
5922    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5923   && !parser->greater_than_is_operator_p)                    \
5924  ? PREC_NOT_OPERATOR                                         \
5925  : binops_by_token[token->type].prec)
5926
5927 static tree
5928 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5929 {
5930   cp_parser_expression_stack stack;
5931   cp_parser_expression_stack_entry *sp = &stack[0];
5932   tree lhs, rhs;
5933   cp_token *token;
5934   enum tree_code tree_type, lhs_type, rhs_type;
5935   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5936   bool overloaded_p;
5937
5938   /* Parse the first expression.  */
5939   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5940   lhs_type = ERROR_MARK;
5941
5942   for (;;)
5943     {
5944       /* Get an operator token.  */
5945       token = cp_lexer_peek_token (parser->lexer);
5946
5947       if (warn_cxx0x_compat
5948           && token->type == CPP_RSHIFT
5949           && !parser->greater_than_is_operator_p)
5950         {
5951           warning (OPT_Wc__0x_compat, 
5952                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
5953                    &token->location);
5954           warning (OPT_Wc__0x_compat, 
5955                    "suggest parentheses around %<>>%> expression");
5956         }
5957
5958       new_prec = TOKEN_PRECEDENCE (token);
5959
5960       /* Popping an entry off the stack means we completed a subexpression:
5961          - either we found a token which is not an operator (`>' where it is not
5962            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5963            will happen repeatedly;
5964          - or, we found an operator which has lower priority.  This is the case
5965            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5966            parsing `3 * 4'.  */
5967       if (new_prec <= prec)
5968         {
5969           if (sp == stack)
5970             break;
5971           else
5972             goto pop;
5973         }
5974
5975      get_rhs:
5976       tree_type = binops_by_token[token->type].tree_type;
5977
5978       /* We used the operator token.  */
5979       cp_lexer_consume_token (parser->lexer);
5980
5981       /* Extract another operand.  It may be the RHS of this expression
5982          or the LHS of a new, higher priority expression.  */
5983       rhs = cp_parser_simple_cast_expression (parser);
5984       rhs_type = ERROR_MARK;
5985
5986       /* Get another operator token.  Look up its precedence to avoid
5987          building a useless (immediately popped) stack entry for common
5988          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5989       token = cp_lexer_peek_token (parser->lexer);
5990       lookahead_prec = TOKEN_PRECEDENCE (token);
5991       if (lookahead_prec > new_prec)
5992         {
5993           /* ... and prepare to parse the RHS of the new, higher priority
5994              expression.  Since precedence levels on the stack are
5995              monotonically increasing, we do not have to care about
5996              stack overflows.  */
5997           sp->prec = prec;
5998           sp->tree_type = tree_type;
5999           sp->lhs = lhs;
6000           sp->lhs_type = lhs_type;
6001           sp++;
6002           lhs = rhs;
6003           lhs_type = rhs_type;
6004           prec = new_prec;
6005           new_prec = lookahead_prec;
6006           goto get_rhs;
6007
6008          pop:
6009           /* If the stack is not empty, we have parsed into LHS the right side
6010              (`4' in the example above) of an expression we had suspended.
6011              We can use the information on the stack to recover the LHS (`3')
6012              from the stack together with the tree code (`MULT_EXPR'), and
6013              the precedence of the higher level subexpression
6014              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6015              which will be used to actually build the additive expression.  */
6016           --sp;
6017           prec = sp->prec;
6018           tree_type = sp->tree_type;
6019           rhs = lhs;
6020           rhs_type = lhs_type;
6021           lhs = sp->lhs;
6022           lhs_type = sp->lhs_type;
6023         }
6024
6025       overloaded_p = false;
6026       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6027                                &overloaded_p);
6028       lhs_type = tree_type;
6029
6030       /* If the binary operator required the use of an overloaded operator,
6031          then this expression cannot be an integral constant-expression.
6032          An overloaded operator can be used even if both operands are
6033          otherwise permissible in an integral constant-expression if at
6034          least one of the operands is of enumeration type.  */
6035
6036       if (overloaded_p
6037           && (cp_parser_non_integral_constant_expression
6038               (parser, "calls to overloaded operators")))
6039         return error_mark_node;
6040     }
6041
6042   return lhs;
6043 }
6044
6045
6046 /* Parse the `? expression : assignment-expression' part of a
6047    conditional-expression.  The LOGICAL_OR_EXPR is the
6048    logical-or-expression that started the conditional-expression.
6049    Returns a representation of the entire conditional-expression.
6050
6051    This routine is used by cp_parser_assignment_expression.
6052
6053      ? expression : assignment-expression
6054
6055    GNU Extensions:
6056
6057      ? : assignment-expression */
6058
6059 static tree
6060 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6061 {
6062   tree expr;
6063   tree assignment_expr;
6064
6065   /* Consume the `?' token.  */
6066   cp_lexer_consume_token (parser->lexer);
6067   if (cp_parser_allow_gnu_extensions_p (parser)
6068       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6069     /* Implicit true clause.  */
6070     expr = NULL_TREE;
6071   else
6072     /* Parse the expression.  */
6073     expr = cp_parser_expression (parser, /*cast_p=*/false);
6074
6075   /* The next token should be a `:'.  */
6076   cp_parser_require (parser, CPP_COLON, "`:'");
6077   /* Parse the assignment-expression.  */
6078   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6079
6080   /* Build the conditional-expression.  */
6081   return build_x_conditional_expr (logical_or_expr,
6082                                    expr,
6083                                    assignment_expr);
6084 }
6085
6086 /* Parse an assignment-expression.
6087
6088    assignment-expression:
6089      conditional-expression
6090      logical-or-expression assignment-operator assignment_expression
6091      throw-expression
6092
6093    CAST_P is true if this expression is the target of a cast.
6094
6095    Returns a representation for the expression.  */
6096
6097 static tree
6098 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6099 {
6100   tree expr;
6101
6102   /* If the next token is the `throw' keyword, then we're looking at
6103      a throw-expression.  */
6104   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6105     expr = cp_parser_throw_expression (parser);
6106   /* Otherwise, it must be that we are looking at a
6107      logical-or-expression.  */
6108   else
6109     {
6110       /* Parse the binary expressions (logical-or-expression).  */
6111       expr = cp_parser_binary_expression (parser, cast_p);
6112       /* If the next token is a `?' then we're actually looking at a
6113          conditional-expression.  */
6114       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6115         return cp_parser_question_colon_clause (parser, expr);
6116       else
6117         {
6118           enum tree_code assignment_operator;
6119
6120           /* If it's an assignment-operator, we're using the second
6121              production.  */
6122           assignment_operator
6123             = cp_parser_assignment_operator_opt (parser);
6124           if (assignment_operator != ERROR_MARK)
6125             {
6126               tree rhs;
6127
6128               /* Parse the right-hand side of the assignment.  */
6129               rhs = cp_parser_assignment_expression (parser, cast_p);
6130               /* An assignment may not appear in a
6131                  constant-expression.  */
6132               if (cp_parser_non_integral_constant_expression (parser,
6133                                                               "an assignment"))
6134                 return error_mark_node;
6135               /* Build the assignment expression.  */
6136               expr = build_x_modify_expr (expr,
6137                                           assignment_operator,
6138                                           rhs);
6139             }
6140         }
6141     }
6142
6143   return expr;
6144 }
6145
6146 /* Parse an (optional) assignment-operator.
6147
6148    assignment-operator: one of
6149      = *= /= %= += -= >>= <<= &= ^= |=
6150
6151    GNU Extension:
6152
6153    assignment-operator: one of
6154      <?= >?=
6155
6156    If the next token is an assignment operator, the corresponding tree
6157    code is returned, and the token is consumed.  For example, for
6158    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6159    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6160    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6161    operator, ERROR_MARK is returned.  */
6162
6163 static enum tree_code
6164 cp_parser_assignment_operator_opt (cp_parser* parser)
6165 {
6166   enum tree_code op;
6167   cp_token *token;
6168
6169   /* Peek at the next toen.  */
6170   token = cp_lexer_peek_token (parser->lexer);
6171
6172   switch (token->type)
6173     {
6174     case CPP_EQ:
6175       op = NOP_EXPR;
6176       break;
6177
6178     case CPP_MULT_EQ:
6179       op = MULT_EXPR;
6180       break;
6181
6182     case CPP_DIV_EQ:
6183       op = TRUNC_DIV_EXPR;
6184       break;
6185
6186     case CPP_MOD_EQ:
6187       op = TRUNC_MOD_EXPR;
6188       break;
6189
6190     case CPP_PLUS_EQ:
6191       op = PLUS_EXPR;
6192       break;
6193
6194     case CPP_MINUS_EQ:
6195       op = MINUS_EXPR;
6196       break;
6197
6198     case CPP_RSHIFT_EQ:
6199       op = RSHIFT_EXPR;
6200       break;
6201
6202     case CPP_LSHIFT_EQ:
6203       op = LSHIFT_EXPR;
6204       break;
6205
6206     case CPP_AND_EQ:
6207       op = BIT_AND_EXPR;
6208       break;
6209
6210     case CPP_XOR_EQ:
6211       op = BIT_XOR_EXPR;
6212       break;
6213
6214     case CPP_OR_EQ:
6215       op = BIT_IOR_EXPR;
6216       break;
6217
6218     default:
6219       /* Nothing else is an assignment operator.  */
6220       op = ERROR_MARK;
6221     }
6222
6223   /* If it was an assignment operator, consume it.  */
6224   if (op != ERROR_MARK)
6225     cp_lexer_consume_token (parser->lexer);
6226
6227   return op;
6228 }
6229
6230 /* Parse an expression.
6231
6232    expression:
6233      assignment-expression
6234      expression , assignment-expression
6235
6236    CAST_P is true if this expression is the target of a cast.
6237
6238    Returns a representation of the expression.  */
6239
6240 static tree
6241 cp_parser_expression (cp_parser* parser, bool cast_p)
6242 {
6243   tree expression = NULL_TREE;
6244
6245   while (true)
6246     {
6247       tree assignment_expression;
6248
6249       /* Parse the next assignment-expression.  */
6250       assignment_expression
6251         = cp_parser_assignment_expression (parser, cast_p);
6252       /* If this is the first assignment-expression, we can just
6253          save it away.  */
6254       if (!expression)
6255         expression = assignment_expression;
6256       else
6257         expression = build_x_compound_expr (expression,
6258                                             assignment_expression);
6259       /* If the next token is not a comma, then we are done with the
6260          expression.  */
6261       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6262         break;
6263       /* Consume the `,'.  */
6264       cp_lexer_consume_token (parser->lexer);
6265       /* A comma operator cannot appear in a constant-expression.  */
6266       if (cp_parser_non_integral_constant_expression (parser,
6267                                                       "a comma operator"))
6268         expression = error_mark_node;
6269     }
6270
6271   return expression;
6272 }
6273
6274 /* Parse a constant-expression.
6275
6276    constant-expression:
6277      conditional-expression
6278
6279   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6280   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6281   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6282   is false, NON_CONSTANT_P should be NULL.  */
6283
6284 static tree
6285 cp_parser_constant_expression (cp_parser* parser,
6286                                bool allow_non_constant_p,
6287                                bool *non_constant_p)
6288 {
6289   bool saved_integral_constant_expression_p;
6290   bool saved_allow_non_integral_constant_expression_p;
6291   bool saved_non_integral_constant_expression_p;
6292   tree expression;
6293
6294   /* It might seem that we could simply parse the
6295      conditional-expression, and then check to see if it were
6296      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6297      one that the compiler can figure out is constant, possibly after
6298      doing some simplifications or optimizations.  The standard has a
6299      precise definition of constant-expression, and we must honor
6300      that, even though it is somewhat more restrictive.
6301
6302      For example:
6303
6304        int i[(2, 3)];
6305
6306      is not a legal declaration, because `(2, 3)' is not a
6307      constant-expression.  The `,' operator is forbidden in a
6308      constant-expression.  However, GCC's constant-folding machinery
6309      will fold this operation to an INTEGER_CST for `3'.  */
6310
6311   /* Save the old settings.  */
6312   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6313   saved_allow_non_integral_constant_expression_p
6314     = parser->allow_non_integral_constant_expression_p;
6315   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6316   /* We are now parsing a constant-expression.  */
6317   parser->integral_constant_expression_p = true;
6318   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6319   parser->non_integral_constant_expression_p = false;
6320   /* Although the grammar says "conditional-expression", we parse an
6321      "assignment-expression", which also permits "throw-expression"
6322      and the use of assignment operators.  In the case that
6323      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6324      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6325      actually essential that we look for an assignment-expression.
6326      For example, cp_parser_initializer_clauses uses this function to
6327      determine whether a particular assignment-expression is in fact
6328      constant.  */
6329   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6330   /* Restore the old settings.  */
6331   parser->integral_constant_expression_p
6332     = saved_integral_constant_expression_p;
6333   parser->allow_non_integral_constant_expression_p
6334     = saved_allow_non_integral_constant_expression_p;
6335   if (allow_non_constant_p)
6336     *non_constant_p = parser->non_integral_constant_expression_p;
6337   else if (parser->non_integral_constant_expression_p)
6338     expression = error_mark_node;
6339   parser->non_integral_constant_expression_p
6340     = saved_non_integral_constant_expression_p;
6341
6342   return expression;
6343 }
6344
6345 /* Parse __builtin_offsetof.
6346
6347    offsetof-expression:
6348      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6349
6350    offsetof-member-designator:
6351      id-expression
6352      | offsetof-member-designator "." id-expression
6353      | offsetof-member-designator "[" expression "]"  */
6354
6355 static tree
6356 cp_parser_builtin_offsetof (cp_parser *parser)
6357 {
6358   int save_ice_p, save_non_ice_p;
6359   tree type, expr;
6360   cp_id_kind dummy;
6361
6362   /* We're about to accept non-integral-constant things, but will
6363      definitely yield an integral constant expression.  Save and
6364      restore these values around our local parsing.  */
6365   save_ice_p = parser->integral_constant_expression_p;
6366   save_non_ice_p = parser->non_integral_constant_expression_p;
6367
6368   /* Consume the "__builtin_offsetof" token.  */
6369   cp_lexer_consume_token (parser->lexer);
6370   /* Consume the opening `('.  */
6371   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6372   /* Parse the type-id.  */
6373   type = cp_parser_type_id (parser);
6374   /* Look for the `,'.  */
6375   cp_parser_require (parser, CPP_COMMA, "`,'");
6376
6377   /* Build the (type *)null that begins the traditional offsetof macro.  */
6378   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6379
6380   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6381   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6382                                                  true, &dummy);
6383   while (true)
6384     {
6385       cp_token *token = cp_lexer_peek_token (parser->lexer);
6386       switch (token->type)
6387         {
6388         case CPP_OPEN_SQUARE:
6389           /* offsetof-member-designator "[" expression "]" */
6390           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6391           break;
6392
6393         case CPP_DOT:
6394           /* offsetof-member-designator "." identifier */
6395           cp_lexer_consume_token (parser->lexer);
6396           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6397                                                          true, &dummy);
6398           break;
6399
6400         case CPP_CLOSE_PAREN:
6401           /* Consume the ")" token.  */
6402           cp_lexer_consume_token (parser->lexer);
6403           goto success;
6404
6405         default:
6406           /* Error.  We know the following require will fail, but
6407              that gives the proper error message.  */
6408           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6409           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6410           expr = error_mark_node;
6411           goto failure;
6412         }
6413     }
6414
6415  success:
6416   /* If we're processing a template, we can't finish the semantics yet.
6417      Otherwise we can fold the entire expression now.  */
6418   if (processing_template_decl)
6419     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6420   else
6421     expr = finish_offsetof (expr);
6422
6423  failure:
6424   parser->integral_constant_expression_p = save_ice_p;
6425   parser->non_integral_constant_expression_p = save_non_ice_p;
6426
6427   return expr;
6428 }
6429
6430 /* Parse a trait expression.  */
6431
6432 static tree
6433 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6434 {
6435   cp_trait_kind kind;
6436   tree type1, type2 = NULL_TREE;
6437   bool binary = false;
6438   cp_decl_specifier_seq decl_specs;
6439
6440   switch (keyword)
6441     {
6442     case RID_HAS_NOTHROW_ASSIGN:
6443       kind = CPTK_HAS_NOTHROW_ASSIGN;
6444       break;
6445     case RID_HAS_NOTHROW_CONSTRUCTOR:
6446       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6447       break;
6448     case RID_HAS_NOTHROW_COPY:
6449       kind = CPTK_HAS_NOTHROW_COPY;
6450       break;
6451     case RID_HAS_TRIVIAL_ASSIGN:
6452       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6453       break;
6454     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6455       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6456       break;
6457     case RID_HAS_TRIVIAL_COPY:
6458       kind = CPTK_HAS_TRIVIAL_COPY;
6459       break;
6460     case RID_HAS_TRIVIAL_DESTRUCTOR:
6461       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6462       break;
6463     case RID_HAS_VIRTUAL_DESTRUCTOR:
6464       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6465       break;
6466     case RID_IS_ABSTRACT:
6467       kind = CPTK_IS_ABSTRACT;
6468       break;
6469     case RID_IS_BASE_OF:
6470       kind = CPTK_IS_BASE_OF;
6471       binary = true;
6472       break;
6473     case RID_IS_CLASS:
6474       kind = CPTK_IS_CLASS;
6475       break;
6476     case RID_IS_CONVERTIBLE_TO:
6477       kind = CPTK_IS_CONVERTIBLE_TO;
6478       binary = true;
6479       break;
6480     case RID_IS_EMPTY:
6481       kind = CPTK_IS_EMPTY;
6482       break;
6483     case RID_IS_ENUM:
6484       kind = CPTK_IS_ENUM;
6485       break;
6486     case RID_IS_POD:
6487       kind = CPTK_IS_POD;
6488       break;
6489     case RID_IS_POLYMORPHIC:
6490       kind = CPTK_IS_POLYMORPHIC;
6491       break;
6492     case RID_IS_UNION:
6493       kind = CPTK_IS_UNION;
6494       break;
6495     default:
6496       gcc_unreachable ();
6497     }
6498
6499   /* Consume the token.  */
6500   cp_lexer_consume_token (parser->lexer);
6501
6502   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6503
6504   type1 = cp_parser_type_id (parser);
6505
6506   if (type1 == error_mark_node)
6507     return error_mark_node;
6508
6509   /* Build a trivial decl-specifier-seq.  */
6510   clear_decl_specs (&decl_specs);
6511   decl_specs.type = type1;
6512
6513   /* Call grokdeclarator to figure out what type this is.  */
6514   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6515                           /*initialized=*/0, /*attrlist=*/NULL);
6516
6517   if (binary)
6518     {
6519       cp_parser_require (parser, CPP_COMMA, "`,'");
6520  
6521       type2 = cp_parser_type_id (parser);
6522
6523       if (type2 == error_mark_node)
6524         return error_mark_node;
6525
6526       /* Build a trivial decl-specifier-seq.  */
6527       clear_decl_specs (&decl_specs);
6528       decl_specs.type = type2;
6529
6530       /* Call grokdeclarator to figure out what type this is.  */
6531       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6532                               /*initialized=*/0, /*attrlist=*/NULL);
6533     }
6534
6535   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6536
6537   /* Complete the trait expression, which may mean either processing
6538      the trait expr now or saving it for template instantiation.  */
6539   return finish_trait_expr (kind, type1, type2);
6540 }
6541
6542 /* Statements [gram.stmt.stmt]  */
6543
6544 /* Parse a statement.
6545
6546    statement:
6547      labeled-statement
6548      expression-statement
6549      compound-statement
6550      selection-statement
6551      iteration-statement
6552      jump-statement
6553      declaration-statement
6554      try-block
6555
6556   IN_COMPOUND is true when the statement is nested inside a
6557   cp_parser_compound_statement; this matters for certain pragmas.
6558
6559   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6560   is a (possibly labeled) if statement which is not enclosed in braces
6561   and has an else clause.  This is used to implement -Wparentheses.  */
6562
6563 static void
6564 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6565                      bool in_compound, bool *if_p)
6566 {
6567   tree statement;
6568   cp_token *token;
6569   location_t statement_location;
6570
6571  restart:
6572   if (if_p != NULL)
6573     *if_p = false;
6574   /* There is no statement yet.  */
6575   statement = NULL_TREE;
6576   /* Peek at the next token.  */
6577   token = cp_lexer_peek_token (parser->lexer);
6578   /* Remember the location of the first token in the statement.  */
6579   statement_location = token->location;
6580   /* If this is a keyword, then that will often determine what kind of
6581      statement we have.  */
6582   if (token->type == CPP_KEYWORD)
6583     {
6584       enum rid keyword = token->keyword;
6585
6586       switch (keyword)
6587         {
6588         case RID_CASE:
6589         case RID_DEFAULT:
6590           /* Looks like a labeled-statement with a case label.
6591              Parse the label, and then use tail recursion to parse
6592              the statement.  */
6593           cp_parser_label_for_labeled_statement (parser);
6594           goto restart;
6595
6596         case RID_IF:
6597         case RID_SWITCH:
6598           statement = cp_parser_selection_statement (parser, if_p);
6599           break;
6600
6601         case RID_WHILE:
6602         case RID_DO:
6603         case RID_FOR:
6604           statement = cp_parser_iteration_statement (parser);
6605           break;
6606
6607         case RID_BREAK:
6608         case RID_CONTINUE:
6609         case RID_RETURN:
6610         case RID_GOTO:
6611           statement = cp_parser_jump_statement (parser);
6612           break;
6613
6614           /* Objective-C++ exception-handling constructs.  */
6615         case RID_AT_TRY:
6616         case RID_AT_CATCH:
6617         case RID_AT_FINALLY:
6618         case RID_AT_SYNCHRONIZED:
6619         case RID_AT_THROW:
6620           statement = cp_parser_objc_statement (parser);
6621           break;
6622
6623         case RID_TRY:
6624           statement = cp_parser_try_block (parser);
6625           break;
6626
6627         case RID_NAMESPACE:
6628           /* This must be a namespace alias definition.  */
6629           cp_parser_declaration_statement (parser);
6630           return;
6631           
6632         default:
6633           /* It might be a keyword like `int' that can start a
6634              declaration-statement.  */
6635           break;
6636         }
6637     }
6638   else if (token->type == CPP_NAME)
6639     {
6640       /* If the next token is a `:', then we are looking at a
6641          labeled-statement.  */
6642       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6643       if (token->type == CPP_COLON)
6644         {
6645           /* Looks like a labeled-statement with an ordinary label.
6646              Parse the label, and then use tail recursion to parse
6647              the statement.  */
6648           cp_parser_label_for_labeled_statement (parser);
6649           goto restart;
6650         }
6651     }
6652   /* Anything that starts with a `{' must be a compound-statement.  */
6653   else if (token->type == CPP_OPEN_BRACE)
6654     statement = cp_parser_compound_statement (parser, NULL, false);
6655   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6656      a statement all its own.  */
6657   else if (token->type == CPP_PRAGMA)
6658     {
6659       /* Only certain OpenMP pragmas are attached to statements, and thus
6660          are considered statements themselves.  All others are not.  In
6661          the context of a compound, accept the pragma as a "statement" and
6662          return so that we can check for a close brace.  Otherwise we
6663          require a real statement and must go back and read one.  */
6664       if (in_compound)
6665         cp_parser_pragma (parser, pragma_compound);
6666       else if (!cp_parser_pragma (parser, pragma_stmt))
6667         goto restart;
6668       return;
6669     }
6670   else if (token->type == CPP_EOF)
6671     {
6672       cp_parser_error (parser, "expected statement");
6673       return;
6674     }
6675
6676   /* Everything else must be a declaration-statement or an
6677      expression-statement.  Try for the declaration-statement
6678      first, unless we are looking at a `;', in which case we know that
6679      we have an expression-statement.  */
6680   if (!statement)
6681     {
6682       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6683         {
6684           cp_parser_parse_tentatively (parser);
6685           /* Try to parse the declaration-statement.  */
6686           cp_parser_declaration_statement (parser);
6687           /* If that worked, we're done.  */
6688           if (cp_parser_parse_definitely (parser))
6689             return;
6690         }
6691       /* Look for an expression-statement instead.  */
6692       statement = cp_parser_expression_statement (parser, in_statement_expr);
6693     }
6694
6695   /* Set the line number for the statement.  */
6696   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6697     SET_EXPR_LOCATION (statement, statement_location);
6698 }
6699
6700 /* Parse the label for a labeled-statement, i.e.
6701
6702    identifier :
6703    case constant-expression :
6704    default :
6705
6706    GNU Extension:
6707    case constant-expression ... constant-expression : statement
6708
6709    When a label is parsed without errors, the label is added to the
6710    parse tree by the finish_* functions, so this function doesn't
6711    have to return the label.  */
6712
6713 static void
6714 cp_parser_label_for_labeled_statement (cp_parser* parser)
6715 {
6716   cp_token *token;
6717
6718   /* The next token should be an identifier.  */
6719   token = cp_lexer_peek_token (parser->lexer);
6720   if (token->type != CPP_NAME
6721       && token->type != CPP_KEYWORD)
6722     {
6723       cp_parser_error (parser, "expected labeled-statement");
6724       return;
6725     }
6726
6727   switch (token->keyword)
6728     {
6729     case RID_CASE:
6730       {
6731         tree expr, expr_hi;
6732         cp_token *ellipsis;
6733
6734         /* Consume the `case' token.  */
6735         cp_lexer_consume_token (parser->lexer);
6736         /* Parse the constant-expression.  */
6737         expr = cp_parser_constant_expression (parser,
6738                                               /*allow_non_constant_p=*/false,
6739                                               NULL);
6740
6741         ellipsis = cp_lexer_peek_token (parser->lexer);
6742         if (ellipsis->type == CPP_ELLIPSIS)
6743           {
6744             /* Consume the `...' token.  */
6745             cp_lexer_consume_token (parser->lexer);
6746             expr_hi =
6747               cp_parser_constant_expression (parser,
6748                                              /*allow_non_constant_p=*/false,
6749                                              NULL);
6750             /* We don't need to emit warnings here, as the common code
6751                will do this for us.  */
6752           }
6753         else
6754           expr_hi = NULL_TREE;
6755
6756         if (parser->in_switch_statement_p)
6757           finish_case_label (expr, expr_hi);
6758         else
6759           error ("case label %qE not within a switch statement", expr);
6760       }
6761       break;
6762
6763     case RID_DEFAULT:
6764       /* Consume the `default' token.  */
6765       cp_lexer_consume_token (parser->lexer);
6766
6767       if (parser->in_switch_statement_p)
6768         finish_case_label (NULL_TREE, NULL_TREE);
6769       else
6770         error ("case label not within a switch statement");
6771       break;
6772
6773     default:
6774       /* Anything else must be an ordinary label.  */
6775       finish_label_stmt (cp_parser_identifier (parser));
6776       break;
6777     }
6778
6779   /* Require the `:' token.  */
6780   cp_parser_require (parser, CPP_COLON, "`:'");
6781 }
6782
6783 /* Parse an expression-statement.
6784
6785    expression-statement:
6786      expression [opt] ;
6787
6788    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6789    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6790    indicates whether this expression-statement is part of an
6791    expression statement.  */
6792
6793 static tree
6794 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6795 {
6796   tree statement = NULL_TREE;
6797
6798   /* If the next token is a ';', then there is no expression
6799      statement.  */
6800   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6801     statement = cp_parser_expression (parser, /*cast_p=*/false);
6802
6803   /* Consume the final `;'.  */
6804   cp_parser_consume_semicolon_at_end_of_statement (parser);
6805
6806   if (in_statement_expr
6807       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6808     /* This is the final expression statement of a statement
6809        expression.  */
6810     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6811   else if (statement)
6812     statement = finish_expr_stmt (statement);
6813   else
6814     finish_stmt ();
6815
6816   return statement;
6817 }
6818
6819 /* Parse a compound-statement.
6820
6821    compound-statement:
6822      { statement-seq [opt] }
6823
6824    Returns a tree representing the statement.  */
6825
6826 static tree
6827 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6828                               bool in_try)
6829 {
6830   tree compound_stmt;
6831
6832   /* Consume the `{'.  */
6833   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6834     return error_mark_node;
6835   /* Begin the compound-statement.  */
6836   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6837   /* Parse an (optional) statement-seq.  */
6838   cp_parser_statement_seq_opt (parser, in_statement_expr);
6839   /* Finish the compound-statement.  */
6840   finish_compound_stmt (compound_stmt);
6841   /* Consume the `}'.  */
6842   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6843
6844   return compound_stmt;
6845 }
6846
6847 /* Parse an (optional) statement-seq.
6848
6849    statement-seq:
6850      statement
6851      statement-seq [opt] statement  */
6852
6853 static void
6854 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6855 {
6856   /* Scan statements until there aren't any more.  */
6857   while (true)
6858     {
6859       cp_token *token = cp_lexer_peek_token (parser->lexer);
6860
6861       /* If we're looking at a `}', then we've run out of statements.  */
6862       if (token->type == CPP_CLOSE_BRACE
6863           || token->type == CPP_EOF
6864           || token->type == CPP_PRAGMA_EOL)
6865         break;
6866       
6867       /* If we are in a compound statement and find 'else' then
6868          something went wrong.  */
6869       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6870         {
6871           if (parser->in_statement & IN_IF_STMT) 
6872             break;
6873           else
6874             {
6875               token = cp_lexer_consume_token (parser->lexer);
6876               error ("%<else%> without a previous %<if%>");
6877             }
6878         }
6879
6880       /* Parse the statement.  */
6881       cp_parser_statement (parser, in_statement_expr, true, NULL);
6882     }
6883 }
6884
6885 /* Parse a selection-statement.
6886
6887    selection-statement:
6888      if ( condition ) statement
6889      if ( condition ) statement else statement
6890      switch ( condition ) statement
6891
6892    Returns the new IF_STMT or SWITCH_STMT.
6893
6894    If IF_P is not NULL, *IF_P is set to indicate whether the statement
6895    is a (possibly labeled) if statement which is not enclosed in
6896    braces and has an else clause.  This is used to implement
6897    -Wparentheses.  */
6898
6899 static tree
6900 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6901 {
6902   cp_token *token;
6903   enum rid keyword;
6904
6905   if (if_p != NULL)
6906     *if_p = false;
6907
6908   /* Peek at the next token.  */
6909   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6910
6911   /* See what kind of keyword it is.  */
6912   keyword = token->keyword;
6913   switch (keyword)
6914     {
6915     case RID_IF:
6916     case RID_SWITCH:
6917       {
6918         tree statement;
6919         tree condition;
6920
6921         /* Look for the `('.  */
6922         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6923           {
6924             cp_parser_skip_to_end_of_statement (parser);
6925             return error_mark_node;
6926           }
6927
6928         /* Begin the selection-statement.  */
6929         if (keyword == RID_IF)
6930           statement = begin_if_stmt ();
6931         else
6932           statement = begin_switch_stmt ();
6933
6934         /* Parse the condition.  */
6935         condition = cp_parser_condition (parser);
6936         /* Look for the `)'.  */
6937         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6938           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6939                                                  /*consume_paren=*/true);
6940
6941         if (keyword == RID_IF)
6942           {
6943             bool nested_if;
6944             unsigned char in_statement;
6945
6946             /* Add the condition.  */
6947             finish_if_stmt_cond (condition, statement);
6948
6949             /* Parse the then-clause.  */
6950             in_statement = parser->in_statement;
6951             parser->in_statement |= IN_IF_STMT;
6952             cp_parser_implicitly_scoped_statement (parser, &nested_if);
6953             parser->in_statement = in_statement;
6954
6955             finish_then_clause (statement);
6956
6957             /* If the next token is `else', parse the else-clause.  */
6958             if (cp_lexer_next_token_is_keyword (parser->lexer,
6959                                                 RID_ELSE))
6960               {
6961                 /* Consume the `else' keyword.  */
6962                 cp_lexer_consume_token (parser->lexer);
6963                 begin_else_clause (statement);
6964                 /* Parse the else-clause.  */
6965                 cp_parser_implicitly_scoped_statement (parser, NULL);
6966                 finish_else_clause (statement);
6967
6968                 /* If we are currently parsing a then-clause, then
6969                    IF_P will not be NULL.  We set it to true to
6970                    indicate that this if statement has an else clause.
6971                    This may trigger the Wparentheses warning below
6972                    when we get back up to the parent if statement.  */
6973                 if (if_p != NULL)
6974                   *if_p = true;
6975               }
6976             else
6977               {
6978                 /* This if statement does not have an else clause.  If
6979                    NESTED_IF is true, then the then-clause is an if
6980                    statement which does have an else clause.  We warn
6981                    about the potential ambiguity.  */
6982                 if (nested_if)
6983                   warning (OPT_Wparentheses,
6984                            ("%Hsuggest explicit braces "
6985                             "to avoid ambiguous %<else%>"),
6986                            EXPR_LOCUS (statement));
6987               }
6988
6989             /* Now we're all done with the if-statement.  */
6990             finish_if_stmt (statement);
6991           }
6992         else
6993           {
6994             bool in_switch_statement_p;
6995             unsigned char in_statement;
6996
6997             /* Add the condition.  */
6998             finish_switch_cond (condition, statement);
6999
7000             /* Parse the body of the switch-statement.  */
7001             in_switch_statement_p = parser->in_switch_statement_p;
7002             in_statement = parser->in_statement;
7003             parser->in_switch_statement_p = true;
7004             parser->in_statement |= IN_SWITCH_STMT;
7005             cp_parser_implicitly_scoped_statement (parser, NULL);
7006             parser->in_switch_statement_p = in_switch_statement_p;
7007             parser->in_statement = in_statement;
7008
7009             /* Now we're all done with the switch-statement.  */
7010             finish_switch_stmt (statement);
7011           }
7012
7013         return statement;
7014       }
7015       break;
7016
7017     default:
7018       cp_parser_error (parser, "expected selection-statement");
7019       return error_mark_node;
7020     }
7021 }
7022
7023 /* Parse a condition.
7024
7025    condition:
7026      expression
7027      type-specifier-seq declarator = assignment-expression
7028
7029    GNU Extension:
7030
7031    condition:
7032      type-specifier-seq declarator asm-specification [opt]
7033        attributes [opt] = assignment-expression
7034
7035    Returns the expression that should be tested.  */
7036
7037 static tree
7038 cp_parser_condition (cp_parser* parser)
7039 {
7040   cp_decl_specifier_seq type_specifiers;
7041   const char *saved_message;
7042
7043   /* Try the declaration first.  */
7044   cp_parser_parse_tentatively (parser);
7045   /* New types are not allowed in the type-specifier-seq for a
7046      condition.  */
7047   saved_message = parser->type_definition_forbidden_message;
7048   parser->type_definition_forbidden_message
7049     = "types may not be defined in conditions";
7050   /* Parse the type-specifier-seq.  */
7051   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7052                                 &type_specifiers);
7053   /* Restore the saved message.  */
7054   parser->type_definition_forbidden_message = saved_message;
7055   /* If all is well, we might be looking at a declaration.  */
7056   if (!cp_parser_error_occurred (parser))
7057     {
7058       tree decl;
7059       tree asm_specification;
7060       tree attributes;
7061       cp_declarator *declarator;
7062       tree initializer = NULL_TREE;
7063
7064       /* Parse the declarator.  */
7065       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7066                                          /*ctor_dtor_or_conv_p=*/NULL,
7067                                          /*parenthesized_p=*/NULL,
7068                                          /*member_p=*/false);
7069       /* Parse the attributes.  */
7070       attributes = cp_parser_attributes_opt (parser);
7071       /* Parse the asm-specification.  */
7072       asm_specification = cp_parser_asm_specification_opt (parser);
7073       /* If the next token is not an `=', then we might still be
7074          looking at an expression.  For example:
7075
7076            if (A(a).x)
7077
7078          looks like a decl-specifier-seq and a declarator -- but then
7079          there is no `=', so this is an expression.  */
7080       cp_parser_require (parser, CPP_EQ, "`='");
7081       /* If we did see an `=', then we are looking at a declaration
7082          for sure.  */
7083       if (cp_parser_parse_definitely (parser))
7084         {
7085           tree pushed_scope;
7086           bool non_constant_p;
7087
7088           /* Create the declaration.  */
7089           decl = start_decl (declarator, &type_specifiers,
7090                              /*initialized_p=*/true,
7091                              attributes, /*prefix_attributes=*/NULL_TREE,
7092                              &pushed_scope);
7093           /* Parse the assignment-expression.  */
7094           initializer
7095             = cp_parser_constant_expression (parser,
7096                                              /*allow_non_constant_p=*/true,
7097                                              &non_constant_p);
7098           if (!non_constant_p)
7099             initializer = fold_non_dependent_expr (initializer);
7100
7101           /* Process the initializer.  */
7102           cp_finish_decl (decl,
7103                           initializer, !non_constant_p,
7104                           asm_specification,
7105                           LOOKUP_ONLYCONVERTING);
7106
7107           if (pushed_scope)
7108             pop_scope (pushed_scope);
7109
7110           return convert_from_reference (decl);
7111         }
7112     }
7113   /* If we didn't even get past the declarator successfully, we are
7114      definitely not looking at a declaration.  */
7115   else
7116     cp_parser_abort_tentative_parse (parser);
7117
7118   /* Otherwise, we are looking at an expression.  */
7119   return cp_parser_expression (parser, /*cast_p=*/false);
7120 }
7121
7122 /* We check for a ) immediately followed by ; with no whitespacing
7123    between.  This is used to issue a warning for:
7124
7125      while (...);
7126
7127    and:
7128
7129      for (...);
7130
7131    as the semicolon is probably extraneous.
7132
7133    On parse errors, the next token might not be a ), so do nothing in
7134    that case. */
7135
7136 static void
7137 check_empty_body (cp_parser* parser, const char* type)
7138 {
7139   cp_token *token;
7140   cp_token *close_paren;
7141   expanded_location close_loc;
7142   expanded_location semi_loc;
7143   
7144   close_paren = cp_lexer_peek_token (parser->lexer);
7145   if (close_paren->type != CPP_CLOSE_PAREN)
7146     return;
7147
7148   close_loc = expand_location (close_paren->location);
7149   token = cp_lexer_peek_nth_token (parser->lexer, 2);
7150
7151   if (token->type != CPP_SEMICOLON
7152       || (token->flags & PREV_WHITE))
7153     return;
7154
7155   semi_loc =  expand_location (token->location);
7156   if (close_loc.line == semi_loc.line
7157 #ifdef USE_MAPPED_LOCATION
7158       && close_loc.column+1 == semi_loc.column
7159 #endif
7160       )
7161     warning (OPT_Wempty_body,
7162              "suggest a space before %<;%> or explicit braces around empty "
7163              "body in %<%s%> statement",
7164              type);
7165 }
7166
7167 /* Parse an iteration-statement.
7168
7169    iteration-statement:
7170      while ( condition ) statement
7171      do statement while ( expression ) ;
7172      for ( for-init-statement condition [opt] ; expression [opt] )
7173        statement
7174
7175    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7176
7177 static tree
7178 cp_parser_iteration_statement (cp_parser* parser)
7179 {
7180   cp_token *token;
7181   enum rid keyword;
7182   tree statement;
7183   unsigned char in_statement;
7184
7185   /* Peek at the next token.  */
7186   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7187   if (!token)
7188     return error_mark_node;
7189
7190   /* Remember whether or not we are already within an iteration
7191      statement.  */
7192   in_statement = parser->in_statement;
7193
7194   /* See what kind of keyword it is.  */
7195   keyword = token->keyword;
7196   switch (keyword)
7197     {
7198     case RID_WHILE:
7199       {
7200         tree condition;
7201
7202         /* Begin the while-statement.  */
7203         statement = begin_while_stmt ();
7204         /* Look for the `('.  */
7205         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7206         /* Parse the condition.  */
7207         condition = cp_parser_condition (parser);
7208         finish_while_stmt_cond (condition, statement);
7209         check_empty_body (parser, "while");
7210         /* Look for the `)'.  */
7211         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7212         /* Parse the dependent statement.  */
7213         parser->in_statement = IN_ITERATION_STMT;
7214         cp_parser_already_scoped_statement (parser);
7215         parser->in_statement = in_statement;
7216         /* We're done with the while-statement.  */
7217         finish_while_stmt (statement);
7218       }
7219       break;
7220
7221     case RID_DO:
7222       {
7223         tree expression;
7224
7225         /* Begin the do-statement.  */
7226         statement = begin_do_stmt ();
7227         /* Parse the body of the do-statement.  */
7228         parser->in_statement = IN_ITERATION_STMT;
7229         cp_parser_implicitly_scoped_statement (parser, NULL);
7230         parser->in_statement = in_statement;
7231         finish_do_body (statement);
7232         /* Look for the `while' keyword.  */
7233         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7234         /* Look for the `('.  */
7235         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7236         /* Parse the expression.  */
7237         expression = cp_parser_expression (parser, /*cast_p=*/false);
7238         /* We're done with the do-statement.  */
7239         finish_do_stmt (expression, statement);
7240         /* Look for the `)'.  */
7241         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7242         /* Look for the `;'.  */
7243         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7244       }
7245       break;
7246
7247     case RID_FOR:
7248       {
7249         tree condition = NULL_TREE;
7250         tree expression = NULL_TREE;
7251
7252         /* Begin the for-statement.  */
7253         statement = begin_for_stmt ();
7254         /* Look for the `('.  */
7255         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7256         /* Parse the initialization.  */
7257         cp_parser_for_init_statement (parser);
7258         finish_for_init_stmt (statement);
7259
7260         /* If there's a condition, process it.  */
7261         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7262           condition = cp_parser_condition (parser);
7263         finish_for_cond (condition, statement);
7264         /* Look for the `;'.  */
7265         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7266
7267         /* If there's an expression, process it.  */
7268         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7269           expression = cp_parser_expression (parser, /*cast_p=*/false);
7270         finish_for_expr (expression, statement);
7271         check_empty_body (parser, "for");
7272         /* Look for the `)'.  */
7273         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7274
7275         /* Parse the body of the for-statement.  */
7276         parser->in_statement = IN_ITERATION_STMT;
7277         cp_parser_already_scoped_statement (parser);
7278         parser->in_statement = in_statement;
7279
7280         /* We're done with the for-statement.  */
7281         finish_for_stmt (statement);
7282       }
7283       break;
7284
7285     default:
7286       cp_parser_error (parser, "expected iteration-statement");
7287       statement = error_mark_node;
7288       break;
7289     }
7290
7291   return statement;
7292 }
7293
7294 /* Parse a for-init-statement.
7295
7296    for-init-statement:
7297      expression-statement
7298      simple-declaration  */
7299
7300 static void
7301 cp_parser_for_init_statement (cp_parser* parser)
7302 {
7303   /* If the next token is a `;', then we have an empty
7304      expression-statement.  Grammatically, this is also a
7305      simple-declaration, but an invalid one, because it does not
7306      declare anything.  Therefore, if we did not handle this case
7307      specially, we would issue an error message about an invalid
7308      declaration.  */
7309   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7310     {
7311       /* We're going to speculatively look for a declaration, falling back
7312          to an expression, if necessary.  */
7313       cp_parser_parse_tentatively (parser);
7314       /* Parse the declaration.  */
7315       cp_parser_simple_declaration (parser,
7316                                     /*function_definition_allowed_p=*/false);
7317       /* If the tentative parse failed, then we shall need to look for an
7318          expression-statement.  */
7319       if (cp_parser_parse_definitely (parser))
7320         return;
7321     }
7322
7323   cp_parser_expression_statement (parser, false);
7324 }
7325
7326 /* Parse a jump-statement.
7327
7328    jump-statement:
7329      break ;
7330      continue ;
7331      return expression [opt] ;
7332      goto identifier ;
7333
7334    GNU extension:
7335
7336    jump-statement:
7337      goto * expression ;
7338
7339    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7340
7341 static tree
7342 cp_parser_jump_statement (cp_parser* parser)
7343 {
7344   tree statement = error_mark_node;
7345   cp_token *token;
7346   enum rid keyword;
7347   unsigned char in_statement;
7348
7349   /* Peek at the next token.  */
7350   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7351   if (!token)
7352     return error_mark_node;
7353
7354   /* See what kind of keyword it is.  */
7355   keyword = token->keyword;
7356   switch (keyword)
7357     {
7358     case RID_BREAK:
7359       in_statement = parser->in_statement & ~IN_IF_STMT;      
7360       switch (in_statement)
7361         {
7362         case 0:
7363           error ("break statement not within loop or switch");
7364           break;
7365         default:
7366           gcc_assert ((in_statement & IN_SWITCH_STMT)
7367                       || in_statement == IN_ITERATION_STMT);
7368           statement = finish_break_stmt ();
7369           break;
7370         case IN_OMP_BLOCK:
7371           error ("invalid exit from OpenMP structured block");
7372           break;
7373         case IN_OMP_FOR:
7374           error ("break statement used with OpenMP for loop");
7375           break;
7376         }
7377       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7378       break;
7379
7380     case RID_CONTINUE:
7381       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7382         {
7383         case 0:
7384           error ("continue statement not within a loop");
7385           break;
7386         case IN_ITERATION_STMT:
7387         case IN_OMP_FOR:
7388           statement = finish_continue_stmt ();
7389           break;
7390         case IN_OMP_BLOCK:
7391           error ("invalid exit from OpenMP structured block");
7392           break;
7393         default:
7394           gcc_unreachable ();
7395         }
7396       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7397       break;
7398
7399     case RID_RETURN:
7400       {
7401         tree expr;
7402
7403         /* If the next token is a `;', then there is no
7404            expression.  */
7405         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7406           expr = cp_parser_expression (parser, /*cast_p=*/false);
7407         else
7408           expr = NULL_TREE;
7409         /* Build the return-statement.  */
7410         statement = finish_return_stmt (expr);
7411         /* Look for the final `;'.  */
7412         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7413       }
7414       break;
7415
7416     case RID_GOTO:
7417       /* Create the goto-statement.  */
7418       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7419         {
7420           /* Issue a warning about this use of a GNU extension.  */
7421           if (pedantic)
7422             pedwarn ("ISO C++ forbids computed gotos");
7423           /* Consume the '*' token.  */
7424           cp_lexer_consume_token (parser->lexer);
7425           /* Parse the dependent expression.  */
7426           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7427         }
7428       else
7429         finish_goto_stmt (cp_parser_identifier (parser));
7430       /* Look for the final `;'.  */
7431       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7432       break;
7433
7434     default:
7435       cp_parser_error (parser, "expected jump-statement");
7436       break;
7437     }
7438
7439   return statement;
7440 }
7441
7442 /* Parse a declaration-statement.
7443
7444    declaration-statement:
7445      block-declaration  */
7446
7447 static void
7448 cp_parser_declaration_statement (cp_parser* parser)
7449 {
7450   void *p;
7451
7452   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7453   p = obstack_alloc (&declarator_obstack, 0);
7454
7455  /* Parse the block-declaration.  */
7456   cp_parser_block_declaration (parser, /*statement_p=*/true);
7457
7458   /* Free any declarators allocated.  */
7459   obstack_free (&declarator_obstack, p);
7460
7461   /* Finish off the statement.  */
7462   finish_stmt ();
7463 }
7464
7465 /* Some dependent statements (like `if (cond) statement'), are
7466    implicitly in their own scope.  In other words, if the statement is
7467    a single statement (as opposed to a compound-statement), it is
7468    none-the-less treated as if it were enclosed in braces.  Any
7469    declarations appearing in the dependent statement are out of scope
7470    after control passes that point.  This function parses a statement,
7471    but ensures that is in its own scope, even if it is not a
7472    compound-statement.
7473
7474    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7475    is a (possibly labeled) if statement which is not enclosed in
7476    braces and has an else clause.  This is used to implement
7477    -Wparentheses.
7478
7479    Returns the new statement.  */
7480
7481 static tree
7482 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7483 {
7484   tree statement;
7485
7486   if (if_p != NULL)
7487     *if_p = false;
7488
7489   /* Mark if () ; with a special NOP_EXPR.  */
7490   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7491     {
7492       cp_lexer_consume_token (parser->lexer);
7493       statement = add_stmt (build_empty_stmt ());
7494     }
7495   /* if a compound is opened, we simply parse the statement directly.  */
7496   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7497     statement = cp_parser_compound_statement (parser, NULL, false);
7498   /* If the token is not a `{', then we must take special action.  */
7499   else
7500     {
7501       /* Create a compound-statement.  */
7502       statement = begin_compound_stmt (0);
7503       /* Parse the dependent-statement.  */
7504       cp_parser_statement (parser, NULL_TREE, false, if_p);
7505       /* Finish the dummy compound-statement.  */
7506       finish_compound_stmt (statement);
7507     }
7508
7509   /* Return the statement.  */
7510   return statement;
7511 }
7512
7513 /* For some dependent statements (like `while (cond) statement'), we
7514    have already created a scope.  Therefore, even if the dependent
7515    statement is a compound-statement, we do not want to create another
7516    scope.  */
7517
7518 static void
7519 cp_parser_already_scoped_statement (cp_parser* parser)
7520 {
7521   /* If the token is a `{', then we must take special action.  */
7522   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7523     cp_parser_statement (parser, NULL_TREE, false, NULL);
7524   else
7525     {
7526       /* Avoid calling cp_parser_compound_statement, so that we
7527          don't create a new scope.  Do everything else by hand.  */
7528       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7529       cp_parser_statement_seq_opt (parser, NULL_TREE);
7530       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7531     }
7532 }
7533
7534 /* Declarations [gram.dcl.dcl] */
7535
7536 /* Parse an optional declaration-sequence.
7537
7538    declaration-seq:
7539      declaration
7540      declaration-seq declaration  */
7541
7542 static void
7543 cp_parser_declaration_seq_opt (cp_parser* parser)
7544 {
7545   while (true)
7546     {
7547       cp_token *token;
7548
7549       token = cp_lexer_peek_token (parser->lexer);
7550
7551       if (token->type == CPP_CLOSE_BRACE
7552           || token->type == CPP_EOF
7553           || token->type == CPP_PRAGMA_EOL)
7554         break;
7555
7556       if (token->type == CPP_SEMICOLON)
7557         {
7558           /* A declaration consisting of a single semicolon is
7559              invalid.  Allow it unless we're being pedantic.  */
7560           cp_lexer_consume_token (parser->lexer);
7561           if (pedantic && !in_system_header)
7562             pedwarn ("extra %<;%>");
7563           continue;
7564         }
7565
7566       /* If we're entering or exiting a region that's implicitly
7567          extern "C", modify the lang context appropriately.  */
7568       if (!parser->implicit_extern_c && token->implicit_extern_c)
7569         {
7570           push_lang_context (lang_name_c);
7571           parser->implicit_extern_c = true;
7572         }
7573       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7574         {
7575           pop_lang_context ();
7576           parser->implicit_extern_c = false;
7577         }
7578
7579       if (token->type == CPP_PRAGMA)
7580         {
7581           /* A top-level declaration can consist solely of a #pragma.
7582              A nested declaration cannot, so this is done here and not
7583              in cp_parser_declaration.  (A #pragma at block scope is
7584              handled in cp_parser_statement.)  */
7585           cp_parser_pragma (parser, pragma_external);
7586           continue;
7587         }
7588
7589       /* Parse the declaration itself.  */
7590       cp_parser_declaration (parser);
7591     }
7592 }
7593
7594 /* Parse a declaration.
7595
7596    declaration:
7597      block-declaration
7598      function-definition
7599      template-declaration
7600      explicit-instantiation
7601      explicit-specialization
7602      linkage-specification
7603      namespace-definition
7604
7605    GNU extension:
7606
7607    declaration:
7608       __extension__ declaration */
7609
7610 static void
7611 cp_parser_declaration (cp_parser* parser)
7612 {
7613   cp_token token1;
7614   cp_token token2;
7615   int saved_pedantic;
7616   void *p;
7617
7618   /* Check for the `__extension__' keyword.  */
7619   if (cp_parser_extension_opt (parser, &saved_pedantic))
7620     {
7621       /* Parse the qualified declaration.  */
7622       cp_parser_declaration (parser);
7623       /* Restore the PEDANTIC flag.  */
7624       pedantic = saved_pedantic;
7625
7626       return;
7627     }
7628
7629   /* Try to figure out what kind of declaration is present.  */
7630   token1 = *cp_lexer_peek_token (parser->lexer);
7631
7632   if (token1.type != CPP_EOF)
7633     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7634   else
7635     {
7636       token2.type = CPP_EOF;
7637       token2.keyword = RID_MAX;
7638     }
7639
7640   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7641   p = obstack_alloc (&declarator_obstack, 0);
7642
7643   /* If the next token is `extern' and the following token is a string
7644      literal, then we have a linkage specification.  */
7645   if (token1.keyword == RID_EXTERN
7646       && cp_parser_is_string_literal (&token2))
7647     cp_parser_linkage_specification (parser);
7648   /* If the next token is `template', then we have either a template
7649      declaration, an explicit instantiation, or an explicit
7650      specialization.  */
7651   else if (token1.keyword == RID_TEMPLATE)
7652     {
7653       /* `template <>' indicates a template specialization.  */
7654       if (token2.type == CPP_LESS
7655           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7656         cp_parser_explicit_specialization (parser);
7657       /* `template <' indicates a template declaration.  */
7658       else if (token2.type == CPP_LESS)
7659         cp_parser_template_declaration (parser, /*member_p=*/false);
7660       /* Anything else must be an explicit instantiation.  */
7661       else
7662         cp_parser_explicit_instantiation (parser);
7663     }
7664   /* If the next token is `export', then we have a template
7665      declaration.  */
7666   else if (token1.keyword == RID_EXPORT)
7667     cp_parser_template_declaration (parser, /*member_p=*/false);
7668   /* If the next token is `extern', 'static' or 'inline' and the one
7669      after that is `template', we have a GNU extended explicit
7670      instantiation directive.  */
7671   else if (cp_parser_allow_gnu_extensions_p (parser)
7672            && (token1.keyword == RID_EXTERN
7673                || token1.keyword == RID_STATIC
7674                || token1.keyword == RID_INLINE)
7675            && token2.keyword == RID_TEMPLATE)
7676     cp_parser_explicit_instantiation (parser);
7677   /* If the next token is `namespace', check for a named or unnamed
7678      namespace definition.  */
7679   else if (token1.keyword == RID_NAMESPACE
7680            && (/* A named namespace definition.  */
7681                (token2.type == CPP_NAME
7682                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7683                     != CPP_EQ))
7684                /* An unnamed namespace definition.  */
7685                || token2.type == CPP_OPEN_BRACE
7686                || token2.keyword == RID_ATTRIBUTE))
7687     cp_parser_namespace_definition (parser);
7688   /* Objective-C++ declaration/definition.  */
7689   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7690     cp_parser_objc_declaration (parser);
7691   /* We must have either a block declaration or a function
7692      definition.  */
7693   else
7694     /* Try to parse a block-declaration, or a function-definition.  */
7695     cp_parser_block_declaration (parser, /*statement_p=*/false);
7696
7697   /* Free any declarators allocated.  */
7698   obstack_free (&declarator_obstack, p);
7699 }
7700
7701 /* Parse a block-declaration.
7702
7703    block-declaration:
7704      simple-declaration
7705      asm-definition
7706      namespace-alias-definition
7707      using-declaration
7708      using-directive
7709
7710    GNU Extension:
7711
7712    block-declaration:
7713      __extension__ block-declaration
7714      label-declaration
7715
7716    C++0x Extension:
7717
7718    block-declaration:
7719      static_assert-declaration
7720
7721    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7722    part of a declaration-statement.  */
7723
7724 static void
7725 cp_parser_block_declaration (cp_parser *parser,
7726                              bool      statement_p)
7727 {
7728   cp_token *token1;
7729   int saved_pedantic;
7730
7731   /* Check for the `__extension__' keyword.  */
7732   if (cp_parser_extension_opt (parser, &saved_pedantic))
7733     {
7734       /* Parse the qualified declaration.  */
7735       cp_parser_block_declaration (parser, statement_p);
7736       /* Restore the PEDANTIC flag.  */
7737       pedantic = saved_pedantic;
7738
7739       return;
7740     }
7741
7742   /* Peek at the next token to figure out which kind of declaration is
7743      present.  */
7744   token1 = cp_lexer_peek_token (parser->lexer);
7745
7746   /* If the next keyword is `asm', we have an asm-definition.  */
7747   if (token1->keyword == RID_ASM)
7748     {
7749       if (statement_p)
7750         cp_parser_commit_to_tentative_parse (parser);
7751       cp_parser_asm_definition (parser);
7752     }
7753   /* If the next keyword is `namespace', we have a
7754      namespace-alias-definition.  */
7755   else if (token1->keyword == RID_NAMESPACE)
7756     cp_parser_namespace_alias_definition (parser);
7757   /* If the next keyword is `using', we have either a
7758      using-declaration or a using-directive.  */
7759   else if (token1->keyword == RID_USING)
7760     {
7761       cp_token *token2;
7762
7763       if (statement_p)
7764         cp_parser_commit_to_tentative_parse (parser);
7765       /* If the token after `using' is `namespace', then we have a
7766          using-directive.  */
7767       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7768       if (token2->keyword == RID_NAMESPACE)
7769         cp_parser_using_directive (parser);
7770       /* Otherwise, it's a using-declaration.  */
7771       else
7772         cp_parser_using_declaration (parser,
7773                                      /*access_declaration_p=*/false);
7774     }
7775   /* If the next keyword is `__label__' we have a label declaration.  */
7776   else if (token1->keyword == RID_LABEL)
7777     {
7778       if (statement_p)
7779         cp_parser_commit_to_tentative_parse (parser);
7780       cp_parser_label_declaration (parser);
7781     }
7782   /* If the next token is `static_assert' we have a static assertion.  */
7783   else if (token1->keyword == RID_STATIC_ASSERT)
7784     cp_parser_static_assert (parser, /*member_p=*/false);
7785   /* Anything else must be a simple-declaration.  */
7786   else
7787     cp_parser_simple_declaration (parser, !statement_p);
7788 }
7789
7790 /* Parse a simple-declaration.
7791
7792    simple-declaration:
7793      decl-specifier-seq [opt] init-declarator-list [opt] ;
7794
7795    init-declarator-list:
7796      init-declarator
7797      init-declarator-list , init-declarator
7798
7799    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7800    function-definition as a simple-declaration.  */
7801
7802 static void
7803 cp_parser_simple_declaration (cp_parser* parser,
7804                               bool function_definition_allowed_p)
7805 {
7806   cp_decl_specifier_seq decl_specifiers;
7807   int declares_class_or_enum;
7808   bool saw_declarator;
7809
7810   /* Defer access checks until we know what is being declared; the
7811      checks for names appearing in the decl-specifier-seq should be
7812      done as if we were in the scope of the thing being declared.  */
7813   push_deferring_access_checks (dk_deferred);
7814
7815   /* Parse the decl-specifier-seq.  We have to keep track of whether
7816      or not the decl-specifier-seq declares a named class or
7817      enumeration type, since that is the only case in which the
7818      init-declarator-list is allowed to be empty.
7819
7820      [dcl.dcl]
7821
7822      In a simple-declaration, the optional init-declarator-list can be
7823      omitted only when declaring a class or enumeration, that is when
7824      the decl-specifier-seq contains either a class-specifier, an
7825      elaborated-type-specifier, or an enum-specifier.  */
7826   cp_parser_decl_specifier_seq (parser,
7827                                 CP_PARSER_FLAGS_OPTIONAL,
7828                                 &decl_specifiers,
7829                                 &declares_class_or_enum);
7830   /* We no longer need to defer access checks.  */
7831   stop_deferring_access_checks ();
7832
7833   /* In a block scope, a valid declaration must always have a
7834      decl-specifier-seq.  By not trying to parse declarators, we can
7835      resolve the declaration/expression ambiguity more quickly.  */
7836   if (!function_definition_allowed_p
7837       && !decl_specifiers.any_specifiers_p)
7838     {
7839       cp_parser_error (parser, "expected declaration");
7840       goto done;
7841     }
7842
7843   /* If the next two tokens are both identifiers, the code is
7844      erroneous. The usual cause of this situation is code like:
7845
7846        T t;
7847
7848      where "T" should name a type -- but does not.  */
7849   if (!decl_specifiers.type
7850       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7851     {
7852       /* If parsing tentatively, we should commit; we really are
7853          looking at a declaration.  */
7854       cp_parser_commit_to_tentative_parse (parser);
7855       /* Give up.  */
7856       goto done;
7857     }
7858
7859   /* If we have seen at least one decl-specifier, and the next token
7860      is not a parenthesis, then we must be looking at a declaration.
7861      (After "int (" we might be looking at a functional cast.)  */
7862   if (decl_specifiers.any_specifiers_p
7863       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7864     cp_parser_commit_to_tentative_parse (parser);
7865
7866   /* Keep going until we hit the `;' at the end of the simple
7867      declaration.  */
7868   saw_declarator = false;
7869   while (cp_lexer_next_token_is_not (parser->lexer,
7870                                      CPP_SEMICOLON))
7871     {
7872       cp_token *token;
7873       bool function_definition_p;
7874       tree decl;
7875
7876       if (saw_declarator)
7877         {
7878           /* If we are processing next declarator, coma is expected */
7879           token = cp_lexer_peek_token (parser->lexer);
7880           gcc_assert (token->type == CPP_COMMA);
7881           cp_lexer_consume_token (parser->lexer);
7882         }
7883       else
7884         saw_declarator = true;
7885
7886       /* Parse the init-declarator.  */
7887       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7888                                         /*checks=*/NULL,
7889                                         function_definition_allowed_p,
7890                                         /*member_p=*/false,
7891                                         declares_class_or_enum,
7892                                         &function_definition_p);
7893       /* If an error occurred while parsing tentatively, exit quickly.
7894          (That usually happens when in the body of a function; each
7895          statement is treated as a declaration-statement until proven
7896          otherwise.)  */
7897       if (cp_parser_error_occurred (parser))
7898         goto done;
7899       /* Handle function definitions specially.  */
7900       if (function_definition_p)
7901         {
7902           /* If the next token is a `,', then we are probably
7903              processing something like:
7904
7905                void f() {}, *p;
7906
7907              which is erroneous.  */
7908           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7909             error ("mixing declarations and function-definitions is forbidden");
7910           /* Otherwise, we're done with the list of declarators.  */
7911           else
7912             {
7913               pop_deferring_access_checks ();
7914               return;
7915             }
7916         }
7917       /* The next token should be either a `,' or a `;'.  */
7918       token = cp_lexer_peek_token (parser->lexer);
7919       /* If it's a `,', there are more declarators to come.  */
7920       if (token->type == CPP_COMMA)
7921         /* will be consumed next time around */;
7922       /* If it's a `;', we are done.  */
7923       else if (token->type == CPP_SEMICOLON)
7924         break;
7925       /* Anything else is an error.  */
7926       else
7927         {
7928           /* If we have already issued an error message we don't need
7929              to issue another one.  */
7930           if (decl != error_mark_node
7931               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7932             cp_parser_error (parser, "expected %<,%> or %<;%>");
7933           /* Skip tokens until we reach the end of the statement.  */
7934           cp_parser_skip_to_end_of_statement (parser);
7935           /* If the next token is now a `;', consume it.  */
7936           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7937             cp_lexer_consume_token (parser->lexer);
7938           goto done;
7939         }
7940       /* After the first time around, a function-definition is not
7941          allowed -- even if it was OK at first.  For example:
7942
7943            int i, f() {}
7944
7945          is not valid.  */
7946       function_definition_allowed_p = false;
7947     }
7948
7949   /* Issue an error message if no declarators are present, and the
7950      decl-specifier-seq does not itself declare a class or
7951      enumeration.  */
7952   if (!saw_declarator)
7953     {
7954       if (cp_parser_declares_only_class_p (parser))
7955         shadow_tag (&decl_specifiers);
7956       /* Perform any deferred access checks.  */
7957       perform_deferred_access_checks ();
7958     }
7959
7960   /* Consume the `;'.  */
7961   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7962
7963  done:
7964   pop_deferring_access_checks ();
7965 }
7966
7967 /* Parse a decl-specifier-seq.
7968
7969    decl-specifier-seq:
7970      decl-specifier-seq [opt] decl-specifier
7971
7972    decl-specifier:
7973      storage-class-specifier
7974      type-specifier
7975      function-specifier
7976      friend
7977      typedef
7978
7979    GNU Extension:
7980
7981    decl-specifier:
7982      attributes
7983
7984    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7985
7986    The parser flags FLAGS is used to control type-specifier parsing.
7987
7988    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7989    flags:
7990
7991      1: one of the decl-specifiers is an elaborated-type-specifier
7992         (i.e., a type declaration)
7993      2: one of the decl-specifiers is an enum-specifier or a
7994         class-specifier (i.e., a type definition)
7995
7996    */
7997
7998 static void
7999 cp_parser_decl_specifier_seq (cp_parser* parser,
8000                               cp_parser_flags flags,
8001                               cp_decl_specifier_seq *decl_specs,
8002                               int* declares_class_or_enum)
8003 {
8004   bool constructor_possible_p = !parser->in_declarator_p;
8005
8006   /* Clear DECL_SPECS.  */
8007   clear_decl_specs (decl_specs);
8008
8009   /* Assume no class or enumeration type is declared.  */
8010   *declares_class_or_enum = 0;
8011
8012   /* Keep reading specifiers until there are no more to read.  */
8013   while (true)
8014     {
8015       bool constructor_p;
8016       bool found_decl_spec;
8017       cp_token *token;
8018
8019       /* Peek at the next token.  */
8020       token = cp_lexer_peek_token (parser->lexer);
8021       /* Handle attributes.  */
8022       if (token->keyword == RID_ATTRIBUTE)
8023         {
8024           /* Parse the attributes.  */
8025           decl_specs->attributes
8026             = chainon (decl_specs->attributes,
8027                        cp_parser_attributes_opt (parser));
8028           continue;
8029         }
8030       /* Assume we will find a decl-specifier keyword.  */
8031       found_decl_spec = true;
8032       /* If the next token is an appropriate keyword, we can simply
8033          add it to the list.  */
8034       switch (token->keyword)
8035         {
8036           /* decl-specifier:
8037                friend  */
8038         case RID_FRIEND:
8039           if (!at_class_scope_p ())
8040             {
8041               error ("%<friend%> used outside of class");
8042               cp_lexer_purge_token (parser->lexer);
8043             }
8044           else
8045             {
8046               ++decl_specs->specs[(int) ds_friend];
8047               /* Consume the token.  */
8048               cp_lexer_consume_token (parser->lexer);
8049             }
8050           break;
8051
8052           /* function-specifier:
8053                inline
8054                virtual
8055                explicit  */
8056         case RID_INLINE:
8057         case RID_VIRTUAL:
8058         case RID_EXPLICIT:
8059           cp_parser_function_specifier_opt (parser, decl_specs);
8060           break;
8061
8062           /* decl-specifier:
8063                typedef  */
8064         case RID_TYPEDEF:
8065           ++decl_specs->specs[(int) ds_typedef];
8066           /* Consume the token.  */
8067           cp_lexer_consume_token (parser->lexer);
8068           /* A constructor declarator cannot appear in a typedef.  */
8069           constructor_possible_p = false;
8070           /* The "typedef" keyword can only occur in a declaration; we
8071              may as well commit at this point.  */
8072           cp_parser_commit_to_tentative_parse (parser);
8073
8074           if (decl_specs->storage_class != sc_none)
8075             decl_specs->conflicting_specifiers_p = true;
8076           break;
8077
8078           /* storage-class-specifier:
8079                auto
8080                register
8081                static
8082                extern
8083                mutable
8084
8085              GNU Extension:
8086                thread  */
8087         case RID_AUTO:
8088         case RID_REGISTER:
8089         case RID_STATIC:
8090         case RID_EXTERN:
8091         case RID_MUTABLE:
8092           /* Consume the token.  */
8093           cp_lexer_consume_token (parser->lexer);
8094           cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8095           break;
8096         case RID_THREAD:
8097           /* Consume the token.  */
8098           cp_lexer_consume_token (parser->lexer);
8099           ++decl_specs->specs[(int) ds_thread];
8100           break;
8101
8102         default:
8103           /* We did not yet find a decl-specifier yet.  */
8104           found_decl_spec = false;
8105           break;
8106         }
8107
8108       /* Constructors are a special case.  The `S' in `S()' is not a
8109          decl-specifier; it is the beginning of the declarator.  */
8110       constructor_p
8111         = (!found_decl_spec
8112            && constructor_possible_p
8113            && (cp_parser_constructor_declarator_p
8114                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8115
8116       /* If we don't have a DECL_SPEC yet, then we must be looking at
8117          a type-specifier.  */
8118       if (!found_decl_spec && !constructor_p)
8119         {
8120           int decl_spec_declares_class_or_enum;
8121           bool is_cv_qualifier;
8122           tree type_spec;
8123
8124           type_spec
8125             = cp_parser_type_specifier (parser, flags,
8126                                         decl_specs,
8127                                         /*is_declaration=*/true,
8128                                         &decl_spec_declares_class_or_enum,
8129                                         &is_cv_qualifier);
8130
8131           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8132
8133           /* If this type-specifier referenced a user-defined type
8134              (a typedef, class-name, etc.), then we can't allow any
8135              more such type-specifiers henceforth.
8136
8137              [dcl.spec]
8138
8139              The longest sequence of decl-specifiers that could
8140              possibly be a type name is taken as the
8141              decl-specifier-seq of a declaration.  The sequence shall
8142              be self-consistent as described below.
8143
8144              [dcl.type]
8145
8146              As a general rule, at most one type-specifier is allowed
8147              in the complete decl-specifier-seq of a declaration.  The
8148              only exceptions are the following:
8149
8150              -- const or volatile can be combined with any other
8151                 type-specifier.
8152
8153              -- signed or unsigned can be combined with char, long,
8154                 short, or int.
8155
8156              -- ..
8157
8158              Example:
8159
8160                typedef char* Pc;
8161                void g (const int Pc);
8162
8163              Here, Pc is *not* part of the decl-specifier seq; it's
8164              the declarator.  Therefore, once we see a type-specifier
8165              (other than a cv-qualifier), we forbid any additional
8166              user-defined types.  We *do* still allow things like `int
8167              int' to be considered a decl-specifier-seq, and issue the
8168              error message later.  */
8169           if (type_spec && !is_cv_qualifier)
8170             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8171           /* A constructor declarator cannot follow a type-specifier.  */
8172           if (type_spec)
8173             {
8174               constructor_possible_p = false;
8175               found_decl_spec = true;
8176             }
8177         }
8178
8179       /* If we still do not have a DECL_SPEC, then there are no more
8180          decl-specifiers.  */
8181       if (!found_decl_spec)
8182         break;
8183
8184       decl_specs->any_specifiers_p = true;
8185       /* After we see one decl-specifier, further decl-specifiers are
8186          always optional.  */
8187       flags |= CP_PARSER_FLAGS_OPTIONAL;
8188     }
8189
8190   cp_parser_check_decl_spec (decl_specs);
8191
8192   /* Don't allow a friend specifier with a class definition.  */
8193   if (decl_specs->specs[(int) ds_friend] != 0
8194       && (*declares_class_or_enum & 2))
8195     error ("class definition may not be declared a friend");
8196 }
8197
8198 /* Parse an (optional) storage-class-specifier.
8199
8200    storage-class-specifier:
8201      auto
8202      register
8203      static
8204      extern
8205      mutable
8206
8207    GNU Extension:
8208
8209    storage-class-specifier:
8210      thread
8211
8212    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8213
8214 static tree
8215 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8216 {
8217   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8218     {
8219     case RID_AUTO:
8220     case RID_REGISTER:
8221     case RID_STATIC:
8222     case RID_EXTERN:
8223     case RID_MUTABLE:
8224     case RID_THREAD:
8225       /* Consume the token.  */
8226       return cp_lexer_consume_token (parser->lexer)->u.value;
8227
8228     default:
8229       return NULL_TREE;
8230     }
8231 }
8232
8233 /* Parse an (optional) function-specifier.
8234
8235    function-specifier:
8236      inline
8237      virtual
8238      explicit
8239
8240    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8241    Updates DECL_SPECS, if it is non-NULL.  */
8242
8243 static tree
8244 cp_parser_function_specifier_opt (cp_parser* parser,
8245                                   cp_decl_specifier_seq *decl_specs)
8246 {
8247   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8248     {
8249     case RID_INLINE:
8250       if (decl_specs)
8251         ++decl_specs->specs[(int) ds_inline];
8252       break;
8253
8254     case RID_VIRTUAL:
8255       /* 14.5.2.3 [temp.mem]
8256
8257          A member function template shall not be virtual.  */
8258       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8259         error ("templates may not be %<virtual%>");
8260       else if (decl_specs)
8261         ++decl_specs->specs[(int) ds_virtual];
8262       break;
8263
8264     case RID_EXPLICIT:
8265       if (decl_specs)
8266         ++decl_specs->specs[(int) ds_explicit];
8267       break;
8268
8269     default:
8270       return NULL_TREE;
8271     }
8272
8273   /* Consume the token.  */
8274   return cp_lexer_consume_token (parser->lexer)->u.value;
8275 }
8276
8277 /* Parse a linkage-specification.
8278
8279    linkage-specification:
8280      extern string-literal { declaration-seq [opt] }
8281      extern string-literal declaration  */
8282
8283 static void
8284 cp_parser_linkage_specification (cp_parser* parser)
8285 {
8286   tree linkage;
8287
8288   /* Look for the `extern' keyword.  */
8289   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8290
8291   /* Look for the string-literal.  */
8292   linkage = cp_parser_string_literal (parser, false, false);
8293
8294   /* Transform the literal into an identifier.  If the literal is a
8295      wide-character string, or contains embedded NULs, then we can't
8296      handle it as the user wants.  */
8297   if (strlen (TREE_STRING_POINTER (linkage))
8298       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8299     {
8300       cp_parser_error (parser, "invalid linkage-specification");
8301       /* Assume C++ linkage.  */
8302       linkage = lang_name_cplusplus;
8303     }
8304   else
8305     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8306
8307   /* We're now using the new linkage.  */
8308   push_lang_context (linkage);
8309
8310   /* If the next token is a `{', then we're using the first
8311      production.  */
8312   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8313     {
8314       /* Consume the `{' token.  */
8315       cp_lexer_consume_token (parser->lexer);
8316       /* Parse the declarations.  */
8317       cp_parser_declaration_seq_opt (parser);
8318       /* Look for the closing `}'.  */
8319       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8320     }
8321   /* Otherwise, there's just one declaration.  */
8322   else
8323     {
8324       bool saved_in_unbraced_linkage_specification_p;
8325
8326       saved_in_unbraced_linkage_specification_p
8327         = parser->in_unbraced_linkage_specification_p;
8328       parser->in_unbraced_linkage_specification_p = true;
8329       cp_parser_declaration (parser);
8330       parser->in_unbraced_linkage_specification_p
8331         = saved_in_unbraced_linkage_specification_p;
8332     }
8333
8334   /* We're done with the linkage-specification.  */
8335   pop_lang_context ();
8336 }
8337
8338 /* Parse a static_assert-declaration.
8339
8340    static_assert-declaration:
8341      static_assert ( constant-expression , string-literal ) ; 
8342
8343    If MEMBER_P, this static_assert is a class member.  */
8344
8345 static void 
8346 cp_parser_static_assert(cp_parser *parser, bool member_p)
8347 {
8348   tree condition;
8349   tree message;
8350   cp_token *token;
8351   location_t saved_loc;
8352
8353   /* Peek at the `static_assert' token so we can keep track of exactly
8354      where the static assertion started.  */
8355   token = cp_lexer_peek_token (parser->lexer);
8356   saved_loc = token->location;
8357
8358   /* Look for the `static_assert' keyword.  */
8359   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8360                                   "`static_assert'"))
8361     return;
8362
8363   /*  We know we are in a static assertion; commit to any tentative
8364       parse.  */
8365   if (cp_parser_parsing_tentatively (parser))
8366     cp_parser_commit_to_tentative_parse (parser);
8367
8368   /* Parse the `(' starting the static assertion condition.  */
8369   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8370
8371   /* Parse the constant-expression.  */
8372   condition = 
8373     cp_parser_constant_expression (parser,
8374                                    /*allow_non_constant_p=*/false,
8375                                    /*non_constant_p=*/NULL);
8376
8377   /* Parse the separating `,'.  */
8378   cp_parser_require (parser, CPP_COMMA, "`,'");
8379
8380   /* Parse the string-literal message.  */
8381   message = cp_parser_string_literal (parser, 
8382                                       /*translate=*/false,
8383                                       /*wide_ok=*/true);
8384
8385   /* A `)' completes the static assertion.  */
8386   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8387     cp_parser_skip_to_closing_parenthesis (parser, 
8388                                            /*recovering=*/true, 
8389                                            /*or_comma=*/false,
8390                                            /*consume_paren=*/true);
8391
8392   /* A semicolon terminates the declaration.  */
8393   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8394
8395   /* Complete the static assertion, which may mean either processing 
8396      the static assert now or saving it for template instantiation.  */
8397   finish_static_assert (condition, message, saved_loc, member_p);
8398 }
8399
8400 /* Parse a `decltype' type. Returns the type. 
8401
8402    simple-type-specifier:
8403      decltype ( expression )  */
8404
8405 static tree
8406 cp_parser_decltype (cp_parser *parser)
8407 {
8408   tree expr;
8409   bool id_expression_or_member_access_p = false;
8410   const char *saved_message;
8411   bool saved_integral_constant_expression_p;
8412   bool saved_non_integral_constant_expression_p;
8413
8414   /* Look for the `decltype' token.  */
8415   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "`decltype'"))
8416     return error_mark_node;
8417
8418   /* Types cannot be defined in a `decltype' expression.  Save away the
8419      old message.  */
8420   saved_message = parser->type_definition_forbidden_message;
8421
8422   /* And create the new one.  */
8423   parser->type_definition_forbidden_message
8424     = "types may not be defined in `decltype' expressions";
8425
8426   /* The restrictions on constant-expressions do not apply inside
8427      decltype expressions.  */
8428   saved_integral_constant_expression_p
8429     = parser->integral_constant_expression_p;
8430   saved_non_integral_constant_expression_p
8431     = parser->non_integral_constant_expression_p;
8432   parser->integral_constant_expression_p = false;
8433
8434   /* Do not actually evaluate the expression.  */
8435   ++skip_evaluation;
8436
8437   /* Parse the opening `('.  */
8438   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8439   
8440   /* First, try parsing an id-expression.  */
8441   cp_parser_parse_tentatively (parser);
8442   expr = cp_parser_id_expression (parser,
8443                                   /*template_keyword_p=*/false,
8444                                   /*check_dependency_p=*/true,
8445                                   /*template_p=*/NULL,
8446                                   /*declarator_p=*/false,
8447                                   /*optional_p=*/false);
8448
8449   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8450     {
8451       bool non_integral_constant_expression_p = false;
8452       tree id_expression = expr;
8453       cp_id_kind idk;
8454       const char *error_msg;
8455
8456       /* Lookup the name we got back from the id-expression.  */
8457       expr = cp_parser_lookup_name (parser, expr,
8458                                     none_type,
8459                                     /*is_template=*/false,
8460                                     /*is_namespace=*/false,
8461                                     /*check_dependency=*/true,
8462                                     /*ambiguous_decls=*/NULL);
8463       
8464       if (expr 
8465           && expr != error_mark_node
8466           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8467           && TREE_CODE (expr) != TYPE_DECL
8468           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8469         {
8470           /* Complete lookup of the id-expression.  */
8471           expr = (finish_id_expression
8472                   (id_expression, expr, parser->scope, &idk,
8473                    /*integral_constant_expression_p=*/false,
8474                    /*allow_non_integral_constant_expression_p=*/true,
8475                    &non_integral_constant_expression_p,
8476                    /*template_p=*/false,
8477                    /*done=*/true,
8478                    /*address_p=*/false,
8479                    /*template_arg_p=*/false,
8480                    &error_msg));
8481
8482           if (expr == error_mark_node)
8483             /* We found an id-expression, but it was something that we
8484                should not have found. This is an error, not something
8485                we can recover from, so note that we found an
8486                id-expression and we'll recover as gracefully as
8487                possible.  */
8488             id_expression_or_member_access_p = true;
8489         }
8490
8491       if (expr 
8492           && expr != error_mark_node
8493           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8494         /* We have an id-expression.  */
8495         id_expression_or_member_access_p = true;
8496     }
8497
8498   if (!id_expression_or_member_access_p)
8499     {
8500       /* Abort the id-expression parse.  */
8501       cp_parser_abort_tentative_parse (parser);
8502
8503       /* Parsing tentatively, again.  */
8504       cp_parser_parse_tentatively (parser);
8505
8506       /* Parse a class member access.  */
8507       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8508                                            /*cast_p=*/false,
8509                                            /*member_access_only_p=*/true);
8510
8511       if (expr 
8512           && expr != error_mark_node
8513           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8514         /* We have an id-expression.  */
8515         id_expression_or_member_access_p = true;
8516     }
8517
8518   if (id_expression_or_member_access_p)
8519     /* We have parsed the complete id-expression or member access.  */
8520     cp_parser_parse_definitely (parser);
8521   else
8522     {
8523       /* Abort our attempt to parse an id-expression or member access
8524          expression.  */
8525       cp_parser_abort_tentative_parse (parser);
8526
8527       /* Parse a full expression.  */
8528       expr = cp_parser_expression (parser, /*cast_p=*/false);
8529     }
8530
8531   /* Go back to evaluating expressions.  */
8532   --skip_evaluation;
8533
8534   /* Restore the old message and the integral constant expression
8535      flags.  */
8536   parser->type_definition_forbidden_message = saved_message;
8537   parser->integral_constant_expression_p
8538     = saved_integral_constant_expression_p;
8539   parser->non_integral_constant_expression_p
8540     = saved_non_integral_constant_expression_p;
8541
8542   if (expr == error_mark_node)
8543     {
8544       /* Skip everything up to the closing `)'.  */
8545       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8546                                              /*consume_paren=*/true);
8547       return error_mark_node;
8548     }
8549   
8550   /* Parse to the closing `)'.  */
8551   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8552     cp_parser_skip_to_closing_parenthesis (parser, true, false,
8553                                            /*consume_paren=*/true);
8554
8555   return finish_decltype_type (expr, id_expression_or_member_access_p);
8556 }
8557
8558 /* Special member functions [gram.special] */
8559
8560 /* Parse a conversion-function-id.
8561
8562    conversion-function-id:
8563      operator conversion-type-id
8564
8565    Returns an IDENTIFIER_NODE representing the operator.  */
8566
8567 static tree
8568 cp_parser_conversion_function_id (cp_parser* parser)
8569 {
8570   tree type;
8571   tree saved_scope;
8572   tree saved_qualifying_scope;
8573   tree saved_object_scope;
8574   tree pushed_scope = NULL_TREE;
8575
8576   /* Look for the `operator' token.  */
8577   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8578     return error_mark_node;
8579   /* When we parse the conversion-type-id, the current scope will be
8580      reset.  However, we need that information in able to look up the
8581      conversion function later, so we save it here.  */
8582   saved_scope = parser->scope;
8583   saved_qualifying_scope = parser->qualifying_scope;
8584   saved_object_scope = parser->object_scope;
8585   /* We must enter the scope of the class so that the names of
8586      entities declared within the class are available in the
8587      conversion-type-id.  For example, consider:
8588
8589        struct S {
8590          typedef int I;
8591          operator I();
8592        };
8593
8594        S::operator I() { ... }
8595
8596      In order to see that `I' is a type-name in the definition, we
8597      must be in the scope of `S'.  */
8598   if (saved_scope)
8599     pushed_scope = push_scope (saved_scope);
8600   /* Parse the conversion-type-id.  */
8601   type = cp_parser_conversion_type_id (parser);
8602   /* Leave the scope of the class, if any.  */
8603   if (pushed_scope)
8604     pop_scope (pushed_scope);
8605   /* Restore the saved scope.  */
8606   parser->scope = saved_scope;
8607   parser->qualifying_scope = saved_qualifying_scope;
8608   parser->object_scope = saved_object_scope;
8609   /* If the TYPE is invalid, indicate failure.  */
8610   if (type == error_mark_node)
8611     return error_mark_node;
8612   return mangle_conv_op_name_for_type (type);
8613 }
8614
8615 /* Parse a conversion-type-id:
8616
8617    conversion-type-id:
8618      type-specifier-seq conversion-declarator [opt]
8619
8620    Returns the TYPE specified.  */
8621
8622 static tree
8623 cp_parser_conversion_type_id (cp_parser* parser)
8624 {
8625   tree attributes;
8626   cp_decl_specifier_seq type_specifiers;
8627   cp_declarator *declarator;
8628   tree type_specified;
8629
8630   /* Parse the attributes.  */
8631   attributes = cp_parser_attributes_opt (parser);
8632   /* Parse the type-specifiers.  */
8633   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8634                                 &type_specifiers);
8635   /* If that didn't work, stop.  */
8636   if (type_specifiers.type == error_mark_node)
8637     return error_mark_node;
8638   /* Parse the conversion-declarator.  */
8639   declarator = cp_parser_conversion_declarator_opt (parser);
8640
8641   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8642                                     /*initialized=*/0, &attributes);
8643   if (attributes)
8644     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8645   return type_specified;
8646 }
8647
8648 /* Parse an (optional) conversion-declarator.
8649
8650    conversion-declarator:
8651      ptr-operator conversion-declarator [opt]
8652
8653    */
8654
8655 static cp_declarator *
8656 cp_parser_conversion_declarator_opt (cp_parser* parser)
8657 {
8658   enum tree_code code;
8659   tree class_type;
8660   cp_cv_quals cv_quals;
8661
8662   /* We don't know if there's a ptr-operator next, or not.  */
8663   cp_parser_parse_tentatively (parser);
8664   /* Try the ptr-operator.  */
8665   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8666   /* If it worked, look for more conversion-declarators.  */
8667   if (cp_parser_parse_definitely (parser))
8668     {
8669       cp_declarator *declarator;
8670
8671       /* Parse another optional declarator.  */
8672       declarator = cp_parser_conversion_declarator_opt (parser);
8673
8674       return cp_parser_make_indirect_declarator
8675         (code, class_type, cv_quals, declarator);
8676    }
8677
8678   return NULL;
8679 }
8680
8681 /* Parse an (optional) ctor-initializer.
8682
8683    ctor-initializer:
8684      : mem-initializer-list
8685
8686    Returns TRUE iff the ctor-initializer was actually present.  */
8687
8688 static bool
8689 cp_parser_ctor_initializer_opt (cp_parser* parser)
8690 {
8691   /* If the next token is not a `:', then there is no
8692      ctor-initializer.  */
8693   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8694     {
8695       /* Do default initialization of any bases and members.  */
8696       if (DECL_CONSTRUCTOR_P (current_function_decl))
8697         finish_mem_initializers (NULL_TREE);
8698
8699       return false;
8700     }
8701
8702   /* Consume the `:' token.  */
8703   cp_lexer_consume_token (parser->lexer);
8704   /* And the mem-initializer-list.  */
8705   cp_parser_mem_initializer_list (parser);
8706
8707   return true;
8708 }
8709
8710 /* Parse a mem-initializer-list.
8711
8712    mem-initializer-list:
8713      mem-initializer ... [opt]
8714      mem-initializer ... [opt] , mem-initializer-list  */
8715
8716 static void
8717 cp_parser_mem_initializer_list (cp_parser* parser)
8718 {
8719   tree mem_initializer_list = NULL_TREE;
8720
8721   /* Let the semantic analysis code know that we are starting the
8722      mem-initializer-list.  */
8723   if (!DECL_CONSTRUCTOR_P (current_function_decl))
8724     error ("only constructors take base initializers");
8725
8726   /* Loop through the list.  */
8727   while (true)
8728     {
8729       tree mem_initializer;
8730
8731       /* Parse the mem-initializer.  */
8732       mem_initializer = cp_parser_mem_initializer (parser);
8733       /* If the next token is a `...', we're expanding member initializers. */
8734       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8735         {
8736           /* Consume the `...'. */
8737           cp_lexer_consume_token (parser->lexer);
8738
8739           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8740              can be expanded but members cannot. */
8741           if (mem_initializer != error_mark_node
8742               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8743             {
8744               error ("cannot expand initializer for member %<%D%>", 
8745                      TREE_PURPOSE (mem_initializer));
8746               mem_initializer = error_mark_node;
8747             }
8748
8749           /* Construct the pack expansion type. */
8750           if (mem_initializer != error_mark_node)
8751             mem_initializer = make_pack_expansion (mem_initializer);
8752         }
8753       /* Add it to the list, unless it was erroneous.  */
8754       if (mem_initializer != error_mark_node)
8755         {
8756           TREE_CHAIN (mem_initializer) = mem_initializer_list;
8757           mem_initializer_list = mem_initializer;
8758         }
8759       /* If the next token is not a `,', we're done.  */
8760       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8761         break;
8762       /* Consume the `,' token.  */
8763       cp_lexer_consume_token (parser->lexer);
8764     }
8765
8766   /* Perform semantic analysis.  */
8767   if (DECL_CONSTRUCTOR_P (current_function_decl))
8768     finish_mem_initializers (mem_initializer_list);
8769 }
8770
8771 /* Parse a mem-initializer.
8772
8773    mem-initializer:
8774      mem-initializer-id ( expression-list [opt] )
8775
8776    GNU extension:
8777
8778    mem-initializer:
8779      ( expression-list [opt] )
8780
8781    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
8782    class) or FIELD_DECL (for a non-static data member) to initialize;
8783    the TREE_VALUE is the expression-list.  An empty initialization
8784    list is represented by void_list_node.  */
8785
8786 static tree
8787 cp_parser_mem_initializer (cp_parser* parser)
8788 {
8789   tree mem_initializer_id;
8790   tree expression_list;
8791   tree member;
8792
8793   /* Find out what is being initialized.  */
8794   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8795     {
8796       pedwarn ("anachronistic old-style base class initializer");
8797       mem_initializer_id = NULL_TREE;
8798     }
8799   else
8800     mem_initializer_id = cp_parser_mem_initializer_id (parser);
8801   member = expand_member_init (mem_initializer_id);
8802   if (member && !DECL_P (member))
8803     in_base_initializer = 1;
8804
8805   expression_list
8806     = cp_parser_parenthesized_expression_list (parser, false,
8807                                                /*cast_p=*/false,
8808                                                /*allow_expansion_p=*/true,
8809                                                /*non_constant_p=*/NULL);
8810   if (expression_list == error_mark_node)
8811     return error_mark_node;
8812   if (!expression_list)
8813     expression_list = void_type_node;
8814
8815   in_base_initializer = 0;
8816
8817   return member ? build_tree_list (member, expression_list) : error_mark_node;
8818 }
8819
8820 /* Parse a mem-initializer-id.
8821
8822    mem-initializer-id:
8823      :: [opt] nested-name-specifier [opt] class-name
8824      identifier
8825
8826    Returns a TYPE indicating the class to be initializer for the first
8827    production.  Returns an IDENTIFIER_NODE indicating the data member
8828    to be initialized for the second production.  */
8829
8830 static tree
8831 cp_parser_mem_initializer_id (cp_parser* parser)
8832 {
8833   bool global_scope_p;
8834   bool nested_name_specifier_p;
8835   bool template_p = false;
8836   tree id;
8837
8838   /* `typename' is not allowed in this context ([temp.res]).  */
8839   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8840     {
8841       error ("keyword %<typename%> not allowed in this context (a qualified "
8842              "member initializer is implicitly a type)");
8843       cp_lexer_consume_token (parser->lexer);
8844     }
8845   /* Look for the optional `::' operator.  */
8846   global_scope_p
8847     = (cp_parser_global_scope_opt (parser,
8848                                    /*current_scope_valid_p=*/false)
8849        != NULL_TREE);
8850   /* Look for the optional nested-name-specifier.  The simplest way to
8851      implement:
8852
8853        [temp.res]
8854
8855        The keyword `typename' is not permitted in a base-specifier or
8856        mem-initializer; in these contexts a qualified name that
8857        depends on a template-parameter is implicitly assumed to be a
8858        type name.
8859
8860      is to assume that we have seen the `typename' keyword at this
8861      point.  */
8862   nested_name_specifier_p
8863     = (cp_parser_nested_name_specifier_opt (parser,
8864                                             /*typename_keyword_p=*/true,
8865                                             /*check_dependency_p=*/true,
8866                                             /*type_p=*/true,
8867                                             /*is_declaration=*/true)
8868        != NULL_TREE);
8869   if (nested_name_specifier_p)
8870     template_p = cp_parser_optional_template_keyword (parser);
8871   /* If there is a `::' operator or a nested-name-specifier, then we
8872      are definitely looking for a class-name.  */
8873   if (global_scope_p || nested_name_specifier_p)
8874     return cp_parser_class_name (parser,
8875                                  /*typename_keyword_p=*/true,
8876                                  /*template_keyword_p=*/template_p,
8877                                  none_type,
8878                                  /*check_dependency_p=*/true,
8879                                  /*class_head_p=*/false,
8880                                  /*is_declaration=*/true);
8881   /* Otherwise, we could also be looking for an ordinary identifier.  */
8882   cp_parser_parse_tentatively (parser);
8883   /* Try a class-name.  */
8884   id = cp_parser_class_name (parser,
8885                              /*typename_keyword_p=*/true,
8886                              /*template_keyword_p=*/false,
8887                              none_type,
8888                              /*check_dependency_p=*/true,
8889                              /*class_head_p=*/false,
8890                              /*is_declaration=*/true);
8891   /* If we found one, we're done.  */
8892   if (cp_parser_parse_definitely (parser))
8893     return id;
8894   /* Otherwise, look for an ordinary identifier.  */
8895   return cp_parser_identifier (parser);
8896 }
8897
8898 /* Overloading [gram.over] */
8899
8900 /* Parse an operator-function-id.
8901
8902    operator-function-id:
8903      operator operator
8904
8905    Returns an IDENTIFIER_NODE for the operator which is a
8906    human-readable spelling of the identifier, e.g., `operator +'.  */
8907
8908 static tree
8909 cp_parser_operator_function_id (cp_parser* parser)
8910 {
8911   /* Look for the `operator' keyword.  */
8912   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8913     return error_mark_node;
8914   /* And then the name of the operator itself.  */
8915   return cp_parser_operator (parser);
8916 }
8917
8918 /* Parse an operator.
8919
8920    operator:
8921      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8922      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8923      || ++ -- , ->* -> () []
8924
8925    GNU Extensions:
8926
8927    operator:
8928      <? >? <?= >?=
8929
8930    Returns an IDENTIFIER_NODE for the operator which is a
8931    human-readable spelling of the identifier, e.g., `operator +'.  */
8932
8933 static tree
8934 cp_parser_operator (cp_parser* parser)
8935 {
8936   tree id = NULL_TREE;
8937   cp_token *token;
8938
8939   /* Peek at the next token.  */
8940   token = cp_lexer_peek_token (parser->lexer);
8941   /* Figure out which operator we have.  */
8942   switch (token->type)
8943     {
8944     case CPP_KEYWORD:
8945       {
8946         enum tree_code op;
8947
8948         /* The keyword should be either `new' or `delete'.  */
8949         if (token->keyword == RID_NEW)
8950           op = NEW_EXPR;
8951         else if (token->keyword == RID_DELETE)
8952           op = DELETE_EXPR;
8953         else
8954           break;
8955
8956         /* Consume the `new' or `delete' token.  */
8957         cp_lexer_consume_token (parser->lexer);
8958
8959         /* Peek at the next token.  */
8960         token = cp_lexer_peek_token (parser->lexer);
8961         /* If it's a `[' token then this is the array variant of the
8962            operator.  */
8963         if (token->type == CPP_OPEN_SQUARE)
8964           {
8965             /* Consume the `[' token.  */
8966             cp_lexer_consume_token (parser->lexer);
8967             /* Look for the `]' token.  */
8968             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8969             id = ansi_opname (op == NEW_EXPR
8970                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8971           }
8972         /* Otherwise, we have the non-array variant.  */
8973         else
8974           id = ansi_opname (op);
8975
8976         return id;
8977       }
8978
8979     case CPP_PLUS:
8980       id = ansi_opname (PLUS_EXPR);
8981       break;
8982
8983     case CPP_MINUS:
8984       id = ansi_opname (MINUS_EXPR);
8985       break;
8986
8987     case CPP_MULT:
8988       id = ansi_opname (MULT_EXPR);
8989       break;
8990
8991     case CPP_DIV:
8992       id = ansi_opname (TRUNC_DIV_EXPR);
8993       break;
8994
8995     case CPP_MOD:
8996       id = ansi_opname (TRUNC_MOD_EXPR);
8997       break;
8998
8999     case CPP_XOR:
9000       id = ansi_opname (BIT_XOR_EXPR);
9001       break;
9002
9003     case CPP_AND:
9004       id = ansi_opname (BIT_AND_EXPR);
9005       break;
9006
9007     case CPP_OR:
9008       id = ansi_opname (BIT_IOR_EXPR);
9009       break;
9010
9011     case CPP_COMPL:
9012       id = ansi_opname (BIT_NOT_EXPR);
9013       break;
9014
9015     case CPP_NOT:
9016       id = ansi_opname (TRUTH_NOT_EXPR);
9017       break;
9018
9019     case CPP_EQ:
9020       id = ansi_assopname (NOP_EXPR);
9021       break;
9022
9023     case CPP_LESS:
9024       id = ansi_opname (LT_EXPR);
9025       break;
9026
9027     case CPP_GREATER:
9028       id = ansi_opname (GT_EXPR);
9029       break;
9030
9031     case CPP_PLUS_EQ:
9032       id = ansi_assopname (PLUS_EXPR);
9033       break;
9034
9035     case CPP_MINUS_EQ:
9036       id = ansi_assopname (MINUS_EXPR);
9037       break;
9038
9039     case CPP_MULT_EQ:
9040       id = ansi_assopname (MULT_EXPR);
9041       break;
9042
9043     case CPP_DIV_EQ:
9044       id = ansi_assopname (TRUNC_DIV_EXPR);
9045       break;
9046
9047     case CPP_MOD_EQ:
9048       id = ansi_assopname (TRUNC_MOD_EXPR);
9049       break;
9050
9051     case CPP_XOR_EQ:
9052       id = ansi_assopname (BIT_XOR_EXPR);
9053       break;
9054
9055     case CPP_AND_EQ:
9056       id = ansi_assopname (BIT_AND_EXPR);
9057       break;
9058
9059     case CPP_OR_EQ:
9060       id = ansi_assopname (BIT_IOR_EXPR);
9061       break;
9062
9063     case CPP_LSHIFT:
9064       id = ansi_opname (LSHIFT_EXPR);
9065       break;
9066
9067     case CPP_RSHIFT:
9068       id = ansi_opname (RSHIFT_EXPR);
9069       break;
9070
9071     case CPP_LSHIFT_EQ:
9072       id = ansi_assopname (LSHIFT_EXPR);
9073       break;
9074
9075     case CPP_RSHIFT_EQ:
9076       id = ansi_assopname (RSHIFT_EXPR);
9077       break;
9078
9079     case CPP_EQ_EQ:
9080       id = ansi_opname (EQ_EXPR);
9081       break;
9082
9083     case CPP_NOT_EQ:
9084       id = ansi_opname (NE_EXPR);
9085       break;
9086
9087     case CPP_LESS_EQ:
9088       id = ansi_opname (LE_EXPR);
9089       break;
9090
9091     case CPP_GREATER_EQ:
9092       id = ansi_opname (GE_EXPR);
9093       break;
9094
9095     case CPP_AND_AND:
9096       id = ansi_opname (TRUTH_ANDIF_EXPR);
9097       break;
9098
9099     case CPP_OR_OR:
9100       id = ansi_opname (TRUTH_ORIF_EXPR);
9101       break;
9102
9103     case CPP_PLUS_PLUS:
9104       id = ansi_opname (POSTINCREMENT_EXPR);
9105       break;
9106
9107     case CPP_MINUS_MINUS:
9108       id = ansi_opname (PREDECREMENT_EXPR);
9109       break;
9110
9111     case CPP_COMMA:
9112       id = ansi_opname (COMPOUND_EXPR);
9113       break;
9114
9115     case CPP_DEREF_STAR:
9116       id = ansi_opname (MEMBER_REF);
9117       break;
9118
9119     case CPP_DEREF:
9120       id = ansi_opname (COMPONENT_REF);
9121       break;
9122
9123     case CPP_OPEN_PAREN:
9124       /* Consume the `('.  */
9125       cp_lexer_consume_token (parser->lexer);
9126       /* Look for the matching `)'.  */
9127       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9128       return ansi_opname (CALL_EXPR);
9129
9130     case CPP_OPEN_SQUARE:
9131       /* Consume the `['.  */
9132       cp_lexer_consume_token (parser->lexer);
9133       /* Look for the matching `]'.  */
9134       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9135       return ansi_opname (ARRAY_REF);
9136
9137     default:
9138       /* Anything else is an error.  */
9139       break;
9140     }
9141
9142   /* If we have selected an identifier, we need to consume the
9143      operator token.  */
9144   if (id)
9145     cp_lexer_consume_token (parser->lexer);
9146   /* Otherwise, no valid operator name was present.  */
9147   else
9148     {
9149       cp_parser_error (parser, "expected operator");
9150       id = error_mark_node;
9151     }
9152
9153   return id;
9154 }
9155
9156 /* Parse a template-declaration.
9157
9158    template-declaration:
9159      export [opt] template < template-parameter-list > declaration
9160
9161    If MEMBER_P is TRUE, this template-declaration occurs within a
9162    class-specifier.
9163
9164    The grammar rule given by the standard isn't correct.  What
9165    is really meant is:
9166
9167    template-declaration:
9168      export [opt] template-parameter-list-seq
9169        decl-specifier-seq [opt] init-declarator [opt] ;
9170      export [opt] template-parameter-list-seq
9171        function-definition
9172
9173    template-parameter-list-seq:
9174      template-parameter-list-seq [opt]
9175      template < template-parameter-list >  */
9176
9177 static void
9178 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9179 {
9180   /* Check for `export'.  */
9181   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9182     {
9183       /* Consume the `export' token.  */
9184       cp_lexer_consume_token (parser->lexer);
9185       /* Warn that we do not support `export'.  */
9186       warning (0, "keyword %<export%> not implemented, and will be ignored");
9187     }
9188
9189   cp_parser_template_declaration_after_export (parser, member_p);
9190 }
9191
9192 /* Parse a template-parameter-list.
9193
9194    template-parameter-list:
9195      template-parameter
9196      template-parameter-list , template-parameter
9197
9198    Returns a TREE_LIST.  Each node represents a template parameter.
9199    The nodes are connected via their TREE_CHAINs.  */
9200
9201 static tree
9202 cp_parser_template_parameter_list (cp_parser* parser)
9203 {
9204   tree parameter_list = NULL_TREE;
9205
9206   begin_template_parm_list ();
9207   while (true)
9208     {
9209       tree parameter;
9210       cp_token *token;
9211       bool is_non_type;
9212       bool is_parameter_pack;
9213
9214       /* Parse the template-parameter.  */
9215       parameter = cp_parser_template_parameter (parser, 
9216                                                 &is_non_type,
9217                                                 &is_parameter_pack);
9218       /* Add it to the list.  */
9219       if (parameter != error_mark_node)
9220         parameter_list = process_template_parm (parameter_list,
9221                                                 parameter,
9222                                                 is_non_type,
9223                                                 is_parameter_pack);
9224       else
9225        {
9226          tree err_parm = build_tree_list (parameter, parameter);
9227          TREE_VALUE (err_parm) = error_mark_node;
9228          parameter_list = chainon (parameter_list, err_parm);
9229        }
9230
9231       /* Peek at the next token.  */
9232       token = cp_lexer_peek_token (parser->lexer);
9233       /* If it's not a `,', we're done.  */
9234       if (token->type != CPP_COMMA)
9235         break;
9236       /* Otherwise, consume the `,' token.  */
9237       cp_lexer_consume_token (parser->lexer);
9238     }
9239
9240   return end_template_parm_list (parameter_list);
9241 }
9242
9243 /* Parse a template-parameter.
9244
9245    template-parameter:
9246      type-parameter
9247      parameter-declaration
9248
9249    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9250    the parameter.  The TREE_PURPOSE is the default value, if any.
9251    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9252    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9253    set to true iff this parameter is a parameter pack. */
9254
9255 static tree
9256 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9257                               bool *is_parameter_pack)
9258 {
9259   cp_token *token;
9260   cp_parameter_declarator *parameter_declarator;
9261   tree parm;
9262
9263   /* Assume it is a type parameter or a template parameter.  */
9264   *is_non_type = false;
9265   /* Assume it not a parameter pack. */
9266   *is_parameter_pack = false;
9267   /* Peek at the next token.  */
9268   token = cp_lexer_peek_token (parser->lexer);
9269   /* If it is `class' or `template', we have a type-parameter.  */
9270   if (token->keyword == RID_TEMPLATE)
9271     return cp_parser_type_parameter (parser, is_parameter_pack);
9272   /* If it is `class' or `typename' we do not know yet whether it is a
9273      type parameter or a non-type parameter.  Consider:
9274
9275        template <typename T, typename T::X X> ...
9276
9277      or:
9278
9279        template <class C, class D*> ...
9280
9281      Here, the first parameter is a type parameter, and the second is
9282      a non-type parameter.  We can tell by looking at the token after
9283      the identifier -- if it is a `,', `=', or `>' then we have a type
9284      parameter.  */
9285   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9286     {
9287       /* Peek at the token after `class' or `typename'.  */
9288       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9289       /* If it's an ellipsis, we have a template type parameter
9290          pack. */
9291       if (token->type == CPP_ELLIPSIS)
9292         return cp_parser_type_parameter (parser, is_parameter_pack);
9293       /* If it's an identifier, skip it.  */
9294       if (token->type == CPP_NAME)
9295         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9296       /* Now, see if the token looks like the end of a template
9297          parameter.  */
9298       if (token->type == CPP_COMMA
9299           || token->type == CPP_EQ
9300           || token->type == CPP_GREATER)
9301         return cp_parser_type_parameter (parser, is_parameter_pack);
9302     }
9303
9304   /* Otherwise, it is a non-type parameter.
9305
9306      [temp.param]
9307
9308      When parsing a default template-argument for a non-type
9309      template-parameter, the first non-nested `>' is taken as the end
9310      of the template parameter-list rather than a greater-than
9311      operator.  */
9312   *is_non_type = true;
9313   parameter_declarator
9314      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9315                                         /*parenthesized_p=*/NULL);
9316
9317   /* If the parameter declaration is marked as a parameter pack, set
9318      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9319      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9320      grokdeclarator. */
9321   if (parameter_declarator
9322       && parameter_declarator->declarator
9323       && parameter_declarator->declarator->parameter_pack_p)
9324     {
9325       *is_parameter_pack = true;
9326       parameter_declarator->declarator->parameter_pack_p = false;
9327     }
9328
9329   /* If the next token is an ellipsis, and we don't already have it
9330      marked as a parameter pack, then we have a parameter pack (that
9331      has no declarator); */
9332   if (!*is_parameter_pack
9333       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9334       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9335     {
9336       /* Consume the `...'. */
9337       cp_lexer_consume_token (parser->lexer);
9338       maybe_warn_variadic_templates ();
9339       
9340       *is_parameter_pack = true;
9341     }
9342
9343   parm = grokdeclarator (parameter_declarator->declarator,
9344                          &parameter_declarator->decl_specifiers,
9345                          PARM, /*initialized=*/0,
9346                          /*attrlist=*/NULL);
9347   if (parm == error_mark_node)
9348     return error_mark_node;
9349
9350   return build_tree_list (parameter_declarator->default_argument, parm);
9351 }
9352
9353 /* Parse a type-parameter.
9354
9355    type-parameter:
9356      class identifier [opt]
9357      class identifier [opt] = type-id
9358      typename identifier [opt]
9359      typename identifier [opt] = type-id
9360      template < template-parameter-list > class identifier [opt]
9361      template < template-parameter-list > class identifier [opt]
9362        = id-expression
9363
9364    GNU Extension (variadic templates):
9365
9366    type-parameter:
9367      class ... identifier [opt]
9368      typename ... identifier [opt]
9369
9370    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9371    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9372    the declaration of the parameter.
9373
9374    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9375
9376 static tree
9377 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9378 {
9379   cp_token *token;
9380   tree parameter;
9381
9382   /* Look for a keyword to tell us what kind of parameter this is.  */
9383   token = cp_parser_require (parser, CPP_KEYWORD,
9384                              "`class', `typename', or `template'");
9385   if (!token)
9386     return error_mark_node;
9387
9388   switch (token->keyword)
9389     {
9390     case RID_CLASS:
9391     case RID_TYPENAME:
9392       {
9393         tree identifier;
9394         tree default_argument;
9395
9396         /* If the next token is an ellipsis, we have a template
9397            argument pack. */
9398         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9399           {
9400             /* Consume the `...' token. */
9401             cp_lexer_consume_token (parser->lexer);
9402             maybe_warn_variadic_templates ();
9403
9404             *is_parameter_pack = true;
9405           }
9406
9407         /* If the next token is an identifier, then it names the
9408            parameter.  */
9409         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9410           identifier = cp_parser_identifier (parser);
9411         else
9412           identifier = NULL_TREE;
9413
9414         /* Create the parameter.  */
9415         parameter = finish_template_type_parm (class_type_node, identifier);
9416
9417         /* If the next token is an `=', we have a default argument.  */
9418         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9419           {
9420             /* Consume the `=' token.  */
9421             cp_lexer_consume_token (parser->lexer);
9422             /* Parse the default-argument.  */
9423             push_deferring_access_checks (dk_no_deferred);
9424             default_argument = cp_parser_type_id (parser);
9425
9426             /* Template parameter packs cannot have default
9427                arguments. */
9428             if (*is_parameter_pack)
9429               {
9430                 if (identifier)
9431                   error ("template parameter pack %qD cannot have a default argument", 
9432                          identifier);
9433                 else
9434                   error ("template parameter packs cannot have default arguments");
9435                 default_argument = NULL_TREE;
9436               }
9437             pop_deferring_access_checks ();
9438           }
9439         else
9440           default_argument = NULL_TREE;
9441
9442         /* Create the combined representation of the parameter and the
9443            default argument.  */
9444         parameter = build_tree_list (default_argument, parameter);
9445       }
9446       break;
9447
9448     case RID_TEMPLATE:
9449       {
9450         tree parameter_list;
9451         tree identifier;
9452         tree default_argument;
9453
9454         /* Look for the `<'.  */
9455         cp_parser_require (parser, CPP_LESS, "`<'");
9456         /* Parse the template-parameter-list.  */
9457         parameter_list = cp_parser_template_parameter_list (parser);
9458         /* Look for the `>'.  */
9459         cp_parser_require (parser, CPP_GREATER, "`>'");
9460         /* Look for the `class' keyword.  */
9461         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9462         /* If the next token is an ellipsis, we have a template
9463            argument pack. */
9464         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9465           {
9466             /* Consume the `...' token. */
9467             cp_lexer_consume_token (parser->lexer);
9468             maybe_warn_variadic_templates ();
9469
9470             *is_parameter_pack = true;
9471           }
9472         /* If the next token is an `=', then there is a
9473            default-argument.  If the next token is a `>', we are at
9474            the end of the parameter-list.  If the next token is a `,',
9475            then we are at the end of this parameter.  */
9476         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9477             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9478             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9479           {
9480             identifier = cp_parser_identifier (parser);
9481             /* Treat invalid names as if the parameter were nameless.  */
9482             if (identifier == error_mark_node)
9483               identifier = NULL_TREE;
9484           }
9485         else
9486           identifier = NULL_TREE;
9487
9488         /* Create the template parameter.  */
9489         parameter = finish_template_template_parm (class_type_node,
9490                                                    identifier);
9491
9492         /* If the next token is an `=', then there is a
9493            default-argument.  */
9494         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9495           {
9496             bool is_template;
9497
9498             /* Consume the `='.  */
9499             cp_lexer_consume_token (parser->lexer);
9500             /* Parse the id-expression.  */
9501             push_deferring_access_checks (dk_no_deferred);
9502             default_argument
9503               = cp_parser_id_expression (parser,
9504                                          /*template_keyword_p=*/false,
9505                                          /*check_dependency_p=*/true,
9506                                          /*template_p=*/&is_template,
9507                                          /*declarator_p=*/false,
9508                                          /*optional_p=*/false);
9509             if (TREE_CODE (default_argument) == TYPE_DECL)
9510               /* If the id-expression was a template-id that refers to
9511                  a template-class, we already have the declaration here,
9512                  so no further lookup is needed.  */
9513                  ;
9514             else
9515               /* Look up the name.  */
9516               default_argument
9517                 = cp_parser_lookup_name (parser, default_argument,
9518                                          none_type,
9519                                          /*is_template=*/is_template,
9520                                          /*is_namespace=*/false,
9521                                          /*check_dependency=*/true,
9522                                          /*ambiguous_decls=*/NULL);
9523             /* See if the default argument is valid.  */
9524             default_argument
9525               = check_template_template_default_arg (default_argument);
9526
9527             /* Template parameter packs cannot have default
9528                arguments. */
9529             if (*is_parameter_pack)
9530               {
9531                 if (identifier)
9532                   error ("template parameter pack %qD cannot have a default argument", 
9533                          identifier);
9534                 else
9535                   error ("template parameter packs cannot have default arguments");
9536                 default_argument = NULL_TREE;
9537               }
9538             pop_deferring_access_checks ();
9539           }
9540         else
9541           default_argument = NULL_TREE;
9542
9543         /* Create the combined representation of the parameter and the
9544            default argument.  */
9545         parameter = build_tree_list (default_argument, parameter);
9546       }
9547       break;
9548
9549     default:
9550       gcc_unreachable ();
9551       break;
9552     }
9553
9554   return parameter;
9555 }
9556
9557 /* Parse a template-id.
9558
9559    template-id:
9560      template-name < template-argument-list [opt] >
9561
9562    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9563    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9564    returned.  Otherwise, if the template-name names a function, or set
9565    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9566    names a class, returns a TYPE_DECL for the specialization.
9567
9568    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9569    uninstantiated templates.  */
9570
9571 static tree
9572 cp_parser_template_id (cp_parser *parser,
9573                        bool template_keyword_p,
9574                        bool check_dependency_p,
9575                        bool is_declaration)
9576 {
9577   int i;
9578   tree template;
9579   tree arguments;
9580   tree template_id;
9581   cp_token_position start_of_id = 0;
9582   deferred_access_check *chk;
9583   VEC (deferred_access_check,gc) *access_check;
9584   cp_token *next_token, *next_token_2;
9585   bool is_identifier;
9586
9587   /* If the next token corresponds to a template-id, there is no need
9588      to reparse it.  */
9589   next_token = cp_lexer_peek_token (parser->lexer);
9590   if (next_token->type == CPP_TEMPLATE_ID)
9591     {
9592       struct tree_check *check_value;
9593
9594       /* Get the stored value.  */
9595       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9596       /* Perform any access checks that were deferred.  */
9597       access_check = check_value->checks;
9598       if (access_check)
9599         {
9600           for (i = 0 ;
9601                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9602                ++i)
9603             {
9604               perform_or_defer_access_check (chk->binfo,
9605                                              chk->decl,
9606                                              chk->diag_decl);
9607             }
9608         }
9609       /* Return the stored value.  */
9610       return check_value->value;
9611     }
9612
9613   /* Avoid performing name lookup if there is no possibility of
9614      finding a template-id.  */
9615   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9616       || (next_token->type == CPP_NAME
9617           && !cp_parser_nth_token_starts_template_argument_list_p
9618                (parser, 2)))
9619     {
9620       cp_parser_error (parser, "expected template-id");
9621       return error_mark_node;
9622     }
9623
9624   /* Remember where the template-id starts.  */
9625   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9626     start_of_id = cp_lexer_token_position (parser->lexer, false);
9627
9628   push_deferring_access_checks (dk_deferred);
9629
9630   /* Parse the template-name.  */
9631   is_identifier = false;
9632   template = cp_parser_template_name (parser, template_keyword_p,
9633                                       check_dependency_p,
9634                                       is_declaration,
9635                                       &is_identifier);
9636   if (template == error_mark_node || is_identifier)
9637     {
9638       pop_deferring_access_checks ();
9639       return template;
9640     }
9641
9642   /* If we find the sequence `[:' after a template-name, it's probably
9643      a digraph-typo for `< ::'. Substitute the tokens and check if we can
9644      parse correctly the argument list.  */
9645   next_token = cp_lexer_peek_token (parser->lexer);
9646   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9647   if (next_token->type == CPP_OPEN_SQUARE
9648       && next_token->flags & DIGRAPH
9649       && next_token_2->type == CPP_COLON
9650       && !(next_token_2->flags & PREV_WHITE))
9651     {
9652       cp_parser_parse_tentatively (parser);
9653       /* Change `:' into `::'.  */
9654       next_token_2->type = CPP_SCOPE;
9655       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9656          CPP_LESS.  */
9657       cp_lexer_consume_token (parser->lexer);
9658       /* Parse the arguments.  */
9659       arguments = cp_parser_enclosed_template_argument_list (parser);
9660       if (!cp_parser_parse_definitely (parser))
9661         {
9662           /* If we couldn't parse an argument list, then we revert our changes
9663              and return simply an error. Maybe this is not a template-id
9664              after all.  */
9665           next_token_2->type = CPP_COLON;
9666           cp_parser_error (parser, "expected %<<%>");
9667           pop_deferring_access_checks ();
9668           return error_mark_node;
9669         }
9670       /* Otherwise, emit an error about the invalid digraph, but continue
9671          parsing because we got our argument list.  */
9672       pedwarn ("%<<::%> cannot begin a template-argument list");
9673       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9674               "between %<<%> and %<::%>");
9675       if (!flag_permissive)
9676         {
9677           static bool hint;
9678           if (!hint)
9679             {
9680               inform ("(if you use -fpermissive G++ will accept your code)");
9681               hint = true;
9682             }
9683         }
9684     }
9685   else
9686     {
9687       /* Look for the `<' that starts the template-argument-list.  */
9688       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9689         {
9690           pop_deferring_access_checks ();
9691           return error_mark_node;
9692         }
9693       /* Parse the arguments.  */
9694       arguments = cp_parser_enclosed_template_argument_list (parser);
9695     }
9696
9697   /* Build a representation of the specialization.  */
9698   if (TREE_CODE (template) == IDENTIFIER_NODE)
9699     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9700   else if (DECL_CLASS_TEMPLATE_P (template)
9701            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9702     {
9703       bool entering_scope;
9704       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9705          template (rather than some instantiation thereof) only if
9706          is not nested within some other construct.  For example, in
9707          "template <typename T> void f(T) { A<T>::", A<T> is just an
9708          instantiation of A.  */
9709       entering_scope = (template_parm_scope_p ()
9710                         && cp_lexer_next_token_is (parser->lexer,
9711                                                    CPP_SCOPE));
9712       template_id
9713         = finish_template_type (template, arguments, entering_scope);
9714     }
9715   else
9716     {
9717       /* If it's not a class-template or a template-template, it should be
9718          a function-template.  */
9719       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9720                    || TREE_CODE (template) == OVERLOAD
9721                    || BASELINK_P (template)));
9722
9723       template_id = lookup_template_function (template, arguments);
9724     }
9725
9726   /* If parsing tentatively, replace the sequence of tokens that makes
9727      up the template-id with a CPP_TEMPLATE_ID token.  That way,
9728      should we re-parse the token stream, we will not have to repeat
9729      the effort required to do the parse, nor will we issue duplicate
9730      error messages about problems during instantiation of the
9731      template.  */
9732   if (start_of_id)
9733     {
9734       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9735
9736       /* Reset the contents of the START_OF_ID token.  */
9737       token->type = CPP_TEMPLATE_ID;
9738       /* Retrieve any deferred checks.  Do not pop this access checks yet
9739          so the memory will not be reclaimed during token replacing below.  */
9740       token->u.tree_check_value = GGC_CNEW (struct tree_check);
9741       token->u.tree_check_value->value = template_id;
9742       token->u.tree_check_value->checks = get_deferred_access_checks ();
9743       token->keyword = RID_MAX;
9744
9745       /* Purge all subsequent tokens.  */
9746       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9747
9748       /* ??? Can we actually assume that, if template_id ==
9749          error_mark_node, we will have issued a diagnostic to the
9750          user, as opposed to simply marking the tentative parse as
9751          failed?  */
9752       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9753         error ("parse error in template argument list");
9754     }
9755
9756   pop_deferring_access_checks ();
9757   return template_id;
9758 }
9759
9760 /* Parse a template-name.
9761
9762    template-name:
9763      identifier
9764
9765    The standard should actually say:
9766
9767    template-name:
9768      identifier
9769      operator-function-id
9770
9771    A defect report has been filed about this issue.
9772
9773    A conversion-function-id cannot be a template name because they cannot
9774    be part of a template-id. In fact, looking at this code:
9775
9776    a.operator K<int>()
9777
9778    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9779    It is impossible to call a templated conversion-function-id with an
9780    explicit argument list, since the only allowed template parameter is
9781    the type to which it is converting.
9782
9783    If TEMPLATE_KEYWORD_P is true, then we have just seen the
9784    `template' keyword, in a construction like:
9785
9786      T::template f<3>()
9787
9788    In that case `f' is taken to be a template-name, even though there
9789    is no way of knowing for sure.
9790
9791    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9792    name refers to a set of overloaded functions, at least one of which
9793    is a template, or an IDENTIFIER_NODE with the name of the template,
9794    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
9795    names are looked up inside uninstantiated templates.  */
9796
9797 static tree
9798 cp_parser_template_name (cp_parser* parser,
9799                          bool template_keyword_p,
9800                          bool check_dependency_p,
9801                          bool is_declaration,
9802                          bool *is_identifier)
9803 {
9804   tree identifier;
9805   tree decl;
9806   tree fns;
9807
9808   /* If the next token is `operator', then we have either an
9809      operator-function-id or a conversion-function-id.  */
9810   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9811     {
9812       /* We don't know whether we're looking at an
9813          operator-function-id or a conversion-function-id.  */
9814       cp_parser_parse_tentatively (parser);
9815       /* Try an operator-function-id.  */
9816       identifier = cp_parser_operator_function_id (parser);
9817       /* If that didn't work, try a conversion-function-id.  */
9818       if (!cp_parser_parse_definitely (parser))
9819         {
9820           cp_parser_error (parser, "expected template-name");
9821           return error_mark_node;
9822         }
9823     }
9824   /* Look for the identifier.  */
9825   else
9826     identifier = cp_parser_identifier (parser);
9827
9828   /* If we didn't find an identifier, we don't have a template-id.  */
9829   if (identifier == error_mark_node)
9830     return error_mark_node;
9831
9832   /* If the name immediately followed the `template' keyword, then it
9833      is a template-name.  However, if the next token is not `<', then
9834      we do not treat it as a template-name, since it is not being used
9835      as part of a template-id.  This enables us to handle constructs
9836      like:
9837
9838        template <typename T> struct S { S(); };
9839        template <typename T> S<T>::S();
9840
9841      correctly.  We would treat `S' as a template -- if it were `S<T>'
9842      -- but we do not if there is no `<'.  */
9843
9844   if (processing_template_decl
9845       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9846     {
9847       /* In a declaration, in a dependent context, we pretend that the
9848          "template" keyword was present in order to improve error
9849          recovery.  For example, given:
9850
9851            template <typename T> void f(T::X<int>);
9852
9853          we want to treat "X<int>" as a template-id.  */
9854       if (is_declaration
9855           && !template_keyword_p
9856           && parser->scope && TYPE_P (parser->scope)
9857           && check_dependency_p
9858           && dependent_type_p (parser->scope)
9859           /* Do not do this for dtors (or ctors), since they never
9860              need the template keyword before their name.  */
9861           && !constructor_name_p (identifier, parser->scope))
9862         {
9863           cp_token_position start = 0;
9864
9865           /* Explain what went wrong.  */
9866           error ("non-template %qD used as template", identifier);
9867           inform ("use %<%T::template %D%> to indicate that it is a template",
9868                   parser->scope, identifier);
9869           /* If parsing tentatively, find the location of the "<" token.  */
9870           if (cp_parser_simulate_error (parser))
9871             start = cp_lexer_token_position (parser->lexer, true);
9872           /* Parse the template arguments so that we can issue error
9873              messages about them.  */
9874           cp_lexer_consume_token (parser->lexer);
9875           cp_parser_enclosed_template_argument_list (parser);
9876           /* Skip tokens until we find a good place from which to
9877              continue parsing.  */
9878           cp_parser_skip_to_closing_parenthesis (parser,
9879                                                  /*recovering=*/true,
9880                                                  /*or_comma=*/true,
9881                                                  /*consume_paren=*/false);
9882           /* If parsing tentatively, permanently remove the
9883              template argument list.  That will prevent duplicate
9884              error messages from being issued about the missing
9885              "template" keyword.  */
9886           if (start)
9887             cp_lexer_purge_tokens_after (parser->lexer, start);
9888           if (is_identifier)
9889             *is_identifier = true;
9890           return identifier;
9891         }
9892
9893       /* If the "template" keyword is present, then there is generally
9894          no point in doing name-lookup, so we just return IDENTIFIER.
9895          But, if the qualifying scope is non-dependent then we can
9896          (and must) do name-lookup normally.  */
9897       if (template_keyword_p
9898           && (!parser->scope
9899               || (TYPE_P (parser->scope)
9900                   && dependent_type_p (parser->scope))))
9901         return identifier;
9902     }
9903
9904   /* Look up the name.  */
9905   decl = cp_parser_lookup_name (parser, identifier,
9906                                 none_type,
9907                                 /*is_template=*/false,
9908                                 /*is_namespace=*/false,
9909                                 check_dependency_p,
9910                                 /*ambiguous_decls=*/NULL);
9911   decl = maybe_get_template_decl_from_type_decl (decl);
9912
9913   /* If DECL is a template, then the name was a template-name.  */
9914   if (TREE_CODE (decl) == TEMPLATE_DECL)
9915     ;
9916   else
9917     {
9918       tree fn = NULL_TREE;
9919
9920       /* The standard does not explicitly indicate whether a name that
9921          names a set of overloaded declarations, some of which are
9922          templates, is a template-name.  However, such a name should
9923          be a template-name; otherwise, there is no way to form a
9924          template-id for the overloaded templates.  */
9925       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9926       if (TREE_CODE (fns) == OVERLOAD)
9927         for (fn = fns; fn; fn = OVL_NEXT (fn))
9928           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9929             break;
9930
9931       if (!fn)
9932         {
9933           /* The name does not name a template.  */
9934           cp_parser_error (parser, "expected template-name");
9935           return error_mark_node;
9936         }
9937     }
9938
9939   /* If DECL is dependent, and refers to a function, then just return
9940      its name; we will look it up again during template instantiation.  */
9941   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9942     {
9943       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9944       if (TYPE_P (scope) && dependent_type_p (scope))
9945         return identifier;
9946     }
9947
9948   return decl;
9949 }
9950
9951 /* Parse a template-argument-list.
9952
9953    template-argument-list:
9954      template-argument ... [opt]
9955      template-argument-list , template-argument ... [opt]
9956
9957    Returns a TREE_VEC containing the arguments.  */
9958
9959 static tree
9960 cp_parser_template_argument_list (cp_parser* parser)
9961 {
9962   tree fixed_args[10];
9963   unsigned n_args = 0;
9964   unsigned alloced = 10;
9965   tree *arg_ary = fixed_args;
9966   tree vec;
9967   bool saved_in_template_argument_list_p;
9968   bool saved_ice_p;
9969   bool saved_non_ice_p;
9970
9971   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9972   parser->in_template_argument_list_p = true;
9973   /* Even if the template-id appears in an integral
9974      constant-expression, the contents of the argument list do
9975      not.  */
9976   saved_ice_p = parser->integral_constant_expression_p;
9977   parser->integral_constant_expression_p = false;
9978   saved_non_ice_p = parser->non_integral_constant_expression_p;
9979   parser->non_integral_constant_expression_p = false;
9980   /* Parse the arguments.  */
9981   do
9982     {
9983       tree argument;
9984
9985       if (n_args)
9986         /* Consume the comma.  */
9987         cp_lexer_consume_token (parser->lexer);
9988
9989       /* Parse the template-argument.  */
9990       argument = cp_parser_template_argument (parser);
9991
9992       /* If the next token is an ellipsis, we're expanding a template
9993          argument pack. */
9994       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9995         {
9996           /* Consume the `...' token. */
9997           cp_lexer_consume_token (parser->lexer);
9998
9999           /* Make the argument into a TYPE_PACK_EXPANSION or
10000              EXPR_PACK_EXPANSION. */
10001           argument = make_pack_expansion (argument);
10002         }
10003
10004       if (n_args == alloced)
10005         {
10006           alloced *= 2;
10007
10008           if (arg_ary == fixed_args)
10009             {
10010               arg_ary = XNEWVEC (tree, alloced);
10011               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10012             }
10013           else
10014             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10015         }
10016       arg_ary[n_args++] = argument;
10017     }
10018   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10019
10020   vec = make_tree_vec (n_args);
10021
10022   while (n_args--)
10023     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10024
10025   if (arg_ary != fixed_args)
10026     free (arg_ary);
10027   parser->non_integral_constant_expression_p = saved_non_ice_p;
10028   parser->integral_constant_expression_p = saved_ice_p;
10029   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10030   return vec;
10031 }
10032
10033 /* Parse a template-argument.
10034
10035    template-argument:
10036      assignment-expression
10037      type-id
10038      id-expression
10039
10040    The representation is that of an assignment-expression, type-id, or
10041    id-expression -- except that the qualified id-expression is
10042    evaluated, so that the value returned is either a DECL or an
10043    OVERLOAD.
10044
10045    Although the standard says "assignment-expression", it forbids
10046    throw-expressions or assignments in the template argument.
10047    Therefore, we use "conditional-expression" instead.  */
10048
10049 static tree
10050 cp_parser_template_argument (cp_parser* parser)
10051 {
10052   tree argument;
10053   bool template_p;
10054   bool address_p;
10055   bool maybe_type_id = false;
10056   cp_token *token;
10057   cp_id_kind idk;
10058
10059   /* There's really no way to know what we're looking at, so we just
10060      try each alternative in order.
10061
10062        [temp.arg]
10063
10064        In a template-argument, an ambiguity between a type-id and an
10065        expression is resolved to a type-id, regardless of the form of
10066        the corresponding template-parameter.
10067
10068      Therefore, we try a type-id first.  */
10069   cp_parser_parse_tentatively (parser);
10070   argument = cp_parser_type_id (parser);
10071   /* If there was no error parsing the type-id but the next token is a '>>',
10072      we probably found a typo for '> >'. But there are type-id which are
10073      also valid expressions. For instance:
10074
10075      struct X { int operator >> (int); };
10076      template <int V> struct Foo {};
10077      Foo<X () >> 5> r;
10078
10079      Here 'X()' is a valid type-id of a function type, but the user just
10080      wanted to write the expression "X() >> 5". Thus, we remember that we
10081      found a valid type-id, but we still try to parse the argument as an
10082      expression to see what happens.  */
10083   if (!cp_parser_error_occurred (parser)
10084       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10085     {
10086       maybe_type_id = true;
10087       cp_parser_abort_tentative_parse (parser);
10088     }
10089   else
10090     {
10091       /* If the next token isn't a `,' or a `>', then this argument wasn't
10092       really finished. This means that the argument is not a valid
10093       type-id.  */
10094       if (!cp_parser_next_token_ends_template_argument_p (parser))
10095         cp_parser_error (parser, "expected template-argument");
10096       /* If that worked, we're done.  */
10097       if (cp_parser_parse_definitely (parser))
10098         return argument;
10099     }
10100   /* We're still not sure what the argument will be.  */
10101   cp_parser_parse_tentatively (parser);
10102   /* Try a template.  */
10103   argument = cp_parser_id_expression (parser,
10104                                       /*template_keyword_p=*/false,
10105                                       /*check_dependency_p=*/true,
10106                                       &template_p,
10107                                       /*declarator_p=*/false,
10108                                       /*optional_p=*/false);
10109   /* If the next token isn't a `,' or a `>', then this argument wasn't
10110      really finished.  */
10111   if (!cp_parser_next_token_ends_template_argument_p (parser))
10112     cp_parser_error (parser, "expected template-argument");
10113   if (!cp_parser_error_occurred (parser))
10114     {
10115       /* Figure out what is being referred to.  If the id-expression
10116          was for a class template specialization, then we will have a
10117          TYPE_DECL at this point.  There is no need to do name lookup
10118          at this point in that case.  */
10119       if (TREE_CODE (argument) != TYPE_DECL)
10120         argument = cp_parser_lookup_name (parser, argument,
10121                                           none_type,
10122                                           /*is_template=*/template_p,
10123                                           /*is_namespace=*/false,
10124                                           /*check_dependency=*/true,
10125                                           /*ambiguous_decls=*/NULL);
10126       if (TREE_CODE (argument) != TEMPLATE_DECL
10127           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10128         cp_parser_error (parser, "expected template-name");
10129     }
10130   if (cp_parser_parse_definitely (parser))
10131     return argument;
10132   /* It must be a non-type argument.  There permitted cases are given
10133      in [temp.arg.nontype]:
10134
10135      -- an integral constant-expression of integral or enumeration
10136         type; or
10137
10138      -- the name of a non-type template-parameter; or
10139
10140      -- the name of an object or function with external linkage...
10141
10142      -- the address of an object or function with external linkage...
10143
10144      -- a pointer to member...  */
10145   /* Look for a non-type template parameter.  */
10146   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10147     {
10148       cp_parser_parse_tentatively (parser);
10149       argument = cp_parser_primary_expression (parser,
10150                                                /*adress_p=*/false,
10151                                                /*cast_p=*/false,
10152                                                /*template_arg_p=*/true,
10153                                                &idk);
10154       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10155           || !cp_parser_next_token_ends_template_argument_p (parser))
10156         cp_parser_simulate_error (parser);
10157       if (cp_parser_parse_definitely (parser))
10158         return argument;
10159     }
10160
10161   /* If the next token is "&", the argument must be the address of an
10162      object or function with external linkage.  */
10163   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10164   if (address_p)
10165     cp_lexer_consume_token (parser->lexer);
10166   /* See if we might have an id-expression.  */
10167   token = cp_lexer_peek_token (parser->lexer);
10168   if (token->type == CPP_NAME
10169       || token->keyword == RID_OPERATOR
10170       || token->type == CPP_SCOPE
10171       || token->type == CPP_TEMPLATE_ID
10172       || token->type == CPP_NESTED_NAME_SPECIFIER)
10173     {
10174       cp_parser_parse_tentatively (parser);
10175       argument = cp_parser_primary_expression (parser,
10176                                                address_p,
10177                                                /*cast_p=*/false,
10178                                                /*template_arg_p=*/true,
10179                                                &idk);
10180       if (cp_parser_error_occurred (parser)
10181           || !cp_parser_next_token_ends_template_argument_p (parser))
10182         cp_parser_abort_tentative_parse (parser);
10183       else
10184         {
10185           if (TREE_CODE (argument) == INDIRECT_REF)
10186             {
10187               gcc_assert (REFERENCE_REF_P (argument));
10188               argument = TREE_OPERAND (argument, 0);
10189             }
10190
10191           if (TREE_CODE (argument) == VAR_DECL)
10192             {
10193               /* A variable without external linkage might still be a
10194                  valid constant-expression, so no error is issued here
10195                  if the external-linkage check fails.  */
10196               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10197                 cp_parser_simulate_error (parser);
10198             }
10199           else if (is_overloaded_fn (argument))
10200             /* All overloaded functions are allowed; if the external
10201                linkage test does not pass, an error will be issued
10202                later.  */
10203             ;
10204           else if (address_p
10205                    && (TREE_CODE (argument) == OFFSET_REF
10206                        || TREE_CODE (argument) == SCOPE_REF))
10207             /* A pointer-to-member.  */
10208             ;
10209           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10210             ;
10211           else
10212             cp_parser_simulate_error (parser);
10213
10214           if (cp_parser_parse_definitely (parser))
10215             {
10216               if (address_p)
10217                 argument = build_x_unary_op (ADDR_EXPR, argument);
10218               return argument;
10219             }
10220         }
10221     }
10222   /* If the argument started with "&", there are no other valid
10223      alternatives at this point.  */
10224   if (address_p)
10225     {
10226       cp_parser_error (parser, "invalid non-type template argument");
10227       return error_mark_node;
10228     }
10229
10230   /* If the argument wasn't successfully parsed as a type-id followed
10231      by '>>', the argument can only be a constant expression now.
10232      Otherwise, we try parsing the constant-expression tentatively,
10233      because the argument could really be a type-id.  */
10234   if (maybe_type_id)
10235     cp_parser_parse_tentatively (parser);
10236   argument = cp_parser_constant_expression (parser,
10237                                             /*allow_non_constant_p=*/false,
10238                                             /*non_constant_p=*/NULL);
10239   argument = fold_non_dependent_expr (argument);
10240   if (!maybe_type_id)
10241     return argument;
10242   if (!cp_parser_next_token_ends_template_argument_p (parser))
10243     cp_parser_error (parser, "expected template-argument");
10244   if (cp_parser_parse_definitely (parser))
10245     return argument;
10246   /* We did our best to parse the argument as a non type-id, but that
10247      was the only alternative that matched (albeit with a '>' after
10248      it). We can assume it's just a typo from the user, and a
10249      diagnostic will then be issued.  */
10250   return cp_parser_type_id (parser);
10251 }
10252
10253 /* Parse an explicit-instantiation.
10254
10255    explicit-instantiation:
10256      template declaration
10257
10258    Although the standard says `declaration', what it really means is:
10259
10260    explicit-instantiation:
10261      template decl-specifier-seq [opt] declarator [opt] ;
10262
10263    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10264    supposed to be allowed.  A defect report has been filed about this
10265    issue.
10266
10267    GNU Extension:
10268
10269    explicit-instantiation:
10270      storage-class-specifier template
10271        decl-specifier-seq [opt] declarator [opt] ;
10272      function-specifier template
10273        decl-specifier-seq [opt] declarator [opt] ;  */
10274
10275 static void
10276 cp_parser_explicit_instantiation (cp_parser* parser)
10277 {
10278   int declares_class_or_enum;
10279   cp_decl_specifier_seq decl_specifiers;
10280   tree extension_specifier = NULL_TREE;
10281
10282   /* Look for an (optional) storage-class-specifier or
10283      function-specifier.  */
10284   if (cp_parser_allow_gnu_extensions_p (parser))
10285     {
10286       extension_specifier
10287         = cp_parser_storage_class_specifier_opt (parser);
10288       if (!extension_specifier)
10289         extension_specifier
10290           = cp_parser_function_specifier_opt (parser,
10291                                               /*decl_specs=*/NULL);
10292     }
10293
10294   /* Look for the `template' keyword.  */
10295   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10296   /* Let the front end know that we are processing an explicit
10297      instantiation.  */
10298   begin_explicit_instantiation ();
10299   /* [temp.explicit] says that we are supposed to ignore access
10300      control while processing explicit instantiation directives.  */
10301   push_deferring_access_checks (dk_no_check);
10302   /* Parse a decl-specifier-seq.  */
10303   cp_parser_decl_specifier_seq (parser,
10304                                 CP_PARSER_FLAGS_OPTIONAL,
10305                                 &decl_specifiers,
10306                                 &declares_class_or_enum);
10307   /* If there was exactly one decl-specifier, and it declared a class,
10308      and there's no declarator, then we have an explicit type
10309      instantiation.  */
10310   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10311     {
10312       tree type;
10313
10314       type = check_tag_decl (&decl_specifiers);
10315       /* Turn access control back on for names used during
10316          template instantiation.  */
10317       pop_deferring_access_checks ();
10318       if (type)
10319         do_type_instantiation (type, extension_specifier,
10320                                /*complain=*/tf_error);
10321     }
10322   else
10323     {
10324       cp_declarator *declarator;
10325       tree decl;
10326
10327       /* Parse the declarator.  */
10328       declarator
10329         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10330                                 /*ctor_dtor_or_conv_p=*/NULL,
10331                                 /*parenthesized_p=*/NULL,
10332                                 /*member_p=*/false);
10333       if (declares_class_or_enum & 2)
10334         cp_parser_check_for_definition_in_return_type (declarator,
10335                                                        decl_specifiers.type);
10336       if (declarator != cp_error_declarator)
10337         {
10338           decl = grokdeclarator (declarator, &decl_specifiers,
10339                                  NORMAL, 0, &decl_specifiers.attributes);
10340           /* Turn access control back on for names used during
10341              template instantiation.  */
10342           pop_deferring_access_checks ();
10343           /* Do the explicit instantiation.  */
10344           do_decl_instantiation (decl, extension_specifier);
10345         }
10346       else
10347         {
10348           pop_deferring_access_checks ();
10349           /* Skip the body of the explicit instantiation.  */
10350           cp_parser_skip_to_end_of_statement (parser);
10351         }
10352     }
10353   /* We're done with the instantiation.  */
10354   end_explicit_instantiation ();
10355
10356   cp_parser_consume_semicolon_at_end_of_statement (parser);
10357 }
10358
10359 /* Parse an explicit-specialization.
10360
10361    explicit-specialization:
10362      template < > declaration
10363
10364    Although the standard says `declaration', what it really means is:
10365
10366    explicit-specialization:
10367      template <> decl-specifier [opt] init-declarator [opt] ;
10368      template <> function-definition
10369      template <> explicit-specialization
10370      template <> template-declaration  */
10371
10372 static void
10373 cp_parser_explicit_specialization (cp_parser* parser)
10374 {
10375   bool need_lang_pop;
10376   /* Look for the `template' keyword.  */
10377   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10378   /* Look for the `<'.  */
10379   cp_parser_require (parser, CPP_LESS, "`<'");
10380   /* Look for the `>'.  */
10381   cp_parser_require (parser, CPP_GREATER, "`>'");
10382   /* We have processed another parameter list.  */
10383   ++parser->num_template_parameter_lists;
10384   /* [temp]
10385
10386      A template ... explicit specialization ... shall not have C
10387      linkage.  */
10388   if (current_lang_name == lang_name_c)
10389     {
10390       error ("template specialization with C linkage");
10391       /* Give it C++ linkage to avoid confusing other parts of the
10392          front end.  */
10393       push_lang_context (lang_name_cplusplus);
10394       need_lang_pop = true;
10395     }
10396   else
10397     need_lang_pop = false;
10398   /* Let the front end know that we are beginning a specialization.  */
10399   if (!begin_specialization ())
10400     {
10401       end_specialization ();
10402       cp_parser_skip_to_end_of_block_or_statement (parser);
10403       return;
10404     }
10405
10406   /* If the next keyword is `template', we need to figure out whether
10407      or not we're looking a template-declaration.  */
10408   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10409     {
10410       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10411           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10412         cp_parser_template_declaration_after_export (parser,
10413                                                      /*member_p=*/false);
10414       else
10415         cp_parser_explicit_specialization (parser);
10416     }
10417   else
10418     /* Parse the dependent declaration.  */
10419     cp_parser_single_declaration (parser,
10420                                   /*checks=*/NULL,
10421                                   /*member_p=*/false,
10422                                   /*explicit_specialization_p=*/true,
10423                                   /*friend_p=*/NULL);
10424   /* We're done with the specialization.  */
10425   end_specialization ();
10426   /* For the erroneous case of a template with C linkage, we pushed an
10427      implicit C++ linkage scope; exit that scope now.  */
10428   if (need_lang_pop)
10429     pop_lang_context ();
10430   /* We're done with this parameter list.  */
10431   --parser->num_template_parameter_lists;
10432 }
10433
10434 /* Parse a type-specifier.
10435
10436    type-specifier:
10437      simple-type-specifier
10438      class-specifier
10439      enum-specifier
10440      elaborated-type-specifier
10441      cv-qualifier
10442
10443    GNU Extension:
10444
10445    type-specifier:
10446      __complex__
10447
10448    Returns a representation of the type-specifier.  For a
10449    class-specifier, enum-specifier, or elaborated-type-specifier, a
10450    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10451
10452    The parser flags FLAGS is used to control type-specifier parsing.
10453
10454    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10455    in a decl-specifier-seq.
10456
10457    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10458    class-specifier, enum-specifier, or elaborated-type-specifier, then
10459    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10460    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10461    zero.
10462
10463    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10464    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10465    is set to FALSE.  */
10466
10467 static tree
10468 cp_parser_type_specifier (cp_parser* parser,
10469                           cp_parser_flags flags,
10470                           cp_decl_specifier_seq *decl_specs,
10471                           bool is_declaration,
10472                           int* declares_class_or_enum,
10473                           bool* is_cv_qualifier)
10474 {
10475   tree type_spec = NULL_TREE;
10476   cp_token *token;
10477   enum rid keyword;
10478   cp_decl_spec ds = ds_last;
10479
10480   /* Assume this type-specifier does not declare a new type.  */
10481   if (declares_class_or_enum)
10482     *declares_class_or_enum = 0;
10483   /* And that it does not specify a cv-qualifier.  */
10484   if (is_cv_qualifier)
10485     *is_cv_qualifier = false;
10486   /* Peek at the next token.  */
10487   token = cp_lexer_peek_token (parser->lexer);
10488
10489   /* If we're looking at a keyword, we can use that to guide the
10490      production we choose.  */
10491   keyword = token->keyword;
10492   switch (keyword)
10493     {
10494     case RID_ENUM:
10495       /* Look for the enum-specifier.  */
10496       type_spec = cp_parser_enum_specifier (parser);
10497       /* If that worked, we're done.  */
10498       if (type_spec)
10499         {
10500           if (declares_class_or_enum)
10501             *declares_class_or_enum = 2;
10502           if (decl_specs)
10503             cp_parser_set_decl_spec_type (decl_specs,
10504                                           type_spec,
10505                                           /*user_defined_p=*/true);
10506           return type_spec;
10507         }
10508       else
10509         goto elaborated_type_specifier;
10510
10511       /* Any of these indicate either a class-specifier, or an
10512          elaborated-type-specifier.  */
10513     case RID_CLASS:
10514     case RID_STRUCT:
10515     case RID_UNION:
10516       /* Parse tentatively so that we can back up if we don't find a
10517          class-specifier.  */
10518       cp_parser_parse_tentatively (parser);
10519       /* Look for the class-specifier.  */
10520       type_spec = cp_parser_class_specifier (parser);
10521       /* If that worked, we're done.  */
10522       if (cp_parser_parse_definitely (parser))
10523         {
10524           if (declares_class_or_enum)
10525             *declares_class_or_enum = 2;
10526           if (decl_specs)
10527             cp_parser_set_decl_spec_type (decl_specs,
10528                                           type_spec,
10529                                           /*user_defined_p=*/true);
10530           return type_spec;
10531         }
10532
10533       /* Fall through.  */
10534     elaborated_type_specifier:
10535       /* We're declaring (not defining) a class or enum.  */
10536       if (declares_class_or_enum)
10537         *declares_class_or_enum = 1;
10538
10539       /* Fall through.  */
10540     case RID_TYPENAME:
10541       /* Look for an elaborated-type-specifier.  */
10542       type_spec
10543         = (cp_parser_elaborated_type_specifier
10544            (parser,
10545             decl_specs && decl_specs->specs[(int) ds_friend],
10546             is_declaration));
10547       if (decl_specs)
10548         cp_parser_set_decl_spec_type (decl_specs,
10549                                       type_spec,
10550                                       /*user_defined_p=*/true);
10551       return type_spec;
10552
10553     case RID_CONST:
10554       ds = ds_const;
10555       if (is_cv_qualifier)
10556         *is_cv_qualifier = true;
10557       break;
10558
10559     case RID_VOLATILE:
10560       ds = ds_volatile;
10561       if (is_cv_qualifier)
10562         *is_cv_qualifier = true;
10563       break;
10564
10565     case RID_RESTRICT:
10566       ds = ds_restrict;
10567       if (is_cv_qualifier)
10568         *is_cv_qualifier = true;
10569       break;
10570
10571     case RID_COMPLEX:
10572       /* The `__complex__' keyword is a GNU extension.  */
10573       ds = ds_complex;
10574       break;
10575
10576     default:
10577       break;
10578     }
10579
10580   /* Handle simple keywords.  */
10581   if (ds != ds_last)
10582     {
10583       if (decl_specs)
10584         {
10585           ++decl_specs->specs[(int)ds];
10586           decl_specs->any_specifiers_p = true;
10587         }
10588       return cp_lexer_consume_token (parser->lexer)->u.value;
10589     }
10590
10591   /* If we do not already have a type-specifier, assume we are looking
10592      at a simple-type-specifier.  */
10593   type_spec = cp_parser_simple_type_specifier (parser,
10594                                                decl_specs,
10595                                                flags);
10596
10597   /* If we didn't find a type-specifier, and a type-specifier was not
10598      optional in this context, issue an error message.  */
10599   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10600     {
10601       cp_parser_error (parser, "expected type specifier");
10602       return error_mark_node;
10603     }
10604
10605   return type_spec;
10606 }
10607
10608 /* Parse a simple-type-specifier.
10609
10610    simple-type-specifier:
10611      :: [opt] nested-name-specifier [opt] type-name
10612      :: [opt] nested-name-specifier template template-id
10613      char
10614      wchar_t
10615      bool
10616      short
10617      int
10618      long
10619      signed
10620      unsigned
10621      float
10622      double
10623      void
10624
10625    C++0x Extension:
10626
10627    simple-type-specifier:
10628      decltype ( expression )   
10629
10630    GNU Extension:
10631
10632    simple-type-specifier:
10633      __typeof__ unary-expression
10634      __typeof__ ( type-id )
10635
10636    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
10637    appropriately updated.  */
10638
10639 static tree
10640 cp_parser_simple_type_specifier (cp_parser* parser,
10641                                  cp_decl_specifier_seq *decl_specs,
10642                                  cp_parser_flags flags)
10643 {
10644   tree type = NULL_TREE;
10645   cp_token *token;
10646
10647   /* Peek at the next token.  */
10648   token = cp_lexer_peek_token (parser->lexer);
10649
10650   /* If we're looking at a keyword, things are easy.  */
10651   switch (token->keyword)
10652     {
10653     case RID_CHAR:
10654       if (decl_specs)
10655         decl_specs->explicit_char_p = true;
10656       type = char_type_node;
10657       break;
10658     case RID_WCHAR:
10659       type = wchar_type_node;
10660       break;
10661     case RID_BOOL:
10662       type = boolean_type_node;
10663       break;
10664     case RID_SHORT:
10665       if (decl_specs)
10666         ++decl_specs->specs[(int) ds_short];
10667       type = short_integer_type_node;
10668       break;
10669     case RID_INT:
10670       if (decl_specs)
10671         decl_specs->explicit_int_p = true;
10672       type = integer_type_node;
10673       break;
10674     case RID_LONG:
10675       if (decl_specs)
10676         ++decl_specs->specs[(int) ds_long];
10677       type = long_integer_type_node;
10678       break;
10679     case RID_SIGNED:
10680       if (decl_specs)
10681         ++decl_specs->specs[(int) ds_signed];
10682       type = integer_type_node;
10683       break;
10684     case RID_UNSIGNED:
10685       if (decl_specs)
10686         ++decl_specs->specs[(int) ds_unsigned];
10687       type = unsigned_type_node;
10688       break;
10689     case RID_FLOAT:
10690       type = float_type_node;
10691       break;
10692     case RID_DOUBLE:
10693       type = double_type_node;
10694       break;
10695     case RID_VOID:
10696       type = void_type_node;
10697       break;
10698
10699     case RID_DECLTYPE:
10700       /* Parse the `decltype' type.  */
10701       type = cp_parser_decltype (parser);
10702
10703       if (decl_specs)
10704         cp_parser_set_decl_spec_type (decl_specs, type,
10705                                       /*user_defined_p=*/true);
10706
10707       return type;
10708
10709     case RID_TYPEOF:
10710       /* Consume the `typeof' token.  */
10711       cp_lexer_consume_token (parser->lexer);
10712       /* Parse the operand to `typeof'.  */
10713       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10714       /* If it is not already a TYPE, take its type.  */
10715       if (!TYPE_P (type))
10716         type = finish_typeof (type);
10717
10718       if (decl_specs)
10719         cp_parser_set_decl_spec_type (decl_specs, type,
10720                                       /*user_defined_p=*/true);
10721
10722       return type;
10723
10724     default:
10725       break;
10726     }
10727
10728   /* If the type-specifier was for a built-in type, we're done.  */
10729   if (type)
10730     {
10731       tree id;
10732
10733       /* Record the type.  */
10734       if (decl_specs
10735           && (token->keyword != RID_SIGNED
10736               && token->keyword != RID_UNSIGNED
10737               && token->keyword != RID_SHORT
10738               && token->keyword != RID_LONG))
10739         cp_parser_set_decl_spec_type (decl_specs,
10740                                       type,
10741                                       /*user_defined=*/false);
10742       if (decl_specs)
10743         decl_specs->any_specifiers_p = true;
10744
10745       /* Consume the token.  */
10746       id = cp_lexer_consume_token (parser->lexer)->u.value;
10747
10748       /* There is no valid C++ program where a non-template type is
10749          followed by a "<".  That usually indicates that the user thought
10750          that the type was a template.  */
10751       cp_parser_check_for_invalid_template_id (parser, type);
10752
10753       return TYPE_NAME (type);
10754     }
10755
10756   /* The type-specifier must be a user-defined type.  */
10757   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10758     {
10759       bool qualified_p;
10760       bool global_p;
10761
10762       /* Don't gobble tokens or issue error messages if this is an
10763          optional type-specifier.  */
10764       if (flags & CP_PARSER_FLAGS_OPTIONAL)
10765         cp_parser_parse_tentatively (parser);
10766
10767       /* Look for the optional `::' operator.  */
10768       global_p
10769         = (cp_parser_global_scope_opt (parser,
10770                                        /*current_scope_valid_p=*/false)
10771            != NULL_TREE);
10772       /* Look for the nested-name specifier.  */
10773       qualified_p
10774         = (cp_parser_nested_name_specifier_opt (parser,
10775                                                 /*typename_keyword_p=*/false,
10776                                                 /*check_dependency_p=*/true,
10777                                                 /*type_p=*/false,
10778                                                 /*is_declaration=*/false)
10779            != NULL_TREE);
10780       /* If we have seen a nested-name-specifier, and the next token
10781          is `template', then we are using the template-id production.  */
10782       if (parser->scope
10783           && cp_parser_optional_template_keyword (parser))
10784         {
10785           /* Look for the template-id.  */
10786           type = cp_parser_template_id (parser,
10787                                         /*template_keyword_p=*/true,
10788                                         /*check_dependency_p=*/true,
10789                                         /*is_declaration=*/false);
10790           /* If the template-id did not name a type, we are out of
10791              luck.  */
10792           if (TREE_CODE (type) != TYPE_DECL)
10793             {
10794               cp_parser_error (parser, "expected template-id for type");
10795               type = NULL_TREE;
10796             }
10797         }
10798       /* Otherwise, look for a type-name.  */
10799       else
10800         type = cp_parser_type_name (parser);
10801       /* Keep track of all name-lookups performed in class scopes.  */
10802       if (type
10803           && !global_p
10804           && !qualified_p
10805           && TREE_CODE (type) == TYPE_DECL
10806           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10807         maybe_note_name_used_in_class (DECL_NAME (type), type);
10808       /* If it didn't work out, we don't have a TYPE.  */
10809       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10810           && !cp_parser_parse_definitely (parser))
10811         type = NULL_TREE;
10812       if (type && decl_specs)
10813         cp_parser_set_decl_spec_type (decl_specs, type,
10814                                       /*user_defined=*/true);
10815     }
10816
10817   /* If we didn't get a type-name, issue an error message.  */
10818   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10819     {
10820       cp_parser_error (parser, "expected type-name");
10821       return error_mark_node;
10822     }
10823
10824   /* There is no valid C++ program where a non-template type is
10825      followed by a "<".  That usually indicates that the user thought
10826      that the type was a template.  */
10827   if (type && type != error_mark_node)
10828     {
10829       /* As a last-ditch effort, see if TYPE is an Objective-C type.
10830          If it is, then the '<'...'>' enclose protocol names rather than
10831          template arguments, and so everything is fine.  */
10832       if (c_dialect_objc ()
10833           && (objc_is_id (type) || objc_is_class_name (type)))
10834         {
10835           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10836           tree qual_type = objc_get_protocol_qualified_type (type, protos);
10837
10838           /* Clobber the "unqualified" type previously entered into
10839              DECL_SPECS with the new, improved protocol-qualified version.  */
10840           if (decl_specs)
10841             decl_specs->type = qual_type;
10842
10843           return qual_type;
10844         }
10845
10846       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10847     }
10848
10849   return type;
10850 }
10851
10852 /* Parse a type-name.
10853
10854    type-name:
10855      class-name
10856      enum-name
10857      typedef-name
10858
10859    enum-name:
10860      identifier
10861
10862    typedef-name:
10863      identifier
10864
10865    Returns a TYPE_DECL for the type.  */
10866
10867 static tree
10868 cp_parser_type_name (cp_parser* parser)
10869 {
10870   tree type_decl;
10871   tree identifier;
10872
10873   /* We can't know yet whether it is a class-name or not.  */
10874   cp_parser_parse_tentatively (parser);
10875   /* Try a class-name.  */
10876   type_decl = cp_parser_class_name (parser,
10877                                     /*typename_keyword_p=*/false,
10878                                     /*template_keyword_p=*/false,
10879                                     none_type,
10880                                     /*check_dependency_p=*/true,
10881                                     /*class_head_p=*/false,
10882                                     /*is_declaration=*/false);
10883   /* If it's not a class-name, keep looking.  */
10884   if (!cp_parser_parse_definitely (parser))
10885     {
10886       /* It must be a typedef-name or an enum-name.  */
10887       identifier = cp_parser_identifier (parser);
10888       if (identifier == error_mark_node)
10889         return error_mark_node;
10890
10891       /* Look up the type-name.  */
10892       type_decl = cp_parser_lookup_name_simple (parser, identifier);
10893
10894       if (TREE_CODE (type_decl) != TYPE_DECL
10895           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10896         {
10897           /* See if this is an Objective-C type.  */
10898           tree protos = cp_parser_objc_protocol_refs_opt (parser);
10899           tree type = objc_get_protocol_qualified_type (identifier, protos);
10900           if (type)
10901             type_decl = TYPE_NAME (type);
10902         }
10903
10904       /* Issue an error if we did not find a type-name.  */
10905       if (TREE_CODE (type_decl) != TYPE_DECL)
10906         {
10907           if (!cp_parser_simulate_error (parser))
10908             cp_parser_name_lookup_error (parser, identifier, type_decl,
10909                                          "is not a type");
10910           type_decl = error_mark_node;
10911         }
10912       /* Remember that the name was used in the definition of the
10913          current class so that we can check later to see if the
10914          meaning would have been different after the class was
10915          entirely defined.  */
10916       else if (type_decl != error_mark_node
10917                && !parser->scope)
10918         maybe_note_name_used_in_class (identifier, type_decl);
10919     }
10920
10921   return type_decl;
10922 }
10923
10924
10925 /* Parse an elaborated-type-specifier.  Note that the grammar given
10926    here incorporates the resolution to DR68.
10927
10928    elaborated-type-specifier:
10929      class-key :: [opt] nested-name-specifier [opt] identifier
10930      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10931      enum :: [opt] nested-name-specifier [opt] identifier
10932      typename :: [opt] nested-name-specifier identifier
10933      typename :: [opt] nested-name-specifier template [opt]
10934        template-id
10935
10936    GNU extension:
10937
10938    elaborated-type-specifier:
10939      class-key attributes :: [opt] nested-name-specifier [opt] identifier
10940      class-key attributes :: [opt] nested-name-specifier [opt]
10941                template [opt] template-id
10942      enum attributes :: [opt] nested-name-specifier [opt] identifier
10943
10944    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10945    declared `friend'.  If IS_DECLARATION is TRUE, then this
10946    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10947    something is being declared.
10948
10949    Returns the TYPE specified.  */
10950
10951 static tree
10952 cp_parser_elaborated_type_specifier (cp_parser* parser,
10953                                      bool is_friend,
10954                                      bool is_declaration)
10955 {
10956   enum tag_types tag_type;
10957   tree identifier;
10958   tree type = NULL_TREE;
10959   tree attributes = NULL_TREE;
10960
10961   /* See if we're looking at the `enum' keyword.  */
10962   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10963     {
10964       /* Consume the `enum' token.  */
10965       cp_lexer_consume_token (parser->lexer);
10966       /* Remember that it's an enumeration type.  */
10967       tag_type = enum_type;
10968       /* Parse the attributes.  */
10969       attributes = cp_parser_attributes_opt (parser);
10970     }
10971   /* Or, it might be `typename'.  */
10972   else if (cp_lexer_next_token_is_keyword (parser->lexer,
10973                                            RID_TYPENAME))
10974     {
10975       /* Consume the `typename' token.  */
10976       cp_lexer_consume_token (parser->lexer);
10977       /* Remember that it's a `typename' type.  */
10978       tag_type = typename_type;
10979       /* The `typename' keyword is only allowed in templates.  */
10980       if (!processing_template_decl)
10981         pedwarn ("using %<typename%> outside of template");
10982     }
10983   /* Otherwise it must be a class-key.  */
10984   else
10985     {
10986       tag_type = cp_parser_class_key (parser);
10987       if (tag_type == none_type)
10988         return error_mark_node;
10989       /* Parse the attributes.  */
10990       attributes = cp_parser_attributes_opt (parser);
10991     }
10992
10993   /* Look for the `::' operator.  */
10994   cp_parser_global_scope_opt (parser,
10995                               /*current_scope_valid_p=*/false);
10996   /* Look for the nested-name-specifier.  */
10997   if (tag_type == typename_type)
10998     {
10999       if (!cp_parser_nested_name_specifier (parser,
11000                                            /*typename_keyword_p=*/true,
11001                                            /*check_dependency_p=*/true,
11002                                            /*type_p=*/true,
11003                                             is_declaration))
11004         return error_mark_node;
11005     }
11006   else
11007     /* Even though `typename' is not present, the proposed resolution
11008        to Core Issue 180 says that in `class A<T>::B', `B' should be
11009        considered a type-name, even if `A<T>' is dependent.  */
11010     cp_parser_nested_name_specifier_opt (parser,
11011                                          /*typename_keyword_p=*/true,
11012                                          /*check_dependency_p=*/true,
11013                                          /*type_p=*/true,
11014                                          is_declaration);
11015  /* For everything but enumeration types, consider a template-id.
11016     For an enumeration type, consider only a plain identifier.  */
11017   if (tag_type != enum_type)
11018     {
11019       bool template_p = false;
11020       tree decl;
11021
11022       /* Allow the `template' keyword.  */
11023       template_p = cp_parser_optional_template_keyword (parser);
11024       /* If we didn't see `template', we don't know if there's a
11025          template-id or not.  */
11026       if (!template_p)
11027         cp_parser_parse_tentatively (parser);
11028       /* Parse the template-id.  */
11029       decl = cp_parser_template_id (parser, template_p,
11030                                     /*check_dependency_p=*/true,
11031                                     is_declaration);
11032       /* If we didn't find a template-id, look for an ordinary
11033          identifier.  */
11034       if (!template_p && !cp_parser_parse_definitely (parser))
11035         ;
11036       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11037          in effect, then we must assume that, upon instantiation, the
11038          template will correspond to a class.  */
11039       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11040                && tag_type == typename_type)
11041         type = make_typename_type (parser->scope, decl,
11042                                    typename_type,
11043                                    /*complain=*/tf_error);
11044       else
11045         type = TREE_TYPE (decl);
11046     }
11047
11048   if (!type)
11049     {
11050       identifier = cp_parser_identifier (parser);
11051
11052       if (identifier == error_mark_node)
11053         {
11054           parser->scope = NULL_TREE;
11055           return error_mark_node;
11056         }
11057
11058       /* For a `typename', we needn't call xref_tag.  */
11059       if (tag_type == typename_type
11060           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11061         return cp_parser_make_typename_type (parser, parser->scope,
11062                                              identifier);
11063       /* Look up a qualified name in the usual way.  */
11064       if (parser->scope)
11065         {
11066           tree decl;
11067           tree ambiguous_decls;
11068
11069           decl = cp_parser_lookup_name (parser, identifier,
11070                                         tag_type,
11071                                         /*is_template=*/false,
11072                                         /*is_namespace=*/false,
11073                                         /*check_dependency=*/true,
11074                                         &ambiguous_decls);
11075
11076           /* If the lookup was ambiguous, an error will already have been
11077              issued.  */
11078           if (ambiguous_decls)
11079             return error_mark_node;
11080
11081           /* If we are parsing friend declaration, DECL may be a
11082              TEMPLATE_DECL tree node here.  However, we need to check
11083              whether this TEMPLATE_DECL results in valid code.  Consider
11084              the following example:
11085
11086                namespace N {
11087                  template <class T> class C {};
11088                }
11089                class X {
11090                  template <class T> friend class N::C; // #1, valid code
11091                };
11092                template <class T> class Y {
11093                  friend class N::C;                    // #2, invalid code
11094                };
11095
11096              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11097              name lookup of `N::C'.  We see that friend declaration must
11098              be template for the code to be valid.  Note that
11099              processing_template_decl does not work here since it is
11100              always 1 for the above two cases.  */
11101
11102           decl = (cp_parser_maybe_treat_template_as_class
11103                   (decl, /*tag_name_p=*/is_friend
11104                          && parser->num_template_parameter_lists));
11105
11106           if (TREE_CODE (decl) != TYPE_DECL)
11107             {
11108               cp_parser_diagnose_invalid_type_name (parser,
11109                                                     parser->scope,
11110                                                     identifier);
11111               return error_mark_node;
11112             }
11113
11114           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11115             {
11116               bool allow_template = (parser->num_template_parameter_lists
11117                                       || DECL_SELF_REFERENCE_P (decl));
11118               type = check_elaborated_type_specifier (tag_type, decl, 
11119                                                       allow_template);
11120
11121               if (type == error_mark_node)
11122                 return error_mark_node;
11123             }
11124
11125           /* Forward declarations of nested types, such as
11126
11127                class C1::C2;
11128                class C1::C2::C3;
11129
11130              are invalid unless all components preceding the final '::'
11131              are complete.  If all enclosing types are complete, these
11132              declarations become merely pointless.
11133
11134              Invalid forward declarations of nested types are errors
11135              caught elsewhere in parsing.  Those that are pointless arrive
11136              here.  */
11137
11138           if (cp_parser_declares_only_class_p (parser)
11139               && !is_friend && !processing_explicit_instantiation)
11140             warning (0, "declaration %qD does not declare anything", decl);
11141
11142           type = TREE_TYPE (decl);
11143         }
11144       else
11145         {
11146           /* An elaborated-type-specifier sometimes introduces a new type and
11147              sometimes names an existing type.  Normally, the rule is that it
11148              introduces a new type only if there is not an existing type of
11149              the same name already in scope.  For example, given:
11150
11151                struct S {};
11152                void f() { struct S s; }
11153
11154              the `struct S' in the body of `f' is the same `struct S' as in
11155              the global scope; the existing definition is used.  However, if
11156              there were no global declaration, this would introduce a new
11157              local class named `S'.
11158
11159              An exception to this rule applies to the following code:
11160
11161                namespace N { struct S; }
11162
11163              Here, the elaborated-type-specifier names a new type
11164              unconditionally; even if there is already an `S' in the
11165              containing scope this declaration names a new type.
11166              This exception only applies if the elaborated-type-specifier
11167              forms the complete declaration:
11168
11169                [class.name]
11170
11171                A declaration consisting solely of `class-key identifier ;' is
11172                either a redeclaration of the name in the current scope or a
11173                forward declaration of the identifier as a class name.  It
11174                introduces the name into the current scope.
11175
11176              We are in this situation precisely when the next token is a `;'.
11177
11178              An exception to the exception is that a `friend' declaration does
11179              *not* name a new type; i.e., given:
11180
11181                struct S { friend struct T; };
11182
11183              `T' is not a new type in the scope of `S'.
11184
11185              Also, `new struct S' or `sizeof (struct S)' never results in the
11186              definition of a new type; a new type can only be declared in a
11187              declaration context.  */
11188
11189           tag_scope ts;
11190           bool template_p;
11191
11192           if (is_friend)
11193             /* Friends have special name lookup rules.  */
11194             ts = ts_within_enclosing_non_class;
11195           else if (is_declaration
11196                    && cp_lexer_next_token_is (parser->lexer,
11197                                               CPP_SEMICOLON))
11198             /* This is a `class-key identifier ;' */
11199             ts = ts_current;
11200           else
11201             ts = ts_global;
11202
11203           template_p =
11204             (parser->num_template_parameter_lists
11205              && (cp_parser_next_token_starts_class_definition_p (parser)
11206                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11207           /* An unqualified name was used to reference this type, so
11208              there were no qualifying templates.  */
11209           if (!cp_parser_check_template_parameters (parser,
11210                                                     /*num_templates=*/0))
11211             return error_mark_node;
11212           type = xref_tag (tag_type, identifier, ts, template_p);
11213         }
11214     }
11215
11216   if (type == error_mark_node)
11217     return error_mark_node;
11218
11219   /* Allow attributes on forward declarations of classes.  */
11220   if (attributes)
11221     {
11222       if (TREE_CODE (type) == TYPENAME_TYPE)
11223         warning (OPT_Wattributes,
11224                  "attributes ignored on uninstantiated type");
11225       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11226                && ! processing_explicit_instantiation)
11227         warning (OPT_Wattributes,
11228                  "attributes ignored on template instantiation");
11229       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11230         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11231       else
11232         warning (OPT_Wattributes,
11233                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11234     }
11235
11236   if (tag_type != enum_type)
11237     cp_parser_check_class_key (tag_type, type);
11238
11239   /* A "<" cannot follow an elaborated type specifier.  If that
11240      happens, the user was probably trying to form a template-id.  */
11241   cp_parser_check_for_invalid_template_id (parser, type);
11242
11243   return type;
11244 }
11245
11246 /* Parse an enum-specifier.
11247
11248    enum-specifier:
11249      enum identifier [opt] { enumerator-list [opt] }
11250
11251    GNU Extensions:
11252      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11253        attributes[opt]
11254
11255    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11256    if the token stream isn't an enum-specifier after all.  */
11257
11258 static tree
11259 cp_parser_enum_specifier (cp_parser* parser)
11260 {
11261   tree identifier;
11262   tree type;
11263   tree attributes;
11264
11265   /* Parse tentatively so that we can back up if we don't find a
11266      enum-specifier.  */
11267   cp_parser_parse_tentatively (parser);
11268
11269   /* Caller guarantees that the current token is 'enum', an identifier
11270      possibly follows, and the token after that is an opening brace.
11271      If we don't have an identifier, fabricate an anonymous name for
11272      the enumeration being defined.  */
11273   cp_lexer_consume_token (parser->lexer);
11274
11275   attributes = cp_parser_attributes_opt (parser);
11276
11277   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11278     identifier = cp_parser_identifier (parser);
11279   else
11280     identifier = make_anon_name ();
11281
11282   /* Look for the `{' but don't consume it yet.  */
11283   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11284     cp_parser_simulate_error (parser);
11285
11286   if (!cp_parser_parse_definitely (parser))
11287     return NULL_TREE;
11288
11289   /* Issue an error message if type-definitions are forbidden here.  */
11290   if (!cp_parser_check_type_definition (parser))
11291     type = error_mark_node;
11292   else
11293     /* Create the new type.  We do this before consuming the opening
11294        brace so the enum will be recorded as being on the line of its
11295        tag (or the 'enum' keyword, if there is no tag).  */
11296     type = start_enum (identifier);
11297   
11298   /* Consume the opening brace.  */
11299   cp_lexer_consume_token (parser->lexer);
11300
11301   if (type == error_mark_node)
11302     {
11303       cp_parser_skip_to_end_of_block_or_statement (parser);
11304       return error_mark_node;
11305     }
11306
11307   /* If the next token is not '}', then there are some enumerators.  */
11308   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11309     cp_parser_enumerator_list (parser, type);
11310
11311   /* Consume the final '}'.  */
11312   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11313
11314   /* Look for trailing attributes to apply to this enumeration, and
11315      apply them if appropriate.  */
11316   if (cp_parser_allow_gnu_extensions_p (parser))
11317     {
11318       tree trailing_attr = cp_parser_attributes_opt (parser);
11319       cplus_decl_attributes (&type,
11320                              trailing_attr,
11321                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11322     }
11323
11324   /* Finish up the enumeration.  */
11325   finish_enum (type);
11326
11327   return type;
11328 }
11329
11330 /* Parse an enumerator-list.  The enumerators all have the indicated
11331    TYPE.
11332
11333    enumerator-list:
11334      enumerator-definition
11335      enumerator-list , enumerator-definition  */
11336
11337 static void
11338 cp_parser_enumerator_list (cp_parser* parser, tree type)
11339 {
11340   while (true)
11341     {
11342       /* Parse an enumerator-definition.  */
11343       cp_parser_enumerator_definition (parser, type);
11344
11345       /* If the next token is not a ',', we've reached the end of
11346          the list.  */
11347       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11348         break;
11349       /* Otherwise, consume the `,' and keep going.  */
11350       cp_lexer_consume_token (parser->lexer);
11351       /* If the next token is a `}', there is a trailing comma.  */
11352       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11353         {
11354           if (pedantic && !in_system_header)
11355             pedwarn ("comma at end of enumerator list");
11356           break;
11357         }
11358     }
11359 }
11360
11361 /* Parse an enumerator-definition.  The enumerator has the indicated
11362    TYPE.
11363
11364    enumerator-definition:
11365      enumerator
11366      enumerator = constant-expression
11367
11368    enumerator:
11369      identifier  */
11370
11371 static void
11372 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11373 {
11374   tree identifier;
11375   tree value;
11376
11377   /* Look for the identifier.  */
11378   identifier = cp_parser_identifier (parser);
11379   if (identifier == error_mark_node)
11380     return;
11381
11382   /* If the next token is an '=', then there is an explicit value.  */
11383   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11384     {
11385       /* Consume the `=' token.  */
11386       cp_lexer_consume_token (parser->lexer);
11387       /* Parse the value.  */
11388       value = cp_parser_constant_expression (parser,
11389                                              /*allow_non_constant_p=*/false,
11390                                              NULL);
11391     }
11392   else
11393     value = NULL_TREE;
11394
11395   /* Create the enumerator.  */
11396   build_enumerator (identifier, value, type);
11397 }
11398
11399 /* Parse a namespace-name.
11400
11401    namespace-name:
11402      original-namespace-name
11403      namespace-alias
11404
11405    Returns the NAMESPACE_DECL for the namespace.  */
11406
11407 static tree
11408 cp_parser_namespace_name (cp_parser* parser)
11409 {
11410   tree identifier;
11411   tree namespace_decl;
11412
11413   /* Get the name of the namespace.  */
11414   identifier = cp_parser_identifier (parser);
11415   if (identifier == error_mark_node)
11416     return error_mark_node;
11417
11418   /* Look up the identifier in the currently active scope.  Look only
11419      for namespaces, due to:
11420
11421        [basic.lookup.udir]
11422
11423        When looking up a namespace-name in a using-directive or alias
11424        definition, only namespace names are considered.
11425
11426      And:
11427
11428        [basic.lookup.qual]
11429
11430        During the lookup of a name preceding the :: scope resolution
11431        operator, object, function, and enumerator names are ignored.
11432
11433      (Note that cp_parser_class_or_namespace_name only calls this
11434      function if the token after the name is the scope resolution
11435      operator.)  */
11436   namespace_decl = cp_parser_lookup_name (parser, identifier,
11437                                           none_type,
11438                                           /*is_template=*/false,
11439                                           /*is_namespace=*/true,
11440                                           /*check_dependency=*/true,
11441                                           /*ambiguous_decls=*/NULL);
11442   /* If it's not a namespace, issue an error.  */
11443   if (namespace_decl == error_mark_node
11444       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11445     {
11446       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11447         error ("%qD is not a namespace-name", identifier);
11448       cp_parser_error (parser, "expected namespace-name");
11449       namespace_decl = error_mark_node;
11450     }
11451
11452   return namespace_decl;
11453 }
11454
11455 /* Parse a namespace-definition.
11456
11457    namespace-definition:
11458      named-namespace-definition
11459      unnamed-namespace-definition
11460
11461    named-namespace-definition:
11462      original-namespace-definition
11463      extension-namespace-definition
11464
11465    original-namespace-definition:
11466      namespace identifier { namespace-body }
11467
11468    extension-namespace-definition:
11469      namespace original-namespace-name { namespace-body }
11470
11471    unnamed-namespace-definition:
11472      namespace { namespace-body } */
11473
11474 static void
11475 cp_parser_namespace_definition (cp_parser* parser)
11476 {
11477   tree identifier, attribs;
11478
11479   /* Look for the `namespace' keyword.  */
11480   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11481
11482   /* Get the name of the namespace.  We do not attempt to distinguish
11483      between an original-namespace-definition and an
11484      extension-namespace-definition at this point.  The semantic
11485      analysis routines are responsible for that.  */
11486   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11487     identifier = cp_parser_identifier (parser);
11488   else
11489     identifier = NULL_TREE;
11490
11491   /* Parse any specified attributes.  */
11492   attribs = cp_parser_attributes_opt (parser);
11493
11494   /* Look for the `{' to start the namespace.  */
11495   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11496   /* Start the namespace.  */
11497   push_namespace_with_attribs (identifier, attribs);
11498   /* Parse the body of the namespace.  */
11499   cp_parser_namespace_body (parser);
11500   /* Finish the namespace.  */
11501   pop_namespace ();
11502   /* Look for the final `}'.  */
11503   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11504 }
11505
11506 /* Parse a namespace-body.
11507
11508    namespace-body:
11509      declaration-seq [opt]  */
11510
11511 static void
11512 cp_parser_namespace_body (cp_parser* parser)
11513 {
11514   cp_parser_declaration_seq_opt (parser);
11515 }
11516
11517 /* Parse a namespace-alias-definition.
11518
11519    namespace-alias-definition:
11520      namespace identifier = qualified-namespace-specifier ;  */
11521
11522 static void
11523 cp_parser_namespace_alias_definition (cp_parser* parser)
11524 {
11525   tree identifier;
11526   tree namespace_specifier;
11527
11528   /* Look for the `namespace' keyword.  */
11529   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11530   /* Look for the identifier.  */
11531   identifier = cp_parser_identifier (parser);
11532   if (identifier == error_mark_node)
11533     return;
11534   /* Look for the `=' token.  */
11535   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11536       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
11537     {
11538       error ("%<namespace%> definition is not allowed here");
11539       /* Skip the definition.  */
11540       cp_lexer_consume_token (parser->lexer);
11541       if (cp_parser_skip_to_closing_brace (parser))
11542         cp_lexer_consume_token (parser->lexer);
11543       return;
11544     }
11545   cp_parser_require (parser, CPP_EQ, "`='");
11546   /* Look for the qualified-namespace-specifier.  */
11547   namespace_specifier
11548     = cp_parser_qualified_namespace_specifier (parser);
11549   /* Look for the `;' token.  */
11550   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11551
11552   /* Register the alias in the symbol table.  */
11553   do_namespace_alias (identifier, namespace_specifier);
11554 }
11555
11556 /* Parse a qualified-namespace-specifier.
11557
11558    qualified-namespace-specifier:
11559      :: [opt] nested-name-specifier [opt] namespace-name
11560
11561    Returns a NAMESPACE_DECL corresponding to the specified
11562    namespace.  */
11563
11564 static tree
11565 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11566 {
11567   /* Look for the optional `::'.  */
11568   cp_parser_global_scope_opt (parser,
11569                               /*current_scope_valid_p=*/false);
11570
11571   /* Look for the optional nested-name-specifier.  */
11572   cp_parser_nested_name_specifier_opt (parser,
11573                                        /*typename_keyword_p=*/false,
11574                                        /*check_dependency_p=*/true,
11575                                        /*type_p=*/false,
11576                                        /*is_declaration=*/true);
11577
11578   return cp_parser_namespace_name (parser);
11579 }
11580
11581 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11582    access declaration.
11583
11584    using-declaration:
11585      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11586      using :: unqualified-id ;  
11587
11588    access-declaration:
11589      qualified-id ;  
11590
11591    */
11592
11593 static bool
11594 cp_parser_using_declaration (cp_parser* parser, 
11595                              bool access_declaration_p)
11596 {
11597   cp_token *token;
11598   bool typename_p = false;
11599   bool global_scope_p;
11600   tree decl;
11601   tree identifier;
11602   tree qscope;
11603
11604   if (access_declaration_p)
11605     cp_parser_parse_tentatively (parser);
11606   else
11607     {
11608       /* Look for the `using' keyword.  */
11609       cp_parser_require_keyword (parser, RID_USING, "`using'");
11610       
11611       /* Peek at the next token.  */
11612       token = cp_lexer_peek_token (parser->lexer);
11613       /* See if it's `typename'.  */
11614       if (token->keyword == RID_TYPENAME)
11615         {
11616           /* Remember that we've seen it.  */
11617           typename_p = true;
11618           /* Consume the `typename' token.  */
11619           cp_lexer_consume_token (parser->lexer);
11620         }
11621     }
11622
11623   /* Look for the optional global scope qualification.  */
11624   global_scope_p
11625     = (cp_parser_global_scope_opt (parser,
11626                                    /*current_scope_valid_p=*/false)
11627        != NULL_TREE);
11628
11629   /* If we saw `typename', or didn't see `::', then there must be a
11630      nested-name-specifier present.  */
11631   if (typename_p || !global_scope_p)
11632     qscope = cp_parser_nested_name_specifier (parser, typename_p,
11633                                               /*check_dependency_p=*/true,
11634                                               /*type_p=*/false,
11635                                               /*is_declaration=*/true);
11636   /* Otherwise, we could be in either of the two productions.  In that
11637      case, treat the nested-name-specifier as optional.  */
11638   else
11639     qscope = cp_parser_nested_name_specifier_opt (parser,
11640                                                   /*typename_keyword_p=*/false,
11641                                                   /*check_dependency_p=*/true,
11642                                                   /*type_p=*/false,
11643                                                   /*is_declaration=*/true);
11644   if (!qscope)
11645     qscope = global_namespace;
11646
11647   if (access_declaration_p && cp_parser_error_occurred (parser))
11648     /* Something has already gone wrong; there's no need to parse
11649        further.  Since an error has occurred, the return value of
11650        cp_parser_parse_definitely will be false, as required.  */
11651     return cp_parser_parse_definitely (parser);
11652
11653   /* Parse the unqualified-id.  */
11654   identifier = cp_parser_unqualified_id (parser,
11655                                          /*template_keyword_p=*/false,
11656                                          /*check_dependency_p=*/true,
11657                                          /*declarator_p=*/true,
11658                                          /*optional_p=*/false);
11659
11660   if (access_declaration_p)
11661     {
11662       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11663         cp_parser_simulate_error (parser);
11664       if (!cp_parser_parse_definitely (parser))
11665         return false;
11666     }
11667
11668   /* The function we call to handle a using-declaration is different
11669      depending on what scope we are in.  */
11670   if (qscope == error_mark_node || identifier == error_mark_node)
11671     ;
11672   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11673            && TREE_CODE (identifier) != BIT_NOT_EXPR)
11674     /* [namespace.udecl]
11675
11676        A using declaration shall not name a template-id.  */
11677     error ("a template-id may not appear in a using-declaration");
11678   else
11679     {
11680       if (at_class_scope_p ())
11681         {
11682           /* Create the USING_DECL.  */
11683           decl = do_class_using_decl (parser->scope, identifier);
11684           /* Add it to the list of members in this class.  */
11685           finish_member_declaration (decl);
11686         }
11687       else
11688         {
11689           decl = cp_parser_lookup_name_simple (parser, identifier);
11690           if (decl == error_mark_node)
11691             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11692           else if (!at_namespace_scope_p ())
11693             do_local_using_decl (decl, qscope, identifier);
11694           else
11695             do_toplevel_using_decl (decl, qscope, identifier);
11696         }
11697     }
11698
11699   /* Look for the final `;'.  */
11700   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11701   
11702   return true;
11703 }
11704
11705 /* Parse a using-directive.
11706
11707    using-directive:
11708      using namespace :: [opt] nested-name-specifier [opt]
11709        namespace-name ;  */
11710
11711 static void
11712 cp_parser_using_directive (cp_parser* parser)
11713 {
11714   tree namespace_decl;
11715   tree attribs;
11716
11717   /* Look for the `using' keyword.  */
11718   cp_parser_require_keyword (parser, RID_USING, "`using'");
11719   /* And the `namespace' keyword.  */
11720   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11721   /* Look for the optional `::' operator.  */
11722   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11723   /* And the optional nested-name-specifier.  */
11724   cp_parser_nested_name_specifier_opt (parser,
11725                                        /*typename_keyword_p=*/false,
11726                                        /*check_dependency_p=*/true,
11727                                        /*type_p=*/false,
11728                                        /*is_declaration=*/true);
11729   /* Get the namespace being used.  */
11730   namespace_decl = cp_parser_namespace_name (parser);
11731   /* And any specified attributes.  */
11732   attribs = cp_parser_attributes_opt (parser);
11733   /* Update the symbol table.  */
11734   parse_using_directive (namespace_decl, attribs);
11735   /* Look for the final `;'.  */
11736   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11737 }
11738
11739 /* Parse an asm-definition.
11740
11741    asm-definition:
11742      asm ( string-literal ) ;
11743
11744    GNU Extension:
11745
11746    asm-definition:
11747      asm volatile [opt] ( string-literal ) ;
11748      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11749      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11750                           : asm-operand-list [opt] ) ;
11751      asm volatile [opt] ( string-literal : asm-operand-list [opt]
11752                           : asm-operand-list [opt]
11753                           : asm-operand-list [opt] ) ;  */
11754
11755 static void
11756 cp_parser_asm_definition (cp_parser* parser)
11757 {
11758   tree string;
11759   tree outputs = NULL_TREE;
11760   tree inputs = NULL_TREE;
11761   tree clobbers = NULL_TREE;
11762   tree asm_stmt;
11763   bool volatile_p = false;
11764   bool extended_p = false;
11765   bool invalid_inputs_p = false;
11766   bool invalid_outputs_p = false;
11767
11768   /* Look for the `asm' keyword.  */
11769   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11770   /* See if the next token is `volatile'.  */
11771   if (cp_parser_allow_gnu_extensions_p (parser)
11772       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11773     {
11774       /* Remember that we saw the `volatile' keyword.  */
11775       volatile_p = true;
11776       /* Consume the token.  */
11777       cp_lexer_consume_token (parser->lexer);
11778     }
11779   /* Look for the opening `('.  */
11780   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11781     return;
11782   /* Look for the string.  */
11783   string = cp_parser_string_literal (parser, false, false);
11784   if (string == error_mark_node)
11785     {
11786       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11787                                              /*consume_paren=*/true);
11788       return;
11789     }
11790
11791   /* If we're allowing GNU extensions, check for the extended assembly
11792      syntax.  Unfortunately, the `:' tokens need not be separated by
11793      a space in C, and so, for compatibility, we tolerate that here
11794      too.  Doing that means that we have to treat the `::' operator as
11795      two `:' tokens.  */
11796   if (cp_parser_allow_gnu_extensions_p (parser)
11797       && parser->in_function_body
11798       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11799           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11800     {
11801       bool inputs_p = false;
11802       bool clobbers_p = false;
11803
11804       /* The extended syntax was used.  */
11805       extended_p = true;
11806
11807       /* Look for outputs.  */
11808       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11809         {
11810           /* Consume the `:'.  */
11811           cp_lexer_consume_token (parser->lexer);
11812           /* Parse the output-operands.  */
11813           if (cp_lexer_next_token_is_not (parser->lexer,
11814                                           CPP_COLON)
11815               && cp_lexer_next_token_is_not (parser->lexer,
11816                                              CPP_SCOPE)
11817               && cp_lexer_next_token_is_not (parser->lexer,
11818                                              CPP_CLOSE_PAREN))
11819             outputs = cp_parser_asm_operand_list (parser);
11820
11821             if (outputs == error_mark_node)
11822               invalid_outputs_p = true;
11823         }
11824       /* If the next token is `::', there are no outputs, and the
11825          next token is the beginning of the inputs.  */
11826       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11827         /* The inputs are coming next.  */
11828         inputs_p = true;
11829
11830       /* Look for inputs.  */
11831       if (inputs_p
11832           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11833         {
11834           /* Consume the `:' or `::'.  */
11835           cp_lexer_consume_token (parser->lexer);
11836           /* Parse the output-operands.  */
11837           if (cp_lexer_next_token_is_not (parser->lexer,
11838                                           CPP_COLON)
11839               && cp_lexer_next_token_is_not (parser->lexer,
11840                                              CPP_CLOSE_PAREN))
11841             inputs = cp_parser_asm_operand_list (parser);
11842
11843             if (inputs == error_mark_node)
11844               invalid_inputs_p = true;
11845         }
11846       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11847         /* The clobbers are coming next.  */
11848         clobbers_p = true;
11849
11850       /* Look for clobbers.  */
11851       if (clobbers_p
11852           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11853         {
11854           /* Consume the `:' or `::'.  */
11855           cp_lexer_consume_token (parser->lexer);
11856           /* Parse the clobbers.  */
11857           if (cp_lexer_next_token_is_not (parser->lexer,
11858                                           CPP_CLOSE_PAREN))
11859             clobbers = cp_parser_asm_clobber_list (parser);
11860         }
11861     }
11862   /* Look for the closing `)'.  */
11863   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11864     cp_parser_skip_to_closing_parenthesis (parser, true, false,
11865                                            /*consume_paren=*/true);
11866   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11867
11868   if (!invalid_inputs_p && !invalid_outputs_p)
11869     {
11870       /* Create the ASM_EXPR.  */
11871       if (parser->in_function_body)
11872         {
11873           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11874                                       inputs, clobbers);
11875           /* If the extended syntax was not used, mark the ASM_EXPR.  */
11876           if (!extended_p)
11877             {
11878               tree temp = asm_stmt;
11879               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11880                 temp = TREE_OPERAND (temp, 0);
11881
11882               ASM_INPUT_P (temp) = 1;
11883             }
11884         }
11885       else
11886         cgraph_add_asm_node (string);
11887     }
11888 }
11889
11890 /* Declarators [gram.dcl.decl] */
11891
11892 /* Parse an init-declarator.
11893
11894    init-declarator:
11895      declarator initializer [opt]
11896
11897    GNU Extension:
11898
11899    init-declarator:
11900      declarator asm-specification [opt] attributes [opt] initializer [opt]
11901
11902    function-definition:
11903      decl-specifier-seq [opt] declarator ctor-initializer [opt]
11904        function-body
11905      decl-specifier-seq [opt] declarator function-try-block
11906
11907    GNU Extension:
11908
11909    function-definition:
11910      __extension__ function-definition
11911
11912    The DECL_SPECIFIERS apply to this declarator.  Returns a
11913    representation of the entity declared.  If MEMBER_P is TRUE, then
11914    this declarator appears in a class scope.  The new DECL created by
11915    this declarator is returned.
11916
11917    The CHECKS are access checks that should be performed once we know
11918    what entity is being declared (and, therefore, what classes have
11919    befriended it).
11920
11921    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11922    for a function-definition here as well.  If the declarator is a
11923    declarator for a function-definition, *FUNCTION_DEFINITION_P will
11924    be TRUE upon return.  By that point, the function-definition will
11925    have been completely parsed.
11926
11927    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11928    is FALSE.  */
11929
11930 static tree
11931 cp_parser_init_declarator (cp_parser* parser,
11932                            cp_decl_specifier_seq *decl_specifiers,
11933                            VEC (deferred_access_check,gc)* checks,
11934                            bool function_definition_allowed_p,
11935                            bool member_p,
11936                            int declares_class_or_enum,
11937                            bool* function_definition_p)
11938 {
11939   cp_token *token;
11940   cp_declarator *declarator;
11941   tree prefix_attributes;
11942   tree attributes;
11943   tree asm_specification;
11944   tree initializer;
11945   tree decl = NULL_TREE;
11946   tree scope;
11947   bool is_initialized;
11948   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
11949      initialized with "= ..", CPP_OPEN_PAREN if initialized with
11950      "(...)".  */
11951   enum cpp_ttype initialization_kind;
11952   bool is_parenthesized_init = false;
11953   bool is_non_constant_init;
11954   int ctor_dtor_or_conv_p;
11955   bool friend_p;
11956   tree pushed_scope = NULL;
11957
11958   /* Gather the attributes that were provided with the
11959      decl-specifiers.  */
11960   prefix_attributes = decl_specifiers->attributes;
11961
11962   /* Assume that this is not the declarator for a function
11963      definition.  */
11964   if (function_definition_p)
11965     *function_definition_p = false;
11966
11967   /* Defer access checks while parsing the declarator; we cannot know
11968      what names are accessible until we know what is being
11969      declared.  */
11970   resume_deferring_access_checks ();
11971
11972   /* Parse the declarator.  */
11973   declarator
11974     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11975                             &ctor_dtor_or_conv_p,
11976                             /*parenthesized_p=*/NULL,
11977                             /*member_p=*/false);
11978   /* Gather up the deferred checks.  */
11979   stop_deferring_access_checks ();
11980
11981   /* If the DECLARATOR was erroneous, there's no need to go
11982      further.  */
11983   if (declarator == cp_error_declarator)
11984     return error_mark_node;
11985
11986   /* Check that the number of template-parameter-lists is OK.  */
11987   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11988     return error_mark_node;
11989
11990   if (declares_class_or_enum & 2)
11991     cp_parser_check_for_definition_in_return_type (declarator,
11992                                                    decl_specifiers->type);
11993
11994   /* Figure out what scope the entity declared by the DECLARATOR is
11995      located in.  `grokdeclarator' sometimes changes the scope, so
11996      we compute it now.  */
11997   scope = get_scope_of_declarator (declarator);
11998
11999   /* If we're allowing GNU extensions, look for an asm-specification
12000      and attributes.  */
12001   if (cp_parser_allow_gnu_extensions_p (parser))
12002     {
12003       /* Look for an asm-specification.  */
12004       asm_specification = cp_parser_asm_specification_opt (parser);
12005       /* And attributes.  */
12006       attributes = cp_parser_attributes_opt (parser);
12007     }
12008   else
12009     {
12010       asm_specification = NULL_TREE;
12011       attributes = NULL_TREE;
12012     }
12013
12014   /* Peek at the next token.  */
12015   token = cp_lexer_peek_token (parser->lexer);
12016   /* Check to see if the token indicates the start of a
12017      function-definition.  */
12018   if (cp_parser_token_starts_function_definition_p (token))
12019     {
12020       if (!function_definition_allowed_p)
12021         {
12022           /* If a function-definition should not appear here, issue an
12023              error message.  */
12024           cp_parser_error (parser,
12025                            "a function-definition is not allowed here");
12026           return error_mark_node;
12027         }
12028       else
12029         {
12030           /* Neither attributes nor an asm-specification are allowed
12031              on a function-definition.  */
12032           if (asm_specification)
12033             error ("an asm-specification is not allowed on a function-definition");
12034           if (attributes)
12035             error ("attributes are not allowed on a function-definition");
12036           /* This is a function-definition.  */
12037           *function_definition_p = true;
12038
12039           /* Parse the function definition.  */
12040           if (member_p)
12041             decl = cp_parser_save_member_function_body (parser,
12042                                                         decl_specifiers,
12043                                                         declarator,
12044                                                         prefix_attributes);
12045           else
12046             decl
12047               = (cp_parser_function_definition_from_specifiers_and_declarator
12048                  (parser, decl_specifiers, prefix_attributes, declarator));
12049
12050           return decl;
12051         }
12052     }
12053
12054   /* [dcl.dcl]
12055
12056      Only in function declarations for constructors, destructors, and
12057      type conversions can the decl-specifier-seq be omitted.
12058
12059      We explicitly postpone this check past the point where we handle
12060      function-definitions because we tolerate function-definitions
12061      that are missing their return types in some modes.  */
12062   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12063     {
12064       cp_parser_error (parser,
12065                        "expected constructor, destructor, or type conversion");
12066       return error_mark_node;
12067     }
12068
12069   /* An `=' or an `(' indicates an initializer.  */
12070   if (token->type == CPP_EQ
12071       || token->type == CPP_OPEN_PAREN)
12072     {
12073       is_initialized = true;
12074       initialization_kind = token->type;
12075     }
12076   else
12077     {
12078       /* If the init-declarator isn't initialized and isn't followed by a
12079          `,' or `;', it's not a valid init-declarator.  */
12080       if (token->type != CPP_COMMA
12081           && token->type != CPP_SEMICOLON)
12082         {
12083           cp_parser_error (parser, "expected initializer");
12084           return error_mark_node;
12085         }
12086       is_initialized = false;
12087       initialization_kind = CPP_EOF;
12088     }
12089
12090   /* Because start_decl has side-effects, we should only call it if we
12091      know we're going ahead.  By this point, we know that we cannot
12092      possibly be looking at any other construct.  */
12093   cp_parser_commit_to_tentative_parse (parser);
12094
12095   /* If the decl specifiers were bad, issue an error now that we're
12096      sure this was intended to be a declarator.  Then continue
12097      declaring the variable(s), as int, to try to cut down on further
12098      errors.  */
12099   if (decl_specifiers->any_specifiers_p
12100       && decl_specifiers->type == error_mark_node)
12101     {
12102       cp_parser_error (parser, "invalid type in declaration");
12103       decl_specifiers->type = integer_type_node;
12104     }
12105
12106   /* Check to see whether or not this declaration is a friend.  */
12107   friend_p = cp_parser_friend_p (decl_specifiers);
12108
12109   /* Enter the newly declared entry in the symbol table.  If we're
12110      processing a declaration in a class-specifier, we wait until
12111      after processing the initializer.  */
12112   if (!member_p)
12113     {
12114       if (parser->in_unbraced_linkage_specification_p)
12115         decl_specifiers->storage_class = sc_extern;
12116       decl = start_decl (declarator, decl_specifiers,
12117                          is_initialized, attributes, prefix_attributes,
12118                          &pushed_scope);
12119     }
12120   else if (scope)
12121     /* Enter the SCOPE.  That way unqualified names appearing in the
12122        initializer will be looked up in SCOPE.  */
12123     pushed_scope = push_scope (scope);
12124
12125   /* Perform deferred access control checks, now that we know in which
12126      SCOPE the declared entity resides.  */
12127   if (!member_p && decl)
12128     {
12129       tree saved_current_function_decl = NULL_TREE;
12130
12131       /* If the entity being declared is a function, pretend that we
12132          are in its scope.  If it is a `friend', it may have access to
12133          things that would not otherwise be accessible.  */
12134       if (TREE_CODE (decl) == FUNCTION_DECL)
12135         {
12136           saved_current_function_decl = current_function_decl;
12137           current_function_decl = decl;
12138         }
12139
12140       /* Perform access checks for template parameters.  */
12141       cp_parser_perform_template_parameter_access_checks (checks);
12142
12143       /* Perform the access control checks for the declarator and the
12144          the decl-specifiers.  */
12145       perform_deferred_access_checks ();
12146
12147       /* Restore the saved value.  */
12148       if (TREE_CODE (decl) == FUNCTION_DECL)
12149         current_function_decl = saved_current_function_decl;
12150     }
12151
12152   /* Parse the initializer.  */
12153   initializer = NULL_TREE;
12154   is_parenthesized_init = false;
12155   is_non_constant_init = true;
12156   if (is_initialized)
12157     {
12158       if (function_declarator_p (declarator))
12159         {
12160            if (initialization_kind == CPP_EQ)
12161              initializer = cp_parser_pure_specifier (parser);
12162            else
12163              {
12164                /* If the declaration was erroneous, we don't really
12165                   know what the user intended, so just silently
12166                   consume the initializer.  */
12167                if (decl != error_mark_node)
12168                  error ("initializer provided for function");
12169                cp_parser_skip_to_closing_parenthesis (parser,
12170                                                       /*recovering=*/true,
12171                                                       /*or_comma=*/false,
12172                                                       /*consume_paren=*/true);
12173              }
12174         }
12175       else
12176         initializer = cp_parser_initializer (parser,
12177                                              &is_parenthesized_init,
12178                                              &is_non_constant_init);
12179     }
12180
12181   /* The old parser allows attributes to appear after a parenthesized
12182      initializer.  Mark Mitchell proposed removing this functionality
12183      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12184      attributes -- but ignores them.  */
12185   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
12186     if (cp_parser_attributes_opt (parser))
12187       warning (OPT_Wattributes,
12188                "attributes after parenthesized initializer ignored");
12189
12190   /* For an in-class declaration, use `grokfield' to create the
12191      declaration.  */
12192   if (member_p)
12193     {
12194       if (pushed_scope)
12195         {
12196           pop_scope (pushed_scope);
12197           pushed_scope = false;
12198         }
12199       decl = grokfield (declarator, decl_specifiers,
12200                         initializer, !is_non_constant_init,
12201                         /*asmspec=*/NULL_TREE,
12202                         prefix_attributes);
12203       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12204         cp_parser_save_default_args (parser, decl);
12205     }
12206
12207   /* Finish processing the declaration.  But, skip friend
12208      declarations.  */
12209   if (!friend_p && decl && decl != error_mark_node)
12210     {
12211       cp_finish_decl (decl,
12212                       initializer, !is_non_constant_init,
12213                       asm_specification,
12214                       /* If the initializer is in parentheses, then this is
12215                          a direct-initialization, which means that an
12216                          `explicit' constructor is OK.  Otherwise, an
12217                          `explicit' constructor cannot be used.  */
12218                       ((is_parenthesized_init || !is_initialized)
12219                      ? 0 : LOOKUP_ONLYCONVERTING));
12220     }
12221   else if ((cxx_dialect != cxx98) && friend_p
12222            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12223     /* Core issue #226 (C++0x only): A default template-argument
12224        shall not be specified in a friend class template
12225        declaration. */
12226     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12227                              /*is_partial=*/0, /*is_friend_decl=*/1);
12228
12229   if (!friend_p && pushed_scope)
12230     pop_scope (pushed_scope);
12231
12232   return decl;
12233 }
12234
12235 /* Parse a declarator.
12236
12237    declarator:
12238      direct-declarator
12239      ptr-operator declarator
12240
12241    abstract-declarator:
12242      ptr-operator abstract-declarator [opt]
12243      direct-abstract-declarator
12244
12245    GNU Extensions:
12246
12247    declarator:
12248      attributes [opt] direct-declarator
12249      attributes [opt] ptr-operator declarator
12250
12251    abstract-declarator:
12252      attributes [opt] ptr-operator abstract-declarator [opt]
12253      attributes [opt] direct-abstract-declarator
12254
12255    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12256    detect constructor, destructor or conversion operators. It is set
12257    to -1 if the declarator is a name, and +1 if it is a
12258    function. Otherwise it is set to zero. Usually you just want to
12259    test for >0, but internally the negative value is used.
12260
12261    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12262    a decl-specifier-seq unless it declares a constructor, destructor,
12263    or conversion.  It might seem that we could check this condition in
12264    semantic analysis, rather than parsing, but that makes it difficult
12265    to handle something like `f()'.  We want to notice that there are
12266    no decl-specifiers, and therefore realize that this is an
12267    expression, not a declaration.)
12268
12269    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12270    the declarator is a direct-declarator of the form "(...)".
12271
12272    MEMBER_P is true iff this declarator is a member-declarator.  */
12273
12274 static cp_declarator *
12275 cp_parser_declarator (cp_parser* parser,
12276                       cp_parser_declarator_kind dcl_kind,
12277                       int* ctor_dtor_or_conv_p,
12278                       bool* parenthesized_p,
12279                       bool member_p)
12280 {
12281   cp_token *token;
12282   cp_declarator *declarator;
12283   enum tree_code code;
12284   cp_cv_quals cv_quals;
12285   tree class_type;
12286   tree attributes = NULL_TREE;
12287
12288   /* Assume this is not a constructor, destructor, or type-conversion
12289      operator.  */
12290   if (ctor_dtor_or_conv_p)
12291     *ctor_dtor_or_conv_p = 0;
12292
12293   if (cp_parser_allow_gnu_extensions_p (parser))
12294     attributes = cp_parser_attributes_opt (parser);
12295
12296   /* Peek at the next token.  */
12297   token = cp_lexer_peek_token (parser->lexer);
12298
12299   /* Check for the ptr-operator production.  */
12300   cp_parser_parse_tentatively (parser);
12301   /* Parse the ptr-operator.  */
12302   code = cp_parser_ptr_operator (parser,
12303                                  &class_type,
12304                                  &cv_quals);
12305   /* If that worked, then we have a ptr-operator.  */
12306   if (cp_parser_parse_definitely (parser))
12307     {
12308       /* If a ptr-operator was found, then this declarator was not
12309          parenthesized.  */
12310       if (parenthesized_p)
12311         *parenthesized_p = true;
12312       /* The dependent declarator is optional if we are parsing an
12313          abstract-declarator.  */
12314       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12315         cp_parser_parse_tentatively (parser);
12316
12317       /* Parse the dependent declarator.  */
12318       declarator = cp_parser_declarator (parser, dcl_kind,
12319                                          /*ctor_dtor_or_conv_p=*/NULL,
12320                                          /*parenthesized_p=*/NULL,
12321                                          /*member_p=*/false);
12322
12323       /* If we are parsing an abstract-declarator, we must handle the
12324          case where the dependent declarator is absent.  */
12325       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12326           && !cp_parser_parse_definitely (parser))
12327         declarator = NULL;
12328
12329       declarator = cp_parser_make_indirect_declarator
12330         (code, class_type, cv_quals, declarator);
12331     }
12332   /* Everything else is a direct-declarator.  */
12333   else
12334     {
12335       if (parenthesized_p)
12336         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12337                                                    CPP_OPEN_PAREN);
12338       declarator = cp_parser_direct_declarator (parser, dcl_kind,
12339                                                 ctor_dtor_or_conv_p,
12340                                                 member_p);
12341     }
12342
12343   if (attributes && declarator && declarator != cp_error_declarator)
12344     declarator->attributes = attributes;
12345
12346   return declarator;
12347 }
12348
12349 /* Parse a direct-declarator or direct-abstract-declarator.
12350
12351    direct-declarator:
12352      declarator-id
12353      direct-declarator ( parameter-declaration-clause )
12354        cv-qualifier-seq [opt]
12355        exception-specification [opt]
12356      direct-declarator [ constant-expression [opt] ]
12357      ( declarator )
12358
12359    direct-abstract-declarator:
12360      direct-abstract-declarator [opt]
12361        ( parameter-declaration-clause )
12362        cv-qualifier-seq [opt]
12363        exception-specification [opt]
12364      direct-abstract-declarator [opt] [ constant-expression [opt] ]
12365      ( abstract-declarator )
12366
12367    Returns a representation of the declarator.  DCL_KIND is
12368    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12369    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
12370    we are parsing a direct-declarator.  It is
12371    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12372    of ambiguity we prefer an abstract declarator, as per
12373    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12374    cp_parser_declarator.  */
12375
12376 static cp_declarator *
12377 cp_parser_direct_declarator (cp_parser* parser,
12378                              cp_parser_declarator_kind dcl_kind,
12379                              int* ctor_dtor_or_conv_p,
12380                              bool member_p)
12381 {
12382   cp_token *token;
12383   cp_declarator *declarator = NULL;
12384   tree scope = NULL_TREE;
12385   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12386   bool saved_in_declarator_p = parser->in_declarator_p;
12387   bool first = true;
12388   tree pushed_scope = NULL_TREE;
12389
12390   while (true)
12391     {
12392       /* Peek at the next token.  */
12393       token = cp_lexer_peek_token (parser->lexer);
12394       if (token->type == CPP_OPEN_PAREN)
12395         {
12396           /* This is either a parameter-declaration-clause, or a
12397              parenthesized declarator. When we know we are parsing a
12398              named declarator, it must be a parenthesized declarator
12399              if FIRST is true. For instance, `(int)' is a
12400              parameter-declaration-clause, with an omitted
12401              direct-abstract-declarator. But `((*))', is a
12402              parenthesized abstract declarator. Finally, when T is a
12403              template parameter `(T)' is a
12404              parameter-declaration-clause, and not a parenthesized
12405              named declarator.
12406
12407              We first try and parse a parameter-declaration-clause,
12408              and then try a nested declarator (if FIRST is true).
12409
12410              It is not an error for it not to be a
12411              parameter-declaration-clause, even when FIRST is
12412              false. Consider,
12413
12414                int i (int);
12415                int i (3);
12416
12417              The first is the declaration of a function while the
12418              second is a the definition of a variable, including its
12419              initializer.
12420
12421              Having seen only the parenthesis, we cannot know which of
12422              these two alternatives should be selected.  Even more
12423              complex are examples like:
12424
12425                int i (int (a));
12426                int i (int (3));
12427
12428              The former is a function-declaration; the latter is a
12429              variable initialization.
12430
12431              Thus again, we try a parameter-declaration-clause, and if
12432              that fails, we back out and return.  */
12433
12434           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12435             {
12436               cp_parameter_declarator *params;
12437               unsigned saved_num_template_parameter_lists;
12438
12439               /* In a member-declarator, the only valid interpretation
12440                  of a parenthesis is the start of a
12441                  parameter-declaration-clause.  (It is invalid to
12442                  initialize a static data member with a parenthesized
12443                  initializer; only the "=" form of initialization is
12444                  permitted.)  */
12445               if (!member_p)
12446                 cp_parser_parse_tentatively (parser);
12447
12448               /* Consume the `('.  */
12449               cp_lexer_consume_token (parser->lexer);
12450               if (first)
12451                 {
12452                   /* If this is going to be an abstract declarator, we're
12453                      in a declarator and we can't have default args.  */
12454                   parser->default_arg_ok_p = false;
12455                   parser->in_declarator_p = true;
12456                 }
12457
12458               /* Inside the function parameter list, surrounding
12459                  template-parameter-lists do not apply.  */
12460               saved_num_template_parameter_lists
12461                 = parser->num_template_parameter_lists;
12462               parser->num_template_parameter_lists = 0;
12463
12464               /* Parse the parameter-declaration-clause.  */
12465               params = cp_parser_parameter_declaration_clause (parser);
12466
12467               parser->num_template_parameter_lists
12468                 = saved_num_template_parameter_lists;
12469
12470               /* If all went well, parse the cv-qualifier-seq and the
12471                  exception-specification.  */
12472               if (member_p || cp_parser_parse_definitely (parser))
12473                 {
12474                   cp_cv_quals cv_quals;
12475                   tree exception_specification;
12476
12477                   if (ctor_dtor_or_conv_p)
12478                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12479                   first = false;
12480                   /* Consume the `)'.  */
12481                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12482
12483                   /* Parse the cv-qualifier-seq.  */
12484                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12485                   /* And the exception-specification.  */
12486                   exception_specification
12487                     = cp_parser_exception_specification_opt (parser);
12488
12489                   /* Create the function-declarator.  */
12490                   declarator = make_call_declarator (declarator,
12491                                                      params,
12492                                                      cv_quals,
12493                                                      exception_specification);
12494                   /* Any subsequent parameter lists are to do with
12495                      return type, so are not those of the declared
12496                      function.  */
12497                   parser->default_arg_ok_p = false;
12498
12499                   /* Repeat the main loop.  */
12500                   continue;
12501                 }
12502             }
12503
12504           /* If this is the first, we can try a parenthesized
12505              declarator.  */
12506           if (first)
12507             {
12508               bool saved_in_type_id_in_expr_p;
12509
12510               parser->default_arg_ok_p = saved_default_arg_ok_p;
12511               parser->in_declarator_p = saved_in_declarator_p;
12512
12513               /* Consume the `('.  */
12514               cp_lexer_consume_token (parser->lexer);
12515               /* Parse the nested declarator.  */
12516               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12517               parser->in_type_id_in_expr_p = true;
12518               declarator
12519                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12520                                         /*parenthesized_p=*/NULL,
12521                                         member_p);
12522               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12523               first = false;
12524               /* Expect a `)'.  */
12525               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12526                 declarator = cp_error_declarator;
12527               if (declarator == cp_error_declarator)
12528                 break;
12529
12530               goto handle_declarator;
12531             }
12532           /* Otherwise, we must be done.  */
12533           else
12534             break;
12535         }
12536       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12537                && token->type == CPP_OPEN_SQUARE)
12538         {
12539           /* Parse an array-declarator.  */
12540           tree bounds;
12541
12542           if (ctor_dtor_or_conv_p)
12543             *ctor_dtor_or_conv_p = 0;
12544
12545           first = false;
12546           parser->default_arg_ok_p = false;
12547           parser->in_declarator_p = true;
12548           /* Consume the `['.  */
12549           cp_lexer_consume_token (parser->lexer);
12550           /* Peek at the next token.  */
12551           token = cp_lexer_peek_token (parser->lexer);
12552           /* If the next token is `]', then there is no
12553              constant-expression.  */
12554           if (token->type != CPP_CLOSE_SQUARE)
12555             {
12556               bool non_constant_p;
12557
12558               bounds
12559                 = cp_parser_constant_expression (parser,
12560                                                  /*allow_non_constant=*/true,
12561                                                  &non_constant_p);
12562               if (!non_constant_p)
12563                 bounds = fold_non_dependent_expr (bounds);
12564               /* Normally, the array bound must be an integral constant
12565                  expression.  However, as an extension, we allow VLAs
12566                  in function scopes.  */
12567               else if (!parser->in_function_body)
12568                 {
12569                   error ("array bound is not an integer constant");
12570                   bounds = error_mark_node;
12571                 }
12572             }
12573           else
12574             bounds = NULL_TREE;
12575           /* Look for the closing `]'.  */
12576           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12577             {
12578               declarator = cp_error_declarator;
12579               break;
12580             }
12581
12582           declarator = make_array_declarator (declarator, bounds);
12583         }
12584       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12585         {
12586           tree qualifying_scope;
12587           tree unqualified_name;
12588           special_function_kind sfk;
12589           bool abstract_ok;
12590           bool pack_expansion_p = false;
12591
12592           /* Parse a declarator-id */
12593           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12594           if (abstract_ok)
12595             {
12596               cp_parser_parse_tentatively (parser);
12597
12598               /* If we see an ellipsis, we should be looking at a
12599                  parameter pack. */
12600               if (token->type == CPP_ELLIPSIS)
12601                 {
12602                   /* Consume the `...' */
12603                   cp_lexer_consume_token (parser->lexer);
12604
12605                   pack_expansion_p = true;
12606                 }
12607             }
12608
12609           unqualified_name
12610             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12611           qualifying_scope = parser->scope;
12612           if (abstract_ok)
12613             {
12614               bool okay = false;
12615
12616               if (!unqualified_name && pack_expansion_p)
12617                 {
12618                   /* Check whether an error occurred. */
12619                   okay = !cp_parser_error_occurred (parser);
12620
12621                   /* We already consumed the ellipsis to mark a
12622                      parameter pack, but we have no way to report it,
12623                      so abort the tentative parse. We will be exiting
12624                      immediately anyway. */
12625                   cp_parser_abort_tentative_parse (parser);
12626                 }
12627               else
12628                 okay = cp_parser_parse_definitely (parser);
12629
12630               if (!okay)
12631                 unqualified_name = error_mark_node;
12632               else if (unqualified_name
12633                        && (qualifying_scope
12634                            || (TREE_CODE (unqualified_name)
12635                                != IDENTIFIER_NODE)))
12636                 {
12637                   cp_parser_error (parser, "expected unqualified-id");
12638                   unqualified_name = error_mark_node;
12639                 }
12640             }
12641
12642           if (!unqualified_name)
12643             return NULL;
12644           if (unqualified_name == error_mark_node)
12645             {
12646               declarator = cp_error_declarator;
12647               pack_expansion_p = false;
12648               declarator->parameter_pack_p = false;
12649               break;
12650             }
12651
12652           if (qualifying_scope && at_namespace_scope_p ()
12653               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12654             {
12655               /* In the declaration of a member of a template class
12656                  outside of the class itself, the SCOPE will sometimes
12657                  be a TYPENAME_TYPE.  For example, given:
12658
12659                  template <typename T>
12660                  int S<T>::R::i = 3;
12661
12662                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
12663                  this context, we must resolve S<T>::R to an ordinary
12664                  type, rather than a typename type.
12665
12666                  The reason we normally avoid resolving TYPENAME_TYPEs
12667                  is that a specialization of `S' might render
12668                  `S<T>::R' not a type.  However, if `S' is
12669                  specialized, then this `i' will not be used, so there
12670                  is no harm in resolving the types here.  */
12671               tree type;
12672
12673               /* Resolve the TYPENAME_TYPE.  */
12674               type = resolve_typename_type (qualifying_scope,
12675                                             /*only_current_p=*/false);
12676               /* If that failed, the declarator is invalid.  */
12677               if (TREE_CODE (type) == TYPENAME_TYPE)
12678                 error ("%<%T::%E%> is not a type",
12679                        TYPE_CONTEXT (qualifying_scope),
12680                        TYPE_IDENTIFIER (qualifying_scope));
12681               qualifying_scope = type;
12682             }
12683
12684           sfk = sfk_none;
12685
12686           if (unqualified_name)
12687             {
12688               tree class_type;
12689
12690               if (qualifying_scope
12691                   && CLASS_TYPE_P (qualifying_scope))
12692                 class_type = qualifying_scope;
12693               else
12694                 class_type = current_class_type;
12695
12696               if (TREE_CODE (unqualified_name) == TYPE_DECL)
12697                 {
12698                   tree name_type = TREE_TYPE (unqualified_name);
12699                   if (class_type && same_type_p (name_type, class_type))
12700                     {
12701                       if (qualifying_scope
12702                           && CLASSTYPE_USE_TEMPLATE (name_type))
12703                         {
12704                           error ("invalid use of constructor as a template");
12705                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12706                                   "name the constructor in a qualified name",
12707                                   class_type,
12708                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12709                                   class_type, name_type);
12710                           declarator = cp_error_declarator;
12711                           break;
12712                         }
12713                       else
12714                         unqualified_name = constructor_name (class_type);
12715                     }
12716                   else
12717                     {
12718                       /* We do not attempt to print the declarator
12719                          here because we do not have enough
12720                          information about its original syntactic
12721                          form.  */
12722                       cp_parser_error (parser, "invalid declarator");
12723                       declarator = cp_error_declarator;
12724                       break;
12725                     }
12726                 }
12727
12728               if (class_type)
12729                 {
12730                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12731                     sfk = sfk_destructor;
12732                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12733                     sfk = sfk_conversion;
12734                   else if (/* There's no way to declare a constructor
12735                               for an anonymous type, even if the type
12736                               got a name for linkage purposes.  */
12737                            !TYPE_WAS_ANONYMOUS (class_type)
12738                            && constructor_name_p (unqualified_name,
12739                                                   class_type))
12740                     {
12741                       unqualified_name = constructor_name (class_type);
12742                       sfk = sfk_constructor;
12743                     }
12744
12745                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
12746                     *ctor_dtor_or_conv_p = -1;
12747                 }
12748             }
12749           declarator = make_id_declarator (qualifying_scope,
12750                                            unqualified_name,
12751                                            sfk);
12752           declarator->id_loc = token->location;
12753           declarator->parameter_pack_p = pack_expansion_p;
12754
12755           if (pack_expansion_p)
12756             maybe_warn_variadic_templates ();
12757
12758         handle_declarator:;
12759           scope = get_scope_of_declarator (declarator);
12760           if (scope)
12761             /* Any names that appear after the declarator-id for a
12762                member are looked up in the containing scope.  */
12763             pushed_scope = push_scope (scope);
12764           parser->in_declarator_p = true;
12765           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12766               || (declarator && declarator->kind == cdk_id))
12767             /* Default args are only allowed on function
12768                declarations.  */
12769             parser->default_arg_ok_p = saved_default_arg_ok_p;
12770           else
12771             parser->default_arg_ok_p = false;
12772
12773           first = false;
12774         }
12775       /* We're done.  */
12776       else
12777         break;
12778     }
12779
12780   /* For an abstract declarator, we might wind up with nothing at this
12781      point.  That's an error; the declarator is not optional.  */
12782   if (!declarator)
12783     cp_parser_error (parser, "expected declarator");
12784
12785   /* If we entered a scope, we must exit it now.  */
12786   if (pushed_scope)
12787     pop_scope (pushed_scope);
12788
12789   parser->default_arg_ok_p = saved_default_arg_ok_p;
12790   parser->in_declarator_p = saved_in_declarator_p;
12791
12792   return declarator;
12793 }
12794
12795 /* Parse a ptr-operator.
12796
12797    ptr-operator:
12798      * cv-qualifier-seq [opt]
12799      &
12800      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12801
12802    GNU Extension:
12803
12804    ptr-operator:
12805      & cv-qualifier-seq [opt]
12806
12807    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12808    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
12809    an rvalue reference. In the case of a pointer-to-member, *TYPE is
12810    filled in with the TYPE containing the member.  *CV_QUALS is
12811    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
12812    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
12813    Note that the tree codes returned by this function have nothing
12814    to do with the types of trees that will be eventually be created
12815    to represent the pointer or reference type being parsed. They are
12816    just constants with suggestive names. */
12817 static enum tree_code
12818 cp_parser_ptr_operator (cp_parser* parser,
12819                         tree* type,
12820                         cp_cv_quals *cv_quals)
12821 {
12822   enum tree_code code = ERROR_MARK;
12823   cp_token *token;
12824
12825   /* Assume that it's not a pointer-to-member.  */
12826   *type = NULL_TREE;
12827   /* And that there are no cv-qualifiers.  */
12828   *cv_quals = TYPE_UNQUALIFIED;
12829
12830   /* Peek at the next token.  */
12831   token = cp_lexer_peek_token (parser->lexer);
12832
12833   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
12834   if (token->type == CPP_MULT)
12835     code = INDIRECT_REF;
12836   else if (token->type == CPP_AND)
12837     code = ADDR_EXPR;
12838   else if ((cxx_dialect != cxx98) &&
12839            token->type == CPP_AND_AND) /* C++0x only */
12840     code = NON_LVALUE_EXPR;
12841
12842   if (code != ERROR_MARK)
12843     {
12844       /* Consume the `*', `&' or `&&'.  */
12845       cp_lexer_consume_token (parser->lexer);
12846
12847       /* A `*' can be followed by a cv-qualifier-seq, and so can a
12848          `&', if we are allowing GNU extensions.  (The only qualifier
12849          that can legally appear after `&' is `restrict', but that is
12850          enforced during semantic analysis.  */
12851       if (code == INDIRECT_REF
12852           || cp_parser_allow_gnu_extensions_p (parser))
12853         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12854     }
12855   else
12856     {
12857       /* Try the pointer-to-member case.  */
12858       cp_parser_parse_tentatively (parser);
12859       /* Look for the optional `::' operator.  */
12860       cp_parser_global_scope_opt (parser,
12861                                   /*current_scope_valid_p=*/false);
12862       /* Look for the nested-name specifier.  */
12863       cp_parser_nested_name_specifier (parser,
12864                                        /*typename_keyword_p=*/false,
12865                                        /*check_dependency_p=*/true,
12866                                        /*type_p=*/false,
12867                                        /*is_declaration=*/false);
12868       /* If we found it, and the next token is a `*', then we are
12869          indeed looking at a pointer-to-member operator.  */
12870       if (!cp_parser_error_occurred (parser)
12871           && cp_parser_require (parser, CPP_MULT, "`*'"))
12872         {
12873           /* Indicate that the `*' operator was used.  */
12874           code = INDIRECT_REF;
12875
12876           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12877             error ("%qD is a namespace", parser->scope);
12878           else
12879             {
12880               /* The type of which the member is a member is given by the
12881                  current SCOPE.  */
12882               *type = parser->scope;
12883               /* The next name will not be qualified.  */
12884               parser->scope = NULL_TREE;
12885               parser->qualifying_scope = NULL_TREE;
12886               parser->object_scope = NULL_TREE;
12887               /* Look for the optional cv-qualifier-seq.  */
12888               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12889             }
12890         }
12891       /* If that didn't work we don't have a ptr-operator.  */
12892       if (!cp_parser_parse_definitely (parser))
12893         cp_parser_error (parser, "expected ptr-operator");
12894     }
12895
12896   return code;
12897 }
12898
12899 /* Parse an (optional) cv-qualifier-seq.
12900
12901    cv-qualifier-seq:
12902      cv-qualifier cv-qualifier-seq [opt]
12903
12904    cv-qualifier:
12905      const
12906      volatile
12907
12908    GNU Extension:
12909
12910    cv-qualifier:
12911      __restrict__
12912
12913    Returns a bitmask representing the cv-qualifiers.  */
12914
12915 static cp_cv_quals
12916 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12917 {
12918   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12919
12920   while (true)
12921     {
12922       cp_token *token;
12923       cp_cv_quals cv_qualifier;
12924
12925       /* Peek at the next token.  */
12926       token = cp_lexer_peek_token (parser->lexer);
12927       /* See if it's a cv-qualifier.  */
12928       switch (token->keyword)
12929         {
12930         case RID_CONST:
12931           cv_qualifier = TYPE_QUAL_CONST;
12932           break;
12933
12934         case RID_VOLATILE:
12935           cv_qualifier = TYPE_QUAL_VOLATILE;
12936           break;
12937
12938         case RID_RESTRICT:
12939           cv_qualifier = TYPE_QUAL_RESTRICT;
12940           break;
12941
12942         default:
12943           cv_qualifier = TYPE_UNQUALIFIED;
12944           break;
12945         }
12946
12947       if (!cv_qualifier)
12948         break;
12949
12950       if (cv_quals & cv_qualifier)
12951         {
12952           error ("duplicate cv-qualifier");
12953           cp_lexer_purge_token (parser->lexer);
12954         }
12955       else
12956         {
12957           cp_lexer_consume_token (parser->lexer);
12958           cv_quals |= cv_qualifier;
12959         }
12960     }
12961
12962   return cv_quals;
12963 }
12964
12965 /* Parse a declarator-id.
12966
12967    declarator-id:
12968      id-expression
12969      :: [opt] nested-name-specifier [opt] type-name
12970
12971    In the `id-expression' case, the value returned is as for
12972    cp_parser_id_expression if the id-expression was an unqualified-id.
12973    If the id-expression was a qualified-id, then a SCOPE_REF is
12974    returned.  The first operand is the scope (either a NAMESPACE_DECL
12975    or TREE_TYPE), but the second is still just a representation of an
12976    unqualified-id.  */
12977
12978 static tree
12979 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12980 {
12981   tree id;
12982   /* The expression must be an id-expression.  Assume that qualified
12983      names are the names of types so that:
12984
12985        template <class T>
12986        int S<T>::R::i = 3;
12987
12988      will work; we must treat `S<T>::R' as the name of a type.
12989      Similarly, assume that qualified names are templates, where
12990      required, so that:
12991
12992        template <class T>
12993        int S<T>::R<T>::i = 3;
12994
12995      will work, too.  */
12996   id = cp_parser_id_expression (parser,
12997                                 /*template_keyword_p=*/false,
12998                                 /*check_dependency_p=*/false,
12999                                 /*template_p=*/NULL,
13000                                 /*declarator_p=*/true,
13001                                 optional_p);
13002   if (id && BASELINK_P (id))
13003     id = BASELINK_FUNCTIONS (id);
13004   return id;
13005 }
13006
13007 /* Parse a type-id.
13008
13009    type-id:
13010      type-specifier-seq abstract-declarator [opt]
13011
13012    Returns the TYPE specified.  */
13013
13014 static tree
13015 cp_parser_type_id (cp_parser* parser)
13016 {
13017   cp_decl_specifier_seq type_specifier_seq;
13018   cp_declarator *abstract_declarator;
13019
13020   /* Parse the type-specifier-seq.  */
13021   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13022                                 &type_specifier_seq);
13023   if (type_specifier_seq.type == error_mark_node)
13024     return error_mark_node;
13025
13026   /* There might or might not be an abstract declarator.  */
13027   cp_parser_parse_tentatively (parser);
13028   /* Look for the declarator.  */
13029   abstract_declarator
13030     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13031                             /*parenthesized_p=*/NULL,
13032                             /*member_p=*/false);
13033   /* Check to see if there really was a declarator.  */
13034   if (!cp_parser_parse_definitely (parser))
13035     abstract_declarator = NULL;
13036
13037   return groktypename (&type_specifier_seq, abstract_declarator);
13038 }
13039
13040 /* Parse a type-specifier-seq.
13041
13042    type-specifier-seq:
13043      type-specifier type-specifier-seq [opt]
13044
13045    GNU extension:
13046
13047    type-specifier-seq:
13048      attributes type-specifier-seq [opt]
13049
13050    If IS_CONDITION is true, we are at the start of a "condition",
13051    e.g., we've just seen "if (".
13052
13053    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13054
13055 static void
13056 cp_parser_type_specifier_seq (cp_parser* parser,
13057                               bool is_condition,
13058                               cp_decl_specifier_seq *type_specifier_seq)
13059 {
13060   bool seen_type_specifier = false;
13061   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13062
13063   /* Clear the TYPE_SPECIFIER_SEQ.  */
13064   clear_decl_specs (type_specifier_seq);
13065
13066   /* Parse the type-specifiers and attributes.  */
13067   while (true)
13068     {
13069       tree type_specifier;
13070       bool is_cv_qualifier;
13071
13072       /* Check for attributes first.  */
13073       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13074         {
13075           type_specifier_seq->attributes =
13076             chainon (type_specifier_seq->attributes,
13077                      cp_parser_attributes_opt (parser));
13078           continue;
13079         }
13080
13081       /* Look for the type-specifier.  */
13082       type_specifier = cp_parser_type_specifier (parser,
13083                                                  flags,
13084                                                  type_specifier_seq,
13085                                                  /*is_declaration=*/false,
13086                                                  NULL,
13087                                                  &is_cv_qualifier);
13088       if (!type_specifier)
13089         {
13090           /* If the first type-specifier could not be found, this is not a
13091              type-specifier-seq at all.  */
13092           if (!seen_type_specifier)
13093             {
13094               cp_parser_error (parser, "expected type-specifier");
13095               type_specifier_seq->type = error_mark_node;
13096               return;
13097             }
13098           /* If subsequent type-specifiers could not be found, the
13099              type-specifier-seq is complete.  */
13100           break;
13101         }
13102
13103       seen_type_specifier = true;
13104       /* The standard says that a condition can be:
13105
13106             type-specifier-seq declarator = assignment-expression
13107
13108          However, given:
13109
13110            struct S {};
13111            if (int S = ...)
13112
13113          we should treat the "S" as a declarator, not as a
13114          type-specifier.  The standard doesn't say that explicitly for
13115          type-specifier-seq, but it does say that for
13116          decl-specifier-seq in an ordinary declaration.  Perhaps it
13117          would be clearer just to allow a decl-specifier-seq here, and
13118          then add a semantic restriction that if any decl-specifiers
13119          that are not type-specifiers appear, the program is invalid.  */
13120       if (is_condition && !is_cv_qualifier)
13121         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13122     }
13123
13124   cp_parser_check_decl_spec (type_specifier_seq);
13125 }
13126
13127 /* Parse a parameter-declaration-clause.
13128
13129    parameter-declaration-clause:
13130      parameter-declaration-list [opt] ... [opt]
13131      parameter-declaration-list , ...
13132
13133    Returns a representation for the parameter declarations.  A return
13134    value of NULL indicates a parameter-declaration-clause consisting
13135    only of an ellipsis.  */
13136
13137 static cp_parameter_declarator *
13138 cp_parser_parameter_declaration_clause (cp_parser* parser)
13139 {
13140   cp_parameter_declarator *parameters;
13141   cp_token *token;
13142   bool ellipsis_p;
13143   bool is_error;
13144
13145   /* Peek at the next token.  */
13146   token = cp_lexer_peek_token (parser->lexer);
13147   /* Check for trivial parameter-declaration-clauses.  */
13148   if (token->type == CPP_ELLIPSIS)
13149     {
13150       /* Consume the `...' token.  */
13151       cp_lexer_consume_token (parser->lexer);
13152       return NULL;
13153     }
13154   else if (token->type == CPP_CLOSE_PAREN)
13155     /* There are no parameters.  */
13156     {
13157 #ifndef NO_IMPLICIT_EXTERN_C
13158       if (in_system_header && current_class_type == NULL
13159           && current_lang_name == lang_name_c)
13160         return NULL;
13161       else
13162 #endif
13163         return no_parameters;
13164     }
13165   /* Check for `(void)', too, which is a special case.  */
13166   else if (token->keyword == RID_VOID
13167            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13168                == CPP_CLOSE_PAREN))
13169     {
13170       /* Consume the `void' token.  */
13171       cp_lexer_consume_token (parser->lexer);
13172       /* There are no parameters.  */
13173       return no_parameters;
13174     }
13175
13176   /* Parse the parameter-declaration-list.  */
13177   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13178   /* If a parse error occurred while parsing the
13179      parameter-declaration-list, then the entire
13180      parameter-declaration-clause is erroneous.  */
13181   if (is_error)
13182     return NULL;
13183
13184   /* Peek at the next token.  */
13185   token = cp_lexer_peek_token (parser->lexer);
13186   /* If it's a `,', the clause should terminate with an ellipsis.  */
13187   if (token->type == CPP_COMMA)
13188     {
13189       /* Consume the `,'.  */
13190       cp_lexer_consume_token (parser->lexer);
13191       /* Expect an ellipsis.  */
13192       ellipsis_p
13193         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
13194     }
13195   /* It might also be `...' if the optional trailing `,' was
13196      omitted.  */
13197   else if (token->type == CPP_ELLIPSIS)
13198     {
13199       /* Consume the `...' token.  */
13200       cp_lexer_consume_token (parser->lexer);
13201       /* And remember that we saw it.  */
13202       ellipsis_p = true;
13203     }
13204   else
13205     ellipsis_p = false;
13206
13207   /* Finish the parameter list.  */
13208   if (parameters && ellipsis_p)
13209     parameters->ellipsis_p = true;
13210
13211   return parameters;
13212 }
13213
13214 /* Parse a parameter-declaration-list.
13215
13216    parameter-declaration-list:
13217      parameter-declaration
13218      parameter-declaration-list , parameter-declaration
13219
13220    Returns a representation of the parameter-declaration-list, as for
13221    cp_parser_parameter_declaration_clause.  However, the
13222    `void_list_node' is never appended to the list.  Upon return,
13223    *IS_ERROR will be true iff an error occurred.  */
13224
13225 static cp_parameter_declarator *
13226 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13227 {
13228   cp_parameter_declarator *parameters = NULL;
13229   cp_parameter_declarator **tail = &parameters;
13230   bool saved_in_unbraced_linkage_specification_p;
13231
13232   /* Assume all will go well.  */
13233   *is_error = false;
13234   /* The special considerations that apply to a function within an
13235      unbraced linkage specifications do not apply to the parameters
13236      to the function.  */
13237   saved_in_unbraced_linkage_specification_p 
13238     = parser->in_unbraced_linkage_specification_p;
13239   parser->in_unbraced_linkage_specification_p = false;
13240
13241   /* Look for more parameters.  */
13242   while (true)
13243     {
13244       cp_parameter_declarator *parameter;
13245       bool parenthesized_p;
13246       /* Parse the parameter.  */
13247       parameter
13248         = cp_parser_parameter_declaration (parser,
13249                                            /*template_parm_p=*/false,
13250                                            &parenthesized_p);
13251
13252       /* If a parse error occurred parsing the parameter declaration,
13253          then the entire parameter-declaration-list is erroneous.  */
13254       if (!parameter)
13255         {
13256           *is_error = true;
13257           parameters = NULL;
13258           break;
13259         }
13260       /* Add the new parameter to the list.  */
13261       *tail = parameter;
13262       tail = &parameter->next;
13263
13264       /* Peek at the next token.  */
13265       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13266           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13267           /* These are for Objective-C++ */
13268           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13269           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13270         /* The parameter-declaration-list is complete.  */
13271         break;
13272       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13273         {
13274           cp_token *token;
13275
13276           /* Peek at the next token.  */
13277           token = cp_lexer_peek_nth_token (parser->lexer, 2);
13278           /* If it's an ellipsis, then the list is complete.  */
13279           if (token->type == CPP_ELLIPSIS)
13280             break;
13281           /* Otherwise, there must be more parameters.  Consume the
13282              `,'.  */
13283           cp_lexer_consume_token (parser->lexer);
13284           /* When parsing something like:
13285
13286                 int i(float f, double d)
13287
13288              we can tell after seeing the declaration for "f" that we
13289              are not looking at an initialization of a variable "i",
13290              but rather at the declaration of a function "i".
13291
13292              Due to the fact that the parsing of template arguments
13293              (as specified to a template-id) requires backtracking we
13294              cannot use this technique when inside a template argument
13295              list.  */
13296           if (!parser->in_template_argument_list_p
13297               && !parser->in_type_id_in_expr_p
13298               && cp_parser_uncommitted_to_tentative_parse_p (parser)
13299               /* However, a parameter-declaration of the form
13300                  "foat(f)" (which is a valid declaration of a
13301                  parameter "f") can also be interpreted as an
13302                  expression (the conversion of "f" to "float").  */
13303               && !parenthesized_p)
13304             cp_parser_commit_to_tentative_parse (parser);
13305         }
13306       else
13307         {
13308           cp_parser_error (parser, "expected %<,%> or %<...%>");
13309           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13310             cp_parser_skip_to_closing_parenthesis (parser,
13311                                                    /*recovering=*/true,
13312                                                    /*or_comma=*/false,
13313                                                    /*consume_paren=*/false);
13314           break;
13315         }
13316     }
13317
13318   parser->in_unbraced_linkage_specification_p
13319     = saved_in_unbraced_linkage_specification_p;
13320
13321   return parameters;
13322 }
13323
13324 /* Parse a parameter declaration.
13325
13326    parameter-declaration:
13327      decl-specifier-seq ... [opt] declarator
13328      decl-specifier-seq declarator = assignment-expression
13329      decl-specifier-seq ... [opt] abstract-declarator [opt]
13330      decl-specifier-seq abstract-declarator [opt] = assignment-expression
13331
13332    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13333    declares a template parameter.  (In that case, a non-nested `>'
13334    token encountered during the parsing of the assignment-expression
13335    is not interpreted as a greater-than operator.)
13336
13337    Returns a representation of the parameter, or NULL if an error
13338    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13339    true iff the declarator is of the form "(p)".  */
13340
13341 static cp_parameter_declarator *
13342 cp_parser_parameter_declaration (cp_parser *parser,
13343                                  bool template_parm_p,
13344                                  bool *parenthesized_p)
13345 {
13346   int declares_class_or_enum;
13347   bool greater_than_is_operator_p;
13348   cp_decl_specifier_seq decl_specifiers;
13349   cp_declarator *declarator;
13350   tree default_argument;
13351   cp_token *token;
13352   const char *saved_message;
13353
13354   /* In a template parameter, `>' is not an operator.
13355
13356      [temp.param]
13357
13358      When parsing a default template-argument for a non-type
13359      template-parameter, the first non-nested `>' is taken as the end
13360      of the template parameter-list rather than a greater-than
13361      operator.  */
13362   greater_than_is_operator_p = !template_parm_p;
13363
13364   /* Type definitions may not appear in parameter types.  */
13365   saved_message = parser->type_definition_forbidden_message;
13366   parser->type_definition_forbidden_message
13367     = "types may not be defined in parameter types";
13368
13369   /* Parse the declaration-specifiers.  */
13370   cp_parser_decl_specifier_seq (parser,
13371                                 CP_PARSER_FLAGS_NONE,
13372                                 &decl_specifiers,
13373                                 &declares_class_or_enum);
13374   /* If an error occurred, there's no reason to attempt to parse the
13375      rest of the declaration.  */
13376   if (cp_parser_error_occurred (parser))
13377     {
13378       parser->type_definition_forbidden_message = saved_message;
13379       return NULL;
13380     }
13381
13382   /* Peek at the next token.  */
13383   token = cp_lexer_peek_token (parser->lexer);
13384
13385   /* If the next token is a `)', `,', `=', `>', or `...', then there
13386      is no declarator. However, when variadic templates are enabled,
13387      there may be a declarator following `...'.  */
13388   if (token->type == CPP_CLOSE_PAREN
13389       || token->type == CPP_COMMA
13390       || token->type == CPP_EQ
13391       || token->type == CPP_GREATER)
13392     {
13393       declarator = NULL;
13394       if (parenthesized_p)
13395         *parenthesized_p = false;
13396     }
13397   /* Otherwise, there should be a declarator.  */
13398   else
13399     {
13400       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13401       parser->default_arg_ok_p = false;
13402
13403       /* After seeing a decl-specifier-seq, if the next token is not a
13404          "(", there is no possibility that the code is a valid
13405          expression.  Therefore, if parsing tentatively, we commit at
13406          this point.  */
13407       if (!parser->in_template_argument_list_p
13408           /* In an expression context, having seen:
13409
13410                (int((char ...
13411
13412              we cannot be sure whether we are looking at a
13413              function-type (taking a "char" as a parameter) or a cast
13414              of some object of type "char" to "int".  */
13415           && !parser->in_type_id_in_expr_p
13416           && cp_parser_uncommitted_to_tentative_parse_p (parser)
13417           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13418         cp_parser_commit_to_tentative_parse (parser);
13419       /* Parse the declarator.  */
13420       declarator = cp_parser_declarator (parser,
13421                                          CP_PARSER_DECLARATOR_EITHER,
13422                                          /*ctor_dtor_or_conv_p=*/NULL,
13423                                          parenthesized_p,
13424                                          /*member_p=*/false);
13425       parser->default_arg_ok_p = saved_default_arg_ok_p;
13426       /* After the declarator, allow more attributes.  */
13427       decl_specifiers.attributes
13428         = chainon (decl_specifiers.attributes,
13429                    cp_parser_attributes_opt (parser));
13430     }
13431
13432   /* If the next token is an ellipsis, and we have not seen a
13433      declarator name, and the type of the declarator contains parameter
13434      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13435      a parameter pack expansion expression. Otherwise, leave the
13436      ellipsis for a C-style variadic function. */
13437   token = cp_lexer_peek_token (parser->lexer);
13438   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13439     {
13440       tree type = decl_specifiers.type;
13441
13442       if (type && DECL_P (type))
13443         type = TREE_TYPE (type);
13444
13445       if (type
13446           && TREE_CODE (type) != TYPE_PACK_EXPANSION
13447           && declarator_can_be_parameter_pack (declarator)
13448           && (!declarator || !declarator->parameter_pack_p)
13449           && uses_parameter_packs (type))
13450         {
13451           /* Consume the `...'. */
13452           cp_lexer_consume_token (parser->lexer);
13453           maybe_warn_variadic_templates ();
13454           
13455           /* Build a pack expansion type */
13456           if (declarator)
13457             declarator->parameter_pack_p = true;
13458           else
13459             decl_specifiers.type = make_pack_expansion (type);
13460         }
13461     }
13462
13463   /* The restriction on defining new types applies only to the type
13464      of the parameter, not to the default argument.  */
13465   parser->type_definition_forbidden_message = saved_message;
13466
13467   /* If the next token is `=', then process a default argument.  */
13468   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13469     {
13470       bool saved_greater_than_is_operator_p;
13471       /* Consume the `='.  */
13472       cp_lexer_consume_token (parser->lexer);
13473
13474       /* If we are defining a class, then the tokens that make up the
13475          default argument must be saved and processed later.  */
13476       if (!template_parm_p && at_class_scope_p ()
13477           && TYPE_BEING_DEFINED (current_class_type))
13478         {
13479           unsigned depth = 0;
13480           cp_token *first_token;
13481           cp_token *token;
13482
13483           /* Add tokens until we have processed the entire default
13484              argument.  We add the range [first_token, token).  */
13485           first_token = cp_lexer_peek_token (parser->lexer);
13486           while (true)
13487             {
13488               bool done = false;
13489
13490               /* Peek at the next token.  */
13491               token = cp_lexer_peek_token (parser->lexer);
13492               /* What we do depends on what token we have.  */
13493               switch (token->type)
13494                 {
13495                   /* In valid code, a default argument must be
13496                      immediately followed by a `,' `)', or `...'.  */
13497                 case CPP_COMMA:
13498                 case CPP_CLOSE_PAREN:
13499                 case CPP_ELLIPSIS:
13500                   /* If we run into a non-nested `;', `}', or `]',
13501                      then the code is invalid -- but the default
13502                      argument is certainly over.  */
13503                 case CPP_SEMICOLON:
13504                 case CPP_CLOSE_BRACE:
13505                 case CPP_CLOSE_SQUARE:
13506                   if (depth == 0)
13507                     done = true;
13508                   /* Update DEPTH, if necessary.  */
13509                   else if (token->type == CPP_CLOSE_PAREN
13510                            || token->type == CPP_CLOSE_BRACE
13511                            || token->type == CPP_CLOSE_SQUARE)
13512                     --depth;
13513                   break;
13514
13515                 case CPP_OPEN_PAREN:
13516                 case CPP_OPEN_SQUARE:
13517                 case CPP_OPEN_BRACE:
13518                   ++depth;
13519                   break;
13520
13521                 case CPP_RSHIFT:
13522                   if (cxx_dialect == cxx98)
13523                     break;
13524                   /* Fall through for C++0x, which treats the `>>'
13525                      operator like two `>' tokens in certain
13526                      cases.  */
13527
13528                 case CPP_GREATER:
13529                   /* If we see a non-nested `>', and `>' is not an
13530                      operator, then it marks the end of the default
13531                      argument.  */
13532                   if (!depth && !greater_than_is_operator_p)
13533                     done = true;
13534                   break;
13535
13536                   /* If we run out of tokens, issue an error message.  */
13537                 case CPP_EOF:
13538                 case CPP_PRAGMA_EOL:
13539                   error ("file ends in default argument");
13540                   done = true;
13541                   break;
13542
13543                 case CPP_NAME:
13544                 case CPP_SCOPE:
13545                   /* In these cases, we should look for template-ids.
13546                      For example, if the default argument is
13547                      `X<int, double>()', we need to do name lookup to
13548                      figure out whether or not `X' is a template; if
13549                      so, the `,' does not end the default argument.
13550
13551                      That is not yet done.  */
13552                   break;
13553
13554                 default:
13555                   break;
13556                 }
13557
13558               /* If we've reached the end, stop.  */
13559               if (done)
13560                 break;
13561
13562               /* Add the token to the token block.  */
13563               token = cp_lexer_consume_token (parser->lexer);
13564             }
13565
13566           /* Create a DEFAULT_ARG to represented the unparsed default
13567              argument.  */
13568           default_argument = make_node (DEFAULT_ARG);
13569           DEFARG_TOKENS (default_argument)
13570             = cp_token_cache_new (first_token, token);
13571           DEFARG_INSTANTIATIONS (default_argument) = NULL;
13572         }
13573       /* Outside of a class definition, we can just parse the
13574          assignment-expression.  */
13575       else
13576         {
13577           bool saved_local_variables_forbidden_p;
13578
13579           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13580              set correctly.  */
13581           saved_greater_than_is_operator_p
13582             = parser->greater_than_is_operator_p;
13583           parser->greater_than_is_operator_p = greater_than_is_operator_p;
13584           /* Local variable names (and the `this' keyword) may not
13585              appear in a default argument.  */
13586           saved_local_variables_forbidden_p
13587             = parser->local_variables_forbidden_p;
13588           parser->local_variables_forbidden_p = true;
13589           /* The default argument expression may cause implicitly
13590              defined member functions to be synthesized, which will
13591              result in garbage collection.  We must treat this
13592              situation as if we were within the body of function so as
13593              to avoid collecting live data on the stack.  */
13594           ++function_depth;
13595           /* Parse the assignment-expression.  */
13596           if (template_parm_p)
13597             push_deferring_access_checks (dk_no_deferred);
13598           default_argument
13599             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13600           if (template_parm_p)
13601             pop_deferring_access_checks ();
13602           /* Restore saved state.  */
13603           --function_depth;
13604           parser->greater_than_is_operator_p
13605             = saved_greater_than_is_operator_p;
13606           parser->local_variables_forbidden_p
13607             = saved_local_variables_forbidden_p;
13608         }
13609       if (!parser->default_arg_ok_p)
13610         {
13611           if (!flag_pedantic_errors)
13612             warning (0, "deprecated use of default argument for parameter of non-function");
13613           else
13614             {
13615               error ("default arguments are only permitted for function parameters");
13616               default_argument = NULL_TREE;
13617             }
13618         }
13619     }
13620   else
13621     default_argument = NULL_TREE;
13622
13623   return make_parameter_declarator (&decl_specifiers,
13624                                     declarator,
13625                                     default_argument);
13626 }
13627
13628 /* Parse a function-body.
13629
13630    function-body:
13631      compound_statement  */
13632
13633 static void
13634 cp_parser_function_body (cp_parser *parser)
13635 {
13636   cp_parser_compound_statement (parser, NULL, false);
13637 }
13638
13639 /* Parse a ctor-initializer-opt followed by a function-body.  Return
13640    true if a ctor-initializer was present.  */
13641
13642 static bool
13643 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13644 {
13645   tree body;
13646   bool ctor_initializer_p;
13647
13648   /* Begin the function body.  */
13649   body = begin_function_body ();
13650   /* Parse the optional ctor-initializer.  */
13651   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13652   /* Parse the function-body.  */
13653   cp_parser_function_body (parser);
13654   /* Finish the function body.  */
13655   finish_function_body (body);
13656
13657   return ctor_initializer_p;
13658 }
13659
13660 /* Parse an initializer.
13661
13662    initializer:
13663      = initializer-clause
13664      ( expression-list )
13665
13666    Returns an expression representing the initializer.  If no
13667    initializer is present, NULL_TREE is returned.
13668
13669    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13670    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
13671    set to FALSE if there is no initializer present.  If there is an
13672    initializer, and it is not a constant-expression, *NON_CONSTANT_P
13673    is set to true; otherwise it is set to false.  */
13674
13675 static tree
13676 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13677                        bool* non_constant_p)
13678 {
13679   cp_token *token;
13680   tree init;
13681
13682   /* Peek at the next token.  */
13683   token = cp_lexer_peek_token (parser->lexer);
13684
13685   /* Let our caller know whether or not this initializer was
13686      parenthesized.  */
13687   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13688   /* Assume that the initializer is constant.  */
13689   *non_constant_p = false;
13690
13691   if (token->type == CPP_EQ)
13692     {
13693       /* Consume the `='.  */
13694       cp_lexer_consume_token (parser->lexer);
13695       /* Parse the initializer-clause.  */
13696       init = cp_parser_initializer_clause (parser, non_constant_p);
13697     }
13698   else if (token->type == CPP_OPEN_PAREN)
13699     init = cp_parser_parenthesized_expression_list (parser, false,
13700                                                     /*cast_p=*/false,
13701                                                     /*allow_expansion_p=*/true,
13702                                                     non_constant_p);
13703   else
13704     {
13705       /* Anything else is an error.  */
13706       cp_parser_error (parser, "expected initializer");
13707       init = error_mark_node;
13708     }
13709
13710   return init;
13711 }
13712
13713 /* Parse an initializer-clause.
13714
13715    initializer-clause:
13716      assignment-expression
13717      { initializer-list , [opt] }
13718      { }
13719
13720    Returns an expression representing the initializer.
13721
13722    If the `assignment-expression' production is used the value
13723    returned is simply a representation for the expression.
13724
13725    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
13726    the elements of the initializer-list (or NULL, if the last
13727    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
13728    NULL_TREE.  There is no way to detect whether or not the optional
13729    trailing `,' was provided.  NON_CONSTANT_P is as for
13730    cp_parser_initializer.  */
13731
13732 static tree
13733 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13734 {
13735   tree initializer;
13736
13737   /* Assume the expression is constant.  */
13738   *non_constant_p = false;
13739
13740   /* If it is not a `{', then we are looking at an
13741      assignment-expression.  */
13742   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13743     {
13744       initializer
13745         = cp_parser_constant_expression (parser,
13746                                         /*allow_non_constant_p=*/true,
13747                                         non_constant_p);
13748       if (!*non_constant_p)
13749         initializer = fold_non_dependent_expr (initializer);
13750     }
13751   else
13752     {
13753       /* Consume the `{' token.  */
13754       cp_lexer_consume_token (parser->lexer);
13755       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
13756       initializer = make_node (CONSTRUCTOR);
13757       /* If it's not a `}', then there is a non-trivial initializer.  */
13758       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13759         {
13760           /* Parse the initializer list.  */
13761           CONSTRUCTOR_ELTS (initializer)
13762             = cp_parser_initializer_list (parser, non_constant_p);
13763           /* A trailing `,' token is allowed.  */
13764           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13765             cp_lexer_consume_token (parser->lexer);
13766         }
13767       /* Now, there should be a trailing `}'.  */
13768       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13769     }
13770
13771   return initializer;
13772 }
13773
13774 /* Parse an initializer-list.
13775
13776    initializer-list:
13777      initializer-clause ... [opt]
13778      initializer-list , initializer-clause ... [opt]
13779
13780    GNU Extension:
13781
13782    initializer-list:
13783      identifier : initializer-clause
13784      initializer-list, identifier : initializer-clause
13785
13786    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
13787    for the initializer.  If the INDEX of the elt is non-NULL, it is the
13788    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
13789    as for cp_parser_initializer.  */
13790
13791 static VEC(constructor_elt,gc) *
13792 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13793 {
13794   VEC(constructor_elt,gc) *v = NULL;
13795
13796   /* Assume all of the expressions are constant.  */
13797   *non_constant_p = false;
13798
13799   /* Parse the rest of the list.  */
13800   while (true)
13801     {
13802       cp_token *token;
13803       tree identifier;
13804       tree initializer;
13805       bool clause_non_constant_p;
13806
13807       /* If the next token is an identifier and the following one is a
13808          colon, we are looking at the GNU designated-initializer
13809          syntax.  */
13810       if (cp_parser_allow_gnu_extensions_p (parser)
13811           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13812           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13813         {
13814           /* Warn the user that they are using an extension.  */
13815           if (pedantic)
13816             pedwarn ("ISO C++ does not allow designated initializers");
13817           /* Consume the identifier.  */
13818           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13819           /* Consume the `:'.  */
13820           cp_lexer_consume_token (parser->lexer);
13821         }
13822       else
13823         identifier = NULL_TREE;
13824
13825       /* Parse the initializer.  */
13826       initializer = cp_parser_initializer_clause (parser,
13827                                                   &clause_non_constant_p);
13828       /* If any clause is non-constant, so is the entire initializer.  */
13829       if (clause_non_constant_p)
13830         *non_constant_p = true;
13831
13832       /* If we have an ellipsis, this is an initializer pack
13833          expansion.  */
13834       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13835         {
13836           /* Consume the `...'.  */
13837           cp_lexer_consume_token (parser->lexer);
13838
13839           /* Turn the initializer into an initializer expansion.  */
13840           initializer = make_pack_expansion (initializer);
13841         }
13842
13843       /* Add it to the vector.  */
13844       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13845
13846       /* If the next token is not a comma, we have reached the end of
13847          the list.  */
13848       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13849         break;
13850
13851       /* Peek at the next token.  */
13852       token = cp_lexer_peek_nth_token (parser->lexer, 2);
13853       /* If the next token is a `}', then we're still done.  An
13854          initializer-clause can have a trailing `,' after the
13855          initializer-list and before the closing `}'.  */
13856       if (token->type == CPP_CLOSE_BRACE)
13857         break;
13858
13859       /* Consume the `,' token.  */
13860       cp_lexer_consume_token (parser->lexer);
13861     }
13862
13863   return v;
13864 }
13865
13866 /* Classes [gram.class] */
13867
13868 /* Parse a class-name.
13869
13870    class-name:
13871      identifier
13872      template-id
13873
13874    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13875    to indicate that names looked up in dependent types should be
13876    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
13877    keyword has been used to indicate that the name that appears next
13878    is a template.  TAG_TYPE indicates the explicit tag given before
13879    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
13880    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
13881    is the class being defined in a class-head.
13882
13883    Returns the TYPE_DECL representing the class.  */
13884
13885 static tree
13886 cp_parser_class_name (cp_parser *parser,
13887                       bool typename_keyword_p,
13888                       bool template_keyword_p,
13889                       enum tag_types tag_type,
13890                       bool check_dependency_p,
13891                       bool class_head_p,
13892                       bool is_declaration)
13893 {
13894   tree decl;
13895   tree scope;
13896   bool typename_p;
13897   cp_token *token;
13898
13899   /* All class-names start with an identifier.  */
13900   token = cp_lexer_peek_token (parser->lexer);
13901   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13902     {
13903       cp_parser_error (parser, "expected class-name");
13904       return error_mark_node;
13905     }
13906
13907   /* PARSER->SCOPE can be cleared when parsing the template-arguments
13908      to a template-id, so we save it here.  */
13909   scope = parser->scope;
13910   if (scope == error_mark_node)
13911     return error_mark_node;
13912
13913   /* Any name names a type if we're following the `typename' keyword
13914      in a qualified name where the enclosing scope is type-dependent.  */
13915   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13916                 && dependent_type_p (scope));
13917   /* Handle the common case (an identifier, but not a template-id)
13918      efficiently.  */
13919   if (token->type == CPP_NAME
13920       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13921     {
13922       cp_token *identifier_token;
13923       tree identifier;
13924       bool ambiguous_p;
13925
13926       /* Look for the identifier.  */
13927       identifier_token = cp_lexer_peek_token (parser->lexer);
13928       ambiguous_p = identifier_token->ambiguous_p;
13929       identifier = cp_parser_identifier (parser);
13930       /* If the next token isn't an identifier, we are certainly not
13931          looking at a class-name.  */
13932       if (identifier == error_mark_node)
13933         decl = error_mark_node;
13934       /* If we know this is a type-name, there's no need to look it
13935          up.  */
13936       else if (typename_p)
13937         decl = identifier;
13938       else
13939         {
13940           tree ambiguous_decls;
13941           /* If we already know that this lookup is ambiguous, then
13942              we've already issued an error message; there's no reason
13943              to check again.  */
13944           if (ambiguous_p)
13945             {
13946               cp_parser_simulate_error (parser);
13947               return error_mark_node;
13948             }
13949           /* If the next token is a `::', then the name must be a type
13950              name.
13951
13952              [basic.lookup.qual]
13953
13954              During the lookup for a name preceding the :: scope
13955              resolution operator, object, function, and enumerator
13956              names are ignored.  */
13957           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13958             tag_type = typename_type;
13959           /* Look up the name.  */
13960           decl = cp_parser_lookup_name (parser, identifier,
13961                                         tag_type,
13962                                         /*is_template=*/false,
13963                                         /*is_namespace=*/false,
13964                                         check_dependency_p,
13965                                         &ambiguous_decls);
13966           if (ambiguous_decls)
13967             {
13968               error ("reference to %qD is ambiguous", identifier);
13969               print_candidates (ambiguous_decls);
13970               if (cp_parser_parsing_tentatively (parser))
13971                 {
13972                   identifier_token->ambiguous_p = true;
13973                   cp_parser_simulate_error (parser);
13974                 }
13975               return error_mark_node;
13976             }
13977         }
13978     }
13979   else
13980     {
13981       /* Try a template-id.  */
13982       decl = cp_parser_template_id (parser, template_keyword_p,
13983                                     check_dependency_p,
13984                                     is_declaration);
13985       if (decl == error_mark_node)
13986         return error_mark_node;
13987     }
13988
13989   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13990
13991   /* If this is a typename, create a TYPENAME_TYPE.  */
13992   if (typename_p && decl != error_mark_node)
13993     {
13994       decl = make_typename_type (scope, decl, typename_type,
13995                                  /*complain=*/tf_error);
13996       if (decl != error_mark_node)
13997         decl = TYPE_NAME (decl);
13998     }
13999
14000   /* Check to see that it is really the name of a class.  */
14001   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14002       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14003       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14004     /* Situations like this:
14005
14006          template <typename T> struct A {
14007            typename T::template X<int>::I i;
14008          };
14009
14010        are problematic.  Is `T::template X<int>' a class-name?  The
14011        standard does not seem to be definitive, but there is no other
14012        valid interpretation of the following `::'.  Therefore, those
14013        names are considered class-names.  */
14014     {
14015       decl = make_typename_type (scope, decl, tag_type, tf_error);
14016       if (decl != error_mark_node)
14017         decl = TYPE_NAME (decl);
14018     }
14019   else if (TREE_CODE (decl) != TYPE_DECL
14020            || TREE_TYPE (decl) == error_mark_node
14021            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
14022     decl = error_mark_node;
14023
14024   if (decl == error_mark_node)
14025     cp_parser_error (parser, "expected class-name");
14026
14027   return decl;
14028 }
14029
14030 /* Parse a class-specifier.
14031
14032    class-specifier:
14033      class-head { member-specification [opt] }
14034
14035    Returns the TREE_TYPE representing the class.  */
14036
14037 static tree
14038 cp_parser_class_specifier (cp_parser* parser)
14039 {
14040   cp_token *token;
14041   tree type;
14042   tree attributes = NULL_TREE;
14043   int has_trailing_semicolon;
14044   bool nested_name_specifier_p;
14045   unsigned saved_num_template_parameter_lists;
14046   bool saved_in_function_body;
14047   tree old_scope = NULL_TREE;
14048   tree scope = NULL_TREE;
14049   tree bases;
14050
14051   push_deferring_access_checks (dk_no_deferred);
14052
14053   /* Parse the class-head.  */
14054   type = cp_parser_class_head (parser,
14055                                &nested_name_specifier_p,
14056                                &attributes,
14057                                &bases);
14058   /* If the class-head was a semantic disaster, skip the entire body
14059      of the class.  */
14060   if (!type)
14061     {
14062       cp_parser_skip_to_end_of_block_or_statement (parser);
14063       pop_deferring_access_checks ();
14064       return error_mark_node;
14065     }
14066
14067   /* Look for the `{'.  */
14068   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
14069     {
14070       pop_deferring_access_checks ();
14071       return error_mark_node;
14072     }
14073
14074   /* Process the base classes. If they're invalid, skip the 
14075      entire class body.  */
14076   if (!xref_basetypes (type, bases))
14077     {
14078       /* Consuming the closing brace yields better error messages
14079          later on.  */
14080       if (cp_parser_skip_to_closing_brace (parser))
14081         cp_lexer_consume_token (parser->lexer);
14082       pop_deferring_access_checks ();
14083       return error_mark_node;
14084     }
14085
14086   /* Issue an error message if type-definitions are forbidden here.  */
14087   cp_parser_check_type_definition (parser);
14088   /* Remember that we are defining one more class.  */
14089   ++parser->num_classes_being_defined;
14090   /* Inside the class, surrounding template-parameter-lists do not
14091      apply.  */
14092   saved_num_template_parameter_lists
14093     = parser->num_template_parameter_lists;
14094   parser->num_template_parameter_lists = 0;
14095   /* We are not in a function body.  */
14096   saved_in_function_body = parser->in_function_body;
14097   parser->in_function_body = false;
14098
14099   /* Start the class.  */
14100   if (nested_name_specifier_p)
14101     {
14102       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14103       old_scope = push_inner_scope (scope);
14104     }
14105   type = begin_class_definition (type, attributes);
14106
14107   if (type == error_mark_node)
14108     /* If the type is erroneous, skip the entire body of the class.  */
14109     cp_parser_skip_to_closing_brace (parser);
14110   else
14111     /* Parse the member-specification.  */
14112     cp_parser_member_specification_opt (parser);
14113
14114   /* Look for the trailing `}'.  */
14115   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14116   /* We get better error messages by noticing a common problem: a
14117      missing trailing `;'.  */
14118   token = cp_lexer_peek_token (parser->lexer);
14119   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14120   /* Look for trailing attributes to apply to this class.  */
14121   if (cp_parser_allow_gnu_extensions_p (parser))
14122     attributes = cp_parser_attributes_opt (parser);
14123   if (type != error_mark_node)
14124     type = finish_struct (type, attributes);
14125   if (nested_name_specifier_p)
14126     pop_inner_scope (old_scope, scope);
14127   /* If this class is not itself within the scope of another class,
14128      then we need to parse the bodies of all of the queued function
14129      definitions.  Note that the queued functions defined in a class
14130      are not always processed immediately following the
14131      class-specifier for that class.  Consider:
14132
14133        struct A {
14134          struct B { void f() { sizeof (A); } };
14135        };
14136
14137      If `f' were processed before the processing of `A' were
14138      completed, there would be no way to compute the size of `A'.
14139      Note that the nesting we are interested in here is lexical --
14140      not the semantic nesting given by TYPE_CONTEXT.  In particular,
14141      for:
14142
14143        struct A { struct B; };
14144        struct A::B { void f() { } };
14145
14146      there is no need to delay the parsing of `A::B::f'.  */
14147   if (--parser->num_classes_being_defined == 0)
14148     {
14149       tree queue_entry;
14150       tree fn;
14151       tree class_type = NULL_TREE;
14152       tree pushed_scope = NULL_TREE;
14153
14154       /* In a first pass, parse default arguments to the functions.
14155          Then, in a second pass, parse the bodies of the functions.
14156          This two-phased approach handles cases like:
14157
14158             struct S {
14159               void f() { g(); }
14160               void g(int i = 3);
14161             };
14162
14163          */
14164       for (TREE_PURPOSE (parser->unparsed_functions_queues)
14165              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14166            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14167            TREE_PURPOSE (parser->unparsed_functions_queues)
14168              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14169         {
14170           fn = TREE_VALUE (queue_entry);
14171           /* If there are default arguments that have not yet been processed,
14172              take care of them now.  */
14173           if (class_type != TREE_PURPOSE (queue_entry))
14174             {
14175               if (pushed_scope)
14176                 pop_scope (pushed_scope);
14177               class_type = TREE_PURPOSE (queue_entry);
14178               pushed_scope = push_scope (class_type);
14179             }
14180           /* Make sure that any template parameters are in scope.  */
14181           maybe_begin_member_template_processing (fn);
14182           /* Parse the default argument expressions.  */
14183           cp_parser_late_parsing_default_args (parser, fn);
14184           /* Remove any template parameters from the symbol table.  */
14185           maybe_end_member_template_processing ();
14186         }
14187       if (pushed_scope)
14188         pop_scope (pushed_scope);
14189       /* Now parse the body of the functions.  */
14190       for (TREE_VALUE (parser->unparsed_functions_queues)
14191              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14192            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14193            TREE_VALUE (parser->unparsed_functions_queues)
14194              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14195         {
14196           /* Figure out which function we need to process.  */
14197           fn = TREE_VALUE (queue_entry);
14198           /* Parse the function.  */
14199           cp_parser_late_parsing_for_member (parser, fn);
14200         }
14201     }
14202
14203   /* Put back any saved access checks.  */
14204   pop_deferring_access_checks ();
14205
14206   /* Restore saved state.  */
14207   parser->in_function_body = saved_in_function_body;
14208   parser->num_template_parameter_lists
14209     = saved_num_template_parameter_lists;
14210
14211   return type;
14212 }
14213
14214 /* Parse a class-head.
14215
14216    class-head:
14217      class-key identifier [opt] base-clause [opt]
14218      class-key nested-name-specifier identifier base-clause [opt]
14219      class-key nested-name-specifier [opt] template-id
14220        base-clause [opt]
14221
14222    GNU Extensions:
14223      class-key attributes identifier [opt] base-clause [opt]
14224      class-key attributes nested-name-specifier identifier base-clause [opt]
14225      class-key attributes nested-name-specifier [opt] template-id
14226        base-clause [opt]
14227
14228    Upon return BASES is initialized to the list of base classes (or
14229    NULL, if there are none) in the same form returned by
14230    cp_parser_base_clause.
14231
14232    Returns the TYPE of the indicated class.  Sets
14233    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14234    involving a nested-name-specifier was used, and FALSE otherwise.
14235
14236    Returns error_mark_node if this is not a class-head.
14237
14238    Returns NULL_TREE if the class-head is syntactically valid, but
14239    semantically invalid in a way that means we should skip the entire
14240    body of the class.  */
14241
14242 static tree
14243 cp_parser_class_head (cp_parser* parser,
14244                       bool* nested_name_specifier_p,
14245                       tree *attributes_p,
14246                       tree *bases)
14247 {
14248   tree nested_name_specifier;
14249   enum tag_types class_key;
14250   tree id = NULL_TREE;
14251   tree type = NULL_TREE;
14252   tree attributes;
14253   bool template_id_p = false;
14254   bool qualified_p = false;
14255   bool invalid_nested_name_p = false;
14256   bool invalid_explicit_specialization_p = false;
14257   tree pushed_scope = NULL_TREE;
14258   unsigned num_templates;
14259
14260   /* Assume no nested-name-specifier will be present.  */
14261   *nested_name_specifier_p = false;
14262   /* Assume no template parameter lists will be used in defining the
14263      type.  */
14264   num_templates = 0;
14265
14266   *bases = NULL_TREE;
14267
14268   /* Look for the class-key.  */
14269   class_key = cp_parser_class_key (parser);
14270   if (class_key == none_type)
14271     return error_mark_node;
14272
14273   /* Parse the attributes.  */
14274   attributes = cp_parser_attributes_opt (parser);
14275
14276   /* If the next token is `::', that is invalid -- but sometimes
14277      people do try to write:
14278
14279        struct ::S {};
14280
14281      Handle this gracefully by accepting the extra qualifier, and then
14282      issuing an error about it later if this really is a
14283      class-head.  If it turns out just to be an elaborated type
14284      specifier, remain silent.  */
14285   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14286     qualified_p = true;
14287
14288   push_deferring_access_checks (dk_no_check);
14289
14290   /* Determine the name of the class.  Begin by looking for an
14291      optional nested-name-specifier.  */
14292   nested_name_specifier
14293     = cp_parser_nested_name_specifier_opt (parser,
14294                                            /*typename_keyword_p=*/false,
14295                                            /*check_dependency_p=*/false,
14296                                            /*type_p=*/false,
14297                                            /*is_declaration=*/false);
14298   /* If there was a nested-name-specifier, then there *must* be an
14299      identifier.  */
14300   if (nested_name_specifier)
14301     {
14302       /* Although the grammar says `identifier', it really means
14303          `class-name' or `template-name'.  You are only allowed to
14304          define a class that has already been declared with this
14305          syntax.
14306
14307          The proposed resolution for Core Issue 180 says that wherever
14308          you see `class T::X' you should treat `X' as a type-name.
14309
14310          It is OK to define an inaccessible class; for example:
14311
14312            class A { class B; };
14313            class A::B {};
14314
14315          We do not know if we will see a class-name, or a
14316          template-name.  We look for a class-name first, in case the
14317          class-name is a template-id; if we looked for the
14318          template-name first we would stop after the template-name.  */
14319       cp_parser_parse_tentatively (parser);
14320       type = cp_parser_class_name (parser,
14321                                    /*typename_keyword_p=*/false,
14322                                    /*template_keyword_p=*/false,
14323                                    class_type,
14324                                    /*check_dependency_p=*/false,
14325                                    /*class_head_p=*/true,
14326                                    /*is_declaration=*/false);
14327       /* If that didn't work, ignore the nested-name-specifier.  */
14328       if (!cp_parser_parse_definitely (parser))
14329         {
14330           invalid_nested_name_p = true;
14331           id = cp_parser_identifier (parser);
14332           if (id == error_mark_node)
14333             id = NULL_TREE;
14334         }
14335       /* If we could not find a corresponding TYPE, treat this
14336          declaration like an unqualified declaration.  */
14337       if (type == error_mark_node)
14338         nested_name_specifier = NULL_TREE;
14339       /* Otherwise, count the number of templates used in TYPE and its
14340          containing scopes.  */
14341       else
14342         {
14343           tree scope;
14344
14345           for (scope = TREE_TYPE (type);
14346                scope && TREE_CODE (scope) != NAMESPACE_DECL;
14347                scope = (TYPE_P (scope)
14348                         ? TYPE_CONTEXT (scope)
14349                         : DECL_CONTEXT (scope)))
14350             if (TYPE_P (scope)
14351                 && CLASS_TYPE_P (scope)
14352                 && CLASSTYPE_TEMPLATE_INFO (scope)
14353                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14354                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14355               ++num_templates;
14356         }
14357     }
14358   /* Otherwise, the identifier is optional.  */
14359   else
14360     {
14361       /* We don't know whether what comes next is a template-id,
14362          an identifier, or nothing at all.  */
14363       cp_parser_parse_tentatively (parser);
14364       /* Check for a template-id.  */
14365       id = cp_parser_template_id (parser,
14366                                   /*template_keyword_p=*/false,
14367                                   /*check_dependency_p=*/true,
14368                                   /*is_declaration=*/true);
14369       /* If that didn't work, it could still be an identifier.  */
14370       if (!cp_parser_parse_definitely (parser))
14371         {
14372           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14373             id = cp_parser_identifier (parser);
14374           else
14375             id = NULL_TREE;
14376         }
14377       else
14378         {
14379           template_id_p = true;
14380           ++num_templates;
14381         }
14382     }
14383
14384   pop_deferring_access_checks ();
14385
14386   if (id)
14387     cp_parser_check_for_invalid_template_id (parser, id);
14388
14389   /* If it's not a `:' or a `{' then we can't really be looking at a
14390      class-head, since a class-head only appears as part of a
14391      class-specifier.  We have to detect this situation before calling
14392      xref_tag, since that has irreversible side-effects.  */
14393   if (!cp_parser_next_token_starts_class_definition_p (parser))
14394     {
14395       cp_parser_error (parser, "expected %<{%> or %<:%>");
14396       return error_mark_node;
14397     }
14398
14399   /* At this point, we're going ahead with the class-specifier, even
14400      if some other problem occurs.  */
14401   cp_parser_commit_to_tentative_parse (parser);
14402   /* Issue the error about the overly-qualified name now.  */
14403   if (qualified_p)
14404     cp_parser_error (parser,
14405                      "global qualification of class name is invalid");
14406   else if (invalid_nested_name_p)
14407     cp_parser_error (parser,
14408                      "qualified name does not name a class");
14409   else if (nested_name_specifier)
14410     {
14411       tree scope;
14412
14413       /* Reject typedef-names in class heads.  */
14414       if (!DECL_IMPLICIT_TYPEDEF_P (type))
14415         {
14416           error ("invalid class name in declaration of %qD", type);
14417           type = NULL_TREE;
14418           goto done;
14419         }
14420
14421       /* Figure out in what scope the declaration is being placed.  */
14422       scope = current_scope ();
14423       /* If that scope does not contain the scope in which the
14424          class was originally declared, the program is invalid.  */
14425       if (scope && !is_ancestor (scope, nested_name_specifier))
14426         {
14427           if (at_namespace_scope_p ())
14428             error ("declaration of %qD in namespace %qD which does not "
14429                    "enclose %qD", type, scope, nested_name_specifier);
14430           else
14431             error ("declaration of %qD in %qD which does not enclose %qD",
14432                    type, scope, nested_name_specifier);
14433           type = NULL_TREE;
14434           goto done;
14435         }
14436       /* [dcl.meaning]
14437
14438          A declarator-id shall not be qualified exception of the
14439          definition of a ... nested class outside of its class
14440          ... [or] a the definition or explicit instantiation of a
14441          class member of a namespace outside of its namespace.  */
14442       if (scope == nested_name_specifier)
14443         {
14444           pedwarn ("extra qualification ignored");
14445           nested_name_specifier = NULL_TREE;
14446           num_templates = 0;
14447         }
14448     }
14449   /* An explicit-specialization must be preceded by "template <>".  If
14450      it is not, try to recover gracefully.  */
14451   if (at_namespace_scope_p ()
14452       && parser->num_template_parameter_lists == 0
14453       && template_id_p)
14454     {
14455       error ("an explicit specialization must be preceded by %<template <>%>");
14456       invalid_explicit_specialization_p = true;
14457       /* Take the same action that would have been taken by
14458          cp_parser_explicit_specialization.  */
14459       ++parser->num_template_parameter_lists;
14460       begin_specialization ();
14461     }
14462   /* There must be no "return" statements between this point and the
14463      end of this function; set "type "to the correct return value and
14464      use "goto done;" to return.  */
14465   /* Make sure that the right number of template parameters were
14466      present.  */
14467   if (!cp_parser_check_template_parameters (parser, num_templates))
14468     {
14469       /* If something went wrong, there is no point in even trying to
14470          process the class-definition.  */
14471       type = NULL_TREE;
14472       goto done;
14473     }
14474
14475   /* Look up the type.  */
14476   if (template_id_p)
14477     {
14478       type = TREE_TYPE (id);
14479       type = maybe_process_partial_specialization (type);
14480       if (nested_name_specifier)
14481         pushed_scope = push_scope (nested_name_specifier);
14482     }
14483   else if (nested_name_specifier)
14484     {
14485       tree class_type;
14486
14487       /* Given:
14488
14489             template <typename T> struct S { struct T };
14490             template <typename T> struct S<T>::T { };
14491
14492          we will get a TYPENAME_TYPE when processing the definition of
14493          `S::T'.  We need to resolve it to the actual type before we
14494          try to define it.  */
14495       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14496         {
14497           class_type = resolve_typename_type (TREE_TYPE (type),
14498                                               /*only_current_p=*/false);
14499           if (TREE_CODE (class_type) != TYPENAME_TYPE)
14500             type = TYPE_NAME (class_type);
14501           else
14502             {
14503               cp_parser_error (parser, "could not resolve typename type");
14504               type = error_mark_node;
14505             }
14506         }
14507
14508       maybe_process_partial_specialization (TREE_TYPE (type));
14509       class_type = current_class_type;
14510       /* Enter the scope indicated by the nested-name-specifier.  */
14511       pushed_scope = push_scope (nested_name_specifier);
14512       /* Get the canonical version of this type.  */
14513       type = TYPE_MAIN_DECL (TREE_TYPE (type));
14514       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14515           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14516         {
14517           type = push_template_decl (type);
14518           if (type == error_mark_node)
14519             {
14520               type = NULL_TREE;
14521               goto done;
14522             }
14523         }
14524
14525       type = TREE_TYPE (type);
14526       *nested_name_specifier_p = true;
14527     }
14528   else      /* The name is not a nested name.  */
14529     {
14530       /* If the class was unnamed, create a dummy name.  */
14531       if (!id)
14532         id = make_anon_name ();
14533       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14534                        parser->num_template_parameter_lists);
14535     }
14536
14537   /* Indicate whether this class was declared as a `class' or as a
14538      `struct'.  */
14539   if (TREE_CODE (type) == RECORD_TYPE)
14540     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14541   cp_parser_check_class_key (class_key, type);
14542
14543   /* If this type was already complete, and we see another definition,
14544      that's an error.  */
14545   if (type != error_mark_node && COMPLETE_TYPE_P (type))
14546     {
14547       error ("redefinition of %q#T", type);
14548       error ("previous definition of %q+#T", type);
14549       type = NULL_TREE;
14550       goto done;
14551     }
14552   else if (type == error_mark_node)
14553     type = NULL_TREE;
14554
14555   /* We will have entered the scope containing the class; the names of
14556      base classes should be looked up in that context.  For example:
14557
14558        struct A { struct B {}; struct C; };
14559        struct A::C : B {};
14560
14561      is valid.  */
14562
14563   /* Get the list of base-classes, if there is one.  */
14564   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14565     *bases = cp_parser_base_clause (parser);
14566
14567  done:
14568   /* Leave the scope given by the nested-name-specifier.  We will
14569      enter the class scope itself while processing the members.  */
14570   if (pushed_scope)
14571     pop_scope (pushed_scope);
14572
14573   if (invalid_explicit_specialization_p)
14574     {
14575       end_specialization ();
14576       --parser->num_template_parameter_lists;
14577     }
14578   *attributes_p = attributes;
14579   return type;
14580 }
14581
14582 /* Parse a class-key.
14583
14584    class-key:
14585      class
14586      struct
14587      union
14588
14589    Returns the kind of class-key specified, or none_type to indicate
14590    error.  */
14591
14592 static enum tag_types
14593 cp_parser_class_key (cp_parser* parser)
14594 {
14595   cp_token *token;
14596   enum tag_types tag_type;
14597
14598   /* Look for the class-key.  */
14599   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14600   if (!token)
14601     return none_type;
14602
14603   /* Check to see if the TOKEN is a class-key.  */
14604   tag_type = cp_parser_token_is_class_key (token);
14605   if (!tag_type)
14606     cp_parser_error (parser, "expected class-key");
14607   return tag_type;
14608 }
14609
14610 /* Parse an (optional) member-specification.
14611
14612    member-specification:
14613      member-declaration member-specification [opt]
14614      access-specifier : member-specification [opt]  */
14615
14616 static void
14617 cp_parser_member_specification_opt (cp_parser* parser)
14618 {
14619   while (true)
14620     {
14621       cp_token *token;
14622       enum rid keyword;
14623
14624       /* Peek at the next token.  */
14625       token = cp_lexer_peek_token (parser->lexer);
14626       /* If it's a `}', or EOF then we've seen all the members.  */
14627       if (token->type == CPP_CLOSE_BRACE
14628           || token->type == CPP_EOF
14629           || token->type == CPP_PRAGMA_EOL)
14630         break;
14631
14632       /* See if this token is a keyword.  */
14633       keyword = token->keyword;
14634       switch (keyword)
14635         {
14636         case RID_PUBLIC:
14637         case RID_PROTECTED:
14638         case RID_PRIVATE:
14639           /* Consume the access-specifier.  */
14640           cp_lexer_consume_token (parser->lexer);
14641           /* Remember which access-specifier is active.  */
14642           current_access_specifier = token->u.value;
14643           /* Look for the `:'.  */
14644           cp_parser_require (parser, CPP_COLON, "`:'");
14645           break;
14646
14647         default:
14648           /* Accept #pragmas at class scope.  */
14649           if (token->type == CPP_PRAGMA)
14650             {
14651               cp_parser_pragma (parser, pragma_external);
14652               break;
14653             }
14654
14655           /* Otherwise, the next construction must be a
14656              member-declaration.  */
14657           cp_parser_member_declaration (parser);
14658         }
14659     }
14660 }
14661
14662 /* Parse a member-declaration.
14663
14664    member-declaration:
14665      decl-specifier-seq [opt] member-declarator-list [opt] ;
14666      function-definition ; [opt]
14667      :: [opt] nested-name-specifier template [opt] unqualified-id ;
14668      using-declaration
14669      template-declaration
14670
14671    member-declarator-list:
14672      member-declarator
14673      member-declarator-list , member-declarator
14674
14675    member-declarator:
14676      declarator pure-specifier [opt]
14677      declarator constant-initializer [opt]
14678      identifier [opt] : constant-expression
14679
14680    GNU Extensions:
14681
14682    member-declaration:
14683      __extension__ member-declaration
14684
14685    member-declarator:
14686      declarator attributes [opt] pure-specifier [opt]
14687      declarator attributes [opt] constant-initializer [opt]
14688      identifier [opt] attributes [opt] : constant-expression  
14689
14690    C++0x Extensions:
14691
14692    member-declaration:
14693      static_assert-declaration  */
14694
14695 static void
14696 cp_parser_member_declaration (cp_parser* parser)
14697 {
14698   cp_decl_specifier_seq decl_specifiers;
14699   tree prefix_attributes;
14700   tree decl;
14701   int declares_class_or_enum;
14702   bool friend_p;
14703   cp_token *token;
14704   int saved_pedantic;
14705
14706   /* Check for the `__extension__' keyword.  */
14707   if (cp_parser_extension_opt (parser, &saved_pedantic))
14708     {
14709       /* Recurse.  */
14710       cp_parser_member_declaration (parser);
14711       /* Restore the old value of the PEDANTIC flag.  */
14712       pedantic = saved_pedantic;
14713
14714       return;
14715     }
14716
14717   /* Check for a template-declaration.  */
14718   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14719     {
14720       /* An explicit specialization here is an error condition, and we
14721          expect the specialization handler to detect and report this.  */
14722       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14723           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14724         cp_parser_explicit_specialization (parser);
14725       else
14726         cp_parser_template_declaration (parser, /*member_p=*/true);
14727
14728       return;
14729     }
14730
14731   /* Check for a using-declaration.  */
14732   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14733     {
14734       /* Parse the using-declaration.  */
14735       cp_parser_using_declaration (parser,
14736                                    /*access_declaration_p=*/false);
14737       return;
14738     }
14739
14740   /* Check for @defs.  */
14741   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14742     {
14743       tree ivar, member;
14744       tree ivar_chains = cp_parser_objc_defs_expression (parser);
14745       ivar = ivar_chains;
14746       while (ivar)
14747         {
14748           member = ivar;
14749           ivar = TREE_CHAIN (member);
14750           TREE_CHAIN (member) = NULL_TREE;
14751           finish_member_declaration (member);
14752         }
14753       return;
14754     }
14755
14756   /* If the next token is `static_assert' we have a static assertion.  */
14757   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14758     {
14759       cp_parser_static_assert (parser, /*member_p=*/true);
14760       return;
14761     }
14762
14763   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14764     return;
14765
14766   /* Parse the decl-specifier-seq.  */
14767   cp_parser_decl_specifier_seq (parser,
14768                                 CP_PARSER_FLAGS_OPTIONAL,
14769                                 &decl_specifiers,
14770                                 &declares_class_or_enum);
14771   prefix_attributes = decl_specifiers.attributes;
14772   decl_specifiers.attributes = NULL_TREE;
14773   /* Check for an invalid type-name.  */
14774   if (!decl_specifiers.type
14775       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14776     return;
14777   /* If there is no declarator, then the decl-specifier-seq should
14778      specify a type.  */
14779   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14780     {
14781       /* If there was no decl-specifier-seq, and the next token is a
14782          `;', then we have something like:
14783
14784            struct S { ; };
14785
14786          [class.mem]
14787
14788          Each member-declaration shall declare at least one member
14789          name of the class.  */
14790       if (!decl_specifiers.any_specifiers_p)
14791         {
14792           cp_token *token = cp_lexer_peek_token (parser->lexer);
14793           if (pedantic && !token->in_system_header)
14794             pedwarn ("%Hextra %<;%>", &token->location);
14795         }
14796       else
14797         {
14798           tree type;
14799
14800           /* See if this declaration is a friend.  */
14801           friend_p = cp_parser_friend_p (&decl_specifiers);
14802           /* If there were decl-specifiers, check to see if there was
14803              a class-declaration.  */
14804           type = check_tag_decl (&decl_specifiers);
14805           /* Nested classes have already been added to the class, but
14806              a `friend' needs to be explicitly registered.  */
14807           if (friend_p)
14808             {
14809               /* If the `friend' keyword was present, the friend must
14810                  be introduced with a class-key.  */
14811                if (!declares_class_or_enum)
14812                  error ("a class-key must be used when declaring a friend");
14813                /* In this case:
14814
14815                     template <typename T> struct A {
14816                       friend struct A<T>::B;
14817                     };
14818
14819                   A<T>::B will be represented by a TYPENAME_TYPE, and
14820                   therefore not recognized by check_tag_decl.  */
14821                if (!type
14822                    && decl_specifiers.type
14823                    && TYPE_P (decl_specifiers.type))
14824                  type = decl_specifiers.type;
14825                if (!type || !TYPE_P (type))
14826                  error ("friend declaration does not name a class or "
14827                         "function");
14828                else
14829                  make_friend_class (current_class_type, type,
14830                                     /*complain=*/true);
14831             }
14832           /* If there is no TYPE, an error message will already have
14833              been issued.  */
14834           else if (!type || type == error_mark_node)
14835             ;
14836           /* An anonymous aggregate has to be handled specially; such
14837              a declaration really declares a data member (with a
14838              particular type), as opposed to a nested class.  */
14839           else if (ANON_AGGR_TYPE_P (type))
14840             {
14841               /* Remove constructors and such from TYPE, now that we
14842                  know it is an anonymous aggregate.  */
14843               fixup_anonymous_aggr (type);
14844               /* And make the corresponding data member.  */
14845               decl = build_decl (FIELD_DECL, NULL_TREE, type);
14846               /* Add it to the class.  */
14847               finish_member_declaration (decl);
14848             }
14849           else
14850             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14851         }
14852     }
14853   else
14854     {
14855       /* See if these declarations will be friends.  */
14856       friend_p = cp_parser_friend_p (&decl_specifiers);
14857
14858       /* Keep going until we hit the `;' at the end of the
14859          declaration.  */
14860       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14861         {
14862           tree attributes = NULL_TREE;
14863           tree first_attribute;
14864
14865           /* Peek at the next token.  */
14866           token = cp_lexer_peek_token (parser->lexer);
14867
14868           /* Check for a bitfield declaration.  */
14869           if (token->type == CPP_COLON
14870               || (token->type == CPP_NAME
14871                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14872                   == CPP_COLON))
14873             {
14874               tree identifier;
14875               tree width;
14876
14877               /* Get the name of the bitfield.  Note that we cannot just
14878                  check TOKEN here because it may have been invalidated by
14879                  the call to cp_lexer_peek_nth_token above.  */
14880               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14881                 identifier = cp_parser_identifier (parser);
14882               else
14883                 identifier = NULL_TREE;
14884
14885               /* Consume the `:' token.  */
14886               cp_lexer_consume_token (parser->lexer);
14887               /* Get the width of the bitfield.  */
14888               width
14889                 = cp_parser_constant_expression (parser,
14890                                                  /*allow_non_constant=*/false,
14891                                                  NULL);
14892
14893               /* Look for attributes that apply to the bitfield.  */
14894               attributes = cp_parser_attributes_opt (parser);
14895               /* Remember which attributes are prefix attributes and
14896                  which are not.  */
14897               first_attribute = attributes;
14898               /* Combine the attributes.  */
14899               attributes = chainon (prefix_attributes, attributes);
14900
14901               /* Create the bitfield declaration.  */
14902               decl = grokbitfield (identifier
14903                                    ? make_id_declarator (NULL_TREE,
14904                                                          identifier,
14905                                                          sfk_none)
14906                                    : NULL,
14907                                    &decl_specifiers,
14908                                    width);
14909               /* Apply the attributes.  */
14910               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14911             }
14912           else
14913             {
14914               cp_declarator *declarator;
14915               tree initializer;
14916               tree asm_specification;
14917               int ctor_dtor_or_conv_p;
14918
14919               /* Parse the declarator.  */
14920               declarator
14921                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14922                                         &ctor_dtor_or_conv_p,
14923                                         /*parenthesized_p=*/NULL,
14924                                         /*member_p=*/true);
14925
14926               /* If something went wrong parsing the declarator, make sure
14927                  that we at least consume some tokens.  */
14928               if (declarator == cp_error_declarator)
14929                 {
14930                   /* Skip to the end of the statement.  */
14931                   cp_parser_skip_to_end_of_statement (parser);
14932                   /* If the next token is not a semicolon, that is
14933                      probably because we just skipped over the body of
14934                      a function.  So, we consume a semicolon if
14935                      present, but do not issue an error message if it
14936                      is not present.  */
14937                   if (cp_lexer_next_token_is (parser->lexer,
14938                                               CPP_SEMICOLON))
14939                     cp_lexer_consume_token (parser->lexer);
14940                   return;
14941                 }
14942
14943               if (declares_class_or_enum & 2)
14944                 cp_parser_check_for_definition_in_return_type
14945                   (declarator, decl_specifiers.type);
14946
14947               /* Look for an asm-specification.  */
14948               asm_specification = cp_parser_asm_specification_opt (parser);
14949               /* Look for attributes that apply to the declaration.  */
14950               attributes = cp_parser_attributes_opt (parser);
14951               /* Remember which attributes are prefix attributes and
14952                  which are not.  */
14953               first_attribute = attributes;
14954               /* Combine the attributes.  */
14955               attributes = chainon (prefix_attributes, attributes);
14956
14957               /* If it's an `=', then we have a constant-initializer or a
14958                  pure-specifier.  It is not correct to parse the
14959                  initializer before registering the member declaration
14960                  since the member declaration should be in scope while
14961                  its initializer is processed.  However, the rest of the
14962                  front end does not yet provide an interface that allows
14963                  us to handle this correctly.  */
14964               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14965                 {
14966                   /* In [class.mem]:
14967
14968                      A pure-specifier shall be used only in the declaration of
14969                      a virtual function.
14970
14971                      A member-declarator can contain a constant-initializer
14972                      only if it declares a static member of integral or
14973                      enumeration type.
14974
14975                      Therefore, if the DECLARATOR is for a function, we look
14976                      for a pure-specifier; otherwise, we look for a
14977                      constant-initializer.  When we call `grokfield', it will
14978                      perform more stringent semantics checks.  */
14979                   if (function_declarator_p (declarator))
14980                     initializer = cp_parser_pure_specifier (parser);
14981                   else
14982                     /* Parse the initializer.  */
14983                     initializer = cp_parser_constant_initializer (parser);
14984                 }
14985               /* Otherwise, there is no initializer.  */
14986               else
14987                 initializer = NULL_TREE;
14988
14989               /* See if we are probably looking at a function
14990                  definition.  We are certainly not looking at a
14991                  member-declarator.  Calling `grokfield' has
14992                  side-effects, so we must not do it unless we are sure
14993                  that we are looking at a member-declarator.  */
14994               if (cp_parser_token_starts_function_definition_p
14995                   (cp_lexer_peek_token (parser->lexer)))
14996                 {
14997                   /* The grammar does not allow a pure-specifier to be
14998                      used when a member function is defined.  (It is
14999                      possible that this fact is an oversight in the
15000                      standard, since a pure function may be defined
15001                      outside of the class-specifier.  */
15002                   if (initializer)
15003                     error ("pure-specifier on function-definition");
15004                   decl = cp_parser_save_member_function_body (parser,
15005                                                               &decl_specifiers,
15006                                                               declarator,
15007                                                               attributes);
15008                   /* If the member was not a friend, declare it here.  */
15009                   if (!friend_p)
15010                     finish_member_declaration (decl);
15011                   /* Peek at the next token.  */
15012                   token = cp_lexer_peek_token (parser->lexer);
15013                   /* If the next token is a semicolon, consume it.  */
15014                   if (token->type == CPP_SEMICOLON)
15015                     cp_lexer_consume_token (parser->lexer);
15016                   return;
15017                 }
15018               else
15019                 /* Create the declaration.  */
15020                 decl = grokfield (declarator, &decl_specifiers,
15021                                   initializer, /*init_const_expr_p=*/true,
15022                                   asm_specification,
15023                                   attributes);
15024             }
15025
15026           /* Reset PREFIX_ATTRIBUTES.  */
15027           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15028             attributes = TREE_CHAIN (attributes);
15029           if (attributes)
15030             TREE_CHAIN (attributes) = NULL_TREE;
15031
15032           /* If there is any qualification still in effect, clear it
15033              now; we will be starting fresh with the next declarator.  */
15034           parser->scope = NULL_TREE;
15035           parser->qualifying_scope = NULL_TREE;
15036           parser->object_scope = NULL_TREE;
15037           /* If it's a `,', then there are more declarators.  */
15038           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15039             cp_lexer_consume_token (parser->lexer);
15040           /* If the next token isn't a `;', then we have a parse error.  */
15041           else if (cp_lexer_next_token_is_not (parser->lexer,
15042                                                CPP_SEMICOLON))
15043             {
15044               cp_parser_error (parser, "expected %<;%>");
15045               /* Skip tokens until we find a `;'.  */
15046               cp_parser_skip_to_end_of_statement (parser);
15047
15048               break;
15049             }
15050
15051           if (decl)
15052             {
15053               /* Add DECL to the list of members.  */
15054               if (!friend_p)
15055                 finish_member_declaration (decl);
15056
15057               if (TREE_CODE (decl) == FUNCTION_DECL)
15058                 cp_parser_save_default_args (parser, decl);
15059             }
15060         }
15061     }
15062
15063   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15064 }
15065
15066 /* Parse a pure-specifier.
15067
15068    pure-specifier:
15069      = 0
15070
15071    Returns INTEGER_ZERO_NODE if a pure specifier is found.
15072    Otherwise, ERROR_MARK_NODE is returned.  */
15073
15074 static tree
15075 cp_parser_pure_specifier (cp_parser* parser)
15076 {
15077   cp_token *token;
15078
15079   /* Look for the `=' token.  */
15080   if (!cp_parser_require (parser, CPP_EQ, "`='"))
15081     return error_mark_node;
15082   /* Look for the `0' token.  */
15083   token = cp_lexer_consume_token (parser->lexer);
15084   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
15085   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15086     {
15087       cp_parser_error (parser,
15088                        "invalid pure specifier (only `= 0' is allowed)");
15089       cp_parser_skip_to_end_of_statement (parser);
15090       return error_mark_node;
15091     }
15092   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15093     {
15094       error ("templates may not be %<virtual%>");
15095       return error_mark_node;
15096     }
15097
15098   return integer_zero_node;
15099 }
15100
15101 /* Parse a constant-initializer.
15102
15103    constant-initializer:
15104      = constant-expression
15105
15106    Returns a representation of the constant-expression.  */
15107
15108 static tree
15109 cp_parser_constant_initializer (cp_parser* parser)
15110 {
15111   /* Look for the `=' token.  */
15112   if (!cp_parser_require (parser, CPP_EQ, "`='"))
15113     return error_mark_node;
15114
15115   /* It is invalid to write:
15116
15117        struct S { static const int i = { 7 }; };
15118
15119      */
15120   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15121     {
15122       cp_parser_error (parser,
15123                        "a brace-enclosed initializer is not allowed here");
15124       /* Consume the opening brace.  */
15125       cp_lexer_consume_token (parser->lexer);
15126       /* Skip the initializer.  */
15127       cp_parser_skip_to_closing_brace (parser);
15128       /* Look for the trailing `}'.  */
15129       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
15130
15131       return error_mark_node;
15132     }
15133
15134   return cp_parser_constant_expression (parser,
15135                                         /*allow_non_constant=*/false,
15136                                         NULL);
15137 }
15138
15139 /* Derived classes [gram.class.derived] */
15140
15141 /* Parse a base-clause.
15142
15143    base-clause:
15144      : base-specifier-list
15145
15146    base-specifier-list:
15147      base-specifier ... [opt]
15148      base-specifier-list , base-specifier ... [opt]
15149
15150    Returns a TREE_LIST representing the base-classes, in the order in
15151    which they were declared.  The representation of each node is as
15152    described by cp_parser_base_specifier.
15153
15154    In the case that no bases are specified, this function will return
15155    NULL_TREE, not ERROR_MARK_NODE.  */
15156
15157 static tree
15158 cp_parser_base_clause (cp_parser* parser)
15159 {
15160   tree bases = NULL_TREE;
15161
15162   /* Look for the `:' that begins the list.  */
15163   cp_parser_require (parser, CPP_COLON, "`:'");
15164
15165   /* Scan the base-specifier-list.  */
15166   while (true)
15167     {
15168       cp_token *token;
15169       tree base;
15170       bool pack_expansion_p = false;
15171
15172       /* Look for the base-specifier.  */
15173       base = cp_parser_base_specifier (parser);
15174       /* Look for the (optional) ellipsis. */
15175       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15176         {
15177           /* Consume the `...'. */
15178           cp_lexer_consume_token (parser->lexer);
15179
15180           pack_expansion_p = true;
15181         }
15182
15183       /* Add BASE to the front of the list.  */
15184       if (base != error_mark_node)
15185         {
15186           if (pack_expansion_p)
15187             /* Make this a pack expansion type. */
15188             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15189           else
15190             check_for_bare_parameter_packs (TREE_VALUE (base));
15191
15192           TREE_CHAIN (base) = bases;
15193           bases = base;
15194         }
15195       /* Peek at the next token.  */
15196       token = cp_lexer_peek_token (parser->lexer);
15197       /* If it's not a comma, then the list is complete.  */
15198       if (token->type != CPP_COMMA)
15199         break;
15200       /* Consume the `,'.  */
15201       cp_lexer_consume_token (parser->lexer);
15202     }
15203
15204   /* PARSER->SCOPE may still be non-NULL at this point, if the last
15205      base class had a qualified name.  However, the next name that
15206      appears is certainly not qualified.  */
15207   parser->scope = NULL_TREE;
15208   parser->qualifying_scope = NULL_TREE;
15209   parser->object_scope = NULL_TREE;
15210
15211   return nreverse (bases);
15212 }
15213
15214 /* Parse a base-specifier.
15215
15216    base-specifier:
15217      :: [opt] nested-name-specifier [opt] class-name
15218      virtual access-specifier [opt] :: [opt] nested-name-specifier
15219        [opt] class-name
15220      access-specifier virtual [opt] :: [opt] nested-name-specifier
15221        [opt] class-name
15222
15223    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
15224    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15225    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
15226    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
15227
15228 static tree
15229 cp_parser_base_specifier (cp_parser* parser)
15230 {
15231   cp_token *token;
15232   bool done = false;
15233   bool virtual_p = false;
15234   bool duplicate_virtual_error_issued_p = false;
15235   bool duplicate_access_error_issued_p = false;
15236   bool class_scope_p, template_p;
15237   tree access = access_default_node;
15238   tree type;
15239
15240   /* Process the optional `virtual' and `access-specifier'.  */
15241   while (!done)
15242     {
15243       /* Peek at the next token.  */
15244       token = cp_lexer_peek_token (parser->lexer);
15245       /* Process `virtual'.  */
15246       switch (token->keyword)
15247         {
15248         case RID_VIRTUAL:
15249           /* If `virtual' appears more than once, issue an error.  */
15250           if (virtual_p && !duplicate_virtual_error_issued_p)
15251             {
15252               cp_parser_error (parser,
15253                                "%<virtual%> specified more than once in base-specified");
15254               duplicate_virtual_error_issued_p = true;
15255             }
15256
15257           virtual_p = true;
15258
15259           /* Consume the `virtual' token.  */
15260           cp_lexer_consume_token (parser->lexer);
15261
15262           break;
15263
15264         case RID_PUBLIC:
15265         case RID_PROTECTED:
15266         case RID_PRIVATE:
15267           /* If more than one access specifier appears, issue an
15268              error.  */
15269           if (access != access_default_node
15270               && !duplicate_access_error_issued_p)
15271             {
15272               cp_parser_error (parser,
15273                                "more than one access specifier in base-specified");
15274               duplicate_access_error_issued_p = true;
15275             }
15276
15277           access = ridpointers[(int) token->keyword];
15278
15279           /* Consume the access-specifier.  */
15280           cp_lexer_consume_token (parser->lexer);
15281
15282           break;
15283
15284         default:
15285           done = true;
15286           break;
15287         }
15288     }
15289   /* It is not uncommon to see programs mechanically, erroneously, use
15290      the 'typename' keyword to denote (dependent) qualified types
15291      as base classes.  */
15292   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15293     {
15294       if (!processing_template_decl)
15295         error ("keyword %<typename%> not allowed outside of templates");
15296       else
15297         error ("keyword %<typename%> not allowed in this context "
15298                "(the base class is implicitly a type)");
15299       cp_lexer_consume_token (parser->lexer);
15300     }
15301
15302   /* Look for the optional `::' operator.  */
15303   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15304   /* Look for the nested-name-specifier.  The simplest way to
15305      implement:
15306
15307        [temp.res]
15308
15309        The keyword `typename' is not permitted in a base-specifier or
15310        mem-initializer; in these contexts a qualified name that
15311        depends on a template-parameter is implicitly assumed to be a
15312        type name.
15313
15314      is to pretend that we have seen the `typename' keyword at this
15315      point.  */
15316   cp_parser_nested_name_specifier_opt (parser,
15317                                        /*typename_keyword_p=*/true,
15318                                        /*check_dependency_p=*/true,
15319                                        typename_type,
15320                                        /*is_declaration=*/true);
15321   /* If the base class is given by a qualified name, assume that names
15322      we see are type names or templates, as appropriate.  */
15323   class_scope_p = (parser->scope && TYPE_P (parser->scope));
15324   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15325
15326   /* Finally, look for the class-name.  */
15327   type = cp_parser_class_name (parser,
15328                                class_scope_p,
15329                                template_p,
15330                                typename_type,
15331                                /*check_dependency_p=*/true,
15332                                /*class_head_p=*/false,
15333                                /*is_declaration=*/true);
15334
15335   if (type == error_mark_node)
15336     return error_mark_node;
15337
15338   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15339 }
15340
15341 /* Exception handling [gram.exception] */
15342
15343 /* Parse an (optional) exception-specification.
15344
15345    exception-specification:
15346      throw ( type-id-list [opt] )
15347
15348    Returns a TREE_LIST representing the exception-specification.  The
15349    TREE_VALUE of each node is a type.  */
15350
15351 static tree
15352 cp_parser_exception_specification_opt (cp_parser* parser)
15353 {
15354   cp_token *token;
15355   tree type_id_list;
15356
15357   /* Peek at the next token.  */
15358   token = cp_lexer_peek_token (parser->lexer);
15359   /* If it's not `throw', then there's no exception-specification.  */
15360   if (!cp_parser_is_keyword (token, RID_THROW))
15361     return NULL_TREE;
15362
15363   /* Consume the `throw'.  */
15364   cp_lexer_consume_token (parser->lexer);
15365
15366   /* Look for the `('.  */
15367   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15368
15369   /* Peek at the next token.  */
15370   token = cp_lexer_peek_token (parser->lexer);
15371   /* If it's not a `)', then there is a type-id-list.  */
15372   if (token->type != CPP_CLOSE_PAREN)
15373     {
15374       const char *saved_message;
15375
15376       /* Types may not be defined in an exception-specification.  */
15377       saved_message = parser->type_definition_forbidden_message;
15378       parser->type_definition_forbidden_message
15379         = "types may not be defined in an exception-specification";
15380       /* Parse the type-id-list.  */
15381       type_id_list = cp_parser_type_id_list (parser);
15382       /* Restore the saved message.  */
15383       parser->type_definition_forbidden_message = saved_message;
15384     }
15385   else
15386     type_id_list = empty_except_spec;
15387
15388   /* Look for the `)'.  */
15389   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15390
15391   return type_id_list;
15392 }
15393
15394 /* Parse an (optional) type-id-list.
15395
15396    type-id-list:
15397      type-id ... [opt]
15398      type-id-list , type-id ... [opt]
15399
15400    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
15401    in the order that the types were presented.  */
15402
15403 static tree
15404 cp_parser_type_id_list (cp_parser* parser)
15405 {
15406   tree types = NULL_TREE;
15407
15408   while (true)
15409     {
15410       cp_token *token;
15411       tree type;
15412
15413       /* Get the next type-id.  */
15414       type = cp_parser_type_id (parser);
15415       /* Parse the optional ellipsis. */
15416       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15417         {
15418           /* Consume the `...'. */
15419           cp_lexer_consume_token (parser->lexer);
15420
15421           /* Turn the type into a pack expansion expression. */
15422           type = make_pack_expansion (type);
15423         }
15424       /* Add it to the list.  */
15425       types = add_exception_specifier (types, type, /*complain=*/1);
15426       /* Peek at the next token.  */
15427       token = cp_lexer_peek_token (parser->lexer);
15428       /* If it is not a `,', we are done.  */
15429       if (token->type != CPP_COMMA)
15430         break;
15431       /* Consume the `,'.  */
15432       cp_lexer_consume_token (parser->lexer);
15433     }
15434
15435   return nreverse (types);
15436 }
15437
15438 /* Parse a try-block.
15439
15440    try-block:
15441      try compound-statement handler-seq  */
15442
15443 static tree
15444 cp_parser_try_block (cp_parser* parser)
15445 {
15446   tree try_block;
15447
15448   cp_parser_require_keyword (parser, RID_TRY, "`try'");
15449   try_block = begin_try_block ();
15450   cp_parser_compound_statement (parser, NULL, true);
15451   finish_try_block (try_block);
15452   cp_parser_handler_seq (parser);
15453   finish_handler_sequence (try_block);
15454
15455   return try_block;
15456 }
15457
15458 /* Parse a function-try-block.
15459
15460    function-try-block:
15461      try ctor-initializer [opt] function-body handler-seq  */
15462
15463 static bool
15464 cp_parser_function_try_block (cp_parser* parser)
15465 {
15466   tree compound_stmt;
15467   tree try_block;
15468   bool ctor_initializer_p;
15469
15470   /* Look for the `try' keyword.  */
15471   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15472     return false;
15473   /* Let the rest of the front end know where we are.  */
15474   try_block = begin_function_try_block (&compound_stmt);
15475   /* Parse the function-body.  */
15476   ctor_initializer_p
15477     = cp_parser_ctor_initializer_opt_and_function_body (parser);
15478   /* We're done with the `try' part.  */
15479   finish_function_try_block (try_block);
15480   /* Parse the handlers.  */
15481   cp_parser_handler_seq (parser);
15482   /* We're done with the handlers.  */
15483   finish_function_handler_sequence (try_block, compound_stmt);
15484
15485   return ctor_initializer_p;
15486 }
15487
15488 /* Parse a handler-seq.
15489
15490    handler-seq:
15491      handler handler-seq [opt]  */
15492
15493 static void
15494 cp_parser_handler_seq (cp_parser* parser)
15495 {
15496   while (true)
15497     {
15498       cp_token *token;
15499
15500       /* Parse the handler.  */
15501       cp_parser_handler (parser);
15502       /* Peek at the next token.  */
15503       token = cp_lexer_peek_token (parser->lexer);
15504       /* If it's not `catch' then there are no more handlers.  */
15505       if (!cp_parser_is_keyword (token, RID_CATCH))
15506         break;
15507     }
15508 }
15509
15510 /* Parse a handler.
15511
15512    handler:
15513      catch ( exception-declaration ) compound-statement  */
15514
15515 static void
15516 cp_parser_handler (cp_parser* parser)
15517 {
15518   tree handler;
15519   tree declaration;
15520
15521   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15522   handler = begin_handler ();
15523   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15524   declaration = cp_parser_exception_declaration (parser);
15525   finish_handler_parms (declaration, handler);
15526   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15527   cp_parser_compound_statement (parser, NULL, false);
15528   finish_handler (handler);
15529 }
15530
15531 /* Parse an exception-declaration.
15532
15533    exception-declaration:
15534      type-specifier-seq declarator
15535      type-specifier-seq abstract-declarator
15536      type-specifier-seq
15537      ...
15538
15539    Returns a VAR_DECL for the declaration, or NULL_TREE if the
15540    ellipsis variant is used.  */
15541
15542 static tree
15543 cp_parser_exception_declaration (cp_parser* parser)
15544 {
15545   cp_decl_specifier_seq type_specifiers;
15546   cp_declarator *declarator;
15547   const char *saved_message;
15548
15549   /* If it's an ellipsis, it's easy to handle.  */
15550   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15551     {
15552       /* Consume the `...' token.  */
15553       cp_lexer_consume_token (parser->lexer);
15554       return NULL_TREE;
15555     }
15556
15557   /* Types may not be defined in exception-declarations.  */
15558   saved_message = parser->type_definition_forbidden_message;
15559   parser->type_definition_forbidden_message
15560     = "types may not be defined in exception-declarations";
15561
15562   /* Parse the type-specifier-seq.  */
15563   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15564                                 &type_specifiers);
15565   /* If it's a `)', then there is no declarator.  */
15566   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15567     declarator = NULL;
15568   else
15569     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15570                                        /*ctor_dtor_or_conv_p=*/NULL,
15571                                        /*parenthesized_p=*/NULL,
15572                                        /*member_p=*/false);
15573
15574   /* Restore the saved message.  */
15575   parser->type_definition_forbidden_message = saved_message;
15576
15577   if (!type_specifiers.any_specifiers_p)
15578     return error_mark_node;
15579
15580   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15581 }
15582
15583 /* Parse a throw-expression.
15584
15585    throw-expression:
15586      throw assignment-expression [opt]
15587
15588    Returns a THROW_EXPR representing the throw-expression.  */
15589
15590 static tree
15591 cp_parser_throw_expression (cp_parser* parser)
15592 {
15593   tree expression;
15594   cp_token* token;
15595
15596   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15597   token = cp_lexer_peek_token (parser->lexer);
15598   /* Figure out whether or not there is an assignment-expression
15599      following the "throw" keyword.  */
15600   if (token->type == CPP_COMMA
15601       || token->type == CPP_SEMICOLON
15602       || token->type == CPP_CLOSE_PAREN
15603       || token->type == CPP_CLOSE_SQUARE
15604       || token->type == CPP_CLOSE_BRACE
15605       || token->type == CPP_COLON)
15606     expression = NULL_TREE;
15607   else
15608     expression = cp_parser_assignment_expression (parser,
15609                                                   /*cast_p=*/false);
15610
15611   return build_throw (expression);
15612 }
15613
15614 /* GNU Extensions */
15615
15616 /* Parse an (optional) asm-specification.
15617
15618    asm-specification:
15619      asm ( string-literal )
15620
15621    If the asm-specification is present, returns a STRING_CST
15622    corresponding to the string-literal.  Otherwise, returns
15623    NULL_TREE.  */
15624
15625 static tree
15626 cp_parser_asm_specification_opt (cp_parser* parser)
15627 {
15628   cp_token *token;
15629   tree asm_specification;
15630
15631   /* Peek at the next token.  */
15632   token = cp_lexer_peek_token (parser->lexer);
15633   /* If the next token isn't the `asm' keyword, then there's no
15634      asm-specification.  */
15635   if (!cp_parser_is_keyword (token, RID_ASM))
15636     return NULL_TREE;
15637
15638   /* Consume the `asm' token.  */
15639   cp_lexer_consume_token (parser->lexer);
15640   /* Look for the `('.  */
15641   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15642
15643   /* Look for the string-literal.  */
15644   asm_specification = cp_parser_string_literal (parser, false, false);
15645
15646   /* Look for the `)'.  */
15647   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15648
15649   return asm_specification;
15650 }
15651
15652 /* Parse an asm-operand-list.
15653
15654    asm-operand-list:
15655      asm-operand
15656      asm-operand-list , asm-operand
15657
15658    asm-operand:
15659      string-literal ( expression )
15660      [ string-literal ] string-literal ( expression )
15661
15662    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
15663    each node is the expression.  The TREE_PURPOSE is itself a
15664    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15665    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15666    is a STRING_CST for the string literal before the parenthesis. Returns
15667    ERROR_MARK_NODE if any of the operands are invalid.  */
15668
15669 static tree
15670 cp_parser_asm_operand_list (cp_parser* parser)
15671 {
15672   tree asm_operands = NULL_TREE;
15673   bool invalid_operands = false;
15674
15675   while (true)
15676     {
15677       tree string_literal;
15678       tree expression;
15679       tree name;
15680
15681       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15682         {
15683           /* Consume the `[' token.  */
15684           cp_lexer_consume_token (parser->lexer);
15685           /* Read the operand name.  */
15686           name = cp_parser_identifier (parser);
15687           if (name != error_mark_node)
15688             name = build_string (IDENTIFIER_LENGTH (name),
15689                                  IDENTIFIER_POINTER (name));
15690           /* Look for the closing `]'.  */
15691           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15692         }
15693       else
15694         name = NULL_TREE;
15695       /* Look for the string-literal.  */
15696       string_literal = cp_parser_string_literal (parser, false, false);
15697
15698       /* Look for the `('.  */
15699       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15700       /* Parse the expression.  */
15701       expression = cp_parser_expression (parser, /*cast_p=*/false);
15702       /* Look for the `)'.  */
15703       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15704
15705       if (name == error_mark_node 
15706           || string_literal == error_mark_node 
15707           || expression == error_mark_node)
15708         invalid_operands = true;
15709
15710       /* Add this operand to the list.  */
15711       asm_operands = tree_cons (build_tree_list (name, string_literal),
15712                                 expression,
15713                                 asm_operands);
15714       /* If the next token is not a `,', there are no more
15715          operands.  */
15716       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15717         break;
15718       /* Consume the `,'.  */
15719       cp_lexer_consume_token (parser->lexer);
15720     }
15721
15722   return invalid_operands ? error_mark_node : nreverse (asm_operands);
15723 }
15724
15725 /* Parse an asm-clobber-list.
15726
15727    asm-clobber-list:
15728      string-literal
15729      asm-clobber-list , string-literal
15730
15731    Returns a TREE_LIST, indicating the clobbers in the order that they
15732    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
15733
15734 static tree
15735 cp_parser_asm_clobber_list (cp_parser* parser)
15736 {
15737   tree clobbers = NULL_TREE;
15738
15739   while (true)
15740     {
15741       tree string_literal;
15742
15743       /* Look for the string literal.  */
15744       string_literal = cp_parser_string_literal (parser, false, false);
15745       /* Add it to the list.  */
15746       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15747       /* If the next token is not a `,', then the list is
15748          complete.  */
15749       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15750         break;
15751       /* Consume the `,' token.  */
15752       cp_lexer_consume_token (parser->lexer);
15753     }
15754
15755   return clobbers;
15756 }
15757
15758 /* Parse an (optional) series of attributes.
15759
15760    attributes:
15761      attributes attribute
15762
15763    attribute:
15764      __attribute__ (( attribute-list [opt] ))
15765
15766    The return value is as for cp_parser_attribute_list.  */
15767
15768 static tree
15769 cp_parser_attributes_opt (cp_parser* parser)
15770 {
15771   tree attributes = NULL_TREE;
15772
15773   while (true)
15774     {
15775       cp_token *token;
15776       tree attribute_list;
15777
15778       /* Peek at the next token.  */
15779       token = cp_lexer_peek_token (parser->lexer);
15780       /* If it's not `__attribute__', then we're done.  */
15781       if (token->keyword != RID_ATTRIBUTE)
15782         break;
15783
15784       /* Consume the `__attribute__' keyword.  */
15785       cp_lexer_consume_token (parser->lexer);
15786       /* Look for the two `(' tokens.  */
15787       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15788       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15789
15790       /* Peek at the next token.  */
15791       token = cp_lexer_peek_token (parser->lexer);
15792       if (token->type != CPP_CLOSE_PAREN)
15793         /* Parse the attribute-list.  */
15794         attribute_list = cp_parser_attribute_list (parser);
15795       else
15796         /* If the next token is a `)', then there is no attribute
15797            list.  */
15798         attribute_list = NULL;
15799
15800       /* Look for the two `)' tokens.  */
15801       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15802       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15803
15804       /* Add these new attributes to the list.  */
15805       attributes = chainon (attributes, attribute_list);
15806     }
15807
15808   return attributes;
15809 }
15810
15811 /* Parse an attribute-list.
15812
15813    attribute-list:
15814      attribute
15815      attribute-list , attribute
15816
15817    attribute:
15818      identifier
15819      identifier ( identifier )
15820      identifier ( identifier , expression-list )
15821      identifier ( expression-list )
15822
15823    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
15824    to an attribute.  The TREE_PURPOSE of each node is the identifier
15825    indicating which attribute is in use.  The TREE_VALUE represents
15826    the arguments, if any.  */
15827
15828 static tree
15829 cp_parser_attribute_list (cp_parser* parser)
15830 {
15831   tree attribute_list = NULL_TREE;
15832   bool save_translate_strings_p = parser->translate_strings_p;
15833
15834   parser->translate_strings_p = false;
15835   while (true)
15836     {
15837       cp_token *token;
15838       tree identifier;
15839       tree attribute;
15840
15841       /* Look for the identifier.  We also allow keywords here; for
15842          example `__attribute__ ((const))' is legal.  */
15843       token = cp_lexer_peek_token (parser->lexer);
15844       if (token->type == CPP_NAME
15845           || token->type == CPP_KEYWORD)
15846         {
15847           tree arguments = NULL_TREE;
15848
15849           /* Consume the token.  */
15850           token = cp_lexer_consume_token (parser->lexer);
15851
15852           /* Save away the identifier that indicates which attribute
15853              this is.  */
15854           identifier = token->u.value;
15855           attribute = build_tree_list (identifier, NULL_TREE);
15856
15857           /* Peek at the next token.  */
15858           token = cp_lexer_peek_token (parser->lexer);
15859           /* If it's an `(', then parse the attribute arguments.  */
15860           if (token->type == CPP_OPEN_PAREN)
15861             {
15862               arguments = cp_parser_parenthesized_expression_list
15863                           (parser, true, /*cast_p=*/false,
15864                            /*allow_expansion_p=*/false,
15865                            /*non_constant_p=*/NULL);
15866               /* Save the arguments away.  */
15867               TREE_VALUE (attribute) = arguments;
15868             }
15869
15870           if (arguments != error_mark_node)
15871             {
15872               /* Add this attribute to the list.  */
15873               TREE_CHAIN (attribute) = attribute_list;
15874               attribute_list = attribute;
15875             }
15876
15877           token = cp_lexer_peek_token (parser->lexer);
15878         }
15879       /* Now, look for more attributes.  If the next token isn't a
15880          `,', we're done.  */
15881       if (token->type != CPP_COMMA)
15882         break;
15883
15884       /* Consume the comma and keep going.  */
15885       cp_lexer_consume_token (parser->lexer);
15886     }
15887   parser->translate_strings_p = save_translate_strings_p;
15888
15889   /* We built up the list in reverse order.  */
15890   return nreverse (attribute_list);
15891 }
15892
15893 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
15894    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
15895    current value of the PEDANTIC flag, regardless of whether or not
15896    the `__extension__' keyword is present.  The caller is responsible
15897    for restoring the value of the PEDANTIC flag.  */
15898
15899 static bool
15900 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15901 {
15902   /* Save the old value of the PEDANTIC flag.  */
15903   *saved_pedantic = pedantic;
15904
15905   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15906     {
15907       /* Consume the `__extension__' token.  */
15908       cp_lexer_consume_token (parser->lexer);
15909       /* We're not being pedantic while the `__extension__' keyword is
15910          in effect.  */
15911       pedantic = 0;
15912
15913       return true;
15914     }
15915
15916   return false;
15917 }
15918
15919 /* Parse a label declaration.
15920
15921    label-declaration:
15922      __label__ label-declarator-seq ;
15923
15924    label-declarator-seq:
15925      identifier , label-declarator-seq
15926      identifier  */
15927
15928 static void
15929 cp_parser_label_declaration (cp_parser* parser)
15930 {
15931   /* Look for the `__label__' keyword.  */
15932   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15933
15934   while (true)
15935     {
15936       tree identifier;
15937
15938       /* Look for an identifier.  */
15939       identifier = cp_parser_identifier (parser);
15940       /* If we failed, stop.  */
15941       if (identifier == error_mark_node)
15942         break;
15943       /* Declare it as a label.  */
15944       finish_label_decl (identifier);
15945       /* If the next token is a `;', stop.  */
15946       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15947         break;
15948       /* Look for the `,' separating the label declarations.  */
15949       cp_parser_require (parser, CPP_COMMA, "`,'");
15950     }
15951
15952   /* Look for the final `;'.  */
15953   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15954 }
15955
15956 /* Support Functions */
15957
15958 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15959    NAME should have one of the representations used for an
15960    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15961    is returned.  If PARSER->SCOPE is a dependent type, then a
15962    SCOPE_REF is returned.
15963
15964    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15965    returned; the name was already resolved when the TEMPLATE_ID_EXPR
15966    was formed.  Abstractly, such entities should not be passed to this
15967    function, because they do not need to be looked up, but it is
15968    simpler to check for this special case here, rather than at the
15969    call-sites.
15970
15971    In cases not explicitly covered above, this function returns a
15972    DECL, OVERLOAD, or baselink representing the result of the lookup.
15973    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15974    is returned.
15975
15976    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15977    (e.g., "struct") that was used.  In that case bindings that do not
15978    refer to types are ignored.
15979
15980    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15981    ignored.
15982
15983    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15984    are ignored.
15985
15986    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15987    types.
15988
15989    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15990    TREE_LIST of candidates if name-lookup results in an ambiguity, and
15991    NULL_TREE otherwise.  */
15992
15993 static tree
15994 cp_parser_lookup_name (cp_parser *parser, tree name,
15995                        enum tag_types tag_type,
15996                        bool is_template,
15997                        bool is_namespace,
15998                        bool check_dependency,
15999                        tree *ambiguous_decls)
16000 {
16001   int flags = 0;
16002   tree decl;
16003   tree object_type = parser->context->object_type;
16004
16005   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16006     flags |= LOOKUP_COMPLAIN;
16007
16008   /* Assume that the lookup will be unambiguous.  */
16009   if (ambiguous_decls)
16010     *ambiguous_decls = NULL_TREE;
16011
16012   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16013      no longer valid.  Note that if we are parsing tentatively, and
16014      the parse fails, OBJECT_TYPE will be automatically restored.  */
16015   parser->context->object_type = NULL_TREE;
16016
16017   if (name == error_mark_node)
16018     return error_mark_node;
16019
16020   /* A template-id has already been resolved; there is no lookup to
16021      do.  */
16022   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16023     return name;
16024   if (BASELINK_P (name))
16025     {
16026       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16027                   == TEMPLATE_ID_EXPR);
16028       return name;
16029     }
16030
16031   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
16032      it should already have been checked to make sure that the name
16033      used matches the type being destroyed.  */
16034   if (TREE_CODE (name) == BIT_NOT_EXPR)
16035     {
16036       tree type;
16037
16038       /* Figure out to which type this destructor applies.  */
16039       if (parser->scope)
16040         type = parser->scope;
16041       else if (object_type)
16042         type = object_type;
16043       else
16044         type = current_class_type;
16045       /* If that's not a class type, there is no destructor.  */
16046       if (!type || !CLASS_TYPE_P (type))
16047         return error_mark_node;
16048       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16049         lazily_declare_fn (sfk_destructor, type);
16050       if (!CLASSTYPE_DESTRUCTORS (type))
16051           return error_mark_node;
16052       /* If it was a class type, return the destructor.  */
16053       return CLASSTYPE_DESTRUCTORS (type);
16054     }
16055
16056   /* By this point, the NAME should be an ordinary identifier.  If
16057      the id-expression was a qualified name, the qualifying scope is
16058      stored in PARSER->SCOPE at this point.  */
16059   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16060
16061   /* Perform the lookup.  */
16062   if (parser->scope)
16063     {
16064       bool dependent_p;
16065
16066       if (parser->scope == error_mark_node)
16067         return error_mark_node;
16068
16069       /* If the SCOPE is dependent, the lookup must be deferred until
16070          the template is instantiated -- unless we are explicitly
16071          looking up names in uninstantiated templates.  Even then, we
16072          cannot look up the name if the scope is not a class type; it
16073          might, for example, be a template type parameter.  */
16074       dependent_p = (TYPE_P (parser->scope)
16075                      && !(parser->in_declarator_p
16076                           && currently_open_class (parser->scope))
16077                      && dependent_type_p (parser->scope));
16078       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16079            && dependent_p)
16080         {
16081           if (tag_type)
16082             {
16083               tree type;
16084
16085               /* The resolution to Core Issue 180 says that `struct
16086                  A::B' should be considered a type-name, even if `A'
16087                  is dependent.  */
16088               type = make_typename_type (parser->scope, name, tag_type,
16089                                          /*complain=*/tf_error);
16090               decl = TYPE_NAME (type);
16091             }
16092           else if (is_template
16093                    && (cp_parser_next_token_ends_template_argument_p (parser)
16094                        || cp_lexer_next_token_is (parser->lexer,
16095                                                   CPP_CLOSE_PAREN)))
16096             decl = make_unbound_class_template (parser->scope,
16097                                                 name, NULL_TREE,
16098                                                 /*complain=*/tf_error);
16099           else
16100             decl = build_qualified_name (/*type=*/NULL_TREE,
16101                                          parser->scope, name,
16102                                          is_template);
16103         }
16104       else
16105         {
16106           tree pushed_scope = NULL_TREE;
16107
16108           /* If PARSER->SCOPE is a dependent type, then it must be a
16109              class type, and we must not be checking dependencies;
16110              otherwise, we would have processed this lookup above.  So
16111              that PARSER->SCOPE is not considered a dependent base by
16112              lookup_member, we must enter the scope here.  */
16113           if (dependent_p)
16114             pushed_scope = push_scope (parser->scope);
16115           /* If the PARSER->SCOPE is a template specialization, it
16116              may be instantiated during name lookup.  In that case,
16117              errors may be issued.  Even if we rollback the current
16118              tentative parse, those errors are valid.  */
16119           decl = lookup_qualified_name (parser->scope, name,
16120                                         tag_type != none_type,
16121                                         /*complain=*/true);
16122           if (pushed_scope)
16123             pop_scope (pushed_scope);
16124         }
16125       parser->qualifying_scope = parser->scope;
16126       parser->object_scope = NULL_TREE;
16127     }
16128   else if (object_type)
16129     {
16130       tree object_decl = NULL_TREE;
16131       /* Look up the name in the scope of the OBJECT_TYPE, unless the
16132          OBJECT_TYPE is not a class.  */
16133       if (CLASS_TYPE_P (object_type))
16134         /* If the OBJECT_TYPE is a template specialization, it may
16135            be instantiated during name lookup.  In that case, errors
16136            may be issued.  Even if we rollback the current tentative
16137            parse, those errors are valid.  */
16138         object_decl = lookup_member (object_type,
16139                                      name,
16140                                      /*protect=*/0,
16141                                      tag_type != none_type);
16142       /* Look it up in the enclosing context, too.  */
16143       decl = lookup_name_real (name, tag_type != none_type,
16144                                /*nonclass=*/0,
16145                                /*block_p=*/true, is_namespace, flags);
16146       parser->object_scope = object_type;
16147       parser->qualifying_scope = NULL_TREE;
16148       if (object_decl)
16149         decl = object_decl;
16150     }
16151   else
16152     {
16153       decl = lookup_name_real (name, tag_type != none_type,
16154                                /*nonclass=*/0,
16155                                /*block_p=*/true, is_namespace, flags);
16156       parser->qualifying_scope = NULL_TREE;
16157       parser->object_scope = NULL_TREE;
16158     }
16159
16160   /* If the lookup failed, let our caller know.  */
16161   if (!decl || decl == error_mark_node)
16162     return error_mark_node;
16163
16164   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
16165   if (TREE_CODE (decl) == TREE_LIST)
16166     {
16167       if (ambiguous_decls)
16168         *ambiguous_decls = decl;
16169       /* The error message we have to print is too complicated for
16170          cp_parser_error, so we incorporate its actions directly.  */
16171       if (!cp_parser_simulate_error (parser))
16172         {
16173           error ("reference to %qD is ambiguous", name);
16174           print_candidates (decl);
16175         }
16176       return error_mark_node;
16177     }
16178
16179   gcc_assert (DECL_P (decl)
16180               || TREE_CODE (decl) == OVERLOAD
16181               || TREE_CODE (decl) == SCOPE_REF
16182               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16183               || BASELINK_P (decl));
16184
16185   /* If we have resolved the name of a member declaration, check to
16186      see if the declaration is accessible.  When the name resolves to
16187      set of overloaded functions, accessibility is checked when
16188      overload resolution is done.
16189
16190      During an explicit instantiation, access is not checked at all,
16191      as per [temp.explicit].  */
16192   if (DECL_P (decl))
16193     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16194
16195   return decl;
16196 }
16197
16198 /* Like cp_parser_lookup_name, but for use in the typical case where
16199    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16200    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
16201
16202 static tree
16203 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
16204 {
16205   return cp_parser_lookup_name (parser, name,
16206                                 none_type,
16207                                 /*is_template=*/false,
16208                                 /*is_namespace=*/false,
16209                                 /*check_dependency=*/true,
16210                                 /*ambiguous_decls=*/NULL);
16211 }
16212
16213 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16214    the current context, return the TYPE_DECL.  If TAG_NAME_P is
16215    true, the DECL indicates the class being defined in a class-head,
16216    or declared in an elaborated-type-specifier.
16217
16218    Otherwise, return DECL.  */
16219
16220 static tree
16221 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16222 {
16223   /* If the TEMPLATE_DECL is being declared as part of a class-head,
16224      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16225
16226        struct A {
16227          template <typename T> struct B;
16228        };
16229
16230        template <typename T> struct A::B {};
16231
16232      Similarly, in an elaborated-type-specifier:
16233
16234        namespace N { struct X{}; }
16235
16236        struct A {
16237          template <typename T> friend struct N::X;
16238        };
16239
16240      However, if the DECL refers to a class type, and we are in
16241      the scope of the class, then the name lookup automatically
16242      finds the TYPE_DECL created by build_self_reference rather
16243      than a TEMPLATE_DECL.  For example, in:
16244
16245        template <class T> struct S {
16246          S s;
16247        };
16248
16249      there is no need to handle such case.  */
16250
16251   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16252     return DECL_TEMPLATE_RESULT (decl);
16253
16254   return decl;
16255 }
16256
16257 /* If too many, or too few, template-parameter lists apply to the
16258    declarator, issue an error message.  Returns TRUE if all went well,
16259    and FALSE otherwise.  */
16260
16261 static bool
16262 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16263                                                 cp_declarator *declarator)
16264 {
16265   unsigned num_templates;
16266
16267   /* We haven't seen any classes that involve template parameters yet.  */
16268   num_templates = 0;
16269
16270   switch (declarator->kind)
16271     {
16272     case cdk_id:
16273       if (declarator->u.id.qualifying_scope)
16274         {
16275           tree scope;
16276           tree member;
16277
16278           scope = declarator->u.id.qualifying_scope;
16279           member = declarator->u.id.unqualified_name;
16280
16281           while (scope && CLASS_TYPE_P (scope))
16282             {
16283               /* You're supposed to have one `template <...>'
16284                  for every template class, but you don't need one
16285                  for a full specialization.  For example:
16286
16287                  template <class T> struct S{};
16288                  template <> struct S<int> { void f(); };
16289                  void S<int>::f () {}
16290
16291                  is correct; there shouldn't be a `template <>' for
16292                  the definition of `S<int>::f'.  */
16293               if (!CLASSTYPE_TEMPLATE_INFO (scope))
16294                 /* If SCOPE does not have template information of any
16295                    kind, then it is not a template, nor is it nested
16296                    within a template.  */
16297                 break;
16298               if (explicit_class_specialization_p (scope))
16299                 break;
16300               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16301                 ++num_templates;
16302
16303               scope = TYPE_CONTEXT (scope);
16304             }
16305         }
16306       else if (TREE_CODE (declarator->u.id.unqualified_name)
16307                == TEMPLATE_ID_EXPR)
16308         /* If the DECLARATOR has the form `X<y>' then it uses one
16309            additional level of template parameters.  */
16310         ++num_templates;
16311
16312       return cp_parser_check_template_parameters (parser,
16313                                                   num_templates);
16314
16315     case cdk_function:
16316     case cdk_array:
16317     case cdk_pointer:
16318     case cdk_reference:
16319     case cdk_ptrmem:
16320       return (cp_parser_check_declarator_template_parameters
16321               (parser, declarator->declarator));
16322
16323     case cdk_error:
16324       return true;
16325
16326     default:
16327       gcc_unreachable ();
16328     }
16329   return false;
16330 }
16331
16332 /* NUM_TEMPLATES were used in the current declaration.  If that is
16333    invalid, return FALSE and issue an error messages.  Otherwise,
16334    return TRUE.  */
16335
16336 static bool
16337 cp_parser_check_template_parameters (cp_parser* parser,
16338                                      unsigned num_templates)
16339 {
16340   /* If there are more template classes than parameter lists, we have
16341      something like:
16342
16343        template <class T> void S<T>::R<T>::f ();  */
16344   if (parser->num_template_parameter_lists < num_templates)
16345     {
16346       error ("too few template-parameter-lists");
16347       return false;
16348     }
16349   /* If there are the same number of template classes and parameter
16350      lists, that's OK.  */
16351   if (parser->num_template_parameter_lists == num_templates)
16352     return true;
16353   /* If there are more, but only one more, then we are referring to a
16354      member template.  That's OK too.  */
16355   if (parser->num_template_parameter_lists == num_templates + 1)
16356       return true;
16357   /* Otherwise, there are too many template parameter lists.  We have
16358      something like:
16359
16360      template <class T> template <class U> void S::f();  */
16361   error ("too many template-parameter-lists");
16362   return false;
16363 }
16364
16365 /* Parse an optional `::' token indicating that the following name is
16366    from the global namespace.  If so, PARSER->SCOPE is set to the
16367    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16368    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16369    Returns the new value of PARSER->SCOPE, if the `::' token is
16370    present, and NULL_TREE otherwise.  */
16371
16372 static tree
16373 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16374 {
16375   cp_token *token;
16376
16377   /* Peek at the next token.  */
16378   token = cp_lexer_peek_token (parser->lexer);
16379   /* If we're looking at a `::' token then we're starting from the
16380      global namespace, not our current location.  */
16381   if (token->type == CPP_SCOPE)
16382     {
16383       /* Consume the `::' token.  */
16384       cp_lexer_consume_token (parser->lexer);
16385       /* Set the SCOPE so that we know where to start the lookup.  */
16386       parser->scope = global_namespace;
16387       parser->qualifying_scope = global_namespace;
16388       parser->object_scope = NULL_TREE;
16389
16390       return parser->scope;
16391     }
16392   else if (!current_scope_valid_p)
16393     {
16394       parser->scope = NULL_TREE;
16395       parser->qualifying_scope = NULL_TREE;
16396       parser->object_scope = NULL_TREE;
16397     }
16398
16399   return NULL_TREE;
16400 }
16401
16402 /* Returns TRUE if the upcoming token sequence is the start of a
16403    constructor declarator.  If FRIEND_P is true, the declarator is
16404    preceded by the `friend' specifier.  */
16405
16406 static bool
16407 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16408 {
16409   bool constructor_p;
16410   tree type_decl = NULL_TREE;
16411   bool nested_name_p;
16412   cp_token *next_token;
16413
16414   /* The common case is that this is not a constructor declarator, so
16415      try to avoid doing lots of work if at all possible.  It's not
16416      valid declare a constructor at function scope.  */
16417   if (parser->in_function_body)
16418     return false;
16419   /* And only certain tokens can begin a constructor declarator.  */
16420   next_token = cp_lexer_peek_token (parser->lexer);
16421   if (next_token->type != CPP_NAME
16422       && next_token->type != CPP_SCOPE
16423       && next_token->type != CPP_NESTED_NAME_SPECIFIER
16424       && next_token->type != CPP_TEMPLATE_ID)
16425     return false;
16426
16427   /* Parse tentatively; we are going to roll back all of the tokens
16428      consumed here.  */
16429   cp_parser_parse_tentatively (parser);
16430   /* Assume that we are looking at a constructor declarator.  */
16431   constructor_p = true;
16432
16433   /* Look for the optional `::' operator.  */
16434   cp_parser_global_scope_opt (parser,
16435                               /*current_scope_valid_p=*/false);
16436   /* Look for the nested-name-specifier.  */
16437   nested_name_p
16438     = (cp_parser_nested_name_specifier_opt (parser,
16439                                             /*typename_keyword_p=*/false,
16440                                             /*check_dependency_p=*/false,
16441                                             /*type_p=*/false,
16442                                             /*is_declaration=*/false)
16443        != NULL_TREE);
16444   /* Outside of a class-specifier, there must be a
16445      nested-name-specifier.  */
16446   if (!nested_name_p &&
16447       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16448        || friend_p))
16449     constructor_p = false;
16450   /* If we still think that this might be a constructor-declarator,
16451      look for a class-name.  */
16452   if (constructor_p)
16453     {
16454       /* If we have:
16455
16456            template <typename T> struct S { S(); };
16457            template <typename T> S<T>::S ();
16458
16459          we must recognize that the nested `S' names a class.
16460          Similarly, for:
16461
16462            template <typename T> S<T>::S<T> ();
16463
16464          we must recognize that the nested `S' names a template.  */
16465       type_decl = cp_parser_class_name (parser,
16466                                         /*typename_keyword_p=*/false,
16467                                         /*template_keyword_p=*/false,
16468                                         none_type,
16469                                         /*check_dependency_p=*/false,
16470                                         /*class_head_p=*/false,
16471                                         /*is_declaration=*/false);
16472       /* If there was no class-name, then this is not a constructor.  */
16473       constructor_p = !cp_parser_error_occurred (parser);
16474     }
16475
16476   /* If we're still considering a constructor, we have to see a `(',
16477      to begin the parameter-declaration-clause, followed by either a
16478      `)', an `...', or a decl-specifier.  We need to check for a
16479      type-specifier to avoid being fooled into thinking that:
16480
16481        S::S (f) (int);
16482
16483      is a constructor.  (It is actually a function named `f' that
16484      takes one parameter (of type `int') and returns a value of type
16485      `S::S'.  */
16486   if (constructor_p
16487       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16488     {
16489       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16490           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16491           /* A parameter declaration begins with a decl-specifier,
16492              which is either the "attribute" keyword, a storage class
16493              specifier, or (usually) a type-specifier.  */
16494           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16495         {
16496           tree type;
16497           tree pushed_scope = NULL_TREE;
16498           unsigned saved_num_template_parameter_lists;
16499
16500           /* Names appearing in the type-specifier should be looked up
16501              in the scope of the class.  */
16502           if (current_class_type)
16503             type = NULL_TREE;
16504           else
16505             {
16506               type = TREE_TYPE (type_decl);
16507               if (TREE_CODE (type) == TYPENAME_TYPE)
16508                 {
16509                   type = resolve_typename_type (type,
16510                                                 /*only_current_p=*/false);
16511                   if (TREE_CODE (type) == TYPENAME_TYPE)
16512                     {
16513                       cp_parser_abort_tentative_parse (parser);
16514                       return false;
16515                     }
16516                 }
16517               pushed_scope = push_scope (type);
16518             }
16519
16520           /* Inside the constructor parameter list, surrounding
16521              template-parameter-lists do not apply.  */
16522           saved_num_template_parameter_lists
16523             = parser->num_template_parameter_lists;
16524           parser->num_template_parameter_lists = 0;
16525
16526           /* Look for the type-specifier.  */
16527           cp_parser_type_specifier (parser,
16528                                     CP_PARSER_FLAGS_NONE,
16529                                     /*decl_specs=*/NULL,
16530                                     /*is_declarator=*/true,
16531                                     /*declares_class_or_enum=*/NULL,
16532                                     /*is_cv_qualifier=*/NULL);
16533
16534           parser->num_template_parameter_lists
16535             = saved_num_template_parameter_lists;
16536
16537           /* Leave the scope of the class.  */
16538           if (pushed_scope)
16539             pop_scope (pushed_scope);
16540
16541           constructor_p = !cp_parser_error_occurred (parser);
16542         }
16543     }
16544   else
16545     constructor_p = false;
16546   /* We did not really want to consume any tokens.  */
16547   cp_parser_abort_tentative_parse (parser);
16548
16549   return constructor_p;
16550 }
16551
16552 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16553    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
16554    they must be performed once we are in the scope of the function.
16555
16556    Returns the function defined.  */
16557
16558 static tree
16559 cp_parser_function_definition_from_specifiers_and_declarator
16560   (cp_parser* parser,
16561    cp_decl_specifier_seq *decl_specifiers,
16562    tree attributes,
16563    const cp_declarator *declarator)
16564 {
16565   tree fn;
16566   bool success_p;
16567
16568   /* Begin the function-definition.  */
16569   success_p = start_function (decl_specifiers, declarator, attributes);
16570
16571   /* The things we're about to see are not directly qualified by any
16572      template headers we've seen thus far.  */
16573   reset_specialization ();
16574
16575   /* If there were names looked up in the decl-specifier-seq that we
16576      did not check, check them now.  We must wait until we are in the
16577      scope of the function to perform the checks, since the function
16578      might be a friend.  */
16579   perform_deferred_access_checks ();
16580
16581   if (!success_p)
16582     {
16583       /* Skip the entire function.  */
16584       cp_parser_skip_to_end_of_block_or_statement (parser);
16585       fn = error_mark_node;
16586     }
16587   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16588     {
16589       /* Seen already, skip it.  An error message has already been output.  */
16590       cp_parser_skip_to_end_of_block_or_statement (parser);
16591       fn = current_function_decl;
16592       current_function_decl = NULL_TREE;
16593       /* If this is a function from a class, pop the nested class.  */
16594       if (current_class_name)
16595         pop_nested_class ();
16596     }
16597   else
16598     fn = cp_parser_function_definition_after_declarator (parser,
16599                                                          /*inline_p=*/false);
16600
16601   return fn;
16602 }
16603
16604 /* Parse the part of a function-definition that follows the
16605    declarator.  INLINE_P is TRUE iff this function is an inline
16606    function defined with a class-specifier.
16607
16608    Returns the function defined.  */
16609
16610 static tree
16611 cp_parser_function_definition_after_declarator (cp_parser* parser,
16612                                                 bool inline_p)
16613 {
16614   tree fn;
16615   bool ctor_initializer_p = false;
16616   bool saved_in_unbraced_linkage_specification_p;
16617   bool saved_in_function_body;
16618   unsigned saved_num_template_parameter_lists;
16619
16620   saved_in_function_body = parser->in_function_body;
16621   parser->in_function_body = true;
16622   /* If the next token is `return', then the code may be trying to
16623      make use of the "named return value" extension that G++ used to
16624      support.  */
16625   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16626     {
16627       /* Consume the `return' keyword.  */
16628       cp_lexer_consume_token (parser->lexer);
16629       /* Look for the identifier that indicates what value is to be
16630          returned.  */
16631       cp_parser_identifier (parser);
16632       /* Issue an error message.  */
16633       error ("named return values are no longer supported");
16634       /* Skip tokens until we reach the start of the function body.  */
16635       while (true)
16636         {
16637           cp_token *token = cp_lexer_peek_token (parser->lexer);
16638           if (token->type == CPP_OPEN_BRACE
16639               || token->type == CPP_EOF
16640               || token->type == CPP_PRAGMA_EOL)
16641             break;
16642           cp_lexer_consume_token (parser->lexer);
16643         }
16644     }
16645   /* The `extern' in `extern "C" void f () { ... }' does not apply to
16646      anything declared inside `f'.  */
16647   saved_in_unbraced_linkage_specification_p
16648     = parser->in_unbraced_linkage_specification_p;
16649   parser->in_unbraced_linkage_specification_p = false;
16650   /* Inside the function, surrounding template-parameter-lists do not
16651      apply.  */
16652   saved_num_template_parameter_lists
16653     = parser->num_template_parameter_lists;
16654   parser->num_template_parameter_lists = 0;
16655   /* If the next token is `try', then we are looking at a
16656      function-try-block.  */
16657   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16658     ctor_initializer_p = cp_parser_function_try_block (parser);
16659   /* A function-try-block includes the function-body, so we only do
16660      this next part if we're not processing a function-try-block.  */
16661   else
16662     ctor_initializer_p
16663       = cp_parser_ctor_initializer_opt_and_function_body (parser);
16664
16665   /* Finish the function.  */
16666   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16667                         (inline_p ? 2 : 0));
16668   /* Generate code for it, if necessary.  */
16669   expand_or_defer_fn (fn);
16670   /* Restore the saved values.  */
16671   parser->in_unbraced_linkage_specification_p
16672     = saved_in_unbraced_linkage_specification_p;
16673   parser->num_template_parameter_lists
16674     = saved_num_template_parameter_lists;
16675   parser->in_function_body = saved_in_function_body;
16676
16677   return fn;
16678 }
16679
16680 /* Parse a template-declaration, assuming that the `export' (and
16681    `extern') keywords, if present, has already been scanned.  MEMBER_P
16682    is as for cp_parser_template_declaration.  */
16683
16684 static void
16685 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16686 {
16687   tree decl = NULL_TREE;
16688   VEC (deferred_access_check,gc) *checks;
16689   tree parameter_list;
16690   bool friend_p = false;
16691   bool need_lang_pop;
16692
16693   /* Look for the `template' keyword.  */
16694   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16695     return;
16696
16697   /* And the `<'.  */
16698   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16699     return;
16700   if (at_class_scope_p () && current_function_decl)
16701     {
16702       /* 14.5.2.2 [temp.mem]
16703
16704          A local class shall not have member templates.  */
16705       error ("invalid declaration of member template in local class");
16706       cp_parser_skip_to_end_of_block_or_statement (parser);
16707       return;
16708     }
16709   /* [temp]
16710
16711      A template ... shall not have C linkage.  */
16712   if (current_lang_name == lang_name_c)
16713     {
16714       error ("template with C linkage");
16715       /* Give it C++ linkage to avoid confusing other parts of the
16716          front end.  */
16717       push_lang_context (lang_name_cplusplus);
16718       need_lang_pop = true;
16719     }
16720   else
16721     need_lang_pop = false;
16722
16723   /* We cannot perform access checks on the template parameter
16724      declarations until we know what is being declared, just as we
16725      cannot check the decl-specifier list.  */
16726   push_deferring_access_checks (dk_deferred);
16727
16728   /* If the next token is `>', then we have an invalid
16729      specialization.  Rather than complain about an invalid template
16730      parameter, issue an error message here.  */
16731   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16732     {
16733       cp_parser_error (parser, "invalid explicit specialization");
16734       begin_specialization ();
16735       parameter_list = NULL_TREE;
16736     }
16737   else
16738     /* Parse the template parameters.  */
16739     parameter_list = cp_parser_template_parameter_list (parser);
16740
16741   /* Get the deferred access checks from the parameter list.  These
16742      will be checked once we know what is being declared, as for a
16743      member template the checks must be performed in the scope of the
16744      class containing the member.  */
16745   checks = get_deferred_access_checks ();
16746
16747   /* Look for the `>'.  */
16748   cp_parser_skip_to_end_of_template_parameter_list (parser);
16749   /* We just processed one more parameter list.  */
16750   ++parser->num_template_parameter_lists;
16751   /* If the next token is `template', there are more template
16752      parameters.  */
16753   if (cp_lexer_next_token_is_keyword (parser->lexer,
16754                                       RID_TEMPLATE))
16755     cp_parser_template_declaration_after_export (parser, member_p);
16756   else
16757     {
16758       /* There are no access checks when parsing a template, as we do not
16759          know if a specialization will be a friend.  */
16760       push_deferring_access_checks (dk_no_check);
16761       decl = cp_parser_single_declaration (parser,
16762                                            checks,
16763                                            member_p,
16764                                            /*explicit_specialization_p=*/false,
16765                                            &friend_p);
16766       pop_deferring_access_checks ();
16767
16768       /* If this is a member template declaration, let the front
16769          end know.  */
16770       if (member_p && !friend_p && decl)
16771         {
16772           if (TREE_CODE (decl) == TYPE_DECL)
16773             cp_parser_check_access_in_redeclaration (decl);
16774
16775           decl = finish_member_template_decl (decl);
16776         }
16777       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16778         make_friend_class (current_class_type, TREE_TYPE (decl),
16779                            /*complain=*/true);
16780     }
16781   /* We are done with the current parameter list.  */
16782   --parser->num_template_parameter_lists;
16783
16784   pop_deferring_access_checks ();
16785
16786   /* Finish up.  */
16787   finish_template_decl (parameter_list);
16788
16789   /* Register member declarations.  */
16790   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16791     finish_member_declaration (decl);
16792   /* For the erroneous case of a template with C linkage, we pushed an
16793      implicit C++ linkage scope; exit that scope now.  */
16794   if (need_lang_pop)
16795     pop_lang_context ();
16796   /* If DECL is a function template, we must return to parse it later.
16797      (Even though there is no definition, there might be default
16798      arguments that need handling.)  */
16799   if (member_p && decl
16800       && (TREE_CODE (decl) == FUNCTION_DECL
16801           || DECL_FUNCTION_TEMPLATE_P (decl)))
16802     TREE_VALUE (parser->unparsed_functions_queues)
16803       = tree_cons (NULL_TREE, decl,
16804                    TREE_VALUE (parser->unparsed_functions_queues));
16805 }
16806
16807 /* Perform the deferred access checks from a template-parameter-list.
16808    CHECKS is a TREE_LIST of access checks, as returned by
16809    get_deferred_access_checks.  */
16810
16811 static void
16812 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16813 {
16814   ++processing_template_parmlist;
16815   perform_access_checks (checks);
16816   --processing_template_parmlist;
16817 }
16818
16819 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16820    `function-definition' sequence.  MEMBER_P is true, this declaration
16821    appears in a class scope.
16822
16823    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
16824    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
16825
16826 static tree
16827 cp_parser_single_declaration (cp_parser* parser,
16828                               VEC (deferred_access_check,gc)* checks,
16829                               bool member_p,
16830                               bool explicit_specialization_p,
16831                               bool* friend_p)
16832 {
16833   int declares_class_or_enum;
16834   tree decl = NULL_TREE;
16835   cp_decl_specifier_seq decl_specifiers;
16836   bool function_definition_p = false;
16837
16838   /* This function is only used when processing a template
16839      declaration.  */
16840   gcc_assert (innermost_scope_kind () == sk_template_parms
16841               || innermost_scope_kind () == sk_template_spec);
16842
16843   /* Defer access checks until we know what is being declared.  */
16844   push_deferring_access_checks (dk_deferred);
16845
16846   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16847      alternative.  */
16848   cp_parser_decl_specifier_seq (parser,
16849                                 CP_PARSER_FLAGS_OPTIONAL,
16850                                 &decl_specifiers,
16851                                 &declares_class_or_enum);
16852   if (friend_p)
16853     *friend_p = cp_parser_friend_p (&decl_specifiers);
16854
16855   /* There are no template typedefs.  */
16856   if (decl_specifiers.specs[(int) ds_typedef])
16857     {
16858       error ("template declaration of %qs", "typedef");
16859       decl = error_mark_node;
16860     }
16861
16862   /* Gather up the access checks that occurred the
16863      decl-specifier-seq.  */
16864   stop_deferring_access_checks ();
16865
16866   /* Check for the declaration of a template class.  */
16867   if (declares_class_or_enum)
16868     {
16869       if (cp_parser_declares_only_class_p (parser))
16870         {
16871           decl = shadow_tag (&decl_specifiers);
16872
16873           /* In this case:
16874
16875                struct C {
16876                  friend template <typename T> struct A<T>::B;
16877                };
16878
16879              A<T>::B will be represented by a TYPENAME_TYPE, and
16880              therefore not recognized by shadow_tag.  */
16881           if (friend_p && *friend_p
16882               && !decl
16883               && decl_specifiers.type
16884               && TYPE_P (decl_specifiers.type))
16885             decl = decl_specifiers.type;
16886
16887           if (decl && decl != error_mark_node)
16888             decl = TYPE_NAME (decl);
16889           else
16890             decl = error_mark_node;
16891
16892           /* Perform access checks for template parameters.  */
16893           cp_parser_perform_template_parameter_access_checks (checks);
16894         }
16895     }
16896   /* If it's not a template class, try for a template function.  If
16897      the next token is a `;', then this declaration does not declare
16898      anything.  But, if there were errors in the decl-specifiers, then
16899      the error might well have come from an attempted class-specifier.
16900      In that case, there's no need to warn about a missing declarator.  */
16901   if (!decl
16902       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16903           || decl_specifiers.type != error_mark_node))
16904     {
16905       decl = cp_parser_init_declarator (parser,
16906                                         &decl_specifiers,
16907                                         checks,
16908                                         /*function_definition_allowed_p=*/true,
16909                                         member_p,
16910                                         declares_class_or_enum,
16911                                         &function_definition_p);
16912
16913     /* 7.1.1-1 [dcl.stc]
16914
16915        A storage-class-specifier shall not be specified in an explicit
16916        specialization...  */
16917     if (decl
16918         && explicit_specialization_p
16919         && decl_specifiers.storage_class != sc_none)
16920       {
16921         error ("explicit template specialization cannot have a storage class");
16922         decl = error_mark_node;
16923       }
16924     }
16925
16926   pop_deferring_access_checks ();
16927
16928   /* Clear any current qualification; whatever comes next is the start
16929      of something new.  */
16930   parser->scope = NULL_TREE;
16931   parser->qualifying_scope = NULL_TREE;
16932   parser->object_scope = NULL_TREE;
16933   /* Look for a trailing `;' after the declaration.  */
16934   if (!function_definition_p
16935       && (decl == error_mark_node
16936           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16937     cp_parser_skip_to_end_of_block_or_statement (parser);
16938
16939   return decl;
16940 }
16941
16942 /* Parse a cast-expression that is not the operand of a unary "&".  */
16943
16944 static tree
16945 cp_parser_simple_cast_expression (cp_parser *parser)
16946 {
16947   return cp_parser_cast_expression (parser, /*address_p=*/false,
16948                                     /*cast_p=*/false);
16949 }
16950
16951 /* Parse a functional cast to TYPE.  Returns an expression
16952    representing the cast.  */
16953
16954 static tree
16955 cp_parser_functional_cast (cp_parser* parser, tree type)
16956 {
16957   tree expression_list;
16958   tree cast;
16959
16960   expression_list
16961     = cp_parser_parenthesized_expression_list (parser, false,
16962                                                /*cast_p=*/true,
16963                                                /*allow_expansion_p=*/true,
16964                                                /*non_constant_p=*/NULL);
16965
16966   cast = build_functional_cast (type, expression_list);
16967   /* [expr.const]/1: In an integral constant expression "only type
16968      conversions to integral or enumeration type can be used".  */
16969   if (TREE_CODE (type) == TYPE_DECL)
16970     type = TREE_TYPE (type);
16971   if (cast != error_mark_node
16972       && !cast_valid_in_integral_constant_expression_p (type)
16973       && (cp_parser_non_integral_constant_expression
16974           (parser, "a call to a constructor")))
16975     return error_mark_node;
16976   return cast;
16977 }
16978
16979 /* Save the tokens that make up the body of a member function defined
16980    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
16981    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
16982    specifiers applied to the declaration.  Returns the FUNCTION_DECL
16983    for the member function.  */
16984
16985 static tree
16986 cp_parser_save_member_function_body (cp_parser* parser,
16987                                      cp_decl_specifier_seq *decl_specifiers,
16988                                      cp_declarator *declarator,
16989                                      tree attributes)
16990 {
16991   cp_token *first;
16992   cp_token *last;
16993   tree fn;
16994
16995   /* Create the function-declaration.  */
16996   fn = start_method (decl_specifiers, declarator, attributes);
16997   /* If something went badly wrong, bail out now.  */
16998   if (fn == error_mark_node)
16999     {
17000       /* If there's a function-body, skip it.  */
17001       if (cp_parser_token_starts_function_definition_p
17002           (cp_lexer_peek_token (parser->lexer)))
17003         cp_parser_skip_to_end_of_block_or_statement (parser);
17004       return error_mark_node;
17005     }
17006
17007   /* Remember it, if there default args to post process.  */
17008   cp_parser_save_default_args (parser, fn);
17009
17010   /* Save away the tokens that make up the body of the
17011      function.  */
17012   first = parser->lexer->next_token;
17013   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17014   /* Handle function try blocks.  */
17015   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17016     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17017   last = parser->lexer->next_token;
17018
17019   /* Save away the inline definition; we will process it when the
17020      class is complete.  */
17021   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17022   DECL_PENDING_INLINE_P (fn) = 1;
17023
17024   /* We need to know that this was defined in the class, so that
17025      friend templates are handled correctly.  */
17026   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17027
17028   /* We're done with the inline definition.  */
17029   finish_method (fn);
17030
17031   /* Add FN to the queue of functions to be parsed later.  */
17032   TREE_VALUE (parser->unparsed_functions_queues)
17033     = tree_cons (NULL_TREE, fn,
17034                  TREE_VALUE (parser->unparsed_functions_queues));
17035
17036   return fn;
17037 }
17038
17039 /* Parse a template-argument-list, as well as the trailing ">" (but
17040    not the opening ">").  See cp_parser_template_argument_list for the
17041    return value.  */
17042
17043 static tree
17044 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17045 {
17046   tree arguments;
17047   tree saved_scope;
17048   tree saved_qualifying_scope;
17049   tree saved_object_scope;
17050   bool saved_greater_than_is_operator_p;
17051   bool saved_skip_evaluation;
17052
17053   /* [temp.names]
17054
17055      When parsing a template-id, the first non-nested `>' is taken as
17056      the end of the template-argument-list rather than a greater-than
17057      operator.  */
17058   saved_greater_than_is_operator_p
17059     = parser->greater_than_is_operator_p;
17060   parser->greater_than_is_operator_p = false;
17061   /* Parsing the argument list may modify SCOPE, so we save it
17062      here.  */
17063   saved_scope = parser->scope;
17064   saved_qualifying_scope = parser->qualifying_scope;
17065   saved_object_scope = parser->object_scope;
17066   /* We need to evaluate the template arguments, even though this
17067      template-id may be nested within a "sizeof".  */
17068   saved_skip_evaluation = skip_evaluation;
17069   skip_evaluation = false;
17070   /* Parse the template-argument-list itself.  */
17071   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17072       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17073     arguments = NULL_TREE;
17074   else
17075     arguments = cp_parser_template_argument_list (parser);
17076   /* Look for the `>' that ends the template-argument-list. If we find
17077      a '>>' instead, it's probably just a typo.  */
17078   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17079     {
17080       if (cxx_dialect != cxx98)
17081         {
17082           /* In C++0x, a `>>' in a template argument list or cast
17083              expression is considered to be two separate `>'
17084              tokens. So, change the current token to a `>', but don't
17085              consume it: it will be consumed later when the outer
17086              template argument list (or cast expression) is parsed.
17087              Note that this replacement of `>' for `>>' is necessary
17088              even if we are parsing tentatively: in the tentative
17089              case, after calling
17090              cp_parser_enclosed_template_argument_list we will always
17091              throw away all of the template arguments and the first
17092              closing `>', either because the template argument list
17093              was erroneous or because we are replacing those tokens
17094              with a CPP_TEMPLATE_ID token.  The second `>' (which will
17095              not have been thrown away) is needed either to close an
17096              outer template argument list or to complete a new-style
17097              cast.  */
17098           cp_token *token = cp_lexer_peek_token (parser->lexer);
17099           token->type = CPP_GREATER;
17100         }
17101       else if (!saved_greater_than_is_operator_p)
17102         {
17103           /* If we're in a nested template argument list, the '>>' has
17104             to be a typo for '> >'. We emit the error message, but we
17105             continue parsing and we push a '>' as next token, so that
17106             the argument list will be parsed correctly.  Note that the
17107             global source location is still on the token before the
17108             '>>', so we need to say explicitly where we want it.  */
17109           cp_token *token = cp_lexer_peek_token (parser->lexer);
17110           error ("%H%<>>%> should be %<> >%> "
17111                  "within a nested template argument list",
17112                  &token->location);
17113
17114           token->type = CPP_GREATER;
17115         }
17116       else
17117         {
17118           /* If this is not a nested template argument list, the '>>'
17119             is a typo for '>'. Emit an error message and continue.
17120             Same deal about the token location, but here we can get it
17121             right by consuming the '>>' before issuing the diagnostic.  */
17122           cp_lexer_consume_token (parser->lexer);
17123           error ("spurious %<>>%>, use %<>%> to terminate "
17124                  "a template argument list");
17125         }
17126     }
17127   else
17128     cp_parser_skip_to_end_of_template_parameter_list (parser);
17129   /* The `>' token might be a greater-than operator again now.  */
17130   parser->greater_than_is_operator_p
17131     = saved_greater_than_is_operator_p;
17132   /* Restore the SAVED_SCOPE.  */
17133   parser->scope = saved_scope;
17134   parser->qualifying_scope = saved_qualifying_scope;
17135   parser->object_scope = saved_object_scope;
17136   skip_evaluation = saved_skip_evaluation;
17137
17138   return arguments;
17139 }
17140
17141 /* MEMBER_FUNCTION is a member function, or a friend.  If default
17142    arguments, or the body of the function have not yet been parsed,
17143    parse them now.  */
17144
17145 static void
17146 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17147 {
17148   /* If this member is a template, get the underlying
17149      FUNCTION_DECL.  */
17150   if (DECL_FUNCTION_TEMPLATE_P (member_function))
17151     member_function = DECL_TEMPLATE_RESULT (member_function);
17152
17153   /* There should not be any class definitions in progress at this
17154      point; the bodies of members are only parsed outside of all class
17155      definitions.  */
17156   gcc_assert (parser->num_classes_being_defined == 0);
17157   /* While we're parsing the member functions we might encounter more
17158      classes.  We want to handle them right away, but we don't want
17159      them getting mixed up with functions that are currently in the
17160      queue.  */
17161   parser->unparsed_functions_queues
17162     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17163
17164   /* Make sure that any template parameters are in scope.  */
17165   maybe_begin_member_template_processing (member_function);
17166
17167   /* If the body of the function has not yet been parsed, parse it
17168      now.  */
17169   if (DECL_PENDING_INLINE_P (member_function))
17170     {
17171       tree function_scope;
17172       cp_token_cache *tokens;
17173
17174       /* The function is no longer pending; we are processing it.  */
17175       tokens = DECL_PENDING_INLINE_INFO (member_function);
17176       DECL_PENDING_INLINE_INFO (member_function) = NULL;
17177       DECL_PENDING_INLINE_P (member_function) = 0;
17178
17179       /* If this is a local class, enter the scope of the containing
17180          function.  */
17181       function_scope = current_function_decl;
17182       if (function_scope)
17183         push_function_context_to (function_scope);
17184
17185
17186       /* Push the body of the function onto the lexer stack.  */
17187       cp_parser_push_lexer_for_tokens (parser, tokens);
17188
17189       /* Let the front end know that we going to be defining this
17190          function.  */
17191       start_preparsed_function (member_function, NULL_TREE,
17192                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
17193
17194       /* Don't do access checking if it is a templated function.  */
17195       if (processing_template_decl)
17196         push_deferring_access_checks (dk_no_check);
17197
17198       /* Now, parse the body of the function.  */
17199       cp_parser_function_definition_after_declarator (parser,
17200                                                       /*inline_p=*/true);
17201
17202       if (processing_template_decl)
17203         pop_deferring_access_checks ();
17204
17205       /* Leave the scope of the containing function.  */
17206       if (function_scope)
17207         pop_function_context_from (function_scope);
17208       cp_parser_pop_lexer (parser);
17209     }
17210
17211   /* Remove any template parameters from the symbol table.  */
17212   maybe_end_member_template_processing ();
17213
17214   /* Restore the queue.  */
17215   parser->unparsed_functions_queues
17216     = TREE_CHAIN (parser->unparsed_functions_queues);
17217 }
17218
17219 /* If DECL contains any default args, remember it on the unparsed
17220    functions queue.  */
17221
17222 static void
17223 cp_parser_save_default_args (cp_parser* parser, tree decl)
17224 {
17225   tree probe;
17226
17227   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17228        probe;
17229        probe = TREE_CHAIN (probe))
17230     if (TREE_PURPOSE (probe))
17231       {
17232         TREE_PURPOSE (parser->unparsed_functions_queues)
17233           = tree_cons (current_class_type, decl,
17234                        TREE_PURPOSE (parser->unparsed_functions_queues));
17235         break;
17236       }
17237 }
17238
17239 /* FN is a FUNCTION_DECL which may contains a parameter with an
17240    unparsed DEFAULT_ARG.  Parse the default args now.  This function
17241    assumes that the current scope is the scope in which the default
17242    argument should be processed.  */
17243
17244 static void
17245 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17246 {
17247   bool saved_local_variables_forbidden_p;
17248   tree parm;
17249
17250   /* While we're parsing the default args, we might (due to the
17251      statement expression extension) encounter more classes.  We want
17252      to handle them right away, but we don't want them getting mixed
17253      up with default args that are currently in the queue.  */
17254   parser->unparsed_functions_queues
17255     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17256
17257   /* Local variable names (and the `this' keyword) may not appear
17258      in a default argument.  */
17259   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17260   parser->local_variables_forbidden_p = true;
17261
17262   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17263        parm;
17264        parm = TREE_CHAIN (parm))
17265     {
17266       cp_token_cache *tokens;
17267       tree default_arg = TREE_PURPOSE (parm);
17268       tree parsed_arg;
17269       VEC(tree,gc) *insts;
17270       tree copy;
17271       unsigned ix;
17272
17273       if (!default_arg)
17274         continue;
17275
17276       if (TREE_CODE (default_arg) != DEFAULT_ARG)
17277         /* This can happen for a friend declaration for a function
17278            already declared with default arguments.  */
17279         continue;
17280
17281        /* Push the saved tokens for the default argument onto the parser's
17282           lexer stack.  */
17283       tokens = DEFARG_TOKENS (default_arg);
17284       cp_parser_push_lexer_for_tokens (parser, tokens);
17285
17286       /* Parse the assignment-expression.  */
17287       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17288
17289       if (!processing_template_decl)
17290         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17291
17292       TREE_PURPOSE (parm) = parsed_arg;
17293
17294       /* Update any instantiations we've already created.  */
17295       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17296            VEC_iterate (tree, insts, ix, copy); ix++)
17297         TREE_PURPOSE (copy) = parsed_arg;
17298
17299       /* If the token stream has not been completely used up, then
17300          there was extra junk after the end of the default
17301          argument.  */
17302       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17303         cp_parser_error (parser, "expected %<,%>");
17304
17305       /* Revert to the main lexer.  */
17306       cp_parser_pop_lexer (parser);
17307     }
17308
17309   /* Make sure no default arg is missing.  */
17310   check_default_args (fn);
17311
17312   /* Restore the state of local_variables_forbidden_p.  */
17313   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17314
17315   /* Restore the queue.  */
17316   parser->unparsed_functions_queues
17317     = TREE_CHAIN (parser->unparsed_functions_queues);
17318 }
17319
17320 /* Parse the operand of `sizeof' (or a similar operator).  Returns
17321    either a TYPE or an expression, depending on the form of the
17322    input.  The KEYWORD indicates which kind of expression we have
17323    encountered.  */
17324
17325 static tree
17326 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17327 {
17328   static const char *format;
17329   tree expr = NULL_TREE;
17330   const char *saved_message;
17331   char *tmp;
17332   bool saved_integral_constant_expression_p;
17333   bool saved_non_integral_constant_expression_p;
17334   bool pack_expansion_p = false;
17335
17336   /* Initialize FORMAT the first time we get here.  */
17337   if (!format)
17338     format = "types may not be defined in '%s' expressions";
17339
17340   /* Types cannot be defined in a `sizeof' expression.  Save away the
17341      old message.  */
17342   saved_message = parser->type_definition_forbidden_message;
17343   /* And create the new one.  */
17344   parser->type_definition_forbidden_message = tmp
17345     = XNEWVEC (char, strlen (format)
17346                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17347                + 1 /* `\0' */);
17348   sprintf (tmp, format, IDENTIFIER_POINTER (ridpointers[keyword]));
17349
17350   /* The restrictions on constant-expressions do not apply inside
17351      sizeof expressions.  */
17352   saved_integral_constant_expression_p
17353     = parser->integral_constant_expression_p;
17354   saved_non_integral_constant_expression_p
17355     = parser->non_integral_constant_expression_p;
17356   parser->integral_constant_expression_p = false;
17357
17358   /* If it's a `...', then we are computing the length of a parameter
17359      pack.  */
17360   if (keyword == RID_SIZEOF
17361       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17362     {
17363       /* Consume the `...'.  */
17364       cp_lexer_consume_token (parser->lexer);
17365       maybe_warn_variadic_templates ();
17366
17367       /* Note that this is an expansion.  */
17368       pack_expansion_p = true;
17369     }
17370
17371   /* Do not actually evaluate the expression.  */
17372   ++skip_evaluation;
17373   /* If it's a `(', then we might be looking at the type-id
17374      construction.  */
17375   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17376     {
17377       tree type;
17378       bool saved_in_type_id_in_expr_p;
17379
17380       /* We can't be sure yet whether we're looking at a type-id or an
17381          expression.  */
17382       cp_parser_parse_tentatively (parser);
17383       /* Consume the `('.  */
17384       cp_lexer_consume_token (parser->lexer);
17385       /* Parse the type-id.  */
17386       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17387       parser->in_type_id_in_expr_p = true;
17388       type = cp_parser_type_id (parser);
17389       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17390       /* Now, look for the trailing `)'.  */
17391       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17392       /* If all went well, then we're done.  */
17393       if (cp_parser_parse_definitely (parser))
17394         {
17395           cp_decl_specifier_seq decl_specs;
17396
17397           /* Build a trivial decl-specifier-seq.  */
17398           clear_decl_specs (&decl_specs);
17399           decl_specs.type = type;
17400
17401           /* Call grokdeclarator to figure out what type this is.  */
17402           expr = grokdeclarator (NULL,
17403                                  &decl_specs,
17404                                  TYPENAME,
17405                                  /*initialized=*/0,
17406                                  /*attrlist=*/NULL);
17407         }
17408     }
17409
17410   /* If the type-id production did not work out, then we must be
17411      looking at the unary-expression production.  */
17412   if (!expr)
17413     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17414                                        /*cast_p=*/false);
17415
17416   if (pack_expansion_p)
17417     /* Build a pack expansion. */
17418     expr = make_pack_expansion (expr);
17419
17420   /* Go back to evaluating expressions.  */
17421   --skip_evaluation;
17422
17423   /* Free the message we created.  */
17424   free (tmp);
17425   /* And restore the old one.  */
17426   parser->type_definition_forbidden_message = saved_message;
17427   parser->integral_constant_expression_p
17428     = saved_integral_constant_expression_p;
17429   parser->non_integral_constant_expression_p
17430     = saved_non_integral_constant_expression_p;
17431
17432   return expr;
17433 }
17434
17435 /* If the current declaration has no declarator, return true.  */
17436
17437 static bool
17438 cp_parser_declares_only_class_p (cp_parser *parser)
17439 {
17440   /* If the next token is a `;' or a `,' then there is no
17441      declarator.  */
17442   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17443           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17444 }
17445
17446 /* Update the DECL_SPECS to reflect the storage class indicated by
17447    KEYWORD.  */
17448
17449 static void
17450 cp_parser_set_storage_class (cp_parser *parser,
17451                              cp_decl_specifier_seq *decl_specs,
17452                              enum rid keyword)
17453 {
17454   cp_storage_class storage_class;
17455
17456   if (parser->in_unbraced_linkage_specification_p)
17457     {
17458       error ("invalid use of %qD in linkage specification",
17459              ridpointers[keyword]);
17460       return;
17461     }
17462   else if (decl_specs->storage_class != sc_none)
17463     {
17464       decl_specs->conflicting_specifiers_p = true;
17465       return;
17466     }
17467
17468   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17469       && decl_specs->specs[(int) ds_thread])
17470     {
17471       error ("%<__thread%> before %qD", ridpointers[keyword]);
17472       decl_specs->specs[(int) ds_thread] = 0;
17473     }
17474
17475   switch (keyword)
17476     {
17477     case RID_AUTO:
17478       storage_class = sc_auto;
17479       break;
17480     case RID_REGISTER:
17481       storage_class = sc_register;
17482       break;
17483     case RID_STATIC:
17484       storage_class = sc_static;
17485       break;
17486     case RID_EXTERN:
17487       storage_class = sc_extern;
17488       break;
17489     case RID_MUTABLE:
17490       storage_class = sc_mutable;
17491       break;
17492     default:
17493       gcc_unreachable ();
17494     }
17495   decl_specs->storage_class = storage_class;
17496
17497   /* A storage class specifier cannot be applied alongside a typedef 
17498      specifier. If there is a typedef specifier present then set 
17499      conflicting_specifiers_p which will trigger an error later
17500      on in grokdeclarator. */
17501   if (decl_specs->specs[(int)ds_typedef])
17502     decl_specs->conflicting_specifiers_p = true;
17503 }
17504
17505 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
17506    is true, the type is a user-defined type; otherwise it is a
17507    built-in type specified by a keyword.  */
17508
17509 static void
17510 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17511                               tree type_spec,
17512                               bool user_defined_p)
17513 {
17514   decl_specs->any_specifiers_p = true;
17515
17516   /* If the user tries to redeclare bool or wchar_t (with, for
17517      example, in "typedef int wchar_t;") we remember that this is what
17518      happened.  In system headers, we ignore these declarations so
17519      that G++ can work with system headers that are not C++-safe.  */
17520   if (decl_specs->specs[(int) ds_typedef]
17521       && !user_defined_p
17522       && (type_spec == boolean_type_node
17523           || type_spec == wchar_type_node)
17524       && (decl_specs->type
17525           || decl_specs->specs[(int) ds_long]
17526           || decl_specs->specs[(int) ds_short]
17527           || decl_specs->specs[(int) ds_unsigned]
17528           || decl_specs->specs[(int) ds_signed]))
17529     {
17530       decl_specs->redefined_builtin_type = type_spec;
17531       if (!decl_specs->type)
17532         {
17533           decl_specs->type = type_spec;
17534           decl_specs->user_defined_type_p = false;
17535         }
17536     }
17537   else if (decl_specs->type)
17538     decl_specs->multiple_types_p = true;
17539   else
17540     {
17541       decl_specs->type = type_spec;
17542       decl_specs->user_defined_type_p = user_defined_p;
17543       decl_specs->redefined_builtin_type = NULL_TREE;
17544     }
17545 }
17546
17547 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17548    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
17549
17550 static bool
17551 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17552 {
17553   return decl_specifiers->specs[(int) ds_friend] != 0;
17554 }
17555
17556 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
17557    issue an error message indicating that TOKEN_DESC was expected.
17558
17559    Returns the token consumed, if the token had the appropriate type.
17560    Otherwise, returns NULL.  */
17561
17562 static cp_token *
17563 cp_parser_require (cp_parser* parser,
17564                    enum cpp_ttype type,
17565                    const char* token_desc)
17566 {
17567   if (cp_lexer_next_token_is (parser->lexer, type))
17568     return cp_lexer_consume_token (parser->lexer);
17569   else
17570     {
17571       /* Output the MESSAGE -- unless we're parsing tentatively.  */
17572       if (!cp_parser_simulate_error (parser))
17573         {
17574           char *message = concat ("expected ", token_desc, NULL);
17575           cp_parser_error (parser, message);
17576           free (message);
17577         }
17578       return NULL;
17579     }
17580 }
17581
17582 /* An error message is produced if the next token is not '>'.
17583    All further tokens are skipped until the desired token is
17584    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
17585
17586 static void
17587 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17588 {
17589   /* Current level of '< ... >'.  */
17590   unsigned level = 0;
17591   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
17592   unsigned nesting_depth = 0;
17593
17594   /* Are we ready, yet?  If not, issue error message.  */
17595   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17596     return;
17597
17598   /* Skip tokens until the desired token is found.  */
17599   while (true)
17600     {
17601       /* Peek at the next token.  */
17602       switch (cp_lexer_peek_token (parser->lexer)->type)
17603         {
17604         case CPP_LESS:
17605           if (!nesting_depth)
17606             ++level;
17607           break;
17608
17609         case CPP_RSHIFT:
17610           if (cxx_dialect == cxx98)
17611             /* C++0x views the `>>' operator as two `>' tokens, but
17612                C++98 does not. */
17613             break;
17614           else if (!nesting_depth && level-- == 0)
17615             {
17616               /* We've hit a `>>' where the first `>' closes the
17617                  template argument list, and the second `>' is
17618                  spurious.  Just consume the `>>' and stop; we've
17619                  already produced at least one error.  */
17620               cp_lexer_consume_token (parser->lexer);
17621               return;
17622             }
17623           /* Fall through for C++0x, so we handle the second `>' in
17624              the `>>'.  */
17625
17626         case CPP_GREATER:
17627           if (!nesting_depth && level-- == 0)
17628             {
17629               /* We've reached the token we want, consume it and stop.  */
17630               cp_lexer_consume_token (parser->lexer);
17631               return;
17632             }
17633           break;
17634
17635         case CPP_OPEN_PAREN:
17636         case CPP_OPEN_SQUARE:
17637           ++nesting_depth;
17638           break;
17639
17640         case CPP_CLOSE_PAREN:
17641         case CPP_CLOSE_SQUARE:
17642           if (nesting_depth-- == 0)
17643             return;
17644           break;
17645
17646         case CPP_EOF:
17647         case CPP_PRAGMA_EOL:
17648         case CPP_SEMICOLON:
17649         case CPP_OPEN_BRACE:
17650         case CPP_CLOSE_BRACE:
17651           /* The '>' was probably forgotten, don't look further.  */
17652           return;
17653
17654         default:
17655           break;
17656         }
17657
17658       /* Consume this token.  */
17659       cp_lexer_consume_token (parser->lexer);
17660     }
17661 }
17662
17663 /* If the next token is the indicated keyword, consume it.  Otherwise,
17664    issue an error message indicating that TOKEN_DESC was expected.
17665
17666    Returns the token consumed, if the token had the appropriate type.
17667    Otherwise, returns NULL.  */
17668
17669 static cp_token *
17670 cp_parser_require_keyword (cp_parser* parser,
17671                            enum rid keyword,
17672                            const char* token_desc)
17673 {
17674   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17675
17676   if (token && token->keyword != keyword)
17677     {
17678       dyn_string_t error_msg;
17679
17680       /* Format the error message.  */
17681       error_msg = dyn_string_new (0);
17682       dyn_string_append_cstr (error_msg, "expected ");
17683       dyn_string_append_cstr (error_msg, token_desc);
17684       cp_parser_error (parser, error_msg->s);
17685       dyn_string_delete (error_msg);
17686       return NULL;
17687     }
17688
17689   return token;
17690 }
17691
17692 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17693    function-definition.  */
17694
17695 static bool
17696 cp_parser_token_starts_function_definition_p (cp_token* token)
17697 {
17698   return (/* An ordinary function-body begins with an `{'.  */
17699           token->type == CPP_OPEN_BRACE
17700           /* A ctor-initializer begins with a `:'.  */
17701           || token->type == CPP_COLON
17702           /* A function-try-block begins with `try'.  */
17703           || token->keyword == RID_TRY
17704           /* The named return value extension begins with `return'.  */
17705           || token->keyword == RID_RETURN);
17706 }
17707
17708 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17709    definition.  */
17710
17711 static bool
17712 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17713 {
17714   cp_token *token;
17715
17716   token = cp_lexer_peek_token (parser->lexer);
17717   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17718 }
17719
17720 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17721    C++0x) ending a template-argument.  */
17722
17723 static bool
17724 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17725 {
17726   cp_token *token;
17727
17728   token = cp_lexer_peek_token (parser->lexer);
17729   return (token->type == CPP_COMMA 
17730           || token->type == CPP_GREATER
17731           || token->type == CPP_ELLIPSIS
17732           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
17733 }
17734
17735 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17736    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
17737
17738 static bool
17739 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17740                                                      size_t n)
17741 {
17742   cp_token *token;
17743
17744   token = cp_lexer_peek_nth_token (parser->lexer, n);
17745   if (token->type == CPP_LESS)
17746     return true;
17747   /* Check for the sequence `<::' in the original code. It would be lexed as
17748      `[:', where `[' is a digraph, and there is no whitespace before
17749      `:'.  */
17750   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17751     {
17752       cp_token *token2;
17753       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17754       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17755         return true;
17756     }
17757   return false;
17758 }
17759
17760 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17761    or none_type otherwise.  */
17762
17763 static enum tag_types
17764 cp_parser_token_is_class_key (cp_token* token)
17765 {
17766   switch (token->keyword)
17767     {
17768     case RID_CLASS:
17769       return class_type;
17770     case RID_STRUCT:
17771       return record_type;
17772     case RID_UNION:
17773       return union_type;
17774
17775     default:
17776       return none_type;
17777     }
17778 }
17779
17780 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
17781
17782 static void
17783 cp_parser_check_class_key (enum tag_types class_key, tree type)
17784 {
17785   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17786     pedwarn ("%qs tag used in naming %q#T",
17787             class_key == union_type ? "union"
17788              : class_key == record_type ? "struct" : "class",
17789              type);
17790 }
17791
17792 /* Issue an error message if DECL is redeclared with different
17793    access than its original declaration [class.access.spec/3].
17794    This applies to nested classes and nested class templates.
17795    [class.mem/1].  */
17796
17797 static void
17798 cp_parser_check_access_in_redeclaration (tree decl)
17799 {
17800   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
17801     return;
17802
17803   if ((TREE_PRIVATE (decl)
17804        != (current_access_specifier == access_private_node))
17805       || (TREE_PROTECTED (decl)
17806           != (current_access_specifier == access_protected_node)))
17807     error ("%qD redeclared with different access", decl);
17808 }
17809
17810 /* Look for the `template' keyword, as a syntactic disambiguator.
17811    Return TRUE iff it is present, in which case it will be
17812    consumed.  */
17813
17814 static bool
17815 cp_parser_optional_template_keyword (cp_parser *parser)
17816 {
17817   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17818     {
17819       /* The `template' keyword can only be used within templates;
17820          outside templates the parser can always figure out what is a
17821          template and what is not.  */
17822       if (!processing_template_decl)
17823         {
17824           error ("%<template%> (as a disambiguator) is only allowed "
17825                  "within templates");
17826           /* If this part of the token stream is rescanned, the same
17827              error message would be generated.  So, we purge the token
17828              from the stream.  */
17829           cp_lexer_purge_token (parser->lexer);
17830           return false;
17831         }
17832       else
17833         {
17834           /* Consume the `template' keyword.  */
17835           cp_lexer_consume_token (parser->lexer);
17836           return true;
17837         }
17838     }
17839
17840   return false;
17841 }
17842
17843 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
17844    set PARSER->SCOPE, and perform other related actions.  */
17845
17846 static void
17847 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17848 {
17849   int i;
17850   struct tree_check *check_value;
17851   deferred_access_check *chk;
17852   VEC (deferred_access_check,gc) *checks;
17853
17854   /* Get the stored value.  */
17855   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17856   /* Perform any access checks that were deferred.  */
17857   checks = check_value->checks;
17858   if (checks)
17859     {
17860       for (i = 0 ;
17861            VEC_iterate (deferred_access_check, checks, i, chk) ;
17862            ++i)
17863         {
17864           perform_or_defer_access_check (chk->binfo,
17865                                          chk->decl,
17866                                          chk->diag_decl);
17867         }
17868     }
17869   /* Set the scope from the stored value.  */
17870   parser->scope = check_value->value;
17871   parser->qualifying_scope = check_value->qualifying_scope;
17872   parser->object_scope = NULL_TREE;
17873 }
17874
17875 /* Consume tokens up through a non-nested END token.  */
17876
17877 static void
17878 cp_parser_cache_group (cp_parser *parser,
17879                        enum cpp_ttype end,
17880                        unsigned depth)
17881 {
17882   while (true)
17883     {
17884       cp_token *token;
17885
17886       /* Abort a parenthesized expression if we encounter a brace.  */
17887       if ((end == CPP_CLOSE_PAREN || depth == 0)
17888           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17889         return;
17890       /* If we've reached the end of the file, stop.  */
17891       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
17892           || (end != CPP_PRAGMA_EOL
17893               && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
17894         return;
17895       /* Consume the next token.  */
17896       token = cp_lexer_consume_token (parser->lexer);
17897       /* See if it starts a new group.  */
17898       if (token->type == CPP_OPEN_BRACE)
17899         {
17900           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
17901           if (depth == 0)
17902             return;
17903         }
17904       else if (token->type == CPP_OPEN_PAREN)
17905         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
17906       else if (token->type == CPP_PRAGMA)
17907         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
17908       else if (token->type == end)
17909         return;
17910     }
17911 }
17912
17913 /* Begin parsing tentatively.  We always save tokens while parsing
17914    tentatively so that if the tentative parsing fails we can restore the
17915    tokens.  */
17916
17917 static void
17918 cp_parser_parse_tentatively (cp_parser* parser)
17919 {
17920   /* Enter a new parsing context.  */
17921   parser->context = cp_parser_context_new (parser->context);
17922   /* Begin saving tokens.  */
17923   cp_lexer_save_tokens (parser->lexer);
17924   /* In order to avoid repetitive access control error messages,
17925      access checks are queued up until we are no longer parsing
17926      tentatively.  */
17927   push_deferring_access_checks (dk_deferred);
17928 }
17929
17930 /* Commit to the currently active tentative parse.  */
17931
17932 static void
17933 cp_parser_commit_to_tentative_parse (cp_parser* parser)
17934 {
17935   cp_parser_context *context;
17936   cp_lexer *lexer;
17937
17938   /* Mark all of the levels as committed.  */
17939   lexer = parser->lexer;
17940   for (context = parser->context; context->next; context = context->next)
17941     {
17942       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17943         break;
17944       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17945       while (!cp_lexer_saving_tokens (lexer))
17946         lexer = lexer->next;
17947       cp_lexer_commit_tokens (lexer);
17948     }
17949 }
17950
17951 /* Abort the currently active tentative parse.  All consumed tokens
17952    will be rolled back, and no diagnostics will be issued.  */
17953
17954 static void
17955 cp_parser_abort_tentative_parse (cp_parser* parser)
17956 {
17957   cp_parser_simulate_error (parser);
17958   /* Now, pretend that we want to see if the construct was
17959      successfully parsed.  */
17960   cp_parser_parse_definitely (parser);
17961 }
17962
17963 /* Stop parsing tentatively.  If a parse error has occurred, restore the
17964    token stream.  Otherwise, commit to the tokens we have consumed.
17965    Returns true if no error occurred; false otherwise.  */
17966
17967 static bool
17968 cp_parser_parse_definitely (cp_parser* parser)
17969 {
17970   bool error_occurred;
17971   cp_parser_context *context;
17972
17973   /* Remember whether or not an error occurred, since we are about to
17974      destroy that information.  */
17975   error_occurred = cp_parser_error_occurred (parser);
17976   /* Remove the topmost context from the stack.  */
17977   context = parser->context;
17978   parser->context = context->next;
17979   /* If no parse errors occurred, commit to the tentative parse.  */
17980   if (!error_occurred)
17981     {
17982       /* Commit to the tokens read tentatively, unless that was
17983          already done.  */
17984       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17985         cp_lexer_commit_tokens (parser->lexer);
17986
17987       pop_to_parent_deferring_access_checks ();
17988     }
17989   /* Otherwise, if errors occurred, roll back our state so that things
17990      are just as they were before we began the tentative parse.  */
17991   else
17992     {
17993       cp_lexer_rollback_tokens (parser->lexer);
17994       pop_deferring_access_checks ();
17995     }
17996   /* Add the context to the front of the free list.  */
17997   context->next = cp_parser_context_free_list;
17998   cp_parser_context_free_list = context;
17999
18000   return !error_occurred;
18001 }
18002
18003 /* Returns true if we are parsing tentatively and are not committed to
18004    this tentative parse.  */
18005
18006 static bool
18007 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18008 {
18009   return (cp_parser_parsing_tentatively (parser)
18010           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18011 }
18012
18013 /* Returns nonzero iff an error has occurred during the most recent
18014    tentative parse.  */
18015
18016 static bool
18017 cp_parser_error_occurred (cp_parser* parser)
18018 {
18019   return (cp_parser_parsing_tentatively (parser)
18020           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18021 }
18022
18023 /* Returns nonzero if GNU extensions are allowed.  */
18024
18025 static bool
18026 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18027 {
18028   return parser->allow_gnu_extensions_p;
18029 }
18030 \f
18031 /* Objective-C++ Productions */
18032
18033
18034 /* Parse an Objective-C expression, which feeds into a primary-expression
18035    above.
18036
18037    objc-expression:
18038      objc-message-expression
18039      objc-string-literal
18040      objc-encode-expression
18041      objc-protocol-expression
18042      objc-selector-expression
18043
18044   Returns a tree representation of the expression.  */
18045
18046 static tree
18047 cp_parser_objc_expression (cp_parser* parser)
18048 {
18049   /* Try to figure out what kind of declaration is present.  */
18050   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18051
18052   switch (kwd->type)
18053     {
18054     case CPP_OPEN_SQUARE:
18055       return cp_parser_objc_message_expression (parser);
18056
18057     case CPP_OBJC_STRING:
18058       kwd = cp_lexer_consume_token (parser->lexer);
18059       return objc_build_string_object (kwd->u.value);
18060
18061     case CPP_KEYWORD:
18062       switch (kwd->keyword)
18063         {
18064         case RID_AT_ENCODE:
18065           return cp_parser_objc_encode_expression (parser);
18066
18067         case RID_AT_PROTOCOL:
18068           return cp_parser_objc_protocol_expression (parser);
18069
18070         case RID_AT_SELECTOR:
18071           return cp_parser_objc_selector_expression (parser);
18072
18073         default:
18074           break;
18075         }
18076     default:
18077       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18078       cp_parser_skip_to_end_of_block_or_statement (parser);
18079     }
18080
18081   return error_mark_node;
18082 }
18083
18084 /* Parse an Objective-C message expression.
18085
18086    objc-message-expression:
18087      [ objc-message-receiver objc-message-args ]
18088
18089    Returns a representation of an Objective-C message.  */
18090
18091 static tree
18092 cp_parser_objc_message_expression (cp_parser* parser)
18093 {
18094   tree receiver, messageargs;
18095
18096   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
18097   receiver = cp_parser_objc_message_receiver (parser);
18098   messageargs = cp_parser_objc_message_args (parser);
18099   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
18100
18101   return objc_build_message_expr (build_tree_list (receiver, messageargs));
18102 }
18103
18104 /* Parse an objc-message-receiver.
18105
18106    objc-message-receiver:
18107      expression
18108      simple-type-specifier
18109
18110   Returns a representation of the type or expression.  */
18111
18112 static tree
18113 cp_parser_objc_message_receiver (cp_parser* parser)
18114 {
18115   tree rcv;
18116
18117   /* An Objective-C message receiver may be either (1) a type
18118      or (2) an expression.  */
18119   cp_parser_parse_tentatively (parser);
18120   rcv = cp_parser_expression (parser, false);
18121
18122   if (cp_parser_parse_definitely (parser))
18123     return rcv;
18124
18125   rcv = cp_parser_simple_type_specifier (parser,
18126                                          /*decl_specs=*/NULL,
18127                                          CP_PARSER_FLAGS_NONE);
18128
18129   return objc_get_class_reference (rcv);
18130 }
18131
18132 /* Parse the arguments and selectors comprising an Objective-C message.
18133
18134    objc-message-args:
18135      objc-selector
18136      objc-selector-args
18137      objc-selector-args , objc-comma-args
18138
18139    objc-selector-args:
18140      objc-selector [opt] : assignment-expression
18141      objc-selector-args objc-selector [opt] : assignment-expression
18142
18143    objc-comma-args:
18144      assignment-expression
18145      objc-comma-args , assignment-expression
18146
18147    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18148    selector arguments and TREE_VALUE containing a list of comma
18149    arguments.  */
18150
18151 static tree
18152 cp_parser_objc_message_args (cp_parser* parser)
18153 {
18154   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18155   bool maybe_unary_selector_p = true;
18156   cp_token *token = cp_lexer_peek_token (parser->lexer);
18157
18158   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18159     {
18160       tree selector = NULL_TREE, arg;
18161
18162       if (token->type != CPP_COLON)
18163         selector = cp_parser_objc_selector (parser);
18164
18165       /* Detect if we have a unary selector.  */
18166       if (maybe_unary_selector_p
18167           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18168         return build_tree_list (selector, NULL_TREE);
18169
18170       maybe_unary_selector_p = false;
18171       cp_parser_require (parser, CPP_COLON, "`:'");
18172       arg = cp_parser_assignment_expression (parser, false);
18173
18174       sel_args
18175         = chainon (sel_args,
18176                    build_tree_list (selector, arg));
18177
18178       token = cp_lexer_peek_token (parser->lexer);
18179     }
18180
18181   /* Handle non-selector arguments, if any. */
18182   while (token->type == CPP_COMMA)
18183     {
18184       tree arg;
18185
18186       cp_lexer_consume_token (parser->lexer);
18187       arg = cp_parser_assignment_expression (parser, false);
18188
18189       addl_args
18190         = chainon (addl_args,
18191                    build_tree_list (NULL_TREE, arg));
18192
18193       token = cp_lexer_peek_token (parser->lexer);
18194     }
18195
18196   return build_tree_list (sel_args, addl_args);
18197 }
18198
18199 /* Parse an Objective-C encode expression.
18200
18201    objc-encode-expression:
18202      @encode objc-typename
18203
18204    Returns an encoded representation of the type argument.  */
18205
18206 static tree
18207 cp_parser_objc_encode_expression (cp_parser* parser)
18208 {
18209   tree type;
18210
18211   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
18212   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18213   type = complete_type (cp_parser_type_id (parser));
18214   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18215
18216   if (!type)
18217     {
18218       error ("%<@encode%> must specify a type as an argument");
18219       return error_mark_node;
18220     }
18221
18222   return objc_build_encode_expr (type);
18223 }
18224
18225 /* Parse an Objective-C @defs expression.  */
18226
18227 static tree
18228 cp_parser_objc_defs_expression (cp_parser *parser)
18229 {
18230   tree name;
18231
18232   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
18233   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18234   name = cp_parser_identifier (parser);
18235   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18236
18237   return objc_get_class_ivars (name);
18238 }
18239
18240 /* Parse an Objective-C protocol expression.
18241
18242   objc-protocol-expression:
18243     @protocol ( identifier )
18244
18245   Returns a representation of the protocol expression.  */
18246
18247 static tree
18248 cp_parser_objc_protocol_expression (cp_parser* parser)
18249 {
18250   tree proto;
18251
18252   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
18253   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18254   proto = cp_parser_identifier (parser);
18255   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18256
18257   return objc_build_protocol_expr (proto);
18258 }
18259
18260 /* Parse an Objective-C selector expression.
18261
18262    objc-selector-expression:
18263      @selector ( objc-method-signature )
18264
18265    objc-method-signature:
18266      objc-selector
18267      objc-selector-seq
18268
18269    objc-selector-seq:
18270      objc-selector :
18271      objc-selector-seq objc-selector :
18272
18273   Returns a representation of the method selector.  */
18274
18275 static tree
18276 cp_parser_objc_selector_expression (cp_parser* parser)
18277 {
18278   tree sel_seq = NULL_TREE;
18279   bool maybe_unary_selector_p = true;
18280   cp_token *token;
18281
18282   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
18283   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18284   token = cp_lexer_peek_token (parser->lexer);
18285
18286   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18287          || token->type == CPP_SCOPE)
18288     {
18289       tree selector = NULL_TREE;
18290
18291       if (token->type != CPP_COLON
18292           || token->type == CPP_SCOPE)
18293         selector = cp_parser_objc_selector (parser);
18294
18295       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18296           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18297         {
18298           /* Detect if we have a unary selector.  */
18299           if (maybe_unary_selector_p)
18300             {
18301               sel_seq = selector;
18302               goto finish_selector;
18303             }
18304           else
18305             {
18306               cp_parser_error (parser, "expected %<:%>");
18307             }
18308         }
18309       maybe_unary_selector_p = false;
18310       token = cp_lexer_consume_token (parser->lexer);
18311
18312       if (token->type == CPP_SCOPE)
18313         {
18314           sel_seq
18315             = chainon (sel_seq,
18316                        build_tree_list (selector, NULL_TREE));
18317           sel_seq
18318             = chainon (sel_seq,
18319                        build_tree_list (NULL_TREE, NULL_TREE));
18320         }
18321       else
18322         sel_seq
18323           = chainon (sel_seq,
18324                      build_tree_list (selector, NULL_TREE));
18325
18326       token = cp_lexer_peek_token (parser->lexer);
18327     }
18328
18329  finish_selector:
18330   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18331
18332   return objc_build_selector_expr (sel_seq);
18333 }
18334
18335 /* Parse a list of identifiers.
18336
18337    objc-identifier-list:
18338      identifier
18339      objc-identifier-list , identifier
18340
18341    Returns a TREE_LIST of identifier nodes.  */
18342
18343 static tree
18344 cp_parser_objc_identifier_list (cp_parser* parser)
18345 {
18346   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18347   cp_token *sep = cp_lexer_peek_token (parser->lexer);
18348
18349   while (sep->type == CPP_COMMA)
18350     {
18351       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18352       list = chainon (list,
18353                       build_tree_list (NULL_TREE,
18354                                        cp_parser_identifier (parser)));
18355       sep = cp_lexer_peek_token (parser->lexer);
18356     }
18357
18358   return list;
18359 }
18360
18361 /* Parse an Objective-C alias declaration.
18362
18363    objc-alias-declaration:
18364      @compatibility_alias identifier identifier ;
18365
18366    This function registers the alias mapping with the Objective-C front end.
18367    It returns nothing.  */
18368
18369 static void
18370 cp_parser_objc_alias_declaration (cp_parser* parser)
18371 {
18372   tree alias, orig;
18373
18374   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
18375   alias = cp_parser_identifier (parser);
18376   orig = cp_parser_identifier (parser);
18377   objc_declare_alias (alias, orig);
18378   cp_parser_consume_semicolon_at_end_of_statement (parser);
18379 }
18380
18381 /* Parse an Objective-C class forward-declaration.
18382
18383    objc-class-declaration:
18384      @class objc-identifier-list ;
18385
18386    The function registers the forward declarations with the Objective-C
18387    front end.  It returns nothing.  */
18388
18389 static void
18390 cp_parser_objc_class_declaration (cp_parser* parser)
18391 {
18392   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
18393   objc_declare_class (cp_parser_objc_identifier_list (parser));
18394   cp_parser_consume_semicolon_at_end_of_statement (parser);
18395 }
18396
18397 /* Parse a list of Objective-C protocol references.
18398
18399    objc-protocol-refs-opt:
18400      objc-protocol-refs [opt]
18401
18402    objc-protocol-refs:
18403      < objc-identifier-list >
18404
18405    Returns a TREE_LIST of identifiers, if any.  */
18406
18407 static tree
18408 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18409 {
18410   tree protorefs = NULL_TREE;
18411
18412   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18413     {
18414       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
18415       protorefs = cp_parser_objc_identifier_list (parser);
18416       cp_parser_require (parser, CPP_GREATER, "`>'");
18417     }
18418
18419   return protorefs;
18420 }
18421
18422 /* Parse a Objective-C visibility specification.  */
18423
18424 static void
18425 cp_parser_objc_visibility_spec (cp_parser* parser)
18426 {
18427   cp_token *vis = cp_lexer_peek_token (parser->lexer);
18428
18429   switch (vis->keyword)
18430     {
18431     case RID_AT_PRIVATE:
18432       objc_set_visibility (2);
18433       break;
18434     case RID_AT_PROTECTED:
18435       objc_set_visibility (0);
18436       break;
18437     case RID_AT_PUBLIC:
18438       objc_set_visibility (1);
18439       break;
18440     default:
18441       return;
18442     }
18443
18444   /* Eat '@private'/'@protected'/'@public'.  */
18445   cp_lexer_consume_token (parser->lexer);
18446 }
18447
18448 /* Parse an Objective-C method type.  */
18449
18450 static void
18451 cp_parser_objc_method_type (cp_parser* parser)
18452 {
18453   objc_set_method_type
18454    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18455     ? PLUS_EXPR
18456     : MINUS_EXPR);
18457 }
18458
18459 /* Parse an Objective-C protocol qualifier.  */
18460
18461 static tree
18462 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18463 {
18464   tree quals = NULL_TREE, node;
18465   cp_token *token = cp_lexer_peek_token (parser->lexer);
18466
18467   node = token->u.value;
18468
18469   while (node && TREE_CODE (node) == IDENTIFIER_NODE
18470          && (node == ridpointers [(int) RID_IN]
18471              || node == ridpointers [(int) RID_OUT]
18472              || node == ridpointers [(int) RID_INOUT]
18473              || node == ridpointers [(int) RID_BYCOPY]
18474              || node == ridpointers [(int) RID_BYREF]
18475              || node == ridpointers [(int) RID_ONEWAY]))
18476     {
18477       quals = tree_cons (NULL_TREE, node, quals);
18478       cp_lexer_consume_token (parser->lexer);
18479       token = cp_lexer_peek_token (parser->lexer);
18480       node = token->u.value;
18481     }
18482
18483   return quals;
18484 }
18485
18486 /* Parse an Objective-C typename.  */
18487
18488 static tree
18489 cp_parser_objc_typename (cp_parser* parser)
18490 {
18491   tree typename = NULL_TREE;
18492
18493   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18494     {
18495       tree proto_quals, cp_type = NULL_TREE;
18496
18497       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
18498       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18499
18500       /* An ObjC type name may consist of just protocol qualifiers, in which
18501          case the type shall default to 'id'.  */
18502       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18503         cp_type = cp_parser_type_id (parser);
18504
18505       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18506       typename = build_tree_list (proto_quals, cp_type);
18507     }
18508
18509   return typename;
18510 }
18511
18512 /* Check to see if TYPE refers to an Objective-C selector name.  */
18513
18514 static bool
18515 cp_parser_objc_selector_p (enum cpp_ttype type)
18516 {
18517   return (type == CPP_NAME || type == CPP_KEYWORD
18518           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18519           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18520           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18521           || type == CPP_XOR || type == CPP_XOR_EQ);
18522 }
18523
18524 /* Parse an Objective-C selector.  */
18525
18526 static tree
18527 cp_parser_objc_selector (cp_parser* parser)
18528 {
18529   cp_token *token = cp_lexer_consume_token (parser->lexer);
18530
18531   if (!cp_parser_objc_selector_p (token->type))
18532     {
18533       error ("invalid Objective-C++ selector name");
18534       return error_mark_node;
18535     }
18536
18537   /* C++ operator names are allowed to appear in ObjC selectors.  */
18538   switch (token->type)
18539     {
18540     case CPP_AND_AND: return get_identifier ("and");
18541     case CPP_AND_EQ: return get_identifier ("and_eq");
18542     case CPP_AND: return get_identifier ("bitand");
18543     case CPP_OR: return get_identifier ("bitor");
18544     case CPP_COMPL: return get_identifier ("compl");
18545     case CPP_NOT: return get_identifier ("not");
18546     case CPP_NOT_EQ: return get_identifier ("not_eq");
18547     case CPP_OR_OR: return get_identifier ("or");
18548     case CPP_OR_EQ: return get_identifier ("or_eq");
18549     case CPP_XOR: return get_identifier ("xor");
18550     case CPP_XOR_EQ: return get_identifier ("xor_eq");
18551     default: return token->u.value;
18552     }
18553 }
18554
18555 /* Parse an Objective-C params list.  */
18556
18557 static tree
18558 cp_parser_objc_method_keyword_params (cp_parser* parser)
18559 {
18560   tree params = NULL_TREE;
18561   bool maybe_unary_selector_p = true;
18562   cp_token *token = cp_lexer_peek_token (parser->lexer);
18563
18564   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18565     {
18566       tree selector = NULL_TREE, typename, identifier;
18567
18568       if (token->type != CPP_COLON)
18569         selector = cp_parser_objc_selector (parser);
18570
18571       /* Detect if we have a unary selector.  */
18572       if (maybe_unary_selector_p
18573           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18574         return selector;
18575
18576       maybe_unary_selector_p = false;
18577       cp_parser_require (parser, CPP_COLON, "`:'");
18578       typename = cp_parser_objc_typename (parser);
18579       identifier = cp_parser_identifier (parser);
18580
18581       params
18582         = chainon (params,
18583                    objc_build_keyword_decl (selector,
18584                                             typename,
18585                                             identifier));
18586
18587       token = cp_lexer_peek_token (parser->lexer);
18588     }
18589
18590   return params;
18591 }
18592
18593 /* Parse the non-keyword Objective-C params.  */
18594
18595 static tree
18596 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18597 {
18598   tree params = make_node (TREE_LIST);
18599   cp_token *token = cp_lexer_peek_token (parser->lexer);
18600   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
18601
18602   while (token->type == CPP_COMMA)
18603     {
18604       cp_parameter_declarator *parmdecl;
18605       tree parm;
18606
18607       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18608       token = cp_lexer_peek_token (parser->lexer);
18609
18610       if (token->type == CPP_ELLIPSIS)
18611         {
18612           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
18613           *ellipsisp = true;
18614           break;
18615         }
18616
18617       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18618       parm = grokdeclarator (parmdecl->declarator,
18619                              &parmdecl->decl_specifiers,
18620                              PARM, /*initialized=*/0,
18621                              /*attrlist=*/NULL);
18622
18623       chainon (params, build_tree_list (NULL_TREE, parm));
18624       token = cp_lexer_peek_token (parser->lexer);
18625     }
18626
18627   return params;
18628 }
18629
18630 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
18631
18632 static void
18633 cp_parser_objc_interstitial_code (cp_parser* parser)
18634 {
18635   cp_token *token = cp_lexer_peek_token (parser->lexer);
18636
18637   /* If the next token is `extern' and the following token is a string
18638      literal, then we have a linkage specification.  */
18639   if (token->keyword == RID_EXTERN
18640       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18641     cp_parser_linkage_specification (parser);
18642   /* Handle #pragma, if any.  */
18643   else if (token->type == CPP_PRAGMA)
18644     cp_parser_pragma (parser, pragma_external);
18645   /* Allow stray semicolons.  */
18646   else if (token->type == CPP_SEMICOLON)
18647     cp_lexer_consume_token (parser->lexer);
18648   /* Finally, try to parse a block-declaration, or a function-definition.  */
18649   else
18650     cp_parser_block_declaration (parser, /*statement_p=*/false);
18651 }
18652
18653 /* Parse a method signature.  */
18654
18655 static tree
18656 cp_parser_objc_method_signature (cp_parser* parser)
18657 {
18658   tree rettype, kwdparms, optparms;
18659   bool ellipsis = false;
18660
18661   cp_parser_objc_method_type (parser);
18662   rettype = cp_parser_objc_typename (parser);
18663   kwdparms = cp_parser_objc_method_keyword_params (parser);
18664   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18665
18666   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18667 }
18668
18669 /* Pars an Objective-C method prototype list.  */
18670
18671 static void
18672 cp_parser_objc_method_prototype_list (cp_parser* parser)
18673 {
18674   cp_token *token = cp_lexer_peek_token (parser->lexer);
18675
18676   while (token->keyword != RID_AT_END)
18677     {
18678       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18679         {
18680           objc_add_method_declaration
18681            (cp_parser_objc_method_signature (parser));
18682           cp_parser_consume_semicolon_at_end_of_statement (parser);
18683         }
18684       else
18685         /* Allow for interspersed non-ObjC++ code.  */
18686         cp_parser_objc_interstitial_code (parser);
18687
18688       token = cp_lexer_peek_token (parser->lexer);
18689     }
18690
18691   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18692   objc_finish_interface ();
18693 }
18694
18695 /* Parse an Objective-C method definition list.  */
18696
18697 static void
18698 cp_parser_objc_method_definition_list (cp_parser* parser)
18699 {
18700   cp_token *token = cp_lexer_peek_token (parser->lexer);
18701
18702   while (token->keyword != RID_AT_END)
18703     {
18704       tree meth;
18705
18706       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18707         {
18708           push_deferring_access_checks (dk_deferred);
18709           objc_start_method_definition
18710            (cp_parser_objc_method_signature (parser));
18711
18712           /* For historical reasons, we accept an optional semicolon.  */
18713           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18714             cp_lexer_consume_token (parser->lexer);
18715
18716           perform_deferred_access_checks ();
18717           stop_deferring_access_checks ();
18718           meth = cp_parser_function_definition_after_declarator (parser,
18719                                                                  false);
18720           pop_deferring_access_checks ();
18721           objc_finish_method_definition (meth);
18722         }
18723       else
18724         /* Allow for interspersed non-ObjC++ code.  */
18725         cp_parser_objc_interstitial_code (parser);
18726
18727       token = cp_lexer_peek_token (parser->lexer);
18728     }
18729
18730   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18731   objc_finish_implementation ();
18732 }
18733
18734 /* Parse Objective-C ivars.  */
18735
18736 static void
18737 cp_parser_objc_class_ivars (cp_parser* parser)
18738 {
18739   cp_token *token = cp_lexer_peek_token (parser->lexer);
18740
18741   if (token->type != CPP_OPEN_BRACE)
18742     return;     /* No ivars specified.  */
18743
18744   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
18745   token = cp_lexer_peek_token (parser->lexer);
18746
18747   while (token->type != CPP_CLOSE_BRACE)
18748     {
18749       cp_decl_specifier_seq declspecs;
18750       int decl_class_or_enum_p;
18751       tree prefix_attributes;
18752
18753       cp_parser_objc_visibility_spec (parser);
18754
18755       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18756         break;
18757
18758       cp_parser_decl_specifier_seq (parser,
18759                                     CP_PARSER_FLAGS_OPTIONAL,
18760                                     &declspecs,
18761                                     &decl_class_or_enum_p);
18762       prefix_attributes = declspecs.attributes;
18763       declspecs.attributes = NULL_TREE;
18764
18765       /* Keep going until we hit the `;' at the end of the
18766          declaration.  */
18767       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18768         {
18769           tree width = NULL_TREE, attributes, first_attribute, decl;
18770           cp_declarator *declarator = NULL;
18771           int ctor_dtor_or_conv_p;
18772
18773           /* Check for a (possibly unnamed) bitfield declaration.  */
18774           token = cp_lexer_peek_token (parser->lexer);
18775           if (token->type == CPP_COLON)
18776             goto eat_colon;
18777
18778           if (token->type == CPP_NAME
18779               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18780                   == CPP_COLON))
18781             {
18782               /* Get the name of the bitfield.  */
18783               declarator = make_id_declarator (NULL_TREE,
18784                                                cp_parser_identifier (parser),
18785                                                sfk_none);
18786
18787              eat_colon:
18788               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
18789               /* Get the width of the bitfield.  */
18790               width
18791                 = cp_parser_constant_expression (parser,
18792                                                  /*allow_non_constant=*/false,
18793                                                  NULL);
18794             }
18795           else
18796             {
18797               /* Parse the declarator.  */
18798               declarator
18799                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18800                                         &ctor_dtor_or_conv_p,
18801                                         /*parenthesized_p=*/NULL,
18802                                         /*member_p=*/false);
18803             }
18804
18805           /* Look for attributes that apply to the ivar.  */
18806           attributes = cp_parser_attributes_opt (parser);
18807           /* Remember which attributes are prefix attributes and
18808              which are not.  */
18809           first_attribute = attributes;
18810           /* Combine the attributes.  */
18811           attributes = chainon (prefix_attributes, attributes);
18812
18813           if (width)
18814             {
18815               /* Create the bitfield declaration.  */
18816               decl = grokbitfield (declarator, &declspecs, width);
18817               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18818             }
18819           else
18820             decl = grokfield (declarator, &declspecs,
18821                               NULL_TREE, /*init_const_expr_p=*/false,
18822                               NULL_TREE, attributes);
18823
18824           /* Add the instance variable.  */
18825           objc_add_instance_variable (decl);
18826
18827           /* Reset PREFIX_ATTRIBUTES.  */
18828           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18829             attributes = TREE_CHAIN (attributes);
18830           if (attributes)
18831             TREE_CHAIN (attributes) = NULL_TREE;
18832
18833           token = cp_lexer_peek_token (parser->lexer);
18834
18835           if (token->type == CPP_COMMA)
18836             {
18837               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
18838               continue;
18839             }
18840           break;
18841         }
18842
18843       cp_parser_consume_semicolon_at_end_of_statement (parser);
18844       token = cp_lexer_peek_token (parser->lexer);
18845     }
18846
18847   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
18848   /* For historical reasons, we accept an optional semicolon.  */
18849   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18850     cp_lexer_consume_token (parser->lexer);
18851 }
18852
18853 /* Parse an Objective-C protocol declaration.  */
18854
18855 static void
18856 cp_parser_objc_protocol_declaration (cp_parser* parser)
18857 {
18858   tree proto, protorefs;
18859   cp_token *tok;
18860
18861   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
18862   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
18863     {
18864       error ("identifier expected after %<@protocol%>");
18865       goto finish;
18866     }
18867
18868   /* See if we have a forward declaration or a definition.  */
18869   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
18870
18871   /* Try a forward declaration first.  */
18872   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
18873     {
18874       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
18875      finish:
18876       cp_parser_consume_semicolon_at_end_of_statement (parser);
18877     }
18878
18879   /* Ok, we got a full-fledged definition (or at least should).  */
18880   else
18881     {
18882       proto = cp_parser_identifier (parser);
18883       protorefs = cp_parser_objc_protocol_refs_opt (parser);
18884       objc_start_protocol (proto, protorefs);
18885       cp_parser_objc_method_prototype_list (parser);
18886     }
18887 }
18888
18889 /* Parse an Objective-C superclass or category.  */
18890
18891 static void
18892 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
18893                                                           tree *categ)
18894 {
18895   cp_token *next = cp_lexer_peek_token (parser->lexer);
18896
18897   *super = *categ = NULL_TREE;
18898   if (next->type == CPP_COLON)
18899     {
18900       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
18901       *super = cp_parser_identifier (parser);
18902     }
18903   else if (next->type == CPP_OPEN_PAREN)
18904     {
18905       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
18906       *categ = cp_parser_identifier (parser);
18907       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18908     }
18909 }
18910
18911 /* Parse an Objective-C class interface.  */
18912
18913 static void
18914 cp_parser_objc_class_interface (cp_parser* parser)
18915 {
18916   tree name, super, categ, protos;
18917
18918   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
18919   name = cp_parser_identifier (parser);
18920   cp_parser_objc_superclass_or_category (parser, &super, &categ);
18921   protos = cp_parser_objc_protocol_refs_opt (parser);
18922
18923   /* We have either a class or a category on our hands.  */
18924   if (categ)
18925     objc_start_category_interface (name, categ, protos);
18926   else
18927     {
18928       objc_start_class_interface (name, super, protos);
18929       /* Handle instance variable declarations, if any.  */
18930       cp_parser_objc_class_ivars (parser);
18931       objc_continue_interface ();
18932     }
18933
18934   cp_parser_objc_method_prototype_list (parser);
18935 }
18936
18937 /* Parse an Objective-C class implementation.  */
18938
18939 static void
18940 cp_parser_objc_class_implementation (cp_parser* parser)
18941 {
18942   tree name, super, categ;
18943
18944   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
18945   name = cp_parser_identifier (parser);
18946   cp_parser_objc_superclass_or_category (parser, &super, &categ);
18947
18948   /* We have either a class or a category on our hands.  */
18949   if (categ)
18950     objc_start_category_implementation (name, categ);
18951   else
18952     {
18953       objc_start_class_implementation (name, super);
18954       /* Handle instance variable declarations, if any.  */
18955       cp_parser_objc_class_ivars (parser);
18956       objc_continue_implementation ();
18957     }
18958
18959   cp_parser_objc_method_definition_list (parser);
18960 }
18961
18962 /* Consume the @end token and finish off the implementation.  */
18963
18964 static void
18965 cp_parser_objc_end_implementation (cp_parser* parser)
18966 {
18967   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
18968   objc_finish_implementation ();
18969 }
18970
18971 /* Parse an Objective-C declaration.  */
18972
18973 static void
18974 cp_parser_objc_declaration (cp_parser* parser)
18975 {
18976   /* Try to figure out what kind of declaration is present.  */
18977   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18978
18979   switch (kwd->keyword)
18980     {
18981     case RID_AT_ALIAS:
18982       cp_parser_objc_alias_declaration (parser);
18983       break;
18984     case RID_AT_CLASS:
18985       cp_parser_objc_class_declaration (parser);
18986       break;
18987     case RID_AT_PROTOCOL:
18988       cp_parser_objc_protocol_declaration (parser);
18989       break;
18990     case RID_AT_INTERFACE:
18991       cp_parser_objc_class_interface (parser);
18992       break;
18993     case RID_AT_IMPLEMENTATION:
18994       cp_parser_objc_class_implementation (parser);
18995       break;
18996     case RID_AT_END:
18997       cp_parser_objc_end_implementation (parser);
18998       break;
18999     default:
19000       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19001       cp_parser_skip_to_end_of_block_or_statement (parser);
19002     }
19003 }
19004
19005 /* Parse an Objective-C try-catch-finally statement.
19006
19007    objc-try-catch-finally-stmt:
19008      @try compound-statement objc-catch-clause-seq [opt]
19009        objc-finally-clause [opt]
19010
19011    objc-catch-clause-seq:
19012      objc-catch-clause objc-catch-clause-seq [opt]
19013
19014    objc-catch-clause:
19015      @catch ( exception-declaration ) compound-statement
19016
19017    objc-finally-clause
19018      @finally compound-statement
19019
19020    Returns NULL_TREE.  */
19021
19022 static tree
19023 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19024   location_t location;
19025   tree stmt;
19026
19027   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
19028   location = cp_lexer_peek_token (parser->lexer)->location;
19029   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19030      node, lest it get absorbed into the surrounding block.  */
19031   stmt = push_stmt_list ();
19032   cp_parser_compound_statement (parser, NULL, false);
19033   objc_begin_try_stmt (location, pop_stmt_list (stmt));
19034
19035   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19036     {
19037       cp_parameter_declarator *parmdecl;
19038       tree parm;
19039
19040       cp_lexer_consume_token (parser->lexer);
19041       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19042       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19043       parm = grokdeclarator (parmdecl->declarator,
19044                              &parmdecl->decl_specifiers,
19045                              PARM, /*initialized=*/0,
19046                              /*attrlist=*/NULL);
19047       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19048       objc_begin_catch_clause (parm);
19049       cp_parser_compound_statement (parser, NULL, false);
19050       objc_finish_catch_clause ();
19051     }
19052
19053   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19054     {
19055       cp_lexer_consume_token (parser->lexer);
19056       location = cp_lexer_peek_token (parser->lexer)->location;
19057       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19058          node, lest it get absorbed into the surrounding block.  */
19059       stmt = push_stmt_list ();
19060       cp_parser_compound_statement (parser, NULL, false);
19061       objc_build_finally_clause (location, pop_stmt_list (stmt));
19062     }
19063
19064   return objc_finish_try_stmt ();
19065 }
19066
19067 /* Parse an Objective-C synchronized statement.
19068
19069    objc-synchronized-stmt:
19070      @synchronized ( expression ) compound-statement
19071
19072    Returns NULL_TREE.  */
19073
19074 static tree
19075 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19076   location_t location;
19077   tree lock, stmt;
19078
19079   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
19080
19081   location = cp_lexer_peek_token (parser->lexer)->location;
19082   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19083   lock = cp_parser_expression (parser, false);
19084   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19085
19086   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19087      node, lest it get absorbed into the surrounding block.  */
19088   stmt = push_stmt_list ();
19089   cp_parser_compound_statement (parser, NULL, false);
19090
19091   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19092 }
19093
19094 /* Parse an Objective-C throw statement.
19095
19096    objc-throw-stmt:
19097      @throw assignment-expression [opt] ;
19098
19099    Returns a constructed '@throw' statement.  */
19100
19101 static tree
19102 cp_parser_objc_throw_statement (cp_parser *parser) {
19103   tree expr = NULL_TREE;
19104
19105   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
19106
19107   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19108     expr = cp_parser_assignment_expression (parser, false);
19109
19110   cp_parser_consume_semicolon_at_end_of_statement (parser);
19111
19112   return objc_build_throw_stmt (expr);
19113 }
19114
19115 /* Parse an Objective-C statement.  */
19116
19117 static tree
19118 cp_parser_objc_statement (cp_parser * parser) {
19119   /* Try to figure out what kind of declaration is present.  */
19120   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19121
19122   switch (kwd->keyword)
19123     {
19124     case RID_AT_TRY:
19125       return cp_parser_objc_try_catch_finally_statement (parser);
19126     case RID_AT_SYNCHRONIZED:
19127       return cp_parser_objc_synchronized_statement (parser);
19128     case RID_AT_THROW:
19129       return cp_parser_objc_throw_statement (parser);
19130     default:
19131       error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19132       cp_parser_skip_to_end_of_block_or_statement (parser);
19133     }
19134
19135   return error_mark_node;
19136 }
19137 \f
19138 /* OpenMP 2.5 parsing routines.  */
19139
19140 /* Returns name of the next clause.
19141    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19142    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
19143    returned and the token is consumed.  */
19144
19145 static pragma_omp_clause
19146 cp_parser_omp_clause_name (cp_parser *parser)
19147 {
19148   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19149
19150   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19151     result = PRAGMA_OMP_CLAUSE_IF;
19152   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19153     result = PRAGMA_OMP_CLAUSE_DEFAULT;
19154   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19155     result = PRAGMA_OMP_CLAUSE_PRIVATE;
19156   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19157     {
19158       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19159       const char *p = IDENTIFIER_POINTER (id);
19160
19161       switch (p[0])
19162         {
19163         case 'c':
19164           if (!strcmp ("copyin", p))
19165             result = PRAGMA_OMP_CLAUSE_COPYIN;
19166           else if (!strcmp ("copyprivate", p))
19167             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19168           break;
19169         case 'f':
19170           if (!strcmp ("firstprivate", p))
19171             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19172           break;
19173         case 'l':
19174           if (!strcmp ("lastprivate", p))
19175             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19176           break;
19177         case 'n':
19178           if (!strcmp ("nowait", p))
19179             result = PRAGMA_OMP_CLAUSE_NOWAIT;
19180           else if (!strcmp ("num_threads", p))
19181             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19182           break;
19183         case 'o':
19184           if (!strcmp ("ordered", p))
19185             result = PRAGMA_OMP_CLAUSE_ORDERED;
19186           break;
19187         case 'r':
19188           if (!strcmp ("reduction", p))
19189             result = PRAGMA_OMP_CLAUSE_REDUCTION;
19190           break;
19191         case 's':
19192           if (!strcmp ("schedule", p))
19193             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19194           else if (!strcmp ("shared", p))
19195             result = PRAGMA_OMP_CLAUSE_SHARED;
19196           break;
19197         }
19198     }
19199
19200   if (result != PRAGMA_OMP_CLAUSE_NONE)
19201     cp_lexer_consume_token (parser->lexer);
19202
19203   return result;
19204 }
19205
19206 /* Validate that a clause of the given type does not already exist.  */
19207
19208 static void
19209 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
19210 {
19211   tree c;
19212
19213   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19214     if (OMP_CLAUSE_CODE (c) == code)
19215       {
19216         error ("too many %qs clauses", name);
19217         break;
19218       }
19219 }
19220
19221 /* OpenMP 2.5:
19222    variable-list:
19223      identifier
19224      variable-list , identifier
19225
19226    In addition, we match a closing parenthesis.  An opening parenthesis
19227    will have been consumed by the caller.
19228
19229    If KIND is nonzero, create the appropriate node and install the decl
19230    in OMP_CLAUSE_DECL and add the node to the head of the list.
19231
19232    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19233    return the list created.  */
19234
19235 static tree
19236 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19237                                 tree list)
19238 {
19239   while (1)
19240     {
19241       tree name, decl;
19242
19243       name = cp_parser_id_expression (parser, /*template_p=*/false,
19244                                       /*check_dependency_p=*/true,
19245                                       /*template_p=*/NULL,
19246                                       /*declarator_p=*/false,
19247                                       /*optional_p=*/false);
19248       if (name == error_mark_node)
19249         goto skip_comma;
19250
19251       decl = cp_parser_lookup_name_simple (parser, name);
19252       if (decl == error_mark_node)
19253         cp_parser_name_lookup_error (parser, name, decl, NULL);
19254       else if (kind != 0)
19255         {
19256           tree u = build_omp_clause (kind);
19257           OMP_CLAUSE_DECL (u) = decl;
19258           OMP_CLAUSE_CHAIN (u) = list;
19259           list = u;
19260         }
19261       else
19262         list = tree_cons (decl, NULL_TREE, list);
19263
19264     get_comma:
19265       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19266         break;
19267       cp_lexer_consume_token (parser->lexer);
19268     }
19269
19270   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19271     {
19272       int ending;
19273
19274       /* Try to resync to an unnested comma.  Copied from
19275          cp_parser_parenthesized_expression_list.  */
19276     skip_comma:
19277       ending = cp_parser_skip_to_closing_parenthesis (parser,
19278                                                       /*recovering=*/true,
19279                                                       /*or_comma=*/true,
19280                                                       /*consume_paren=*/true);
19281       if (ending < 0)
19282         goto get_comma;
19283     }
19284
19285   return list;
19286 }
19287
19288 /* Similarly, but expect leading and trailing parenthesis.  This is a very
19289    common case for omp clauses.  */
19290
19291 static tree
19292 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19293 {
19294   if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19295     return cp_parser_omp_var_list_no_open (parser, kind, list);
19296   return list;
19297 }
19298
19299 /* OpenMP 2.5:
19300    default ( shared | none ) */
19301
19302 static tree
19303 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19304 {
19305   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19306   tree c;
19307
19308   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19309     return list;
19310   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19311     {
19312       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19313       const char *p = IDENTIFIER_POINTER (id);
19314
19315       switch (p[0])
19316         {
19317         case 'n':
19318           if (strcmp ("none", p) != 0)
19319             goto invalid_kind;
19320           kind = OMP_CLAUSE_DEFAULT_NONE;
19321           break;
19322
19323         case 's':
19324           if (strcmp ("shared", p) != 0)
19325             goto invalid_kind;
19326           kind = OMP_CLAUSE_DEFAULT_SHARED;
19327           break;
19328
19329         default:
19330           goto invalid_kind;
19331         }
19332
19333       cp_lexer_consume_token (parser->lexer);
19334     }
19335   else
19336     {
19337     invalid_kind:
19338       cp_parser_error (parser, "expected %<none%> or %<shared%>");
19339     }
19340
19341   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19342     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19343                                            /*or_comma=*/false,
19344                                            /*consume_paren=*/true);
19345
19346   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19347     return list;
19348
19349   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19350   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19351   OMP_CLAUSE_CHAIN (c) = list;
19352   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19353
19354   return c;
19355 }
19356
19357 /* OpenMP 2.5:
19358    if ( expression ) */
19359
19360 static tree
19361 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19362 {
19363   tree t, c;
19364
19365   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19366     return list;
19367
19368   t = cp_parser_condition (parser);
19369
19370   if (t == error_mark_node
19371       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19372     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19373                                            /*or_comma=*/false,
19374                                            /*consume_paren=*/true);
19375
19376   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19377
19378   c = build_omp_clause (OMP_CLAUSE_IF);
19379   OMP_CLAUSE_IF_EXPR (c) = t;
19380   OMP_CLAUSE_CHAIN (c) = list;
19381
19382   return c;
19383 }
19384
19385 /* OpenMP 2.5:
19386    nowait */
19387
19388 static tree
19389 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19390 {
19391   tree c;
19392
19393   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19394
19395   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19396   OMP_CLAUSE_CHAIN (c) = list;
19397   return c;
19398 }
19399
19400 /* OpenMP 2.5:
19401    num_threads ( expression ) */
19402
19403 static tree
19404 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19405 {
19406   tree t, c;
19407
19408   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19409     return list;
19410
19411   t = cp_parser_expression (parser, false);
19412
19413   if (t == error_mark_node
19414       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19415     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19416                                            /*or_comma=*/false,
19417                                            /*consume_paren=*/true);
19418
19419   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19420
19421   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19422   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19423   OMP_CLAUSE_CHAIN (c) = list;
19424
19425   return c;
19426 }
19427
19428 /* OpenMP 2.5:
19429    ordered */
19430
19431 static tree
19432 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19433 {
19434   tree c;
19435
19436   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19437
19438   c = build_omp_clause (OMP_CLAUSE_ORDERED);
19439   OMP_CLAUSE_CHAIN (c) = list;
19440   return c;
19441 }
19442
19443 /* OpenMP 2.5:
19444    reduction ( reduction-operator : variable-list )
19445
19446    reduction-operator:
19447      One of: + * - & ^ | && || */
19448
19449 static tree
19450 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19451 {
19452   enum tree_code code;
19453   tree nlist, c;
19454
19455   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19456     return list;
19457
19458   switch (cp_lexer_peek_token (parser->lexer)->type)
19459     {
19460     case CPP_PLUS:
19461       code = PLUS_EXPR;
19462       break;
19463     case CPP_MULT:
19464       code = MULT_EXPR;
19465       break;
19466     case CPP_MINUS:
19467       code = MINUS_EXPR;
19468       break;
19469     case CPP_AND:
19470       code = BIT_AND_EXPR;
19471       break;
19472     case CPP_XOR:
19473       code = BIT_XOR_EXPR;
19474       break;
19475     case CPP_OR:
19476       code = BIT_IOR_EXPR;
19477       break;
19478     case CPP_AND_AND:
19479       code = TRUTH_ANDIF_EXPR;
19480       break;
19481     case CPP_OR_OR:
19482       code = TRUTH_ORIF_EXPR;
19483       break;
19484     default:
19485       cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19486     resync_fail:
19487       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19488                                              /*or_comma=*/false,
19489                                              /*consume_paren=*/true);
19490       return list;
19491     }
19492   cp_lexer_consume_token (parser->lexer);
19493
19494   if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19495     goto resync_fail;
19496
19497   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19498   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19499     OMP_CLAUSE_REDUCTION_CODE (c) = code;
19500
19501   return nlist;
19502 }
19503
19504 /* OpenMP 2.5:
19505    schedule ( schedule-kind )
19506    schedule ( schedule-kind , expression )
19507
19508    schedule-kind:
19509      static | dynamic | guided | runtime  */
19510
19511 static tree
19512 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19513 {
19514   tree c, t;
19515
19516   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19517     return list;
19518
19519   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19520
19521   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19522     {
19523       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19524       const char *p = IDENTIFIER_POINTER (id);
19525
19526       switch (p[0])
19527         {
19528         case 'd':
19529           if (strcmp ("dynamic", p) != 0)
19530             goto invalid_kind;
19531           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19532           break;
19533
19534         case 'g':
19535           if (strcmp ("guided", p) != 0)
19536             goto invalid_kind;
19537           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19538           break;
19539
19540         case 'r':
19541           if (strcmp ("runtime", p) != 0)
19542             goto invalid_kind;
19543           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19544           break;
19545
19546         default:
19547           goto invalid_kind;
19548         }
19549     }
19550   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19551     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19552   else
19553     goto invalid_kind;
19554   cp_lexer_consume_token (parser->lexer);
19555
19556   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19557     {
19558       cp_lexer_consume_token (parser->lexer);
19559
19560       t = cp_parser_assignment_expression (parser, false);
19561
19562       if (t == error_mark_node)
19563         goto resync_fail;
19564       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19565         error ("schedule %<runtime%> does not take "
19566                "a %<chunk_size%> parameter");
19567       else
19568         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19569
19570       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19571         goto resync_fail;
19572     }
19573   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19574     goto resync_fail;
19575
19576   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19577   OMP_CLAUSE_CHAIN (c) = list;
19578   return c;
19579
19580  invalid_kind:
19581   cp_parser_error (parser, "invalid schedule kind");
19582  resync_fail:
19583   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19584                                          /*or_comma=*/false,
19585                                          /*consume_paren=*/true);
19586   return list;
19587 }
19588
19589 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
19590    is a bitmask in MASK.  Return the list of clauses found; the result
19591    of clause default goes in *pdefault.  */
19592
19593 static tree
19594 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19595                            const char *where, cp_token *pragma_tok)
19596 {
19597   tree clauses = NULL;
19598
19599   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19600     {
19601       pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
19602       const char *c_name;
19603       tree prev = clauses;
19604
19605       switch (c_kind)
19606         {
19607         case PRAGMA_OMP_CLAUSE_COPYIN:
19608           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19609           c_name = "copyin";
19610           break;
19611         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19612           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19613                                             clauses);
19614           c_name = "copyprivate";
19615           break;
19616         case PRAGMA_OMP_CLAUSE_DEFAULT:
19617           clauses = cp_parser_omp_clause_default (parser, clauses);
19618           c_name = "default";
19619           break;
19620         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19621           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19622                                             clauses);
19623           c_name = "firstprivate";
19624           break;
19625         case PRAGMA_OMP_CLAUSE_IF:
19626           clauses = cp_parser_omp_clause_if (parser, clauses);
19627           c_name = "if";
19628           break;
19629         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19630           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19631                                             clauses);
19632           c_name = "lastprivate";
19633           break;
19634         case PRAGMA_OMP_CLAUSE_NOWAIT:
19635           clauses = cp_parser_omp_clause_nowait (parser, clauses);
19636           c_name = "nowait";
19637           break;
19638         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19639           clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19640           c_name = "num_threads";
19641           break;
19642         case PRAGMA_OMP_CLAUSE_ORDERED:
19643           clauses = cp_parser_omp_clause_ordered (parser, clauses);
19644           c_name = "ordered";
19645           break;
19646         case PRAGMA_OMP_CLAUSE_PRIVATE:
19647           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19648                                             clauses);
19649           c_name = "private";
19650           break;
19651         case PRAGMA_OMP_CLAUSE_REDUCTION:
19652           clauses = cp_parser_omp_clause_reduction (parser, clauses);
19653           c_name = "reduction";
19654           break;
19655         case PRAGMA_OMP_CLAUSE_SCHEDULE:
19656           clauses = cp_parser_omp_clause_schedule (parser, clauses);
19657           c_name = "schedule";
19658           break;
19659         case PRAGMA_OMP_CLAUSE_SHARED:
19660           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19661                                             clauses);
19662           c_name = "shared";
19663           break;
19664         default:
19665           cp_parser_error (parser, "expected %<#pragma omp%> clause");
19666           goto saw_error;
19667         }
19668
19669       if (((mask >> c_kind) & 1) == 0)
19670         {
19671           /* Remove the invalid clause(s) from the list to avoid
19672              confusing the rest of the compiler.  */
19673           clauses = prev;
19674           error ("%qs is not valid for %qs", c_name, where);
19675         }
19676     }
19677  saw_error:
19678   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19679   return finish_omp_clauses (clauses);
19680 }
19681
19682 /* OpenMP 2.5:
19683    structured-block:
19684      statement
19685
19686    In practice, we're also interested in adding the statement to an
19687    outer node.  So it is convenient if we work around the fact that
19688    cp_parser_statement calls add_stmt.  */
19689
19690 static unsigned
19691 cp_parser_begin_omp_structured_block (cp_parser *parser)
19692 {
19693   unsigned save = parser->in_statement;
19694
19695   /* Only move the values to IN_OMP_BLOCK if they weren't false.
19696      This preserves the "not within loop or switch" style error messages
19697      for nonsense cases like
19698         void foo() {
19699         #pragma omp single
19700           break;
19701         }
19702   */
19703   if (parser->in_statement)
19704     parser->in_statement = IN_OMP_BLOCK;
19705
19706   return save;
19707 }
19708
19709 static void
19710 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19711 {
19712   parser->in_statement = save;
19713 }
19714
19715 static tree
19716 cp_parser_omp_structured_block (cp_parser *parser)
19717 {
19718   tree stmt = begin_omp_structured_block ();
19719   unsigned int save = cp_parser_begin_omp_structured_block (parser);
19720
19721   cp_parser_statement (parser, NULL_TREE, false, NULL);
19722
19723   cp_parser_end_omp_structured_block (parser, save);
19724   return finish_omp_structured_block (stmt);
19725 }
19726
19727 /* OpenMP 2.5:
19728    # pragma omp atomic new-line
19729      expression-stmt
19730
19731    expression-stmt:
19732      x binop= expr | x++ | ++x | x-- | --x
19733    binop:
19734      +, *, -, /, &, ^, |, <<, >>
19735
19736   where x is an lvalue expression with scalar type.  */
19737
19738 static void
19739 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19740 {
19741   tree lhs, rhs;
19742   enum tree_code code;
19743
19744   cp_parser_require_pragma_eol (parser, pragma_tok);
19745
19746   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19747                                     /*cast_p=*/false);
19748   switch (TREE_CODE (lhs))
19749     {
19750     case ERROR_MARK:
19751       goto saw_error;
19752
19753     case PREINCREMENT_EXPR:
19754     case POSTINCREMENT_EXPR:
19755       lhs = TREE_OPERAND (lhs, 0);
19756       code = PLUS_EXPR;
19757       rhs = integer_one_node;
19758       break;
19759
19760     case PREDECREMENT_EXPR:
19761     case POSTDECREMENT_EXPR:
19762       lhs = TREE_OPERAND (lhs, 0);
19763       code = MINUS_EXPR;
19764       rhs = integer_one_node;
19765       break;
19766
19767     default:
19768       switch (cp_lexer_peek_token (parser->lexer)->type)
19769         {
19770         case CPP_MULT_EQ:
19771           code = MULT_EXPR;
19772           break;
19773         case CPP_DIV_EQ:
19774           code = TRUNC_DIV_EXPR;
19775           break;
19776         case CPP_PLUS_EQ:
19777           code = PLUS_EXPR;
19778           break;
19779         case CPP_MINUS_EQ:
19780           code = MINUS_EXPR;
19781           break;
19782         case CPP_LSHIFT_EQ:
19783           code = LSHIFT_EXPR;
19784           break;
19785         case CPP_RSHIFT_EQ:
19786           code = RSHIFT_EXPR;
19787           break;
19788         case CPP_AND_EQ:
19789           code = BIT_AND_EXPR;
19790           break;
19791         case CPP_OR_EQ:
19792           code = BIT_IOR_EXPR;
19793           break;
19794         case CPP_XOR_EQ:
19795           code = BIT_XOR_EXPR;
19796           break;
19797         default:
19798           cp_parser_error (parser,
19799                            "invalid operator for %<#pragma omp atomic%>");
19800           goto saw_error;
19801         }
19802       cp_lexer_consume_token (parser->lexer);
19803
19804       rhs = cp_parser_expression (parser, false);
19805       if (rhs == error_mark_node)
19806         goto saw_error;
19807       break;
19808     }
19809   finish_omp_atomic (code, lhs, rhs);
19810   cp_parser_consume_semicolon_at_end_of_statement (parser);
19811   return;
19812
19813  saw_error:
19814   cp_parser_skip_to_end_of_block_or_statement (parser);
19815 }
19816
19817
19818 /* OpenMP 2.5:
19819    # pragma omp barrier new-line  */
19820
19821 static void
19822 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19823 {
19824   cp_parser_require_pragma_eol (parser, pragma_tok);
19825   finish_omp_barrier ();
19826 }
19827
19828 /* OpenMP 2.5:
19829    # pragma omp critical [(name)] new-line
19830      structured-block  */
19831
19832 static tree
19833 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19834 {
19835   tree stmt, name = NULL;
19836
19837   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19838     {
19839       cp_lexer_consume_token (parser->lexer);
19840
19841       name = cp_parser_identifier (parser);
19842
19843       if (name == error_mark_node
19844           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19845         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19846                                                /*or_comma=*/false,
19847                                                /*consume_paren=*/true);
19848       if (name == error_mark_node)
19849         name = NULL;
19850     }
19851   cp_parser_require_pragma_eol (parser, pragma_tok);
19852
19853   stmt = cp_parser_omp_structured_block (parser);
19854   return c_finish_omp_critical (stmt, name);
19855 }
19856
19857 /* OpenMP 2.5:
19858    # pragma omp flush flush-vars[opt] new-line
19859
19860    flush-vars:
19861      ( variable-list ) */
19862
19863 static void
19864 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
19865 {
19866   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19867     (void) cp_parser_omp_var_list (parser, 0, NULL);
19868   cp_parser_require_pragma_eol (parser, pragma_tok);
19869
19870   finish_omp_flush ();
19871 }
19872
19873 /* Parse the restricted form of the for statment allowed by OpenMP.  */
19874
19875 static tree
19876 cp_parser_omp_for_loop (cp_parser *parser)
19877 {
19878   tree init, cond, incr, body, decl, pre_body;
19879   location_t loc;
19880
19881   if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19882     {
19883       cp_parser_error (parser, "for statement expected");
19884       return NULL;
19885     }
19886   loc = cp_lexer_consume_token (parser->lexer)->location;
19887   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19888     return NULL;
19889
19890   init = decl = NULL;
19891   pre_body = push_stmt_list ();
19892   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19893     {
19894       cp_decl_specifier_seq type_specifiers;
19895
19896       /* First, try to parse as an initialized declaration.  See
19897          cp_parser_condition, from whence the bulk of this is copied.  */
19898
19899       cp_parser_parse_tentatively (parser);
19900       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
19901                                     &type_specifiers);
19902       if (!cp_parser_error_occurred (parser))
19903         {
19904           tree asm_specification, attributes;
19905           cp_declarator *declarator;
19906
19907           declarator = cp_parser_declarator (parser,
19908                                              CP_PARSER_DECLARATOR_NAMED,
19909                                              /*ctor_dtor_or_conv_p=*/NULL,
19910                                              /*parenthesized_p=*/NULL,
19911                                              /*member_p=*/false);
19912           attributes = cp_parser_attributes_opt (parser);
19913           asm_specification = cp_parser_asm_specification_opt (parser);
19914
19915           cp_parser_require (parser, CPP_EQ, "`='");
19916           if (cp_parser_parse_definitely (parser))
19917             {
19918               tree pushed_scope;
19919
19920               decl = start_decl (declarator, &type_specifiers,
19921                                  /*initialized_p=*/false, attributes,
19922                                  /*prefix_attributes=*/NULL_TREE,
19923                                  &pushed_scope);
19924
19925               init = cp_parser_assignment_expression (parser, false);
19926
19927               cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
19928                               asm_specification, LOOKUP_ONLYCONVERTING);
19929
19930               if (pushed_scope)
19931                 pop_scope (pushed_scope);
19932             }
19933         }
19934       else
19935         cp_parser_abort_tentative_parse (parser);
19936
19937       /* If parsing as an initialized declaration failed, try again as
19938          a simple expression.  */
19939       if (decl == NULL)
19940         init = cp_parser_expression (parser, false);
19941     }
19942   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19943   pre_body = pop_stmt_list (pre_body);
19944
19945   cond = NULL;
19946   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19947     cond = cp_parser_condition (parser);
19948   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19949
19950   incr = NULL;
19951   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19952     incr = cp_parser_expression (parser, false);
19953
19954   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19955     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19956                                            /*or_comma=*/false,
19957                                            /*consume_paren=*/true);
19958
19959   /* Note that we saved the original contents of this flag when we entered
19960      the structured block, and so we don't need to re-save it here.  */
19961   parser->in_statement = IN_OMP_FOR;
19962
19963   /* Note that the grammar doesn't call for a structured block here,
19964      though the loop as a whole is a structured block.  */
19965   body = push_stmt_list ();
19966   cp_parser_statement (parser, NULL_TREE, false, NULL);
19967   body = pop_stmt_list (body);
19968
19969   return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19970 }
19971
19972 /* OpenMP 2.5:
19973    #pragma omp for for-clause[optseq] new-line
19974      for-loop  */
19975
19976 #define OMP_FOR_CLAUSE_MASK                             \
19977         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
19978         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
19979         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
19980         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
19981         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
19982         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
19983         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19984
19985 static tree
19986 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19987 {
19988   tree clauses, sb, ret;
19989   unsigned int save;
19990
19991   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19992                                        "#pragma omp for", pragma_tok);
19993
19994   sb = begin_omp_structured_block ();
19995   save = cp_parser_begin_omp_structured_block (parser);
19996
19997   ret = cp_parser_omp_for_loop (parser);
19998   if (ret)
19999     OMP_FOR_CLAUSES (ret) = clauses;
20000
20001   cp_parser_end_omp_structured_block (parser, save);
20002   add_stmt (finish_omp_structured_block (sb));
20003
20004   return ret;
20005 }
20006
20007 /* OpenMP 2.5:
20008    # pragma omp master new-line
20009      structured-block  */
20010
20011 static tree
20012 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
20013 {
20014   cp_parser_require_pragma_eol (parser, pragma_tok);
20015   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
20016 }
20017
20018 /* OpenMP 2.5:
20019    # pragma omp ordered new-line
20020      structured-block  */
20021
20022 static tree
20023 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
20024 {
20025   cp_parser_require_pragma_eol (parser, pragma_tok);
20026   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
20027 }
20028
20029 /* OpenMP 2.5:
20030
20031    section-scope:
20032      { section-sequence }
20033
20034    section-sequence:
20035      section-directive[opt] structured-block
20036      section-sequence section-directive structured-block  */
20037
20038 static tree
20039 cp_parser_omp_sections_scope (cp_parser *parser)
20040 {
20041   tree stmt, substmt;
20042   bool error_suppress = false;
20043   cp_token *tok;
20044
20045   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
20046     return NULL_TREE;
20047
20048   stmt = push_stmt_list ();
20049
20050   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
20051     {
20052       unsigned save;
20053
20054       substmt = begin_omp_structured_block ();
20055       save = cp_parser_begin_omp_structured_block (parser);
20056
20057       while (1)
20058         {
20059           cp_parser_statement (parser, NULL_TREE, false, NULL);
20060
20061           tok = cp_lexer_peek_token (parser->lexer);
20062           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20063             break;
20064           if (tok->type == CPP_CLOSE_BRACE)
20065             break;
20066           if (tok->type == CPP_EOF)
20067             break;
20068         }
20069
20070       cp_parser_end_omp_structured_block (parser, save);
20071       substmt = finish_omp_structured_block (substmt);
20072       substmt = build1 (OMP_SECTION, void_type_node, substmt);
20073       add_stmt (substmt);
20074     }
20075
20076   while (1)
20077     {
20078       tok = cp_lexer_peek_token (parser->lexer);
20079       if (tok->type == CPP_CLOSE_BRACE)
20080         break;
20081       if (tok->type == CPP_EOF)
20082         break;
20083
20084       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20085         {
20086           cp_lexer_consume_token (parser->lexer);
20087           cp_parser_require_pragma_eol (parser, tok);
20088           error_suppress = false;
20089         }
20090       else if (!error_suppress)
20091         {
20092           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
20093           error_suppress = true;
20094         }
20095
20096       substmt = cp_parser_omp_structured_block (parser);
20097       substmt = build1 (OMP_SECTION, void_type_node, substmt);
20098       add_stmt (substmt);
20099     }
20100   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
20101
20102   substmt = pop_stmt_list (stmt);
20103
20104   stmt = make_node (OMP_SECTIONS);
20105   TREE_TYPE (stmt) = void_type_node;
20106   OMP_SECTIONS_BODY (stmt) = substmt;
20107
20108   add_stmt (stmt);
20109   return stmt;
20110 }
20111
20112 /* OpenMP 2.5:
20113    # pragma omp sections sections-clause[optseq] newline
20114      sections-scope  */
20115
20116 #define OMP_SECTIONS_CLAUSE_MASK                        \
20117         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20118         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20119         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
20120         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
20121         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20122
20123 static tree
20124 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
20125 {
20126   tree clauses, ret;
20127
20128   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
20129                                        "#pragma omp sections", pragma_tok);
20130
20131   ret = cp_parser_omp_sections_scope (parser);
20132   if (ret)
20133     OMP_SECTIONS_CLAUSES (ret) = clauses;
20134
20135   return ret;
20136 }
20137
20138 /* OpenMP 2.5:
20139    # pragma parallel parallel-clause new-line
20140    # pragma parallel for parallel-for-clause new-line
20141    # pragma parallel sections parallel-sections-clause new-line  */
20142
20143 #define OMP_PARALLEL_CLAUSE_MASK                        \
20144         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
20145         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20146         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20147         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
20148         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
20149         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
20150         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
20151         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
20152
20153 static tree
20154 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
20155 {
20156   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
20157   const char *p_name = "#pragma omp parallel";
20158   tree stmt, clauses, par_clause, ws_clause, block;
20159   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
20160   unsigned int save;
20161
20162   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20163     {
20164       cp_lexer_consume_token (parser->lexer);
20165       p_kind = PRAGMA_OMP_PARALLEL_FOR;
20166       p_name = "#pragma omp parallel for";
20167       mask |= OMP_FOR_CLAUSE_MASK;
20168       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20169     }
20170   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20171     {
20172       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20173       const char *p = IDENTIFIER_POINTER (id);
20174       if (strcmp (p, "sections") == 0)
20175         {
20176           cp_lexer_consume_token (parser->lexer);
20177           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
20178           p_name = "#pragma omp parallel sections";
20179           mask |= OMP_SECTIONS_CLAUSE_MASK;
20180           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20181         }
20182     }
20183
20184   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
20185   block = begin_omp_parallel ();
20186   save = cp_parser_begin_omp_structured_block (parser);
20187
20188   switch (p_kind)
20189     {
20190     case PRAGMA_OMP_PARALLEL:
20191       cp_parser_already_scoped_statement (parser);
20192       par_clause = clauses;
20193       break;
20194
20195     case PRAGMA_OMP_PARALLEL_FOR:
20196       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20197       stmt = cp_parser_omp_for_loop (parser);
20198       if (stmt)
20199         OMP_FOR_CLAUSES (stmt) = ws_clause;
20200       break;
20201
20202     case PRAGMA_OMP_PARALLEL_SECTIONS:
20203       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20204       stmt = cp_parser_omp_sections_scope (parser);
20205       if (stmt)
20206         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
20207       break;
20208
20209     default:
20210       gcc_unreachable ();
20211     }
20212
20213   cp_parser_end_omp_structured_block (parser, save);
20214   stmt = finish_omp_parallel (par_clause, block);
20215   if (p_kind != PRAGMA_OMP_PARALLEL)
20216     OMP_PARALLEL_COMBINED (stmt) = 1;
20217   return stmt;
20218 }
20219
20220 /* OpenMP 2.5:
20221    # pragma omp single single-clause[optseq] new-line
20222      structured-block  */
20223
20224 #define OMP_SINGLE_CLAUSE_MASK                          \
20225         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
20226         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
20227         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
20228         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20229
20230 static tree
20231 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
20232 {
20233   tree stmt = make_node (OMP_SINGLE);
20234   TREE_TYPE (stmt) = void_type_node;
20235
20236   OMP_SINGLE_CLAUSES (stmt)
20237     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
20238                                  "#pragma omp single", pragma_tok);
20239   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
20240
20241   return add_stmt (stmt);
20242 }
20243
20244 /* OpenMP 2.5:
20245    # pragma omp threadprivate (variable-list) */
20246
20247 static void
20248 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20249 {
20250   tree vars;
20251
20252   vars = cp_parser_omp_var_list (parser, 0, NULL);
20253   cp_parser_require_pragma_eol (parser, pragma_tok);
20254
20255   finish_omp_threadprivate (vars);
20256 }
20257
20258 /* Main entry point to OpenMP statement pragmas.  */
20259
20260 static void
20261 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20262 {
20263   tree stmt;
20264
20265   switch (pragma_tok->pragma_kind)
20266     {
20267     case PRAGMA_OMP_ATOMIC:
20268       cp_parser_omp_atomic (parser, pragma_tok);
20269       return;
20270     case PRAGMA_OMP_CRITICAL:
20271       stmt = cp_parser_omp_critical (parser, pragma_tok);
20272       break;
20273     case PRAGMA_OMP_FOR:
20274       stmt = cp_parser_omp_for (parser, pragma_tok);
20275       break;
20276     case PRAGMA_OMP_MASTER:
20277       stmt = cp_parser_omp_master (parser, pragma_tok);
20278       break;
20279     case PRAGMA_OMP_ORDERED:
20280       stmt = cp_parser_omp_ordered (parser, pragma_tok);
20281       break;
20282     case PRAGMA_OMP_PARALLEL:
20283       stmt = cp_parser_omp_parallel (parser, pragma_tok);
20284       break;
20285     case PRAGMA_OMP_SECTIONS:
20286       stmt = cp_parser_omp_sections (parser, pragma_tok);
20287       break;
20288     case PRAGMA_OMP_SINGLE:
20289       stmt = cp_parser_omp_single (parser, pragma_tok);
20290       break;
20291     default:
20292       gcc_unreachable ();
20293     }
20294
20295   if (stmt)
20296     SET_EXPR_LOCATION (stmt, pragma_tok->location);
20297 }
20298 \f
20299 /* The parser.  */
20300
20301 static GTY (()) cp_parser *the_parser;
20302
20303 \f
20304 /* Special handling for the first token or line in the file.  The first
20305    thing in the file might be #pragma GCC pch_preprocess, which loads a
20306    PCH file, which is a GC collection point.  So we need to handle this
20307    first pragma without benefit of an existing lexer structure.
20308
20309    Always returns one token to the caller in *FIRST_TOKEN.  This is
20310    either the true first token of the file, or the first token after
20311    the initial pragma.  */
20312
20313 static void
20314 cp_parser_initial_pragma (cp_token *first_token)
20315 {
20316   tree name = NULL;
20317
20318   cp_lexer_get_preprocessor_token (NULL, first_token);
20319   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20320     return;
20321
20322   cp_lexer_get_preprocessor_token (NULL, first_token);
20323   if (first_token->type == CPP_STRING)
20324     {
20325       name = first_token->u.value;
20326
20327       cp_lexer_get_preprocessor_token (NULL, first_token);
20328       if (first_token->type != CPP_PRAGMA_EOL)
20329         error ("junk at end of %<#pragma GCC pch_preprocess%>");
20330     }
20331   else
20332     error ("expected string literal");
20333
20334   /* Skip to the end of the pragma.  */
20335   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20336     cp_lexer_get_preprocessor_token (NULL, first_token);
20337
20338   /* Now actually load the PCH file.  */
20339   if (name)
20340     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20341
20342   /* Read one more token to return to our caller.  We have to do this
20343      after reading the PCH file in, since its pointers have to be
20344      live.  */
20345   cp_lexer_get_preprocessor_token (NULL, first_token);
20346 }
20347
20348 /* Normal parsing of a pragma token.  Here we can (and must) use the
20349    regular lexer.  */
20350
20351 static bool
20352 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20353 {
20354   cp_token *pragma_tok;
20355   unsigned int id;
20356
20357   pragma_tok = cp_lexer_consume_token (parser->lexer);
20358   gcc_assert (pragma_tok->type == CPP_PRAGMA);
20359   parser->lexer->in_pragma = true;
20360
20361   id = pragma_tok->pragma_kind;
20362   switch (id)
20363     {
20364     case PRAGMA_GCC_PCH_PREPROCESS:
20365       error ("%<#pragma GCC pch_preprocess%> must be first");
20366       break;
20367
20368     case PRAGMA_OMP_BARRIER:
20369       switch (context)
20370         {
20371         case pragma_compound:
20372           cp_parser_omp_barrier (parser, pragma_tok);
20373           return false;
20374         case pragma_stmt:
20375           error ("%<#pragma omp barrier%> may only be "
20376                  "used in compound statements");
20377           break;
20378         default:
20379           goto bad_stmt;
20380         }
20381       break;
20382
20383     case PRAGMA_OMP_FLUSH:
20384       switch (context)
20385         {
20386         case pragma_compound:
20387           cp_parser_omp_flush (parser, pragma_tok);
20388           return false;
20389         case pragma_stmt:
20390           error ("%<#pragma omp flush%> may only be "
20391                  "used in compound statements");
20392           break;
20393         default:
20394           goto bad_stmt;
20395         }
20396       break;
20397
20398     case PRAGMA_OMP_THREADPRIVATE:
20399       cp_parser_omp_threadprivate (parser, pragma_tok);
20400       return false;
20401
20402     case PRAGMA_OMP_ATOMIC:
20403     case PRAGMA_OMP_CRITICAL:
20404     case PRAGMA_OMP_FOR:
20405     case PRAGMA_OMP_MASTER:
20406     case PRAGMA_OMP_ORDERED:
20407     case PRAGMA_OMP_PARALLEL:
20408     case PRAGMA_OMP_SECTIONS:
20409     case PRAGMA_OMP_SINGLE:
20410       if (context == pragma_external)
20411         goto bad_stmt;
20412       cp_parser_omp_construct (parser, pragma_tok);
20413       return true;
20414
20415     case PRAGMA_OMP_SECTION:
20416       error ("%<#pragma omp section%> may only be used in "
20417              "%<#pragma omp sections%> construct");
20418       break;
20419
20420     default:
20421       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20422       c_invoke_pragma_handler (id);
20423       break;
20424
20425     bad_stmt:
20426       cp_parser_error (parser, "expected declaration specifiers");
20427       break;
20428     }
20429
20430   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20431   return false;
20432 }
20433
20434 /* The interface the pragma parsers have to the lexer.  */
20435
20436 enum cpp_ttype
20437 pragma_lex (tree *value)
20438 {
20439   cp_token *tok;
20440   enum cpp_ttype ret;
20441
20442   tok = cp_lexer_peek_token (the_parser->lexer);
20443
20444   ret = tok->type;
20445   *value = tok->u.value;
20446
20447   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20448     ret = CPP_EOF;
20449   else if (ret == CPP_STRING)
20450     *value = cp_parser_string_literal (the_parser, false, false);
20451   else
20452     {
20453       cp_lexer_consume_token (the_parser->lexer);
20454       if (ret == CPP_KEYWORD)
20455         ret = CPP_NAME;
20456     }
20457
20458   return ret;
20459 }
20460
20461 \f
20462 /* External interface.  */
20463
20464 /* Parse one entire translation unit.  */
20465
20466 void
20467 c_parse_file (void)
20468 {
20469   bool error_occurred;
20470   static bool already_called = false;
20471
20472   if (already_called)
20473     {
20474       sorry ("inter-module optimizations not implemented for C++");
20475       return;
20476     }
20477   already_called = true;
20478
20479   the_parser = cp_parser_new ();
20480   push_deferring_access_checks (flag_access_control
20481                                 ? dk_no_deferred : dk_no_check);
20482   error_occurred = cp_parser_translation_unit (the_parser);
20483   the_parser = NULL;
20484 }
20485
20486 #include "gt-cp-parser.h"